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 represent a non-selectable * user interface object that displays a string or image. * When SEPARATOR is specified, displays a single * vertical or horizontal line. *
*
Styles:
*
SEPARATOR, HORIZONTAL, SHADOW_IN, SHADOW_OUT, VERTICAL
*
CENTER, LEFT, RIGHT, WRAP
*
Events:
*
(none)
*
*

* Note: Only one of SHADOW_IN and SHADOW_OUT may be specified. Only * one of HORIZONTAL and VERTICAL may be specified. Only one of CENTER, * LEFT and RIGHT may be specified. *

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

*/ public class Label extends Control { int boxHandle, frameHandle; Image image; String text; /** * 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 Label (Composite parent, int style) { super (parent, checkStyle (style)); } static int checkStyle (int style) { if ((style & SWT.SEPARATOR) != 0) return style; return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0); } void createHandle (int index) { state |= HANDLE; boxHandle = OS.gtk_event_box_new (); if (boxHandle == 0) error (SWT.ERROR_NO_HANDLES); frameHandle = OS.gtk_frame_new(null); if (frameHandle == 0) error (SWT.ERROR_NO_HANDLES); handle = ((style&SWT.SEPARATOR) != 0)? (((style&SWT.HORIZONTAL)!= 0)? OS.gtk_hseparator_new() : OS.gtk_vseparator_new()): OS.gtk_label_new (new byte [1]); if (handle == 0) error (SWT.ERROR_NO_HANDLES); } void createWidget (int index) { super.createWidget (index); text = ""; } void setHandleStyle () { int type = (style & SWT.BORDER) != 0 ? OS.GTK_SHADOW_ETCHED_IN : OS.GTK_SHADOW_NONE; OS.gtk_frame_set_shadow_type (frameHandle, type); if ((style & SWT.SEPARATOR) != 0) return; if ((style & SWT.WRAP) != 0) OS.gtk_label_set_line_wrap (handle, true); if ((style & SWT.LEFT) != 0) { OS.gtk_misc_set_alignment (handle, 0.0f, 0.0f); OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_LEFT); return; } if ((style & SWT.CENTER) != 0) { OS.gtk_misc_set_alignment (handle, 0.5f, 0.0f); OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_CENTER); return; } if ((style & SWT.RIGHT) != 0) { OS.gtk_misc_set_alignment (handle, 1.0f, 0.0f); OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_RIGHT); return; } } void configure() { _connectParent(); OS.gtk_container_add(boxHandle, frameHandle); OS.gtk_container_add(frameHandle, handle); } void showHandle() { OS.gtk_widget_show (boxHandle); OS.gtk_widget_show (frameHandle); OS.gtk_widget_show (handle); OS.gtk_widget_realize (handle); } void register () { super.register (); WidgetTable.put (boxHandle, this); WidgetTable.put (frameHandle, this); } void deregister () { super.deregister (); WidgetTable.remove (boxHandle); WidgetTable.remove (frameHandle); } int eventHandle () { return boxHandle; } void releaseWidget () { super.releaseWidget (); image = null; text = null; } void releaseHandle () { super.releaseHandle (); boxHandle = frameHandle = 0; } int topHandle () { return boxHandle; } int computeHandle () { return frameHandle; } public Point computeSize (int wHint, int hHint, boolean changed) { checkWidget (); if ((style&SWT.SEPARATOR) != 0) { int w, h; if ((style&SWT.HORIZONTAL)!= 0) { w = 45; h = 6; } else { // vertical w = 6; h = 45; } if (wHint != SWT.DEFAULT) w = wHint; if (hHint != SWT.DEFAULT) h = hHint; return new Point(w,h); } return super.computeSize(wHint, hHint, changed); } /** * Returns a value which describes the position of the * text or image in the receiver. The value will be one of * LEFT, RIGHT or CENTER * unless the receiver is a SEPARATOR label, in * which case, NONE is returned. * * @return the alignment * * @exception SWTException */ public int getAlignment () { checkWidget (); if ((style & SWT.SEPARATOR) != 0) return 0; if ((style & SWT.LEFT) != 0) return SWT.LEFT; if ((style & SWT.CENTER) != 0) return SWT.CENTER; if ((style & SWT.RIGHT) != 0) return SWT.RIGHT; return SWT.LEFT; } /** * Returns the receiver's image if it has one, or null * if it does not. * * @return the receiver's image * * @exception SWTException */ public Image getImage () { checkWidget (); return image; } String getNameText () { return getText (); } /** * Returns the receiver's text, which will be an empty * string if it has never been set or if the receiver is * a SEPARATOR label. * * @return the receiver's text * * @exception SWTException */ public String getText () { checkWidget (); if ((style & SWT.SEPARATOR) != 0) return ""; return text; } /** * Controls how text and images will be displayed in the receiver. * The argument should be one of LEFT, RIGHT * or CENTER. If the receiver is a SEPARATOR * label, the argument is ignored and the alignment is not changed. * * @param alignment the new alignment * * @exception SWTException */ public void setAlignment (int alignment) { checkWidget (); if ((style & SWT.SEPARATOR) != 0) return; if ((alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER)) == 0) return; style &= ~(SWT.LEFT | SWT.RIGHT | SWT.CENTER); style |= alignment & (SWT.LEFT | SWT.RIGHT | SWT.CENTER); boolean isText = OS.GTK_WIDGET_TYPE (handle) == OS.gtk_label_get_type (); if ((style & SWT.LEFT) != 0) { OS.gtk_misc_set_alignment (handle, 0.0f, 0.0f); if (isText) OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_LEFT); return; } if ((style & SWT.CENTER) != 0) { OS.gtk_misc_set_alignment (handle, 0.5f, 0.0f); if (isText) OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_CENTER); return; } if ((style & SWT.RIGHT) != 0) { OS.gtk_misc_set_alignment (handle, 1.0f, 0.0f); if (isText) OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_RIGHT); return; } } /** * Sets the receiver's image to the argument, which may be * null indicating that no image should be displayed. * * @param image the image to display on the receiver (may be null) * * @exception IllegalArgumentException * @exception SWTException */ public void setImage (Image image) { checkWidget (); this.image = image; if ((style & SWT.SEPARATOR) != 0) return; //NOT IMPLEMENTED - events and state of handle lost WidgetTable.remove (handle); OS.gtk_widget_destroy (handle); if (image == null) { handle = OS.gtk_label_new (new byte [1]); } else { handle = OS.gtk_pixmap_new (image.pixmap, image.mask); } OS.gtk_container_add (frameHandle, handle); WidgetTable.put (handle, this); int alignment = style & (SWT.LEFT | SWT.RIGHT | SWT.CENTER); switch (alignment) { case SWT.LEFT: OS.gtk_misc_set_alignment (handle, 0.0f, 0.0f); break; case SWT.CENTER: OS.gtk_misc_set_alignment (handle, 0.5f, 0.0f); break; case SWT.RIGHT: OS.gtk_misc_set_alignment (handle, 1.0f, 0.0f); break; } OS.gtk_widget_show (handle); } /** * Sets the receiver's text. *

* This method sets the widget label. The label may include * the mnemonic characters and line delimiters. *

* * @param string the new text * * @exception IllegalArgumentException * @exception SWTException */ public void setText (String string) { checkWidget(); if (string == null) error (SWT.ERROR_NULL_ARGUMENT); if ((style & SWT.SEPARATOR) != 0) return; text = string; boolean isText = OS.GTK_WIDGET_TYPE (handle) == OS.gtk_label_get_type (); if (!isText) { //NOT IMPLEMENTED - events and state of handle lost WidgetTable.remove (handle); OS.gtk_widget_destroy (handle); handle = OS.gtk_label_new (new byte [1]); OS.gtk_container_add (frameHandle, handle); WidgetTable.put (handle, this); int alignment = style & (SWT.LEFT | SWT.RIGHT | SWT.CENTER); switch (alignment) { case SWT.LEFT: OS.gtk_misc_set_alignment (handle, 0.0f, 0.0f); OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_LEFT); break; case SWT.CENTER: OS.gtk_misc_set_alignment (handle, 0.5f, 0.0f); OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_CENTER); break; case SWT.RIGHT: OS.gtk_misc_set_alignment (handle, 1.0f, 0.0f); OS.gtk_label_set_justify (handle, OS.GTK_JUSTIFY_RIGHT); break; } } OS.gtk_label_parse_uline (handle, string2bytesConvertMnemonic(string)); } }