sizegroup.inc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. (* Size Groups
  2. *
  3. * GtkSizeGroup provides a mechanism for grouping a number of
  4. * widgets together so they all request the same amount of space.
  5. * This is typically useful when you want a column of widgets to
  6. * have the same size, but you can't use a GtkTable widget.
  7. *
  8. * Note that size groups only affect the amount of space requested,
  9. * not the size that the widgets finally receive. If you want the
  10. * widgets in a GtkSizeGroup to actually be the same size, you need
  11. * to pack them in such a way that they get the size they request
  12. * and not more. For example, if you are packing your widgets
  13. * into a table, you would not include the GTK_FILL flag.
  14. *)
  15. var
  16. sg_window : PGtkWidget;
  17. const
  18. color_options : array [0..3] of pchar = ('Red', 'Green', 'Blue', NULL);
  19. dash_options : array [0..3] of pchar = ('Solid', 'Dashed', 'Dotted', NULL);
  20. end_options : array [0..3] of pchar = ('Square', 'Round', 'Arrow', NULL);
  21. (* Convenience function to create an option menu holding a number of strings
  22. *)
  23. function create_option_menu (strings : ppchar): PGtkWidget;
  24. var
  25. menu,
  26. menu_item,
  27. option_menu : PGtkWidget;
  28. str : ppchar;
  29. begin
  30. menu := gtk_menu_new ();
  31. str := strings;
  32. while str^ <> NULL do
  33. begin
  34. menu_item := gtk_menu_item_new_with_label ( str[0]);
  35. gtk_widget_show (menu_item);
  36. gtk_menu_shell_append (pGtkMenuShell(menu), menu_item);
  37. inc(str);
  38. end;
  39. option_menu := gtk_option_menu_new ();
  40. gtk_option_menu_set_menu (pGtkOptionMenu(option_menu), menu);
  41. create_option_menu := option_menu;
  42. end;
  43. procedure add_row (table : PGtkTable;
  44. row : integer;
  45. size_group : PGtkSizeGroup;
  46. label_text : pchar;
  47. options : ppchar);
  48. var
  49. option_menu : PGtkWidget;
  50. thelabel : PGtkWidget;
  51. begin
  52. thelabel := gtk_label_new_with_mnemonic (label_text);
  53. gtk_misc_set_alignment (pGtkMisc(thelabel), 0, 1);
  54. gtk_table_attach (pGtkTable(table), thelabel,
  55. 0, 1, row, row + 1,
  56. GTK_EXPAND or GTK_FILL, 0,
  57. 0, 0);
  58. option_menu := create_option_menu (options);
  59. gtk_label_set_mnemonic_widget (pGtkLabel(thelabel), option_menu);
  60. gtk_size_group_add_widget (size_group, option_menu);
  61. gtk_table_attach (pGtkTable(table), option_menu,
  62. 1, 2, row, row + 1,
  63. 0, 0,
  64. 0, 0);
  65. end;
  66. procedure toggle_grouping (check_button : PGtkToggleButton;
  67. size_group : PGtkSizeGroup); cdecl;
  68. var
  69. new_mode : TGtkSizeGroupMode;
  70. begin
  71. (* GTK_SIZE_GROUP_NONE is not generally useful, but is useful
  72. * here to show the effect of GTK_SIZE_GROUP_HORIZONTAL by
  73. * contrast.
  74. *)
  75. if gtk_toggle_button_get_active (check_button) then
  76. new_mode := GTK_SIZE_GROUP_HORIZONTAL
  77. else
  78. new_mode := GTK_SIZE_GROUP_NONE;
  79. gtk_size_group_set_mode (size_group, new_mode);
  80. end;
  81. function do_sizegroup : PGtkWidget;
  82. var
  83. table,
  84. frame,
  85. vbox,
  86. check_button : PGtkWidget;
  87. size_group : PGtkSizeGroup;
  88. begin
  89. if sg_window = NULL then
  90. begin
  91. sg_window := gtk_dialog_new_with_buttons ('Size Groups',
  92. NULL, 0,
  93. GTK_STOCK_CLOSE,
  94. [ GTK_RESPONSE_NONE,
  95. NULL]);
  96. gtk_window_set_resizable (pGtkWindow(sg_window), FALSE);
  97. g_signal_connect (sg_window, 'response',
  98. TGCallback(@gtk_widget_destroy), NULL);
  99. g_signal_connect (sg_window, 'destroy',
  100. TGCallback(@gtk_widget_destroyed), @sg_window);
  101. vbox := gtk_vbox_new (FALSE, 5);
  102. gtk_box_pack_start (pGtkBox(pGtkDialog (sg_window)^.vbox), vbox, TRUE, TRUE, 0);
  103. gtk_container_set_border_width (pGtkContainer(vbox), 5);
  104. size_group := gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
  105. (* Create one frame holding color options
  106. *)
  107. frame := gtk_frame_new ('Color Options');
  108. gtk_box_pack_start (pGtkBox(vbox), frame, TRUE, TRUE, 0);
  109. table := gtk_table_new (2, 2, FALSE);
  110. gtk_container_set_border_width (pGtkContainer(table), 5);
  111. gtk_table_set_row_spacings (pGtkTable(table), 5);
  112. gtk_table_set_col_spacings (pGtkTable(table), 10);
  113. gtk_container_add (pGtkContainer(frame), table);
  114. add_row (pGtkTable(table), 0, size_group, '_Foreground', @color_options[0]);
  115. add_row (pGtkTable(table), 1, size_group, '_Background', @color_options[0]);
  116. (* And another frame holding line style options
  117. *)
  118. frame := gtk_frame_new ('Line Options');
  119. gtk_box_pack_start (pGtkBox(vbox), frame, FALSE, FALSE, 0);
  120. table := gtk_table_new (2, 2, FALSE);
  121. gtk_container_set_border_width (pGtkContainer(table), 5);
  122. gtk_table_set_row_spacings (pGtkTable(table), 5);
  123. gtk_table_set_col_spacings (pGtkTable(table), 10);
  124. gtk_container_add (pGtkContainer(frame), table);
  125. add_row (pGtkTable(table), 0, size_group, '_Dashing', @dash_options[0]);
  126. add_row (pGtkTable(table), 1, size_group, '_Line ends', @end_options[0]);
  127. (* And a check button to turn grouping on and off *)
  128. check_button := gtk_check_button_new_with_mnemonic ('_Enable grouping');
  129. gtk_box_pack_start (pGtkBox(vbox), check_button, FALSE, FALSE, 0);
  130. gtk_toggle_button_set_active (pGtkToggleButton(check_button), TRUE);
  131. g_signal_connect (check_button, 'toggled',
  132. TGCallback (@toggle_grouping), size_group);
  133. end;
  134. if not GTK_WIDGET_VISIBLE (sg_window) then
  135. gtk_widget_show_all (sg_window)
  136. else begin
  137. gtk_widget_destroy (sg_window);
  138. sg_window := NULL;
  139. end;
  140. do_sizegroup := sg_window;
  141. end;