78
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2004 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 * Bill Baxter
|
|
12 *******************************************************************************/
|
|
13 module snippets.coolbar.Snippet140;
|
|
14
|
|
15 /*
|
|
16 * CoolBar example snippet: drop-down a chevron menu containing hidden tool items
|
|
17 *
|
|
18 * For a list of all SWT example snippets see
|
|
19 * http://www.eclipse.org/swt/snippets/
|
|
20 */
|
|
21 import dwt.DWT;
|
|
22 import dwt.graphics.Point;
|
|
23 import dwt.graphics.Rectangle;
|
|
24 import dwt.widgets.Display;
|
|
25 import dwt.widgets.Shell;
|
|
26 import dwt.widgets.Menu;
|
|
27 import dwt.widgets.MenuItem;
|
|
28 import dwt.widgets.ToolBar;
|
|
29 import dwt.widgets.ToolItem;
|
|
30 import dwt.widgets.CoolBar;
|
|
31 import dwt.widgets.CoolItem;
|
|
32 import dwt.events.SelectionEvent;
|
|
33 import dwt.events.SelectionAdapter;
|
|
34 import dwt.layout.GridLayout;
|
|
35 import dwt.layout.GridData;
|
|
36
|
|
37 import tango.util.Convert;
|
|
38
|
|
39 static Display display;
|
|
40 static Shell shell;
|
|
41 static CoolBar coolBar;
|
|
42 static Menu chevronMenu = null;
|
|
43
|
|
44
|
|
45 void main () {
|
|
46 display = new Display ();
|
|
47 shell = new Shell (display);
|
|
48 shell.setLayout(new GridLayout());
|
|
49 coolBar = new CoolBar(shell, DWT.FLAT | DWT.BORDER);
|
|
50 coolBar.setLayoutData(new GridData(GridData.FILL_BOTH));
|
|
51 ToolBar toolBar = new ToolBar(coolBar, DWT.FLAT | DWT.WRAP);
|
|
52 int minWidth = 0;
|
|
53 for (int j = 0; j < 5; j++) {
|
|
54 int width = 0;
|
|
55 ToolItem item = new ToolItem(toolBar, DWT.PUSH);
|
|
56 item.setText("B" ~ to!(char[])(j));
|
|
57 width = item.getWidth();
|
|
58 /* find the width of the widest tool */
|
|
59 if (width > minWidth) minWidth = width;
|
|
60 }
|
|
61 CoolItem coolItem = new CoolItem(coolBar, DWT.DROP_DOWN);
|
|
62 coolItem.setControl(toolBar);
|
|
63 Point size = toolBar.computeSize(DWT.DEFAULT, DWT.DEFAULT);
|
|
64 Point coolSize = coolItem.computeSize (size.x, size.y);
|
|
65 coolItem.setMinimumSize(minWidth, coolSize.y);
|
|
66 coolItem.setPreferredSize(coolSize);
|
|
67 coolItem.setSize(coolSize);
|
|
68 coolItem.addSelectionListener(new class() SelectionAdapter {
|
|
69 public void widgetSelected(SelectionEvent event) {
|
|
70 if (event.detail == DWT.ARROW) {
|
|
71 CoolItem item = cast(CoolItem) event.widget;
|
|
72 Rectangle itemBounds = item.getBounds ();
|
|
73 Point pt = coolBar.toDisplay(new Point(itemBounds.x, itemBounds.y));
|
|
74 itemBounds.x = pt.x;
|
|
75 itemBounds.y = pt.y;
|
|
76 ToolBar bar = cast(ToolBar) item.getControl ();
|
|
77 ToolItem[] tools = bar.getItems ();
|
|
78
|
|
79 int i = 0;
|
|
80 while (i < tools.length) {
|
|
81 Rectangle toolBounds = tools[i].getBounds ();
|
|
82 pt = bar.toDisplay(new Point(toolBounds.x, toolBounds.y));
|
|
83 toolBounds.x = pt.x;
|
|
84 toolBounds.y = pt.y;
|
|
85
|
|
86 /* Figure out the visible portion of the tool by looking at the
|
|
87 * intersection of the tool bounds with the cool item bounds.
|
|
88 */
|
|
89 Rectangle intersection = itemBounds.intersection (toolBounds);
|
|
90
|
|
91 /* If the tool is not completely within the cool item bounds, then it
|
|
92 * is partially hidden, and all remaining tools are completely hidden.
|
|
93 */
|
|
94 if (intersection != toolBounds) break;
|
|
95 i++;
|
|
96 }
|
|
97
|
|
98 /* Create a menu with items for each of the completely hidden buttons. */
|
|
99 if (chevronMenu !is null) chevronMenu.dispose();
|
|
100 chevronMenu = new Menu (coolBar);
|
|
101 for (int j = i; j < tools.length; j++) {
|
|
102 MenuItem menuItem = new MenuItem (chevronMenu, DWT.PUSH);
|
|
103 menuItem.setText (tools[j].getText());
|
|
104 }
|
|
105
|
|
106 /* Drop down the menu below the chevron, with the left edges aligned. */
|
|
107 pt = coolBar.toDisplay(new Point(event.x, event.y));
|
|
108 chevronMenu.setLocation (pt.x, pt.y);
|
|
109 chevronMenu.setVisible (true);
|
|
110 }
|
|
111 }
|
|
112 });
|
|
113
|
|
114 shell.pack();
|
|
115 shell.open ();
|
|
116 while (!shell.isDisposed ()) {
|
|
117 if (!display.readAndDispatch ()) display.sleep ();
|
|
118 }
|
|
119 display.dispose ();
|
|
120 }
|