diff options
author | Felipe Heidrich <fheidric> | 2008-02-28 23:27:14 +0000 |
---|---|---|
committer | Felipe Heidrich <fheidric> | 2008-02-28 23:27:14 +0000 |
commit | c3725acb1dcb31dfc3cdf09d7f0b8ec5b826ac4f (patch) | |
tree | b57b3b0a2218811aaab0d3d37538328d87c4b3d4 | |
parent | 965eaac6a7106d9e7a95dc443b5e485e99434569 (diff) | |
download | eclipse.platform.swt-c3725acb1dcb31dfc3cdf09d7f0b8ec5b826ac4f.tar.gz eclipse.platform.swt-c3725acb1dcb31dfc3cdf09d7f0b8ec5b826ac4f.tar.xz eclipse.platform.swt-c3725acb1dcb31dfc3cdf09d7f0b8ec5b826ac4f.zip |
Bug 186634 Spinner: getSelection() during modify event should provide a mechanism to detect if the user has typed a value that is out of range
Bug 214030 setTextLimit for SWT Spinner widget
4 files changed, 180 insertions, 24 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Spinner.java b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Spinner.java index 04d3317001..e73329784a 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Spinner.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/carbon/org/eclipse/swt/widgets/Spinner.java @@ -45,9 +45,28 @@ public class Spinner extends Composite { int increment = 1; int pageIncrement = 10; int digits = 0; - + int textLimit = LIMIT; static int GAP = 3; - + + /** + * The maximum number of characters that can be entered + * into a text widget. + * <p> + * Note that this value is platform dependent, based upon + * the native widget implementation. + * </p> + */ + public static final int LIMIT; + + /* + * These values can be different on different platforms. + * Therefore they are not initialized in the declaration + * to stop the compiler from inlining. + */ + static { + LIMIT = 0x7FFFFFFF; + } + /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. @@ -507,6 +526,24 @@ int getSelectionText (boolean [] parseFail) { return -1; } +public String getText() { + int [] ptr = new int [1]; + int [] actualSize = new int [1]; + int result = OS.GetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextCFStringTag, 4, ptr, actualSize); + if (result != OS.noErr) return ""; + CFRange range = new CFRange (); + range.length = OS.CFStringGetLength (ptr [0]); + char [] buffer= new char [range.length]; + OS.CFStringGetCharacters (ptr [0], range, buffer); + OS.CFRelease (ptr [0]); + return new String (buffer); +} + +public int getTextLimit () { + checkWidget(); + return textLimit; +} + int getVisibleRegion (int control, boolean clipChildren) { if (control == textHandle) { if (!clipChildren) return super.getVisibleRegion (control, clipChildren); @@ -613,20 +650,21 @@ int kEventUnicodeKeyPressed (int nextHandler, int theEvent, int userData) { OS.GetEventParameter (theEvent, OS.kEventParamTextInputSendKeyboardEvent, OS.typeEventRef, null, keyboardEvent.length * 4, null, keyboardEvent); int [] keyCode = new int [1]; OS.GetEventParameter (keyboardEvent [0], OS.kEventParamKeyCode, OS.typeUInt32, null, keyCode.length * 4, null, keyCode); -// if (hooks (SWT.Verify) || filters (SWT.Verify)) { -// int [] modifiers = new int [1]; -// OS.GetEventParameter (keyboardEvent [0], OS.kEventParamKeyModifiers, OS.typeUInt32, null, 4, null, modifiers); -// if (modifiers [0] == OS.cmdKey) { -// switch (keyCode [0]) { -// case 7: /* X */ -// cut (); -// return OS.noErr; -// case 9: /* V */ -// paste (); -// return OS.noErr; -// } -// } -// } + int [] modifiers = new int [1]; + OS.GetEventParameter (keyboardEvent [0], OS.kEventParamKeyModifiers, OS.typeUInt32, null, 4, null, modifiers); + if (modifiers [0] == OS.cmdKey) { + switch (keyCode [0]) { + case 7: /* X */ + cut (); + return OS.noErr; + case 8: /* C */ + copy (); + return OS.noErr; + case 9: /* V */ + paste (); + return OS.noErr; + } + } int delta = 0; switch (keyCode [0]) { case 76: /* KP Enter */ @@ -842,10 +880,13 @@ boolean sendKeyEvent (int type, Event event) { } newText = verifyText (oldText, start, end, event); if (newText == null) return false; + if (charCount - (end - start) + newText.length () > textLimit) { + return false; + } if (newText != oldText) { setText (newText, start, end, false); start += newText.length (); - selection = new short [] {(short)start, (short)start }; + selection = new short [] {(short)start, (short)start}; OS.SetControlData (textHandle, (short)OS.kControlEntireControl, OS.kControlEditTextSelectionTag, 4, selection); } /* @@ -1063,21 +1104,26 @@ char [] setText (String string, int start, int end, boolean notify) { 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 null; - int length = OS.CFStringGetLength (ptr [0]); - - char [] text = new char [length - (end - start) + string.length ()]; + int charCount = OS.CFStringGetLength (ptr [0]); + int length = string.length (); + if (textLimit != LIMIT) { + if (charCount - (end - start) + length > textLimit) { + length = textLimit - charCount + (end - start); + } + } + char [] text = new char [charCount - (end - start) + length]; CFRange range = new CFRange (); range.location = 0; range.length = start; char [] buffer = new char [range.length]; OS.CFStringGetCharacters (ptr [0], range, buffer); System.arraycopy (buffer, 0, text, 0, range.length); - string.getChars (0, string.length (), text, start); + string.getChars (0, length, text, start); range.location = end; - range.length = length - end; + range.length = charCount - end; buffer = new char [range.length]; OS.CFStringGetCharacters (ptr [0], range, buffer); - System.arraycopy (buffer, 0, text, start + string.length (), range.length); + System.arraycopy (buffer, 0, text, start + length, range.length); /* Copying the return value to buffer */ range.location = start; @@ -1094,6 +1140,12 @@ char [] setText (String string, int start, int end, boolean notify) { return buffer; } +public void setTextLimit (int limit) { + checkWidget(); + if (limit == 0) error (SWT.ERROR_CANNOT_BE_ZERO); + textLimit = limit; +} + /** * Sets the receiver's selection, minimum value, maximum * value, digits, increment and page increment all at once. diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Spinner.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Spinner.java index 0df3ba0dc2..bddcd99b76 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Spinner.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Spinner.java @@ -44,6 +44,24 @@ public class Spinner extends Composite { int /*long*/ gdkEventKey = 0; int fixStart = -1, fixEnd = -1; + /** + * The maximum number of characters that can be entered + * into a text widget. + * <p> + * Note that this value is platform dependent, based upon + * the native widget implementation. + * </p> + */ + public final static int LIMIT; + /* + * These values can be different on different platforms. + * Therefore they are not initialized in the declaration + * to stop the compiler from inlining. + */ + static { + LIMIT = 0x7FFFFFFF; + } + /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. @@ -465,6 +483,22 @@ public int getSelection () { return (int) (value > 0 ? value + 0.5 : value - 0.5); } +public String getText () { + checkWidget (); + int /*long*/ str = OS.gtk_entry_get_text (handle); + if (str == 0) return ""; + int length = OS.strlen (str); + byte [] buffer = new byte [length]; + OS.memmove (buffer, str, length); + return new String (Converter.mbcsToWcs (null, buffer)); +} + +public int getTextLimit () { + checkWidget (); + int limit = OS.gtk_entry_get_max_length (handle); + return limit == 0 ? 0xFFFF : limit; +} + /** * Returns the number of decimal places used by the receiver. * @@ -933,6 +967,12 @@ public void setSelection (int value) { OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, VALUE_CHANGED); } +public void setTextLimit (int limit) { + checkWidget (); + if (limit == 0) error (SWT.ERROR_CANNOT_BE_ZERO); + OS.gtk_entry_set_max_length (handle, limit); +} + /** * Sets the number of decimal places used by the receiver. * <p> diff --git a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Spinner.java b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Spinner.java index 8aee4a5eb4..5b0bfe7949 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Spinner.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Spinner.java @@ -38,7 +38,21 @@ import org.eclipse.swt.events.*; * @since 3.1 */ public class Spinner extends Composite { + /** + * the operating system limit for the number of characters + * that the text field in an instance of this class can hold + */ + public static final int LIMIT; + /* + * These values can be different on different platforms. + * Therefore they are not initialized in the declaration + * to stop the compiler from inlining. + */ + static { + LIMIT = 0x7FFFFFFF; + } + /** * Constructs a new instance of this class given its parent * and a style value describing its behavior and appearance. @@ -422,6 +436,25 @@ public int getSelection () { OS.XtGetValues (handle, argList, argList.length / 2); return argList [1]; } +public String getText () { + checkWidget(); + int [] argList = {OS.XmNtextField, 0}; + OS.XtGetValues (handle, argList, argList.length / 2); + + int ptr = OS.XmTextGetString (argList[1]); + if (ptr == 0) return ""; + int length = OS.strlen (ptr); + byte [] buffer = new byte [length]; + OS.memmove (buffer, ptr, length); + OS.XtFree (ptr); + return new String (Converter.mbcsToWcs (getCodePage (), buffer)); +} +public int getTextLimit () { + checkWidget(); + int [] argList = {OS.XmNtextField, 0}; + OS.XtGetValues (handle, argList, argList.length / 2); + return OS.XmTextGetMaxLength (argList[1]); +} void hookEvents () { super.hookEvents (); int windowProc = display.windowProc; @@ -707,7 +740,13 @@ public void setSelection (int value) { int [] argList1 = {OS.XmNposition, value}; OS.XtSetValues (handle, argList1, argList1.length / 2); } - +public void setTextLimit (int limit) { + checkWidget(); + if (limit == 0) error (SWT.ERROR_CANNOT_BE_ZERO); + int [] argList = {OS.XmNtextField, 0}; + OS.XtGetValues (handle, argList, argList.length / 2); + OS.XmTextSetMaxLength (argList[1], limit); +} /** * Sets the receiver's selection, minimum value, maximum * value, digits, increment and page increment all at once. diff --git a/bundles/org.eclipse.swt/Eclipse SWT/photon/org/eclipse/swt/widgets/Spinner.java b/bundles/org.eclipse.swt/Eclipse SWT/photon/org/eclipse/swt/widgets/Spinner.java index 21cac85dda..40b2dd70d0 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/photon/org/eclipse/swt/widgets/Spinner.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/photon/org/eclipse/swt/widgets/Spinner.java @@ -36,6 +36,16 @@ import org.eclipse.swt.*; * @since 3.1 */ public class Spinner extends Composite { + public static final int LIMIT; + + /* + * These values can be different on different platforms. + * Therefore they are not initialized in the declaration + * to stop the compiler from inlining. + */ + static { + LIMIT = 0x7FFF; + } /** * Constructs a new instance of this class given its parent @@ -425,6 +435,16 @@ public int getSelection () { return args [1]; } +public String getText () { + checkWidget (); + return ""; +} + +public int getTextLimit () { + checkWidget (); + return 0; +} + boolean hasFocus () { return OS.PtIsFocused (handle) != 0; } @@ -679,6 +699,11 @@ public void setSelection (int value) { OS.PtSetResource (handle, OS.Pt_ARG_NUMERIC_VALUE, value, 0); } +public void setTextLimit (int limit) { + checkWidget (); + if (limit == 0) error (SWT.ERROR_CANNOT_BE_ZERO); +} + /** * Sets the receiver's selection, minimum value, maximum * value, digits, increment and page increment all at once. |