KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > Canvas


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  *******************************************************************************/

11 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.graphics.*;
17
18 /**
19  * Instances of this class provide a surface for drawing
20  * arbitrary graphics.
21  * <dl>
22  * <dt><b>Styles:</b></dt>
23  * <dd>(none)</dd>
24  * <dt><b>Events:</b></dt>
25  * <dd>(none)</dd>
26  * </dl>
27  * <p>
28  * This class may be subclassed by custom control implementors
29  * who are building controls that are <em>not</em> constructed
30  * from aggregates of other controls. That is, they are either
31  * painted using SWT graphics calls or are handled by native
32  * methods.
33  * </p>
34  *
35  * @see Composite
36  */

37
38 public class Canvas extends Composite {
39     Caret caret;
40     
41 /**
42  * Prevents uninitialized instances from being created outside the package.
43  */

44 Canvas () {
45 }
46
47 /**
48  * Constructs a new instance of this class given its parent
49  * and a style value describing its behavior and appearance.
50  * <p>
51  * The style value is either one of the style constants defined in
52  * class <code>SWT</code> which is applicable to instances of this
53  * class, or must be built by <em>bitwise OR</em>'ing together
54  * (that is, using the <code>int</code> "|" operator) two or more
55  * of those <code>SWT</code> style constants. The class description
56  * lists the style constants that are applicable to the class.
57  * Style bits are also inherited from superclasses.
58  * </p>
59  *
60  * @param parent a composite control which will be the parent of the new instance (cannot be null)
61  * @param style the style of control to construct
62  *
63  * @exception IllegalArgumentException <ul>
64  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
65  * </ul>
66  * @exception SWTException <ul>
67  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
68  * </ul>
69  *
70  * @see SWT
71  * @see Widget#checkSubclass
72  * @see Widget#getStyle
73  */

74 public Canvas (Composite parent, int style) {
75     super (parent, style);
76 }
77
78 void clearArea (int x, int y, int width, int height) {
79     checkWidget ();
80     if (OS.IsWindowVisible (handle)) {
81         RECT rect = new RECT ();
82         OS.SetRect (rect, x, y, x + width, y + height);
83         int hDC = OS.GetDCEx (handle, 0, OS.DCX_CACHE | OS.DCX_CLIPCHILDREN | OS.DCX_CLIPSIBLINGS);
84         drawBackground (hDC, rect);
85         OS.ReleaseDC (handle, hDC);
86     }
87 }
88
89 /**
90  * Returns the caret.
91  * <p>
92  * The caret for the control is automatically hidden
93  * and shown when the control is painted or resized,
94  * when focus is gained or lost and when an the control
95  * is scrolled. To avoid drawing on top of the caret,
96  * the programmer must hide and show the caret when
97  * drawing in the window any other time.
98  * </p>
99  *
100  * @return the caret
101  *
102  * @exception SWTException <ul>
103  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
104  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
105  * </ul>
106  */

107 public Caret getCaret () {
108     checkWidget ();
109     return caret;
110 }
111
112 void releaseChildren (boolean destroy) {
113     if (caret != null) {
114         caret.release (false);
115         caret = null;
116     }
117     super.releaseChildren (destroy);
118 }
119
120 /**
121  * Fills the interior of the rectangle specified by the arguments,
122  * with the receiver's background.
123  *
124  * @param gc the gc where the rectangle is to be filled
125  * @param x the x coordinate of the rectangle to be filled
126  * @param y the y coordinate of the rectangle to be filled
127  * @param width the width of the rectangle to be filled
128  * @param height the height of the rectangle to be filled
129  *
130  * @exception IllegalArgumentException <ul>
131  * <li>ERROR_NULL_ARGUMENT - if the gc is null</li>
132  * <li>ERROR_INVALID_ARGUMENT - if the gc has been disposed</li>
133  * </ul>
134  * @exception SWTException <ul>
135  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
136  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
137  * </ul>
138  *
139  * @since 3.2
140  */

141 public void drawBackground (GC gc, int x, int y, int width, int height) {
142     checkWidget ();
143     if (gc == null) error (SWT.ERROR_NULL_ARGUMENT);
144     if (gc.isDisposed ()) error (SWT.ERROR_INVALID_ARGUMENT);
145     RECT rect = new RECT ();
146     OS.SetRect (rect, x, y, x + width, y + height);
147     int hDC = gc.handle;
148     int pixel = background == -1 ? gc.getBackground ().handle : -1;
149     drawBackground (hDC, rect, pixel);
150 }
151
152 /**
153  * Scrolls a rectangular area of the receiver by first copying
154  * the source area to the destination and then causing the area
155  * of the source which is not covered by the destination to
156  * be repainted. Children that intersect the rectangle are
157  * optionally moved during the operation. In addition, outstanding
158  * paint events are flushed before the source area is copied to
159  * ensure that the contents of the canvas are drawn correctly.
160  *
161  * @param destX the x coordinate of the destination
162  * @param destY the y coordinate of the destination
163  * @param x the x coordinate of the source
164  * @param y the y coordinate of the source
165  * @param width the width of the area
166  * @param height the height of the area
167  * @param all <code>true</code>if children should be scrolled, and <code>false</code> otherwise
168  *
169  * @exception SWTException <ul>
170  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
171  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
172  * </ul>
173  */

174 public void scroll (int destX, int destY, int x, int y, int width, int height, boolean all) {
175     checkWidget ();
176     forceResize ();
177     boolean isFocus = caret != null && caret.isFocusCaret ();
178     if (isFocus) caret.killFocus ();
179     RECT sourceRect = new RECT ();
180     OS.SetRect (sourceRect, x, y, x + width, y + height);
181     RECT clientRect = new RECT ();
182     OS.GetClientRect (handle, clientRect);
183     if (OS.IntersectRect (clientRect, sourceRect, clientRect)) {
184         if (OS.IsWinCE) {
185             OS.UpdateWindow (handle);
186         } else {
187             int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
188             OS.RedrawWindow (handle, null, 0, flags);
189         }
190     }
191     int deltaX = destX - x, deltaY = destY - y;
192     if (findImageControl () != null) {
193         if (OS.IsWinCE) {
194             OS.InvalidateRect (handle, sourceRect, true);
195         } else {
196             int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
197             if (all) flags |= OS.RDW_ALLCHILDREN;
198             OS.RedrawWindow (handle, sourceRect, 0, flags);
199         }
200         OS.OffsetRect (sourceRect, deltaX, deltaY);
201         if (OS.IsWinCE) {
202             OS.InvalidateRect (handle, sourceRect, true);
203         } else {
204             int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
205             if (all) flags |= OS.RDW_ALLCHILDREN;
206             OS.RedrawWindow (handle, sourceRect, 0, flags);
207         }
208     } else {
209         int flags = OS.SW_INVALIDATE | OS.SW_ERASE;
210         /*
211         * Feature in Windows. If any child in the widget tree partially
212         * intersects the scrolling rectangle, Windows moves the child
213         * and copies the bits that intersect the scrolling rectangle but
214         * does not redraw the child.
215         *
216         * Feature in Windows. When any child in the widget tree does not
217         * intersect the scrolling rectangle but the parent does intersect,
218         * Windows does not move the child. This is the documented (but
219         * strange) Windows behavior.
220         *
221         * The fix is to not use SW_SCROLLCHILDREN and move the children
222         * explicitly after scrolling.
223         */

224 // if (all) flags |= OS.SW_SCROLLCHILDREN;
225
OS.ScrollWindowEx (handle, deltaX, deltaY, sourceRect, null, 0, null, flags);
226     }
227     if (all) {
228         Control [] children = _getChildren ();
229         for (int i=0; i<children.length; i++) {
230             Control child = children [i];
231             Rectangle rect = child.getBounds ();
232             if (Math.min (x + width, rect.x + rect.width) >= Math.max (x, rect.x) &&
233                 Math.min (y + height, rect.y + rect.height) >= Math.max (y, rect.y)) {
234                     child.setLocation (rect.x + deltaX, rect.y + deltaY);
235             }
236         }
237     }
238     if (isFocus) caret.setFocus ();
239 }
240
241 /**
242  * Sets the receiver's caret.
243  * <p>
244  * The caret for the control is automatically hidden
245  * and shown when the control is painted or resized,
246  * when focus is gained or lost and when an the control
247  * is scrolled. To avoid drawing on top of the caret,
248  * the programmer must hide and show the caret when
249  * drawing in the window any other time.
250  * </p>
251  * @param caret the new caret for the receiver, may be null
252  *
253  * @exception IllegalArgumentException <ul>
254  * <li>ERROR_INVALID_ARGUMENT - if the caret has been disposed</li>
255  * </ul>
256  * @exception SWTException <ul>
257  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
258  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
259  * </ul>
260  */

261 public void setCaret (Caret caret) {
262     checkWidget ();
263     Caret newCaret = caret;
264     Caret oldCaret = this.caret;
265     this.caret = newCaret;
266     if (hasFocus ()) {
267         if (oldCaret != null) oldCaret.killFocus ();
268         if (newCaret != null) {
269             if (newCaret.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
270             newCaret.setFocus ();
271         }
272     }
273 }
274
275 public void setFont (Font font) {
276     checkWidget ();
277     if (caret != null) caret.setFont (font);
278     super.setFont (font);
279 }
280
281 int windowProc (int hwnd, int msg, int wParam, int lParam) {
282     if (msg == Display.SWT_RESTORECARET) {
283         if ((state & CANVAS) != 0) {
284             if (caret != null) {
285                 caret.killFocus ();
286                 caret.setFocus ();
287                 return 1;
288             }
289         }
290     }
291     return super.windowProc (hwnd, msg, wParam, lParam);
292 }
293
294 LRESULT WM_IME_COMPOSITION (int wParam, int lParam) {
295     LRESULT result = super.WM_IME_COMPOSITION (wParam, lParam);
296     /*
297     * Bug in Windows. On Korean Windows XP, the IME window
298     * for the Korean Input System (MS-IME 2002) always opens
299     * in the top left corner of the screen, despite the fact
300     * that ImmSetCompositionWindow() was called to position
301     * the IME when focus is gained. The fix is to position
302     * the IME on every WM_IME_COMPOSITION message.
303     */

304     if (!OS.IsWinCE && OS.WIN32_VERSION == OS.VERSION (5, 1)) {
305         if (OS.IsDBLocale) {
306             short langID = OS.GetSystemDefaultUILanguage ();
307             short primaryLang = OS.PRIMARYLANGID (langID);
308             if (primaryLang == OS.LANG_KOREAN) {
309                 if (caret != null && caret.isFocusCaret ()) {
310                     POINT ptCurrentPos = new POINT ();
311                     if (OS.GetCaretPos (ptCurrentPos)) {
312                         COMPOSITIONFORM lpCompForm = new COMPOSITIONFORM ();
313                         lpCompForm.dwStyle = OS.CFS_POINT;
314                         lpCompForm.x = ptCurrentPos.x;
315                         lpCompForm.y = ptCurrentPos.y;
316                         int hIMC = OS.ImmGetContext (handle);
317                         OS.ImmSetCompositionWindow (hIMC, lpCompForm);
318                         OS.ImmReleaseContext (handle, hIMC);
319                     }
320                 }
321             }
322         }
323     }
324     return result;
325 }
326
327 LRESULT WM_INPUTLANGCHANGE (int wParam, int lParam) {
328     LRESULT result = super.WM_INPUTLANGCHANGE (wParam, lParam);
329     if (caret != null && caret.isFocusCaret ()) {
330         caret.setIMEFont ();
331         caret.resizeIME ();
332     }
333     return result;
334 }
335
336 LRESULT WM_KILLFOCUS (int wParam, int lParam) {
337     LRESULT result = super.WM_KILLFOCUS (wParam, lParam);
338     if (caret != null) caret.killFocus ();
339     return result;
340 }
341
342 LRESULT WM_SETFOCUS (int wParam, int lParam) {
343     LRESULT result = super.WM_SETFOCUS (wParam, lParam);
344     if (caret != null) caret.setFocus ();
345     return result;
346 }
347
348 LRESULT WM_SIZE (int wParam, int lParam) {
349     LRESULT result = super.WM_SIZE (wParam, lParam);
350     if (caret != null && caret.isFocusCaret ()) caret.resizeIME ();
351     return result;
352 }
353
354 LRESULT WM_WINDOWPOSCHANGED (int wParam, int lParam) {
355     LRESULT result = super.WM_WINDOWPOSCHANGED (wParam, lParam);
356     if (result != null) return result;
357     /*
358     * Bug in Windows. When a window with style WS_EX_LAYOUTRTL
359     * that contains a caret is resized, Windows does not move the
360     * caret in relation to the mirrored origin in the top right.
361     * The fix is to hide the caret in WM_WINDOWPOSCHANGING and
362     * show the caret in WM_WINDOWPOSCHANGED.
363     */

364     boolean isFocus = (style & SWT.RIGHT_TO_LEFT) != 0 && caret != null && caret.isFocusCaret ();
365     if (isFocus) caret.setFocus ();
366     return result;
367 }
368
369 LRESULT WM_WINDOWPOSCHANGING (int wParam, int lParam) {
370     LRESULT result = super.WM_WINDOWPOSCHANGING (wParam, lParam);
371     if (result != null) return result;
372     /*
373     * Bug in Windows. When a window with style WS_EX_LAYOUTRTL
374     * that contains a caret is resized, Windows does not move the
375     * caret in relation to the mirrored origin in the top right.
376     * The fix is to hide the caret in WM_WINDOWPOSCHANGING and
377     * show the caret in WM_WINDOWPOSCHANGED.
378     */

379     boolean isFocus = (style & SWT.RIGHT_TO_LEFT) != 0 && caret != null && caret.isFocusCaret ();
380     if (isFocus) caret.killFocus ();
381     return result;
382 }
383
384 }
385
Popular Tags