1 /*******************************************************************************
2 * Copyright (c) 2000, 2013 IBM Corporation and others.
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.swt.widgets;
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
23 * Instances of this class provide a surface for drawing
26 * <dt><b>Styles:</b></dt>
28 * <dt><b>Events:</b></dt>
32 * This class may be subclassed by custom control implementors
33 * who are building controls that are <em>not</em> constructed
34 * from aggregates of other controls. That is, they are either
35 * painted using SWT graphics calls or are handled by native
40 * @see <a href="http://www.eclipse.org/swt/snippets/#canvas">Canvas snippets</a>
41 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
42 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
44 public class Canvas extends Composite {
49 * Prevents uninitialized instances from being created outside the package.
55 * Constructs a new instance of this class given its parent
56 * and a style value describing its behavior and appearance.
58 * The style value is either one of the style constants defined in
59 * class <code>SWT</code> which is applicable to instances of this
60 * class, or must be built by <em>bitwise OR</em>'ing together
61 * (that is, using the <code>int</code> "|" operator) two or more
62 * of those <code>SWT</code> style constants. The class description
63 * lists the style constants that are applicable to the class.
64 * Style bits are also inherited from superclasses.
67 * @param parent a composite control which will be the parent of the new instance (cannot be null)
68 * @param style the style of control to construct
70 * @exception IllegalArgumentException <ul>
71 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
73 * @exception SWTException <ul>
74 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
78 * @see Widget#getStyle
80 public Canvas (Composite parent, int style) {
81 super (parent, style);
84 void clearArea (int x, int y, int width, int height) {
86 if (OS.IsWindowVisible (handle)) {
87 RECT rect = new RECT ();
88 OS.SetRect (rect, x, y, x + width, y + height);
89 long hDC = OS.GetDCEx (handle, 0, OS.DCX_CACHE | OS.DCX_CLIPCHILDREN | OS.DCX_CLIPSIBLINGS);
90 drawBackground (hDC, rect);
91 OS.ReleaseDC (handle, hDC);
96 * Fills the interior of the rectangle specified by the arguments,
97 * with the receiver's background.
99 * @param gc the gc where the rectangle is to be filled
100 * @param x the x coordinate of the rectangle to be filled
101 * @param y the y coordinate of the rectangle to be filled
102 * @param width the width of the rectangle to be filled
103 * @param height the height of the rectangle to be filled
105 * @exception IllegalArgumentException <ul>
106 * <li>ERROR_NULL_ARGUMENT - if the gc is null</li>
107 * <li>ERROR_INVALID_ARGUMENT - if the gc has been disposed</li>
109 * @exception SWTException <ul>
110 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
111 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
116 public void drawBackground (GC gc, int x, int y, int width, int height) {
117 x = DPIUtil.autoScaleUp(x);
118 y = DPIUtil.autoScaleUp(y);
119 width = DPIUtil.autoScaleUp(width);
120 height = DPIUtil.autoScaleUp(height);
121 drawBackgroundInPixels(gc, x, y, width, height, 0, 0);
127 * The caret for the control is automatically hidden
128 * and shown when the control is painted or resized,
129 * when focus is gained or lost and when an the control
130 * is scrolled. To avoid drawing on top of the caret,
131 * the programmer must hide and show the caret when
132 * drawing in the window any other time.
135 * @return the caret for the receiver, may be null
137 * @exception SWTException <ul>
138 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
139 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
142 public Caret getCaret () {
152 * @exception SWTException <ul>
153 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
154 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
159 public IME getIME () {
165 void releaseChildren (boolean destroy) {
167 caret.release (false);
174 super.releaseChildren (destroy);
178 void reskinChildren (int flags) {
179 if (caret != null) caret.reskin (flags);
180 if (ime != null) ime.reskin (flags);
181 super.reskinChildren (flags);
185 * Scrolls a rectangular area of the receiver by first copying
186 * the source area to the destination and then causing the area
187 * of the source which is not covered by the destination to
188 * be repainted. Children that intersect the rectangle are
189 * optionally moved during the operation. In addition, all outstanding
190 * paint events are flushed before the source area is copied to
191 * ensure that the contents of the canvas are drawn correctly.
193 * @param destX the x coordinate of the destination
194 * @param destY the y coordinate of the destination
195 * @param x the x coordinate of the source
196 * @param y the y coordinate of the source
197 * @param width the width of the area
198 * @param height the height of the area
199 * @param all <code>true</code>if children should be scrolled, and <code>false</code> otherwise
201 * @exception SWTException <ul>
202 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
203 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
206 public void scroll (int destX, int destY, int x, int y, int width, int height, boolean all) {
208 destX = DPIUtil.autoScaleUp(destX);
209 destY = DPIUtil.autoScaleUp(destY);
210 x = DPIUtil.autoScaleUp(x);
211 y = DPIUtil.autoScaleUp(y);
212 width = DPIUtil.autoScaleUp(width);
213 height = DPIUtil.autoScaleUp(height);
214 scrollInPixels(destX, destY, x, y, width, height, all);
217 void scrollInPixels (int destX, int destY, int x, int y, int width, int height, boolean all) {
219 boolean isFocus = caret != null && caret.isFocusCaret ();
220 if (isFocus) caret.killFocus ();
221 RECT sourceRect = new RECT ();
222 OS.SetRect (sourceRect, x, y, x + width, y + height);
223 RECT clientRect = new RECT ();
224 OS.GetClientRect (handle, clientRect);
225 if (OS.IntersectRect (clientRect, sourceRect, clientRect)) {
226 int flags = OS.RDW_UPDATENOW | OS.RDW_ALLCHILDREN;
227 OS.RedrawWindow (handle, null, 0, flags);
229 int deltaX = destX - x, deltaY = destY - y;
230 if (findImageControl () != null) {
231 int flags = OS.RDW_ERASE | OS.RDW_FRAME | OS.RDW_INVALIDATE;
232 if (all) flags |= OS.RDW_ALLCHILDREN;
233 OS.RedrawWindow (handle, sourceRect, 0, flags);
234 OS.OffsetRect (sourceRect, deltaX, deltaY);
235 OS.RedrawWindow (handle, sourceRect, 0, flags);
237 int flags = OS.SW_INVALIDATE | OS.SW_ERASE;
239 * Feature in Windows. If any child in the widget tree partially
240 * intersects the scrolling rectangle, Windows moves the child
241 * and copies the bits that intersect the scrolling rectangle but
242 * does not redraw the child.
244 * Feature in Windows. When any child in the widget tree does not
245 * intersect the scrolling rectangle but the parent does intersect,
246 * Windows does not move the child. This is the documented (but
247 * strange) Windows behavior.
249 * The fix is to not use SW_SCROLLCHILDREN and move the children
250 * explicitly after scrolling.
252 // if (all) flags |= OS.SW_SCROLLCHILDREN;
253 OS.ScrollWindowEx (handle, deltaX, deltaY, sourceRect, null, 0, null, flags);
256 Control [] children = _getChildren ();
257 for (int i=0; i<children.length; i++) {
258 Control child = children [i];
259 Rectangle rect = child.getBoundsInPixels ();
260 if (Math.min (x + width, rect.x + rect.width) >= Math.max (x, rect.x) &&
261 Math.min (y + height, rect.y + rect.height) >= Math.max (y, rect.y)) {
262 child.setLocationInPixels (rect.x + deltaX, rect.y + deltaY);
266 if (isFocus) caret.setFocus ();
270 * Sets the receiver's caret.
272 * The caret for the control is automatically hidden
273 * and shown when the control is painted or resized,
274 * when focus is gained or lost and when an the control
275 * is scrolled. To avoid drawing on top of the caret,
276 * the programmer must hide and show the caret when
277 * drawing in the window any other time.
279 * @param caret the new caret for the receiver, may be null
281 * @exception IllegalArgumentException <ul>
282 * <li>ERROR_INVALID_ARGUMENT - if the caret has been disposed</li>
284 * @exception SWTException <ul>
285 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
286 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
289 public void setCaret (Caret caret) {
291 Caret newCaret = caret;
292 Caret oldCaret = this.caret;
293 this.caret = newCaret;
295 if (oldCaret != null) oldCaret.killFocus ();
296 if (newCaret != null) {
297 if (newCaret.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
298 newCaret.setFocus ();
304 public void setFont (Font font) {
306 if (caret != null) caret.setFont (font);
307 super.setFont (font);
311 * Sets the receiver's IME.
313 * @param ime the new IME for the receiver, may be null
315 * @exception IllegalArgumentException <ul>
316 * <li>ERROR_INVALID_ARGUMENT - if the IME has been disposed</li>
318 * @exception SWTException <ul>
319 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
320 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
325 public void setIME (IME ime) {
327 if (ime != null && ime.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
332 TCHAR windowClass () {
333 if (display.useOwnDC) return display.windowOwnDCClass;
334 return super.windowClass ();
338 long windowProc (long hwnd, int msg, long wParam, long lParam) {
339 if (msg == Display.SWT_RESTORECARET) {
340 if ((state & CANVAS) != 0) {
348 return super.windowProc (hwnd, msg, wParam, lParam);
352 LRESULT WM_CHAR (long wParam, long lParam) {
353 LRESULT result = super.WM_CHAR (wParam, lParam);
354 if (result != null) return result;
356 switch ((int)wParam) {
362 if (OS.GetKeyState (OS.VK_CONTROL) >= 0) {
363 int [] value = new int [1];
364 if (OS.SystemParametersInfo (OS.SPI_GETMOUSEVANISH, 0, value, 0)) {
365 if (value [0] != 0) OS.SetCursor (0);
375 LRESULT WM_IME_COMPOSITION (long wParam, long lParam) {
377 LRESULT result = ime.WM_IME_COMPOSITION (wParam, lParam);
378 if (result != null) return result;
380 return super.WM_IME_COMPOSITION (wParam, lParam);
384 LRESULT WM_IME_COMPOSITION_START (long wParam, long lParam) {
386 LRESULT result = ime.WM_IME_COMPOSITION_START (wParam, lParam);
387 if (result != null) return result;
389 return super.WM_IME_COMPOSITION_START (wParam, lParam);
393 LRESULT WM_IME_ENDCOMPOSITION (long wParam, long lParam) {
395 LRESULT result = ime.WM_IME_ENDCOMPOSITION (wParam, lParam);
396 if (result != null) return result;
398 return super.WM_IME_ENDCOMPOSITION (wParam, lParam);
402 LRESULT WM_INPUTLANGCHANGE (long wParam, long lParam) {
403 LRESULT result = super.WM_INPUTLANGCHANGE (wParam, lParam);
404 if (caret != null && caret.isFocusCaret ()) {
413 LRESULT WM_KEYDOWN (long wParam, long lParam) {
414 LRESULT result = super.WM_KEYDOWN (wParam, lParam);
415 if (result != null) return result;
417 ime.WM_KEYDOWN (wParam, lParam);
423 LRESULT WM_KILLFOCUS (long wParam, long lParam) {
425 LRESULT result = ime.WM_KILLFOCUS (wParam, lParam);
426 if (result != null) return result;
428 Caret caret = this.caret;
429 LRESULT result = super.WM_KILLFOCUS (wParam, lParam);
430 if (caret != null) caret.killFocus ();
435 LRESULT WM_LBUTTONDOWN (long wParam, long lParam) {
437 LRESULT result = ime.WM_LBUTTONDOWN (wParam, lParam);
438 if (result != null) return result;
440 return super.WM_LBUTTONDOWN (wParam, lParam);
444 LRESULT WM_SETFOCUS (long wParam, long lParam) {
445 LRESULT result = super.WM_SETFOCUS (wParam, lParam);
446 if (caret != null && caret.isFocusCaret ()) caret.setFocus ();
451 LRESULT WM_SIZE (long wParam, long lParam) {
452 LRESULT result = super.WM_SIZE (wParam, lParam);
453 if (caret != null && caret.isFocusCaret ()) caret.resizeIME ();
458 LRESULT WM_WINDOWPOSCHANGED (long wParam, long lParam) {
459 LRESULT result = super.WM_WINDOWPOSCHANGED (wParam, lParam);
460 //if (result != null) return result;
462 * Bug in Windows. When a window with style WS_EX_LAYOUTRTL
463 * that contains a caret is resized, Windows does not move the
464 * caret in relation to the mirrored origin in the top right.
465 * The fix is to hide the caret in WM_WINDOWPOSCHANGING and
466 * show the caret in WM_WINDOWPOSCHANGED.
468 boolean isFocus = (style & SWT.RIGHT_TO_LEFT) != 0 && caret != null && caret.isFocusCaret ();
469 if (isFocus) caret.setFocus ();
474 LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) {
475 LRESULT result = super.WM_WINDOWPOSCHANGING (wParam, lParam);
476 if (result != null) return result;
478 * Bug in Windows. When a window with style WS_EX_LAYOUTRTL
479 * that contains a caret is resized, Windows does not move the
480 * caret in relation to the mirrored origin in the top right.
481 * The fix is to hide the caret in WM_WINDOWPOSCHANGING and
482 * show the caret in WM_WINDOWPOSCHANGED.
484 boolean isFocus = (style & SWT.RIGHT_TO_LEFT) != 0 && caret != null && caret.isFocusCaret ();
485 if (isFocus) caret.killFocus ();