package org.eclipse.swt.widgets; /* * Copyright (c) 2000, 2002 IBM Corp. All rights reserved. * This file is made available under the terms of the Common Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/cpl-v10.html */ import org.eclipse.swt.*; import org.eclipse.swt.internal.*; import org.eclipse.swt.internal.gtk.*; import org.eclipse.swt.graphics.*; /** * Instances of this class provide an i-beam that is typically used * as the insertion point for text. *
*
Styles:
*
(none)
*
Events:
*
(none)
*
*

* IMPORTANT: This class is intended to be subclassed only * within the SWT implementation. *

*/ public class Caret extends Widget { Canvas parent; int x, y, width, height; boolean moved, resized; boolean isVisible,isShowing; int blinkRate = 500; Image image; /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. *

* The style value is either one of the style constants defined in * class SWT which is applicable to instances of this * class, or must be built by bitwise OR'ing together * (that is, using the int "|" operator) two or more * of those SWT style constants. The class description * for all SWT widget classes should include a comment which * describes the style constants which are applicable to the class. *

* * @param parent a composite control which will be the parent of the new instance (cannot be null) * @param style the style of control to construct * * @exception IllegalArgumentException * @exception SWTException * * @see SWT * @see Widget#checkSubclass * @see Widget#getStyle */ public Caret (Canvas parent, int style) { super (parent, style); this.parent = parent; createWidget (0); } void createWidget (int index) { super.createWidget (index); isVisible = true; if (parent.getCaret () == null) { parent.setCaret (this); } } boolean blinkCaret () { if (!isVisible) return true; if (!isShowing) return showCaret(); if (blinkRate==0) return true; return hideCaret(); } boolean drawCaret () { if (parent == null) return false; if (parent.isDisposed ()) return false; /* The parent is a Canvas; its handle is a GtkDrawingArea. * Get the DA's GDK window to draw on. */ GtkWidget widget = new GtkWidget(); OS.memmove (widget, parent.handle, GtkWidget.sizeof); int window = widget.window; /* Create the GC, and set the working color and rop. */ int gc = OS.gdk_gc_new(window); GdkGCValues gcvalues = new GdkGCValues(); OS.gdk_gc_get_values(gc, gcvalues); /* Actually, we should look at the background and foreground colors. * This would require distinguishing between the cases when the GC * gives the color as RGB or Pixel, and in the case of Pixel, we * would need to distinguish between direct and indexed color. * In general, it's not easy to find out the RGB value of a GdkColor * (somebody please correct me if I am wrong). */ GdkColor c = new GdkColor(); c.red = c.green = c.blue = (short)0xFFFF; OS.gdk_color_alloc(OS.gdk_colormap_get_system(), c); OS.gdk_gc_set_foreground(gc, c); OS.gdk_gc_set_function(gc, OS.GDK_XOR); /* Draw the caret */ int nWidth = width; if (nWidth <= 0) nWidth = 2; OS.gdk_draw_rectangle(window, gc, 1, x, y, nWidth, height); OS.gdk_gc_destroy(gc); return true; } /** * Returns a rectangle describing the receiver's size and location * relative to its parent (or its display if its parent is null). * * @return the receiver's bounding rectangle * * @exception SWTException */ public Rectangle getBounds () { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); return new Rectangle (x, y, width, height); } public Display getDisplay () { Composite parent = this.parent; if (parent == null) error (SWT.ERROR_WIDGET_DISPOSED); return parent.getDisplay (); } /** * Returns the font that the receiver will use to paint textual information. * * @return the receiver's font * * @exception SWTException */ public Font getFont () { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); return parent.getFont (); } /** * Returns a point describing the receiver's location relative * to its parent (or its display if its parent is null). * * @return the receiver's location * * @exception SWTException */ public Point getLocation () { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); return new Point (x, y); } /** * Returns the receiver's parent, which must be a Canvas. * * @return the receiver's parent * * @exception SWTException */ public Canvas getParent () { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); return parent; } /** * Returns a point describing the receiver's size. * * @return the receiver's size * * @exception SWTException */ public Point getSize () { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); return new Point (width, height); } /** * Returns true if the receiver is visible, and * false otherwise. *

* If one of the receiver's ancestors is not visible or some * other condition makes the receiver not visible, this method * may still indicate that it is considered visible even though * it may not actually be showing. *

* * @return the receiver's visibility state * * @exception SWTException */ public boolean getVisible () { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); return isVisible; } boolean hideCaret () { // Display display = getDisplay (); // if (display.currentCaret != this) return false; if (!isShowing) return true; isShowing = false; return drawCaret (); } /** * Returns true if the receiver is visible, and * false otherwise. *

* If one of the receiver's ancestors is not visible or some * other condition makes the receiver not visible, this method * may still indicate that it is considered visible even though * it may not actually be showing. *

* * @return the receiver's visibility state * * @exception SWTException */ public boolean isVisible () { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); return isVisible && parent.isVisible (); // && parent.hasFocus (); } void killFocus () { // OS.DestroyCaret (); // self restoreFont. } void move () { showCaret(); moved = false; } void releaseChild () { super.releaseChild (); if (this == parent.getCaret ()) parent.setCaret (null); } void releaseWidget () { super.releaseWidget (); parent = null; } void resize () { int hwnd = parent.handle; if (hwnd == 0) return; // OS.DestroyCaret (); // OS.CreateCaret (hwnd, 0, width, height); // OS.SetCaretPos (x, y); // OS.ShowCaret (hwnd); // showCaret(); resized = false; } /** * Sets the receiver's size and location to the rectangular * area specified by the arguments. The x and * y arguments are relative to the receiver's * parent (or its display if its parent is null). * * @param x the new x coordinate for the receiver * @param y the new y coordinate for the receiver * @param width the new width for the receiver * @param height the new height for the receiver * * @exception SWTException */ public void setBounds (int x, int y, int width, int height) { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); boolean samePosition, sameExtent, showing; samePosition = (this.x == x) && (this.y == y); sameExtent = (this.width == width) && (this.height == height); if (samePosition && sameExtent) return; if (isShowing) hideCaret (); this.x = x; this.y = y; this.width = width; this.height = height; if (sameExtent) { moved = true; if (isVisible ()) move (); } else { resized = true; if (isVisible ()) resize (); } if(isVisible()) showCaret (); } /** * Sets the receiver's size and location to the rectangular * area specified by the argument. The x and * y fields of the rectangle are relative to * the receiver's parent (or its display if its parent is null). * * @param rect the new bounds for the receiver * * @exception SWTException */ public void setBounds (Rectangle rect) { if (rect == null) error (SWT.ERROR_NULL_ARGUMENT); setBounds (rect.x, rect.y, rect.width, rect.height); } void setFocus () { Display display = getDisplay(); if (display.currentCaret==this) return; display.setCurrentCaret(this); if (isVisible) showCaret (); } /** * Sets the font that the receiver will use to paint textual information * to the font specified by the argument, or to the default font for that * kind of control if the argument is null. * * @param font the new font (or null) * * @exception IllegalArgumentException * @exception SWTException */ public void setFont (Font font) { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); if (font != null && font.isDisposed ()) { error (SWT.ERROR_INVALID_ARGUMENT); } } /** * Returns the image that the receiver will use to paint the caret. * * @return the receiver's image * * @exception SWTException */ public Image getImage () { checkWidget(); return image; } /** * Sets the image that the receiver will use to paint the caret * to the image specified by the argument, or to the default * which is a filled rectangle if the argument is null * * @param font the new font (or null) * * @exception IllegalArgumentException * @exception SWTException */ public void setImage (Image image) { checkWidget(); if (image != null && image.isDisposed ()) { error (SWT.ERROR_INVALID_ARGUMENT); } if (isShowing) hideCaret (); this.image = image; if (isShowing) showCaret (); } /** * Sets the receiver's location to the point specified by * the arguments which are relative to the receiver's * parent (or its display if its parent is null). * * @param x the new x coordinate for the receiver * @param y the new y coordinate for the receiver * * @exception SWTException */ public void setLocation (int x, int y) { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); setBounds (x, y, width, height); } /** * Sets the receiver's location to the point specified by * the argument which is relative to the receiver's * parent (or its display if its parent is null). * * @param location the new location for the receiver * * @exception SWTException */ public void setLocation (Point location) { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); if (location == null) error (SWT.ERROR_NULL_ARGUMENT); setLocation (location.x, location.y); } /** * Sets the receiver's size to the point specified by the arguments. * * @param width the new width for the receiver * @param height the new height for the receiver * * @exception SWTException */ public void setSize (int width, int height) { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); if (this.width == width && this.height == height) return; this.width = width; this.height = height; resized = true; if (isVisible ()) resize (); } /** * Sets the receiver's size to the point specified by the argument. * * @param size the new extent for the receiver * @param height the new height for the receiver * * @exception IllegalArgumentException * @exception SWTException */ public void setSize (Point size) { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); if (size == null) error (SWT.ERROR_NULL_ARGUMENT); setSize (size.x, size.y); } /** * Marks the receiver as visible if the argument is true, * and marks it invisible otherwise. *

* If one of the receiver's ancestors is not visible or some * other condition makes the receiver not visible, marking * it visible may not actually cause it to be displayed. *

* * @param visible the new visibility state * * @exception SWTException */ public void setVisible (boolean visible) { if (!isValidThread ()) error (SWT.ERROR_THREAD_INVALID_ACCESS); if (!isValidWidget ()) error (SWT.ERROR_WIDGET_DISPOSED); if (visible == isVisible) return; if (isVisible = visible) { showCaret (); } else { hideCaret (); } } boolean showCaret () { // if (getDisplay ().currentCaret != this) return false; if (isShowing) return true; isShowing = true; return drawCaret (); } }