Mercurial > projects > dwt-mac
view dwt/widgets/Spinner.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 649b8e223d5a |
line wrap: on
line source
/******************************************************************************* * 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 *******************************************************************************/ module dwt.widgets.Spinner; import dwt.dwthelper.utils; import dwt.DWT; import dwt.DWTException; import dwt.events.ModifyListener; import dwt.events.SelectionEvent; import dwt.events.SelectionListener; import dwt.events.VerifyListener; import dwt.graphics.Point; import dwt.graphics.Rectangle; import dwt.internal.cocoa.NSAttributedString; import dwt.internal.cocoa.NSCell; import dwt.internal.cocoa.NSMutableDictionary; import dwt.internal.cocoa.NSRect; import dwt.internal.cocoa.NSSize; import dwt.internal.cocoa.NSStepper; import dwt.internal.cocoa.NSString; import dwt.internal.cocoa.NSTextField; import dwt.internal.cocoa.OS; import dwt.internal.cocoa.SWTStepper; import dwt.internal.cocoa.SWTTextField; import dwt.internal.cocoa.SWTView; /** * Instances of this class are selectable user interface * objects that allow the user to enter and modify numeric * values. * <p> * Note that although this class is a subclass of <code>Composite</code>, * it does not make sense to add children to it, or set a layout on it. * </p><p> * <dl> * <dt><b>Styles:</b></dt> * <dd>READ_ONLY, WRAP</dd> * <dt><b>Events:</b></dt> * <dd>Selection, Modify, Verify</dd> * </dl> * </p><p> * IMPORTANT: This class is <em>not</em> intended to be subclassed. * </p> * * @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. * <p> * The style value is either one of the style constants defined in * class <code>DWT</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>DWT</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 DWTException <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 DWT#READ_ONLY * @see DWT#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 <code>ModifyListener</code> * interface. * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception DWTException <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 ModifyListener * @see #removeModifyListener */ public void addModifyListener (ModifyListener listener) { checkWidget (); if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (DWT.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 <code>SelectionListener</code> * interface. * <p> * <code>widgetSelected</code> is not called for texts. * <code>widgetDefaultSelected</code> is typically called when ENTER is pressed in a single-line text. * </p> * * @param listener the listener which should be notified when the control is selected by the user * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception DWTException <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 SelectionListener * @see #removeSelectionListener * @see SelectionEvent */ public void addSelectionListener(SelectionListener listener) { checkWidget (); if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener(listener); addListener (DWT.Selection,typedListener); addListener (DWT.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 <code>VerifyListener</code> * interface. * * @param listener the listener which should be notified * * @exception IllegalArgumentException <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception DWTException <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 VerifyListener * @see #removeVerifyListener */ void addVerifyListener (VerifyListener listener) { checkWidget(); if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); TypedListener typedListener = new TypedListener (listener); addListener (DWT.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 DWT style. */ return style & ~(DWT.H_SCROLL | DWT.V_SCROLL); } protected void checkSubclass () { if (!isValidSubclass ()) error (DWT.ERROR_INVALID_SUBCLASS); } public Point computeSize (int wHint, int hHint, bool 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 !is DWT.DEFAULT) width = wHint; if (hHint !is DWT.DEFAULT) height = hHint; Rectangle trim = computeTrim (0, 0, (int) width, (int) height); return new Point (trim.width, trim.height); } /** * Copies the selected text. * <p> * The current selection is copied to the clipboard. * </p> * * @exception DWTException <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 copy () { checkWidget (); // short [] selection = new short [2]; // if (OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection, null) !is OS.noErr) return; // if (selection [0] is 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) !is 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 & DWT.WRAP) !is 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 & DWT.READ_ONLY) is 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. * <p> * The current selection is first copied to the * clipboard and then deleted from the widget. * </p> * * @exception DWTException <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 cut () { checkWidget (); if ((style & DWT.READ_ONLY) !is 0) return; // short [] selection = new short [2]; // if (OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection, null) !is OS.noErr) return; // if (selection [0] is selection [1]) return; // char [] buffer = setText ("", selection [0], selection [1], true); // if (buffer !is null) { // copyToClipboard (buffer); // } } void enableWidget (bool enabled) { buttonView.setEnabled(enabled); textView.setEnabled(enabled); } /** * Returns the number of decimal places used by the receiver. * * @return the digits * * @exception DWTException <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 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 DWTException <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 getIncrement () { checkWidget (); return (int)buttonView.increment(); } /** * Returns the maximum value which the receiver will allow. * * @return the maximum * * @exception DWTException <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 getMaximum () { checkWidget (); return (int)buttonView.maxValue(); } /** * Returns the minimum value which the receiver will allow. * * @return the minimum * * @exception DWTException <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 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 DWTException <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 getPageIncrement () { checkWidget (); return pageIncrement; } /** * Returns the <em>selection</em>, which is the receiver's position. * * @return the selection * * @exception DWTException <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 getSelection () { checkWidget (); return (int)((NSStepper)buttonView).doubleValue(); } int getSelectionText () { return -1; } /** * Pastes text from clipboard. * <p> * The selected text is deleted from the widget * and new text inserted from the clipboard. * </p> * * @exception DWTException <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 paste () { checkWidget (); if ((style & DWT.READ_ONLY) !is 0) return; // String text = getClipboardText (); // short [] selection = new short [2]; // if (OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection, null) !is OS.noErr) return; // setText (text, selection [0], selection [1], true); } void releaseHandle () { super.releaseHandle(); if (buttonView !is null) { buttonView.setTag(-1); buttonView.release(); } if (textView !is 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 <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception DWTException <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 ModifyListener * @see #addModifyListener */ public void removeModifyListener (ModifyListener listener) { checkWidget (); if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); if (eventTable is null) return; eventTable.unhook (DWT.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 <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception DWTException <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 SelectionListener * @see #addSelectionListener */ public void removeSelectionListener(SelectionListener listener) { checkWidget (); if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); if (eventTable is null) return; eventTable.unhook (DWT.Selection, listener); eventTable.unhook (DWT.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 <ul> * <li>ERROR_NULL_ARGUMENT - if the listener is null</li> * </ul> * @exception DWTException <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 VerifyListener * @see #addVerifyListener */ void removeVerifyListener (VerifyListener listener) { checkWidget (); if (listener is null) error (DWT.ERROR_NULL_ARGUMENT); if (eventTable is null) return; eventTable.unhook (DWT.Verify, listener); } void sendSelection () { setSelection (getSelection(), false, true, true); } int setBounds (int x, int y, int width, int height, bool move, bool resize) { int result = super.setBounds(x, y, width, height, move, resize); if ((result & RESIZED) !is 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. * <p> * 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. * </p> * * @param value the new digits (must be greater than or equal to zero) * * @exception IllegalArgumentException <ul> * <li>ERROR_INVALID_ARGUMENT - if the value is less than zero</li> * </ul> * @exception DWTException <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 setDigits (int value) { checkWidget (); if (value < 0) error (DWT.ERROR_INVALID_ARGUMENT); // if (value is 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 DWTException <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 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 DWTException <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 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 negative or 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 nonnegative and less than the current maximum * * @exception DWTException <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 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 DWTException <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 setPageIncrement (int value) { checkWidget (); if (value < 1) return; pageIncrement = value; } /** * Sets the <em>selection</em>, 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 DWTException <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 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, bool setPos, bool setText, bool 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 (DWT.Verify) || filters (DWT.Verify)) { int length = cell.title().length(); string = verifyText (string, 0, length, null); if (string is 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 (DWT.Modify); } if (notify) postEvent (DWT.Selection); } /** * Sets the receiver's selection, minimum value, maximum * value, digits, increment and page increment all at once. * <p> * 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. * </p> * * @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 DWTException <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> * * @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 !is 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 !is -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 is 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 (DWT.Verify, event); if (!event.doit || isDisposed ()) return null; return event.text; } }