Source code: org/eclipse/swt/widgets/Canvas.java
1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-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.graphics.*;
15 import org.eclipse.swt.internal.gtk.*;
16 import org.eclipse.swt.*;
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 public class Canvas extends Composite {
38 Caret caret;
39
40 Canvas () {}
41
42 /**
43 * Constructs a new instance of this class given its parent
44 * and a style value describing its behavior and appearance.
45 * <p>
46 * The style value is either one of the style constants defined in
47 * class <code>SWT</code> which is applicable to instances of this
48 * class, or must be built by <em>bitwise OR</em>'ing together
49 * (that is, using the <code>int</code> "|" operator) two or more
50 * of those <code>SWT</code> style constants. The class description
51 * lists the style constants that are applicable to the class.
52 * Style bits are also inherited from superclasses.
53 * </p>
54 *
55 * @param parent a composite control which will be the parent of the new instance (cannot be null)
56 * @param style the style of control to construct
57 *
58 * @exception IllegalArgumentException <ul>
59 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
60 * </ul>
61 * @exception SWTException <ul>
62 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
63 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
64 * </ul>
65 *
66 * @see SWT
67 * @see Widget#checkSubclass
68 * @see Widget#getStyle
69 */
70 public Canvas (Composite parent, int style) {
71 super (parent, style);
72 }
73
74 /**
75 * Returns the caret.
76 * <p>
77 * The caret for the control is automatically hidden
78 * and shown when the control is painted or resized,
79 * when focus is gained or lost and when an the control
80 * is scrolled. To avoid drawing on top of the caret,
81 * the programmer must hide and show the caret when
82 * drawing in the window any other time.
83 * </p>
84 *
85 * @return the caret
86 *
87 * @exception SWTException <ul>
88 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
89 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
90 * </ul>
91 */
92 public Caret getCaret () {
93 checkWidget();
94 return caret;
95 }
96
97 Point getIMCaretPos () {
98 if (caret == null) return super.getIMCaretPos ();
99 return new Point (caret.x, caret.y);
100 }
101
102 long /*int*/ gtk_expose_event (long /*int*/ widget, long /*int*/ event) {
103 if ((state & OBSCURED) != 0) return 0;
104 boolean isFocus = caret != null && caret.isFocusCaret ();
105 if (isFocus) caret.killFocus ();
106 long /*int*/ result = super.gtk_expose_event (widget, event);
107 if (isFocus) caret.setFocus ();
108 return result;
109 }
110
111 long /*int*/ gtk_focus_in_event (long /*int*/ widget, long /*int*/ event) {
112 long /*int*/ result = super.gtk_focus_in_event (widget, event);
113 if (caret != null) caret.setFocus ();
114 return result;
115 }
116
117 long /*int*/ gtk_focus_out_event (long /*int*/ widget, long /*int*/ event) {
118 long /*int*/ result = super.gtk_focus_out_event (widget, event);
119 if (caret != null) caret.killFocus ();
120 return result;
121 }
122
123 void redrawWidget (int x, int y, int width, int height, boolean all) {
124 boolean isFocus = caret != null && caret.isFocusCaret ();
125 if (isFocus) caret.killFocus ();
126 super.redrawWidget (x, y, width, height, all);
127 if (isFocus) caret.setFocus ();
128 }
129
130 void releaseWidget () {
131 if (caret != null) caret.releaseResources ();
132 caret = null;
133 super.releaseWidget();
134 }
135
136 /**
137 * Scrolls a rectangular area of the receiver by first copying
138 * the source area to the destination and then causing the area
139 * of the source which is not covered by the destination to
140 * be repainted. Children that intersect the rectangle are
141 * optionally moved during the operation. In addition, outstanding
142 * paint events are flushed before the source area is copied to
143 * ensure that the contents of the canvas are drawn correctly.
144 *
145 * @param destX the x coordinate of the destination
146 * @param destY the y coordinate of the destination
147 * @param x the x coordinate of the source
148 * @param y the y coordinate of the source
149 * @param width the width of the area
150 * @param height the height of the area
151 * @param all <code>true</code>if children should be scrolled, and <code>false</code> otherwise
152 *
153 * @exception SWTException <ul>
154 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
155 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
156 * </ul>
157 */
158 public void scroll (int destX, int destY, int x, int y, int width, int height, boolean all) {
159 checkWidget();
160 if (width <= 0 || height <= 0) return;
161 int deltaX = destX - x, deltaY = destY - y;
162 if (deltaX == 0 && deltaY == 0) return;
163 if (!isVisible ()) return;
164 boolean isFocus = caret != null && caret.isFocusCaret ();
165 if (isFocus) caret.killFocus ();
166 long /*int*/ window = paintWindow ();
167 long /*int*/ visibleRegion = OS.gdk_drawable_get_visible_region (window);
168 GdkRectangle srcRect = new GdkRectangle ();
169 srcRect.x = x;
170 srcRect.y = y;
171 srcRect.width = width;
172 srcRect.height = height;
173 long /*int*/ copyRegion = OS.gdk_region_rectangle (srcRect);
174 OS.gdk_region_intersect(copyRegion, visibleRegion);
175 long /*int*/ invalidateRegion = OS.gdk_region_rectangle (srcRect);
176 OS.gdk_region_subtract (invalidateRegion, visibleRegion);
177 OS.gdk_region_offset (invalidateRegion, deltaX, deltaY);
178 GdkRectangle copyRect = new GdkRectangle();
179 OS.gdk_region_get_clipbox (copyRegion, copyRect);
180 if (copyRect.width != 0 && copyRect.height != 0) {
181 update ();
182 }
183 // GC gc = new GC (this);
184 // gc.copyArea (x, y, width, height, destX, destY);
185 // gc.dispose ();
186 long /*int*/ gdkGC = OS.gdk_gc_new (window);
187 OS.gdk_gc_set_exposures (gdkGC, true);
188 OS.gdk_draw_drawable (window, gdkGC, window, copyRect.x, copyRect.y, copyRect.x + deltaX, copyRect.y + deltaY, copyRect.width, copyRect.height);
189 OS.g_object_unref (gdkGC);
190 boolean disjoint = (destX + width < x) || (x + width < destX) || (destY + height < y) || (y + height < destY);
191 if (disjoint) {
192 GdkRectangle rect = new GdkRectangle ();
193 rect.x = x;
194 rect.y = y;
195 rect.width = width;
196 rect.height = height;
197 OS.gdk_region_union_with_rect (invalidateRegion, rect);
198 } else {
199 GdkRectangle rect = new GdkRectangle ();
200 if (deltaX != 0) {
201 int newX = destX - deltaX;
202 if (deltaX < 0) newX = destX + width;
203 rect.x = newX;
204 rect.y = y;
205 rect.width = Math.abs(deltaX);
206 rect.height = height;
207 OS.gdk_region_union_with_rect (invalidateRegion, rect);
208 }
209 if (deltaY != 0) {
210 int newY = destY - deltaY;
211 if (deltaY < 0) newY = destY + height;
212 rect.x = x;
213 rect.y = newY;
214 rect.width = width;
215 rect.height = Math.abs(deltaY);
216 OS.gdk_region_union_with_rect (invalidateRegion, rect);
217 }
218 }
219 OS.gdk_window_invalidate_region(window, invalidateRegion, all);
220 OS.gdk_region_destroy (visibleRegion);
221 OS.gdk_region_destroy (copyRegion);
222 OS.gdk_region_destroy (invalidateRegion);
223 if (all) {
224 Control [] children = _getChildren ();
225 for (int i=0; i<children.length; i++) {
226 Control child = children [i];
227 Rectangle rect = child.getBounds ();
228 if (Math.min(x + width, rect.x + rect.width) >= Math.max (x, rect.x) &&
229 Math.min(y + height, rect.y + rect.height) >= Math.max (y, rect.y)) {
230 child.setLocation (rect.x + deltaX, rect.y + deltaY);
231 }
232 }
233 }
234 if (isFocus) caret.setFocus ();
235 }
236
237 boolean setBounds (int x, int y, int width, int height, boolean move, boolean resize) {
238 boolean isFocus = caret != null && caret.isFocusCaret ();
239 if (isFocus) caret.killFocus ();
240 boolean changed = super.setBounds (x, y, width, height, move, resize);
241 if (isFocus) caret.setFocus ();
242 return changed;
243 }
244
245 /**
246 * Sets the receiver's caret.
247 * <p>
248 * The caret for the control is automatically hidden
249 * and shown when the control is painted or resized,
250 * when focus is gained or lost and when an the control
251 * is scrolled. To avoid drawing on top of the caret,
252 * the programmer must hide and show the caret when
253 * drawing in the window any other time.
254 * </p>
255 * @param caret the new caret for the receiver, may be null
256 *
257 * @exception IllegalArgumentException <ul>
258 * <li>ERROR_INVALID_ARGUMENT - if the caret has been disposed</li>
259 * </ul>
260 * @exception SWTException <ul>
261 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
262 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
263 * </ul>
264 */
265 public void setCaret (Caret caret) {
266 checkWidget();
267 Caret newCaret = caret;
268 Caret oldCaret = this.caret;
269 this.caret = newCaret;
270 if (hasFocus ()) {
271 if (oldCaret != null) oldCaret.killFocus ();
272 if (newCaret != null) {
273 if (newCaret.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT);
274 newCaret.setFocus ();
275 }
276 }
277 }
278
279 public void setFont (Font font) {
280 checkWidget();
281 if (caret != null) caret.setFont (font);
282 super.setFont (font);
283 }
284
285 void updateCaret () {
286 long /*int*/ imHandle = imHandle ();
287 if (imHandle == 0) return;
288 GdkRectangle rect = new GdkRectangle ();
289 rect.x = caret.x;
290 rect.y = caret.y;
291 rect.width = caret.width;
292 rect.height = caret.height;
293 OS.gtk_im_context_set_cursor_location (imHandle, rect);
294 }
295
296 }