1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet48"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * D Port:
46  *     Bill Baxter <wbaxter> at gmail com
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet48;
49  
50 /*
51  * Canvas example snippet: scroll an image (flicker free, no double buffering)
52  *
53  * For a list of all SWT example snippets see
54  * http://www.eclipse.org/swt/snippets/
55  */
56 import org.eclipse.swt.SWT;
57 import org.eclipse.swt.graphics.GC;
58 import org.eclipse.swt.graphics.Point;
59 import org.eclipse.swt.graphics.Rectangle;
60 import org.eclipse.swt.graphics.Color;
61 import org.eclipse.swt.graphics.Image;
62 import org.eclipse.swt.layout.FillLayout;
63 import org.eclipse.swt.widgets.Display;
64 import org.eclipse.swt.widgets.Shell;
65 import org.eclipse.swt.widgets.Event;
66 import org.eclipse.swt.widgets.Listener;
67 import org.eclipse.swt.widgets.FileDialog;
68 import org.eclipse.swt.widgets.Canvas;
69 import org.eclipse.swt.widgets.ScrollBar;
70 import org.eclipse.swt.layout.FillLayout;
71 
72 import java.lang.all;
73 
74 void main () {
75 	Display display = new Display ();
76 	Shell shell = new Shell (display);
77 	shell.setLayout(new FillLayout());
78 	Image originalImage = null;
79 	FileDialog dialog = new FileDialog (shell, SWT.OPEN);
80 	dialog.setText ("Open an image file or cancel");
81 	String string = dialog.open ();
82 	if (string !is null) {
83 		originalImage = new Image (display, string);
84 	}
85 	if (originalImage is null) {
86 		int width = 150, height = 200;
87 		originalImage = new Image (display, width, height);
88 		GC gc = new GC (originalImage);
89 		gc.fillRectangle (0, 0, width, height);
90 		gc.drawLine (0, 0, width, height);
91 		gc.drawLine (0, height, width, 0);
92 		gc.drawText ("Default Image", 10, 10);
93 		gc.dispose ();
94 	}
95 	Image image = originalImage;
96 	Point origin = new Point (0, 0);
97 	Canvas canvas = new Canvas (shell, SWT.NO_BACKGROUND |
98 			SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL);
99 	ScrollBar hBar = canvas.getHorizontalBar ();
100     void onHBarSelection (Event e) {
101         int hSelection = hBar.getSelection ();
102         int destX = -hSelection - origin.x;
103         Rectangle rect = image.getBounds ();
104         canvas.scroll (destX, 0, 0, 0, rect.width, rect.height, false);
105         origin.x = -hSelection;
106     }
107 	ScrollBar vBar = canvas.getVerticalBar ();
108     void onVBarSelection(Event e) {
109         int vSelection = vBar.getSelection ();
110         int destY = -vSelection - origin.y;
111         Rectangle rect = image.getBounds ();
112         canvas.scroll (0, destY, 0, 0, rect.width, rect.height, false);
113         origin.y = -vSelection;
114     }
115     void onResize(Event e) {
116         Rectangle rect = image.getBounds ();
117         Rectangle client = canvas.getClientArea ();
118         hBar.setMaximum (rect.width);
119         vBar.setMaximum (rect.height);
120         hBar.setThumb (Math.min (rect.width, client.width));
121         vBar.setThumb (Math.min (rect.height, client.height));
122         int hPage = rect.width - client.width;
123         int vPage = rect.height - client.height;
124         int hSelection = hBar.getSelection ();
125         int vSelection = vBar.getSelection ();
126         if (hSelection >= hPage) {
127             if (hPage <= 0) hSelection = 0;
128             origin.x = -hSelection;
129         }
130         if (vSelection >= vPage) {
131             if (vPage <= 0) vSelection = 0;
132             origin.y = -vSelection;
133         }
134         canvas.redraw ();
135     }
136     void onPaint (Event e) {
137         GC gc = e.gc;
138         gc.drawImage (image, origin.x, origin.y);
139         Rectangle rect = image.getBounds ();
140         Rectangle client = canvas.getClientArea ();
141         int marginWidth = client.width - rect.width;
142         if (marginWidth > 0) {
143             gc.fillRectangle (rect.width, 0, marginWidth, client.height);
144         }
145         int marginHeight = client.height - rect.height;
146         if (marginHeight > 0) {
147             gc.fillRectangle (0, rect.height, client.width, marginHeight);
148         }
149     }
150 
151 	hBar.addListener (SWT.Selection, dgListener(&onHBarSelection));
152 	vBar.addListener (SWT.Selection, dgListener(&onVBarSelection));
153 	canvas.addListener (SWT.Resize,  dgListener(&onResize));
154 	canvas.addListener (SWT.Paint, dgListener(&onPaint));
155 
156 	shell.setSize (200, 150);
157 	shell.open ();
158 	while (!shell.isDisposed ()) {
159 		if (!display.readAndDispatch ()) display.sleep ();
160 	}
161 	originalImage.dispose();
162 	display.dispose ();
163 }
164