| /******************************************************************************* |
| * Copyright (c) 2000, 2003 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are 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 |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.swt.widgets; |
| |
| |
| import org.eclipse.swt.internal.carbon.ControlFontStyleRec; |
| import org.eclipse.swt.internal.carbon.FontInfo; |
| import org.eclipse.swt.internal.carbon.OS; |
| |
| import org.eclipse.swt.*; |
| 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. |
| * <dl> |
| * <dt><b>Styles:</b></dt> |
| * <dd>SEPARATOR, HORIZONTAL, VERTICAL</dd> |
| * <dd>SHADOW_IN, SHADOW_OUT, SHADOW_NONE</dd> |
| * <dd>CENTER, LEFT, RIGHT, WRAP</dd> |
| * <dt><b>Events:</b></dt> |
| * <dd>(none)</dd> |
| * </dl> |
| * <p> |
| * Note: Only one of SHADOW_IN, SHADOW_OUT and SHADOW_NONE may be specified. |
| * SHADOW_NONE is a HINT. Only one of HORIZONTAL and VERTICAL may be specified. |
| * Only one of CENTER, LEFT and RIGHT may be specified. |
| * </p><p> |
| * IMPORTANT: This class is intended to be subclassed <em>only</em> |
| * within the SWT implementation. |
| * </p> |
| */ |
| public class Label extends Control { |
| String text = ""; |
| Image image; |
| boolean isImage; |
| |
| /** |
| * 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#SEPARATOR |
| * @see SWT#HORIZONTAL |
| * @see SWT#VERTICAL |
| * @see SWT#SHADOW_IN |
| * @see SWT#SHADOW_OUT |
| * @see SWT#SHADOW_NONE |
| * @see SWT#CENTER |
| * @see SWT#LEFT |
| * @see SWT#RIGHT |
| * @see SWT#WRAP |
| * @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) { |
| style = checkBits (style, SWT.VERTICAL, SWT.HORIZONTAL, 0, 0, 0, 0); |
| return checkBits (style, SWT.SHADOW_OUT, SWT.SHADOW_IN, SWT.SHADOW_NONE, 0, 0, 0); |
| } |
| return checkBits (style, SWT.LEFT, SWT.CENTER, SWT.RIGHT, 0, 0, 0); |
| } |
| |
| public Point computeSize (int wHint, int hHint, boolean changed) { |
| checkWidget(); |
| int width = 0, height = 0; |
| if ((style & SWT.SEPARATOR) != 0) { |
| if ((style & SWT.HORIZONTAL) != 0) { |
| width = DEFAULT_WIDTH; |
| height = 3; |
| } else { |
| width = 3; |
| height = DEFAULT_HEIGHT; |
| } |
| } else { |
| if (isImage && image != null) { |
| Rectangle r = image.getBounds (); |
| width = r.width; |
| height = r.height; |
| } else { |
| width = DEFAULT_WIDTH; |
| int length = text.length (); |
| if (length != 0) { |
| int [] ptr = new int [1]; |
| OS.GetControlData (handle, (short) 0 , OS.kControlStaticTextCFStringTag, 4, ptr, null); |
| if (ptr [0] != 0) { |
| org.eclipse.swt.internal.carbon.Point ioBounds = new org.eclipse.swt.internal.carbon.Point (); |
| if ((style & SWT.WRAP) != 0 && wHint != SWT.DEFAULT) ioBounds.h = (short) wHint; |
| if (font == null) { |
| OS.GetThemeTextDimensions (ptr [0], (short)OS.kThemePushButtonFont, OS.kThemeStateActive, ioBounds.h != 0, ioBounds, null); |
| } else { |
| int [] currentPort = new int [1]; |
| OS.GetPort (currentPort); |
| OS.SetPortWindowPort (OS.GetControlOwner (handle)); |
| OS.TextFont (font.id); |
| OS.TextFace (font.style); |
| OS.TextSize (font.size); |
| OS.GetThemeTextDimensions (ptr [0], (short) OS.kThemeCurrentPortFont, OS.kThemeStateActive, ioBounds.h != 0, ioBounds, null); |
| OS.SetPort (currentPort [0]); |
| } |
| width = ioBounds.h; |
| height = ioBounds.v; |
| } |
| OS.CFRelease (ptr [0]); |
| } else { |
| Font font = getFont (); |
| FontInfo info = new FontInfo (); |
| OS.FetchFontInfo(font.id, font.size, font.style, info); |
| int fontHeight = info.ascent + info.descent; |
| height = fontHeight; |
| } |
| } |
| } |
| if (wHint != SWT.DEFAULT) width = wHint; |
| if (hHint != SWT.DEFAULT) height = hHint; |
| return new Point (width, height); |
| } |
| |
| void createHandle () { |
| state |= GRAB; |
| int [] outControl = new int [1]; |
| int window = OS.GetControlOwner (parent.handle); |
| if ((style & SWT.SEPARATOR) != 0) { |
| OS.CreateSeparatorControl (window, null, outControl); |
| } else { |
| int just = OS.teFlushLeft; |
| if ((style & SWT.CENTER) != 0) just = OS.teCenter; |
| if ((style & SWT.RIGHT) != 0) just = OS.teFlushRight; |
| ControlFontStyleRec fontStyle = new ControlFontStyleRec (); |
| fontStyle.flags |= OS.kControlUseJustMask; |
| fontStyle.just = (short) just; |
| OS.CreateStaticTextControl (window, null, 0, fontStyle, outControl); |
| } |
| if (outControl [0] == 0) error (SWT.ERROR_NO_HANDLES); |
| handle = outControl [0]; |
| } |
| |
| void drawBackground (int control) { |
| if ((style & SWT.SEPARATOR) != 0) { |
| drawBackground (control, background); |
| } |
| } |
| |
| void drawWidget (int control, int damageRgn, int visibleRgn, int theEvent) { |
| if (isImage && image != null) { |
| GCData data = new GCData (); |
| data.paintEvent = theEvent; |
| data.visibleRgn = visibleRgn; |
| GC gc = GC.carbon_new (this, data); |
| int x = 0; |
| Point size = getSize (); |
| Rectangle bounds = image.getBounds (); |
| if ((style & SWT.CENTER) != 0) x = (size.x - bounds.width) / 2; |
| if ((style & SWT.RIGHT) != 0) x = size.x - bounds.width; |
| gc.drawImage (image, x, 0); |
| gc.dispose (); |
| } |
| super.drawWidget (control, damageRgn, visibleRgn, theEvent); |
| } |
| |
| /** |
| * Returns a value which describes the position of the |
| * text or image in the receiver. The value will be one of |
| * <code>LEFT</code>, <code>RIGHT</code> or <code>CENTER</code> |
| * unless the receiver is a <code>SEPARATOR</code> label, in |
| * which case, <code>NONE</code> is returned. |
| * |
| * @return the alignment |
| * |
| * @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 int getAlignment () { |
| checkWidget(); |
| if ((style & SWT.SEPARATOR) != 0) return SWT.LEFT; |
| if ((style & SWT.CENTER) != 0) return SWT.CENTER; |
| if ((style & SWT.RIGHT) != 0) return SWT.RIGHT; |
| return SWT.LEFT; |
| } |
| |
| public int getBorderWidth () { |
| checkWidget(); |
| return (style & SWT.BORDER) != 0 ? 1 : 0; |
| } |
| |
| /** |
| * Returns the receiver's image if it has one, or null |
| * if it does not. |
| * |
| * @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; |
| } |
| |
| 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 <code>SEPARATOR</code> label. |
| * |
| * @return the receiver's text |
| * |
| * @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 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 <code>LEFT</code>, <code>RIGHT</code> |
| * or <code>CENTER</code>. If the receiver is a <code>SEPARATOR</code> |
| * label, the argument is ignored and the alignment is not changed. |
| * |
| * @param alignment the new alignment |
| * |
| * @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 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); |
| int just = OS.teFlushLeft; |
| if ((alignment & SWT.CENTER) != 0) just = OS.teCenter; |
| if ((alignment & SWT.RIGHT) != 0) just = OS.teFlushRight; |
| ControlFontStyleRec fontStyle = new ControlFontStyleRec (); |
| OS.GetControlData (handle, (short) OS.kControlEntireControl, OS.kControlFontStyleTag, ControlFontStyleRec.sizeof, fontStyle, null); |
| fontStyle.flags |= OS.kControlUseJustMask; |
| fontStyle.just = (short) just; |
| OS.SetControlFontStyle (handle, fontStyle); |
| redraw (); |
| } |
| |
| /** |
| * 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 <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 ((style & SWT.SEPARATOR) != 0) return; |
| if (image != null && image.isDisposed ()) { |
| error (SWT.ERROR_INVALID_ARGUMENT); |
| } |
| this.image = image; |
| isImage = true; |
| redraw (); |
| } |
| |
| /** |
| * Sets the receiver's text. |
| * <p> |
| * This method sets the widget label. The label may include |
| * the mnemonic character and line delimiters. |
| * </p> |
| * <p> |
| * Mnemonics are indicated by an '&' that causes the next |
| * character to be the mnemonic. When the user presses a |
| * key sequence that matches the mnemonic, focus is assigned |
| * to the control that follows the label. On most platforms, |
| * the mnemonic appears underlined but may be emphasised in a |
| * platform specific manner. The mnemonic indicator character |
| *'&' can be escaped by doubling it in the string, causing |
| * a single '&' to be displayed. |
| * </p> |
| * |
| * @param string the new text |
| * |
| * @exception IllegalArgumentException <ul> |
| * <li>ERROR_NULL_ARGUMENT - if the text 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 setText (String string) { |
| checkWidget(); |
| if (string == null) error (SWT.ERROR_NULL_ARGUMENT); |
| if ((style & SWT.SEPARATOR) != 0) return; |
| isImage = false; |
| text = string; |
| char [] buffer = new char [text.length ()]; |
| text.getChars (0, buffer.length, buffer, 0); |
| int i=0, j=0; |
| while (i < buffer.length) { |
| if ((buffer [j++] = buffer [i++]) == Mnemonic) { |
| if (i == buffer.length) {continue;} |
| if (buffer [i] == Mnemonic) {i++; continue;} |
| j--; |
| } |
| } |
| int ptr = OS.CFStringCreateWithCharacters (OS.kCFAllocatorDefault, buffer, j); |
| if (ptr == 0) error (SWT.ERROR_CANNOT_SET_TEXT); |
| OS.SetControlData (handle, 0 , OS.kControlStaticTextCFStringTag, 4, new int[]{ptr}); |
| OS.CFRelease (ptr); |
| redraw (); |
| } |
| |
| } |