1 #!/usr/bin/env dub 2 /+ 3 dub.sdl: 4 name "snippet140" 5 dependency "dwt" path="../../../../../../" 6 libs \ 7 "atk-1.0" \ 8 "cairo" \ 9 "dl" \ 10 "fontconfig" \ 11 "gdk-x11-2.0" \ 12 "gdk_pixbuf-2.0" \ 13 "glib-2.0" \ 14 "gmodule-2.0" \ 15 "gnomeui-2" \ 16 "gnomevfs-2" \ 17 "gobject-2.0" \ 18 "gthread-2.0" \ 19 "gtk-x11-2.0" \ 20 "pango-1.0" \ 21 "pangocairo-1.0" \ 22 "X11" \ 23 "Xcomposite" \ 24 "Xcursor" \ 25 "Xdamage" \ 26 "Xext" \ 27 "Xfixes" \ 28 "Xi" \ 29 "Xinerama" \ 30 "Xrandr" \ 31 "Xrender" \ 32 "Xtst" \ 33 platform="linux" 34 +/ 35 36 /******************************************************************************* 37 * Copyright (c) 2000, 2004 IBM Corporation and others. 38 * All rights reserved. This program and the accompanying materials 39 * are made available under the terms of the Eclipse Public License v1.0 40 * which accompanies this distribution, and is available at 41 * http://www.eclipse.org/legal/epl-v10.html 42 * 43 * Contributors: 44 * IBM Corporation - initial API and implementation 45 * Port to the D programming language: 46 * Bill Baxter 47 *******************************************************************************/ 48 module org.eclipse.swt.snippets.Snippet140; 49 50 /* 51 * CoolBar example snippet: drop-down a chevron menu containing hidden tool items 52 * 53 * For a list of all SWT example snippets see 54 * http://www.eclipse.org/swt/snippets/ 55 */ 56 import org.eclipse.swt.SWT; 57 import org.eclipse.swt.graphics.Point; 58 import org.eclipse.swt.graphics.Rectangle; 59 import org.eclipse.swt.widgets.Display; 60 import org.eclipse.swt.widgets.Shell; 61 import org.eclipse.swt.widgets.Menu; 62 import org.eclipse.swt.widgets.MenuItem; 63 import org.eclipse.swt.widgets.ToolBar; 64 import org.eclipse.swt.widgets.ToolItem; 65 import org.eclipse.swt.widgets.CoolBar; 66 import org.eclipse.swt.widgets.CoolItem; 67 import org.eclipse.swt.events.SelectionEvent; 68 import org.eclipse.swt.events.SelectionAdapter; 69 import org.eclipse.swt.layout.GridLayout; 70 import org.eclipse.swt.layout.GridData; 71 import java.lang.all; 72 73 version(Tango){ 74 import tango.util.Convert; 75 } else { // Phobos 76 import std.conv; 77 } 78 79 80 void main () { 81 Menu chevronMenu = null; 82 83 auto display = new Display (); 84 auto shell = new Shell (display); 85 shell.setLayout(new GridLayout()); 86 auto coolBar = new CoolBar(shell, SWT.FLAT | SWT.BORDER); 87 coolBar.setLayoutData(new GridData(GridData.FILL_BOTH)); 88 ToolBar toolBar = new ToolBar(coolBar, SWT.FLAT | SWT.WRAP); 89 int minWidth = 0; 90 for (int j = 0; j < 5; j++) { 91 int width = 0; 92 ToolItem item = new ToolItem(toolBar, SWT.PUSH); 93 item.setText("B" ~ to!(String)(j)); 94 width = item.getWidth(); 95 /* find the width of the widest tool */ 96 if (width > minWidth) minWidth = width; 97 } 98 CoolItem coolItem = new CoolItem(coolBar, SWT.DROP_DOWN); 99 coolItem.setControl(toolBar); 100 Point size = toolBar.computeSize(SWT.DEFAULT, SWT.DEFAULT); 101 Point coolSize = coolItem.computeSize (size.x, size.y); 102 coolItem.setMinimumSize(minWidth, coolSize.y); 103 coolItem.setPreferredSize(coolSize); 104 coolItem.setSize(coolSize); 105 coolItem.addSelectionListener(new class SelectionAdapter { 106 override 107 public void widgetSelected(SelectionEvent event) { 108 if (event.detail == SWT.ARROW) { 109 CoolItem item = cast(CoolItem) event.widget; 110 Rectangle itemBounds = item.getBounds (); 111 Point pt = coolBar.toDisplay(new Point(itemBounds.x, itemBounds.y)); 112 itemBounds.x = pt.x; 113 itemBounds.y = pt.y; 114 ToolBar bar = cast(ToolBar) item.getControl (); 115 ToolItem[] tools = bar.getItems (); 116 117 int i = 0; 118 while (i < tools.length) { 119 Rectangle toolBounds = tools[i].getBounds (); 120 pt = bar.toDisplay(new Point(toolBounds.x, toolBounds.y)); 121 toolBounds.x = pt.x; 122 toolBounds.y = pt.y; 123 124 /* Figure out the visible portion of the tool by looking at the 125 * intersection of the tool bounds with the cool item bounds. 126 */ 127 Rectangle intersection = itemBounds.intersection (toolBounds); 128 129 /* If the tool is not completely within the cool item bounds, then it 130 * is partially hidden, and all remaining tools are completely hidden. 131 */ 132 if (intersection != toolBounds) break; 133 i++; 134 } 135 136 /* Create a menu with items for each of the completely hidden buttons. */ 137 if (chevronMenu !is null) chevronMenu.dispose(); 138 chevronMenu = new Menu (coolBar); 139 for (int j = i; j < tools.length; j++) { 140 MenuItem menuItem = new MenuItem (chevronMenu, SWT.PUSH); 141 menuItem.setText (tools[j].getText()); 142 } 143 144 /* Drop down the menu below the chevron, with the left edges aligned. */ 145 pt = coolBar.toDisplay(new Point(event.x, event.y)); 146 chevronMenu.setLocation (pt.x, pt.y); 147 chevronMenu.setVisible (true); 148 } 149 } 150 }); 151 152 shell.pack(); 153 shell.open (); 154 while (!shell.isDisposed ()) { 155 if (!display.readAndDispatch ()) display.sleep (); 156 } 157 display.dispose (); 158 }