KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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 an i-beam that is typically used
20  * as the insertion point for text.
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  * IMPORTANT: This class is intended to be subclassed <em>only</em>
29  * within the SWT implementation.
30  * </p>
31  */

32
33 public class Caret extends Widget {
34     Canvas parent;
35     int x, y, width, height;
36     boolean moved, resized;
37     boolean isVisible;
38     Image image;
39     Font font;
40     LOGFONT oldFont;
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 Caret (Canvas parent, int style) {
71     super (parent, style);
72     this.parent = parent;
73     createWidget ();
74 }
75
76 void createWidget () {
77     isVisible = true;
78     if (parent.getCaret () == null) {
79         parent.setCaret (this);
80     }
81 }
82
83 int defaultFont () {
84     int hwnd = parent.handle;
85     int hwndIME = OS.ImmGetDefaultIMEWnd (hwnd);
86     int hFont = 0;
87     if (hwndIME != 0) {
88         hFont = OS.SendMessage (hwndIME, OS.WM_GETFONT, 0, 0);
89     }
90     if (hFont == 0) {
91         hFont = OS.SendMessage (hwnd, OS.WM_GETFONT, 0, 0);
92     }
93     if (hFont == 0) return parent.defaultFont ();
94     return hFont;
95 }
96
97 /**
98  * Returns a rectangle describing the receiver's size and location
99  * relative to its parent (or its display if its parent is null).
100  *
101  * @return the receiver's bounding rectangle
102  *
103  * @exception SWTException <ul>
104  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
105  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
106  * </ul>
107  */

108 public Rectangle getBounds () {
109     checkWidget();
110     if (image != null) {
111         Rectangle rect = image.getBounds ();
112         return new Rectangle (x, y, rect.width, rect.height);
113     }
114     return new Rectangle (x, y, width, height);
115 }
116
117 /**
118  * Returns the font that the receiver will use to paint textual information.
119  *
120  * @return the receiver's font
121  *
122  * @exception SWTException <ul>
123  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
124  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
125  * </ul>
126  */

127 public Font getFont () {
128     checkWidget();
129     if (font == null) {
130         int hFont = defaultFont ();
131         return Font.win32_new (display, hFont);
132     }
133     return font;
134 }
135
136 /**
137  * Returns the image that the receiver will use to paint the caret.
138  *
139  * @return the receiver's image
140  *
141  * @exception SWTException <ul>
142  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
143  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
144  * </ul>
145  */

146 public Image getImage () {
147     checkWidget();
148     return image;
149 }
150
151 /**
152  * Returns a point describing the receiver's location relative
153  * to its parent (or its display if its parent is null).
154  *
155  * @return the receiver's location
156  *
157  * @exception SWTException <ul>
158  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
159  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
160  * </ul>
161  */

162 public Point getLocation () {
163     checkWidget();
164     return new Point (x, y);
165 }
166
167 /**
168  * Returns the receiver's parent, which must be a <code>Canvas</code>.
169  *
170  * @return the receiver's parent
171  *
172  * @exception SWTException <ul>
173  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
174  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
175  * </ul>
176  */

177 public Canvas getParent () {
178     checkWidget();
179     return parent;
180 }
181
182 /**
183  * Returns a point describing the receiver's size.
184  *
185  * @return the receiver's size
186  *
187  * @exception SWTException <ul>
188  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
189  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
190  * </ul>
191  */

192 public Point getSize () {
193     checkWidget();
194     if (image != null) {
195         Rectangle rect = image.getBounds ();
196         return new Point (rect.width, rect.height);
197     }
198     return new Point (width, height);
199 }
200
201 /**
202  * Returns <code>true</code> if the receiver is visible, and
203  * <code>false</code> otherwise.
204  * <p>
205  * If one of the receiver's ancestors is not visible or some
206  * other condition makes the receiver not visible, this method
207  * may still indicate that it is considered visible even though
208  * it may not actually be showing.
209  * </p>
210  *
211  * @return the receiver's visibility state
212  *
213  * @exception SWTException <ul>
214  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
215  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
216  * </ul>
217  */

218 public boolean getVisible () {
219     checkWidget();
220     return isVisible;
221 }
222
223 boolean hasFocus () {
224     return parent.handle == OS.GetFocus ();
225 }
226
227 boolean isFocusCaret () {
228     return parent.caret == this && hasFocus ();
229 }
230
231 /**
232  * Returns <code>true</code> if the receiver is visible and all
233  * of the receiver's ancestors are visible and <code>false</code>
234  * otherwise.
235  *
236  * @return the receiver's visibility state
237  *
238  * @exception SWTException <ul>
239  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
240  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
241  * </ul>
242  *
243  * @see #getVisible
244  */

245 public boolean isVisible () {
246     checkWidget();
247     return isVisible && parent.isVisible () && hasFocus ();
248 }
249
250 void killFocus () {
251     OS.DestroyCaret ();
252     restoreIMEFont ();
253 }
254
255 void move () {
256     moved = false;
257     if (!OS.SetCaretPos (x, y)) return;
258     resizeIME ();
259 }
260
261 void resizeIME () {
262     if (!OS.IsDBLocale) return;
263     POINT ptCurrentPos = new POINT ();
264     if (!OS.GetCaretPos (ptCurrentPos)) return;
265     int hwnd = parent.handle;
266     RECT rect = new RECT ();
267     OS.GetClientRect (hwnd, rect);
268     COMPOSITIONFORM lpCompForm = new COMPOSITIONFORM ();
269     lpCompForm.dwStyle = OS.CFS_RECT;
270     lpCompForm.x = ptCurrentPos.x;
271     lpCompForm.y = ptCurrentPos.y;
272     lpCompForm.left = rect.left;
273     lpCompForm.right = rect.right;
274     lpCompForm.top = rect.top;
275     lpCompForm.bottom = rect.bottom;
276     int hIMC = OS.ImmGetContext (hwnd);
277     OS.ImmSetCompositionWindow (hIMC, lpCompForm);
278     OS.ImmReleaseContext (hwnd, hIMC);
279 }
280
281 void releaseParent () {
282     super.releaseParent ();
283     if (this == parent.getCaret ()) parent.setCaret (null);
284 }
285
286 void releaseWidget () {
287     super.releaseWidget ();
288     parent = null;
289     image = null;
290     font = null;
291     oldFont = null;
292 }
293
294 void resize () {
295     resized = false;
296     int hwnd = parent.handle;
297     OS.DestroyCaret ();
298     int hBitmap = image != null ? image.handle : 0;
299     OS.CreateCaret (hwnd, hBitmap, width, height);
300     OS.SetCaretPos (x, y);
301     OS.ShowCaret (hwnd);
302     move ();
303 }
304
305 void restoreIMEFont () {
306     if (!OS.IsDBLocale) return;
307     if (oldFont == null) return;
308     int hwnd = parent.handle;
309     int hIMC = OS.ImmGetContext (hwnd);
310     OS.ImmSetCompositionFont (hIMC, oldFont);
311     OS.ImmReleaseContext (hwnd, hIMC);
312     oldFont = null;
313 }
314
315 /**
316  * Sets the receiver's size and location to the rectangular
317  * area specified by the arguments. The <code>x</code> and
318  * <code>y</code> arguments are relative to the receiver's
319  * parent (or its display if its parent is null).
320  *
321  * @param x the new x coordinate for the receiver
322  * @param y the new y coordinate for the receiver
323  * @param width the new width for the receiver
324  * @param height the new height for the receiver
325  *
326  * @exception SWTException <ul>
327  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
328  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
329  * </ul>
330  */

331 public void setBounds (int x, int y, int width, int height) {
332     checkWidget();
333     boolean samePosition = this.x == x && this.y == y;
334     boolean sameExtent = this.width == width && this.height == height;
335     if (samePosition && sameExtent) return;
336     this.x = x; this.y = y;
337     this.width = width; this.height = height;
338     if (sameExtent) {
339         moved = true;
340         if (isVisible && hasFocus ()) move ();
341     } else {
342         resized = true;
343         if (isVisible && hasFocus ()) resize ();
344     }
345 }
346
347 /**
348  * Sets the receiver's size and location to the rectangular
349  * area specified by the argument. The <code>x</code> and
350  * <code>y</code> fields of the rectangle are relative to
351  * the receiver's parent (or its display if its parent is null).
352  *
353  * @param rect the new bounds for the receiver
354  *
355  * @exception SWTException <ul>
356  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
357  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
358  * </ul>
359  */

360 public void setBounds (Rectangle rect) {
361     if (rect == null) error (SWT.ERROR_NULL_ARGUMENT);
362     setBounds (rect.x, rect.y, rect.width, rect.height);
363 }
364
365 void setFocus () {
366     int hwnd = parent.handle;
367     int hBitmap = 0;
368     if (image != null) hBitmap = image.handle;
369     OS.CreateCaret (hwnd, hBitmap, width, height);
370     move ();
371     setIMEFont ();
372     if (isVisible) OS.ShowCaret (hwnd);
373 }
374
375 /**
376  * Sets the font that the receiver will use to paint textual information
377  * to the font specified by the argument, or to the default font for that
378  * kind of control if the argument is null.
379  *
380  * @param font the new font (or null)
381  *
382  * @exception IllegalArgumentException <ul>
383  * <li>ERROR_INVALID_ARGUMENT - if the font has been disposed</li>
384  * </ul>
385  * @exception SWTException <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 setFont (Font font) {
391     checkWidget();
392     if (font != null && font.isDisposed ()) {
393         error (SWT.ERROR_INVALID_ARGUMENT);
394     }
395     this.font = font;
396     if (hasFocus ()) setIMEFont ();
397 }
398
399 /**
400  * Sets the image that the receiver will use to paint the caret
401  * to the image specified by the argument, or to the default
402  * which is a filled rectangle if the argument is null
403  *
404  * @param image the new image (or null)
405  *
406  * @exception IllegalArgumentException <ul>
407  * <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
408  * </ul>
409  * @exception SWTException <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 setImage (Image image) {
415     checkWidget();
416     if (image != null && image.isDisposed ()) {
417         error (SWT.ERROR_INVALID_ARGUMENT);
418     }
419     this.image = image;
420     if (isVisible && hasFocus ()) resize ();
421 }
422
423 void setIMEFont () {
424     if (!OS.IsDBLocale) return;
425     int hFont = 0;
426     if (font != null) hFont = font.handle;
427     if (hFont == 0) hFont = defaultFont ();
428     int hwnd = parent.handle;
429     int hIMC = OS.ImmGetContext (hwnd);
430     /* Save the current IME font */
431     if (oldFont == null) {
432         oldFont = OS.IsUnicode ? (LOGFONT) new LOGFONTW () : new LOGFONTA ();
433         if (!OS.ImmGetCompositionFont (hIMC, oldFont)) oldFont = null;
434     }
435     /* Set new IME font */
436     LOGFONT logFont = OS.IsUnicode ? (LOGFONT) new LOGFONTW () : new LOGFONTA ();
437     if (OS.GetObject (hFont, LOGFONT.sizeof, logFont) != 0) {
438         OS.ImmSetCompositionFont (hIMC, logFont);
439     }
440     OS.ImmReleaseContext (hwnd, hIMC);
441 }
442
443 /**
444  * Sets the receiver's location to the point specified by
445  * the arguments which are relative to the receiver's
446  * parent (or its display if its parent is null).
447  *
448  * @param x the new x coordinate for the receiver
449  * @param y the new y coordinate for the receiver
450  *
451  * @exception SWTException <ul>
452  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
453  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
454  * </ul>
455  */

