# HG changeset patch # User Frank Benoit # Date 1207488383 -7200 # Node ID 5a5be20dc12088592a7c61112a1adf1460b04079 # Parent 2862fc9bbe363e0d6b69d6ef075c301d4e31f5e0 add coolbar/Snippet140, thanks Bill. diff -r 2862fc9bbe36 -r 5a5be20dc120 dsss.conf --- a/dsss.conf Sun Apr 06 14:52:50 2008 +0200 +++ b/dsss.conf Sun Apr 06 15:26:23 2008 +0200 @@ -33,6 +33,7 @@ [dwtsnippets/combo/Snippet26.d] [dwtsnippets/composite/Snippet9.d] [dwtsnippets/coolbar/Snippet20.d] +[dwtsnippets/coolbar/Snippet140.d] [dwtsnippets/coolbar/Snippet150.d] [dwtsnippets/ctabfolder/Snippet165.d] [dwtsnippets/directorydialog/Snippet33.d] diff -r 2862fc9bbe36 -r 5a5be20dc120 dwtsnippets/coolbar/Snippet140.d --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtsnippets/coolbar/Snippet140.d Sun Apr 06 15:26:23 2008 +0200 @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2000, 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Port to the D programming language: + * Bill Baxter + *******************************************************************************/ +module dwtsnippets.coolbar.Snippet140; + +/* + * CoolBar example snippet: drop-down a chevron menu containing hidden tool items + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import dwt.DWT; +import dwt.graphics.Point; +import dwt.graphics.Rectangle; +import dwt.widgets.Display; +import dwt.widgets.Shell; +import dwt.widgets.Menu; +import dwt.widgets.MenuItem; +import dwt.widgets.ToolBar; +import dwt.widgets.ToolItem; +import dwt.widgets.CoolBar; +import dwt.widgets.CoolItem; +import dwt.events.SelectionEvent; +import dwt.events.SelectionAdapter; +import dwt.layout.GridLayout; +import dwt.layout.GridData; + +import tango.util.Convert; + +static Display display; +static Shell shell; +static CoolBar coolBar; +static Menu chevronMenu = null; + + +void main () { + display = new Display (); + shell = new Shell (display); + shell.setLayout(new GridLayout()); + coolBar = new CoolBar(shell, DWT.FLAT | DWT.BORDER); + coolBar.setLayoutData(new GridData(GridData.FILL_BOTH)); + ToolBar toolBar = new ToolBar(coolBar, DWT.FLAT | DWT.WRAP); + int minWidth = 0; + for (int j = 0; j < 5; j++) { + int width = 0; + ToolItem item = new ToolItem(toolBar, DWT.PUSH); + item.setText("B" ~ to!(char[])(j)); + width = item.getWidth(); + /* find the width of the widest tool */ + if (width > minWidth) minWidth = width; + } + CoolItem coolItem = new CoolItem(coolBar, DWT.DROP_DOWN); + coolItem.setControl(toolBar); + Point size = toolBar.computeSize(DWT.DEFAULT, DWT.DEFAULT); + Point coolSize = coolItem.computeSize (size.x, size.y); + coolItem.setMinimumSize(minWidth, coolSize.y); + coolItem.setPreferredSize(coolSize); + coolItem.setSize(coolSize); + coolItem.addSelectionListener(new class() SelectionAdapter { + public void widgetSelected(SelectionEvent event) { + if (event.detail == DWT.ARROW) { + CoolItem item = cast(CoolItem) event.widget; + Rectangle itemBounds = item.getBounds (); + Point pt = coolBar.toDisplay(new Point(itemBounds.x, itemBounds.y)); + itemBounds.x = pt.x; + itemBounds.y = pt.y; + ToolBar bar = cast(ToolBar) item.getControl (); + ToolItem[] tools = bar.getItems (); + + int i = 0; + while (i < tools.length) { + Rectangle toolBounds = tools[i].getBounds (); + pt = bar.toDisplay(new Point(toolBounds.x, toolBounds.y)); + toolBounds.x = pt.x; + toolBounds.y = pt.y; + + /* Figure out the visible portion of the tool by looking at the + * intersection of the tool bounds with the cool item bounds. */ + Rectangle intersection = itemBounds.intersection (toolBounds); + + /* If the tool is not completely within the cool item bounds, then it + * is partially hidden, and all remaining tools are completely hidden. */ + if (intersection != toolBounds) break; + i++; + } + + /* Create a menu with items for each of the completely hidden buttons. */ + if (chevronMenu !is null) chevronMenu.dispose(); + chevronMenu = new Menu (coolBar); + for (int j = i; j < tools.length; j++) { + MenuItem menuItem = new MenuItem (chevronMenu, DWT.PUSH); + menuItem.setText (tools[j].getText()); + } + + /* Drop down the menu below the chevron, with the left edges aligned. */ + pt = coolBar.toDisplay(new Point(event.x, event.y)); + chevronMenu.setLocation (pt.x, pt.y); + chevronMenu.setVisible (true); + } + } + }); + + shell.pack(); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); +}