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
16var
17  sg_window : PGtkWidget;
18
19
20const
21  color_options : array [0..3] of pchar = ('Red', 'Green', 'Blue', NULL);
22
23  dash_options  : array [0..3] of pchar = ('Solid', 'Dashed', 'Dotted', NULL);
24
25  end_options   : array [0..3] of pchar = ('Square', 'Round', 'Arrow', NULL);
26
27
28(* Convenience function to create an option menu holding a number of strings
29 *)
30
31function create_option_menu (strings : ppchar): PGtkWidget;
32var
33  menu,
34  menu_item,
35  option_menu : PGtkWidget;
36
37  str         : ppchar;
38
39begin
40  menu := gtk_menu_new ();
41
42  str := strings;
43  while str^ <> NULL do
44  begin
45    menu_item := gtk_menu_item_new_with_label ( str[0]);
46    gtk_widget_show (menu_item);
47
48    gtk_menu_shell_append (pGtkMenuShell(menu), menu_item);
49    inc(str);
50  end;
51
52  option_menu := gtk_option_menu_new ();
53  gtk_option_menu_set_menu (pGtkOptionMenu(option_menu), menu);
54
55  create_option_menu := option_menu;
56end;
57
58procedure add_row (table      : PGtkTable;
59                   row        : integer;
60                   size_group : PGtkSizeGroup;
61                   label_text : pchar;
62                   options    : ppchar);
63var
64  option_menu : PGtkWidget;
65  thelabel    : PGtkWidget;
66
67begin
68  thelabel := gtk_label_new_with_mnemonic (label_text);
69  gtk_misc_set_alignment (pGtkMisc(thelabel), 0, 1);
70  gtk_table_attach (pGtkTable(table), thelabel,
71                    0, 1,                   row, row + 1,
72                    GTK_EXPAND or GTK_FILL, 0,
73                    0,                      0);
74
75  option_menu := create_option_menu (options);
76  gtk_label_set_mnemonic_widget (pGtkLabel(thelabel), option_menu);
77  gtk_size_group_add_widget (size_group, option_menu);
78  gtk_table_attach (pGtkTable(table), option_menu,
79                    1, 2,                  row, row + 1,
80                    0,                     0,
81                    0,                     0);
82end;
83
84procedure toggle_grouping (check_button : PGtkToggleButton;
85                           size_group   : PGtkSizeGroup); cdecl;
86var
87  new_mode  : TGtkSizeGroupMode;
88
89begin
90  (* GTK_SIZE_GROUP_NONE is not generally useful, but is useful
91   * here to show the effect of GTK_SIZE_GROUP_HORIZONTAL by
92   * contrast.
93   *)
94
95  if gtk_toggle_button_get_active (check_button) then
96    new_mode := GTK_SIZE_GROUP_HORIZONTAL
97  else
98    new_mode := GTK_SIZE_GROUP_NONE;
99
100  gtk_size_group_set_mode (size_group, new_mode);
101end;
102
103
104
105
106function do_sizegroup      : PGtkWidget;
107var
108  table,
109  frame,
110  vbox,
111  check_button  : PGtkWidget;
112
113  size_group    : PGtkSizeGroup;
114
115begin
116  if sg_window = NULL then
117  begin
118    sg_window := gtk_dialog_new_with_buttons ('Size Groups',
119                        NULL, 0,
120                                            GTK_STOCK_CLOSE,
121                                           [ GTK_RESPONSE_NONE,
122                                            NULL]);
123
124    gtk_window_set_resizable (pGtkWindow(sg_window), FALSE);
125
126    g_signal_connect (sg_window, 'response',
127                        TGCallback(@gtk_widget_destroy), NULL);
128    g_signal_connect (sg_window, 'destroy',
129                        TGCallback(@gtk_widget_destroyed), @sg_window);
130
131    vbox := gtk_vbox_new (FALSE, 5);
132    gtk_box_pack_start (pGtkBox(pGtkDialog (sg_window)^.vbox), vbox, TRUE, TRUE, 0);
133    gtk_container_set_border_width (pGtkContainer(vbox), 5);
134
135    size_group := gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
136
137    (* Create one frame holding color options
138     *)
139    frame := gtk_frame_new ('Color Options');
140    gtk_box_pack_start (pGtkBox(vbox), frame, TRUE, TRUE, 0);
141
142    table := gtk_table_new (2, 2, FALSE);
143    gtk_container_set_border_width (pGtkContainer(table), 5);
144    gtk_table_set_row_spacings (pGtkTable(table), 5);
145    gtk_table_set_col_spacings (pGtkTable(table), 10);
146    gtk_container_add (pGtkContainer(frame), table);
147
148    add_row (pGtkTable(table), 0, size_group, '_Foreground', @color_options[0]);
149    add_row (pGtkTable(table), 1, size_group, '_Background', @color_options[0]);
150
151    (* And another frame holding line style options
152     *)
153    frame := gtk_frame_new ('Line Options');
154    gtk_box_pack_start (pGtkBox(vbox), frame, FALSE, FALSE, 0);
155
156    table := gtk_table_new (2, 2, FALSE);
157    gtk_container_set_border_width (pGtkContainer(table), 5);
158    gtk_table_set_row_spacings (pGtkTable(table), 5);
159    gtk_table_set_col_spacings (pGtkTable(table), 10);
160    gtk_container_add (pGtkContainer(frame), table);
161
162    add_row (pGtkTable(table), 0, size_group, '_Dashing', @dash_options[0]);
163    add_row (pGtkTable(table), 1, size_group, '_Line ends', @end_options[0]);
164
165    (* And a check button to turn grouping on and off *)
166    check_button := gtk_check_button_new_with_mnemonic ('_Enable grouping');
167    gtk_box_pack_start (pGtkBox(vbox), check_button, FALSE, FALSE, 0);
168
169    gtk_toggle_button_set_active (pGtkToggleButton(check_button), TRUE);
170    g_signal_connect (check_button, 'toggled',
171             TGCallback (@toggle_grouping), size_group);
172  end;
173
174  if not GTK_WIDGET_VISIBLE (sg_window) then
175    gtk_widget_show_all (sg_window)
176  else begin
177    gtk_widget_destroy (sg_window);
178    sg_window := NULL;
179  end;
180
181  do_sizegroup := sg_window;
182end;
183