1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet275"
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, 2007 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.Snippet275;
49 
50 /*
51  * Canvas snippet: update a portion of a Canvas frequently
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.Color;
59 import org.eclipse.swt.graphics.Point;
60 import org.eclipse.swt.graphics.Image;
61 import org.eclipse.swt.widgets.Display;
62 import org.eclipse.swt.widgets.Shell;
63 import org.eclipse.swt.widgets.Event;
64 import org.eclipse.swt.widgets.Listener;
65 import org.eclipse.swt.widgets.Canvas;
66 
67 import java.lang.all;
68 
69 version(Tango){
70     import tango.util.Convert;
71 } else { // Phobos
72     import std.conv;
73 }
74 
75 void main () {
76     String value;
77 	int INTERVAL = 888;
78 	Display display = new Display ();
79 	Image image = new Image (display, 750, 750);
80 	GC gc = new GC (image);
81 	gc.setBackground (display.getSystemColor (SWT.COLOR_RED));
82 	gc.fillRectangle (image.getBounds ());
83 	gc.dispose ();
84 
85 	Shell shell = new Shell (display);
86 	shell.setBounds (10, 10, 790, 790);
87 	Canvas canvas = new Canvas (shell, SWT.NONE);
88 	canvas.setBounds (10, 10, 750, 750);
89 
90     void onPaint (Event event) {
91         value = to!(String)(System.currentTimeMillis ());
92         event.gc.drawImage (image, 0, 0);
93         event.gc.drawString (value, 10, 10, true);
94     }
95 	canvas.addListener (SWT.Paint, dgListener(&onPaint));
96 
97 	display.timerExec (INTERVAL, new class Runnable {
98         void run () {
99 			if (canvas.isDisposed ()) return;
100 			// canvas.redraw (); // <-- bad, damages more than is needed
101 			GC gc = new GC (canvas);
102 			Point extent = gc.stringExtent (value ~ '0');
103 			gc.dispose ();
104 			canvas.redraw (10, 10, extent.x, extent.y, false);
105 			display.timerExec (INTERVAL, this);
106 		}
107 	});
108 	shell.open ();
109 	while (!shell.isDisposed ()){
110 		if (!display.readAndDispatch ()) display.sleep ();
111 	}
112 	image.dispose ();
113 	display.dispose ();
114 }