root/branches/0.2/controlexample/coolbartab.d

Revision 46, 15.0 kB (checked in by JJR, 4 years ago)

* 0.2 Branch added: New working DWT version submitted by Shawn Liu.

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 module controlexample.coolbartab;
12
13  private {
14 import swt.all;
15 import controlexample.tab;
16 import controlexample.controlexample;
17 }
18
19
20 class CoolBarTab : Tab {
21     Button dropDownButton, lockedButton, flatButton;
22
23     /* Example widgets and group that contains them */
24     CoolBar coolBar;
25     CoolItem pushItem, dropDownItem, radioItem, checkItem;
26     Group coolBarGroup;
27    
28     Point[] sizes;
29     int[] wrapIndices;
30     int[] order;
31    
32     /**
33      * Creates the Tab within a given instance_ren of ControlExample.
34      */
35     this(ControlExample instance_ren) {
36         super(instance_ren);
37     }
38    
39     /**
40      * Creates the "Other" group.
41      */
42     void createOtherGroup () {
43         super.createOtherGroup ();
44    
45         /* Create display controls specific to this example */
46         lockedButton = new Button (otherGroup, SWT.CHECK);
47         lockedButton.setText (ControlExample.getResourceString("Locked"));
48    
49         /* Add the listeners */
50         lockedButton.handleEvent(this, SWT.Selection, &setWidgetLocked);
51     }
52    
53     /**
54      * Creates the "Example" group.
55      */
56     void createExampleGroup () {
57         super.createExampleGroup ();
58         coolBarGroup = new Group (exampleGroup, SWT.NONE);
59         coolBarGroup.setLayout (new GridLayout ());
60         coolBarGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
61         coolBarGroup.setText ("CoolBar");
62     }
63    
64     /**
65      * Creates the "Example" widgets.
66      */
67     void createExampleWidgets () {
68         int style = getDefaultStyle(), itemStyle = 0;
69
70         /* Compute the widget style */
71         int toolBarStyle = SWT.FLAT;
72         if (borderButton.getSelection()) style |= SWT.BORDER;
73         if (flatButton.getSelection()) style |= SWT.FLAT;
74         if (dropDownButton.getSelection()) itemStyle |= SWT.DROP_DOWN;
75    
76         /*
77         * Create the example widgets.
78         */
79         coolBar = new CoolBar (coolBarGroup, style);
80        
81         /* create the push button toolbar */
82         ToolBar toolBar = new ToolBar (coolBar, toolBarStyle);
83         ToolItem item = new ToolItem (toolBar, SWT.PUSH);
84         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
85         item.setToolTipText ("SWT.PUSH");
86         item = new ToolItem (toolBar, SWT.PUSH);
87         item.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
88         item.setToolTipText ("SWT.PUSH");
89         item = new ToolItem (toolBar, SWT.PUSH);
90         item.setImage (instance_ren.images[ControlExample.ciTarget]);
91         item.setToolTipText ("SWT.PUSH");
92         item = new ToolItem (toolBar, SWT.SEPARATOR);
93         item = new ToolItem (toolBar, SWT.PUSH);
94         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
95         item.setToolTipText ("SWT.PUSH");
96         item = new ToolItem (toolBar, SWT.PUSH);
97         item.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
98         item.setToolTipText ("SWT.PUSH");       
99         pushItem = new CoolItem (coolBar, itemStyle);
100         pushItem.setControl (toolBar);
101         Point pushSize = (cast(Control)toolBar).computeSize(SWT.DEFAULT, SWT.DEFAULT);
102         pushSize = pushItem.computeSize(pushSize.x, pushSize.y);
103         pushItem.setSize(pushSize);
104         pushItem.setMinimumSize(item.getWidth(), pushSize.y);
105         pushItem.handleSelection(this, &onCoolItemSelection);
106                
107         /* create the dropdown toolbar */
108         toolBar = new ToolBar (coolBar, toolBarStyle);
109         item = new ToolItem (toolBar, SWT.DROP_DOWN);
110         item.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
111         item.setToolTipText ("SWT.DROP_DOWN");
112         item.handleSelection (this, &onDropDownSelection);
113         item = new ToolItem (toolBar, SWT.DROP_DOWN);
114         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
115         item.setToolTipText ("SWT.DROP_DOWN");
116         item.handleSelection(this, &onDropDownSelection);
117         dropDownItem = new CoolItem (coolBar, itemStyle);
118         dropDownItem.setControl (toolBar);
119         Point dropSize = (cast(Control)toolBar).computeSize(SWT.DEFAULT, SWT.DEFAULT);
120         dropSize = dropDownItem.computeSize(dropSize.x, dropSize.y);
121         dropDownItem.setSize(dropSize);
122         dropDownItem.setMinimumSize(item.getWidth(), dropSize.y);
123         dropDownItem.handleSelection(this, &onCoolItemSelection);
124                
125         /* create the radio button toolbar */
126         toolBar = new ToolBar (coolBar, toolBarStyle);
127         item = new ToolItem (toolBar, SWT.RADIO);
128         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
129         item.setToolTipText ("SWT.RADIO");
130         item = new ToolItem (toolBar, SWT.RADIO);
131         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
132         item.setToolTipText ("SWT.RADIO");
133         item = new ToolItem (toolBar, SWT.RADIO);
134         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
135         item.setToolTipText ("SWT.RADIO");
136         radioItem = new CoolItem (coolBar, itemStyle);
137         radioItem.setControl (toolBar);
138         Point radioSize = (cast(Control)toolBar).computeSize(SWT.DEFAULT, SWT.DEFAULT);
139         radioSize = radioItem.computeSize(radioSize.x, radioSize.y);
140         radioItem.setSize(radioSize);
141         radioItem.setMinimumSize(item.getWidth(), radioSize.y);
142         radioItem.handleSelection(this, &onCoolItemSelection);
143        
144         /* create the check button toolbar */
145         toolBar = new ToolBar (coolBar, toolBarStyle);
146         item = new ToolItem (toolBar, SWT.CHECK);
147         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
148         item.setToolTipText ("SWT.CHECK");
149         item = new ToolItem (toolBar, SWT.CHECK);
150         item.setImage (instance_ren.images[ControlExample.ciTarget]);
151         item.setToolTipText ("SWT.CHECK");
152         item = new ToolItem (toolBar, SWT.CHECK);
153         item.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
154         item.setToolTipText ("SWT.CHECK");
155         item = new ToolItem (toolBar, SWT.CHECK);
156         item.setImage (instance_ren.images[ControlExample.ciTarget]);
157         item.setToolTipText ("SWT.CHECK");
158         checkItem = new CoolItem (coolBar, itemStyle);
159         checkItem.setControl (toolBar);
160         Point checkSize = (cast(Control)toolBar).computeSize(SWT.DEFAULT, SWT.DEFAULT);
161         checkSize = checkItem.computeSize(checkSize.x, checkSize.y);
162         checkItem.setSize(checkSize);
163         checkItem.setMinimumSize(item.getWidth(), checkSize.y);
164         checkItem.handleSelection(this, &onCoolItemSelection);
165        
166         /* if we have saved state, restore it */
167         if (order !== null) {
168             coolBar.setItemLayout(order, wrapIndices, sizes);
169             /*
170              * special case: because setItemLayout will restore the items
171              * to the sizes the user left them at, the preferred size may not
172              * be the same as the actual size. Thus we must explicitly set
173              * the preferred sizes.
174              */
175             pushItem.setPreferredSize(pushSize);
176             dropDownItem.setPreferredSize(dropSize);
177             radioItem.setPreferredSize(radioSize);
178             checkItem.setPreferredSize(checkSize);
179         }
180         else {
181             int[] intArr = new int[2];
182             intArr[0] = 1;
183             intArr[1] = 3;
184             coolBar.setWrapIndices(intArr);
185         }
186        
187         /* add a listener to resize the group box to match the coolbar */
188         coolBar.handleEvent(this, SWT.Resize, delegate(Event e) {
189             CoolBarTab c = cast(CoolBarTab)e.cData;
190             c.exampleGroup.layout();
191         });
192     }
193    
194     /**
195      * Creates the "Style" group.
196      */
197     void createStyleGroup() {
198         super.createStyleGroup();
199    
200         /* Create the extra widget */
201         borderButton = new Button (styleGroup, SWT.CHECK);
202         borderButton.setText ("SWT.BORDER");
203         flatButton = new Button (styleGroup, SWT.CHECK);
204         flatButton.setText ("SWT.FLAT");
205         Group itemGroup = new Group(styleGroup, SWT.NONE);
206         itemGroup.setLayout (new GridLayout ());
207         itemGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
208         itemGroup.setText(ControlExample.getResourceString("Item_Styles"));
209         dropDownButton = new Button (itemGroup, SWT.CHECK);
210         dropDownButton.setText ("SWT.DROP_DOWN");
211         dropDownButton.handleEvent(this, SWT.Selection, &recreateExampleWidgets);
212     }
213    
214     /**
215      * Disposes the "Example" widgets.
216      */
217     void disposeExampleWidgets () {
218         /* store the state of the toolbar if applicable */
219         if (coolBar !== null) {
220             sizes = coolBar.getItemSizes();
221             wrapIndices = coolBar.getWrapIndices();
222             order = coolBar.getItemOrder();
223         }
224         super.disposeExampleWidgets(); 
225     }
226
227     /**
228      * Gets the "Example" widget children's items, if any.
229      *
230      * @return an array containing the example widget children's items
231      */
232     Item [] getExampleWidgetItems () {
233         return coolBar.getItems();
234     }
235    
236     /**
237      * Gets the "Example" widget children.
238      */
239     Control [] getExampleWidgets () {
240         Control[] result;
241         result ~= coolBar;
242         return result;
243     }
244    
245     /**
246      * Gets the text for the tab folder item.
247      */
248     String getTabText () {
249         return "CoolBar";
250     }
251    
252     /**
253      * Sets the state of the "Example" widgets.
254      */
255     void setExampleWidgetState () {
256         super.setExampleWidgetState ();
257         borderButton.setSelection ((coolBar.getStyle () & SWT.BORDER) != 0);
258         flatButton.setSelection ((coolBar.getStyle () & SWT.FLAT) != 0);
259         dropDownButton.setSelection ((coolBar.getItem(0).getStyle () & SWT.DROP_DOWN) != 0);
260         setWidgetLocked ();
261     }
262    
263     /**
264      * Sets the header visible state of the "Example" widgets.
265      */
266     void setWidgetLocked () {
267         coolBar.setLocked (lockedButton.getSelection ());
268     }
269    
270     /**
271      * Listens to widgetSelected() events on DWT.DROP_DOWN type ToolItems
272      * and opens/closes a menu when appropriate.
273      */
274  public void onDropDownSelection(SelectionEvent event) {
275         Menu menu = null;
276         BOOL visible = false;
277
278         void setMenuVisible(BOOL _visible) {
279             menu.setVisible(_visible);
280             visible = _visible;
281         }
282         void onMenuHidden(MenuEvent e) { visible = false;}
283
284         // Create the menu if it has not already been created
285         if (menu is null) {
286             // Lazy create the menu.
287             Shell shell = tabFolderPage.getShell();
288             menu = new Menu(shell);
289 // TODO:           
290 //          menu.handleMenuHide(this, &onMenuHidden);
291            
292             for (int i = 0; i < 9; ++i) {
293                 String text = ControlExample.getResourceString("DropDownData_" ~std.string.toString(i));
294                 if (text.length != 0) {
295                     MenuItem menuItem = new MenuItem(menu, SWT.NONE);
296                     menuItem.setText(text);
297                     /*
298                      * Add a menu selection listener so that the menu is hidden
299                      * when the user selects an item from the drop down menu.
300                      */                 
301                     menuItem.handleEvent(menu, SWT.Selection, delegate(Event e) {
302                         Menu menu = cast(Menu)e.cData;
303                         menu.setVisible(false);
304 //                      setMenuVisible(false);
305                     });
306                 } else {
307                     new MenuItem(menu, SWT.SEPARATOR);
308                 }
309             }
310         }
311        
312         /**
313          * A selection event will be fired when a drop down tool
314          * item is selected in the main area and in the drop
315          * down arrow.  Examine the event detail to determine
316          * where the widget was selected.
317          */     
318         if (event.detail == SWT.ARROW) {
319             /*
320              * The drop down arrow was selected.
321              */
322             if (visible) {
323                 // Hide the menu to give the Arrow the appearance of being a toggle button.
324                 setMenuVisible(false);
325             } else {   
326                 // Position the menu below and vertically aligned with the the drop down tool button.
327                 ToolItem toolItem = cast(ToolItem) event.widget;
328                 ToolBar  toolBar = toolItem.getParent();
329                
330                 Rectangle toolItemBounds = toolItem.getBounds();
331                 Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y));
332                 menu.setLocation(point.x, point.y + toolItemBounds.height);
333                 setMenuVisible(true);
334             }
335         } else {
336             /*
337              * Main area of drop down tool item selected.
338              * An application would invoke the code to perform the action for the tool item.
339              */
340         }
341     }
342        
343
344     /**
345      * Listens to widgetSelected() events on DWT.DROP_DOWN type CoolItems
346      * and opens/closes a menu when appropriate.
347      */
348     public void onCoolItemSelection(SelectionEvent event) {
349         Menu menu = null;
350         /**
351          * A selection event will be fired when the cool item
352          * is selected by its gripper or if the drop down arrow
353          * (or 'chevron') is selected. Examine the event detail
354          * to determine where the widget was selected.
355          */
356         if (event.detail == SWT.ARROW) {
357             /* If the popup menu is already up (i.e. user pressed arrow twice),
358              * then dispose it.
359              */
360             if (menu !== null) {
361                 menu.dispose();
362                 menu = null;
363                 return;
364             }
365            
366             /* Get the cool item and convert its bounds to display coordinates. */
367             CoolItem coolItem = cast(CoolItem) event.widget;
368             Rectangle itemBounds = coolItem.getBounds ();
369             Point pt = coolBar.toDisplay(new Point (itemBounds.x, itemBounds.y));
370             itemBounds.x = pt.x;
371             itemBounds.y = pt.y;
372            
373             /* Get the toolbar from the cool item. */
374             ToolBar toolBar = cast(ToolBar) coolItem.getControl ();
375             ToolItem[] tools = toolBar.getItems ();
376             int toolCount = tools.length;
377                            
378             /* Convert the bounds of each tool item to display coordinates,
379              * and determine which ones are past the bounds of the cool item.
380              */
381             int i = 0;
382             while (i < toolCount) {
383                 Rectangle toolBounds = tools[i].getBounds ();
384                 pt = toolBar.toDisplay(new Point(toolBounds.x, toolBounds.y));
385                 toolBounds.x = pt.x;
386                 toolBounds.y = pt.y;
387                 Rectangle intersection = itemBounds.intersection (toolBounds);
388                 if (!intersection.equals (toolBounds)) break;
389                 i++;
390             }
391            
392             /* Create a pop-up menu with items for each of the hidden buttons. */
393             menu = new Menu (coolBar);
394             for (int j = i; j < toolCount; j++) {
395                 ToolItem tool = tools[j];
396                 Image image = tool.getImage();
397                 if (image is null) {
398                     new MenuItem (menu, SWT.SEPARATOR);
399                 } else {
400                     if ((tool.getStyle() & SWT.DROP_DOWN) != 0) {
401                         MenuItem menuItem = new MenuItem (menu, SWT.CASCADE);
402                         menuItem.setImage(image);
403                         Menu m = new Menu(menu);
404                         menuItem.setMenu(m);
405                         for (int k = 0; k < 9; ++k) {
406                             String text = ControlExample.getResourceString("DropDownData_" ~std.string.toString(k));
407                             if (text.length != 0) {
408                                 MenuItem mi = new MenuItem(m, SWT.NONE);
409                                 mi.setText(text);
410                                 /* Application code to perform the action for the submenu item would go here. */
411                             } else {
412                                 new MenuItem(m, SWT.SEPARATOR);
413                             }
414                         }
415                     } else {
416                         MenuItem menuItem = new MenuItem (menu, SWT.NONE);
417                         menuItem.setImage(image);
418                     }
419                     /* Application code to perform the action for the menu item would go here. */
420
421                 }
422             }
423            
424             /* Display the pop-up menu at the lower left corner of the arrow button.
425              * Dispose the menu when the user is done with it.
426              */
427             pt = coolBar.toDisplay(new Point(event.x, event.y));
428             menu.setLocation (pt.x, pt.y);
429             menu.setVisible (true);
430             Display display = coolBar.getDisplay ();
431             while (menu !== null && !menu.isDisposed() && menu.isVisible ()) {
432                 if (!display.readAndDispatch ()) display.sleep ();
433             }
434             if (menu !== null) {
435                 menu.dispose ();
436                 menu = null;
437             }
438         }
439     }
440 }
Note: See TracBrowser for help on using the browser.