78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2005 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.MenuTab;
|
|
14
|
|
15
|
|
16
|
|
17 import dwt.DWT;
|
|
18 import dwt.events.PaintEvent;
|
|
19 import dwt.events.PaintListener;
|
|
20 import dwt.events.SelectionAdapter;
|
|
21 import dwt.events.SelectionEvent;
|
|
22 import dwt.layout.GridData;
|
|
23 import dwt.layout.GridLayout;
|
|
24 import dwt.widgets.Button;
|
|
25 import dwt.widgets.Group;
|
|
26 import dwt.widgets.Label;
|
|
27 import dwt.widgets.Menu;
|
|
28 import dwt.widgets.MenuItem;
|
|
29 import dwt.widgets.Shell;
|
|
30
|
|
31 import examples.controlexample.Tab;
|
|
32 import examples.controlexample.ControlExample;
|
|
33
|
|
34 import dwt.dwthelper.utils;
|
|
35
|
|
36 import tango.util.Convert;
|
|
37
|
|
38 class MenuTab : Tab {
|
|
39 /* Widgets added to the "Menu Style", "MenuItem Style" and "Other" groups */
|
|
40 Button barButton, dropDownButton, popUpButton, noRadioGroupButton, leftToRightButton, rightToLeftButton;
|
|
41 Button checkButton, cascadeButton, pushButton, radioButton, separatorButton;
|
|
42 Button imagesButton, acceleratorsButton, mnemonicsButton, subMenuButton, subSubMenuButton;
|
|
43 Button createButton, closeAllButton;
|
|
44 Group menuItemStyleGroup;
|
|
45
|
|
46 /* Variables used to track the open shells */
|
|
47 int shellCount = 0;
|
|
48 Shell [] shells;
|
|
49
|
|
50 /**
|
|
51 * Creates the Tab within a given instance of ControlExample.
|
|
52 */
|
|
53 this(ControlExample instance) {
|
|
54 super(instance);
|
|
55 shells = new Shell [4];
|
|
56 }
|
|
57
|
|
58 /**
|
|
59 * Close all the example shells.
|
|
60 */
|
|
61 void closeAllShells() {
|
|
62 for (int i = 0; i<shellCount; i++) {
|
|
63 if (shells[i] !is null & !shells [i].isDisposed ()) {
|
|
64 shells [i].dispose();
|
|
65 }
|
|
66 }
|
|
67 shellCount = 0;
|
|
68 }
|
|
69
|
|
70 /**
|
|
71 * Handle the Create button selection event.
|
|
72 *
|
|
73 * @param event org.eclipse.swt.events.SelectionEvent
|
|
74 */
|
|
75 public void createButtonSelected(SelectionEvent event) {
|
|
76
|
|
77 /*
|
|
78 * Remember the example shells so they
|
|
79 * can be disposed by the user.
|
|
80 */
|
|
81 if (shellCount >= shells.length) {
|
|
82 Shell [] newShells = new Shell [shells.length + 4];
|
|
83 System.arraycopy (shells, 0, newShells, 0, shells.length);
|
|
84 shells = newShells;
|
|
85 }
|
|
86
|
|
87 int orientation = 0;
|
|
88 if (leftToRightButton.getSelection()) orientation |= DWT.LEFT_TO_RIGHT;
|
|
89 if (rightToLeftButton.getSelection()) orientation |= DWT.RIGHT_TO_LEFT;
|
|
90 int radioBehavior = 0;
|
|
91 if (noRadioGroupButton.getSelection()) radioBehavior |= DWT.NO_RADIO_GROUP;
|
|
92
|
|
93 /* Create the shell and menu(s) */
|
|
94 Shell shell = new Shell (DWT.SHELL_TRIM | orientation);
|
|
95 shells [shellCount] = shell;
|
|
96 if (barButton.getSelection ()) {
|
|
97 /* Create menu bar. */
|
|
98 Menu menuBar = new Menu(shell, DWT.BAR | radioBehavior);
|
|
99 shell.setMenuBar(menuBar);
|
|
100 hookListeners(menuBar);
|
|
101
|
|
102 if (dropDownButton.getSelection() && cascadeButton.getSelection()) {
|
|
103 /* Create cascade button and drop-down menu in menu bar. */
|
|
104 MenuItem item = new MenuItem(menuBar, DWT.CASCADE);
|
|
105 item.setText(getMenuItemText("Cascade"));
|
|
106 if (imagesButton.getSelection()) item.setImage(instance.images[ControlExample.ciOpenFolder]);
|
|
107 hookListeners(item);
|
|
108 Menu dropDownMenu = new Menu(shell, DWT.DROP_DOWN | radioBehavior);
|
|
109 item.setMenu(dropDownMenu);
|
|
110 hookListeners(dropDownMenu);
|
|
111
|
|
112 /* Create various menu items, depending on selections. */
|
|
113 createMenuItems(dropDownMenu, subMenuButton.getSelection(), subSubMenuButton.getSelection());
|
|
114 }
|
|
115 }
|
|
116
|
|
117 if (popUpButton.getSelection()) {
|
|
118 /* Create pop-up menu. */
|
|
119 Menu popUpMenu = new Menu(shell, DWT.POP_UP | radioBehavior);
|
|
120 shell.setMenu(popUpMenu);
|
|
121 hookListeners(popUpMenu);
|
|
122
|
|
123 /* Create various menu items, depending on selections. */
|
|
124 createMenuItems(popUpMenu, subMenuButton.getSelection(), subSubMenuButton.getSelection());
|
|
125 }
|
|
126
|
|
127 /* Set the size, title and open the shell. */
|
|
128 shell.setSize (300, 100);
|
|
129 shell.setText (ControlExample.getResourceString("Title") ~ to!(char[])(shellCount));
|
|
130 shell.addPaintListener(new class() PaintListener {
|
|
131 public void paintControl(PaintEvent e) {
|
|
132 e.gc.drawString(ControlExample.getResourceString("PopupMenuHere"), 20, 20);
|
|
133 }
|
|
134 });
|
|
135 shell.open ();
|
|
136 shellCount++;
|
|
137 }
|
|
138
|
|
139 /**
|
|
140 * Creates the "Control" group.
|
|
141 */
|
|
142 void createControlGroup () {
|
|
143 /*
|
|
144 * Create the "Control" group. This is the group on the
|
|
145 * right half of each example tab. For MenuTab, it consists of
|
|
146 * the Menu style group, the MenuItem style group and the 'other' group.
|
|
147 */
|
|
148 controlGroup = new Group (tabFolderPage, DWT.NONE);
|
|
149 controlGroup.setLayout (new GridLayout (2, true));
|
|
150 controlGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
|
|
151 controlGroup.setText (ControlExample.getResourceString("Parameters"));
|
|
152
|
|
153 /* Create a group for the menu style controls */
|
|
154 styleGroup = new Group (controlGroup, DWT.NONE);
|
|
155 styleGroup.setLayout (new GridLayout ());
|
|
156 styleGroup.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
|
|
157 styleGroup.setText (ControlExample.getResourceString("Menu_Styles"));
|
|
158
|
|
159 /* Create a group for the menu item style controls */
|
|
160 menuItemStyleGroup = new Group (controlGroup, DWT.NONE);
|
|
161 menuItemStyleGroup.setLayout (new GridLayout ());
|
|
162 menuItemStyleGroup.setLayoutData (new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
|
|
163 menuItemStyleGroup.setText (ControlExample.getResourceString("MenuItem_Styles"));
|
|
164
|
|
165 /* Create a group for the 'other' controls */
|
|
166 otherGroup = new Group (controlGroup, DWT.NONE);
|
|
167 otherGroup.setLayout (new GridLayout ());
|
|
168 otherGroup.setLayoutData (new GridData(GridData.HORIZONTAL_ALIGN_FILL | GridData.VERTICAL_ALIGN_FILL));
|
|
169 otherGroup.setText (ControlExample.getResourceString("Other"));
|
|
170 }
|
|
171
|
|
172 /**
|
|
173 * Creates the "Control" widget children.
|
|
174 */
|
|
175 void createControlWidgets () {
|
|
176
|
|
177 /* Create the menu style buttons */
|
|
178 barButton = new Button (styleGroup, DWT.CHECK);
|
|
179 barButton.setText ("DWT.BAR");
|
|
180 dropDownButton = new Button (styleGroup, DWT.CHECK);
|
|
181 dropDownButton.setText ("DWT.DROP_DOWN");
|
|
182 popUpButton = new Button (styleGroup, DWT.CHECK);
|
|
183 popUpButton.setText ("DWT.POP_UP");
|
|
184 noRadioGroupButton = new Button (styleGroup, DWT.CHECK);
|
|
185 noRadioGroupButton.setText ("DWT.NO_RADIO_GROUP");
|
|
186 leftToRightButton = new Button (styleGroup, DWT.RADIO);
|
|
187 leftToRightButton.setText ("DWT.LEFT_TO_RIGHT");
|
|
188 leftToRightButton.setSelection(true);
|
|
189 rightToLeftButton = new Button (styleGroup, DWT.RADIO);
|
|
190 rightToLeftButton.setText ("DWT.RIGHT_TO_LEFT");
|
|
191
|
|
192 /* Create the menu item style buttons */
|
|
193 cascadeButton = new Button (menuItemStyleGroup, DWT.CHECK);
|
|
194 cascadeButton.setText ("DWT.CASCADE");
|
|
195 checkButton = new Button (menuItemStyleGroup, DWT.CHECK);
|
|
196 checkButton.setText ("DWT.CHECK");
|
|
197 pushButton = new Button (menuItemStyleGroup, DWT.CHECK);
|
|
198 pushButton.setText ("DWT.PUSH");
|
|
199 radioButton = new Button (menuItemStyleGroup, DWT.CHECK);
|
|
200 radioButton.setText ("DWT.RADIO");
|
|
201 separatorButton = new Button (menuItemStyleGroup, DWT.CHECK);
|
|
202 separatorButton.setText ("DWT.SEPARATOR");
|
|
203
|
|
204 /* Create the 'other' buttons */
|
|
205 imagesButton = new Button (otherGroup, DWT.CHECK);
|
|
206 imagesButton.setText (ControlExample.getResourceString("Images"));
|
|
207 acceleratorsButton = new Button (otherGroup, DWT.CHECK);
|
|
208 acceleratorsButton.setText (ControlExample.getResourceString("Accelerators"));
|
|
209 mnemonicsButton = new Button (otherGroup, DWT.CHECK);
|
|
210 mnemonicsButton.setText (ControlExample.getResourceString("Mnemonics"));
|
|
211 subMenuButton = new Button (otherGroup, DWT.CHECK);
|
|
212 subMenuButton.setText (ControlExample.getResourceString("SubMenu"));
|
|
213 subSubMenuButton = new Button (otherGroup, DWT.CHECK);
|
|
214 subSubMenuButton.setText (ControlExample.getResourceString("SubSubMenu"));
|
|
215
|
|
216 /* Create the "create" and "closeAll" buttons (and a 'filler' label to place them) */
|
|
217 new Label(controlGroup, DWT.NONE);
|
|
218 createButton = new Button (controlGroup, DWT.NONE);
|
|
219 createButton.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_END));
|
|
220 createButton.setText (ControlExample.getResourceString("Create_Shell"));
|
|
221 closeAllButton = new Button (controlGroup, DWT.NONE);
|
|
222 closeAllButton.setLayoutData (new GridData (GridData.HORIZONTAL_ALIGN_BEGINNING));
|
|
223 closeAllButton.setText (ControlExample.getResourceString("Close_All_Shells"));
|
|
224
|
|
225 /* Add the listeners */
|
|
226 createButton.addSelectionListener(new class() SelectionAdapter {
|
|
227 public void widgetSelected(SelectionEvent e) {
|
|
228 createButtonSelected(e);
|
|
229 }
|
|
230 });
|
|
231 closeAllButton.addSelectionListener(new class() SelectionAdapter {
|
|
232 public void widgetSelected(SelectionEvent e) {
|
|
233 closeAllShells ();
|
|
234 }
|
|
235 });
|
|
236 subMenuButton.addSelectionListener(new class() SelectionAdapter {
|
|
237 public void widgetSelected(SelectionEvent e) {
|
|
238 subSubMenuButton.setEnabled (subMenuButton.getSelection ());
|
|
239 }
|
|
240 });
|
|
241
|
|
242 /* Set the default state */
|
|
243 barButton.setSelection (true);
|
|
244 dropDownButton.setSelection (true);
|
|
245 popUpButton.setSelection (true);
|
|
246 cascadeButton.setSelection (true);
|
|
247 checkButton.setSelection (true);
|
|
248 pushButton.setSelection (true);
|
|
249 radioButton.setSelection (true);
|
|
250 separatorButton.setSelection (true);
|
|
251 subSubMenuButton.setEnabled (subMenuButton.getSelection ());
|
|
252 }
|
|
253
|
|
254 /* Create various menu items, depending on selections. */
|
|
255 void createMenuItems(Menu menu, bool createSubMenu, bool createSubSubMenu) {
|
|
256 MenuItem item;
|
|
257 if (pushButton.getSelection()) {
|
|
258 item = new MenuItem(menu, DWT.PUSH);
|
|
259 item.setText(getMenuItemText("Push"));
|
|
260 if (acceleratorsButton.getSelection()) item.setAccelerator(DWT.MOD1 + DWT.MOD2 + 'P');
|
|
261 if (imagesButton.getSelection()) item.setImage(instance.images[ControlExample.ciClosedFolder]);
|
|
262 hookListeners(item);
|
|
263 }
|
|
264
|
|
265 if (separatorButton.getSelection()) {
|
|
266 new MenuItem(menu, DWT.SEPARATOR);
|
|
267 }
|
|
268
|
|
269 if (checkButton.getSelection()) {
|
|
270 item = new MenuItem(menu, DWT.CHECK);
|
|
271 item.setText(getMenuItemText("Check"));
|
|
272 if (acceleratorsButton.getSelection()) item.setAccelerator(DWT.MOD1 + DWT.MOD2 + 'C');
|
|
273 if (imagesButton.getSelection()) item.setImage(instance.images[ControlExample.ciOpenFolder]);
|
|
274 hookListeners(item);
|
|
275 }
|
|
276
|
|
277 if (radioButton.getSelection()) {
|
|
278 item = new MenuItem(menu, DWT.RADIO);
|
|
279 item.setText(getMenuItemText("1Radio"));
|
|
280 if (acceleratorsButton.getSelection()) item.setAccelerator(DWT.MOD1 + DWT.MOD2 + '1');
|
|
281 if (imagesButton.getSelection()) item.setImage(instance.images[ControlExample.ciTarget]);
|
|
282 item.setSelection(true);
|
|
283 hookListeners(item);
|
|
284
|
|
285 item = new MenuItem(menu, DWT.RADIO);
|
|
286 item.setText(getMenuItemText("2Radio"));
|
|
287 if (acceleratorsButton.getSelection()) item.setAccelerator(DWT.MOD1 + DWT.MOD2 + '2');
|
|
288 if (imagesButton.getSelection()) item.setImage(instance.images[ControlExample.ciTarget]);
|
|
289 hookListeners(item);
|
|
290 }
|
|
291
|
|
292 if (createSubMenu && cascadeButton.getSelection()) {
|
|
293 /* Create cascade button and drop-down menu for the sub-menu. */
|
|
294 item = new MenuItem(menu, DWT.CASCADE);
|
|
295 item.setText(getMenuItemText("Cascade"));
|
|
296 if (imagesButton.getSelection()) item.setImage(instance.images[ControlExample.ciOpenFolder]);
|
|
297 hookListeners(item);
|
|
298 Menu subMenu = new Menu(menu.getShell(), DWT.DROP_DOWN);
|
|
299 item.setMenu(subMenu);
|
|
300 hookListeners(subMenu);
|
|
301
|
|
302 createMenuItems(subMenu, createSubSubMenu, false);
|
|
303 }
|
|
304 }
|
|
305
|
|
306 char[] getMenuItemText(char[] item) {
|
|
307 bool cascade = ( item == "Cascade");
|
|
308 bool mnemonic = mnemonicsButton.getSelection();
|
|
309 bool accelerator = acceleratorsButton.getSelection();
|
|
310 char acceleratorKey = item[0];
|
|
311 if (mnemonic && accelerator && !cascade) {
|
|
312 return ControlExample.getResourceString(item ~ "WithMnemonic") ~ "\tCtrl+Shift+" ~ acceleratorKey;
|
|
313 }
|
|
314 if (accelerator && !cascade) {
|
|
315 return ControlExample.getResourceString(item) ~ "\tCtrl+Shift+" ~ acceleratorKey;
|
|
316 }
|
|
317 if (mnemonic) {
|
|
318 return ControlExample.getResourceString(item ~ "WithMnemonic");
|
|
319 }
|
|
320 return ControlExample.getResourceString(item);
|
|
321 }
|
|
322
|
|
323 /**
|
|
324 * Gets the text for the tab folder item.
|
|
325 */
|
|
326 char[] getTabText () {
|
|
327 return "Menu";
|
|
328 }
|
|
329 }
|