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