]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.eclipse.swt.win32.win32.x86_64/src/org/eclipse/swt/widgets/Canvas.java
Remove invalid SHA-256-Digests
[simantics/platform.git] / bundles / org.eclipse.swt.win32.win32.x86_64 / src / org / eclipse / swt / widgets / Canvas.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2013 IBM Corporation and others.
3  *
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/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.swt.widgets;
15
16
17 import org.eclipse.swt.*;
18 import org.eclipse.swt.graphics.*;
19 import org.eclipse.swt.internal.*;
20 import org.eclipse.swt.internal.win32.*;
21
22 /**
23  * Instances of this class provide a surface for drawing
24  * arbitrary graphics.
25  * <dl>
26  * <dt><b>Styles:</b></dt>
27  * <dd>(none)</dd>
28  * <dt><b>Events:</b></dt>
29  * <dd>(none)</dd>
30  * </dl>
31  * <p>
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
36  * methods.
37  * </p>
38  *
39  * @see Composite
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>
43  */
44 public class Canvas extends Composite {
45         Caret caret;
46         IME ime;
47
48 /**
49  * Prevents uninitialized instances from being created outside the package.
50  */
51 Canvas () {
52 }
53
54 /**
55  * Constructs a new instance of this class given its parent
56  * and a style value describing its behavior and appearance.
57  * <p>
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.
65  * </p>
66  *
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
69  *
70  * @exception IllegalArgumentException <ul>
71  *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
72  * </ul>
73  * @exception SWTException <ul>
74  *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
75  * </ul>
76  *
77  * @see SWT
78  * @see Widget#getStyle
79  */
80 public Canvas (Composite parent, int style) {
81         super (parent, style);
82 }
83
84 void clearArea (int x, int y, int width, int height) {
85         checkWidget ();
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);
92         }
93 }
94
95 /**
96  * Fills the interior of the rectangle specified by the arguments,
97  * with the receiver's background.
98  *
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
104  *
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>
108  * </ul>
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>
112  * </ul>
113  *
114  * @since 3.2
115  */
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);
122 }
123
124 /**
125  * Returns the caret.
126  * <p>
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.
133  * </p>
134  *
135  * @return the caret for the receiver, may be null
136  *
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>
140  * </ul>
141  */
142 public Caret getCaret () {
143         checkWidget ();
144         return caret;
145 }
146
147 /**
148  * Returns the IME.
149  *
150  * @return the IME
151  *
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>
155  * </ul>
156  *
157  * @since 3.4
158  */
159 public IME getIME () {
160         checkWidget ();
161         return ime;
162 }
163
164 @Override
165 void releaseChildren (boolean destroy) {
166         if (caret != null) {
167                 caret.release (false);
168                 caret = null;
169         }
170         if (ime != null) {
171                 ime.release (false);
172                 ime = null;
173         }
174         super.releaseChildren (destroy);
175 }
176
177 @Override
178 void reskinChildren (int flags) {
179         if (caret != null) caret.reskin (flags);
180         if (ime != null)  ime.reskin (flags);
181         super.reskinChildren (flags);
182 }
183
184 /**
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.
192  *
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
200  *
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>
204  * </ul>
205  */
206 public void scroll (int destX, int destY, int x, int y, int width, int height, boolean all) {
207         checkWidget ();
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);
215 }
216
217 void scrollInPixels (int destX, int destY, int x, int y, int width, int height, boolean all) {
218         forceResize ();
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);
228         }
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);
236         } else {
237                 int flags = OS.SW_INVALIDATE | OS.SW_ERASE;
238                 /*
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.
243                 *
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.
248                 *
249                 * The fix is to not use SW_SCROLLCHILDREN and move the children
250                 * explicitly after scrolling.
251                 */
252 //              if (all) flags |= OS.SW_SCROLLCHILDREN;
253                 OS.ScrollWindowEx (handle, deltaX, deltaY, sourceRect, null, 0, null, flags);
254         }
255         if (all) {
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);
263                         }
264                 }
265         }
266         if (isFocus) caret.setFocus ();
267 }
268
269 /**
270  * Sets the receiver's caret.
271  * <p>
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.
278  * </p>
279  * @param caret the new caret for the receiver, may be null
280  *
281  * @exception IllegalArgumentException <ul>
282  *    <li>ERROR_INVALID_ARGUMENT - if the caret has been disposed</li>
283  * </ul>
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>
287  * </ul>
288  */
289 public void setCaret (Caret caret) {
290         checkWidget ();
291         Caret newCaret = caret;
292         Caret oldCaret = this.caret;
293         this.caret = newCaret;
294         if (hasFocus ()) {
295                 if (oldCaret != null) oldCaret.killFocus ();
296                 if (newCaret != null) {
297                         if (newCaret.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
298                         newCaret.setFocus ();
299                 }
300         }
301 }
302
303 @Override
304 public void setFont (Font font) {
305         checkWidget ();
306         if (caret != null) caret.setFont (font);
307         super.setFont (font);
308 }
309
310 /**
311  * Sets the receiver's IME.
312  *
313  * @param ime the new IME for the receiver, may be null
314  *
315  * @exception IllegalArgumentException <ul>
316  *    <li>ERROR_INVALID_ARGUMENT - if the IME has been disposed</li>
317  * </ul>
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>
321  * </ul>
322  *
323  * @since 3.4
324  */
325 public void setIME (IME ime) {
326         checkWidget ();
327         if (ime != null && ime.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
328         this.ime = ime;
329 }
330
331 @Override
332 TCHAR windowClass () {
333         if (display.useOwnDC) return display.windowOwnDCClass;
334         return super.windowClass ();
335 }
336
337 @Override
338 long windowProc (long hwnd, int msg, long wParam, long lParam) {
339         if (msg == Display.SWT_RESTORECARET) {
340                 if ((state & CANVAS) != 0) {
341                         if (caret != null) {
342                                 caret.killFocus ();
343                                 caret.setFocus ();
344                                 return 1;
345                         }
346                 }
347         }
348         return super.windowProc (hwnd, msg, wParam, lParam);
349 }
350
351 @Override
352 LRESULT WM_CHAR (long wParam, long lParam) {
353         LRESULT result = super.WM_CHAR (wParam, lParam);
354         if (result != null) return result;
355         if (caret != null) {
356                 switch ((int)wParam) {
357                         case SWT.DEL:
358                         case SWT.BS:
359                         case SWT.ESC:
360                                 break;
361                         default: {
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);
366                                         }
367                                 }
368                         }
369                 }
370         }
371         return result;
372 }
373
374 @Override
375 LRESULT WM_IME_COMPOSITION (long wParam, long lParam) {
376         if (ime != null) {
377                 LRESULT result = ime.WM_IME_COMPOSITION (wParam, lParam);
378                 if (result != null) return result;
379         }
380         return super.WM_IME_COMPOSITION (wParam, lParam);
381 }
382
383 @Override
384 LRESULT WM_IME_COMPOSITION_START (long wParam, long lParam) {
385         if (ime != null) {
386                 LRESULT result = ime.WM_IME_COMPOSITION_START (wParam, lParam);
387                 if (result != null) return result;
388         }
389         return super.WM_IME_COMPOSITION_START (wParam, lParam);
390 }
391
392 @Override
393 LRESULT WM_IME_ENDCOMPOSITION (long wParam, long lParam) {
394         if (ime != null) {
395                 LRESULT result = ime.WM_IME_ENDCOMPOSITION (wParam, lParam);
396                 if (result != null) return result;
397         }
398         return super.WM_IME_ENDCOMPOSITION (wParam, lParam);
399 }
400
401 @Override
402 LRESULT WM_INPUTLANGCHANGE (long wParam, long lParam) {
403         LRESULT result  = super.WM_INPUTLANGCHANGE (wParam, lParam);
404         if (caret != null && caret.isFocusCaret ()) {
405                 caret.setIMEFont ();
406                 caret.resizeIME ();
407         }
408         return result;
409 }
410
411
412 @Override
413 LRESULT WM_KEYDOWN (long wParam, long lParam) {
414         LRESULT result = super.WM_KEYDOWN (wParam, lParam);
415         if (result != null) return result;
416         if (ime != null) {
417                 ime.WM_KEYDOWN (wParam, lParam);
418         }
419         return result;
420 }
421
422 @Override
423 LRESULT WM_KILLFOCUS (long wParam, long lParam) {
424         if (ime != null) {
425                 LRESULT result = ime.WM_KILLFOCUS (wParam, lParam);
426                 if (result != null) return result;
427         }
428         Caret caret = this.caret;
429         LRESULT result  = super.WM_KILLFOCUS (wParam, lParam);
430         if (caret != null) caret.killFocus ();
431         return result;
432 }
433
434 @Override
435 LRESULT WM_LBUTTONDOWN (long wParam, long lParam) {
436         if (ime != null) {
437                 LRESULT result = ime.WM_LBUTTONDOWN (wParam, lParam);
438                 if (result != null) return result;
439         }
440         return super.WM_LBUTTONDOWN (wParam, lParam);
441 }
442
443 @Override
444 LRESULT WM_SETFOCUS (long wParam, long lParam) {
445         LRESULT result  = super.WM_SETFOCUS (wParam, lParam);
446         if (caret != null && caret.isFocusCaret ()) caret.setFocus ();
447         return result;
448 }
449
450 @Override
451 LRESULT WM_SIZE (long wParam, long lParam) {
452         LRESULT result  = super.WM_SIZE (wParam, lParam);
453         if (caret != null && caret.isFocusCaret ()) caret.resizeIME ();
454         return result;
455 }
456
457 @Override
458 LRESULT WM_WINDOWPOSCHANGED (long wParam, long lParam) {
459         LRESULT result  = super.WM_WINDOWPOSCHANGED (wParam, lParam);
460         //if (result != null) return result;
461         /*
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.
467         */
468         boolean isFocus = (style & SWT.RIGHT_TO_LEFT) != 0 && caret != null && caret.isFocusCaret ();
469         if (isFocus) caret.setFocus ();
470         return result;
471 }
472
473 @Override
474 LRESULT WM_WINDOWPOSCHANGING (long wParam, long lParam) {
475         LRESULT result  = super.WM_WINDOWPOSCHANGING (wParam, lParam);
476         if (result != null) return result;
477         /*
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.
483         */
484         boolean isFocus = (style & SWT.RIGHT_TO_LEFT) != 0 && caret != null && caret.isFocusCaret ();
485         if (isFocus) caret.killFocus ();
486         return result;
487 }
488
489 }