org.eclipse.swt.widgets.Caret.java Source code

Java tutorial

Introduction

Here is the source code for org.eclipse.swt.widgets.Caret.java

Source

/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.widgets;

import org.eclipse.swt.internal.wpf.*;
import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;

/**
 * Instances of this class provide an i-beam that is typically used
 * as the insertion point for text.
 * <dl>
 * <dt><b>Styles:</b></dt>
 * <dd>(none)</dd>
 * <dt><b>Events:</b></dt>
 * <dd>(none)</dd>
 * </dl>
 * <p>
 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
 * </p>
 * 
 * @see <a href="http://www.eclipse.org/swt/snippets/#caret">Caret snippets</a>
 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample, Canvas tab</a>
 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
 * @noextend This class is not intended to be subclassed by clients.
 */

public class Caret extends Widget {
    Canvas parent;
    int x, y, width, height;
    int imageHandle;
    boolean isVisible;
    Image image;
    Font font;

    /**
     * Constructs a new instance of this class given its parent
     * and a style value describing its behavior and appearance.
     * <p>
     * The style value is either one of the style constants defined in
     * class <code>SWT</code> which is applicable to instances of this
     * class, or must be built by <em>bitwise OR</em>'ing together 
     * (that is, using the <code>int</code> "|" operator) two or more
     * of those <code>SWT</code> style constants. The class description
     * lists the style constants that are applicable to the class.
     * Style bits are also inherited from superclasses.
     * </p>
     *
     * @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 <ul>
     *    <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
     * </ul>
     * @exception SWTException <ul>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
     *    <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
     * </ul>
     *
     * @see SWT
     * @see Widget#checkSubclass
     * @see Widget#getStyle
     */
    public Caret(Canvas parent, int style) {
        super(parent, style);
        this.parent = parent;
        createWidget();
    }

    void createWidget() {
        createHandle();
        if (parent.getCaret() == null) {
            parent.setCaret(this);
        }
    }

    void createHandle() {
        handle = OS.gcnew_StackPanel();
        //handle = OS.gcnew_ContentControl ();
        if (handle == 0)
            error(SWT.ERROR_NO_HANDLES);

        int animation = OS.gcnew_DoubleAnimationUsingKeyFrames();
        if (animation == 0)
            error(SWT.ERROR_NO_HANDLES);
        int timespan = OS.gcnew_TimeSpan(6000000);
        if (timespan == 0)
            error(SWT.ERROR_NO_HANDLES);
        int duration = OS.gcnew_Duration(timespan);
        if (duration == 0)
            error(SWT.ERROR_NO_HANDLES);
        int repeatBehavior = OS.RepeatBehavior_Forever();

        OS.Timeline_AutoReverse(animation, true);
        OS.Timeline_Duration(animation, duration);
        OS.Timeline_RepeatBehavior(animation, repeatBehavior);
        int keyFrames = OS.DoubleAnimationUsingKeyFrames_KeyFrames(animation);
        int keyTime = OS.KeyTime_Uniform();
        int keyFrame0 = OS.gcnew_DiscreteDoubleKeyFrame(0, keyTime);
        int keyFrame1 = OS.gcnew_DiscreteDoubleKeyFrame(0, keyTime);
        OS.DoubleKeyFrameCollection_Add(keyFrames, keyFrame0);
        OS.DoubleKeyFrameCollection_Add(keyFrames, keyFrame1);
        int opacityProperty = OS.UIElement_OpacityProperty();
        OS.UIElement_BeginAnimation(handle, opacityProperty, animation);

        int brush = OS.SystemColors_ControlTextBrush();
        OS.Panel_Background(handle, brush);

        imageHandle = OS.gcnew_Image();
        if (imageHandle == 0)
            error(SWT.ERROR_NO_HANDLES);
        OS.Image_Stretch(imageHandle, OS.Stretch_None);
        OS.UIElement_Visibility(imageHandle, OS.Visibility_Collapsed);

        int children = OS.Panel_Children(handle);
        OS.UIElementCollection_Add(children, imageHandle);
        OS.GCHandle_Free(children);
        hide();
        OS.GCHandle_Free(brush);
        OS.GCHandle_Free(opacityProperty);
        OS.GCHandle_Free(keyFrames);
        OS.GCHandle_Free(keyTime);
        OS.GCHandle_Free(keyFrame0);
        OS.GCHandle_Free(keyFrame1);
        OS.GCHandle_Free(animation);
        OS.GCHandle_Free(duration);
        OS.GCHandle_Free(timespan);
        OS.GCHandle_Free(repeatBehavior);
    }

