annotate snippets/canvas/Snippet48.d @ 120:1f0a7a472680

fix rm snippets from module statements.
author Frank Benoit <benoit@tionex.de>
date Sun, 20 Jul 2008 19:17:23 +0200
parents 29bf9ba4756b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
90
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
1 /*******************************************************************************
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
3 * All rights reserved. This program and the accompanying materials
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
4 * are made available under the terms of the Eclipse Public License v1.0
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
5 * which accompanies this distribution, and is available at
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
6 * http://www.eclipse.org/legal/epl-v10.html
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
7 *
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
8 * Contributors:
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
9 * IBM Corporation - initial API and implementation
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
10 * D Port:
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
11 * Bill Baxter <wbaxter> at gmail com
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
12 *******************************************************************************/
120
1f0a7a472680 fix rm snippets from module statements.
Frank Benoit <benoit@tionex.de>
parents: 90
diff changeset
13 module canvas.Snippet48;
90
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
14
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
15 /*
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
16 * Canvas example snippet: scroll an image (flicker free, no double buffering)
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
17 *
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
18 * For a list of all SWT example snippets see
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
19 * http://www.eclipse.org/swt/snippets/
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
20 */
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
21 import dwt.DWT;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
22 import dwt.graphics.GC;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
23 import dwt.graphics.Point;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
24 import dwt.graphics.Rectangle;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
25 import dwt.graphics.Color;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
26 import dwt.graphics.Image;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
27 import dwt.layout.FillLayout;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
28 import dwt.widgets.Display;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
29 import dwt.widgets.Shell;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
30 import dwt.widgets.Event;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
31 import dwt.widgets.Listener;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
32 import dwt.widgets.FileDialog;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
33 import dwt.widgets.Canvas;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
34 import dwt.widgets.ScrollBar;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
35 import dwt.layout.FillLayout;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
36
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
37 import dwt.dwthelper.utils;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
38
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
39 void main () {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
40 Display display = new Display ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
41 Shell shell = new Shell (display);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
42 shell.setLayout(new FillLayout());
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
43 Image originalImage = null;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
44 FileDialog dialog = new FileDialog (shell, DWT.OPEN);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
45 dialog.setText ("Open an image file or cancel");
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
46 String string = dialog.open ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
47 if (string !is null) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
48 originalImage = new Image (display, string);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
49 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
50 if (originalImage is null) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
51 int width = 150, height = 200;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
52 originalImage = new Image (display, width, height);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
53 GC gc = new GC (originalImage);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
54 gc.fillRectangle (0, 0, width, height);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
55 gc.drawLine (0, 0, width, height);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
56 gc.drawLine (0, height, width, 0);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
57 gc.drawText ("Default Image", 10, 10);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
58 gc.dispose ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
59 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
60 final Image image = originalImage;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
61 final Point origin = new Point (0, 0);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
62 final Canvas canvas = new Canvas (shell, DWT.NO_BACKGROUND |
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
63 DWT.NO_REDRAW_RESIZE | DWT.V_SCROLL | DWT.H_SCROLL);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
64 final ScrollBar hBar = canvas.getHorizontalBar ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
65 void onHBarSelection (Event e) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
66 int hSelection = hBar.getSelection ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
67 int destX = -hSelection - origin.x;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
68 Rectangle rect = image.getBounds ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
69 canvas.scroll (destX, 0, 0, 0, rect.width, rect.height, false);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
70 origin.x = -hSelection;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
71 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
72 final ScrollBar vBar = canvas.getVerticalBar ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
73 void onVBarSelection(Event e) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
74 int vSelection = vBar.getSelection ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
75 int destY = -vSelection - origin.y;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
76 Rectangle rect = image.getBounds ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
77 canvas.scroll (0, destY, 0, 0, rect.width, rect.height, false);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
78 origin.y = -vSelection;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
79 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
80 void onResize(Event e) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
81 Rectangle rect = image.getBounds ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
82 Rectangle client = canvas.getClientArea ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
83 hBar.setMaximum (rect.width);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
84 vBar.setMaximum (rect.height);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
85 hBar.setThumb (Math.min (rect.width, client.width));
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
86 vBar.setThumb (Math.min (rect.height, client.height));
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
87 int hPage = rect.width - client.width;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
88 int vPage = rect.height - client.height;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
89 int hSelection = hBar.getSelection ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
90 int vSelection = vBar.getSelection ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
91 if (hSelection >= hPage) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
92 if (hPage <= 0) hSelection = 0;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
93 origin.x = -hSelection;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
94 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
95 if (vSelection >= vPage) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
96 if (vPage <= 0) vSelection = 0;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
97 origin.y = -vSelection;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
98 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
99 canvas.redraw ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
100 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
101 void onPaint (Event e) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
102 GC gc = e.gc;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
103 gc.drawImage (image, origin.x, origin.y);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
104 Rectangle rect = image.getBounds ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
105 Rectangle client = canvas.getClientArea ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
106 int marginWidth = client.width - rect.width;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
107 if (marginWidth > 0) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
108 gc.fillRectangle (rect.width, 0, marginWidth, client.height);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
109 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
110 int marginHeight = client.height - rect.height;
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
111 if (marginHeight > 0) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
112 gc.fillRectangle (0, rect.height, client.width, marginHeight);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
113 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
114 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
115
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
116 hBar.addListener (DWT.Selection, dgListener(&onHBarSelection));
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
117 vBar.addListener (DWT.Selection, dgListener(&onVBarSelection));
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
118 canvas.addListener (DWT.Resize, dgListener(&onResize));
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
119 canvas.addListener (DWT.Paint, dgListener(&onPaint));
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
120
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
121 shell.setSize (200, 150);
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
122 shell.open ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
123 while (!shell.isDisposed ()) {
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
124 if (!display.readAndDispatch ()) display.sleep ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
125 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
126 originalImage.dispose();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
127 display.dispose ();
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
128 }
29bf9ba4756b Port of all canvas snippets
Bill Baxter <bill@billbaxter.com>
parents:
diff changeset
129