Mercurial > projects > dwt2
annotate org.eclipse.swt.snippets/src/org/eclipse/swt/snippets/Snippet48.d @ 112:9f4c18c268b2
Update to compile and execute with dmd 2.052.
author | kntroh |
---|---|
date | Wed, 16 Mar 2011 21:53:53 +0900 |
parents | 4e5843b771cc |
children |
rev | line source |
---|---|
28 | 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 org.eclipse.swt.snippets.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 org.eclipse.swt.SWT; | |
22 import org.eclipse.swt.graphics.GC; | |
23 import org.eclipse.swt.graphics.Point; | |
24 import org.eclipse.swt.graphics.Rectangle; | |
25 import org.eclipse.swt.graphics.Color; | |
26 import org.eclipse.swt.graphics.Image; | |
27 import org.eclipse.swt.layout.FillLayout; | |
28 import org.eclipse.swt.widgets.Display; | |
29 import org.eclipse.swt.widgets.Shell; | |
30 import org.eclipse.swt.widgets.Event; | |
31 import org.eclipse.swt.widgets.Listener; | |
32 import org.eclipse.swt.widgets.FileDialog; | |
33 import org.eclipse.swt.widgets.Canvas; | |
34 import org.eclipse.swt.widgets.ScrollBar; | |
35 import org.eclipse.swt.layout.FillLayout; | |
36 | |
37 import java.lang.all; | |
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, SWT.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 } | |
112 | 60 Image image = originalImage; |
61 Point origin = new Point (0, 0); | |
62 Canvas canvas = new Canvas (shell, SWT.NO_BACKGROUND | | |
29
4e5843b771cc
First snippets buildable on windows
Frank Benoit <benoit@tionex.de>
parents:
28
diff
changeset
|
63 SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); |
112 | 64 ScrollBar hBar = canvas.getHorizontalBar (); |
28 | 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 } | |
112 | 72 ScrollBar vBar = canvas.getVerticalBar (); |
28 | 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 (SWT.Selection, dgListener(&onHBarSelection)); | |
117 vBar.addListener (SWT.Selection, dgListener(&onVBarSelection)); | |
118 canvas.addListener (SWT.Resize, dgListener(&onResize)); | |
119 canvas.addListener (SWT.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 |