alternative to assert
[gtkD.git] / gtkD / demos / gtkD / TestScales.d
blob39098e3ae393e203d7a32e65a4f4b6fc76ff8a90
1 /*
2 * This file is part of gtkD.
3 *
4 * gtkD is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * gtkD is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with gtkD; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 module gtkD.TestScales;
21 private import gtkc.gtktypes;
22 private import gtk.Table;
24 private import gtk.VScale;
25 private import gtk.HScale;
26 private import gtk.MenuItem;
27 private import gtk.Menu;
28 private import gtk.CheckButton;
29 private import gtk.Box;
30 private import gtk.Button;
31 private import gtk.Scrollbar;
32 private import gtk.Separator;
33 //private import gtk.OptionMenu;
34 private import gtk.Label;
35 private import gtk.Scale;
36 private import gtk.Adjustment;
37 private import gtk.VBox;
38 private import gtk.HBox;
39 private import gtk.HScrollbar;
40 private import gtk.Range;
42 /**
43 * This tests the gtkD scales and scrollbar widgets
46 class TestScales : Table //, MenuItemListener
48 VScale vscale;
49 HScale hscale;
51 this()
54 debug(1)
56 printf("instantiating TestScales\n");
59 super(1,1,0);
61 createRangeControls();
63 private import gtk.ComboBox;
64 void createRangeControls()
67 Box box1,box2,box3;
68 Button button;
69 Scrollbar scrollbar;
70 Separator separator;
71 ComboBox positionSelection;
72 ComboBox updateSelection;
74 Label label;
75 Scale scale;
76 Adjustment adj1, adj2;
78 box1 = new VBox(false,0);
79 add(box1);
81 box2 = new HBox(false,0);
82 box2.setBorderWidth(10);
83 box1.packStart(box2, true,true,0);
85 /* value, lower, upper, step_increment, page_increment, page_size */
86 /* Note that the page_size value only makes a difference for
87 * scrollbar widgets, and the highest value you'll get is actually
88 * (upper - page_size). */
89 adj1 = new Adjustment(0.0, 0.0, 101.0, 0.1, 1.0, 1.0);
91 vscale = new VScale(adj1);
92 //scale_set_default_values (GTK_SCALE (vscale));
93 box2.packStart(vscale, true, true, 0);
95 box3 = new VBox(false,10);
96 box2.packStart(box3,true,true,0);
98 /* Reuse the same adjustment */
99 hscale = new HScale(adj1);
100 hscale.setSizeRequest(200,-1);
101 //scale_set_default_values (GTK_SCALE (hscale));
102 box3.packStart(hscale,true,true,0);
104 /* Reuse the same adjustment again */
105 scrollbar = new HScrollbar(adj1);
106 /* Notice how this causes the scales to always be updated
107 * continuously when the scrollbar is moved */
108 scrollbar.setUpdatePolicy(UpdateType.CONTINUOUS);
109 box3.packStart(scrollbar,true,true,0);
111 box2 = new HBox(false,10);
112 box2.setBorderWidth(10);
113 box1.packStart(box2,true,true,0);
115 /* A checkbutton to control whether the value is displayed or not */
116 CheckButton cButton = new CheckButton("Display value on scale widgets");
117 cButton.setActive(true);
118 cButton.addOnClicked(&displayValues);
119 box2.packStart(cButton,true,true,0);
121 box2 = new HBox(false,10);
122 box2.setBorderWidth(10);
124 /* An option menu to change the position of the value */
125 label = new Label("Scale Value Position:");
126 box2.packStart(label,false,false,0);
128 positionSelection = new ComboBox();
129 positionSelection.appendText("Top");
130 positionSelection.appendText("Bottom");
131 positionSelection.appendText("Left");
132 positionSelection.appendText("Right");
133 positionSelection.addOnChanged(&onPositionSelectionChanged);
134 positionSelection.setActive(0);
136 box2.packStart(positionSelection,false,false,0);
138 box1.packStart(box2,false,false,0);
140 box2 = new HBox(false,10);
141 box2.setBorderWidth(10);
143 /* Yet another option menu, this time for the update policy of the scale widgets */
144 label = new Label("Scale Update Policy:");
145 box2.packStart(label,false,false,0);
147 updateSelection = new ComboBox();
149 updateSelection.appendText("Continuous");
150 updateSelection.appendText("Discontinuous");
151 updateSelection.appendText("Delayed");
152 updateSelection.addOnChanged(&onUpdateSelectionChanged);
153 updateSelection.setActive(0);
155 box2.packStart(updateSelection,false,false,0);
156 box1.packStart(box2,false,false,0);
160 * Here is a bit a pure C GTK+ code.
161 * This code would compile and execute in D,
162 * we just need to define the functions as extern.
165 box2 = gtk_hbox_new (FALSE, 10);
166 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
168 /* An HScale widget for adjusting the number of digits on the
169 * sample scales. */
170 label = gtk_label_new ("Scale Digits:");
171 gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
172 gtk_widget_show (label);
174 adj2 = gtk_adjustment_new (1.0, 0.0, 5.0, 1.0, 1.0, 0.0);
175 g_signal_connect (G_OBJECT (adj2), "value_changed",
176 G_CALLBACK (cb_digits_scale), NULL);
177 scale = gtk_hscale_new (GTK_ADJUSTMENT (adj2));
178 gtk_scale_set_digits (GTK_SCALE (scale), 0);
179 gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0);
180 gtk_widget_show (scale);
182 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
183 gtk_widget_show (box2);
185 box2 = gtk_hbox_new (FALSE, 10);
186 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
188 /* And, one last HScale widget for adjusting the page size of the
189 * scrollbar. */
190 label = gtk_label_new ("Scrollbar Page Size:");
191 gtk_box_pack_start (GTK_BOX (box2), label, FALSE, FALSE, 0);
192 gtk_widget_show (label);
194 adj2 = gtk_adjustment_new (1.0, 1.0, 101.0, 1.0, 1.0, 0.0);
195 g_signal_connect (G_OBJECT (adj2), "value_changed",
196 G_CALLBACK (cb_page_size), (gpointer) adj1);
197 scale = gtk_hscale_new (GTK_ADJUSTMENT (adj2));
198 gtk_scale_set_digits (GTK_SCALE (scale), 0);
199 gtk_box_pack_start (GTK_BOX (box2), scale, TRUE, TRUE, 0);
200 gtk_widget_show (scale);
202 gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE, 0);
203 gtk_widget_show (box2);
205 separator = gtk_hseparator_new ();
206 gtk_box_pack_start (GTK_BOX (box1), separator, FALSE, TRUE, 0);
207 gtk_widget_show (separator);
209 box2 = gtk_vbox_new (FALSE, 10);
210 gtk_container_set_border_width (GTK_CONTAINER (box2), 10);
211 gtk_box_pack_start (GTK_BOX (box1), box2, FALSE, TRUE, 0);
212 gtk_widget_show (box2);
214 button = gtk_button_new_with_label ("Quit");
215 g_signal_connect_swapped (G_OBJECT (button), "clicked",
216 G_CALLBACK (gtk_main_quit),
217 NULL);
218 gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE, 0);
219 GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
220 gtk_widget_grab_default (button);
221 gtk_widget_show (button);
223 gtk_widget_show (window);
228 void onPositionSelectionChanged(ComboBox comboBox)
231 switch ( comboBox.getActiveText() )
233 case "Top":
234 vscale.setValuePos(PositionType.TOP);
235 hscale.setValuePos(PositionType.TOP);
236 break;
237 case "Bottom":
238 vscale.setValuePos(PositionType.BOTTOM);
239 hscale.setValuePos(PositionType.BOTTOM);
240 break;
241 case "Left":
242 vscale.setValuePos(PositionType.LEFT);
243 hscale.setValuePos(PositionType.LEFT);
244 break;
245 case "Right":
246 vscale.setValuePos(PositionType.RIGHT);
247 hscale.setValuePos(PositionType.RIGHT);
248 break;
249 default:
250 break;
255 void onUpdateSelectionChanged(ComboBox comboBox)
257 switch ( comboBox.getActiveText() )
259 case "Continuous":
260 vscale.setUpdatePolicy(UpdateType.CONTINUOUS);
261 hscale.setUpdatePolicy(UpdateType.CONTINUOUS);
262 break;
263 case "Discontinuous":
264 vscale.setUpdatePolicy(UpdateType.DISCONTINUOUS);
265 hscale.setUpdatePolicy(UpdateType.DISCONTINUOUS);
266 break;
267 case "Delayed":
268 vscale.setUpdatePolicy(UpdateType.DELAYED);
269 hscale.setUpdatePolicy(UpdateType.DELAYED);
270 break;
271 default:
272 break;
279 void activateItemCallback(MenuItem menuItem, char [] action)
283 void activateCallback(MenuItem menuItem, char [] action)
285 switch ( action )
287 case "ValueTop":
288 vscale.setValuePos(Scale.TOP);
289 hscale.setValuePos(Scale.TOP);
290 break;
291 case "ValueBottom":
292 vscale.setValuePos(Scale.BOTTOM);
293 hscale.setValuePos(Scale.BOTTOM);
294 break;
295 case "ValueLeft":
296 vscale.setValuePos(Scale.LEFT);
297 hscale.setValuePos(Scale.LEFT);
298 break;
299 case "ValueRight":
300 vscale.setValuePos(Scale.RIGHT);
301 hscale.setValuePos(Scale.RIGHT);
302 break;
303 case "ScaleContinuous":
304 vscale.setUpdatePolicy(Scale.CONTINUOUS);
305 hscale.setUpdatePolicy(Scale.CONTINUOUS);
306 break;
307 case "ScaleDiscontinuous":
308 vscale.setUpdatePolicy(Scale.DISCONTINUOUS);
309 hscale.setUpdatePolicy(Scale.DISCONTINUOUS);
310 break;
311 case "ScaleDelayed":
312 vscale.setUpdatePolicy(Scale.DELAYED);
313 hscale.setUpdatePolicy(Scale.DELAYED);
314 break;
315 default:
316 printf("menuItem.action %.*s received\n",action);
317 break;
322 void displayValues(Button checkButton)
324 //CheckButton cb = (CheckButton)button;
325 //printf("check button %X %X\n",button,(CheckButton)button);
326 //printf("button.toString %.*s\n",button.toString());
327 vscale.setDrawValue((cast(CheckButton)checkButton).getActive());
328 hscale.setDrawValue((cast(CheckButton)checkButton).getActive());