source: trunk/tests/SWT/java/SWT003_03.java

Last change on this file was 12, checked in by lpino, 18 years ago
  • Initial commit
File size: 4.7 KB
Line 
1/*
2 * SWT003_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
12import org.eclipse.swt.*;
13import org.eclipse.swt.widgets.*;
14import org.eclipse.swt.events.*;
15import org.eclipse.swt.graphics.*;
16
17/**
18 * This example tests partial invalidation and clipping.
19 * When the test is started it draws the grid 20 by 20 pixels.
20 *
21 * Upon the first closure the grid 10 by 10 pixels is drawn and
22 * only the part of the window is invalidated to see the effect.
23 *
24 * When the shell is closed for the 2nd time the grid 5 by 5 is
25 * drawn after the clipping rectangle is set. Other (clipped) parts
26 * of the shell should stay dirty when, for example, another window
27 * is being moved over them.
28 *
29 * When the shell is closed for the 3rd time the same as above happens,
30 * but the clipping area is the region that consists of two intersecting
31 * rectangles.
32 *
33 * Being closed for the 4rd time the shell turns off its redraw state causing
34 * all further drawing operations to be ignored. The screen area under the
35 * shell should become dirty.
36 *
37 * And upon the final closure the shell sets the redraw state back to true
38 * which should result into the correct cleanup (no screen area should be
39 * left durty after the shell is closed).
40 */
41
42public class SWT003_03 extends SWTTestCase {
43
44static {
45 STEP= "003";
46 TEST = "03";
47 DESC = "Invalidation & Clipping";
48}
49
50int closes = 0;
51
52public static void main (String [] args) {
53 go (new SWT003_03 ());
54}
55
56Shell createTopShell (Display display) {
57
58 final Shell shell = new Shell (display,
59 SWT.SHELL_TRIM | /*SWT.NO_REDRAW_RESIZE |*/ SWT.NO_MERGE_PAINTS);
60
61 shell.addShellListener (new ShellAdapter () {
62 public void shellClosed (ShellEvent e) {
63 closes++;
64 e.doit = false;
65 switch (closes) {
66 case 1:
67 saySubObjective ("\nInvalidate");
68 shell.redraw (110, 110, 101, 101, true);
69 break;
70 case 2:
71 saySubObjective ("\nRectangle Clip");
72 shell.redraw ();
73 break;
74 case 3:
75 saySubObjective ("\nRegion Clip");
76 shell.redraw ();
77 break;
78 case 4:
79 saySubObjective ("\nNo redraw: On");
80 shell.setRedraw (false);
81 break;
82 default:
83 saySubObjective ("\nNo redraw: Off");
84 shell.setRedraw (true);
85 e.doit = true;
86 }
87 }
88 });
89
90 final Color red = new Color (shell.getDisplay (), 255,0,0);
91 shell.addPaintListener(new PaintListener () {
92 public void paintControl(PaintEvent event) {
93 GC gc = event.gc;
94 Rectangle r = shell.getClientArea();
95
96 switch (closes) {
97 case 1 :
98 drawGrid (gc, 0, 0, r.width, r.height, 10);
99 break;
100 case 2 :
101 say ("Clip before: " + gc.getClipping ().toString ());
102 gc.setClipping (200, 100, 41, 201);
103 say ("Clip after: " + gc.getClipping ().toString ());
104 drawGrid (gc, 0, 0, r.width, r.height, 5);
105 break;
106 case 3 :
107 say ("Clip before: " + gc.getClipping ().toString ());
108 Region rg1 = new Region ();
109 rg1.add (new Rectangle (100, 100, 81, 81));
110 rg1.add (new Rectangle (160, 160, 81, 81));
111 Region rg2 = new Region ();
112 rg2.add (new Rectangle (300, 100, 81, 81));
113 rg2.add (new Rectangle (360, 160, 81, 81));
114 rg1.add (rg2);
115 gc.setClipping (rg1);
116 say ("Clip after: " + gc.getClipping ().toString ());
117 drawGrid (gc, 0, 0, r.width, r.height, 5);
118 rg1.dispose ();
119 rg2.dispose ();
120 break;
121 default :
122 drawGrid (gc, 0, 0, r.width, r.height, 20);
123 break;
124 }
125 }
126 });
127
128 Rectangle dr = display.getBounds ();
129 Rectangle sr = shell.getBounds ();
130 sr.x = (dr.width-sr.width)/2;
131 sr.y = (dr.height-sr.height)/2;
132 shell.setBounds (sr);
133 return shell;
134}
135
136void drawGrid (GC gc, int x0, int y0, int width, int height, int step)
137{
138 int i;
139 for (i = 0; i <= width/step; i++)
140 gc.drawLine (x0+i*step, y0, x0+i*step, y0+height);
141 for (i = 0; i <= height/step; i++)
142 gc.drawLine (x0, y0+i*step, x0+width, y0+i*step);
143}
144
145}
Note: See TracBrowser for help on using the repository browser.