comparison examples/controlexample/ToolBarTab.d @ 78:4a04b6759f98

Clean up directory names
author John Reimer <terminal.node@gmail.com>
date Sat, 10 May 2008 13:32:45 -0700
parents
children eb84f9418bbf
comparison
equal deleted inserted replaced
76:04f122e90b0a 78:4a04b6759f98
1 /*******************************************************************************
2 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module examples.controlexample.ToolBarTab;
14
15
16
17 import dwt.DWT;
18 import dwt.events.SelectionAdapter;
19 import dwt.events.SelectionEvent;
20 import dwt.graphics.Point;
21 import dwt.graphics.Rectangle;
22 import dwt.layout.GridData;
23 import dwt.layout.GridLayout;
24 import dwt.widgets.Button;
25 import dwt.widgets.Combo;
26 import dwt.widgets.Group;
27 import dwt.widgets.Item;
28 import dwt.widgets.Menu;
29 import dwt.widgets.MenuItem;
30 import dwt.widgets.ToolBar;
31 import dwt.widgets.ToolItem;
32 import dwt.widgets.Widget;
33
34 import examples.controlexample.Tab;
35 import examples.controlexample.ControlExample;
36
37 import dwt.dwthelper.utils;
38
39 import tango.util.Convert;
40
41 class ToolBarTab : Tab {
42 /* Example widgets and groups that contain them */
43 ToolBar imageToolBar, textToolBar, imageTextToolBar;
44 Group imageToolBarGroup, textToolBarGroup, imageTextToolBarGroup;
45
46 /* Style widgets added to the "Style" group */
47 Button horizontalButton, verticalButton, flatButton, shadowOutButton, wrapButton, rightButton;
48
49 /* Other widgets added to the "Other" group */
50 Button comboChildButton;
51
52 /**
53 * Creates the Tab within a given instance of ControlExample.
54 */
55 this(ControlExample instance) {
56 super(instance);
57 }
58
59 /**
60 * Creates the "Example" group.
61 */
62 void createExampleGroup () {
63 super.createExampleGroup ();
64
65 /* Create a group for the image tool bar */
66 imageToolBarGroup = new Group (exampleGroup, DWT.NONE);
67 imageToolBarGroup.setLayout (new GridLayout ());
68 imageToolBarGroup.setLayoutData (new GridData (DWT.FILL, DWT.FILL, true, true));
69 imageToolBarGroup.setText (ControlExample.getResourceString("Image_ToolBar"));
70
71 /* Create a group for the text tool bar */
72 textToolBarGroup = new Group (exampleGroup, DWT.NONE);
73 textToolBarGroup.setLayout (new GridLayout ());
74 textToolBarGroup.setLayoutData (new GridData (DWT.FILL, DWT.FILL, true, true));
75 textToolBarGroup.setText (ControlExample.getResourceString("Text_ToolBar"));
76
77 /* Create a group for the image and text tool bar */
78 imageTextToolBarGroup = new Group (exampleGroup, DWT.NONE);
79 imageTextToolBarGroup.setLayout (new GridLayout ());
80 imageTextToolBarGroup.setLayoutData (new GridData (DWT.FILL, DWT.FILL, true, true));
81 imageTextToolBarGroup.setText (ControlExample.getResourceString("ImageText_ToolBar"));
82 }
83
84 /**
85 * Creates the "Example" widgets.
86 */
87 void createExampleWidgets () {
88
89 /* Compute the widget style */
90 int style = getDefaultStyle();
91 if (horizontalButton.getSelection()) style |= DWT.HORIZONTAL;
92 if (verticalButton.getSelection()) style |= DWT.VERTICAL;
93 if (flatButton.getSelection()) style |= DWT.FLAT;
94 if (wrapButton.getSelection()) style |= DWT.WRAP;
95 if (borderButton.getSelection()) style |= DWT.BORDER;
96 if (shadowOutButton.getSelection()) style |= DWT.SHADOW_OUT;
97 if (rightButton.getSelection()) style |= DWT.RIGHT;
98
99 /*
100 * Create the example widgets.
101 *
102 * A tool bar must consist of all image tool
103 * items or all text tool items but not both.
104 */
105
106 /* Create the image tool bar */
107 imageToolBar = new ToolBar (imageToolBarGroup, style);
108 ToolItem item = new ToolItem (imageToolBar, DWT.PUSH);
109 item.setImage (instance.images[ControlExample.ciClosedFolder]);
110 item.setToolTipText("DWT.PUSH");
111 item = new ToolItem (imageToolBar, DWT.PUSH);
112 item.setImage (instance.images[ControlExample.ciClosedFolder]);
113 item.setToolTipText ("DWT.PUSH");
114 item = new ToolItem (imageToolBar, DWT.RADIO);
115 item.setImage (instance.images[ControlExample.ciOpenFolder]);
116 item.setToolTipText ("DWT.RADIO");
117 item = new ToolItem (imageToolBar, DWT.RADIO);
118 item.setImage (instance.images[ControlExample.ciOpenFolder]);
119 item.setToolTipText ("DWT.RADIO");
120 item = new ToolItem (imageToolBar, DWT.CHECK);
121 item.setImage (instance.images[ControlExample.ciTarget]);
122 item.setToolTipText ("DWT.CHECK");
123 item = new ToolItem (imageToolBar, DWT.RADIO);
124 item.setImage (instance.images[ControlExample.ciClosedFolder]);
125 item.setToolTipText ("DWT.RADIO");
126 item = new ToolItem (imageToolBar, DWT.RADIO);
127 item.setImage (instance.images[ControlExample.ciClosedFolder]);
128 item.setToolTipText ("DWT.RADIO");
129 item = new ToolItem (imageToolBar, DWT.SEPARATOR);
130 item.setToolTipText("DWT.SEPARATOR");
131 if (comboChildButton.getSelection ()) {
132 Combo combo = new Combo (imageToolBar, DWT.NONE);
133 combo.setItems (["250", "500", "750"]);
134 combo.setText (combo.getItem (0));
135 combo.pack ();
136 item.setWidth (combo.getSize ().x);
137 item.setControl (combo);
138 }
139 item = new ToolItem (imageToolBar, DWT.DROP_DOWN);
140 item.setImage (instance.images[ControlExample.ciTarget]);
141 item.setToolTipText ("DWT.DROP_DOWN");
142 item.addSelectionListener(new DropDownSelectionListener());
143
144 /* Create the text tool bar */
145 textToolBar = new ToolBar (textToolBarGroup, style);
146 item = new ToolItem (textToolBar, DWT.PUSH);
147 item.setText (ControlExample.getResourceString("Push"));
148 item.setToolTipText("DWT.PUSH");
149 item = new ToolItem (textToolBar, DWT.PUSH);
150 item.setText (ControlExample.getResourceString("Push"));
151 item.setToolTipText("DWT.PUSH");
152 item = new ToolItem (textToolBar, DWT.RADIO);
153 item.setText (ControlExample.getResourceString("Radio"));
154 item.setToolTipText("DWT.RADIO");
155 item = new ToolItem (textToolBar, DWT.RADIO);
156 item.setText (ControlExample.getResourceString("Radio"));
157 item.setToolTipText("DWT.RADIO");
158 item = new ToolItem (textToolBar, DWT.CHECK);
159 item.setText (ControlExample.getResourceString("Check"));
160 item.setToolTipText("DWT.CHECK");
161 item = new ToolItem (textToolBar, DWT.RADIO);
162 item.setText (ControlExample.getResourceString("Radio"));
163 item.setToolTipText("DWT.RADIO");
164 item = new ToolItem (textToolBar, DWT.RADIO);
165 item.setText (ControlExample.getResourceString("Radio"));
166 item.setToolTipText("DWT.RADIO");
167 item = new ToolItem (textToolBar, DWT.SEPARATOR);
168 item.setToolTipText("DWT.SEPARATOR");
169 if (comboChildButton.getSelection ()) {
170 Combo combo = new Combo (textToolBar, DWT.NONE);
171 combo.setItems (["250", "500", "750"]);
172 combo.setText (combo.getItem (0));
173 combo.pack ();
174 item.setWidth (combo.getSize ().x);
175 item.setControl (combo);
176 }
177 item = new ToolItem (textToolBar, DWT.DROP_DOWN);
178 item.setText (ControlExample.getResourceString("Drop_Down"));
179 item.setToolTipText("DWT.DROP_DOWN");
180 item.addSelectionListener(new DropDownSelectionListener());
181
182 /* Create the image and text tool bar */
183 imageTextToolBar = new ToolBar (imageTextToolBarGroup, style);
184 item = new ToolItem (imageTextToolBar, DWT.PUSH);
185 item.setImage (instance.images[ControlExample.ciClosedFolder]);
186 item.setText (ControlExample.getResourceString("Push"));
187 item.setToolTipText("DWT.PUSH");
188 item = new ToolItem (imageTextToolBar, DWT.PUSH);
189 item.setImage (instance.images[ControlExample.ciClosedFolder]);
190 item.setText (ControlExample.getResourceString("Push"));
191 item.setToolTipText("DWT.PUSH");
192 item = new ToolItem (imageTextToolBar, DWT.RADIO);
193 item.setImage (instance.images[ControlExample.ciOpenFolder]);
194 item.setText (ControlExample.getResourceString("Radio"));
195 item.setToolTipText("DWT.RADIO");
196 item = new ToolItem (imageTextToolBar, DWT.RADIO);
197 item.setImage (instance.images[ControlExample.ciOpenFolder]);
198 item.setText (ControlExample.getResourceString("Radio"));
199 item.setToolTipText("DWT.RADIO");
200 item = new ToolItem (imageTextToolBar, DWT.CHECK);
201 item.setImage (instance.images[ControlExample.ciTarget]);
202 item.setText (ControlExample.getResourceString("Check"));
203 item.setToolTipText("DWT.CHECK");
204 item = new ToolItem (imageTextToolBar, DWT.RADIO);
205 item.setImage (instance.images[ControlExample.ciClosedFolder]);
206 item.setText (ControlExample.getResourceString("Radio"));
207 item.setToolTipText("DWT.RADIO");
208 item = new ToolItem (imageTextToolBar, DWT.RADIO);
209 item.setImage (instance.images[ControlExample.ciClosedFolder]);
210 item.setText (ControlExample.getResourceString("Radio"));
211 item.setToolTipText("DWT.RADIO");
212 item = new ToolItem (imageTextToolBar, DWT.SEPARATOR);
213 item.setToolTipText("DWT.SEPARATOR");
214 if (comboChildButton.getSelection ()) {
215 Combo combo = new Combo (imageTextToolBar, DWT.NONE);
216 combo.setItems (["250", "500", "750"]);
217 combo.setText (combo.getItem (0));
218 combo.pack ();
219 item.setWidth (combo.getSize ().x);
220 item.setControl (combo);
221 }
222 item = new ToolItem (imageTextToolBar, DWT.DROP_DOWN);
223 item.setImage (instance.images[ControlExample.ciTarget]);
224 item.setText (ControlExample.getResourceString("Drop_Down"));
225 item.setToolTipText("DWT.DROP_DOWN");
226 item.addSelectionListener(new DropDownSelectionListener());
227
228 /*
229 * Do not add the selection event for this drop down
230 * tool item. Without hooking the event, the drop down
231 * widget does nothing special when the drop down area
232 * is selected.
233 */
234 }
235
236 /**
237 * Creates the "Other" group.
238 */
239 void createOtherGroup () {
240 super.createOtherGroup ();
241
242 /* Create display controls specific to this example */
243 comboChildButton = new Button (otherGroup, DWT.CHECK);
244 comboChildButton.setText (ControlExample.getResourceString("Combo_child"));
245
246 /* Add the listeners */
247 comboChildButton.addSelectionListener (new class() SelectionAdapter {
248 public void widgetSelected (SelectionEvent event) {
249 recreateExampleWidgets ();
250 }
251 });
252 }
253
254 /**
255 * Creates the "Style" group.
256 */
257 void createStyleGroup() {
258 super.createStyleGroup();
259
260 /* Create the extra widgets */
261 horizontalButton = new Button (styleGroup, DWT.RADIO);
262 horizontalButton.setText ("DWT.HORIZONTAL");
263 verticalButton = new Button (styleGroup, DWT.RADIO);
264 verticalButton.setText ("DWT.VERTICAL");
265 flatButton = new Button (styleGroup, DWT.CHECK);
266 flatButton.setText ("DWT.FLAT");
267 shadowOutButton = new Button (styleGroup, DWT.CHECK);
268 shadowOutButton.setText ("DWT.SHADOW_OUT");
269 wrapButton = new Button (styleGroup, DWT.CHECK);
270 wrapButton.setText ("DWT.WRAP");
271 rightButton = new Button (styleGroup, DWT.CHECK);
272 rightButton.setText ("DWT.RIGHT");
273 borderButton = new Button (styleGroup, DWT.CHECK);
274 borderButton.setText ("DWT.BORDER");
275 }
276
277 void disposeExampleWidgets () {
278 super.disposeExampleWidgets ();
279 }
280
281 /**
282 * Gets the "Example" widget children's items, if any.
283 *
284 * @return an array containing the example widget children's items
285 */
286 Item [] getExampleWidgetItems () {
287 Item [] imageToolBarItems = imageToolBar.getItems();
288 Item [] textToolBarItems = textToolBar.getItems();
289 Item [] imageTextToolBarItems = imageTextToolBar.getItems();
290 Item [] allItems = new Item [imageToolBarItems.length + textToolBarItems.length + imageTextToolBarItems.length];
291 System.arraycopy(imageToolBarItems, 0, allItems, 0, imageToolBarItems.length);
292 System.arraycopy(textToolBarItems, 0, allItems, imageToolBarItems.length, textToolBarItems.length);
293 System.arraycopy(imageTextToolBarItems, 0, allItems, imageToolBarItems.length + textToolBarItems.length, imageTextToolBarItems.length);
294 return allItems;
295 }
296
297 /**
298 * Gets the "Example" widget children.
299 */
300 Widget [] getExampleWidgets () {
301 return [ cast(Widget) imageToolBar, textToolBar, imageTextToolBar ];
302 }
303
304 /**
305 * Gets the short text for the tab folder item.
306 */
307 public char[] getShortTabText() {
308 return "TB";
309 }
310
311 /**
312 * Gets the text for the tab folder item.
313 */
314 char[] getTabText () {
315 return "ToolBar";
316 }
317
318 /**
319 * Sets the state of the "Example" widgets.
320 */
321 void setExampleWidgetState () {
322 super.setExampleWidgetState ();
323 horizontalButton.setSelection ((imageToolBar.getStyle () & DWT.HORIZONTAL) !is 0);
324 verticalButton.setSelection ((imageToolBar.getStyle () & DWT.VERTICAL) !is 0);
325 flatButton.setSelection ((imageToolBar.getStyle () & DWT.FLAT) !is 0);
326 wrapButton.setSelection ((imageToolBar.getStyle () & DWT.WRAP) !is 0);
327 shadowOutButton.setSelection ((imageToolBar.getStyle () & DWT.SHADOW_OUT) !is 0);
328 borderButton.setSelection ((imageToolBar.getStyle () & DWT.BORDER) !is 0);
329 rightButton.setSelection ((imageToolBar.getStyle () & DWT.RIGHT) !is 0);
330 }
331
332 /**
333 * Listens to widgetSelected() events on DWT.DROP_DOWN type ToolItems
334 * and opens/closes a menu when appropriate.
335 */
336 class DropDownSelectionListener : SelectionAdapter {
337 private Menu menu = null;
338 private bool visible = false;
339
340 public void widgetSelected(SelectionEvent event) {
341 // Create the menu if it has not already been created
342 if (menu is null) {
343 // Lazy create the menu.
344 menu = new Menu(shell);
345 for (int i = 0; i < 9; ++i) {
346 final char[] text = ControlExample.getResourceString("DropDownData_" ~ to!(char[])(i));
347 if (text.length !is 0) {
348 MenuItem menuItem = new MenuItem(menu, DWT.NONE);
349 menuItem.setText(text);
350 /*
351 * Add a menu selection listener so that the menu is hidden
352 * when the user selects an item from the drop down menu.
353 */
354 menuItem.addSelectionListener(new class() SelectionAdapter {
355 public void widgetSelected(SelectionEvent e) {
356 setMenuVisible(false);
357 }
358 });
359 } else {
360 new MenuItem(menu, DWT.SEPARATOR);
361 }
362 }
363 }
364
365 /**
366 * A selection event will be fired when a drop down tool
367 * item is selected in the main area and in the drop
368 * down arrow. Examine the event detail to determine
369 * where the widget was selected.
370 */
371 if (event.detail is DWT.ARROW) {
372 /*
373 * The drop down arrow was selected.
374 */
375 if (visible) {
376 // Hide the menu to give the Arrow the appearance of being a toggle button.
377 setMenuVisible(false);
378 } else {
379 // Position the menu below and vertically aligned with the the drop down tool button.
380 final ToolItem toolItem = cast(ToolItem) event.widget;
381 final ToolBar toolBar = toolItem.getParent();
382
383 Rectangle toolItemBounds = toolItem.getBounds();
384 Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y));
385 menu.setLocation(point.x, point.y + toolItemBounds.height);
386 setMenuVisible(true);
387 }
388 } else {
389 /*
390 * Main area of drop down tool item selected.
391 * An application would invoke the code to perform the action for the tool item.
392 */
393 }
394 }
395 private void setMenuVisible(bool visible) {
396 menu.setVisible(visible);
397 this.visible = visible;
398 }
399 }
400 }