Index: src/org/eclipse/swt/snippets/Snippet140.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet140.java,v retrieving revision 1.4 diff -u -r1.4 Snippet140.java --- src/org/eclipse/swt/snippets/Snippet140.java 16 Sep 2005 19:24:39 -0000 1.4 +++ src/org/eclipse/swt/snippets/Snippet140.java 3 Jul 2007 07:30:23 -0000 @@ -51,47 +51,44 @@ coolItem.setMinimumSize(minWidth, coolSize.y); coolItem.setPreferredSize(coolSize); coolItem.setSize(coolSize); - coolItem.addSelectionListener(new SelectionAdapter() { - public void widgetSelected(SelectionEvent event) { - if (event.detail == SWT.ARROW) { - CoolItem item = (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 = (ToolBar) item.getControl (); - ToolItem[] tools = bar.getItems (); + coolItem.addMenuDetectListener(new MenuDetectListener() { + public void menuDetected(MenuDetectEvent event) { + CoolItem item = (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 = (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; - 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.equals (toolBounds)) break; - i++; - } + /* 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); - /* Create a menu with items for each of the completely hidden buttons. */ - if (chevronMenu != null) chevronMenu.dispose(); - chevronMenu = new Menu (coolBar); - for (int j = i; j < tools.length; j++) { - MenuItem menuItem = new MenuItem (chevronMenu, SWT.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); + /* 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.equals (toolBounds)) break; + i++; + } + + /* Create a menu with items for each of the completely hidden buttons. */ + if (chevronMenu != null) chevronMenu.dispose(); + chevronMenu = new Menu (coolBar); + for (int j = i; j < tools.length; j++) { + MenuItem menuItem = new MenuItem (chevronMenu, SWT.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)); + item.setMenu(chevronMenu); } }); Index: src/org/eclipse/swt/snippets/Snippet67.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet67.java,v retrieving revision 1.3 diff -u -r1.3 Snippet67.java --- src/org/eclipse/swt/snippets/Snippet67.java 16 Sep 2005 19:24:37 -0000 1.3 +++ src/org/eclipse/swt/snippets/Snippet67.java 3 Jul 2007 07:30:23 -0000 @@ -32,17 +32,7 @@ item.setText ("Item " + i); } final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN); - item.addListener (SWT.Selection, new Listener () { - public void handleEvent (Event event) { - if (event.detail == SWT.ARROW) { - Rectangle rect = item.getBounds (); - Point pt = new Point (rect.x, rect.y + rect.height); - pt = toolBar.toDisplay (pt); - menu.setLocation (pt.x, pt.y); - menu.setVisible (true); - } - } - }); + item.setMenu(menu); toolBar.pack (); shell.pack (); shell.open (); Index: src/org/eclipse/swt/snippets/Snippet67x.java =================================================================== RCS file: src/org/eclipse/swt/snippets/Snippet67x.java diff -N src/org/eclipse/swt/snippets/Snippet67x.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/swt/snippets/Snippet67x.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,137 @@ +/******************************************************************************* + * 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 + *******************************************************************************/ +package org.eclipse.swt.snippets; + +/* + * ToolBar example snippet: place a drop down menu in a tool bar + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import java.util.Date; +import java.util.Random; + +import org.eclipse.swt.*; +import org.eclipse.swt.events.KeyAdapter; +import org.eclipse.swt.events.KeyEvent; +import org.eclipse.swt.events.MenuDetectEvent; +import org.eclipse.swt.events.MenuDetectListener; +import org.eclipse.swt.events.MouseAdapter; +import org.eclipse.swt.events.MouseEvent; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.widgets.*; + +public class Snippet67x { + +public static void main (String [] args) { + final Display display = new Display (); + final Shell shell = new Shell (display); + final ToolBar toolBar = new ToolBar (shell, SWT.FLAT); + toolBar.addMouseListener(new MouseAdapter() { + + @Override + public void mouseDown(MouseEvent e) { + long time = e.time & 0xFFFFFFFFL; + System.out.println("mousedown " + new Date(time)); + } + + @Override + public void mouseDoubleClick(MouseEvent e) { + long time = e.time & 0xFFFFFFFFL; + System.out.println("mousedblclick"); + } + + @Override + public void mouseUp(MouseEvent e) { + long time = e.time & 0xFFFFFFFFL; + System.out.println("mouseup " + new Date(time)); + } + + }); + toolBar.addKeyListener(new KeyAdapter() { + + @Override + public void keyPressed(KeyEvent e) { + System.out.println("keypressed"); + } + + @Override + public void keyReleased(KeyEvent e) { + System.out.println("keyreleased"); + } + + }); + final Menu menu = new Menu (shell, SWT.POP_UP); + for (int i=0; i<8; i++) { + MenuItem item = new MenuItem (menu, SWT.PUSH); + item.setText ("Item " + i); + } + final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN); + item.addMenuDetectListener(new MenuDetectListener() { + + public void menuDetected(MenuDetectEvent e) { + System.out.println("menudetectdropdown"); + } + + }); + item.setText("My Item"); + item.setMenu(menu); + final ToolItem item2 = new ToolItem (toolBar, SWT.PUSH, 0); + item2.setText("theone"); + item2.addSelectionListener(new SelectionListener() { + + public void widgetSelected(SelectionEvent e) { + System.out.println("sel"); + } + + public void widgetDefaultSelected(SelectionEvent e) { + System.out.println("default sel"); + } + + }); + item2.addMenuDetectListener(new MenuDetectListener(){ + + public void menuDetected(MenuDetectEvent event) { + System.out.println("menudetect"); + if (false) { + event.doit = false; + } else { + if (item2.getMenu() != null) { + item2.getMenu().dispose(); + } + final Menu menu = new Menu (shell, SWT.POP_UP); + for (int i=0; i<8; i++) { + MenuItem item = new MenuItem (menu, SWT.PUSH); + item.setText ("Item " + new Random().nextInt(10)); + } + item2.setMenu(menu); + } + } + + }); + toolBar.pack (); + + Button btn = new Button(shell, SWT.PUSH); + btn.setLocation(120, 00); + btn.setText("Hello"); + btn.pack(); + + shell.pack (); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + menu.dispose (); + display.dispose (); +} +}