|
1 /******************************************************************************* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. This program and the accompanying materials |
|
4 * are made available under the terms of the Eclipse Public License v1.0 |
|
5 * which accompanies this distribution, and is available at |
|
6 * http://www.eclipse.org/legal/epl-v10.html |
|
7 * |
|
8 * Contributors: |
|
9 * Nokia Corporation - initial implementation |
|
10 *******************************************************************************/ |
|
11 package org.eclipse.swt.internal.qt; |
|
12 |
|
13 import org.eclipse.swt.SWT; |
|
14 import org.eclipse.swt.graphics.Point; |
|
15 import org.eclipse.swt.widgets.Event; |
|
16 import org.eclipse.swt.widgets.Widget; |
|
17 |
|
18 public class TextUtils extends Object { |
|
19 public static final int LINE_EDIT = 0; |
|
20 public static final int TEXT_EDIT = 1; |
|
21 |
|
22 /** |
|
23 * @see Text.append |
|
24 * @param limit - if char count bigger than limit, text gets truncated |
|
25 * @param verifier - owner of verify listeners, can be null |
|
26 */ |
|
27 public static void append (int variant, int handle, String string, int limit, Widget verifier) { |
|
28 if (string == null) { |
|
29 SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
30 } |
|
31 |
|
32 int count = getCharCount(variant, handle); |
|
33 string = verify(variant, handle, string, verifier, limit, count, count); |
|
34 if (string == null) { |
|
35 return; |
|
36 } |
|
37 |
|
38 if (variant == LINE_EDIT) { |
|
39 OS.QLineEdit_end(handle, false); |
|
40 OS.QLineEdit_insert(handle, string); |
|
41 } |
|
42 else { |
|
43 OS.QTextEdit_swt_append(handle, string); |
|
44 } |
|
45 } |
|
46 |
|
47 /** |
|
48 * @see Text.append |
|
49 */ |
|
50 public static void clearSelection (int variant, int handle) { |
|
51 if (variant == LINE_EDIT) { |
|
52 OS.QLineEdit_setCursorPosition(handle, OS.QLineEdit_cursorPosition(handle)); |
|
53 } else { |
|
54 OS.QTextEdit_swt_clearSelection(handle); |
|
55 } |
|
56 } |
|
57 |
|
58 /** |
|
59 * @see Text.copy |
|
60 */ |
|
61 public static void copy (int variant, int handle) { |
|
62 if (variant == LINE_EDIT) { |
|
63 OS.QLineEdit_copy(handle); |
|
64 } else { |
|
65 OS.QTextEdit_copy(handle); |
|
66 } |
|
67 } |
|
68 |
|
69 /** |
|
70 * @see Text.cut |
|
71 * @param verifier - owner of verify listeners, can be null |
|
72 */ |
|
73 public static void cut (int variant, int handle, Widget verifier) { |
|
74 Point selection = getSelection(variant, handle); |
|
75 String string = verify(variant, handle, "", verifier, -1, |
|
76 Math.min(selection.x, selection.y), Math.max(selection.x, selection.y)); |
|
77 if (string == null) { |
|
78 return; |
|
79 } |
|
80 |
|
81 if (variant == LINE_EDIT) { |
|
82 OS.QLineEdit_cut(handle); |
|
83 } else { |
|
84 OS.QTextEdit_cut(handle); |
|
85 } |
|
86 |
|
87 if (!string.equals("")) { |
|
88 if (variant == LINE_EDIT) { |
|
89 OS.QLineEdit_insert(handle, string); |
|
90 } else { |
|
91 OS.QTextEdit_insertPlainText(handle, string); |
|
92 } |
|
93 } |
|
94 } |
|
95 |
|
96 /** |
|
97 * @see Text.getEditable |
|
98 */ |
|
99 public static boolean getReadOnly (int variant, int handle) { |
|
100 if (variant == TextUtils.LINE_EDIT) { |
|
101 return OS.QLineEdit_isReadOnly(handle); |
|
102 } else { |
|
103 return OS.QTextEdit_isReadOnly(handle); |
|
104 } |
|
105 } |
|
106 |
|
107 /** |
|
108 * @see Text.getSelection |
|
109 */ |
|
110 public static Point getSelection (int variant, int handle) { |
|
111 if (variant == LINE_EDIT) { |
|
112 int firstSelected = OS.QLineEdit_selectionStart(handle); |
|
113 if (firstSelected == -1) { |
|
114 int cursorPos = OS.QLineEdit_cursorPosition(handle); |
|
115 return new Point(cursorPos, cursorPos); |
|
116 } |
|
117 return new Point(firstSelected, firstSelected |
|
118 + OS.QLineEdit_selectedText(handle).length()); |
|
119 |
|
120 } else { |
|
121 int start = OS.QTextEdit_swt_selectionStart(handle); |
|
122 int end = OS.QTextEdit_swt_selectionEnd(handle); |
|
123 return new Point(Math.min(start, end), Math.max(start, end)); |
|
124 } |
|
125 } |
|
126 |
|
127 /** |
|
128 * @see Text.getSelectionText |
|
129 */ |
|
130 public static String getSelectionText (int variant, int handle) { |
|
131 if (variant == LINE_EDIT) { |
|
132 return OS.QLineEdit_selectedText(handle); |
|
133 } else { |
|
134 return OS.QTextEdit_swt_getSelectionText(handle); |
|
135 } |
|
136 } |
|
137 |
|
138 /** |
|
139 * @see Text.getCaretPosition |
|
140 */ |
|
141 public static int getCaretPosition (int variant, int handle) { |
|
142 if (variant == LINE_EDIT) { |
|
143 return OS.QLineEdit_cursorPosition(handle); |
|
144 } else { |
|
145 return OS.QTextEdit_swt_getCaretPosition(handle); |
|
146 } |
|
147 } |
|
148 |
|
149 /** |
|
150 * @see Text.getCharCount |
|
151 */ |
|
152 public static int getCharCount (int variant, int handle) { |
|
153 if (variant == LINE_EDIT) { |
|
154 return OS.QLineEdit_text(handle).length(); |
|
155 } else { |
|
156 return OS.QTextEdit_swt_getCharCount(handle); |
|
157 } |
|
158 } |
|
159 |
|
160 /** |
|
161 * @see Text.getText |
|
162 */ |
|
163 public static String getText (int variant, int handle) { |
|
164 if (variant == LINE_EDIT) { |
|
165 return OS.QLineEdit_text(handle); |
|
166 } else { |
|
167 return OS.QTextEdit_toPlainText(handle); |
|
168 } |
|
169 } |
|
170 |
|
171 /** |
|
172 * @see Text.insert |
|
173 * @param limit - if char count bigger than limit, text gets truncated |
|
174 * @param verifier - owner of verify listeners, can be null |
|
175 */ |
|
176 public static void insert (int variant, int handle, String string, int limit, Widget verifier) { |
|
177 if (string == null) { |
|
178 SWT.error (SWT.ERROR_NULL_ARGUMENT); |
|
179 } |
|
180 |
|
181 Point selection = getSelection(variant, handle); |
|
182 string = verify(variant, handle, string, verifier, limit, |
|
183 Math.min(selection.x, selection.y), Math.max(selection.x, selection.y)); |
|
184 if (string == null) { |
|
185 return; |
|
186 } |
|
187 |
|
188 if (variant == LINE_EDIT) { |
|
189 OS.QLineEdit_insert(handle, string); |
|
190 } else { |
|
191 OS.QTextEdit_insertPlainText(handle, string); |
|
192 } |
|
193 } |
|
194 |
|
195 /** |
|
196 * @see Text.insert |
|
197 * @param limit - if char count bigger than limit, text gets truncated |
|
198 * @param verifier - owner of verify listeners, can be null |
|
199 */ |
|
200 public static void paste (int variant, int handle, int limit, Widget verifier) { |
|
201 Point selection = getSelection(variant, handle); |
|
202 String string = verify(variant, handle, OS.QClipboard_text(), verifier, limit, |
|
203 Math.min(selection.x, selection.y), Math.max(selection.x, selection.y)); |
|
204 if (string == null) { |
|
205 return; |
|
206 } |
|
207 |
|
208 if (variant == LINE_EDIT) { |
|
209 OS.QLineEdit_insert(handle, string); |
|
210 } else { |
|
211 OS.QTextEdit_insertPlainText(handle, string); |
|
212 } |
|
213 } |
|
214 |
|
215 /** |
|
216 * @see Text.selectAll |
|
217 */ |
|
218 public static void selectAll (int variant, int handle) { |
|
219 if (variant == TextUtils.LINE_EDIT) { |
|
220 OS.QLineEdit_selectAll(handle); |
|
221 } else { |
|
222 OS.QTextEdit_swt_selectAll(handle); |
|
223 } |
|
224 } |
|
225 |
|
226 /** |
|
227 * @see Text.setEditable |
|
228 */ |
|
229 public static void setReadOnly (int variant, int handle, boolean readOnly) { |
|
230 if (variant == TextUtils.LINE_EDIT) { |
|
231 OS.QLineEdit_setReadOnly(handle, readOnly); |
|
232 } else { |
|
233 OS.QTextEdit_setReadOnly(handle, readOnly); |
|
234 } |
|
235 } |
|
236 |
|
237 /** |
|
238 * @see Text.setSelection |
|
239 */ |
|
240 public static void setSelection (int variant, int handle, int start, int end) { |
|
241 int length = getCharCount(variant, handle); |
|
242 start = Math.min(Math.max(0, start), length); |
|
243 end = Math.min(Math.max(0, end), length); |
|
244 |
|
245 if (variant == LINE_EDIT) { |
|
246 if(start == end) { |
|
247 OS.QLineEdit_deselect(handle); |
|
248 OS.QLineEdit_setCursorPosition(handle, start); |
|
249 } else { |
|
250 OS.QLineEdit_setSelection(handle, start, (end - start)); |
|
251 } |
|
252 } else { |
|
253 OS.QTextEdit_swt_setCursorPosition(handle, start, true); |
|
254 OS.QTextEdit_swt_setCursorPosition(handle, end, false); |
|
255 } |
|
256 } |
|
257 |
|
258 /** |
|
259 * @see Text.setText |
|
260 * @param limit - if char count bigger than limit, text gets truncated |
|
261 * @param verifier - owner of verify listeners, can be null |
|
262 */ |
|
263 public static void setText (int variant, int handle, String string, int limit, Widget verifier) { |
|
264 if (string == null) { |
|
265 SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
266 } |
|
267 |
|
268 string = verify(variant, handle, string, verifier, limit, 0, getCharCount(variant, handle)); |
|
269 if (string == null) { |
|
270 return; |
|
271 } |
|
272 |
|
273 if (variant == LINE_EDIT) { |
|
274 OS.QLineEdit_setText(handle, string); |
|
275 OS.QLineEdit_setCursorPosition(handle, 0); |
|
276 } else { |
|
277 OS.QTextEdit_setPlainText(handle, string); |
|
278 } |
|
279 } |
|
280 |
|
281 /** |
|
282 * @see Text.qt_event_keypress |
|
283 */ |
|
284 public static boolean handle_keypress(int variant, int handle, int key, |
|
285 int modifier, int character, Event event, int textLimit, Widget verifier) { |
|
286 |
|
287 String origText = "" + (char) character; |
|
288 Point selection = getSelection(variant, handle); |
|
289 int start = selection.x; |
|
290 int end = selection.y; |
|
291 |
|
292 // keyboard shortcuts for paste |
|
293 if ((modifier == OS.QT_SHIFTMODIFIER && key == OS.QT_KEY_INSERT) |
|
294 || (modifier == OS.QT_CONTROLMODIFIER && key == OS.QT_KEY_V)) { |
|
295 |
|
296 origText = OS.QClipboard_text(); |
|
297 if (origText == null || origText.equals("")) { |
|
298 return false; |
|
299 } |
|
300 event = null; |
|
301 |
|
302 // keyboard shortcuts for cut |
|
303 } else if ((modifier == OS.QT_SHIFTMODIFIER && key == OS.QT_KEY_DELETE) |
|
304 || (modifier == OS.QT_CONTROLMODIFIER && key == OS.QT_KEY_X)) { |
|
305 |
|
306 if (end == start) { |
|
307 return false; |
|
308 } |
|
309 event = null; |
|
310 origText = ""; |
|
311 |
|
312 // Cancel ctrl + Del, ctrl + Backspace, alt + Bs and ctrl+k because these key bindings |
|
313 // behave differently in QLineEdit and in QTextEdit and making verifyEvents |
|
314 // to work fully reliably in all platforms is impossible. |
|
315 } else if (((modifier & OS.QT_CONTROLMODIFIER) != 0 && key == OS.QT_KEY_DELETE) |
|
316 || ((modifier & OS.QT_CONTROLMODIFIER) != 0 && key == OS.QT_KEY_BACKSPACE) |
|
317 || ((modifier & OS.QT_ALTMODIFIER) != 0 && key == OS.QT_KEY_BACKSPACE) |
|
318 || (modifier == OS.QT_CONTROLMODIFIER && key == OS.QT_KEY_K)) { |
|
319 return true; |
|
320 // Non-printable keys, return, backspace and delete |
|
321 } else if ((key & OS.QT_NONPRINTABLEKEYMASK) != 0) { |
|
322 |
|
323 switch (key) { |
|
324 case OS.QT_KEY_RETURN: |
|
325 case OS.QT_KEY_ENTER: |
|
326 if (variant == LINE_EDIT) { |
|
327 return false; |
|
328 } |
|
329 break; |
|
330 case OS.QT_KEY_BACKSPACE: |
|
331 if (end == start) { |
|
332 if (start == 0) { |
|
333 return false; |
|
334 } |
|
335 --start; |
|
336 } |
|
337 origText = ""; |
|
338 break; |
|
339 case OS.QT_KEY_DELETE: |
|
340 if (modifier != OS.QT_NOMODIFIER) { |
|
341 return false; |
|
342 } |
|
343 if (end == start) { |
|
344 if (end == getCharCount(variant, handle)) { |
|
345 return false; |
|
346 } |
|
347 ++end; |
|
348 } |
|
349 origText = ""; |
|
350 break; |
|
351 default: |
|
352 // No need to verify other non-printable characters |
|
353 return false; |
|
354 } |
|
355 |
|
356 // Don't verify key pressed that don't change the text. If |
|
357 // chararcter is smaller than the first printable ASCII char |
|
358 // (= space), the text content is not modified. |
|
359 // For some unknown reason when ctrl+8 is pressed, character is |
|
360 // set Del. Ctrl+8 does not modify the text content anyway. |
|
361 } else if ((modifier & OS.QT_CONTROLMODIFIER) != 0 && ((char) character < ' ' || (char) character == SWT.DEL)) { |
|
362 return false; |
|
363 } |
|
364 |
|
365 String verifiedText = verify(variant, handle, origText, verifier, textLimit, start, end, event); |
|
366 |
|
367 if (verifiedText == null) { |
|
368 // Key event is not forwarded to native widget |
|
369 return true; |
|
370 } else if (verifiedText.equals(origText)) { |
|
371 return false; |
|
372 } else { |
|
373 setSelection(variant, handle, start, end); |
|
374 |
|
375 if (variant == LINE_EDIT) { |
|
376 OS.QLineEdit_insert(handle, verifiedText); |
|
377 } else { |
|
378 OS.QTextEdit_insertPlainText(handle, verifiedText); |
|
379 } |
|
380 |
|
381 // Key event is not forwarded to native widget |
|
382 return true; |
|
383 } |
|
384 } |
|
385 |
|
386 /** |
|
387 * Verify, limit, range |
|
388 */ |
|
389 public static String verify (int variant, int handle, String string, |
|
390 Widget verifier, int limit, int start, int end) { |
|
391 return verify(variant, handle, string, verifier, limit, start, end, null); |
|
392 } |
|
393 |
|
394 /** |
|
395 * Verify, limit, range, key event |
|
396 */ |
|
397 public static String verify (int variant, int handle, String string, |
|
398 Widget verifier, int limit, int start, int end, Event keyEvent) { |
|
399 if (string.length() == 0 && start == end) { |
|
400 return null; |
|
401 } |
|
402 |
|
403 if ((verifier != null) && ((verifier.getStyle() & SWT.SINGLE) != 0)) { |
|
404 string = string.replace('\n', '\u0020');//Replace with space |
|
405 } |
|
406 |
|
407 if (verifier != null) { |
|
408 Event event = new Event(); |
|
409 event.text = string; |
|
410 event.start = start; |
|
411 event.end = end; |
|
412 |
|
413 if (keyEvent != null) { |
|
414 event.keyCode = keyEvent.keyCode; |
|
415 event.character = keyEvent.character; |
|
416 event.stateMask = keyEvent.stateMask; |
|
417 } else { |
|
418 event.keyCode = 0; |
|
419 event.character = 0; |
|
420 event.stateMask = 0; |
|
421 } |
|
422 |
|
423 verifier.notifyListeners(SWT.Verify, event); |
|
424 if (!event.doit || verifier.isDisposed()) { |
|
425 return null; |
|
426 } |
|
427 string = event.text; |
|
428 } |
|
429 |
|
430 if (limit > 0 && string.length() > 0) { |
|
431 int count = 0; |
|
432 if (variant == LINE_EDIT) { |
|
433 count = OS.QLineEdit_text(handle).length(); |
|
434 } |
|
435 else { |
|
436 count = OS.QTextEdit_swt_getCharCount(handle); |
|
437 } |
|
438 int spaceLeft = limit - count + (end - start); |
|
439 int endIdx = Math.min(Math.max(0, spaceLeft), string.length()); |
|
440 string = string.substring(0, endIdx); |
|
441 } |
|
442 |
|
443 if (string == null || (string.length() == 0 && start == end)) { |
|
444 return null; |
|
445 } |
|
446 |
|
447 return string; |
|
448 } |
|
449 |
|
450 } |