1 | /*
|
---|
2 | * SWT006_03.java
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (c) 2002, 2004 EclipseOS2 Team.
|
---|
7 | * This file is made available under the terms of the Common Public License v1.0
|
---|
8 | * which accompanies this distribution, and is available at
|
---|
9 | * http://www.eclipse.org/legal/cpl-v10.html
|
---|
10 | */
|
---|
11 |
|
---|
12 | import org.eclipse.swt.*;
|
---|
13 | import org.eclipse.swt.widgets.*;
|
---|
14 | import org.eclipse.swt.events.*;
|
---|
15 | import org.eclipse.swt.graphics.*;
|
---|
16 | import org.eclipse.swt.layout.FormAttachment;
|
---|
17 | import org.eclipse.swt.layout.FormData;
|
---|
18 | import org.eclipse.swt.layout.FormLayout;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * This testcase tests GC.copyArea() functionality as well as some clipping
|
---|
22 | * operations (GC.getClipping(Region) ad GC.isClipped()).
|
---|
23 | *
|
---|
24 | * At first, an image is loaded. Then it is broken into four equal rectangles
|
---|
25 | * and these rectangles are swapped diagonally. Then swapping is repeated, so
|
---|
26 | * finally we should get the original image.
|
---|
27 | *
|
---|
28 | * After pressing the "Start" button the image inside a dotted rectangle
|
---|
29 | * starts scrolling diagonally, from top left to bottom right.
|
---|
30 | *
|
---|
31 | * If the "Debug" check box is checked during scrolling the output should be:
|
---|
32 |
|
---|
33 | Debug ON
|
---|
34 | paint = 0
|
---|
35 | update = Rectangle {0, 0, 632, 450}
|
---|
36 | is clipped = false
|
---|
37 | clip = Rectangle {0, 0, 632, 450}
|
---|
38 | clip region = Rectangle {0, 0, 632, 450}
|
---|
39 | is clipped (after) = true
|
---|
40 | paint = 1
|
---|
41 | update = Rectangle {170, 80, 280, 10}
|
---|
42 | is clipped = false
|
---|
43 | clip = Rectangle {170, 80, 280, 280}
|
---|
44 | clip region = Rectangle {170, 80, 280, 280}
|
---|
45 | is clipped (after) = true
|
---|
46 | paint = 0
|
---|
47 | update = Rectangle {170, 90, 10, 270}
|
---|
48 | is clipped = true
|
---|
49 | clip = Rectangle {170, 80, 280, 10}
|
---|
50 | clip region = Rectangle {170, 80, 280, 10}
|
---|
51 | is clipped (after) = true
|
---|
52 | ... (last 12 lines are repeated until termination or turning debug off)
|
---|
53 |
|
---|
54 | * to ensure that the update region is set correctly inside the paint event
|
---|
55 | * (only parts of the area being scrolled that are left uncovered after
|
---|
56 | * placing the area into a new origin should be updated) and the clipping is
|
---|
57 | * also reported correctly.
|
---|
58 | *
|
---|
59 | * NOTE. This testcase, when scrolling, causes a trap (SYS3175) in JITC.DLL
|
---|
60 | * wheh running under IBM JRE 1.3.1 (at least, builds co131-20030618 and
|
---|
61 | * co131-20020227). Most likely this is a bug inside the IBM JIT compiler --
|
---|
62 | * the trap disappears when we disable JIT.
|
---|
63 | */
|
---|
64 |
|
---|
65 | public class SWT006_03 extends SWTTestCase {
|
---|
66 |
|
---|
67 | static {
|
---|
68 | STEP= "006";
|
---|
69 | TEST = "03";
|
---|
70 | DESC = "Images - Scrolling";
|
---|
71 | }
|
---|
72 |
|
---|
73 | boolean started;
|
---|
74 |
|
---|
75 | Shell shell;
|
---|
76 |
|
---|
77 | Button startStop;
|
---|
78 |
|
---|
79 | Image background = null;
|
---|
80 |
|
---|
81 | final static String imageDir = System.getProperty ("user.dir") +
|
---|
82 | "/tests/SWT/images/";
|
---|
83 |
|
---|
84 | public static void main (String [] args) {
|
---|
85 | go (new SWT006_03 ());
|
---|
86 | }
|
---|
87 |
|
---|
88 | boolean debug = false;
|
---|
89 |
|
---|
90 | class ScrollData {
|
---|
91 | int dx = 0;
|
---|
92 | int dy = 0;
|
---|
93 | int delta = 1;
|
---|
94 | int delay = 10;
|
---|
95 | boolean terminate;
|
---|
96 | }
|
---|
97 |
|
---|
98 | ScrollData sData = new ScrollData();
|
---|
99 | int sWidth = 280;
|
---|
100 | int sHeight = 280;
|
---|
101 |
|
---|
102 | Shell createTopShell (Display display) {
|
---|
103 |
|
---|
104 | shell = new Shell (display,
|
---|
105 | SWT.DIALOG_TRIM | SWT.NO_REDRAW_RESIZE | SWT.NO_MERGE_PAINTS | SWT.NO_BACKGROUND);
|
---|
106 |
|
---|
107 | final Image img = new Image (display, imageDir + "img01.24.bmp");
|
---|
108 | {
|
---|
109 | Rectangle r = img.getBounds();
|
---|
110 | GC gc = new GC (img);
|
---|
111 | int w = r.width / 2;
|
---|
112 | int h = r.height / 2;
|
---|
113 | Image i = new Image (null, w, h);
|
---|
114 | for (int j = 0; j < 2; j++) {
|
---|
115 | gc.copyArea (i, w, h);
|
---|
116 | gc.copyArea (0, 0, w, h, w, h);
|
---|
117 | gc.copyArea (w, 0, w, h, 0, 0);
|
---|
118 | gc.copyArea (0, h, w, h, w, 0);
|
---|
119 | gc.copyArea (0, 0, w, h, 0, h);
|
---|
120 | gc.drawImage (i, 0, 0);
|
---|
121 | }
|
---|
122 | i.dispose();
|
---|
123 | gc.dispose();
|
---|
124 | }
|
---|
125 |
|
---|
126 | final Image rotImg = new Image (display, sWidth, sHeight);
|
---|
127 |
|
---|
128 | final Display dspl = display;
|
---|
129 | final Thread runner = new Thread () {
|
---|
130 | public synchronized void run () {
|
---|
131 | while (!sData.terminate) {
|
---|
132 | synchronized (sData) {
|
---|
133 | dspl.asyncExec (new Runnable () {
|
---|
134 | public void run() {
|
---|
135 | if (!shell.isDisposed ()) {
|
---|
136 | Rectangle cr = shell.getClientArea();
|
---|
137 | int x0 = ((cr.width - sWidth) / 2) / 10 * 10;
|
---|
138 | int y0 = ((cr.height - sHeight) / 2) / 10 * 10;
|
---|
139 | GC gc = new GC (shell);
|
---|
140 | gc.setClipping (new Rectangle (x0, y0, sWidth, sHeight));
|
---|
141 | gc.copyArea (
|
---|
142 | x0, y0, sWidth, sHeight,
|
---|
143 | x0 + sData.delta, y0 + sData.delta);
|
---|
144 | gc.dispose();
|
---|
145 | sData.dx += sData.delta; sData.dy += sData.delta;
|
---|
146 | if (sData.dx >= sWidth) sData.dx = 0;
|
---|
147 | if (sData.dy >= sHeight) sData.dy = 0;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | });
|
---|
151 | }
|
---|
152 | try {
|
---|
153 | Thread.sleep (sData.delay);
|
---|
154 | } catch (InterruptedException e) {
|
---|
155 | }
|
---|
156 | }
|
---|
157 | System.out.println ("runner: terminated.");
|
---|
158 | }
|
---|
159 | };
|
---|
160 |
|
---|
161 | shell.addDisposeListener (new DisposeListener () {
|
---|
162 | public void widgetDisposed (DisposeEvent e) {
|
---|
163 | synchronized (sData) {
|
---|
164 | sData.terminate = true;
|
---|
165 | sData.notifyAll ();
|
---|
166 | }
|
---|
167 | synchronized (runner) {}
|
---|
168 | if (background != null) background.dispose();
|
---|
169 | img.dispose();
|
---|
170 | rotImg.dispose();
|
---|
171 | }
|
---|
172 | });
|
---|
173 |
|
---|
174 | shell.addPaintListener(new PaintListener () {
|
---|
175 | public void paintControl(PaintEvent event) {
|
---|
176 | GC gc = event.gc;
|
---|
177 |
|
---|
178 | Rectangle cr = shell.getClientArea();
|
---|
179 | int x0 = ((cr.width - sWidth) / 2) / 10 * 10;
|
---|
180 | int y0 = ((cr.height - sHeight) / 2) / 10 * 10;
|
---|
181 |
|
---|
182 | Rectangle r = new Rectangle (event.x, event.y, event.width, event.height);
|
---|
183 |
|
---|
184 | if (debug) {
|
---|
185 | System.out.println ("paint = " + event.count);
|
---|
186 | System.out.println ("update = " + r);
|
---|
187 | System.out.println ("is clipped = " + gc.isClipped());
|
---|
188 | System.out.println ("clip = " + gc.getClipping());
|
---|
189 | Region rgn = new Region();
|
---|
190 | gc.getClipping (rgn);
|
---|
191 | System.out.println ("clip region = " + rgn.getBounds());
|
---|
192 | rgn.dispose();
|
---|
193 | }
|
---|
194 |
|
---|
195 | // narrow clipping area according to the current rectangle of the
|
---|
196 | // complex update region to optimize image drawing (current clipping
|
---|
197 | // is used inside GC.dtawImage() for optimizations).
|
---|
198 | gc.setClipping (r);
|
---|
199 | if (debug) System.out.println ("is clipped (after) = " + gc.isClipped());
|
---|
200 |
|
---|
201 | Rectangle interRect = new Rectangle (x0, y0, sWidth, sHeight);
|
---|
202 | interRect = interRect.intersection (r);
|
---|
203 | if (!started || !interRect.equals (r)) {
|
---|
204 | drawCheckers (gc,
|
---|
205 | (r.x / 20) * 20, (r.y / 20) * 20,
|
---|
206 | r.width + 20, r.height + 20
|
---|
207 | );
|
---|
208 | gc.drawFocus (x0 - 1, y0 - 1, sWidth + 2, sHeight + 2);
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (started) {
|
---|
212 | synchronized (sData) {
|
---|
213 | gc.drawImage (rotImg,
|
---|
214 | 0, 0, sWidth - sData.dx, sHeight - sData.dy,
|
---|
215 | x0 + sData.dx, y0 + sData.dy, sWidth - sData.dx, sHeight - sData.dy
|
---|
216 | );
|
---|
217 | gc.drawImage (rotImg,
|
---|
218 | sWidth - sData.dx, sHeight - sData.dy, sData.dx, sData.dy,
|
---|
219 | x0, y0, sData.dx, sData.dy
|
---|
220 | );
|
---|
221 | gc.drawImage (rotImg,
|
---|
222 | sWidth - sData.dx, 0, sData.dx, sHeight - sData.dy,
|
---|
223 | x0, y0 + sData.dy, sData.dx, sHeight - sData.dy
|
---|
224 | );
|
---|
225 | gc.drawImage (rotImg,
|
---|
226 | 0, sHeight - sData.dy, sWidth - sData.dx, sData.dy,
|
---|
227 | x0 + sData.dx, y0, sWidth - sData.dx, sData.dy
|
---|
228 | );
|
---|
229 | }
|
---|
230 | } else {
|
---|
231 | Rectangle ir = img.getBounds();
|
---|
232 | int x = ((cr.width - ir.width) / 2) / 10 * 10 - 5;
|
---|
233 | int y = ((cr.height - ir.height) / 2) / 10 * 10 - 5;
|
---|
234 | gc.drawImage (img, x, y);
|
---|
235 | gc.copyArea (rotImg, x0, y0);
|
---|
236 | }
|
---|
237 | }
|
---|
238 | });
|
---|
239 |
|
---|
240 | startStop = new Button (shell, SWT.PUSH);
|
---|
241 | startStop.setText ("Start");
|
---|
242 | startStop.addSelectionListener (new SelectionAdapter () {
|
---|
243 | public void widgetSelected (SelectionEvent e) {
|
---|
244 | if (started) {
|
---|
245 | shell.close();
|
---|
246 | } else {
|
---|
247 | started = true;
|
---|
248 | startStop.setText ("Stop");
|
---|
249 | runner.start();
|
---|
250 | }
|
---|
251 | }
|
---|
252 | });
|
---|
253 |
|
---|
254 | Button checks = new Button (shell, SWT.CHECK);
|
---|
255 | checks.setText ("Debug");
|
---|
256 | checks.setSelection (debug);
|
---|
257 | checks.addSelectionListener (new SelectionAdapter () {
|
---|
258 | public void widgetSelected (SelectionEvent e) {
|
---|
259 | synchronized (sData) {
|
---|
260 | debug = ((Button)e.widget).getSelection ();
|
---|
261 | if (debug) {
|
---|
262 | if (sData.dx % 10 != 0) {
|
---|
263 | sData.dx += 10 - (sData.dx % 10);
|
---|
264 | if (sData.dx >= sWidth) sData.dx = 0;
|
---|
265 | }
|
---|
266 | if (sData.dy % 10 != 0) {
|
---|
267 | sData.dy += 10 - (sData.dy % 10);
|
---|
268 | if (sData.dy >= sHeight) sData.dy = 0;
|
---|
269 | }
|
---|
270 | sData.delta = 10;
|
---|
271 | sData.delay = 1000;
|
---|
272 | System.out.println ("Debug ON");
|
---|
273 | } else {
|
---|
274 | sData.delta = 1;
|
---|
275 | sData.delay = 10;
|
---|
276 | System.out.println ("Debug OFF");
|
---|
277 | }
|
---|
278 | }
|
---|
279 | shell.redraw();
|
---|
280 | }
|
---|
281 | });
|
---|
282 |
|
---|
283 | FormData fdata = new FormData();
|
---|
284 | fdata.right = new FormAttachment (100, -10);
|
---|
285 | fdata.bottom = new FormAttachment (100, -10);
|
---|
286 | startStop.setLayoutData (fdata);
|
---|
287 | fdata = new FormData();
|
---|
288 | fdata.left = new FormAttachment (0, 10);
|
---|
289 | fdata.bottom = new FormAttachment (100, -10);
|
---|
290 | checks.setLayoutData (fdata);
|
---|
291 | FormLayout formLayout = new FormLayout ();
|
---|
292 | shell.setLayout (formLayout);
|
---|
293 |
|
---|
294 | Rectangle dr = display.getClientArea ();
|
---|
295 | Rectangle sr = new Rectangle (0, 0, 640, 480);
|
---|
296 | sr.x = (dr.width-sr.width)/2;
|
---|
297 | sr.y = (dr.height-sr.height)/2;
|
---|
298 | shell.setBounds (sr);
|
---|
299 |
|
---|
300 | return shell;
|
---|
301 | }
|
---|
302 |
|
---|
303 | void drawCheckers (GC gc, int x0, int y0, int width, int height) {
|
---|
304 | if (background == null) {
|
---|
305 | int w = 20, h = 20;
|
---|
306 | Display display = Display.getDefault();
|
---|
307 | background = new Image (display, w, h);
|
---|
308 | GC bgc = new GC (background);
|
---|
309 | Color c1 = new Color (display, 255, 255, 255);
|
---|
310 | Color c2 = new Color (display, 192, 192, 192);
|
---|
311 | bgc.setBackground (c1);
|
---|
312 | bgc.fillRectangle(0, 0, 10, 10);
|
---|
313 | bgc.fillRectangle(10, 10, 10, 10);
|
---|
314 | bgc.setBackground (c2);
|
---|
315 | bgc.fillRectangle(10, 0, 10, 10);
|
---|
316 | bgc.fillRectangle(0, 10, 10, 10);
|
---|
317 | c1.dispose();
|
---|
318 | c2.dispose();
|
---|
319 | bgc.dispose();
|
---|
320 | }
|
---|
321 | Rectangle r = background.getBounds();
|
---|
322 | for (int y = 0; y <= height; y += r.height)
|
---|
323 | for (int x = 0; x <= width; x += r.width)
|
---|
324 | gc.drawImage (background, x0 + x, y0 + y);
|
---|
325 | }
|
---|
326 |
|
---|
327 | }
|
---|
328 |
|
---|