    /**
     * 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 <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public Rectangle getBounds() {
        checkWidget();
        if (image != null) {
            Rectangle rect = image.getBounds();
            return new Rectangle(x, y, rect.width, rect.height);
        }
        return new Rectangle(x, y, width, height);
    }

    /**
     * Returns the font that the receiver will use to paint textual information.
     *
     * @return the receiver's font
     *
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public Font getFont() {
        checkWidget();
        if (font == null) {
            return parent.display.systemFont;
        }
        return font;
    }

    /**
     * Returns the image that the receiver will use to paint the caret.
     *
     * @return the receiver's image
     *
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public Image getImage() {
        checkWidget();
        return image;
    }

    /**
     * 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 <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public Point getLocation() {
        checkWidget();
        return new Point(x, y);
    }

    /**
     * Returns the receiver's parent, which must be a <code>Canvas</code>.
     *
     * @return the receiver's parent
     *
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public Canvas getParent() {
        checkWidget();
        return parent;
    }

    /**
     * Returns a point describing the receiver's size.
     *
     * @return the receiver's size
     *
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public Point getSize() {
        checkWidget();
        if (image != null) {
            Rectangle rect = image.getBounds();
            return new Point(rect.width, rect.height);
        }
        return new Point(width, height);
    }

    /**
     * Returns <code>true</code> if the receiver is visible, and
     * <code>false</code> otherwise.
     * <p>
     * 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.
     * </p>
     *
     * @return the receiver's visibility state
     *
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public boolean getVisible() {
        checkWidget();
        return isVisible;
    }

    boolean hasFocus() {
        return OS.UIElement_IsKeyboardFocused(parent.handle);
    }

    boolean isFocusCaret() {
        return parent.caret == this && hasFocus();
    }

    /**
     * Returns <code>true</code> if the receiver is visible and all
     * of the receiver's ancestors are visible and <code>false</code>
     * otherwise.
     *
     * @return the receiver's visibility state
     *
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     *
     * @see #getVisible
     */
    public boolean isVisible() {
        checkWidget();
        return OS.UIElement_IsVisible(handle) && parent.isVisible() && hasFocus();
    }

    void hide() {
        OS.UIElement_Visibility(handle, OS.Visibility_Collapsed);
    }

    void move(int x, int y) {
        OS.Canvas_SetLeft(handle, x);
        OS.Canvas_SetTop(handle, y);
    }

    void releaseHandle() {
        if (handle != 0)
            OS.GCHandle_Free(handle);
        handle = 0;
        if (imageHandle != 0)
            OS.GCHandle_Free(imageHandle);
        imageHandle = 0;
        super.releaseHandle();
    }

    void releaseParent() {
        super.releaseParent();
        if (this == parent.getCaret())
            parent.setCaret(null);
    }

    void releaseWidget() {
        super.releaseWidget();
        parent = null;
        image = null;
        font = null;
    }

    void resize(int width, int height) {
        if (image != null) {
            Rectangle rect = image.getBounds();
            width = rect.width;
            height = rect.height;
        }
        if (width == 0)
            width = 1;
        OS.FrameworkElement_Width(handle, width);
        OS.FrameworkElement_Height(handle, height);
    }

    /**
     * Sets the receiver's size and location to the rectangular
     * area specified by the arguments. The <code>x</code> and 
     * <code>y</code> 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 <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setBounds(int x, int y, int width, int height) {
        checkWidget();
        boolean samePosition = this.x == x && this.y == y;
        boolean sameExtent = this.width == width && this.height == height;
        if (samePosition && sameExtent)
            return;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        if (!sameExtent)
            resize(width, height);
        if (!samePosition)
            move(x, y);
    }

    /**
     * Sets the receiver's size and location to the rectangular
     * area specified by the argument. The <code>x</code> and 
     * <code>y</code> 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 <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setBounds(Rectangle rect) {
        if (rect == null)
            error(SWT.ERROR_NULL_ARGUMENT);
        setBounds(rect.x, rect.y, rect.width, rect.height);
    }

    void show() {
        OS.UIElement_Visibility(handle, OS.Visibility_Visible);
    }

    /**
     * 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 <ul>
     *    <li>ERROR_INVALID_ARGUMENT - if the font has been disposed</li>
     * </ul> 
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setFont(Font font) {
        checkWidget();
        if (font != null && font.isDisposed()) {
            error(SWT.ERROR_INVALID_ARGUMENT);
        }
        this.font = font;
    }

    /**
     * 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 image the new image (or null)
     *
     * @exception IllegalArgumentException <ul>
     *    <li>ERROR_INVALID_ARGUMENT - if the image has been disposed</li>
     * </ul> 
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setImage(Image image) {
        checkWidget();
        if (image != null && image.isDisposed()) {
            error(SWT.ERROR_INVALID_ARGUMENT);
        }
        this.image = image;
        if (image != null) {
            OS.Image_Source(imageHandle, image.handle);
            OS.UIElement_Visibility(imageHandle, OS.Visibility_Visible);
        } else {
            OS.Image_Source(imageHandle, 0);
            OS.UIElement_Visibility(imageHandle, OS.Visibility_Collapsed);
        }
        resize(width, height);
    }

    /**
     * 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 <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setLocation(int x, int y) {
        checkWidget();
        if (this.x == x && this.y == y)
            return;
        this.x = x;
        this.y = y;
        move(x, y);
    }

    /**
     * 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 <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setLocation(Point location) {
        checkWidget();
        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 <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setSize(int width, int height) {
        checkWidget();
        if (this.width == width && this.height == height)
            return;
        this.width = width;
        this.height = height;
        resize(width, height);
    }

    /**
     * Sets the receiver's size to the point specified by the argument.
     *
     * @param size the new extent for the receiver
     *
     * @exception IllegalArgumentException <ul>
     *    <li>ERROR_NULL_ARGUMENT - if the point is null</li>
     * </ul>
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setSize(Point size) {
        checkWidget();
        if (size == null)
            error(SWT.ERROR_NULL_ARGUMENT);
        setSize(size.x, size.y);
    }

    /**
     * Marks the receiver as visible if the argument is <code>true</code>,
     * and marks it invisible otherwise. 
     * <p>
     * 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.
     * </p>
     *
     * @param visible the new visibility state
     *
     * @exception SWTException <ul>
     *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
     *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
     * </ul>
     */
    public void setVisible(boolean visible) {
        checkWidget();
        if (visible == isVisible)
            return;
        isVisible = visible;
        if (!hasFocus())
            return;
        OS.UIElement_Visibility(handle, visible ? OS.Visibility_Visible : OS.Visibility_Hidden);
    }

}