]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/sizegroup.c
Make a GtkCellEditable (get_widget_window_size): Change to let it honor
[~andy/gtk] / demos / gtk-demo / sizegroup.c
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
16 #include <gtk/gtk.h>
17
18 static GtkWidget *window = NULL;
19
20 /* Convenience function to create an option menu holding a number of strings
21  */
22 GtkWidget *
23 create_option_menu (const char **strings)
24 {
25   GtkWidget *menu;
26   GtkWidget *option_menu;
27   const char **str;
28
29   menu = gtk_menu_new ();
30   
31   for (str = strings; *str; str++)
32     {
33       GtkWidget *menu_item = gtk_menu_item_new_with_label (*str);
34       gtk_widget_show (menu_item);
35
36       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
37     }
38
39   option_menu = gtk_option_menu_new ();
40   gtk_option_menu_set_menu (GTK_OPTION_MENU (option_menu), menu);
41
42   return option_menu;
43 }
44
45 static void
46 add_row (GtkTable     *table,
47          int           row,
48          GtkSizeGroup *size_group,
49          const char   *label_text,
50          const char  **options)
51 {
52   GtkWidget *option_menu;
53   GtkWidget *label;
54
55   label = gtk_label_new_with_mnemonic (label_text);
56   gtk_misc_set_alignment (GTK_MISC (label), 0, 1);
57   gtk_table_attach (GTK_TABLE (table), label,
58                     0, 1,                  row, row + 1,
59                     GTK_EXPAND | GTK_FILL, 0,
60                     0,                     0);
61   
62   option_menu = create_option_menu (options);
63   gtk_label_set_mnemonic_widget (GTK_LABEL (label), option_menu);
64   gtk_size_group_add_widget (size_group, option_menu);
65   gtk_table_attach (GTK_TABLE (table), option_menu,
66                     1, 2,                  row, row + 1,
67                     0,                     0,
68                     0,                     0);
69 }
70
71 static void
72 toggle_grouping (GtkToggleButton *check_button,
73                  GtkSizeGroup    *size_group)
74 {
75   GtkSizeGroupMode new_mode;
76
77   /* GTK_SIZE_GROUP_NONE is not generally useful, but is useful
78    * here to show the effect of GTK_SIZE_GROUP_HORIZONTAL by
79    * contrast.
80    */
81   if (gtk_toggle_button_get_active (check_button))
82     new_mode = GTK_SIZE_GROUP_HORIZONTAL;
83   else
84     new_mode = GTK_SIZE_GROUP_NONE;
85   
86   gtk_size_group_set_mode (size_group, new_mode);
87 }
88
89 GtkWidget *
90 do_sizegroup (void)
91 {
92   GtkWidget *table;
93   GtkWidget *frame;
94   GtkWidget *vbox;
95   GtkWidget *check_button;
96   GtkSizeGroup *size_group;
97
98   static const char *color_options[] = {
99     "Red", "Green", "Blue", NULL
100   };
101   
102   static const char *dash_options[] = {
103     "Solid", "Dashed", "Dotted", NULL
104   };
105   
106   static const char *end_options[] = {
107     "Square", "Round", "Arrow", NULL
108   };
109   
110   if (!window)
111     {
112       window = gtk_dialog_new_with_buttons ("GtkSizeGroup",
113                                             NULL, 0,
114                                             GTK_STOCK_CLOSE,
115                                             GTK_RESPONSE_NONE,
116                                             NULL);
117       gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
118       
119       gtk_signal_connect (GTK_OBJECT (window), "response",
120                           GTK_SIGNAL_FUNC (gtk_widget_destroy), NULL);
121       gtk_signal_connect (GTK_OBJECT (window), "destroy",
122                           GTK_SIGNAL_FUNC (gtk_widget_destroyed), &window);
123
124       vbox = gtk_vbox_new (FALSE, 5);
125       gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0);
126       gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
127
128       size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
129       
130       /* Create one frame holding color options
131        */
132       frame = gtk_frame_new ("Color Options");
133       gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
134
135       table = gtk_table_new (2, 2, FALSE);
136       gtk_container_set_border_width (GTK_CONTAINER (table), 5);
137       gtk_table_set_row_spacings (GTK_TABLE (table), 5);
138       gtk_table_set_col_spacings (GTK_TABLE (table), 10);
139       gtk_container_add (GTK_CONTAINER (frame), table);
140
141       add_row (GTK_TABLE (table), 0, size_group, "_Foreground", color_options);
142       add_row (GTK_TABLE (table), 1, size_group, "_Background", color_options);
143
144       /* And another frame holding line style options
145        */
146       frame = gtk_frame_new ("Line Options");
147       gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
148
149       table = gtk_table_new (2, 2, FALSE);
150       gtk_container_set_border_width (GTK_CONTAINER (table), 5);
151       gtk_table_set_row_spacings (GTK_TABLE (table), 5);
152       gtk_table_set_col_spacings (GTK_TABLE (table), 10);
153       gtk_container_add (GTK_CONTAINER (frame), table);
154
155       add_row (GTK_TABLE (table), 0, size_group, "_Dashing", dash_options);
156       add_row (GTK_TABLE (table), 1, size_group, "_Line ends", end_options);
157
158       /* And a check button to turn grouping on and off */
159       check_button = gtk_check_button_new_with_mnemonic ("_Enable grouping");
160       gtk_box_pack_start (GTK_BOX (vbox), check_button, FALSE, FALSE, 0);
161       
162       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check_button), TRUE);
163       gtk_signal_connect (GTK_OBJECT (check_button), "toggled",
164                           GTK_SIGNAL_FUNC (toggle_grouping), size_group);
165     }
166
167   if (!GTK_WIDGET_VISIBLE (window))
168     gtk_widget_show_all (window);
169   else
170     gtk_widget_destroy (window);
171
172   return window;
173 }