Debian Codegraph @

demos/gtk-demo/sizegroup.c

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