annotate snippets/composite/Snippet46.d @ 119:9a1be6ff19a2

More snippets, thanks to Tom D.
author Frank Benoit <benoit@tionex.de>
date Sun, 20 Jul 2008 15:26:06 +0200
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
119
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
1 /*******************************************************************************
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
3 * All rights reserved. This program and the accompanying materials
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
4 * are made available under the terms of the Eclipse Public License v1.0
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
5 * which accompanies this distribution, and is available at
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
6 * http://www.eclipse.org/legal/epl-v10.html
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
7 *
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
8 * Contributors:
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
9 * IBM Corporation - initial API and implementation
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
10 * D Port:
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
11 * Thomas Demmer <t_demmer AT web DOT de>
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
12 *******************************************************************************/
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
13 module composite.Snippet46;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
14
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
15 /*
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
16 * Composite example snippet: intercept mouse events (drag a button with the mouse)
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
17 *
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
18 * For a list of all SWT example snippets see
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
19 * http://www.eclipse.org/swt/snippets/
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
20 */
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
21 import dwt.DWT;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
22 import dwt.graphics.Point;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
23 import dwt.graphics.Rectangle;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
24 import dwt.widgets.Button;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
25 import dwt.widgets.Composite;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
26 import dwt.widgets.Display;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
27 import dwt.widgets.Event;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
28 import dwt.widgets.Listener;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
29 import dwt.widgets.Shell;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
30 import dwt.layout.FillLayout;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
31
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
32 import dwt.dwthelper.utils;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
33
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
34 void main (String [] args) {
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
35 Display display = new Display ();
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
36 Shell shell = new Shell (display);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
37 Composite composite = new Composite (shell, DWT.NONE);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
38 composite.setEnabled (false);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
39 composite.setLayout (new FillLayout ());
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
40 Button button = new Button (composite, DWT.PUSH);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
41 button.setText ("Button");
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
42 composite.pack ();
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
43 composite.setLocation (10, 10);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
44 Point [] offset = new Point [1];
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
45 Listener listener = new class() Listener{
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
46 public void handleEvent (Event event) {
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
47 switch (event.type) {
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
48 case DWT.MouseDown:
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
49 Rectangle rect = composite.getBounds ();
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
50 if (rect.contains (event.x, event.y)) {
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
51 Point pt1 = composite.toDisplay (0, 0);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
52 Point pt2 = shell.toDisplay (event.x, event.y);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
53 offset [0] = new Point (pt2.x - pt1.x, pt2.y - pt1.y);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
54 }
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
55 break;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
56 case DWT.MouseMove:
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
57 if (offset [0] !is null) {
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
58 Point pt = offset [0];
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
59 composite.setLocation (event.x - pt.x, event.y - pt.y);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
60 }
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
61 break;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
62 case DWT.MouseUp:
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
63 offset [0] = null;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
64 break;
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
65 }
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
66 }
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
67 };
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
68 shell.addListener (DWT.MouseDown, listener);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
69 shell.addListener (DWT.MouseUp, listener);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
70 shell.addListener (DWT.MouseMove, listener);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
71 shell.setSize (300, 300);
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
72 shell.open ();
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
73 while (!shell.isDisposed ()) {
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
74 if (!display.readAndDispatch ()) display.sleep ();
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
75 }
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
76 display.dispose ();
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
77 }
9a1be6ff19a2 More snippets, thanks to Tom D.
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
78