root/branches/0.2/controlexample/toolbartab.d

Revision 46, 10.3 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.toolbartab;
12
13 private {
14 import swt.all;
15 import controlexample.tab;
16 import controlexample.controlexample;
17 }
18
19 class ToolBarTab : Tab {
20     /* Example widgets and groups that contain them */
21     ToolBar imageToolBar, textToolBar;
22     Group imageToolBarGroup, textToolBarGroup;
23    
24     /* Style widgets added to the "Style" group */
25     Button horizontalButton, verticalButton, flatButton, shadowOutButton, wrapButton, rightButton;
26
27     /**
28      * Creates the Tab within a given instance_ren of ControlExample.
29      */
30     this(ControlExample instance_ren) {
31         super(instance_ren);
32     }
33
34     /**
35      * Creates the "Example" group.
36      */
37     void createExampleGroup () {
38         super.createExampleGroup ();
39        
40         /* Create a group for the image tool bar */
41         imageToolBarGroup = new Group (exampleGroup, SWT.NONE);
42         imageToolBarGroup.setLayout (new GridLayout ());
43         imageToolBarGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
44         imageToolBarGroup.setText (ControlExample.getResourceString("Image_ToolBar"));
45    
46         /* Create a group for the text tool bar */
47         textToolBarGroup = new Group (exampleGroup, SWT.NONE);
48         textToolBarGroup.setLayout (new GridLayout ());
49         textToolBarGroup.setLayoutData (new GridData (GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
50         textToolBarGroup.setText (ControlExample.getResourceString("Text_ToolBar"));
51     }
52    
53     /**
54      * Creates the "Example" widgets.
55      */
56     void createExampleWidgets () {
57    
58         /* Compute the widget style */
59         int style = getDefaultStyle();
60         if (horizontalButton.getSelection()) style |= SWT.HORIZONTAL;
61         if (verticalButton.getSelection()) style |= SWT.VERTICAL;
62         if (flatButton.getSelection()) style |= SWT.FLAT;
63         if (wrapButton.getSelection()) style |= SWT.WRAP;
64         if (borderButton.getSelection()) style |= SWT.BORDER;
65         if (shadowOutButton.getSelection()) style |= SWT.SHADOW_OUT;
66         if (rightButton.getSelection()) style |= SWT.RIGHT;
67    
68         /*
69         * Create the example widgets.
70         *
71         * A tool bar must consist of all image tool
72         * items or all text tool items but not both.
73         */
74    
75         /* Create the image tool bar */
76         imageToolBar = new ToolBar (imageToolBarGroup, style);
77         ToolItem item = new ToolItem (imageToolBar, SWT.PUSH);
78         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
79         item.setToolTipText("SWT.PUSH");
80         item = new ToolItem (imageToolBar, SWT.PUSH);
81         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
82         item.setToolTipText ("SWT.PUSH");
83         item = new ToolItem (imageToolBar, SWT.RADIO);
84         item.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
85         item.setToolTipText ("SWT.RADIO");
86         item = new ToolItem (imageToolBar, SWT.RADIO);
87         item.setImage (instance_ren.images[ControlExample.ciOpenFolder]);
88         item.setToolTipText ("SWT.RADIO");
89         item = new ToolItem (imageToolBar, SWT.CHECK);
90         item.setImage (instance_ren.images[ControlExample.ciTarget]);
91         item.setToolTipText ("SWT.CHECK");
92         item = new ToolItem (imageToolBar, SWT.RADIO);
93         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
94         item.setToolTipText ("SWT.RADIO");
95         item = new ToolItem (imageToolBar, SWT.RADIO);
96         item.setImage (instance_ren.images[ControlExample.ciClosedFolder]);
97         item.setToolTipText ("SWT.RADIO");
98         item = new ToolItem (imageToolBar, SWT.SEPARATOR);
99         item.setToolTipText("SWT.SEPARATOR");
100         item = new ToolItem (imageToolBar, SWT.DROP_DOWN);
101         item.setImage (instance_ren.images[ControlExample.ciTarget]);
102         item.setToolTipText ("SWT.DROP_DOWN");
103         item.handleSelection(this, &onDropDownSelected);
104    
105         /* Create the text tool bar */
106         textToolBar = new ToolBar (textToolBarGroup, style);
107         item = new ToolItem (textToolBar, SWT.PUSH);
108         item.setText (ControlExample.getResourceString("Push"));
109         item.setToolTipText("SWT.PUSH");
110         item = new ToolItem (textToolBar, SWT.PUSH);
111         item.setText (ControlExample.getResourceString("Push"));
112         item.setToolTipText("SWT.PUSH");
113         item = new ToolItem (textToolBar, SWT.RADIO);
114         item.setText (ControlExample.getResourceString("Radio"));
115         item.setToolTipText("SWT.RADIO");
116         item = new ToolItem (textToolBar, SWT.RADIO);
117         item.setText (ControlExample.getResourceString("Radio"));
118         item.setToolTipText("SWT.RADIO");
119         item = new ToolItem (textToolBar, SWT.CHECK);
120         item.setText (ControlExample.getResourceString("Check"));
121         item.setToolTipText("SWT.CHECK");
122         item = new ToolItem (textToolBar, SWT.RADIO);
123         item.setText (ControlExample.getResourceString("Radio"));
124         item.setToolTipText("SWT.RADIO");
125         item = new ToolItem (textToolBar, SWT.RADIO);
126         item.setText (ControlExample.getResourceString("Radio"));
127         item.setToolTipText("SWT.RADIO");
128         item = new ToolItem (textToolBar, SWT.SEPARATOR);
129         item.setToolTipText("SWT.SEPARATOR");
130         item = new ToolItem (textToolBar, SWT.DROP_DOWN);
131         item.setText (ControlExample.getResourceString("Drop_Down"));
132         item.setToolTipText("SWT.DROP_DOWN");
133         item.handleSelection(this, &onDropDownSelected);
134    
135         /*
136         * Do not add the selection event for this drop down
137         * tool item.  Without hooking the event, the drop down
138         * widget does nothing special when the drop down area
139         * is selected.
140         */
141     }
142    
143     /**
144      * Creates the "Style" group.
145      */
146     void createStyleGroup() {
147         super.createStyleGroup();
148    
149         /* Create the extra widgets */
150         horizontalButton = new Button (styleGroup, SWT.RADIO);
151         horizontalButton.setText ("SWT.HORIZONTAL");
152         verticalButton = new Button (styleGroup, SWT.RADIO);
153         verticalButton.setText ("SWT.VERTICAL");
154         flatButton = new Button (styleGroup, SWT.CHECK);
155         flatButton.setText ("SWT.FLAT");
156         shadowOutButton = new Button (styleGroup, SWT.CHECK);
157         shadowOutButton.setText ("SWT.SHADOW_OUT");
158         wrapButton = new Button (styleGroup, SWT.CHECK);
159         wrapButton.setText ("SWT.WRAP");
160         rightButton = new Button (styleGroup, SWT.CHECK);
161         rightButton.setText ("SWT.RIGHT");
162         borderButton = new Button (styleGroup, SWT.CHECK);
163         borderButton.setText ("SWT.BORDER");
164     }
165    
166     void disposeExampleWidgets () {
167         super.disposeExampleWidgets ();
168     }
169    
170     /**
171      * Gets the "Example" widget children's items, if any.
172      *
173      * @return an array containing the example widget children's items
174      */
175     Item [] getExampleWidgetItems () {
176         Item [] imageToolBarItems = imageToolBar.getItems();
177         Item [] textToolBarItems = textToolBar.getItems();
178         Item [] allItems = new Item [imageToolBarItems.length + textToolBarItems.length];
179         System.arraycopy(imageToolBarItems, 0, allItems, 0, imageToolBarItems.length);
180         System.arraycopy(textToolBarItems, 0, allItems, imageToolBarItems.length, textToolBarItems.length);
181         return allItems;
182     }
183    
184     /**
185      * Gets the "Example" widget children.
186      */
187     Control [] getExampleWidgets () {
188         Control[] controls;
189         controls ~= imageToolBar;
190         controls ~= textToolBar;
191         return controls;
192     }
193    
194     /**
195      * Gets the text for the tab folder item.
196      */
197     String getTabText () {
198         return "ToolBar";
199     }
200    
201     /**
202      * Sets the state of the "Example" widgets.
203      */
204     void setExampleWidgetState () {
205         super.setExampleWidgetState ();
206         horizontalButton.setSelection ((imageToolBar.getStyle () & SWT.HORIZONTAL) != 0);
207         verticalButton.setSelection ((imageToolBar.getStyle () & SWT.VERTICAL) != 0);
208         flatButton.setSelection ((imageToolBar.getStyle () & SWT.FLAT) != 0);
209         wrapButton.setSelection ((imageToolBar.getStyle () & SWT.WRAP) != 0);
210         shadowOutButton.setSelection ((imageToolBar.getStyle () & SWT.SHADOW_OUT) != 0);
211         borderButton.setSelection ((imageToolBar.getStyle () & SWT.BORDER) != 0);
212         rightButton.setSelection ((imageToolBar.getStyle () & SWT.RIGHT) != 0);
213     }
214    
215     /**
216      * Listens to widgetSelected() events on DWT.DROP_DOWN type ToolItems
217      * and opens/closes a menu when appropriate.
218      */
219     public void onDropDownSelected(SelectionEvent event) {
220         Menu    menu = null;
221         BOOL visible = false;
222
223         void setMenuVisible(BOOL _visible) {
224             menu.setVisible(_visible);
225             visible = _visible;
226         }
227         // Create the menu if it has not already been created
228         if (menu is null) {
229             // Lazy create the menu.
230             Shell shell = tabFolderPage.getShell();
231             menu = new Menu(shell);
232             for (int i = 0; i < 9; ++i) {
233                 String text = ControlExample.getResourceString("DropDownData_" ~ std.string.toString(i));
234                 if (text.length != 0) {
235                     MenuItem menuItem = new MenuItem(menu, SWT.NONE);
236                     menuItem.setText(text);
237                     /*
238                      * Add a menu selection listener so that the menu is hidden
239                      * when the user selects an item from the drop down menu.
240                      */
241                     menuItem.handleSelection(menu, delegate(SelectionEvent e) {
242                         Menu c = cast(Menu)e.cData;
243                         c.setVisible(false);
244                     });
245                 } else {
246                     new MenuItem(menu, SWT.SEPARATOR);
247                 }
248             }
249         }
250        
251         /**
252          * A selection event will be fired when a drop down tool
253          * item is selected in the main area and in the drop
254          * down arrow.  Examine the event detail to determine
255          * where the widget was selected.
256          */     
257         if (event.detail == SWT.ARROW) {
258             /*
259              * The drop down arrow was selected.
260              */
261             if (visible) {
262                 // Hide the menu to give the Arrow the appearance of being a toggle button.
263                 setMenuVisible(false);
264             } else {   
265                 // Position the menu below and vertically aligned with the the drop down tool button.
266                 ToolItem toolItem = cast(ToolItem) event.widget;
267                 ToolBar  toolBar = toolItem.getParent();
268                
269                 Rectangle toolItemBounds = toolItem.getBounds();
270                 Point point = toolBar.toDisplay(new Point(toolItemBounds.x, toolItemBounds.y));
271                 menu.setLocation(point.x, point.y + toolItemBounds.height);
272                 setMenuVisible(true);
273             }
274         } else {
275             /*
276              * Main area of drop down tool item selected.
277              * An application would invoke the code to perform the action for the tool item.
278              */
279         }
280
281     }
282 }
Note: See TracBrowser for help on using the browser.