/******************************************************************************* * Copyright (c) 2000, 2007 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.*; import org.eclipse.swt.events.*; import org.eclipse.swt.graphics.*; import org.eclipse.swt.internal.cocoa.*; /** * Instances of this class are selectable user interface * objects that allow the user to enter and modify numeric * values. *

* Note that although this class is a subclass of Composite, * it does not make sense to add children to it, or set a layout on it. *

*

*
Styles:
*
READ_ONLY, WRAP
*
Events:
*
Selection, Modify, Verify
*
*

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

* * @see Spinner snippets * @see SWT Example: ControlExample * @see Sample code and further information * * @since 3.1 */ public class Spinner extends Composite { NSTextField textView; NSStepper buttonView; int pageIncrement = 10; int digits = 0; static int GAP = 0; /** * 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#READ_ONLY * @see SWT#WRAP * @see Widget#checkSubclass * @see Widget#getStyle */ public Spinner (Composite parent, int style) { super (parent, checkStyle (style)); } /** * Adds the listener to the collection of listeners who will * be notified when the receiver's text is modified, by sending * it one of the messages defined in the ModifyListener * interface. * * @param listener the listener which should be notified * * @exception IllegalArgumentException * @exception SWTException * * @see ModifyListener * @see #removeModifyListener */ public void addModifyListener (ModifyListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (SWT.Modify, typedListener); } /** * 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 not called for texts. * widgetDefaultSelected is typically called when ENTER is pressed in a single-line text. *

* * @param listener the listener which should be notified when the control is selected by the user * * @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); } /** * Adds the listener to the collection of listeners who will * be notified when the receiver's text is verified, by sending * it one of the messages defined in the VerifyListener * interface. * * @param listener the listener which should be notified * * @exception IllegalArgumentException * @exception SWTException * * @see VerifyListener * @see #removeVerifyListener */ void addVerifyListener (VerifyListener listener) { checkWidget(); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (SWT.Verify, typedListener); } static int checkStyle (int style) { /* * Even though it is legal to create this widget * with scroll bars, they serve no useful purpose * because they do not automatically scroll the * widget's client area. The fix is to clear * the SWT style. */ return style & ~(SWT.H_SCROLL | SWT.V_SCROLL); } protected void checkSubclass () { if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS); } public Point computeSize (int wHint, int hHint, boolean changed) { checkWidget (); float width = 0, height = 0; String string = Double.toString (buttonView.maxValue ()); NSMutableDictionary dict = NSMutableDictionary.dictionaryWithCapacity (1); dict.setObject(textView.font (), OS.NSFontAttributeName ()); int length = string.length (); char [] chars = new char [length]; string.getChars (0, length, chars, 0); NSString nsString = NSString.stringWithCharacters (chars, length); NSAttributedString str = ((NSAttributedString) new NSAttributedString ().alloc ()).initWithString_attributes_ (nsString, dict); NSSize size = str.size (); str.release (); width = size.width; height = size.height; NSRect frameRect = textView.frame(); NSCell cell = new NSCell (textView.cell ()); NSRect cellRect = cell.drawingRectForBounds(frameRect); width += frameRect.width - cellRect.width; height += frameRect.height - cellRect.height; width += GAP; NSRect oldRect = buttonView.frame (); buttonView.sizeToFit(); NSRect newRect = buttonView.frame (); buttonView.setFrame (oldRect); width += newRect.width; height = Math.max (height, newRect.height); if (wHint != SWT.DEFAULT) width = wHint; if (hHint != SWT.DEFAULT) height = hHint; Rectangle trim = computeTrim (0, 0, (int) width, (int) height); return new Point (trim.width, trim.height); } /** * Copies the selected text. *

* The current selection is copied to the clipboard. *

* * @exception SWTException */ public void copy () { checkWidget (); // short [] selection = new short [2]; // if (OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection, null) != OS.noErr) return; // if (selection [0] == selection [1]) return; // int [] actualSize = new int [1]; // int [] ptr = new int [1]; // if (OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextCFStringTag, 4, ptr, actualSize) != OS.noErr) return; // CFRange range = new CFRange (); // range.location = selection [0]; // range.length = selection [1] - selection [0]; // char [] buffer= new char [range.length]; // OS.CFStringGetCharacters (ptr [0], range, buffer); // OS.CFRelease (ptr [0]); // copyToClipboard (buffer); } void createHandle () { SWTView widget = (SWTView)new SWTView().alloc(); widget.initWithFrame(new NSRect()); // widget.setDrawsBackground(false); widget.setTag(jniRef); NSStepper buttonWidget = (NSStepper)new SWTStepper().alloc(); buttonWidget.initWithFrame(new NSRect()); buttonWidget.setValueWraps((style & SWT.WRAP) != 0); buttonWidget.setTarget(buttonWidget); buttonWidget.setAction(OS.sel_sendSelection); buttonWidget.setTag(jniRef); NSTextField textWidget = (NSTextField)new SWTTextField().alloc(); textWidget.initWithFrame(new NSRect()); // textWidget.setTarget(widget); textWidget.setTag(jniRef); textWidget.setEditable((style & SWT.READ_ONLY) == 0); widget.addSubview_(textWidget); widget.addSubview_(buttonWidget); buttonView = buttonWidget; textView = textWidget; view = widget; parent.contentView().addSubview_(widget); setSelection (0, false, true, false); } /** * Cuts the selected text. *

* The current selection is first copied to the * clipboard and then deleted from the widget. *

* * @exception SWTException */ public void cut () { checkWidget (); if ((style & SWT.READ_ONLY) != 0) return; // short [] selection = new short [2]; // if (OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection, null) != OS.noErr) return; // if (selection [0] == selection [1]) return; // char [] buffer = setText ("", selection [0], selection [1], true); // if (buffer != null) { // copyToClipboard (buffer); // } } void enableWidget (boolean enabled) { buttonView.setEnabled(enabled); textView.setEnabled(enabled); } /** * Returns the number of decimal places used by the receiver. * * @return the digits * * @exception SWTException */ public int getDigits () { checkWidget (); return digits; } /** * Returns the amount that the receiver's value will be * modified by when the up/down arrows are pressed. * * @return the increment * * @exception SWTException */ public int getIncrement () { checkWidget (); return (int)buttonView.increment(); } /** * Returns the maximum value which the receiver will allow. * * @return the maximum * * @exception SWTException */ public int getMaximum () { checkWidget (); return (int)buttonView.maxValue(); } /** * Returns the minimum value which the receiver will allow. * * @return the minimum * * @exception SWTException */ public int getMinimum () { checkWidget (); return (int)buttonView.minValue(); } /** * Returns the amount that the receiver's position will be * modified by when the page up/down keys are pressed. * * @return the page increment * * @exception SWTException */ public int getPageIncrement () { checkWidget (); return pageIncrement; } /** * Returns the selection, which is the receiver's position. * * @return the selection * * @exception SWTException */ public int getSelection () { checkWidget (); return (int)((NSStepper)buttonView).doubleValue(); } int getSelectionText () { return -1; } /** * Pastes text from clipboard. *

* The selected text is deleted from the widget * and new text inserted from the clipboard. *

* * @exception SWTException */ public void paste () { checkWidget (); if ((style & SWT.READ_ONLY) != 0) return; // String text = getClipboardText (); // short [] selection = new short [2]; // if (OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection, null) != OS.noErr) return; // setText (text, selection [0], selection [1], true); } void releaseHandle () { super.releaseHandle(); if (buttonView != null) { buttonView.setTag(-1); buttonView.release(); } if (textView != null) { textView.setTag(-1); textView.release(); } buttonView = null; textView = null; } /** * Removes the listener from the collection of listeners who will * be notified when the receiver's text is modified. * * @param listener the listener which should no longer be notified * * @exception IllegalArgumentException * @exception SWTException * * @see ModifyListener * @see #addModifyListener */ public void removeModifyListener (ModifyListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Modify, listener); } /** * 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); } /** * Removes the listener from the collection of listeners who will * be notified when the control is verified. * * @param listener the listener which should be notified * * @exception IllegalArgumentException * @exception SWTException * * @see VerifyListener * @see #addVerifyListener */ void removeVerifyListener (VerifyListener listener) { checkWidget (); if (listener == null) error (SWT.ERROR_NULL_ARGUMENT); if (eventTable == null) return; eventTable.unhook (SWT.Verify, listener); } void sendSelection () { setSelection (getSelection(), false, true, true); } int setBounds (int x, int y, int width, int height, boolean move, boolean resize) { int result = super.setBounds(x, y, width, height, move, resize); if ((result & RESIZED) != 0) { buttonView.sizeToFit(); NSRect buttonFrame = buttonView.bounds(); NSRect frame = view.frame(); buttonFrame.x = frame.width - buttonFrame.width; buttonFrame.y = 0; frame.x = 0; frame.y = 0; frame.width -= buttonFrame.width + GAP; textView.setFrame(frame); buttonView.setFrame(buttonFrame); } return result; } /** * Sets the number of decimal places used by the receiver. *

* The digit setting is used to allow for floating point values in the receiver. * For example, to set the selection to a floating point value of 1.37 call setDigits() with * a value of 2 and setSelection() with a value of 137. Similarly, if getDigits() has a value * of 2 and getSelection() returns 137 this should be interpreted as 1.37. This applies to all * numeric APIs. *

* * @param value the new digits (must be greater than or equal to zero) * * @exception IllegalArgumentException * @exception SWTException */ public void setDigits (int value) { checkWidget (); if (value < 0) error (SWT.ERROR_INVALID_ARGUMENT); // if (value == digits) return; // digits = value; // int pos = OS.GetControl32BitValue (buttonHandle); // setSelection (pos, false, true, false); } /** * Sets the amount that the receiver's value will be * modified by when the up/down arrows are pressed to * the argument, which must be at least one. * * @param value the new increment (must be greater than zero) * * @exception SWTException */ public void setIncrement (int value) { checkWidget (); if (value < 1) return; buttonView.setIncrement(value); } /** * Sets the maximum value that the receiver will allow. This new * value will be ignored if it is not greater than the receiver's current * minimum value. If the new maximum is applied then the receiver's * selection value will be adjusted if necessary to fall within its new range. * * @param value the new maximum, which must be greater than the current minimum * * @exception SWTException */ public void setMaximum (int value) { checkWidget (); if (value < 0) return; int min = getMinimum (); if (value <= min) return; int pos = getSelection(); buttonView.setMaxValue(value); if (pos > value) setSelection (value, true, true, false); } /** * Sets the minimum value that the receiver will allow. This new * value will be ignored if it is not less than the receiver's * current maximum value. If the new minimum is applied then the receiver's * selection value will be adjusted if necessary to fall within its new range. * * @param value the new minimum, which must be less than the current maximum * * @exception SWTException */ public void setMinimum (int value) { checkWidget (); if (value < 0) return; int max = getMaximum(); if (value >= max) return; int pos = getSelection(); buttonView.setMinValue(value); if (pos < value) setSelection (value, true, true, false); } /** * Sets the amount that the receiver's position will be * modified by when the page up/down keys are pressed * to the argument, which must be at least one. * * @param value the page increment (must be greater than zero) * * @exception SWTException */ public void setPageIncrement (int value) { checkWidget (); if (value < 1) return; pageIncrement = value; } /** * Sets the selection, which is the receiver's * position, to the argument. If the argument is not within * the range specified by minimum and maximum, it will be * adjusted to fall within this range. * * @param value the new selection (must be zero or greater) * * @exception SWTException */ public void setSelection (int value) { checkWidget (); int min = getMinimum(); int max = getMaximum(); value = Math.min (Math.max (min, value), max); setSelection (value, true, true, false); } void setSelection (int value, boolean setPos, boolean setText, boolean notify) { if (setPos) { ((NSStepper)buttonView).setDoubleValue(value); } if (setText) { String string = String.valueOf (value); if (digits > 0) { String decimalSeparator = ".";//getDecimalSeparator (); int index = string.length () - digits; StringBuffer buffer = new StringBuffer (); if (index > 0) { buffer.append (string.substring (0, index)); buffer.append (decimalSeparator); buffer.append (string.substring (index)); } else { buffer.append ("0"); buffer.append (decimalSeparator); while (index++ < 0) buffer.append ("0"); buffer.append (string); } string = buffer.toString (); } NSCell cell = new NSCell(textView.cell()); if (hooks (SWT.Verify) || filters (SWT.Verify)) { int length = cell.title().length(); string = verifyText (string, 0, length, null); if (string == null) return; } cell.setTitle(NSString.stringWith(string)); // short [] selection = new short [] {0, (short)string.length ()}; // OS.SetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection); sendEvent (SWT.Modify); } if (notify) postEvent (SWT.Selection); } /** * Sets the receiver's selection, minimum value, maximum * value, digits, increment and page increment all at once. *

* Note: This is similar to setting the values individually * using the appropriate methods, but may be implemented in a * more efficient fashion on some platforms. *

* * @param selection the new selection value * @param minimum the new minimum value * @param maximum the new maximum value * @param digits the new digits value * @param increment the new increment value * @param pageIncrement the new pageIncrement value * * @exception SWTException * * @since 3.2 */ public void setValues (int selection, int minimum, int maximum, int digits, int increment, int pageIncrement) { checkWidget (); if (minimum < 0) return; if (maximum <= minimum) return; if (digits < 0) return; if (increment < 1) return; if (pageIncrement < 1) return; selection = Math.min (Math.max (minimum, selection), maximum); this.pageIncrement = pageIncrement; this.digits = digits; buttonView.setIncrement(increment); buttonView.setMaxValue(maximum); buttonView.setMinValue(minimum); setSelection (selection, true, true, false); } String verifyText (String string, int start, int end, Event keyEvent) { Event event = new Event (); event.text = string; event.start = start; event.end = end; if (keyEvent != null) { event.character = keyEvent.character; event.keyCode = keyEvent.keyCode; event.stateMask = keyEvent.stateMask; } int index = 0; if (digits > 0) { String decimalSeparator = ".";//getDecimalSeparator (); index = string.indexOf (decimalSeparator); if (index != -1) { string = string.substring (0, index) + string.substring (index + 1); } index = 0; } while (index < string.length ()) { if (!Character.isDigit (string.charAt (index))) break; index++; } event.doit = index == string.length (); /* * It is possible (but unlikely), that application * code could have disposed the widget in the verify * event. If this happens, answer null to cancel * the operation. */ sendEvent (SWT.Verify, event); if (!event.doit || isDisposed ()) return null; return event.text; } }