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