diff options
author | Lakshmi Shanmugam <lshanmugam> | 2010-09-22 14:59:34 +0000 |
---|---|---|
committer | Lakshmi Shanmugam <lshanmugam> | 2010-09-22 14:59:34 +0000 |
commit | 7757f31e4a8851faa85deefdaf3e3c6340230705 (patch) | |
tree | a46af19a5e9ab20561c2180e85f392e7ad302ccd /bundles | |
parent | b2738a28cb5231d346c6c5441b6e2379f19da0e4 (diff) | |
download | eclipse.platform.swt-7757f31e4a8851faa85deefdaf3e3c6340230705.tar.gz eclipse.platform.swt-7757f31e4a8851faa85deefdaf3e3c6340230705.tar.xz eclipse.platform.swt-7757f31e4a8851faa85deefdaf3e3c6340230705.zip |
Bug 297412 - Need setText (and getText) method on Text widget which takes char[] input
Diffstat (limited to 'bundles')
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Text.java | 96 | ||||
-rwxr-xr-x | bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java | 101 |
2 files changed, 155 insertions, 42 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Text.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Text.java index c62de0080d..8ffcaede07 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Text.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/Text.java @@ -981,22 +981,7 @@ int getTabWidth (int tabs) { * </ul> */ public String getText () { - checkWidget (); - int /*long*/ address; - if ((style & SWT.SINGLE) != 0) { - address = OS.gtk_entry_get_text (handle); - } else { - byte [] start = new byte [ITER_SIZEOF]; - byte [] end = new byte [ITER_SIZEOF]; - OS.gtk_text_buffer_get_bounds (bufferHandle, start, end); - address = OS.gtk_text_buffer_get_text (bufferHandle, start, end, true); - } - if (address == 0) return ""; - int length = OS.strlen (address); - byte [] buffer = new byte [length]; - OS.memmove (buffer, address, length); - if ((style & SWT.MULTI) != 0) OS.g_free (address); - return new String (Converter.mbcsToWcs (null, buffer)); + return new String (getTextChars()); } /** @@ -1044,6 +1029,40 @@ public String getText (int start, int end) { } /** + * Returns the widget's text as a character array. + * <p> + * The text for a text widget is the characters in the widget, or + * a zero length array if this has never been set. + * </p> + * + * @return a character array that contains the widget'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> + * @since 3.7 + */ +public char [] getTextChars () { + checkWidget (); + int /*long*/ address; + if ((style & SWT.SINGLE) != 0) { + address = OS.gtk_entry_get_text (handle); + } else { + byte [] start = new byte [ITER_SIZEOF]; + byte [] end = new byte [ITER_SIZEOF]; + OS.gtk_text_buffer_get_bounds (bufferHandle, start, end); + address = OS.gtk_text_buffer_get_text (bufferHandle, start, end, true); + } + if (address == 0) return new char[0]; + int length = OS.strlen (address); + byte [] buffer = new byte [length]; + OS.memmove (buffer, address, length); + if ((style & SWT.MULTI) != 0) OS.g_free (address); + return Converter.mbcsToWcs (null, buffer); +} + +/** * Returns the maximum number of characters that the receiver is capable of holding. * <p> * If this has not been changed by <code>setTextLimit()</code>, @@ -2040,8 +2059,47 @@ public void setText (String string) { string = verifyText (string, 0, getCharCount ()); if (string == null) return; } + char [] text = new char [string.length()]; + string.getChars(0, text.length, text, 0); + setText (text); +} + +/** + * Sets the contents of the receiver to the characters in the array. If the receiver has style + * SINGLE and the argument contains multiple lines of text, the result of this + * operation is undefined and may vary from platform to platform. + * + * @param text a character array that contains the new text + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the array 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> + * @since 3.7 + */ +public void setTextChars (char [] text) { + checkWidget (); + if (text == null) error (SWT.ERROR_NULL_ARGUMENT); + /* + * Feature in gtk. When text is set in gtk, separate events are fired for the deletion and + * insertion of the text. This is not wrong, but is inconsistent with other platforms. The + * fix is to block the firing of these events and fire them ourselves in a consistent manner. + */ + if (hooks (SWT.Verify) || filters (SWT.Verify)) { + String string = verifyText (new String(text), 0, getCharCount ()); + if (string == null) return; + text = new char [string.length()]; + string.getChars (0, text.length, text, 0); + } + setText (text); +} + +void setText (char [] text) { if ((style & SWT.SINGLE) != 0) { - byte [] buffer = Converter.wcsToMbcs (null, string, true); + byte [] buffer = Converter.wcsToMbcs (null, text, true); OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, CHANGED); OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, DELETE_TEXT); OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, INSERT_TEXT); @@ -2050,7 +2108,7 @@ public void setText (String string) { OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, DELETE_TEXT); OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, INSERT_TEXT); } else { - byte [] buffer = Converter.wcsToMbcs (null, string, false); + byte [] buffer = Converter.wcsToMbcs (null, text, false); byte [] position = new byte [ITER_SIZEOF]; OS.g_signal_handlers_block_matched (bufferHandle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, CHANGED); OS.g_signal_handlers_block_matched (bufferHandle, OS.G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, DELETE_RANGE); @@ -2175,7 +2233,7 @@ int traversalCode (int key, GdkEventKey event) { } String verifyText (String string, int start, int end) { - if (string.length () == 0 && start == end) return null; + if (string != null && string.length () == 0 && start == end) return null; Event event = new Event (); event.text = string; event.start = start; diff --git a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java index 3e72a30b02..6269586881 100755 --- a/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/motif/org/eclipse/swt/widgets/Text.java @@ -58,7 +58,8 @@ import org.eclipse.swt.events.*; public class Text extends Scrollable { char echoCharacter; boolean ignoreChange; - String hiddenText, message; + char [] hiddenText; + String message; int drawCount; Color disabledColor; @@ -462,7 +463,8 @@ ScrollBar createScrollBar (int type) { } void createWidget (int index) { super.createWidget (index); - hiddenText = message = ""; + hiddenText = new char [0]; + message = ""; if ((style & SWT.PASSWORD) != 0) setEchoChar ('*'); if ((style & SWT.SINGLE) != 0) { disabledColor = new Color (display, MSG_FOREGROUND); @@ -660,7 +662,7 @@ public boolean getEditable () { public int getLineCount () { checkWidget(); if ((style & SWT.SINGLE) != 0) return 1; - int lastChar = echoCharacter != '\0' ? hiddenText.length () : OS.XmTextGetLastPosition (handle); + int lastChar = echoCharacter != '\0' ? hiddenText.length : OS.XmTextGetLastPosition (handle); return getLineNumber (lastChar) + 1; } /** @@ -712,7 +714,7 @@ int getLineNumber (int position) { int length = page; if (start + page > position) length = position - start; if (echoCharacter != '\0') { - hiddenText.getChars (start, start + length, buffer, 0); + System.arraycopy (hiddenText, start, buffer, 0, length); } else { if (OS.IsLinux) { OS.XmTextGetSubstring (handle, start, length, buffer1.length, buffer1); @@ -891,15 +893,7 @@ public int getTabs () { * </ul> */ public String getText () { - checkWidget(); - if (echoCharacter != '\0') return hiddenText; - int ptr = OS.XmTextGetString (handle); - 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)); + return new String (getTextChars()); } /** * Returns a range of text. Returns an empty string if the @@ -923,13 +917,13 @@ public String getText (int start, int end) { checkWidget(); if (!(start <= end && 0 <= end)) return ""; boolean hasEcho = echoCharacter != '\0'; - int length = hasEcho ? hiddenText.length () : OS.XmTextGetLastPosition (handle); + int length = hasEcho ? hiddenText.length : OS.XmTextGetLastPosition (handle); if (length == 0) return ""; end = Math.min (end, length - 1); if (start > end) return ""; start = Math.max (0, start); - if (hasEcho) return hiddenText.substring (start, end + 1); int numChars = end - start + 1; + if (hasEcho) return new String(hiddenText, start, numChars); int bufLength = numChars * OS.MB_CUR_MAX () + 1; byte [] buffer = new byte [bufLength]; int code = OS.XmTextGetSubstring (handle, start, numChars, bufLength, buffer); @@ -942,6 +936,36 @@ public String getText (int start, int end) { return new String (unicode, 0, numChars); } /** + * Returns the widget's text as a character array. + * <p> + * The text for a text widget is the characters in the widget, or + * a zero length array if this has never been set. + * </p> + * + * @return a character array that contains the widget'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> + * @since 3.7 + */ +public char [] getTextChars () { + checkWidget(); + if (echoCharacter != '\0') { + char [] text = new char [hiddenText.length]; + System.arraycopy(hiddenText, 0, text, 0, text.length); + return text; + } + int ptr = OS.XmTextGetString (handle); + if (ptr == 0) return new char [0]; + int length = OS.strlen (ptr); + byte [] buffer = new byte [length]; + OS.memmove (buffer, ptr, length); + OS.XtFree (ptr); + return Converter.mbcsToWcs (getCodePage (), buffer); +} +/** * Returns the maximum number of characters that the receiver is capable of holding. * <p> * If this has not been changed by <code>setTextLimit()</code>, @@ -1092,7 +1116,8 @@ public void paste () { } void releaseWidget () { super.releaseWidget (); - hiddenText = message = null; + hiddenText = null; + message = null; if (disabledColor != null) disabledColor.dispose (); disabledColor = null; } @@ -1265,18 +1290,18 @@ public void setEchoChar (char echo) { checkWidget(); if ((style & SWT.MULTI) != 0) return; if (echoCharacter == echo) return; - String newText; + char [] newText; if (echo == 0) { newText = hiddenText; - hiddenText = ""; + hiddenText = new char [0]; } else { - newText = hiddenText = getText(); + newText = hiddenText = getTextChars (); } echoCharacter = echo; Point selection = getSelection(); boolean oldValue = ignoreChange; ignoreChange = true; - setText(newText); + setTextChars(newText); setSelection(selection); ignoreChange = oldValue; } @@ -1531,6 +1556,33 @@ public void setText (String string) { display.setWarnings(warnings); } /** + * Sets the contents of the receiver to the characters in the array. If the receiver has style + * SINGLE and the argument contains multiple lines of text, the result of this + * operation is undefined and may vary from platform to platform. + * + * @param text a character array that contains the new text + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the array 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> + * @since 3.7 + */ +public void setTextChars (char [] text) { + checkWidget(); + if (text == null) error (SWT.ERROR_NULL_ARGUMENT); + byte [] buffer = Converter.wcsToMbcs (getCodePage (), text, true); + boolean warnings = display.getWarnings (); + display.setWarnings (false); + OS.XmTextSetString (handle, buffer); + OS.XmTextSetInsertionPosition (handle, 0); + display.setWarnings(warnings); +} + +/** * Sets the maximum number of characters that the receiver * is capable of holding to be the argument. * <p> @@ -1709,9 +1761,12 @@ int XmNmodifyVerifyCallback (int w, int client_data, int call_data) { } if (newText != null) { if (echoCharacter != '\0' && (textVerify.doit != 0)) { - String prefix = hiddenText.substring (0, textVerify.startPos); - String suffix = hiddenText.substring (textVerify.endPos, hiddenText.length ()); - hiddenText = prefix + newText + suffix; + String string = new String (hiddenText); + String prefix = string.substring (0, textVerify.startPos); + String suffix = string.substring (textVerify.endPos, string.length ()); + string = prefix + newText + suffix; + hiddenText = new char [string.length()]; + string.getChars(0, hiddenText.length, hiddenText, 0); char [] charBuffer = new char [newText.length ()]; for (int i=0; i<charBuffer.length; i++) { charBuffer [i] = echoCharacter; |