4 GtkWidget *hscale, *vscale;
6 static void cb_pos_menu_select( GtkWidget *item,
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);
14 static void cb_update_menu_select( GtkWidget *item,
15 GtkUpdateType policy )
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);
22 static void cb_digits_scale( GtkAdjustment *adj )
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);
29 static void cb_page_size( GtkAdjustment *get,
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;
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,
41 (set->upper - set->page_size)));
42 g_signal_emit_by_name(G_OBJECT(set), "changed");
45 static void cb_draw_value( GtkToggleButton *button )
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);
53 /* Convenience functions */
55 static GtkWidget *make_menu_item ( gchar *name,
61 item = gtk_menu_item_new_with_label (name);
62 g_signal_connect (item, "activate",
63 callback, (gpointer) data);
64 gtk_widget_show (item);
69 static void scale_set_default_values( GtkScale *scale )
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);
78 /* makes the sample window */
80 static void create_range_controls( void )
83 GtkWidget *box1, *box2, *box3;
87 GtkWidget *opt, *menu, *item;
90 GtkAdjustment *adj1, *adj2;
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),
97 gtk_window_set_title (GTK_WINDOW (window), "range controls");
99 box1 = gtk_vbox_new (FALSE, 0);
100 gtk_container_add (GTK_CONTAINER (window), box1);
101 gtk_widget_show (box1);
103 box2 = gtk_hbox_new (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);
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);
114 vscale = gtk_vscale_new (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);
119 box3 = gtk_vbox_new (FALSE, 10);
120 gtk_box_pack_start (GTK_BOX (box2), box3, TRUE, TRUE, 0);
121 gtk_widget_show (box3);
123 /* Reuse the same adjustment */
124 hscale = gtk_hscale_new (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);
130 /* Reuse the same adjustment again */
131 scrollbar = gtk_hscrollbar_new (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);
139 box2 = gtk_hbox_new (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);
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);
152 box2 = gtk_hbox_new (FALSE, 10);
153 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
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);
160 opt = gtk_option_menu_new ();
161 menu = gtk_menu_new ();
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);
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);
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);
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);
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);
184 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
185 gtk_widget_show (box2);
187 box2 = gtk_hbox_new (FALSE, 10);
188 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
190 /* Yet another option menu, this time for the update policy of the
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);
196 opt = gtk_option_menu_new ();
197 menu = gtk_menu_new ();
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);
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);
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);
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);
218 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
219 gtk_widget_show (box2);
221 box2 = gtk_hbox_new (FALSE, 10);
222 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
224 /* An HScale widget for adjusting the number of digits on the
226 label = gtk_label_new ("Scale Digits:");
227 gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
228 gtk_widget_show (label);
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_hscale_new (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);
238 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
239 gtk_widget_show (box2);
241 box2 = gtk_hbox_new (FALSE, 10);
242 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
244 /* And, one last HScale widget for adjusting the page size of the
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);
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_hscale_new (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);
258 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
259 gtk_widget_show (box2);
261 separator = gtk_hseparator_new ();
262 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
263 gtk_widget_show (separator);
265 box2 = gtk_vbox_new (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);
270 button = gtk_button_new_with_label ("Quit");
271 g_signal_connect_swapped (button, "clicked",
272 G_CALLBACK (gtk_main_quit),
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);
279 gtk_widget_show (window);
285 gtk_init (&argc, &argv);
287 create_range_controls ();