Mercurial > projects > dwt-mac
comparison dwt/widgets/Caret.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 649b8e223d5a 2952d5604c0a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
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 module dwt.widgets.Caret; | |
12 | |
13 import dwt.dwthelper.utils; | |
14 | |
15 | |
16 import dwt.DWT; | |
17 import dwt.DWTException; | |
18 import dwt.graphics.Font; | |
19 import dwt.graphics.Image; | |
20 import dwt.graphics.Point; | |
21 import dwt.graphics.Rectangle; | |
22 import dwt.internal.cocoa.NSRect; | |
23 import dwt.internal.cocoa.NSSize; | |
24 | |
25 /** | |
26 * Instances of this class provide an i-beam that is typically used | |
27 * as the insertion point for text. | |
28 * <dl> | |
29 * <dt><b>Styles:</b></dt> | |
30 * <dd>(none)</dd> | |
31 * <dt><b>Events:</b></dt> | |
32 * <dd>(none)</dd> | |
33 * </dl> | |
34 * <p> | |
35 * IMPORTANT: This class is intended to be subclassed <em>only</em> | |
36 * within the DWT implementation. | |
37 * </p> | |
38 */ | |
39 public class Caret extends Widget { | |
40 Canvas parent; | |
41 int x, y, width, height; | |
42 bool isVisible, isShowing; | |
43 int blinkRate; | |
44 Image image; | |
45 Font font; | |
46 | |
47 static final int DEFAULT_WIDTH = 1; | |
48 | |
49 /** | |
50 * Constructs a new instance of this class given its parent | |
51 * and a style value describing its behavior and appearance. | |
52 * <p> | |
53 * The style value is either one of the style constants defined in | |
54 * class <code>DWT</code> which is applicable to instances of this | |
55 * class, or must be built by <em>bitwise OR</em>'ing together | |
56 * (that is, using the <code>int</code> "|" operator) two or more | |
57 * of those <code>DWT</code> style constants. The class description | |
58 * lists the style constants that are applicable to the class. | |
59 * Style bits are also inherited from superclasses. | |
60 * </p> | |
61 * | |
62 * @param parent a composite control which will be the parent of the new instance (cannot be null) | |
63 * @param style the style of control to construct | |
64 * | |
65 * @exception IllegalArgumentException <ul> | |
66 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li> | |
67 * </ul> | |
68 * @exception DWTException <ul> | |
69 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li> | |
70 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li> | |
71 * </ul> | |
72 * | |
73 * @see DWT | |
74 * @see Widget#checkSubclass | |
75 * @see Widget#getStyle | |
76 */ | |
77 public Caret (Canvas parent, int style) { | |
78 super (parent, style); | |
79 this.parent = parent; | |
80 createWidget (); | |
81 } | |
82 | |
83 bool blinkCaret () { | |
84 if (!isVisible) return true; | |
85 if (!isShowing) return showCaret (); | |
86 if (blinkRate is 0) return true; | |
87 return hideCaret (); | |
88 } | |
89 | |
90 void createWidget () { | |
91 super.createWidget (); | |
92 blinkRate = display.getCaretBlinkTime (); | |
93 isVisible = true; | |
94 if (parent.getCaret () is null) { | |
95 parent.setCaret (this); | |
96 } | |
97 } | |
98 | |
99 bool drawCaret () { | |
100 if (parent is null) return false; | |
101 if (parent.isDisposed ()) return false; | |
102 int nWidth = width, nHeight = height; | |
103 if (nWidth <= 0) nWidth = DEFAULT_WIDTH; | |
104 if (image !is null) { | |
105 NSSize size = image.handle.size(); | |
106 nWidth = (int)size.width; | |
107 nHeight = (int)size.height; | |
108 } | |
109 NSRect rect = new NSRect(); | |
110 rect.x = x; | |
111 rect.y = y; | |
112 rect.width = nWidth; | |
113 rect.height = nHeight; | |
114 parent.view.setNeedsDisplayInRect(rect); | |
115 return true; | |
116 } | |
117 | |
118 /** | |
119 * Returns a rectangle describing the receiver's size and location | |
120 * relative to its parent (or its display if its parent is null). | |
121 * | |
122 * @return the receiver's bounding rectangle | |
123 * | |
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 public Rectangle getBounds () { | |
130 checkWidget(); | |
131 if (image !is null) { | |
132 Rectangle rect = image.getBounds (); | |
133 return new Rectangle (x, y, rect.width, rect.height); | |
134 } else { | |
135 if (width is 0) { | |
136 return new Rectangle (x, y, DEFAULT_WIDTH, height); | |
137 } | |
138 } | |
139 return new Rectangle (x, y, width, height); | |
140 } | |
141 | |
142 /** | |
143 * Returns the font that the receiver will use to paint textual information. | |
144 * | |
145 * @return the receiver's font | |
146 * | |
147 * @exception DWTException <ul> | |
148 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
149 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
150 * </ul> | |
151 */ | |
152 public Font getFont () { | |
153 checkWidget(); | |
154 if (font !is null) return font; | |
155 return parent.getFont (); | |
156 } | |
157 | |
158 /** | |
159 * Returns the image that the receiver will use to paint the caret. | |
160 * | |
161 * @return the receiver's image | |
162 * | |
163 * @exception DWTException <ul> | |
164 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
165 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
166 * </ul> | |
167 */ | |
168 public Image getImage () { | |
169 checkWidget(); | |
170 return image; | |
171 } | |
172 | |
173 /** | |
174 * Returns a point describing the receiver's location relative | |
175 * to its parent (or its display if its parent is null). | |
176 * | |
177 * @return the receiver's location | |
178 * | |
179 * @exception DWTException <ul> | |
180 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
181 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
182 * </ul> | |
183 */ | |
184 public Point getLocation () { | |
185 checkWidget(); | |
186 return new Point (x, y); | |
187 } | |
188 | |
189 /** | |
190 * Returns the receiver's parent, which must be a <code>Canvas</code>. | |
191 * | |
192 * @return the receiver's parent | |
193 * | |
194 * @exception DWTException <ul> | |
195 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
196 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
197 * </ul> | |
198 */ | |
199 public Canvas getParent () { | |
200 checkWidget(); | |
201 return parent; | |
202 } | |
203 | |
204 /** | |
205 * Returns a point describing the receiver's size. | |
206 * | |
207 * @return the receiver's size | |
208 * | |
209 * @exception DWTException <ul> | |
210 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
211 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
212 * </ul> | |
213 */ | |
214 public Point getSize () { | |
215 checkWidget(); | |
216 if (image !is null) { | |
217 Rectangle rect = image.getBounds (); | |
218 return new Point (rect.width, rect.height); | |
219 } else { | |
220 if (width is 0) { | |
221 return new Point (DEFAULT_WIDTH, height); | |
222 } | |
223 } | |
224 return new Point (width, height); | |
225 } | |
226 | |
227 /** | |
228 * Returns <code>true</code> if the receiver is visible, and | |
229 * <code>false</code> otherwise. | |
230 * <p> | |
231 * If one of the receiver's ancestors is not visible or some | |
232 * other condition makes the receiver not visible, this method | |
233 * may still indicate that it is considered visible even though | |
234 * it may not actually be showing. | |
235 * </p> | |
236 * | |
237 * @return the receiver's visibility state | |
238 * | |
239 * @exception DWTException <ul> | |
240 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
241 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
242 * </ul> | |
243 */ | |
244 public bool getVisible () { | |
245 checkWidget(); | |
246 return isVisible; | |
247 } | |
248 | |
249 bool hideCaret () { | |
250 if (!isShowing) return true; | |
251 isShowing = false; | |
252 return drawCaret (); | |
253 } | |
254 | |
255 /** | |
256 * Returns <code>true</code> if the receiver is visible and all | |
257 * of the receiver's ancestors are visible and <code>false</code> | |
258 * otherwise. | |
259 * | |
260 * @return the receiver's visibility state | |
261 * | |
262 * @exception DWTException <ul> | |
263 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
264 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
265 * </ul> | |
266 * | |
267 * @see #getVisible | |
268 */ | |
269 public bool isVisible () { | |
270 checkWidget(); | |
271 return isVisible && parent.isVisible () && parent.hasFocus (); | |
272 } | |
273 | |
274 bool isFocusCaret () { | |
275 return this is display.currentCaret; | |
276 } | |
277 | |
278 void killFocus () { | |
279 if (display.currentCaret !is this) return; | |
280 display.setCurrentCaret (null); | |
281 if (isVisible) hideCaret (); | |
282 } | |
283 | |
284 void releaseParent () { | |
285 super.releaseParent (); | |
286 if (this is parent.getCaret ()) parent.setCaret (null); | |
287 } | |
288 | |
289 void releaseWidget () { | |
290 super.releaseWidget (); | |
291 if (display.currentCaret is this) { | |
292 hideCaret (); | |
293 display.setCurrentCaret (null); | |
294 } | |
295 parent = null; | |
296 image = null; | |
297 } | |
298 | |
299 /** | |
300 * Sets the receiver's size and location to the rectangular | |
301 * area specified by the arguments. The <code>x</code> and | |
302 * <code>y</code> arguments are relative to the receiver's | |
303 * parent (or its display if its parent is null). | |
304 * | |
305 * @param x the new x coordinate for the receiver | |
306 * @param y the new y coordinate for the receiver | |
307 * @param width the new width for the receiver | |
308 * @param height the new height for the receiver | |
309 * | |
310 * @exception DWTException <ul> | |
311 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
312 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
313 * </ul> | |
314 */ | |
315 public void setBounds (int x, int y, int width, int height) { | |
316 checkWidget(); | |
317 if (this.x is x && this.y is y && this.width is width && this.height is height) return; | |
318 bool isFocus = isFocusCaret (); | |
319 if (isFocus && isVisible) hideCaret (); | |
320 this.x = x; | |
321 this.y = y; | |
322 this.width = width; | |
323 this.height = height; | |
324 if (isFocus && isVisible) showCaret (); | |
325 } | |
326 | |
327 /** | |
328 * Sets the receiver's size and location to the rectangular | |
329 * area specified by the argument. The <code>x</code> and | |
330 * <code>y</code> fields of the rectangle are relative to | |
331 * the receiver's parent (or its display if its parent is null). | |
332 * | |
333 * @param rect the new bounds for the receiver | |
334 * | |
335 * @exception DWTException <ul> | |
336 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
337 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
338 * </ul> | |
339 */ | |
340 public void setBounds (Rectangle rect) { | |
341 checkWidget(); | |
342 if (rect is null) error (DWT.ERROR_NULL_ARGUMENT); | |
343 setBounds (rect.x, rect.y, rect.width, rect.height); | |
344 } | |
345 | |
346 void setFocus () { | |
347 if (display.currentCaret is this) return; | |
348 display.setCurrentCaret (this); | |
349 if (isVisible) showCaret (); | |
350 } | |
351 | |
352 /** | |
353 * Sets the font that the receiver will use to paint textual information | |
354 * to the font specified by the argument, or to the default font for that | |
355 * kind of control if the argument is null. | |
356 * | |
357 * @param font the new font (or null) | |
358 * | |
359 * @exception IllegalArgumentException <ul> | |
360 * <li>ERROR_INVALID_ARGUMENT - if the font has been disposed</li> | |
361 * </ul> | |
362 * @exception DWTException <ul> | |
363 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
364 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
365 * </ul> | |
366 */ | |
367 public void setFont (Font font) { | |
368 checkWidget(); | |
369 if (font !is null && font.isDisposed ()) { | |
370 error (DWT.ERROR_INVALID_ARGUMENT); | |
371 } | |
372 this.font = font; | |
373 } | |
374 | |
375 /** | |
376 * Sets the image that the receiver will use to paint the caret | |
377 * to the image specified by the argument, or to the default | |
378 * which is a filled rectangle if the argument is null | |
379 * | |
380 * @param image the new image (or null) | |
381 * | |
382 * @exception IllegalArgumentException <ul> | |
383 * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li> | |
384 * </ul> | |
385 * @exception DWTException <ul> | |
386 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
387 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
388 * </ul> | |
389 */ | |
390 public void setImage (Image image) { | |
391 checkWidget(); | |
392 if (image !is null && image.isDisposed ()) { | |
393 error (DWT.ERROR_INVALID_ARGUMENT); | |
394 } | |
395 bool isFocus = isFocusCaret (); | |
396 if (isFocus && isVisible) hideCaret (); | |
397 this.image = image; | |
398 if (isFocus && isVisible) showCaret (); | |
399 } | |
400 | |
401 /** | |
402 * Sets the receiver's location to the point specified by | |
403 * the arguments which are relative to the receiver's | |
404 * parent (or its display if its parent is null). | |
405 * | |
406 * @param x the new x coordinate for the receiver | |
407 * @param y the new y coordinate for the receiver | |
408 * | |
409 * @exception DWTException <ul> | |
410 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
411 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
412 * </ul> | |
413 */ | |
414 public void setLocation (int x, int y) { | |
415 checkWidget(); | |
416 setBounds (x, y, width, height); | |
417 } | |
418 | |
419 /** | |
420 * Sets the receiver's location to the point specified by | |
421 * the argument which is relative to the receiver's | |
422 * parent (or its display if its parent is null). | |
423 * | |
424 * @param location the new location for the receiver | |
425 * | |
426 * @exception DWTException <ul> | |
427 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
428 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
429 * </ul> | |
430 */ | |
431 public void setLocation (Point location) { | |
432 checkWidget(); | |
433 if (location is null) error (DWT.ERROR_NULL_ARGUMENT); | |
434 setLocation (location.x, location.y); | |
435 } | |
436 | |
437 /** | |
438 * Sets the receiver's size to the point specified by the arguments. | |
439 * | |
440 * @param width the new width for the receiver | |
441 * @param height the new height for the receiver | |
442 * | |
443 * @exception DWTException <ul> | |
444 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
445 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
446 * </ul> | |
447 */ | |
448 public void setSize (int width, int height) { | |
449 checkWidget(); | |
450 setBounds (x, y, width, height); | |
451 } | |
452 | |
453 /** | |
454 * Sets the receiver's size to the point specified by the argument. | |
455 * | |
456 * @param size the new extent for the receiver | |
457 * | |
458 * @exception IllegalArgumentException <ul> | |
459 * <li>ERROR_NULL_ARGUMENT - if the point is null</li> | |
460 * </ul> | |
461 * @exception DWTException <ul> | |
462 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
463 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
464 * </ul> | |
465 */ | |
466 public void setSize (Point size) { | |
467 checkWidget(); | |
468 if (size is null) error (DWT.ERROR_NULL_ARGUMENT); | |
469 setSize (size.x, size.y); | |
470 } | |
471 | |
472 /** | |
473 * Marks the receiver as visible if the argument is <code>true</code>, | |
474 * and marks it invisible otherwise. | |
475 * <p> | |
476 * If one of the receiver's ancestors is not visible or some | |
477 * other condition makes the receiver not visible, marking | |
478 * it visible may not actually cause it to be displayed. | |
479 * </p> | |
480 * | |
481 * @param visible the new visibility state | |
482 * | |
483 * @exception DWTException <ul> | |
484 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li> | |
485 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li> | |
486 * </ul> | |
487 */ | |
488 public void setVisible (bool visible) { | |
489 checkWidget(); | |
490 if (visible is isVisible) return; | |
491 isVisible = visible; | |
492 if (!isFocusCaret ()) return; | |
493 if (isVisible) { | |
494 showCaret (); | |
495 } else { | |
496 hideCaret (); | |
497 } | |
498 } | |
499 | |
500 bool showCaret () { | |
501 if (isShowing) return true; | |
502 isShowing = true; | |
503 return drawCaret (); | |
504 } | |
505 | |
506 } |