Mercurial > projects > dwt-samples
annotate examples/controlexample/CoolBarTab.d @ 161:eb84f9418bbf
Change module names to this not have examples in the FQN.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 01 Sep 2008 22:24:27 +0200 |
parents | 4a04b6759f98 |
children |
rev | line source |
---|---|
78 | 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 *******************************************************************************/ | |
161
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
13 module controlexample.CoolBarTab; |
78 | 14 |
15 | |
16 | |
17 import dwt.DWT; | |
18 import dwt.events.MenuAdapter; | |
19 import dwt.events.MenuEvent; | |
20 import dwt.events.SelectionAdapter; | |
21 import dwt.events.SelectionEvent; | |
22 import dwt.graphics.Image; | |
23 import dwt.graphics.Point; | |
24 import dwt.graphics.Rectangle; | |
25 import dwt.layout.GridData; | |
26 import dwt.layout.GridLayout; | |
27 import dwt.widgets.Button; | |
28 import dwt.widgets.Control; | |
29 import dwt.widgets.CoolBar; | |
30 import dwt.widgets.CoolItem; | |
31 import dwt.widgets.Event; | |
32 import dwt.widgets.Group; | |
33 import dwt.widgets.Item; | |
34 import dwt.widgets.Listener; | |
35 import dwt.widgets.Menu; | |
36 import dwt.widgets.MenuItem; | |
37 import dwt.widgets.Text; | |
38 import dwt.widgets.ToolBar; | |
39 import dwt.widgets.ToolItem; | |
40 import dwt.widgets.Widget; | |
41 | |
161
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
42 import controlexample.Tab; |
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
78
diff
changeset
|
43 import controlexample.ControlExample; |
78 | 44 import tango.util.Convert; |
45 | |
46 class CoolBarTab : Tab { | |
47 /* Example widgets and group that contains them */ | |
48 CoolBar coolBar; | |
49 CoolItem pushItem, dropDownItem, radioItem, checkItem, textItem; | |
50 Group coolBarGroup; | |
51 | |
52 /* Style widgets added to the "Style" group */ | |
53 Button horizontalButton, verticalButton; | |
54 Button dropDownButton, flatButton; | |
55 | |
56 /* Other widgets added to the "Other" group */ | |
57 Button lockedButton; | |
58 | |
59 Point[] sizes; | |
60 int[] wrapIndices; | |
61 int[] order; | |
62 | |
63 /** | |
64 * Creates the Tab within a given instance of ControlExample. | |
65 */ | |
66 this(ControlExample instance) { | |
67 super(instance); | |
68 } | |
69 | |
70 /** | |
71 * Creates the "Other" group. | |
72 */ | |
73 void createOtherGroup () { | |
74 super.createOtherGroup (); | |
75 | |
76 /* Create display controls specific to this example */ | |
77 lockedButton = new Button (otherGroup, DWT.CHECK); | |
78 lockedButton.setText (ControlExample.getResourceString("Locked")); | |
79 | |
80 /* Add the listeners */ | |
81 lockedButton.addSelectionListener (new class() SelectionAdapter { | |
82 public void widgetSelected (SelectionEvent event) { | |
83 setWidgetLocked (); | |
84 } | |
85 }); | |
86 } | |
87 | |
88 /** | |
89 * Creates the "Example" group. | |
90 */ | |
91 void createExampleGroup () { | |
92 super.createExampleGroup (); | |
93 coolBarGroup = new Group (exampleGroup, DWT.NONE); | |
94 coolBarGroup.setLayout (new GridLayout ()); | |
95 coolBarGroup.setLayoutData (new GridData (DWT.FILL, DWT.FILL, true, true)); | |
96 coolBarGroup.setText ("CoolBar"); | |
97 } | |
98 | |
99 /** | |
100 * Creates the "Example" widgets. | |
101 */ | |
102 void createExampleWidgets () { | |
103 int style = getDefaultStyle(), itemStyle = 0; | |
104 | |
105 /* Compute the widget, item, and item toolBar styles */ | |
106 int toolBarStyle = DWT.FLAT; | |
107 bool vertical = false; | |
108 if (horizontalButton.getSelection ()) { | |
109 style |= DWT.HORIZONTAL; | |
110 toolBarStyle |= DWT.HORIZONTAL; | |
111 } | |
112 if (verticalButton.getSelection ()) { | |
113 style |= DWT.VERTICAL; | |
114 toolBarStyle |= DWT.VERTICAL; | |
115 vertical = true; | |
116 } | |
117 if (borderButton.getSelection()) style |= DWT.BORDER; | |
118 if (flatButton.getSelection()) style |= DWT.FLAT; | |
119 if (dropDownButton.getSelection()) itemStyle |= DWT.DROP_DOWN; | |
120 | |
121 /* | |
122 * Create the example widgets. | |
123 */ | |
124 coolBar = new CoolBar (coolBarGroup, style); | |
125 | |
126 /* Create the push button toolbar cool item */ | |
127 ToolBar toolBar = new ToolBar (coolBar, toolBarStyle); | |
128 ToolItem item = new ToolItem (toolBar, DWT.PUSH); | |
129 item.setImage (instance.images[ControlExample.ciClosedFolder]); | |
130 item.setToolTipText ("DWT.PUSH"); | |
131 item = new ToolItem (toolBar, DWT.PUSH); | |
132 item.setImage (instance.images[ControlExample.ciOpenFolder]); | |
133 item.setToolTipText ("DWT.PUSH"); | |
134 item = new ToolItem (toolBar, DWT.PUSH); | |
135 item.setImage (instance.images[ControlExample.ciTarget]); | |
136 item.setToolTipText ("DWT.PUSH"); | |
137 item = new ToolItem (toolBar, DWT.SEPARATOR); | |
138 item = new ToolItem (toolBar, DWT.PUSH); | |
139 item.setImage (instance.images[ControlExample.ciClosedFolder]); | |
140 item.setToolTipText ("DWT.PUSH"); | |
141 item = new ToolItem (toolBar, DWT.PUSH); | |
142 item.setImage (instance.images[ControlExample.ciOpenFolder]); | |
143 item.setToolTipText ("DWT.PUSH"); | |
144 pushItem = new CoolItem (coolBar, itemStyle); | |
145 pushItem.setControl (toolBar); | |
146 pushItem.addSelectionListener (new CoolItemSelectionListener()); | |
147 | |
148 /* Create the dropdown toolbar cool item */ | |
149 toolBar = new ToolBar (coolBar, toolBarStyle); | |
150 item = new ToolItem (toolBar, DWT.DROP_DOWN); | |
151 item.setImage (instance.images[ControlExample.ciOpenFolder]); | |
152 item.setToolTipText ("DWT.DROP_DOWN"); | |
153 item.addSelectionListener (new DropDownSelectionListener()); | |
154 item = new ToolItem (toolBar, DWT.DROP_DOWN); | |
155 item.setImage (instance.images[ControlExample.ciClosedFolder]); | |
156 item.setToolTipText ("DWT.DROP_DOWN"); | |
157 item.addSelectionListener (new DropDownSelectionListener()); | |
158 dropDownItem = new CoolItem (coolBar, itemStyle); | |
159 dropDownItem.setControl (toolBar); | |
160 dropDownItem.addSelectionListener (new CoolItemSelectionListener()); | |
161 | |
162 /* Create the radio button toolbar cool item */ | |
163 toolBar = new ToolBar (coolBar, toolBarStyle); | |
164 item = new ToolItem (toolBar, DWT.RADIO); | |
165 item.setImage (instance.images[ControlExample.ciClosedFolder]); | |
166 item.setToolTipText ("DWT.RADIO"); | |
167 item = new ToolItem (toolBar, DWT.RADIO); | |
168 item.setImage (instance.images[ControlExample.ciClosedFolder]); | |
169 item.setToolTipText ("DWT.RADIO"); | |
170 item = new ToolItem (toolBar, DWT.RADIO); | |
171 item.setImage (instance.images[ControlExample.ciClosedFolder]); | |
172 item.setToolTipText ("DWT.RADIO"); | |
173 radioItem = new CoolItem (coolBar, itemStyle); | |
174 radioItem.setControl (toolBar); | |
175 radioItem.addSelectionListener (new CoolItemSelectionListener()); | |
176 | |
177 /* Create the check button toolbar cool item */ | |
178 toolBar = new ToolBar (coolBar, toolBarStyle); | |
179 item = new ToolItem (toolBar, DWT.CHECK); | |
180 item.setImage (instance.images[ControlExample.ciClosedFolder]); | |
181 item.setToolTipText ("DWT.CHECK"); | |
182 item = new ToolItem (toolBar, DWT.CHECK); | |
183 item.setImage (instance.images[ControlExample.ciTarget]); | |
184 item.setToolTipText ("DWT.CHECK"); | |
185 item = new ToolItem (toolBar, DWT.CHECK); | |
186 item.setImage (instance.images[ControlExample.ciOpenFolder]); | |
187 item.setToolTipText ("DWT.CHECK"); | |
188 item = new ToolItem (toolBar, DWT.CHECK); | |
189 item.setImage (instance.images[ControlExample.ciTarget]); | |
190 item.setToolTipText ("DWT.CHECK"); | |
191 checkItem = new CoolItem (coolBar, itemStyle); | |
192 checkItem.setControl (toolBar); | |
193 checkItem.addSelectionListener (new CoolItemSelectionListener()); | |
194 | |
195 /* Create the text cool item */ | |
196 if (!vertical) { | |
197 Text text = new Text (coolBar, DWT.BORDER | DWT.SINGLE); | |
198 textItem = new CoolItem (coolBar, itemStyle); | |
199 textItem.setControl (text); | |
200 textItem.addSelectionListener (new CoolItemSelectionListener()); | |
201 Point textSize = text.computeSize(DWT.DEFAULT, DWT.DEFAULT); | |
202 textSize = textItem.computeSize(textSize.x, textSize.y); | |
203 textItem.setMinimumSize(textSize); | |
204 textItem.setPreferredSize(textSize); | |
205 textItem.setSize(textSize); | |
206 } | |
207 | |
208 /* Set the sizes after adding all cool items */ | |
209 CoolItem[] coolItems = coolBar.getItems(); | |
210 for (int i = 0; i < coolItems.length; i++) { | |
211 CoolItem coolItem = coolItems[i]; | |
212 Control control = coolItem.getControl(); | |
213 Point size = control.computeSize(DWT.DEFAULT, DWT.DEFAULT); | |
214 Point coolSize = coolItem.computeSize(size.x, size.y); | |
215 if ( auto bar = cast(ToolBar)control ) { | |
216 if (bar.getItemCount() > 0) { | |
217 if (vertical) { | |
218 size.y = bar.getItem(0).getBounds().height; | |
219 } else { | |
220 size.x = bar.getItem(0).getWidth(); | |
221 } | |
222 } | |
223 } | |
224 coolItem.setMinimumSize(size); | |
225 coolItem.setPreferredSize(coolSize); | |
226 coolItem.setSize(coolSize); | |
227 } | |
228 | |
229 /* If we have saved state, restore it */ | |
230 if (order !is null && order.length is coolBar.getItemCount()) { | |
231 coolBar.setItemLayout(order, wrapIndices, sizes); | |
232 } else { | |
233 coolBar.setWrapIndices([1, 3]); | |
234 } | |
235 | |
236 /* Add a listener to resize the group box to match the coolbar */ | |
237 coolBar.addListener(DWT.Resize, new class() Listener { | |
238 public void handleEvent(Event event) { | |
239 exampleGroup.layout(); | |
240 } | |
241 }); | |
242 } | |
243 | |
244 /** | |
245 * Creates the "Style" group. | |
246 */ | |
247 void createStyleGroup() { | |
248 super.createStyleGroup(); | |
249 | |
250 /* Create the extra widgets */ | |
251 horizontalButton = new Button (styleGroup, DWT.RADIO); | |
252 horizontalButton.setText ("DWT.HORIZONTAL"); | |
253 verticalButton = new Button (styleGroup, DWT.RADIO); | |
254 verticalButton.setText ("DWT.VERTICAL"); | |
255 borderButton = new Button (styleGroup, DWT.CHECK); | |
256 borderButton.setText ("DWT.BORDER"); | |
257 flatButton = new Button (styleGroup, DWT.CHECK); | |
258 flatButton.setText ("DWT.FLAT"); | |
259 Group itemGroup = new Group(styleGroup, DWT.NONE); | |
260 itemGroup.setLayout (new GridLayout ()); | |
261 itemGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL)); | |
262 itemGroup.setText(ControlExample.getResourceString("Item_Styles")); | |
263 dropDownButton = new Button (itemGroup, DWT.CHECK); | |
264 dropDownButton.setText ("DWT.DROP_DOWN"); | |
265 } | |
266 | |
267 /** | |
268 * Disposes the "Example" widgets. | |
269 */ | |
270 void disposeExampleWidgets () { | |
271 /* store the state of the toolbar if applicable */ | |
272 if (coolBar !is null) { | |
273 sizes = coolBar.getItemSizes(); | |
274 wrapIndices = coolBar.getWrapIndices(); | |
275 order = coolBar.getItemOrder(); | |
276 } | |
277 super.disposeExampleWidgets(); | |
278 } | |
279 | |
280 /** | |
281 * Gets the "Example" widget children's items, if any. | |
282 * | |
283 * @return an array containing the example widget children's items | |
284 */ | |
285 Item [] getExampleWidgetItems () { | |
286 return coolBar.getItems(); | |
287 } | |
288 | |
289 /** | |
290 * Gets the "Example" widget children. | |
291 */ | |
292 Widget [] getExampleWidgets () { | |
293 return [ cast(Widget) coolBar]; | |
294 } | |
295 | |
296 /** | |
297 * Returns a list of set/get API method names (without the set/get prefix) | |
298 * that can be used to set/get values in the example control(s). | |
299 */ | |
300 char[][] getMethodNames() { | |
301 return ["ToolTipText"]; | |
302 } | |
303 | |
304 /** | |
305 * Gets the short text for the tab folder item. | |
306 */ | |
307 public char[] getShortTabText() { | |
308 return "CB"; | |
309 } | |
310 | |
311 /** | |
312 * Gets the text for the tab folder item. | |
313 */ | |
314 char[] getTabText () { | |
315 return "CoolBar"; | |
316 } | |
317 | |
318 /** | |
319 * Sets the state of the "Example" widgets. | |
320 */ | |
321 void setExampleWidgetState () { | |
322 super.setExampleWidgetState (); | |
323 horizontalButton.setSelection ((coolBar.getStyle () & DWT.HORIZONTAL) !is 0); | |
324 verticalButton.setSelection ((coolBar.getStyle () & DWT.VERTICAL) !is 0); | |
325 borderButton.setSelection ((coolBar.getStyle () & DWT.BORDER) !is 0); | |
326 flatButton.setSelection ((coolBar.getStyle () & DWT.FLAT) !is 0); | |
327 dropDownButton.setSelection ((coolBar.getItem(0).getStyle () & DWT.DROP_DOWN) !is 0); | |
328 lockedButton.setSelection(coolBar.getLocked()); | |
329 if (!instance.startup) setWidgetLocked (); | |
330 } | |
331 | |
332 /** | |
333 * Sets the header visible state of the "Example" widgets. | |
334 */ | |
335 void setWidgetLocked () { | |
336 coolBar.setLocked (lockedButton.getSelection ()); | |
337 } | |
338 | |
339 /** | |
340 * Listens to widgetSelected() events on DWT.DROP_DOWN type ToolItems | |
341 * and opens/closes a menu when appropriate. | |
342 */ | |
343 class DropDownSelectionListener : SelectionAdapter { | |
344 private Menu menu = null; | |
345 private bool visible = false; | |
346 | |
347 public void widgetSelected(SelectionEvent event) { | |
348 // Create the menu if it has not already been created | |
349 if (menu is null) { | |
350 // Lazy create the menu. | |
351 menu = new Menu(shell); | |
352 menu.addMenuListener(new class() MenuAdapter { | |
353 public void menuHidden(MenuEvent e) { | |
354 visible = false; | |
355 } | |
356 }); | |
357 for (int i = 0; i < 9; ++i) { | |
358 final char[] text = ControlExample.getResourceString("DropDownData_" ~ to!(char[])(i)); | |
359 if (text.length !is 0) { | |
360 MenuItem menuItem = new MenuItem(menu, DWT.NONE); | |
361 menuItem.setText(text); | |
362 /* | |
363 * Add a menu selection listener so that the menu is hidden | |
364 * when the user selects an item from the drop down menu. | |
365 */ | |
366 menuItem.addSelectionListener(new class() SelectionAdapter { | |
367 public void widgetSelected(SelectionEvent e) { | |
368 setMenuVisible(false); | |
369 } | |
370 }); | |
371 } else { | |
372 new MenuItem(menu, DWT.SEPARATOR); | |
373 } | |
374 } | |
375 } | |
376 | |
377 /** | |
378 * A selection event will be fired when a drop down tool | |
379 * item is selected in the main area and in the drop | |
380 * down arrow. Examine the event detail to determine | |
381 * where the widget was selected. | |
382 */ | |
383 if (event.detail is DWT.ARROW) { | |
384 /* | |
385 * The drop down arrow was selected. | |
386 */ | |
387 if (visible) { | |
388 // Hide the menu to give the Arrow the appearance of being a toggle button. | |
389 setMenuVisible(false); | |
390 } else { | |
391 // Position the menu below and vertically aligned with the the drop down tool button. | |
392 ToolItem toolItem = cast(ToolItem) event.widget; | |
393 ToolBar toolBar = toolItem.getParent(); | |
394 | |
395 Rectangle toolItemBounds = toolItem.getBounds(); | |
396 Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y)); | |
397 menu.setLocation(point.x, point.y + toolItemBounds.height); | |
398 setMenuVisible(true); | |
399 } | |
400 } else { | |
401 /* | |
402 * Main area of drop down tool item selected. | |
403 * An application would invoke the code to perform the action for the tool item. | |
404 */ | |
405 } | |
406 } | |
407 private void setMenuVisible(bool visible) { | |
408 menu.setVisible(visible); | |
409 this.visible = visible; | |
410 } | |
411 } | |
412 | |
413 /** | |
414 * Listens to widgetSelected() events on DWT.DROP_DOWN type CoolItems | |
415 * and opens/closes a menu when appropriate. | |
416 */ | |
417 class CoolItemSelectionListener : SelectionAdapter { | |
418 private Menu menu = null; | |
419 | |
420 public void widgetSelected(SelectionEvent event) { | |
421 /** | |
422 * A selection event will be fired when the cool item | |
423 * is selected by its gripper or if the drop down arrow | |
424 * (or 'chevron') is selected. Examine the event detail | |
425 * to determine where the widget was selected. | |
426 */ | |
427 if (event.detail is DWT.ARROW) { | |
428 /* If the popup menu is already up (i.e. user pressed arrow twice), | |
429 * then dispose it. | |
430 */ | |
431 if (menu !is null) { | |
432 menu.dispose(); | |
433 menu = null; | |
434 return; | |
435 } | |
436 | |
437 /* Get the cool item and convert its bounds to display coordinates. */ | |
438 CoolItem coolItem = cast(CoolItem) event.widget; | |
439 Rectangle itemBounds = coolItem.getBounds (); | |
440 itemBounds.width = event.x - itemBounds.x; | |
441 Point pt = coolBar.toDisplay(new Point (itemBounds.x, itemBounds.y)); | |
442 itemBounds.x = pt.x; | |
443 itemBounds.y = pt.y; | |
444 | |
445 /* Get the toolbar from the cool item. */ | |
446 ToolBar toolBar = cast(ToolBar) coolItem.getControl (); | |
447 ToolItem[] tools = toolBar.getItems (); | |
448 int toolCount = tools.length; | |
449 | |
450 /* Convert the bounds of each tool item to display coordinates, | |
451 * and determine which ones are past the bounds of the cool item. | |
452 */ | |
453 int i = 0; | |
454 while (i < toolCount) { | |
455 Rectangle toolBounds = tools[i].getBounds (); | |
456 pt = toolBar.toDisplay(new Point(toolBounds.x, toolBounds.y)); | |
457 toolBounds.x = pt.x; | |
458 toolBounds.y = pt.y; | |
459 Rectangle intersection = itemBounds.intersection (toolBounds); | |
460 if (intersection!=toolBounds) break; | |
461 i++; | |
462 } | |
463 | |
464 /* Create a pop-up menu with items for each of the hidden buttons. */ | |
465 menu = new Menu (coolBar); | |
466 for (int j = i; j < toolCount; j++) { | |
467 ToolItem tool = tools[j]; | |
468 Image image = tool.getImage(); | |
469 if (image is null) { | |
470 new MenuItem (menu, DWT.SEPARATOR); | |
471 } else { | |
472 if ((tool.getStyle() & DWT.DROP_DOWN) !is 0) { | |
473 MenuItem menuItem = new MenuItem (menu, DWT.CASCADE); | |
474 menuItem.setImage(image); | |
475 char[] text = tool.getToolTipText(); | |
476 if (text !is null) menuItem.setText(text); | |
477 Menu m = new Menu(menu); | |
478 menuItem.setMenu(m); | |
479 for (int k = 0; k < 9; ++k) { | |
480 text = ControlExample.getResourceString("DropDownData_" ~ to!(char[])(k)); | |
481 if (text.length !is 0) { | |
482 MenuItem mi = new MenuItem(m, DWT.NONE); | |
483 mi.setText(text); | |
484 /* Application code to perform the action for the submenu item would go here. */ | |
485 } else { | |
486 new MenuItem(m, DWT.SEPARATOR); | |
487 } | |
488 } | |
489 } else { | |
490 MenuItem menuItem = new MenuItem (menu, DWT.NONE); | |
491 menuItem.setImage(image); | |
492 char[] text = tool.getToolTipText(); | |
493 if (text !is null) menuItem.setText(text); | |
494 } | |
495 /* Application code to perform the action for the menu item would go here. */ | |
496 } | |
497 } | |
498 | |
499 /* Display the pop-up menu at the lower left corner of the arrow button. | |
500 * Dispose the menu when the user is done with it. | |
501 */ | |
502 pt = coolBar.toDisplay(new Point(event.x, event.y)); | |
503 menu.setLocation (pt.x, pt.y); | |
504 menu.setVisible (true); | |
505 while (menu !is null && !menu.isDisposed() && menu.isVisible ()) { | |
506 if (!display.readAndDispatch ()) display.sleep (); | |
507 } | |
508 if (menu !is null) { | |
509 menu.dispose (); | |
510 menu = null; | |
511 } | |
512 } | |
513 } | |
514 } | |
515 } |