456 public void setLocation (int x, int y) {
457     checkWidget();
458     if (this.x == x && this.y == y) return;
459     this.x = x; this.y = y;
460     moved = true;
461     if (isVisible && hasFocus ()) move ();
462 }
463
464 /**
465  * Sets the receiver's location to the point specified by
466  * the argument which is relative to the receiver's
467  * parent (or its display if its parent is null).
468  *
469  * @param location the new location for the receiver
470  *
471  * @exception SWTException <ul>
472  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
473  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
474  * </ul>
475  */

476 public void setLocation (Point location) {
477     checkWidget();
478     if (location == null) error (SWT.ERROR_NULL_ARGUMENT);
479     setLocation (location.x, location.y);
480 }
481
482 /**
483  * Sets the receiver's size to the point specified by the arguments.
484  *
485  * @param width the new width for the receiver
486  * @param height the new height for the receiver
487  *
488  * @exception SWTException <ul>
489  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
490  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
491  * </ul>
492  */

493 public void setSize (int width, int height) {
494     checkWidget();
495     if (this.width == width && this.height == height) return;
496     this.width = width; this.height = height;
497     resized = true;
498     if (isVisible && hasFocus ()) resize ();
499 }
500
501 /**
502  * Sets the receiver's size to the point specified by the argument.
503  *
504  * @param size the new extent for the receiver
505  *
506  * @exception IllegalArgumentException <ul>
507  * <li>ERROR_NULL_ARGUMENT - if the point is null</li>
508  * </ul>
509  * @exception SWTException <ul>
510  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
511  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
512  * </ul>
513  */

514 public void setSize (Point size) {
515     checkWidget();
516     if (size == null) error (SWT.ERROR_NULL_ARGUMENT);
517     setSize (size.x, size.y);
518 }
519
520 /**
521  * Marks the receiver as visible if the argument is <code>true</code>,
522  * and marks it invisible otherwise.
523  * <p>
524  * If one of the receiver's ancestors is not visible or some
525  * other condition makes the receiver not visible, marking
526  * it visible may not actually cause it to be displayed.
527  * </p>
528  *
529  * @param visible the new visibility state
530  *
531  * @exception SWTException <ul>
532  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
533  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
534  * </ul>
535  */

536 public void setVisible (boolean visible) {
537     checkWidget();
538     if (visible == isVisible) return;
539     isVisible = visible;
540     int hwnd = parent.handle;
541     if (OS.GetFocus () != hwnd) return;
542     if (!isVisible) {
543         OS.HideCaret (hwnd);
544     } else {
545         if (resized) {
546             resize ();
547         } else {
548             if (moved) move ();
549         }
550         OS.ShowCaret (hwnd);
551     }
552 }
553
554 }
555
Popular Tags