View | Details | Raw Unified | Return to bug 193318 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/swt/snippets/Snippet140.java (-38 / +35 lines)
Lines 51-97 Link Here
51
	coolItem.setMinimumSize(minWidth, coolSize.y);
51
	coolItem.setMinimumSize(minWidth, coolSize.y);
52
	coolItem.setPreferredSize(coolSize);
52
	coolItem.setPreferredSize(coolSize);
53
	coolItem.setSize(coolSize);
53
	coolItem.setSize(coolSize);
54
	coolItem.addSelectionListener(new SelectionAdapter() {
54
	coolItem.addMenuDetectListener(new MenuDetectListener() {
55
		public void widgetSelected(SelectionEvent event) {
55
		public void menuDetected(MenuDetectEvent event) {
56
			if (event.detail == SWT.ARROW) {
56
			CoolItem item = (CoolItem) event.widget;
57
				CoolItem item = (CoolItem) event.widget;
57
			Rectangle itemBounds = item.getBounds ();
58
				Rectangle itemBounds = item.getBounds ();
58
			Point pt = coolBar.toDisplay(new Point(itemBounds.x, itemBounds.y));
59
				Point pt = coolBar.toDisplay(new Point(itemBounds.x, itemBounds.y));
59
			itemBounds.x = pt.x;
60
				itemBounds.x = pt.x;
60
			itemBounds.y = pt.y;
61
				itemBounds.y = pt.y;
61
			ToolBar bar = (ToolBar) item.getControl ();
62
				ToolBar bar = (ToolBar) item.getControl ();
62
			ToolItem[] tools = bar.getItems ();
63
				ToolItem[] tools = bar.getItems ();
63
			
64
			int i = 0;
65
			while (i < tools.length) {
66
				Rectangle toolBounds = tools[i].getBounds ();
67
				pt = bar.toDisplay(new Point(toolBounds.x, toolBounds.y));
68
				toolBounds.x = pt.x;
69
				toolBounds.y = pt.y;
64
				
70
				
65
				int i = 0;
71
				/* Figure out the visible portion of the tool by looking at the
66
				while (i < tools.length) {
72
				 * intersection of the tool bounds with the cool item bounds. */
67
					Rectangle toolBounds = tools[i].getBounds ();
73
				Rectangle intersection = itemBounds.intersection (toolBounds);
68
					pt = bar.toDisplay(new Point(toolBounds.x, toolBounds.y));
69
					toolBounds.x = pt.x;
70
					toolBounds.y = pt.y;
71
					
72
					/* Figure out the visible portion of the tool by looking at the
73
					 * intersection of the tool bounds with the cool item bounds. */
74
			  		Rectangle intersection = itemBounds.intersection (toolBounds);
75
			  		
76
					/* If the tool is not completely within the cool item bounds, then it
77
					 * is partially hidden, and all remaining tools are completely hidden. */
78
			  		if (!intersection.equals (toolBounds)) break;
79
			  		i++;
80
				}
81
				
74
				
82
				/* Create a menu with items for each of the completely hidden buttons. */
75
				/* If the tool is not completely within the cool item bounds, then it
83
				if (chevronMenu != null) chevronMenu.dispose();
76
				 * is partially hidden, and all remaining tools are completely hidden. */
84
				chevronMenu = new Menu (coolBar);
77
				if (!intersection.equals (toolBounds)) break;
85
				for (int j = i; j < tools.length; j++) {
78
				i++;
86
					MenuItem menuItem = new MenuItem (chevronMenu, SWT.PUSH);
79
			}
87
					menuItem.setText (tools[j].getText());
80
			
88
				}
81
			/* Create a menu with items for each of the completely hidden buttons. */
89
				
82
			if (chevronMenu != null) chevronMenu.dispose();
90
				/* Drop down the menu below the chevron, with the left edges aligned. */
83
			chevronMenu = new Menu (coolBar);
91
				pt = coolBar.toDisplay(new Point(event.x, event.y));
84
			for (int j = i; j < tools.length; j++) {
92
				chevronMenu.setLocation (pt.x, pt.y);
85
				MenuItem menuItem = new MenuItem (chevronMenu, SWT.PUSH);
93
				chevronMenu.setVisible (true);
86
				menuItem.setText (tools[j].getText());
94
			}
87
			}
88
			
89
			/* Drop down the menu below the chevron, with the left edges aligned. */
90
			pt = coolBar.toDisplay(new Point(event.x, event.y));
91
			item.setMenu(chevronMenu);
95
		}
92
		}
96
	});
93
	});
97
94
(-)src/org/eclipse/swt/snippets/Snippet67.java (-11 / +1 lines)
Lines 32-48 Link Here
32
		item.setText ("Item " + i);
32
		item.setText ("Item " + i);
33
	}
33
	}
34
	final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
34
	final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
