]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/sizegroup.c
gtk-demo: add a test for a CSS accordion
[~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 a combo box holding a number of strings
21  */
22 GtkWidget *
23 create_combo_box (const char **strings)
24 {
25   GtkWidget *combo_box;
26   const char **str;
27
28   combo_box = gtk_combo_box_text_new ();
29
30   for (str = strings; *str; str++)
31     gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (combo_box), *str);
32
33   gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
34
35   return combo_box;
36 }
37
38 static void
39 add_row (GtkGrid      *table,
40          int           row,
41          GtkSizeGroup *size_group,
42          const char   *label_text,
43          const char  **options)
44 {
45   GtkWidget *combo_box;
46   GtkWidget *label;
47
48   label = gtk_label_new_with_mnemonic (label_text);
49   gtk_widget_set_halign (label, GTK_ALIGN_START);
50   gtk_widget_set_valign (label, GTK_ALIGN_END);
51   gtk_widget_set_hexpand (label, TRUE);
52   gtk_grid_attach (table, label, 0, row, 1, 1);
53
54   combo_box = create_combo_box (options);
55   gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo_box);
56   gtk_size_group_add_widget (size_group, combo_box);
57   gtk_grid_attach (table, combo_box, 1, row, 1, 1);
58 }
59
60 static void
61 toggle_grouping (GtkToggleButton *check_button,
62                  GtkSizeGroup    *size_group)
63 {
64   GtkSizeGroupMode new_mode;
65
66   /* GTK_SIZE_GROUP_NONE is not generally useful, but is useful
67    * here to show the effect of GTK_SIZE_GROUP_HORIZONTAL by
68    * contrast.
69    */
70   if (gtk_toggle_button_get_active (check_button))
71     new_mode = GTK_SIZE_GROUP_HORIZONTAL;
72   else
73     new_mode = GTK_SIZE_GROUP_NONE;
74
75   gtk_size_group_set_mode (size_group, new_mode);
76 }
77
78 GtkWidget *
79 do_sizegroup (GtkWidget *do_widget)
80 {
81   GtkWidget *content_area;
82   GtkWidget *table;
83   GtkWidget *frame;
84   GtkWidget *vbox;
85   GtkWidget *check_button;
86   GtkSizeGroup *size_group;
87
88   static const char *color_options[] = {
89     "Red", "Green", "Blue", NULL
90   };
91
92   static const char *dash_options[] = {
93     "Solid", "Dashed", "Dotted", NULL
94   };
95
96   static const char *end_options[] = {
97     "Square", "Round", "Arrow", NULL
98   };
99
100   if (!window)
101     {
102       window = gtk_dialog_new_with_buttons ("GtkSizeGroup",
103                                             GTK_WINDOW (do_widget),
104                                             0,
105                                             GTK_STOCK_CLOSE,
106                                             GTK_RESPONSE_NONE,
107                                             NULL);
108       gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
109
110       g_signal_connect (window, "response",
111                         G_CALLBACK (gtk_widget_destroy), NULL);
112       g_signal_connect (window, "destroy",
113                         G_CALLBACK (gtk_widget_destroyed), &window);
114
115       content_area = gtk_dialog_get_content_area (GTK_DIALOG (window));
116
117       vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
118       gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
119       gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
120
121       size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
122
123       /* Create one frame holding color options */
124       frame = gtk_frame_new ("Color Options");
125       gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
126
127       table = gtk_grid_new ();
128       gtk_container_set_border_width (GTK_CONTAINER (table), 5);
129       gtk_grid_set_row_spacing (GTK_GRID (table), 5);
130       gtk_grid_set_column_spacing (GTK_GRID (table), 10);
131       gtk_container_add (GTK_CONTAINER (frame), table);
132
133       add_row (GTK_GRID (table), 0, size_group, "_Foreground", color_options);
134       add_row (GTK_GRID (table), 1, size_group, "_Background", color_options);
135
136       /* And another frame holding line style options */
137       frame = gtk_frame_new ("Line Options");
138       gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
139
140       table = gtk_grid_new ();
141       gtk_container_set_border_width (GTK_CONTAINER (table), 5);
142       gtk_grid_set_row_spacing (GTK_GRID (table), 5);
143       gtk_grid_set_column_spacing (GTK_GRID (table), 10);
144       gtk_container_add (GTK_CONTAINER (frame), table);
145
146       add_row (GTK_GRID (table), 0, size_group, "_Dashing", dash_options);
147       add_row (GTK_GRID (table), 1, size_group, "_Line ends", end_options);
148
149       /* And a check button to turn grouping on and off */
150       check_button = gtk_check_button_new_with_mnemonic ("_Enable grouping");
151       gtk_box_pack_start (GTK_BOX (vbox), check_button, FALSE, FALSE, 0);
152
153       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check_button), TRUE);
154       g_signal_connect (check_button, "toggled",
155                         G_CALLBACK (toggle_grouping), size_group);
156     }
157
158   if (!gtk_widget_get_visible (window))
159     gtk_widget_show_all (window);
160   else
161     gtk_widget_destroy (window);
162
163   return window;
164 }