comparison snippets/canvas/Snippet48.d @ 90:29bf9ba4756b

Port of all canvas snippets
author Bill Baxter <bill@billbaxter.com>
date Tue, 20 May 2008 12:10:54 +0900
parents
children 1f0a7a472680
comparison
equal deleted inserted replaced
89:cbba80cceb7a 90:29bf9ba4756b
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 * D Port:
11 * Bill Baxter <wbaxter> at gmail com
12 *******************************************************************************/
13 module snippets.canvas.Snippet48;
14
15 /*
16 * Canvas example snippet: scroll an image (flicker free, no double buffering)
17 *
18 * For a list of all SWT example snippets see
19 * http://www.eclipse.org/swt/snippets/
20 */
21 import dwt.DWT;
22 import dwt.graphics.GC;
23 import dwt.graphics.Point;
24 import dwt.graphics.Rectangle;
25 import dwt.graphics.Color;
26 import dwt.graphics.Image;
27 import dwt.layout.FillLayout;
28 import dwt.widgets.Display;
29 import dwt.widgets.Shell;
30 import dwt.widgets.Event;
31 import dwt.widgets.Listener;
32 import dwt.widgets.FileDialog;
33 import dwt.widgets.Canvas;
34 import dwt.widgets.ScrollBar;
35 import dwt.layout.FillLayout;
36
37 import dwt.dwthelper.utils;
38
39 void main () {
40 Display display = new Display ();
41 Shell shell = new Shell (display);
42 shell.setLayout(new FillLayout());
43 Image originalImage = null;
44 FileDialog dialog = new FileDialog (shell, DWT.OPEN);
45 dialog.setText ("Open an image file or cancel");
46 String string = dialog.open ();
47 if (string !is null) {
48 originalImage = new Image (display, string);
49 }
50 if (originalImage is null) {
51 int width = 150, height = 200;
52 originalImage = new Image (display, width, height);
53 GC gc = new GC (originalImage);
54 gc.fillRectangle (0, 0, width, height);
55 gc.drawLine (0, 0, width, height);
56 gc.drawLine (0, height, width, 0);
57 gc.drawText ("Default Image", 10, 10);
58 gc.dispose ();
59 }
60 final Image image = originalImage;
61 final Point origin = new Point (0, 0);
62 final Canvas canvas = new Canvas (shell, DWT.NO_BACKGROUND |
63 DWT.NO_REDRAW_RESIZE | DWT.V_SCROLL | DWT.H_SCROLL);
64 final ScrollBar hBar = canvas.getHorizontalBar ();
65 void onHBarSelection (Event e) {
66 int hSelection = hBar.getSelection ();
67 int destX = -hSelection - origin.x;
68 Rectangle rect = image.getBounds ();
69 canvas.scroll (destX, 0, 0, 0, rect.width, rect.height, false);
70 origin.x = -hSelection;
71 }
72 final ScrollBar vBar = canvas.getVerticalBar ();
73 void onVBarSelection(Event e) {
74 int vSelection = vBar.getSelection ();
75 int destY = -vSelection - origin.y;
76 Rectangle rect = image.getBounds ();
77 canvas.scroll (0, destY, 0, 0, rect.width, rect.height, false);
78 origin.y = -vSelection;
79 }
80 void onResize(Event e) {
81 Rectangle rect = image.getBounds ();
82 Rectangle client = canvas.getClientArea ();
83 hBar.setMaximum (rect.width);
84 vBar.setMaximum (rect.height);
85 hBar.setThumb (Math.min (rect.width, client.width));
86 vBar.setThumb (Math.min (rect.height, client.height));
87 int hPage = rect.width - client.width;
88 int vPage = rect.height - client.height;
89 int hSelection = hBar.getSelection ();
90 int vSelection = vBar.getSelection ();
91 if (hSelection >= hPage) {
92 if (hPage <= 0) hSelection = 0;
93 origin.x = -hSelection;
94 }
95 if (vSelection >= vPage) {
96 if (vPage <= 0) vSelection = 0;
97 origin.y = -vSelection;
98 }
99 canvas.redraw ();
100 }
101 void onPaint (Event e) {
102 GC gc = e.gc;
103 gc.drawImage (image, origin.x, origin.y);
104 Rectangle rect = image.getBounds ();
105 Rectangle client = canvas.getClientArea ();
106 int marginWidth = client.width - rect.width;
107 if (marginWidth > 0) {
108 gc.fillRectangle (rect.width, 0, marginWidth, client.height);
109 }
110 int marginHeight = client.height - rect.height;
111 if (marginHeight > 0) {
112 gc.fillRectangle (0, rect.height, client.width, marginHeight);
113 }
114 }
115
116 hBar.addListener (DWT.Selection, dgListener(&onHBarSelection));
117 vBar.addListener (DWT.Selection, dgListener(&onVBarSelection));
118 canvas.addListener (DWT.Resize, dgListener(&onResize));
119 canvas.addListener (DWT.Paint, dgListener(&onPaint));
120
121 shell.setSize (200, 150);
122 shell.open ();
123 while (!shell.isDisposed ()) {
124 if (!display.readAndDispatch ()) display.sleep ();
125 }
126 originalImage.dispose();
127 display.dispose ();
128 }
129