35
	item.addListener (SWT.Selection, new Listener () {
35
	item.setMenu(menu);
36
		public void handleEvent (Event event) {
37
			if (event.detail == SWT.ARROW) {
38
				Rectangle rect = item.getBounds ();
39
				Point pt = new Point (rect.x, rect.y + rect.height);
40
				pt = toolBar.toDisplay (pt);
41
				menu.setLocation (pt.x, pt.y);
42
				menu.setVisible (true);
43
			}
44
		}
45
	});
46
	toolBar.pack ();
36
	toolBar.pack ();
47
	shell.pack ();
37
	shell.pack ();
48
	shell.open ();
38
	shell.open ();
(-)src/org/eclipse/swt/snippets/Snippet67x.java (+137 lines)
Added Link Here
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
 *******************************************************************************/
11
package org.eclipse.swt.snippets;
12
13
/*
14
 * ToolBar example snippet: place a drop down menu in a tool bar
15
 *
16
 * For a list of all SWT example snippets see
17
 * http://www.eclipse.org/swt/snippets/
18
 */
19
import java.util.Date;
20
import java.util.Random;
21
22
import org.eclipse.swt.*;
23
import org.eclipse.swt.events.KeyAdapter;
24
import org.eclipse.swt.events.KeyEvent;
25
import org.eclipse.swt.events.MenuDetectEvent;
26
import org.eclipse.swt.events.MenuDetectListener;
27
import org.eclipse.swt.events.MouseAdapter;
28
import org.eclipse.swt.events.MouseEvent;
29
import org.eclipse.swt.events.SelectionEvent;
30
import org.eclipse.swt.events.SelectionListener;
31
import org.eclipse.swt.graphics.*;
32
import org.eclipse.swt.widgets.*;
33
34
public class Snippet67x {
35
36
public static void main (String [] args) {
37
	final Display display = new Display ();
38
	final Shell shell = new Shell (display);
39
	final ToolBar toolBar = new ToolBar (shell, SWT.FLAT);
40
	toolBar.addMouseListener(new MouseAdapter() {
41
	
42
		@Override
43
		public void mouseDown(MouseEvent e) {
44
			long time = e.time & 0xFFFFFFFFL;
45
			System.out.println("mousedown " + new Date(time));
46
		}
47
48
		@Override
49
		public void mouseDoubleClick(MouseEvent e) {
50
			long time = e.time & 0xFFFFFFFFL;
51
			System.out.println("mousedblclick");
52
		}
53
54
		@Override
55
		public void mouseUp(MouseEvent e) {
56
			long time = e.time & 0xFFFFFFFFL;
57
			System.out.println("mouseup " + new Date(time));
58
		}
59
	
60
	});
61
	toolBar.addKeyListener(new KeyAdapter() {
62
	
63
		@Override
64
		public void keyPressed(KeyEvent e) {
65
			System.out.println("keypressed");
66
		}
67
68
		@Override
69
		public void keyReleased(KeyEvent e) {
70
			System.out.println("keyreleased");
71
		}
72
73
	});
74
	final Menu menu = new Menu (shell, SWT.POP_UP);
75
	for (int i=0; i<8; i++) {
76
		MenuItem item = new MenuItem (menu, SWT.PUSH);
77
		item.setText ("Item " + i);
78
	}
79
	final ToolItem item = new ToolItem (toolBar, SWT.DROP_DOWN);
80
	item.addMenuDetectListener(new MenuDetectListener() {
81
	
82
		public void menuDetected(MenuDetectEvent e) {
83
			System.out.println("menudetectdropdown");
84
		}
85
	
86
	});
87
	item.setText("My Item");
88
	item.setMenu(menu);
89
	final ToolItem item2 = new ToolItem (toolBar, SWT.PUSH, 0);
90
	item2.setText("theone");
91
	item2.addSelectionListener(new SelectionListener() {
92
	
93
		public void widgetSelected(SelectionEvent e) {
94
			System.out.println("sel");
95
		}
96
	
97
		public void widgetDefaultSelected(SelectionEvent e) {
98
			System.out.println("default sel");
99
		}
100
	
101
	});
102
	item2.addMenuDetectListener(new MenuDetectListener(){
103
104
		public void menuDetected(MenuDetectEvent event) {
105
			System.out.println("menudetect");
106
			if (false) {
107
				event.doit = false;
108
			} else {
109
				if (item2.getMenu() != null) {
110
					item2.getMenu().dispose();
111
				}
112
				final Menu menu = new Menu (shell, SWT.POP_UP);
113
				for (int i=0; i<8; i++) {
114
					MenuItem item = new MenuItem (menu, SWT.PUSH);
115
					item.setText ("Item " + new Random().nextInt(10));
116
				}
117
				item2.setMenu(menu);
118
			}
119
		}
120
121
	});
122
	toolBar.pack ();
123
	
124
	Button btn = new Button(shell, SWT.PUSH);
125
	btn.setLocation(120, 00);
126
	btn.setText("Hello");
127
	btn.pack();
128
129
	shell.pack ();
130
	shell.open ();
131
	while (!shell.isDisposed ()) {
132
		if (!display.readAndDispatch ()) display.sleep ();
133
	}
134
	menu.dispose ();
135
	display.dispose ();
136
}
137
} 

Return to bug 193318