root/dwt/widgets/Canvas.d

Revision 246:fd9c62a2998e, 17.1 kB (checked in by Frank Benoit <benoit@tionex.de>, 5 months ago)

Updater SWT 3.4M7 to 3.4

Line 
1 /*******************************************************************************
2  * Copyright (c) 2000, 2008 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  *******************************************************************************/
13 module dwt.widgets.Canvas;
14
15 import dwt.widgets.Composite;
16
17
18 import dwt.DWT;
19 import dwt.DWTException;
20 import dwt.graphics.Font;
21 import dwt.graphics.GC;
22 import dwt.graphics.Rectangle;
23 import dwt.internal.win32.OS;
24 import dwt.widgets.Caret;
25 import dwt.widgets.Control;
26 import dwt.widgets.Display;
27 import dwt.widgets.IME;
28
29 import dwt.dwthelper.utils;
30
31 /**
32  * Instances of this class provide a surface for drawing
33  * arbitrary graphics.
34  * <dl>
35  * <dt><b>Styles:</b></dt>
36  * <dd>(none)</dd>
37  * <dt><b>Events:</b></dt>
38  * <dd>(none)</dd>
39  * </dl>
40  * <p>
41  * This class may be subclassed by custom control implementors
42  * who are building controls that are <em>not</em> constructed
43  * from aggregates of other controls. That is, they are either
44  * painted using DWT graphics calls or are handled by native
45  * methods.
46  * </p>
47  *
48  * @see Composite
49  * @see <a href="http://www.eclipse.org/swt/snippets/#canvas">Canvas snippets</a>
50  * @see <a href="http://www.eclipse.org/swt/examples.php">DWT Example: ControlExample</a>
51  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
52  */
53
54 public class Canvas : Composite {
55
56     alias Composite.drawBackground drawBackground;
57     alias Composite.windowProc windowProc;
58
59     Caret caret;
60     IME ime;
61
62 /**
63  * Prevents uninitialized instances from being created outside the package.
64  */
65 this () {
66 }
67
68 /**
69  * Constructs a new instance of this class given its parent
70  * and a style value describing its behavior and appearance.
71  * <p>
72  * The style value is either one of the style constants defined in
73  * class <code>DWT</code> which is applicable to instances of this
74  * class, or must be built by <em>bitwise OR</em>'ing together
75  * (that is, using the <code>int</code> "|" operator) two or more
76  * of those <code>DWT</code> style constants. The class description
77  * lists the style constants that are applicable to the class.
78  * Style bits are also inherited from superclasses.
79  * </p>
80  *
81  * @param parent a composite control which will be the parent of the new instance (cannot be null)
82  * @param style the style of control to construct
83  *
84  * @exception IllegalArgumentException <ul>
85  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
86  * </ul>
87  * @exception DWTException <ul>
88  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
89  * </ul>
90  *
91  * @see DWT
92  * @see Widget#checkSubclass
93  * @see Widget#getStyle
94  */
95 public this (Composite parent, int style) {
96     super (parent, style);
97 }
98
99 void clearArea (int x, int y, int width, int height) {
100     checkWidget ();
101     if (OS.IsWindowVisible (handle)) {
102         RECT rect;
103         OS.SetRect (&rect, x, y, x + width, y + height);
104         auto hDC = OS.GetDCEx (handle, null, OS.DCX_CACHE | OS.DCX_CLIPCHILDREN | OS.DCX_CLIPSIBLINGS);
105         drawBackground (hDC, &rect);
106         OS.ReleaseDC (handle, hDC);
107     }
108 }
109
110 /**
111  * Fills the interior of the rectangle specified by the arguments,
112  * with the receiver's background.
113  *
114  * @param gc the gc where the rectangle is to be filled
115  * @param x the x coordinate of the rectangle to be filled
116  * @param y the y coordinate of the rectangle to be filled
117  * @param width the width of the rectangle to be filled
118  * @param height the height of the rectangle to be filled
119  *
120  * @exception IllegalArgumentException <ul>
121  *    <li>ERROR_NULL_ARGUMENT - if the gc is null</li>
122  *    <li>ERROR_INVALID_ARGUMENT - if the gc has been disposed</li>
123  * </ul>
124  * @exception DWTException <ul>
125  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
126  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
127  * </ul>
128  *
129  * @since 3.2
130  */
131 public void drawBackground (GC gc, int x, int y, int width, int height) {
132     checkWidget ();
133     if (gc is null) error (DWT.ERROR_NULL_ARGUMENT);
134     if (gc.isDisposed ()) error (DWT.ERROR_INVALID_ARGUMENT);
135     RECT rect;
136     OS.SetRect (&rect, x, y, x + width, y + height);
137     auto hDC = gc.handle;
138     int pixel = background is -1 ? gc.getBackground ().handle : -1;
139     drawBackground (hDC, &rect, pixel);
140 }
141
142 /**
143  * Returns the caret.
144  * <p>
145  * The caret for the control is automatically hidden
146  * and shown when the control is painted or resized,
147  * when focus is gained or lost and when an the control
148  * is scrolled.  To avoid drawing on top of the caret,
149  * the programmer must hide and show the caret when
150  * drawing in the window any other time.
151  * </p>
152  *
153  * @return the caret for the receiver, may be null
154  *
155  * @exception DWTException <ul>
156  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
157  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
158  * </ul>
159  */
160 public Caret getCaret () {
161     checkWidget ();
162     return caret;
163 }
164
165 /**
166  * Returns the IME.
167  *
168  * @return the IME
169  *
170  * @exception DWTException <ul>
171  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
172  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
173  * </ul>
174  *
175  * @since 3.4
176  */
177 public IME getIME () {
178     checkWidget ();
179     return ime;
180 }
181
182 void releaseChildren (bool destroy) {
183     if (caret !is null) {
184         caret.release (false);
185         caret = null;
186     }
187     if (ime !is null) {
188         ime.release (false);
189         ime = null;
190     }
191     super.releaseChildren (destroy);
192 }
193
194 /**
195  * Scrolls a rectangular area of the receiver by first copying
196  * the source area to the destination and then causing the area
197  * of the source which is not covered by the destination to
198  * be repainted. Children that intersect the rectangle are
199  * optionally moved during the operation. In addition, outstanding
200  * paint events are flushed before the source area is copied to
201  * ensure that the contents of the canvas are drawn correctly.
202  *
203  * @param destX the x coordinate of the destination
204  * @param destY the y coordinate of the destination
205  * @param x the x coordinate of the source
206  * @param y the y coordinate of the source
207  * @param width the width of the area
208  * @param height the height of the area
209  * @param all <code>true</code>if children should be scrolled, and <code>false</code> otherwise
210  *
211  * @exception DWTException <ul>
212  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
213  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
214  * </ul>
215  */
216 public void scroll (int destX, int destY, int x, int y, int width, int height, bool all) {
217     checkWidget ();
218     forceResize ();
219     bool isFocus = caret !is null && caret.isFocusCaret ();
220     if (isFocus) caret.killFocus ();
221     RECT sourceRect;
222     OS.SetRect (&sourceRect, x, y, x + width, y + height);
223     RECT clientRect;
224     OS.GetClientRect (handle, &clientRect);
225     if (OS.IntersectRect (&clientRect, &sourceRect, &clientRect)) {
226         static if (OS.IsWinCE) {
227             OS.UpdateWindow (handle);
228         } else {
229             int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
230             OS.RedrawWindow (handle, null, null, flags);
231         }
232     }
233     int deltaX = destX - x, deltaY = destY - y;
234     if (findImageControl () !is null) {
235         static if (OS.IsWinCE) {
236             OS.InvalidateRect (handle, &sourceRect, true);
237         } else {
238             { // scope for flags
239             int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
240             if (all) flags |= OS.RDW_ALLCHILDREN;
241             OS.RedrawWindow (handle, &sourceRect, null, flags);
242             }
243         }
244         OS.OffsetRect (&sourceRect, deltaX, deltaY);
245         static if (OS.IsWinCE) {
246             OS.InvalidateRect (handle, &sourceRect, true);
247         } else {
248             int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
249             if (all) flags |= OS.RDW_ALLCHILDREN;
250             OS.RedrawWindow (handle, &sourceRect, null, flags);
251         }
252     } else {
253         int flags = OS.SW_INVALIDATE | OS.SW_ERASE;
254         /*
255         * Feature in Windows.  If any child in the widget tree partially
256         * intersects the scrolling rectangle, Windows moves the child
257         * and copies the bits that intersect the scrolling rectangle but
258         * does not redraw the child.
259         *
260         * Feature in Windows.  When any child in the widget tree does not
261         * intersect the scrolling rectangle but the parent does intersect,
262         * Windows does not move the child.  This is the documented (but
263         * strange) Windows behavior.
264         *
265         * The fix is to not use SW_SCROLLCHILDREN and move the children
266         * explicitly after scrolling.
267         */
268 //      if (all) flags |= OS.SW_SCROLLCHILDREN;
269         OS.ScrollWindowEx (handle, deltaX, deltaY, &sourceRect, null, null, null, flags);
270     }
271     if (all) {
272         Control [] children = _getChildren ();
273         for (int i=0; i<children.length; i++) {
274             Control child = children [i];
275             Rectangle rect = child.getBounds ();
276             if (Math.min (x + width, rect.x + rect.width) >= Math.max (x, rect.x) &&
277                 Math.min (y + height, rect.y + rect.height) >= Math.max (y, rect.y)) {
278                     child.setLocation (rect.x + deltaX, rect.y + deltaY);
279             }
280         }
281     }
282     if (isFocus) caret.setFocus ();
283 }
284
285 /**
286  * Sets the receiver's caret.
287  * <p>
288  * The caret for the control is automatically hidden
289  * and shown when the control is painted or resized,
290  * when focus is gained or lost and when an the control
291  * is scrolled.  To avoid drawing on top of the caret,
292  * the programmer must hide and show the caret when
293  * drawing in the window any other time.
294  * </p>
295  * @param caret the new caret for the receiver, may be null
296  *
297  * @exception IllegalArgumentException <ul>
298  *    <li>ERROR_INVALID_ARGUMENT - if the caret has been disposed</li>
299  * </ul>
300  * @exception DWTException <ul>
301  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
302  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
303  * </ul>
304  */
305 public void setCaret (Caret caret) {
306     checkWidget ();
307     Caret newCaret = caret;
308     Caret oldCaret = this.caret;
309     this.caret = newCaret;
310     if (hasFocus ()) {
311         if (oldCaret !is null) oldCaret.killFocus ();
312         if (newCaret !is null) {
313             if (newCaret.isDisposed()) error(DWT.ERROR_INVALID_ARGUMENT);
314             newCaret.setFocus ();
315         }
316     }
317 }
318
319 override public void setFont (Font font) {
320     checkWidget ();
321     if (caret !is null) caret.setFont (font);
322     super.setFont (font);
323 }
324
325 /**
326  * Sets the receiver's IME.
327  *
328  * @param ime the new IME for the receiver, may be null
329  *
330  * @exception IllegalArgumentException <ul>
331  *    <li>ERROR_INVALID_ARGUMENT - if the IME has been disposed</li>
332  * </ul>
333  * @exception DWTException <ul>
334  *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
335  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
336  * </ul>
337  *
338  * @since 3.4
339  */
340 public void setIME (IME ime) {
341     checkWidget ();
342     if (ime !is null && ime.isDisposed()) error(DWT.ERROR_INVALID_ARGUMENT);
343     this.ime = ime;
344 }
345
346 override int windowProc (HWND hwnd, int msg, int wParam, int lParam) {
347     if (msg is Display.SWT_RESTORECARET) {
348         if ((state & CANVAS) !is 0) {
349             if (caret !is null) {
350                 caret.killFocus ();
351                 caret.setFocus ();
352                 return 1;
353             }
354         }
355     }
356     return super.windowProc (hwnd, msg, wParam, lParam);
357 }
358
359 override LRESULT WM_CHAR (int wParam, int lParam) {
360     LRESULT result = super.WM_CHAR (wParam, lParam);
361     if (result !is null) return result;
362     if (caret !is null) {
363         switch (wParam) {
364             case DWT.DEL:
365             case DWT.BS:
366             case DWT.ESC:
367                 break;
368             default: {
369                 if (OS.GetKeyState (OS.VK_CONTROL) >= 0) {
370                     int value;
371                     if (OS.SystemParametersInfo (OS.SPI_GETMOUSEVANISH, 0, &value, 0)) {
372                         if (value !is 0) OS.SetCursor (null);
373                     }
374                 }
375             }
376         }
377     }
378     return result;
379 }
380
381 override LRESULT WM_IME_COMPOSITION (int /*long*/ wParam, int /*long*/ lParam) {
382     if (ime !is null) {
383         LRESULT result = ime.WM_IME_COMPOSITION (wParam, lParam);
384         if (result !is null) return result;
385     }
386
387     /*
388     * Bug in Windows.  On Korean Windows XP, the IME window
389     * for the Korean Input System (MS-IME 2002) always opens
390     * in the top left corner of the screen, despite the fact
391     * that ImmSetCompositionWindow() was called to position
392     * the IME when focus is gained.  The fix is to position
393     * the IME on every WM_IME_COMPOSITION message.
394     */
395     if (!OS.IsWinCE && OS.WIN32_VERSION is OS.VERSION (5, 1)) {
396         if (OS.IsDBLocale) {
397             short langID = OS.GetSystemDefaultUILanguage ();
398             short primaryLang = OS.PRIMARYLANGID (langID);
399             if (primaryLang is OS.LANG_KOREAN) {
400                 if (caret !is null && caret.isFocusCaret ()) {
401                     POINT ptCurrentPos;
402                     if (OS.GetCaretPos (&ptCurrentPos)) {
403                         COMPOSITIONFORM lpCompForm;
404                         lpCompForm.dwStyle = OS.CFS_POINT;
405                         lpCompForm.ptCurrentPos.x = ptCurrentPos.x;
406                         lpCompForm.ptCurrentPos.y = ptCurrentPos.y;
407                         auto hIMC = OS.ImmGetContext (handle);
408                         OS.ImmSetCompositionWindow (hIMC, &lpCompForm);
409                         OS.ImmReleaseContext (handle, hIMC);
410                     }
411                 }
412             }
413         }
414     }
415     return super.WM_IME_COMPOSITION (wParam, lParam);
416 }
417
418 override LRESULT WM_IME_COMPOSITION_START (int /*long*/ wParam, int /*long*/ lParam) {
419     if (ime !is null) {
420         LRESULT result = ime.WM_IME_COMPOSITION_START (wParam, lParam);
421         if (result !is null) return result;
422     }
423     return super.WM_IME_COMPOSITION_START (wParam, lParam);
424 }
425
426 override LRESULT WM_IME_ENDCOMPOSITION (int /*long*/ wParam, int /*long*/ lParam) {
427     if (ime !is null) {
428         LRESULT result = ime.WM_IME_ENDCOMPOSITION (wParam, lParam);
429         if (result !is null) return result;
430     }
431     return super.WM_IME_ENDCOMPOSITION (wParam, lParam);
432 }
433
434 override LRESULT WM_INPUTLANGCHANGE (int wParam, int lParam) {
435     LRESULT result  = super.WM_INPUTLANGCHANGE (wParam, lParam);
436     if (caret !is null && caret.isFocusCaret ()) {
437         caret.setIMEFont ();
438         caret.resizeIME ();
439     }
440     return result;
441 }
442
443 override LRESULT WM_KILLFOCUS (int wParam, int lParam) {
444     if (ime !is null) {
445         LRESULT result = ime.WM_KILLFOCUS (wParam, lParam);
446         if (result !is null) return result;
447     }
448     LRESULT result  = super.WM_KILLFOCUS (wParam, lParam);
449     if (caret !is null) caret.killFocus ();
450     return result;
451 }
452
453 override LRESULT WM_LBUTTONDOWN (int /*long*/ wParam, int /*long*/ lParam) {
454     if (ime !is null) {
455         LRESULT result = ime.WM_LBUTTONDOWN (wParam, lParam);
456         if (result !is null) return result;
457     }
458     return super.WM_LBUTTONDOWN (wParam, lParam);
459 }
460
461 override LRESULT WM_SETFOCUS (int wParam, int lParam) {
462     LRESULT result  = super.WM_SETFOCUS (wParam, lParam);
463     if (caret !is null) caret.setFocus ();
464     return result;
465 }
466
467 override LRESULT WM_SIZE (int wParam, int lParam) {
468     LRESULT result  = super.WM_SIZE (wParam, lParam);
469     if (caret !is null && caret.isFocusCaret ()) caret.resizeIME ();
470     return result;
471 }
472
473 override LRESULT WM_WINDOWPOSCHANGED (int wParam, int lParam) {
474     LRESULT result  = super.WM_WINDOWPOSCHANGED (wParam, lParam);
475     //if (result !is null) return result;
476     /*
477     * Bug in Windows.  When a window with style WS_EX_LAYOUTRTL
478     * that contains a caret is resized, Windows does not move the
479     * caret in relation to the mirrored origin in the top right.
480     * The fix is to hide the caret in WM_WINDOWPOSCHANGING and
481     * show the caret in WM_WINDOWPOSCHANGED.
482     */
483     bool isFocus = (style & DWT.RIGHT_TO_LEFT) !is 0 && caret !is null && caret.isFocusCaret ();
484     if (isFocus) caret.setFocus ();
485     return result;
486 }
487
488 override LRESULT WM_WINDOWPOSCHANGING (int wParam, int lParam) {
489     LRESULT result  = super.WM_WINDOWPOSCHANGING (wParam, lParam);
490     if (result !is null) return result;
491     /*
492     * Bug in Windows.  When a window with style WS_EX_LAYOUTRTL
493     * that contains a caret is resized, Windows does not move the
494     * caret in relation to the mirrored origin in the top right.
495     * The fix is to hide the caret in WM_WINDOWPOSCHANGING and
496     * show the caret in WM_WINDOWPOSCHANGED.
497     */
498     bool isFocus = (style & DWT.RIGHT_TO_LEFT) !is 0 && caret !is null && caret.isFocusCaret ();
499     if (isFocus) caret.killFocus ();
500     return result;
501 }
502
503 }
Note: See TracBrowser for help on using the browser.