Mercurial > projects > dwt-samples
annotate examples/controlexample/CanvasTab.d @ 161:eb84f9418bbf
Change module names to this not have examples in the FQN.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Mon, 01 Sep 2008 22:24:27 +0200 |
parents | 8a1930f94cbb |
children |
rev | line source |
---|---|
78 | 1 /******************************************************************************* |
2 * Copyright (c) 2000, 2007 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
161
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
80
diff
changeset
|
13 module controlexample.CanvasTab; |
78 | 14 |
15 | |
16 | |
17 import dwt.DWT; | |
18 import dwt.events.ControlAdapter; | |
19 import dwt.events.ControlEvent; | |
20 import dwt.events.PaintEvent; | |
21 import dwt.events.PaintListener; | |
22 import dwt.events.SelectionAdapter; | |
23 import dwt.events.SelectionEvent; | |
24 import dwt.graphics.Color; | |
25 import dwt.graphics.Font; | |
26 import dwt.graphics.GC; | |
27 import dwt.graphics.Point; | |
28 import dwt.graphics.Rectangle; | |
29 import dwt.layout.GridData; | |
30 import dwt.layout.GridLayout; | |
31 import dwt.widgets.Button; | |
32 import dwt.widgets.Canvas; | |
33 import dwt.widgets.Caret; | |
34 import dwt.widgets.Composite; | |
35 import dwt.widgets.Group; | |
36 import dwt.widgets.ScrollBar; | |
37 import dwt.widgets.TabFolder; | |
38 import dwt.widgets.Widget; | |
39 | |
161
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
80
diff
changeset
|
40 import controlexample.Tab; |
eb84f9418bbf
Change module names to this not have examples in the FQN.
Frank Benoit <benoit@tionex.de>
parents:
80
diff
changeset
|
41 import controlexample.ControlExample; |
78 | 42 |
43 class CanvasTab : Tab { | |
44 static const int colors [] = [ | |
45 DWT.COLOR_RED, | |
46 DWT.COLOR_GREEN, | |
47 DWT.COLOR_BLUE, | |
48 DWT.COLOR_MAGENTA, | |
49 DWT.COLOR_YELLOW, | |
50 DWT.COLOR_CYAN, | |
51 DWT.COLOR_DARK_RED, | |
52 DWT.COLOR_DARK_GREEN, | |
53 DWT.COLOR_DARK_BLUE, | |
54 DWT.COLOR_DARK_MAGENTA, | |
55 DWT.COLOR_DARK_YELLOW, | |
56 DWT.COLOR_DARK_CYAN | |
57 ]; | |
58 static final char[] canvasString = "Canvas"; //$NON-NLS-1$ | |
59 | |
60 /* Example widgets and groups that contain them */ | |
61 Canvas canvas; | |
62 Group canvasGroup; | |
63 | |
64 /* Style widgets added to the "Style" group */ | |
65 Button horizontalButton, verticalButton, noBackgroundButton, noFocusButton, | |
66 noMergePaintsButton, noRedrawResizeButton, doubleBufferedButton; | |
67 | |
68 /* Other widgets added to the "Other" group */ | |
69 Button caretButton, fillDamageButton; | |
70 | |
71 int paintCount; | |
72 int cx, cy; | |
73 int maxX, maxY; | |
74 | |
75 /** | |
76 * Creates the Tab within a given instance of ControlExample. | |
77 */ | |
78 this(ControlExample instance) { | |
79 super(instance); | |
80 } | |
81 | |
82 /** | |
83 * Creates the "Other" group. | |
84 */ | |
85 void createOtherGroup () { | |
86 super.createOtherGroup (); | |
87 | |
88 /* Create display controls specific to this example */ | |
89 caretButton = new Button (otherGroup, DWT.CHECK); | |
90 caretButton.setText (ControlExample.getResourceString("Caret")); | |
91 fillDamageButton = new Button (otherGroup, DWT.CHECK); | |
92 fillDamageButton.setText (ControlExample.getResourceString("FillDamage")); | |
93 | |
94 /* Add the listeners */ | |
95 caretButton.addSelectionListener (new class() SelectionAdapter { | |
96 public void widgetSelected (SelectionEvent event) { | |
97 setCaret (); | |
98 } | |
99 }); | |
100 } | |
101 | |
102 /** | |
103 * Creates the "Example" group. | |
104 */ | |
105 void createExampleGroup () { | |
106 super.createExampleGroup (); | |
107 | |
108 /* Create a group for the canvas widget */ | |
109 canvasGroup = new Group (exampleGroup, DWT.NONE); | |
110 canvasGroup.setLayout (new GridLayout ()); | |
111 canvasGroup.setLayoutData (new GridData (DWT.FILL, DWT.FILL, true, true)); | |
112 canvasGroup.setText ("Canvas"); | |
113 } | |
114 | |
115 /** | |
116 * Creates the "Example" widgets. | |
117 */ | |
118 void createExampleWidgets () { | |
119 | |
120 /* Compute the widget style */ | |
121 int style = getDefaultStyle(); | |
122 if (horizontalButton.getSelection ()) style |= DWT.H_SCROLL; | |
123 if (verticalButton.getSelection ()) style |= DWT.V_SCROLL; | |
124 if (borderButton.getSelection ()) style |= DWT.BORDER; | |
125 if (noBackgroundButton.getSelection ()) style |= DWT.NO_BACKGROUND; | |
126 if (noFocusButton.getSelection ()) style |= DWT.NO_FOCUS; | |
127 if (noMergePaintsButton.getSelection ()) style |= DWT.NO_MERGE_PAINTS; | |
128 if (noRedrawResizeButton.getSelection ()) style |= DWT.NO_REDRAW_RESIZE; | |
129 if (doubleBufferedButton.getSelection ()) style |= DWT.DOUBLE_BUFFERED; | |
130 | |
131 /* Create the example widgets */ | |
132 paintCount = 0; cx = 0; cy = 0; | |
133 canvas = new Canvas (canvasGroup, style); | |
134 canvas.addPaintListener(new class() PaintListener { | |
135 public void paintControl(PaintEvent e) { | |
136 paintCount++; | |
137 GC gc = e.gc; | |
138 if (fillDamageButton.getSelection ()) { | |
139 Color color = e.display.getSystemColor (colors [paintCount % colors.length]); | |
140 gc.setBackground(color); | |
141 gc.fillRectangle(e.x, e.y, e.width, e.height); | |
142 } | |
143 Point size = canvas.getSize (); | |
144 gc.drawArc(cx + 1, cy + 1, size.x - 2, size.y - 2, 0, 360); | |
145 gc.drawRectangle(cx + (size.x - 10) / 2, cy + (size.y - 10) / 2, 10, 10); | |
146 Point extent = gc.textExtent(canvasString); | |
147 gc.drawString(canvasString, cx + (size.x - extent.x) / 2, cy - extent.y + (size.y - 10) / 2, true); | |
148 } | |
149 }); | |
150 canvas.addControlListener(new class() ControlAdapter { | |
151 public void controlResized(ControlEvent event) { | |
152 Point size = canvas.getSize (); | |
153 maxX = size.x * 3 / 2; maxY = size.y * 3 / 2; | |
154 resizeScrollBars (); | |
155 } | |
156 }); | |
157 ScrollBar bar = canvas.getHorizontalBar(); | |
158 if (bar !is null) { | |
159 hookListeners (bar); | |
160 bar.addSelectionListener(new class() SelectionAdapter { | |
161 public void widgetSelected(SelectionEvent event) { | |
162 scrollHorizontal (cast(ScrollBar)event.widget); | |
163 } | |
164 }); | |
165 } | |
166 bar = canvas.getVerticalBar(); | |
167 if (bar !is null) { | |
168 hookListeners (bar); | |
169 bar.addSelectionListener(new class() SelectionAdapter { | |
170 public void widgetSelected(SelectionEvent event) { | |
171 scrollVertical (cast(ScrollBar)event.widget); | |
172 } | |
173 }); | |
174 } | |
175 } | |
176 | |
177 /** | |
178 * Creates the "Style" group. | |
179 */ | |
180 void createStyleGroup() { | |
181 super.createStyleGroup(); | |
182 | |
183 /* Create the extra widgets */ | |
184 horizontalButton = new Button (styleGroup, DWT.CHECK); | |
185 horizontalButton.setText ("DWT.H_SCROLL"); | |
186 horizontalButton.setSelection(true); | |
187 verticalButton = new Button (styleGroup, DWT.CHECK); | |
188 verticalButton.setText ("DWT.V_SCROLL"); | |
189 verticalButton.setSelection(true); | |
190 borderButton = new Button (styleGroup, DWT.CHECK); | |
191 borderButton.setText ("DWT.BORDER"); | |
192 noBackgroundButton = new Button (styleGroup, DWT.CHECK); | |
193 noBackgroundButton.setText ("DWT.NO_BACKGROUND"); | |
194 noFocusButton = new Button (styleGroup, DWT.CHECK); | |
195 noFocusButton.setText ("DWT.NO_FOCUS"); | |
196 noMergePaintsButton = new Button (styleGroup, DWT.CHECK); | |
197 noMergePaintsButton.setText ("DWT.NO_MERGE_PAINTS"); | |
198 noRedrawResizeButton = new Button (styleGroup, DWT.CHECK); | |
199 noRedrawResizeButton.setText ("DWT.NO_REDRAW_RESIZE"); | |
200 doubleBufferedButton = new Button (styleGroup, DWT.CHECK); | |
201 doubleBufferedButton.setText ("DWT.DOUBLE_BUFFERED"); | |
202 } | |
203 | |
204 /** | |
205 * Creates the tab folder page. | |
206 * | |
207 * @param tabFolder org.eclipse.swt.widgets.TabFolder | |
208 * @return the new page for the tab folder | |
209 */ | |
210 Composite createTabFolderPage (TabFolder tabFolder) { | |
211 super.createTabFolderPage (tabFolder); | |
212 | |
213 /* | |
214 * Add a resize listener to the tabFolderPage so that | |
215 * if the user types into the example widget to change | |
216 * its preferred size, and then resizes the shell, we | |
217 * recalculate the preferred size correctly. | |
218 */ | |
219 tabFolderPage.addControlListener(new class() ControlAdapter { | |
220 public void controlResized(ControlEvent e) { | |
221 setExampleWidgetSize (); | |
222 } | |
223 }); | |
224 | |
225 return tabFolderPage; | |
226 } | |
227 | |
228 /** | |
229 * Gets the "Example" widget children. | |
230 */ | |
231 Widget [] getExampleWidgets () { | |
232 return [ cast(Widget) canvas ]; | |
233 } | |
234 | |
235 /** | |
236 * Returns a list of set/get API method names (without the set/get prefix) | |
237 * that can be used to set/get values in the example control(s). | |
238 */ | |
239 char[][] getMethodNames() { | |
240 return ["ToolTipText"]; | |
241 } | |
242 | |
243 /** | |
244 * Gets the text for the tab folder item. | |
245 */ | |
246 char[] getTabText () { | |
247 return "Canvas"; | |
248 } | |
249 | |
250 /** | |
251 * Resizes the maximum and thumb of both scrollbars. | |
252 */ | |
253 void resizeScrollBars () { | |
254 Rectangle clientArea = canvas.getClientArea(); | |
255 ScrollBar bar = canvas.getHorizontalBar(); | |
256 if (bar !is null) { | |
257 bar.setMaximum(maxX); | |
258 bar.setThumb(clientArea.width); | |
259 bar.setPageIncrement(clientArea.width); | |
260 } | |
261 bar = canvas.getVerticalBar(); | |
262 if (bar !is null) { | |
263 bar.setMaximum(maxY); | |
264 bar.setThumb(clientArea.height); | |
265 bar.setPageIncrement(clientArea.height); | |
266 } | |
267 } | |
268 | |
269 /** | |
270 * Scrolls the canvas horizontally. | |
271 * | |
272 * @param scrollBar | |
273 */ | |
274 void scrollHorizontal (ScrollBar scrollBar) { | |
275 Rectangle bounds = canvas.getClientArea(); | |
276 int x = -scrollBar.getSelection(); | |
277 if (x + maxX < bounds.width) { | |
278 x = bounds.width - maxX; | |
279 } | |
280 canvas.scroll(x, cy, cx, cy, maxX, maxY, false); | |
281 cx = x; | |
282 } | |
283 | |
284 /** | |
285 * Scrolls the canvas vertically. | |
286 * | |
287 * @param scrollBar | |
288 */ | |
289 void scrollVertical (ScrollBar scrollBar) { | |
290 Rectangle bounds = canvas.getClientArea(); | |
291 int y = -scrollBar.getSelection(); | |
292 if (y + maxY < bounds.height) { | |
293 y = bounds.height - maxY; | |
294 } | |
295 canvas.scroll(cx, y, cx, cy, maxX, maxY, false); | |
296 cy = y; | |
297 } | |
298 | |
299 /** | |
300 * Sets or clears the caret in the "Example" widget. | |
301 */ | |
302 void setCaret () { | |
303 Caret oldCaret = canvas.getCaret (); | |
304 if (caretButton.getSelection ()) { | |
305 Caret newCaret = new Caret(canvas, DWT.NONE); | |
306 Font font = canvas.getFont(); | |
307 newCaret.setFont(font); | |
308 GC gc = new GC(canvas); | |
309 gc.setFont(font); | |
310 newCaret.setBounds(1, 1, 1, gc.getFontMetrics().getHeight()); | |
311 gc.dispose(); | |
312 canvas.setCaret (newCaret); | |
313 canvas.setFocus(); | |
314 } else { | |
315 canvas.setCaret (null); | |
316 } | |
317 if (oldCaret !is null) oldCaret.dispose (); | |
318 } | |
319 | |
320 /** | |
321 * Sets the state of the "Example" widgets. | |
322 */ | |
323 void setExampleWidgetState () { | |
324 super.setExampleWidgetState (); | |
325 horizontalButton.setSelection ((canvas.getStyle () & DWT.H_SCROLL) !is 0); | |
326 verticalButton.setSelection ((canvas.getStyle () & DWT.V_SCROLL) !is 0); | |
327 borderButton.setSelection ((canvas.getStyle () & DWT.BORDER) !is 0); | |
328 noBackgroundButton.setSelection ((canvas.getStyle () & DWT.NO_BACKGROUND) !is 0); | |
329 noFocusButton.setSelection ((canvas.getStyle () & DWT.NO_FOCUS) !is 0); | |
330 noMergePaintsButton.setSelection ((canvas.getStyle () & DWT.NO_MERGE_PAINTS) !is 0); | |
331 noRedrawResizeButton.setSelection ((canvas.getStyle () & DWT.NO_REDRAW_RESIZE) !is 0); | |
332 doubleBufferedButton.setSelection ((canvas.getStyle () & DWT.DOUBLE_BUFFERED) !is 0); | |
333 if (!instance.startup) setCaret (); | |
334 } | |
335 } |