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), text: *str); |
31 | |
32 | gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), index_: 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 (str: label_text); |
48 | gtk_widget_set_halign (widget: label, align: GTK_ALIGN_START); |
49 | gtk_widget_set_valign (widget: label, align: GTK_ALIGN_BASELINE); |
50 | gtk_widget_set_hexpand (widget: label, TRUE); |
51 | gtk_grid_attach (grid: table, child: label, column: 0, row, width: 1, height: 1); |
52 | |
53 | combo_box = create_combo_box (strings: options); |
54 | gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget: combo_box); |
55 | gtk_widget_set_halign (widget: combo_box, align: GTK_ALIGN_END); |
56 | gtk_widget_set_valign (widget: combo_box, align: GTK_ALIGN_BASELINE); |
57 | gtk_size_group_add_widget (size_group, widget: combo_box); |
58 | gtk_grid_attach (grid: table, child: combo_box, column: 1, row, width: 1, height: 1); |
59 | } |
60 | |
61 | static void |
62 | toggle_grouping (GtkCheckButton *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_check_button_get_active (self: 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, mode: 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 (); |
104 | gtk_window_set_display (GTK_WINDOW (window), display: gtk_widget_get_display (widget: do_widget)); |
105 | gtk_window_set_title (GTK_WINDOW (window), title: "Size Groups" ); |
106 | gtk_window_set_resizable (GTK_WINDOW (window), FALSE); |
107 | g_object_add_weak_pointer (G_OBJECT (window), weak_pointer_location: (gpointer *)&window); |
108 | |
109 | vbox = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 5); |
110 | gtk_widget_set_margin_start (widget: vbox, margin: 5); |
111 | gtk_widget_set_margin_end (widget: vbox, margin: 5); |
112 | gtk_widget_set_margin_top (widget: vbox, margin: 5); |
113 | gtk_widget_set_margin_bottom (widget: vbox, margin: 5); |
114 | gtk_window_set_child (GTK_WINDOW (window), child: vbox); |
115 | |
116 | size_group = gtk_size_group_new (mode: GTK_SIZE_GROUP_HORIZONTAL); |
117 | g_object_set_data_full (G_OBJECT (window), key: "size-group" , data: size_group, destroy: g_object_unref); |
118 | |
119 | /* Create one frame holding color options */ |
120 | frame = gtk_frame_new (label: "Color Options" ); |
121 | gtk_box_append (GTK_BOX (vbox), child: frame); |
122 | |
123 | table = gtk_grid_new (); |
124 | gtk_widget_set_margin_start (widget: table, margin: 5); |
125 | gtk_widget_set_margin_end (widget: table, margin: 5); |
126 | gtk_widget_set_margin_top (widget: table, margin: 5); |
127 | gtk_widget_set_margin_bottom (widget: table, margin: 5); |
128 | gtk_grid_set_row_spacing (GTK_GRID (table), spacing: 5); |
129 | gtk_grid_set_column_spacing (GTK_GRID (table), spacing: 10); |
130 | gtk_frame_set_child (GTK_FRAME (frame), child: table); |
131 | |
132 | add_row (GTK_GRID (table), row: 0, size_group, label_text: "_Foreground" , options: color_options); |
133 | add_row (GTK_GRID (table), row: 1, size_group, label_text: "_Background" , options: color_options); |
134 | |
135 | /* And another frame holding line style options */ |
136 | frame = gtk_frame_new (label: "Line Options" ); |
137 | gtk_box_append (GTK_BOX (vbox), child: frame); |
138 | |
139 | table = gtk_grid_new (); |
140 | gtk_widget_set_margin_start (widget: table, margin: 5); |
141 | gtk_widget_set_margin_end (widget: table, margin: 5); |
142 | gtk_widget_set_margin_top (widget: table, margin: 5); |
143 | gtk_widget_set_margin_bottom (widget: table, margin: 5); |
144 | gtk_grid_set_row_spacing (GTK_GRID (table), spacing: 5); |
145 | gtk_grid_set_column_spacing (GTK_GRID (table), spacing: 10); |
146 | gtk_frame_set_child (GTK_FRAME (frame), child: table); |
147 | |
148 | add_row (GTK_GRID (table), row: 0, size_group, label_text: "_Dashing" , options: dash_options); |
149 | add_row (GTK_GRID (table), row: 1, size_group, label_text: "_Line ends" , options: end_options); |
150 | |
151 | /* And a check button to turn grouping on and off */ |
152 | check_button = gtk_check_button_new_with_mnemonic (label: "_Enable grouping" ); |
153 | gtk_box_append (GTK_BOX (vbox), child: check_button); |
154 | |
155 | gtk_check_button_set_active (GTK_CHECK_BUTTON (check_button), TRUE); |
156 | g_signal_connect (check_button, "toggled" , |
157 | G_CALLBACK (toggle_grouping), size_group); |
158 | } |
159 | |
160 | if (!gtk_widget_get_visible (widget: window)) |
161 | gtk_widget_show (widget: window); |
162 | else |
163 | gtk_window_destroy (GTK_WINDOW (window)); |
164 | |
165 | return window; |
166 | } |
167 | |