]> Pileus Git - ~andy/gtk/blob - examples/rangewidgets/rangewidgets.c
Use gtk_box_new() instead gtk_[v|h]box_new()
[~andy/gtk] / examples / rangewidgets / rangewidgets.c
1
2 #include <gtk/gtk.h>
3
4 GtkWidget *hscale, *vscale;
5
6 static void cb_pos_menu_select( GtkWidget       *item,
7                                 GtkPositionType  pos )
8 {
9     /* Set the value position on both scale widgets */
10     gtk_scale_set_value_pos (GTK_SCALE (hscale), pos);
11     gtk_scale_set_value_pos (GTK_SCALE (vscale), pos);
12 }
13
14 static void cb_update_menu_select( GtkWidget     *item,
15                                    GtkUpdateType  policy )
16 {
17     /* Set the update policy for both scale widgets */
18     gtk_range_set_update_policy (GTK_RANGE (hscale), policy);
19     gtk_range_set_update_policy (GTK_RANGE (vscale), policy);
20 }
21
22 static void cb_digits_scale( GtkAdjustment *adj )
23 {
24     /* Set the number of decimal places to which adj->value is rounded */
25     gtk_scale_set_digits (GTK_SCALE (hscale), (gint) adj->value);
26     gtk_scale_set_digits (GTK_SCALE (vscale), (gint) adj->value);
27 }
28
29 static void cb_page_size( GtkAdjustment *get,
30                           GtkAdjustment *set )
31 {
32     /* Set the page size and page increment size of the sample
33      * adjustment to the value specified by the "Page Size" scale */
34     set->page_size = get->value;
35     set->page_increment = get->value;
36
37     /* This sets the adjustment and makes it emit the "changed" signal to
38        reconfigure all the widgets that are attached to this signal.  */
39     gtk_adjustment_set_value (set, CLAMP (set->value,
40                                           set->lower,
41                                           (set->upper - set->page_size)));
42     g_signal_emit_by_name(G_OBJECT(set), "changed");
43 }
44
45 static void cb_draw_value( GtkToggleButton *button )
46 {
47     /* Turn the value display on the scale widgets off or on depending
48      *  on the state of the checkbutton */
49     gtk_scale_set_draw_value (GTK_SCALE (hscale), button->active);
50     gtk_scale_set_draw_value (GTK_SCALE (vscale), button->active);
51 }
52
53 /* Convenience functions */
54
55 static GtkWidget *make_menu_item ( gchar     *name,
56                                    GCallback  callback,
57                                    gpointer   data )
58 {
59     GtkWidget *item;
60
61     item = gtk_menu_item_new_with_label (name);
62     g_signal_connect (item, "activate",
63                       callback, (gpointer) data);
64     gtk_widget_show (item);
65
66     return item;
67 }
68
69 static void scale_set_default_values( GtkScale *scale )
70 {
71     gtk_range_set_update_policy (GTK_RANGE (scale),
72                                  GTK_UPDATE_CONTINUOUS);
73     gtk_scale_set_digits (scale, 1);
74     gtk_scale_set_value_pos (scale, GTK_POS_TOP);
75     gtk_scale_set_draw_value (scale, TRUE);
76 }
77
78 /* makes the sample window */
79
80 static void create_range_controls( void )
81 {
82     GtkWidget *window;
83     GtkWidget *box1, *box2, *box3;
84     GtkWidget *button;
85     GtkWidget *scrollbar;
86     GtkWidget *separator;
87     GtkWidget *opt, *menu, *item;
88     GtkWidget *label;
89     GtkWidget *scale;
90     GtkAdjustment *adj1, *adj2;
91
92     /* Standard window-creating stuff */
93     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
94     g_signal_connect (window, "destroy",
95                       G_CALLBACK (gtk_main_quit),
96                       NULL);
97     gtk_window_set_title (GTK_WINDOW (window), "range controls");
98
99     box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 0);
100     gtk_container_add (GTK_CONTAINER (window), box1);
101     gtk_widget_show (box1);
102
103     box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 10);
104     gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
105     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
106     gtk_widget_show (box2);
107
108     /* value, lower, upper, step_increment, page_increment, page_size */
109     /* Note that the page_size value only makes a difference for
110      * scrollbar widgets, and the highest value you'll get is actually
111      * (upper - page_size). */
112     adj1 = gtk_adjustment_new (0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
113
114     vscale = gtk_scale_new (GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT (adj1));
115     scale_set_default_values (GTK_SCALE (vscale));
116     gtk_box_pack_start (GTK_BOX (box2), vscale, TRUE, TRUE, 0);
117     gtk_widget_show (vscale);
118
119     box3 = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 10);
120     gtk_box_pack_start (GTK_BOX (box2), box3, TRUE, TRUE, 0);
121     gtk_widget_show (box3);
122
123     /* Reuse the same adjustment */
124     hscale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (adj1));
125     gtk_widget_set_size_request (GTK_WIDGET (hscale), 200, -1);
126     scale_set_default_values (GTK_SCALE (hscale));
127     gtk_box_pack_start (GTK_BOX (box3), hscale, TRUE, TRUE, 0);
128     gtk_widget_show (hscale);
129
130     /* Reuse the same adjustment again */
131     scrollbar = gtk_scrollbar_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (adj1));
132     /* Notice how this causes the scales to always be updated
133      * continuously when the scrollbar is moved */
134     gtk_range_set_update_policy (GTK_RANGE (scrollbar),
135                                  GTK_UPDATE_CONTINUOUS);
136     gtk_box_pack_start (GTK_BOX (box3), scrollbar, TRUE, TRUE, 0);
137     gtk_widget_show (scrollbar);
138
139     box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 10);
140     gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
141     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
142     gtk_widget_show (box2);
143
144     /* A checkbutton to control whether the value is displayed or not */
145     button = gtk_check_button_new_with_label("Display value on scale widgets");
146     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
147     g_signal_connect (button, "toggled",
148                       G_CALLBACK (cb_draw_value), NULL);
149     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
150     gtk_widget_show (button);
151
152     box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 10);
153     gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
154
155     /* An option menu to change the position of the value */
156     label = gtk_label_new ("Scale Value Position:");
157     gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
158     gtk_widget_show (label);
159
160     opt = gtk_option_menu_new ();
161     menu = gtk_menu_new ();
162
163     item = make_menu_item ("Top",
164                            G_CALLBACK (cb_pos_menu_select),
165                            GINT_TO_POINTER (GTK_POS_TOP));
166     gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
167
168     item = make_menu_item ("Bottom", G_CALLBACK (cb_pos_menu_select),
169                            GINT_TO_POINTER (GTK_POS_BOTTOM));
170     gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
171
172     item = make_menu_item ("Left", G_CALLBACK (cb_pos_menu_select),
173                            GINT_TO_POINTER (GTK_POS_LEFT));
174     gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
175
176     item = make_menu_item ("Right", G_CALLBACK (cb_pos_menu_select),
177                            GINT_TO_POINTER (GTK_POS_RIGHT));
178     gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
179
180     gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
181     gtk_box_pack_start (GTK_BOX (box2), opt, TRUE, TRUE, 0);
182     gtk_widget_show (opt);
183
184     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
185     gtk_widget_show (box2);
186
187     box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 10);
188     gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
189
190     /* Yet another option menu, this time for the update policy of the
191      * scale widgets */
192     label = gtk_label_new ("Scale Update Policy:");
193     gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
194     gtk_widget_show (label);
195
196     opt = gtk_option_menu_new ();
197     menu = gtk_menu_new ();
198
199     item = make_menu_item ("Continuous",
200                            G_CALLBACK (cb_update_menu_select),
201                            GINT_TO_POINTER (GTK_UPDATE_CONTINUOUS));
202     gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
203
204     item = make_menu_item ("Discontinuous",
205                            G_CALLBACK (cb_update_menu_select),
206                            GINT_TO_POINTER (GTK_UPDATE_DISCONTINUOUS));
207     gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
208
209     item = make_menu_item ("Delayed",
210                            G_CALLBACK (cb_update_menu_select),
211                            GINT_TO_POINTER (GTK_UPDATE_DELAYED));
212     gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
213
214     gtk_option_menu_set_menu (GTK_OPTION_MENU (opt), menu);
215     gtk_box_pack_start (GTK_BOX (box2), opt, TRUE, TRUE, 0);
216     gtk_widget_show (opt);
217
218     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
219     gtk_widget_show (box2);
220
221     box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 10);
222     gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
223
224     /* An HScale widget for adjusting the number of digits on the
225      * sample scales. */
226     label = gtk_label_new ("Scale Digits:");
227     gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
228     gtk_widget_show (label);
229
230     adj2 = gtk_adjustment_new (1.0, 0.0, 5.0, 1.0, 1.0, 0.0);
231     g_signal_connect (adj2, "value_changed",
232                       G_CALLBACK (cb_digits_scale), NULL);
233     scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (adj2));
234     gtk_scale_set_digits (GTK_SCALE (scale), 0);
235     gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0);
236     gtk_widget_show (scale);
237
238     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
239     gtk_widget_show (box2);
240
241     box2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, FALSE, 10);
242     gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
243
244     /* And, one last HScale widget for adjusting the page size of the
245      * scrollbar. */
246     label = gtk_label_new ("Scrollbar Page Size:");
247     gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
248     gtk_widget_show (label);
249
250     adj2 = gtk_adjustment_new (1.0, 1.0, 101.0, 1.0, 1.0, 0.0);
251     g_signal_connect (adj2, "value-changed",
252                       G_CALLBACK (cb_page_size), (gpointer) adj1);
253     scale = gtk_scale_new (GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT (adj2));
254     gtk_scale_set_digits (GTK_SCALE (scale), 0);
255     gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0);
256     gtk_widget_show (scale);
257
258     gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
259     gtk_widget_show (box2);
260
261     separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
262     gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
263     gtk_widget_show (separator);
264
265     box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE, 10);
266     gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
267     gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
268     gtk_widget_show (box2);
269
270     button = gtk_button_new_with_label ("Quit");
271     g_signal_connect_swapped (button, "clicked",
272                               G_CALLBACK (gtk_main_quit),
273                               NULL);
274     gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
275     gtk_widget_set_can_default (button, TRUE);
276     gtk_widget_grab_default (button);
277     gtk_widget_show (button);
278
279     gtk_widget_show (window);
280 }
281
282 int main( int   argc,
283           char *argv[] )
284 {
285     gtk_init (&argc, &argv);
286
287     create_range_controls ();
288
289     gtk_main ();
290
291     return 0;
292 }
293