/******************************************************************************* * 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.*; import org.eclipse.swt.internal.photon.*; import org.eclipse.swt.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.events.*; /** * Instances of this class represent a selectable user interface object that * issues notification when pressed and released. *
*
Styles:
*
ARROW, CHECK, PUSH, RADIO, TOGGLE, FLAT
*
UP, DOWN, LEFT, RIGHT, CENTER
*
Events:
*
Selection
*
*

* Note: Only one of the styles ARROW, CHECK, PUSH, RADIO, and TOGGLE * may be specified. *

* Note: Only one of the styles LEFT, RIGHT, and CENTER may be specified. *

* Note: Only one of the styles UP, DOWN, LEFT, and RIGHT may be specified * when the ARROW style is specified. *

* IMPORTANT: This class is not intended to be subclassed. *

* * @see Button snippets * @see SWT Example: ControlExample * @see Sample code and further information * @noextend This class is not intended to be subclassed by clients. */ public class Button extends Control { String text = ""; Image image; boolean grayed; /** * 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 * lists the style constants that are applicable to the class. * Style bits are also inherited from superclasses. *

* * @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#ARROW * @see SWT#CHECK * @see SWT#PUSH * @see SWT#RADIO * @see SWT#TOGGLE * @see SWT#FLAT * @see SWT#UP * @see SWT#DOWN * @see SWT#LEFT * @see SWT#RIGHT * @see SWT#CENTER * @see Widget#checkSubclass * @see Widget#getStyle */ public Button (Composite parent, int style) { super (parent, checkStyle (style)); } static int checkStyle (int style) { style = checkBits (style, SWT.PUSH, SWT.ARROW, SWT.CHECK, SWT.RADIO, SWT.TOGGLE, 0); if ((style & (SWT.PUSH | SWT.TOGGLE)) != 0) { return checkBits (style, SWT.CENTER, SWT.LEFT, SWT.RIGHT, 0, 0, 0); } if ((style & (SWT.CHECK | SWT.RADIO)) != 0) { return checkBits (style, SWT.LEFT, SWT.RIGHT, SWT.CENTER, 0, 0, 0); } if ((style & SWT.ARROW) != 0) { style |= SWT.NO_FOCUS; return checkBits (style, SWT.UP, SWT.DOWN, SWT.LEFT, SWT.RIGHT, 0, 0); } return style; } /** * Adds the listener to the collection of listeners who will * be notified when the control is selected by the user, by sending * it one of the messages defined in the SelectionListener * interface. *

* widgetSelected is called when the control is selected by the user. * widgetDefaultSelected is not called. *

*

* When the SWT.RADIO style bit is set, the widgetSelected method is * also called when the receiver loses selection because another item in the same radio group * was selected by the user. During widgetSelected the application can use * getSelection() to determine the current selected state of the receiver. *

* * @param listener the listener which should be notified * * @exception IllegalArgumentException * @exception SWTException * * @see SelectionListener * @see #removeSelectionListener * @see SelectionEvent */ public void addSelectionListener (SelectionListener listener) { checkWidget(); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (SWT.Selection,typedListener); addListener (SWT.DefaultSelection,typedListener); } void click () { click (handle); } public Point computeSize (int wHint, int hHint, boolean changed) { checkWidget(); int border = getBorderWidth (); int width = border * 2, height = border * 2; if ((style & SWT.ARROW) != 0) { if (wHint != SWT.DEFAULT) width += wHint; else width = 17; if (hHint != SWT.DEFAULT) height += hHint; else height = 17; return new Point (width, height); } PhDim_t dim = new PhDim_t(); if (!OS.PtWidgetIsRealized (handle)) OS.PtExtentWidget (handle); OS.PtWidgetPreferredSize(handle, dim); width = dim.w; height = dim.h; if (wHint != SWT.DEFAULT || hHint != SWT.DEFAULT) { int [] args = { OS.Pt_ARG_MARGIN_LEFT, 0, 0, // 1 OS.Pt_ARG_MARGIN_RIGHT, 0, 0, // 4 OS.Pt_ARG_MARGIN_TOP, 0, 0, // 7 OS.Pt_ARG_MARGIN_BOTTOM, 0, 0, // 10 // OS.Pt_ARG_MARGIN_WIDTH, 0, 0, // 13 // OS.Pt_ARG_MARGIN_HEIGHT, 0, 0, // 16 }; OS.PtGetResources (handle, args.length / 3, args); PhArea_t area = new PhArea_t (); area.size_w = (short) wHint; area.size_h = (short) hHint; /* * This code is intentionally commented. Bug compatible with Windows. */ // PhRect_t rect = new PhRect_t (); // rect.lr_x = (short) (wHint - 1); // rect.lr_y = (short) (hHint - 1); // OS.PtSetAreaFromWidgetCanvas (handle, rect, area); if (wHint != SWT.DEFAULT) { width = area.size_w + /*(args [13] * 2)*/ + args [1] + args [4]; } if (hHint != SWT.DEFAULT) { height = area.size_h + /*(args [16] * 2)*/ + args [7] + args [10]; } } return new Point (width, height); } void createHandle (int index) { state |= HANDLE; int parentHandle = parent.parentingHandle (); /* ARROW button */ if ((style & SWT.ARROW) != 0) { int [] args = { OS.Pt_ARG_FLAGS, 0, OS.Pt_GETS_FOCUS, OS.Pt_ARG_BASIC_FLAGS, OS.Pt_HORIZONTAL_GRADIENT, OS.Pt_STATIC_GRADIENT | OS.Pt_HORIZONTAL_GRADIENT, OS.Pt_ARG_RESIZE_FLAGS, 0, OS.Pt_RESIZE_XY_BITS, }; handle = OS.PtCreateWidget (display.PtButton, parentHandle, args.length / 3, args); if (handle == 0) error (SWT.ERROR_NO_HANDLES); return; } /* Compute alignment */ int alignment = OS.Pt_LEFT; if ((style & SWT.CENTER) != 0) alignment = OS.Pt_CENTER; if ((style & SWT.RIGHT) != 0) alignment = OS.Pt_RIGHT; /* CHECK or RADIO button */ if ((style & (SWT.CHECK | SWT.RADIO)) != 0) { int [] args = { OS.Pt_ARG_HORIZONTAL_ALIGNMENT, alignment, 0, OS.Pt_ARG_INDICATOR_TYPE, (style & SWT.CHECK) != 0 ? OS.Pt_N_OF_MANY : OS.Pt_ONE_OF_MANY, 0, OS.Pt_ARG_FILL_COLOR, display.WIDGET_BACKGROUND, 0, OS.Pt_ARG_RESIZE_FLAGS, 0, OS.Pt_RESIZE_XY_BITS, }; handle = OS.PtCreateWidget (display.PtToggleButton, parentHandle, args.length / 3, args); if (handle == 0) error (SWT.ERROR_NO_HANDLES); return; } /* PUSH or TOGGLE button */ int [] args = { OS.Pt_ARG_FLAGS, (style & SWT.TOGGLE) != 0 ? OS.Pt_TOGGLE : 0, OS.Pt_TOGGLE, OS.Pt_ARG_HORIZONTAL_ALIGNMENT, alignment, 0, OS.Pt_ARG_RESIZE_FLAGS, 0, OS.Pt_RESIZE_XY_BITS, }; handle = OS.PtCreateWidget (display.PtButton, parentHandle, args.length / 3, args); if (handle == 0) error (SWT.ERROR_NO_HANDLES); } void drawWidget (int widget, int damage) { super.drawWidget (widget, damage); if ((style & SWT.ARROW) != 0) { PhRect_t rect = new PhRect_t (); OS.PtCalcCanvas (handle, rect); int flags = 0; if ((style & SWT.RIGHT) != 0) flags = 2; if ((style & SWT.LEFT) != 0) flags = 1; if ((style & SWT.DOWN) != 0) flags = 8; if ((style & SWT.UP) != 0) flags = 4; OS.PgDrawArrow (rect, (short)0, 0x000000, flags); } } /** * 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 an ARROW button, in * which case, the alignment will indicate the direction of * the arrow (one of LEFT, RIGHT, * UP or DOWN). * * @return the alignment * * @exception SWTException */ public int getAlignment () { checkWidget(); if ((style & SWT.ARROW) != 0) { if ((style & SWT.UP) != 0) return SWT.UP; if ((style & SWT.DOWN) != 0) return SWT.DOWN; if ((style & SWT.LEFT) != 0) return SWT.LEFT; if ((style & SWT.RIGHT) != 0) return SWT.RIGHT; return SWT.UP; } 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; } boolean getDefault () { if ((style & SWT.PUSH) == 0) return false; int [] args = {OS.Pt_ARG_BEVEL_CONTRAST, 0, 0}; OS.PtGetResources (handle, args.length / 3, args); return args [1] == 100; } /** * Returns true if the receiver is grayed, * and false otherwise. When the widget does not have * the CHECK style, return false. * * @return the grayed state of the checkbox * * @exception SWTException * * @since 3.4 */ public boolean getGrayed () { checkWidget(); if ((style & SWT.CHECK) == 0) return false; return grayed; } /** * 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; } /** * Returns true if the receiver is selected, * and false otherwise. *

* When the receiver is of type CHECK or RADIO, * it is selected when it is checked. When it is of type TOGGLE, * it is selected when it is pushed in. If the receiver is of any other type, * this method returns false. * * @return the selection state * * @exception SWTException

*/ public boolean getSelection () { checkWidget(); if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) == 0) return false; return (OS.PtWidgetFlags (handle) & OS.Pt_SET) != 0; } 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 * an ARROW button. * * @return the receiver's text * * @exception SWTException */ public String getText () { checkWidget(); if ((style & SWT.ARROW) != 0) return ""; return text; } void hookEvents () { super.hookEvents (); int windowProc = display.windowProc; OS.PtAddCallback (handle, OS.Pt_CB_ACTIVATE, windowProc, OS.Pt_CB_ACTIVATE); } int hotkeyProc (int widget, int data, int info) { if (setFocus ()) click (); return OS.Pt_CONTINUE; } int Pt_CB_ACTIVATE (int widget, int info) { if ((style & SWT.RADIO) != 0) { if ((parent.getStyle () & SWT.NO_RADIO_GROUP) == 0) { selectRadio (); } } postEvent (SWT.Selection); return OS.Pt_CONTINUE; } int Pt_CB_GOT_FOCUS (int widget, int info) { int result = super.Pt_CB_GOT_FOCUS (widget, info); // widget could be disposed at this point if (handle == 0) return result; if ((style & SWT.PUSH) == 0) return result; getShell ().setDefaultButton (this, false); return result; } int Pt_CB_LOST_FOCUS (int widget, int info) { int result = super.Pt_CB_LOST_FOCUS (widget, info); // widget could be disposed at this point if (handle == 0) return result; if ((style & SWT.PUSH) == 0) return result; if (getDefault ()) { getShell ().setDefaultButton (null, false); } return result; } void releaseWidget () { super.releaseWidget (); image = null; } /** * Removes the listener from the collection of listeners who will * be notified when the control is selected by the user. * * @param listener the listener which should no longer be notified * * @exception IllegalArgumentException * @exception SWTException * * @see SelectionListener * @see #addSelectionListener */ public void removeSelectionListener (SelectionListener listener) { checkWidget(); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Selection, listener); eventTable.unhook (SWT.DefaultSelection,listener); } void selectRadio () { /* * This code is intentionally commented. When two groups * of radio buttons with the same parent are separated by * another control, the correct behavior should be that * the two groups act independently. This is consistent * with radio tool and menu items. The commented code * implements this behavior. */ // int index = 0; // Control [] children = parent._getChildren (); // while (index < children.length && children [index] != this) index++; // int i = index - 1; // while (i >= 0 && children [i].setRadioSelection (false)) --i; // int j = index + 1; // while (j < children.length && children [j].setRadioSelection (false)) j++; // setSelection (true); Control [] children = parent._getChildren (); for (int i=0; iLEFT, RIGHT or CENTER * unless the receiver is an ARROW button, in * which case, the argument indicates the direction of * the arrow (one of LEFT, RIGHT, * UP or DOWN). * * @param alignment the new alignment * * @exception SWTException */ public void setAlignment (int alignment) { checkWidget(); if ((style & SWT.ARROW) != 0) { if ((style & (SWT.UP | SWT.DOWN | SWT.LEFT | SWT.RIGHT)) == 0) return; style &= ~(SWT.UP | SWT.DOWN | SWT.LEFT | SWT.RIGHT); style |= alignment & (SWT.UP | SWT.DOWN | SWT.LEFT | SWT.RIGHT); OS.PtDamageWidget (handle); 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 align = OS.Pt_LEFT; if ((style & SWT.CENTER) != 0) align = OS.Pt_CENTER; if ((style & SWT.RIGHT) != 0) align = OS.Pt_RIGHT; OS.PtSetResource (handle, OS.Pt_ARG_HORIZONTAL_ALIGNMENT, align, 0); } void setDefault (boolean value) { if ((style & SWT.PUSH) == 0) return; if (getShell ().parent == null) return; OS.PtSetResource (handle, OS.Pt_ARG_BEVEL_CONTRAST, value ? 100 : 20, 0); } boolean setRadioSelection (boolean value) { if ((style & SWT.RADIO) == 0) return false; if (getSelection () != value) { setSelection (value); postEvent (SWT.Selection); } return true; } /** * Sets the selection state of the receiver, if it is of type CHECK, * RADIO, or TOGGLE. * *

* When the receiver is of type CHECK or RADIO, * it is selected when it is checked. When it is of type TOGGLE, * it is selected when it is pushed in. * * @param selected the new selection state * * @exception SWTException

*/ public void setSelection (boolean selected) { checkWidget(); if ((style & (SWT.CHECK | SWT.RADIO | SWT.TOGGLE)) == 0) return; OS.PtSetResource (handle, OS.Pt_ARG_FLAGS, selected ? OS.Pt_SET : 0, OS.Pt_SET); } /** * Sets the grayed state of the receiver. This state change * only applies if the control was created with the SWT.CHECK * style. * * @param grayed the new grayed state * * @exception SWTException * * @since 3.4 */ public void setGrayed (boolean grayed) { checkWidget (); if ((style & SWT.CHECK) == 0) return; this.grayed = grayed; } /** * Sets the receiver's image to the argument, which may be * null indicating that no image should be displayed. *

* Note that a Button can display an image and text simultaneously * on Windows (starting with XP), GTK+ and OSX. On other platforms, * a Button that has an image and text set into it will display the * image or text that was set most recently. *

* @param image the image to display on the receiver (may be null) * * @exception IllegalArgumentException * @exception SWTException */ public void setImage (Image image) { checkWidget(); if ((style & SWT.ARROW) != 0) return; this.image = image; int imageHandle = 0; if (image != null) { if (image.isDisposed()) error(SWT.ERROR_INVALID_ARGUMENT); imageHandle = copyPhImage (image.handle); } int [] args = { OS.Pt_ARG_LABEL_IMAGE, imageHandle, 0, OS.Pt_ARG_LABEL_TYPE, OS.Pt_IMAGE, 0 }; OS.PtSetResources (handle, args.length / 3, args); if (imageHandle != 0) OS.free (imageHandle); } /** * Sets the receiver's text. *

* This method sets the button label. The label may include * the mnemonic character but must not contain line delimiters. *

*

* 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, a selection * event occurs. On most platforms, the mnemonic appears * underlined but may be emphasized in a platform specific * manner. The mnemonic indicator character '&' can be * escaped by doubling it in the string, causing a single * '&' to be displayed. *

* Note that a Button can display an image and text simultaneously * on Windows (starting with XP), GTK+ and OSX. On other platforms, * a Button that has an image and text set into it will display the * image or text that was set most recently. *

* @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.ARROW) != 0) return; text = string; char [] text = new char [string.length ()]; string.getChars (0, text.length, text, 0); char mnemonic = fixMnemonic (text); byte [] buffer = Converter.wcsToMbcs (null, text, true); int ptr1 = OS.malloc (buffer.length); OS.memmove (ptr1, buffer, buffer.length); int ptr2 = 0; if (mnemonic != 0) { byte [] buffer2 = Converter.wcsToMbcs (null, new char []{mnemonic}, true); ptr2 = OS.malloc (buffer2.length); OS.memmove (ptr2, buffer2, buffer2.length); } replaceMnemonic (mnemonic, true, true); int [] args = { OS.Pt_ARG_TEXT_STRING, ptr1, 0, OS.Pt_ARG_LABEL_TYPE, OS.Pt_Z_STRING, 0, OS.Pt_ARG_ACCEL_KEY, ptr2, 0, }; OS.PtSetResources (handle, args.length / 3, args); OS.free (ptr1); OS.free (ptr2); } int traversalCode (int key_sym, PhKeyEvent_t ke) { int code = super.traversalCode (key_sym, ke); if ((style & SWT.RADIO) != 0) code |= SWT.TRAVERSE_ARROW_NEXT | SWT.TRAVERSE_ARROW_PREVIOUS; return code; } int widgetClass () { if ((style & (SWT.CHECK | SWT.RADIO)) != 0) return OS.PtToggleButton (); return OS.PtButton (); } }