comparison org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet140.d @ 28:69b1fa94a4a8

Added SWT snippets
author Frank Benoit <benoit@tionex.de>
date Sun, 22 Mar 2009 15:17:04 +0100
parents
children 4e5843b771cc
comparison
equal deleted inserted replaced
27:1bf55a6eb092 28:69b1fa94a4a8
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 org.eclipse.swt.snippets.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 org.eclipse.swt.SWT;
22 import org.eclipse.swt.graphics.Point;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.swt.widgets.Display;
25 import org.eclipse.swt.widgets.Shell;
26 import org.eclipse.swt.widgets.Menu;
27 import org.eclipse.swt.widgets.MenuItem;
28 import org.eclipse.swt.widgets.ToolBar;
29 import org.eclipse.swt.widgets.ToolItem;
30 import org.eclipse.swt.widgets.CoolBar;
31 import org.eclipse.swt.widgets.CoolItem;
32 import org.eclipse.swt.events.SelectionEvent;
33 import org.eclipse.swt.events.SelectionAdapter;
34 import org.eclipse.swt.layout.GridLayout;
35 import org.eclipse.swt.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, SWT.FLAT | DWT.BORDER);
50 coolBar.setLayoutData(new GridData(GridData.FILL_BOTH));
51 ToolBar toolBar = new ToolBar(coolBar, SWT.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, SWT.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, SWT.DROP_DOWN);
62 coolItem.setControl(toolBar);
63 Point size = toolBar.computeSize(SWT.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 == SWT.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, SWT.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 }