25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
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 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.widgets.Spinner;
|
|
14
|
|
15
|
|
16 import org.eclipse.swt.widgets.Composite;
|
|
17 import org.eclipse.swt.widgets.TypedListener;
|
|
18 import org.eclipse.swt.widgets.Event;
|
|
19 import org.eclipse.swt.widgets.Widget;
|
|
20 import org.eclipse.swt.SWT;
|
|
21 import org.eclipse.swt.internal.gtk.OS;
|
|
22 import org.eclipse.swt.graphics.Point;
|
|
23 import org.eclipse.swt.graphics.Rectangle;
|
|
24 import org.eclipse.swt.events.SelectionListener;
|
|
25 import org.eclipse.swt.events.SelectionEvent;
|
|
26 import org.eclipse.swt.events.ModifyListener;
|
|
27 import org.eclipse.swt.events.VerifyListener;
|
|
28
|
48
|
29 import java.lang.all;
|
|
30 version(Tango){
|
25
|
31 import tango.util.Convert;
|
48
|
32 } else { // Phobos
|
|
33 }
|
25
|
34
|
|
35 /**
|
|
36 * Instances of this class are selectable user interface
|
|
37 * objects that allow the user to enter and modify numeric
|
|
38 * values.
|
|
39 * <p>
|
|
40 * Note that although this class is a subclass of <code>Composite</code>,
|
|
41 * it does not make sense to add children to it, or set a layout on it.
|
|
42 * </p><p>
|
|
43 * <dl>
|
|
44 * <dt><b>Styles:</b></dt>
|
|
45 * <dd>READ_ONLY, WRAP</dd>
|
|
46 * <dt><b>Events:</b></dt>
|
|
47 * <dd>Selection, Modify, Verify</dd>
|
|
48 * </dl>
|
|
49 * </p><p>
|
|
50 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
|
|
51 * </p>
|
|
52 *
|
|
53 * @see <a href="http://www.eclipse.org/swt/snippets/#spinner">Spinner snippets</a>
|
|
54 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
|
|
55 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
56 *
|
|
57 * @since 3.1
|
|
58 */
|
|
59 public class Spinner : Composite {
|
|
60
|
|
61 alias Composite.computeSize computeSize;
|
|
62 alias Composite.createHandle createHandle;
|
|
63 alias Composite.setBackgroundColor setBackgroundColor;
|
|
64 alias Composite.setCursor setCursor;
|
|
65 alias Composite.translateTraversal translateTraversal;
|
|
66
|
|
67 static final int INNER_BORDER = 2;
|
|
68 static final int MIN_ARROW_WIDTH = 6;
|
|
69 int lastEventTime = 0;
|
|
70 GdkEventKey* gdkEventKey;
|
|
71 int fixStart = -1, fixEnd = -1;
|
|
72
|
|
73 /**
|
|
74 * the operating system limit for the number of characters
|
|
75 * that the text field in an instance of this class can hold
|
|
76 *
|
|
77 * @since 3.4
|
|
78 */
|
|
79 public const static int LIMIT = 0x7FFFFFFF;
|
|
80
|
|
81 /**
|
|
82 * Constructs a new instance of this class given its parent
|
|
83 * and a style value describing its behavior and appearance.
|
|
84 * <p>
|
|
85 * The style value is either one of the style constants defined in
|
|
86 * class <code>SWT</code> which is applicable to instances of this
|
|
87 * class, or must be built by <em>bitwise OR</em>'ing together
|
|
88 * (that is, using the <code>int</code> "|" operator) two or more
|
|
89 * of those <code>SWT</code> style constants. The class description
|
|
90 * lists the style constants that are applicable to the class.
|
|
91 * Style bits are also inherited from superclasses.
|
|
92 * </p>
|
|
93 *
|
|
94 * @param parent a composite control which will be the parent of the new instance (cannot be null)
|
|
95 * @param style the style of control to construct
|
|
96 *
|
|
97 * @exception IllegalArgumentException <ul>
|
|
98 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
|
|
99 * </ul>
|
|
100 * @exception SWTException <ul>
|
|
101 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
|
|
102 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
|
|
103 * </ul>
|
|
104 *
|
|
105 * @see SWT#READ_ONLY
|
|
106 * @see SWT#WRAP
|
|
107 * @see Widget#checkSubclass
|
|
108 * @see Widget#getStyle
|
|
109 */
|
|
110 public this (Composite parent, int style) {
|
|
111 super (parent, checkStyle (style));
|
|
112 }
|
|
113
|
|
114 /**
|
|
115 * Adds the listener to the collection of listeners who will
|
|
116 * be notified when the receiver's text is modified, by sending
|
|
117 * it one of the messages defined in the <code>ModifyListener</code>
|
|
118 * interface.
|
|
119 *
|
|
120 * @param listener the listener which should be notified
|
|
121 *
|
|
122 * @exception IllegalArgumentException <ul>
|
|
123 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
124 * </ul>
|
|
125 * @exception SWTException <ul>
|
|
126 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
127 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
128 * </ul>
|
|
129 *
|
|
130 * @see ModifyListener
|
|
131 * @see #removeModifyListener
|
|
132 */
|
|
133 public void addModifyListener (ModifyListener listener) {
|
|
134 checkWidget ();
|
|
135 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
136 TypedListener typedListener = new TypedListener (listener);
|
|
137 addListener (SWT.Modify, typedListener);
|
|
138 }
|
|
139
|
|
140 /**
|
|
141 * Adds the listener to the collection of listeners who will
|
|
142 * be notified when the control is selected by the user, by sending
|
|
143 * it one of the messages defined in the <code>SelectionListener</code>
|
|
144 * interface.
|
|
145 * <p>
|
|
146 * <code>widgetSelected</code> is not called for texts.
|
|
147 * <code>widgetDefaultSelected</code> is typically called when ENTER is pressed in a single-line text.
|
|
148 * </p>
|
|
149 *
|
|
150 * @param listener the listener which should be notified when the control is selected by the user
|
|
151 *
|
|
152 * @exception IllegalArgumentException <ul>
|
|
153 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
154 * </ul>
|
|
155 * @exception SWTException <ul>
|
|
156 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
157 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
158 * </ul>
|
|
159 *
|
|
160 * @see SelectionListener
|
|
161 * @see #removeSelectionListener
|
|
162 * @see SelectionEvent
|
|
163 */
|
|
164 public void addSelectionListener(SelectionListener listener) {
|
|
165 checkWidget ();
|
|
166 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
167 TypedListener typedListener = new TypedListener(listener);
|
|
168 addListener(SWT.Selection,typedListener);
|
|
169 addListener(SWT.DefaultSelection,typedListener);
|
|
170 }
|
|
171
|
|
172 /**
|
|
173 * Adds the listener to the collection of listeners who will
|
|
174 * be notified when the receiver's text is verified, by sending
|
|
175 * it one of the messages defined in the <code>VerifyListener</code>
|
|
176 * interface.
|
|
177 *
|
|
178 * @param listener the listener which should be notified
|
|
179 *
|
|
180 * @exception IllegalArgumentException <ul>
|
|
181 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
182 * </ul>
|
|
183 * @exception SWTException <ul>
|
|
184 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
185 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
186 * </ul>
|
|
187 *
|
|
188 * @see VerifyListener
|
|
189 * @see #removeVerifyListener
|
|
190 */
|
|
191 void addVerifyListener (VerifyListener listener) {
|
|
192 checkWidget();
|
|
193 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
194 TypedListener typedListener = new TypedListener (listener);
|
|
195 addListener (SWT.Verify, typedListener);
|
|
196 }
|
|
197
|
|
198 static int checkStyle (int style) {
|
|
199 /*
|
|
200 * Even though it is legal to create this widget
|
|
201 * with scroll bars, they serve no useful purpose
|
|
202 * because they do not automatically scroll the
|
|
203 * widget's client area. The fix is to clear
|
|
204 * the SWT style.
|
|
205 */
|
|
206 return style & ~(SWT.H_SCROLL | SWT.V_SCROLL);
|
|
207 }
|
|
208
|
|
209 protected override void checkSubclass () {
|
|
210 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
|
|
211 }
|
|
212
|
|
213 public override Point computeSize (int wHint, int hHint, bool changed) {
|
|
214 checkWidget ();
|
|
215 if (wHint !is SWT.DEFAULT && wHint < 0) wHint = 0;
|
|
216 if (hHint !is SWT.DEFAULT && hHint < 0) hHint = 0;
|
|
217 int[1] w, h;
|
|
218 OS.gtk_widget_realize (handle);
|
|
219 auto layout = OS.gtk_entry_get_layout (cast(GtkEntry*)handle);
|
|
220 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
221 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
222 for (int i = 0; i < digits; i++) adjustment.upper *= 10;
|
|
223 String string = to!(String)( (cast(int) adjustment.upper) );
|
|
224 if (digits > 0) {
|
|
225 //PROTING_TODO: Efficiency
|
|
226 String buffer = string ~ getDecimalSeparator ();
|
|
227 int count = digits - string.length;
|
|
228 while (count >= 0) {
|
|
229 buffer ~= "0";
|
|
230 count--;
|
|
231 }
|
|
232 string = buffer;
|
|
233 }
|
|
234 char [] buffer1 = string;
|
|
235 auto ptr = OS.pango_layout_get_text (layout);
|
|
236 String buffer2 = fromStringz( ptr ).dup;
|
|
237 OS.pango_layout_set_text (layout, buffer1.ptr, buffer1.length);
|
|
238 OS.pango_layout_get_size (layout, w.ptr, h.ptr);
|
|
239 OS.pango_layout_set_text (layout, buffer2.ptr, buffer2.length);
|
|
240 int width = OS.PANGO_PIXELS (w [0]);
|
|
241 int height = OS.PANGO_PIXELS (h [0]);
|
|
242 width = wHint is SWT.DEFAULT ? width : wHint;
|
|
243 height = hHint is SWT.DEFAULT ? height : hHint;
|
|
244 Rectangle trim = computeTrim (0, 0, width, height);
|
|
245 return new Point (trim.width, trim.height);
|
|
246 }
|
|
247
|
|
248 override public Rectangle computeTrim (int x, int y, int width, int height) {
|
|
249 checkWidget ();
|
|
250 int xborder = 0, yborder = 0;
|
|
251 auto style = OS.gtk_widget_get_style (handle);
|
|
252 if ((this.style & SWT.BORDER) !is 0) {
|
|
253 xborder += OS.gtk_style_get_xthickness (style);
|
|
254 yborder += OS.gtk_style_get_ythickness (style);
|
|
255 }
|
|
256 xborder += INNER_BORDER;
|
|
257 yborder += INNER_BORDER;
|
|
258 int property;
|
|
259 OS.gtk_widget_style_get1 (handle, OS.interior_focus.ptr, &property);
|
|
260 if (property is 0) {
|
|
261 OS.gtk_widget_style_get1 (handle, OS.focus_line_width.ptr, &property);
|
|
262 xborder += property;
|
|
263 yborder += property;
|
|
264 }
|
|
265 auto fontDesc = OS.gtk_style_get_font_desc (style);
|
|
266 int fontSize = OS.pango_font_description_get_size (fontDesc);
|
|
267 int arrowSize = Math.max (OS.PANGO_PIXELS (fontSize), MIN_ARROW_WIDTH);
|
|
268 arrowSize = arrowSize - arrowSize % 2;
|
|
269 Rectangle trim = super.computeTrim (x, y, width, height);
|
|
270 trim.x -= xborder;
|
|
271 trim.y -= yborder;
|
|
272 trim.width += 2 * xborder;
|
|
273 trim.height += 2 * yborder;
|
|
274 trim.width += arrowSize + (2 * OS.gtk_style_get_xthickness (style));
|
|
275 return new Rectangle (trim.x, trim.y, trim.width, trim.height);
|
|
276 }
|
|
277
|
|
278 /**
|
|
279 * Copies the selected text.
|
|
280 * <p>
|
|
281 * The current selection is copied to the clipboard.
|
|
282 * </p>
|
|
283 *
|
|
284 * @exception SWTException <ul>
|
|
285 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
286 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
287 * </ul>
|
|
288 */
|
|
289 public void copy () {
|
|
290 checkWidget ();
|
|
291 OS.gtk_editable_copy_clipboard (cast(GtkEditable*)handle);
|
|
292 }
|
|
293
|
|
294 override void createHandle (int index) {
|
|
295 state |= HANDLE | MENU;
|
|
296 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null);
|
|
297 if (fixedHandle is null) error (SWT.ERROR_NO_HANDLES);
|
|
298 OS.gtk_fixed_set_has_window (cast(GtkFixed*)fixedHandle, true);
|
|
299 auto adjustment = OS.gtk_adjustment_new (0, 0, 100, 1, 10, 0);
|
|
300 if (adjustment is null) error (SWT.ERROR_NO_HANDLES);
|
|
301 handle = cast(GtkWidget*)OS.gtk_spin_button_new (cast(GtkAdjustment*)adjustment, 1, 0);
|
|
302 if (handle is null) error (SWT.ERROR_NO_HANDLES);
|
|
303 OS.gtk_container_add (cast(GtkContainer*)fixedHandle, handle);
|
|
304 OS.gtk_editable_set_editable (cast(GtkEditable*)handle, (style & SWT.READ_ONLY) is 0);
|
|
305 OS.gtk_entry_set_has_frame (cast(GtkEntry*)handle, (style & SWT.BORDER) !is 0);
|
|
306 OS.gtk_spin_button_set_wrap (cast(GtkSpinButton*)handle, (style & SWT.WRAP) !is 0);
|
|
307 }
|
|
308
|
|
309 /**
|
|
310 * Cuts the selected text.
|
|
311 * <p>
|
|
312 * The current selection is first copied to the
|
|
313 * clipboard and then deleted from the widget.
|
|
314 * </p>
|
|
315 *
|
|
316 * @exception SWTException <ul>
|
|
317 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
318 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
319 * </ul>
|
|
320 */
|
|
321 public void cut () {
|
|
322 checkWidget ();
|
|
323 OS.gtk_editable_cut_clipboard (cast(GtkEditable*)handle);
|
|
324 }
|
|
325
|
|
326 override void deregister () {
|
|
327 super.deregister ();
|
|
328 auto imContext = imContext ();
|
|
329 if (imContext !is null) display.removeWidget (cast(GtkWidget*)imContext);
|
|
330 }
|
|
331
|
|
332 override GdkDrawable* eventWindow () {
|
|
333 return paintWindow ();
|
|
334 }
|
|
335
|
|
336 override GtkWidget* enterExitHandle () {
|
|
337 return fixedHandle;
|
|
338 }
|
|
339
|
|
340 override bool filterKey (int keyval, GdkEventKey* event) {
|
|
341 int time = OS.gdk_event_get_time (cast(GdkEvent*)event);
|
|
342 if (time !is lastEventTime) {
|
|
343 lastEventTime = time;
|
|
344 auto imContext = imContext ();
|
|
345 if (imContext !is null) {
|
|
346 return cast(bool)OS.gtk_im_context_filter_keypress (imContext, event);
|
|
347 }
|
|
348 }
|
|
349 gdkEventKey = event;
|
|
350 return false;
|
|
351 }
|
|
352
|
|
353 void fixIM () {
|
|
354 /*
|
|
355 * The IM filter has to be called one time for each key press event.
|
|
356 * When the IM is open the key events are duplicated. The first event
|
|
357 * is filtered by SWT and the second event is filtered by GTK. In some
|
|
358 * cases the GTK handler does not run (the widget is destroyed, the
|
|
359 * application code consumes the event, etc), for these cases the IM
|
|
360 * filter has to be called by SWT.
|
|
361 */
|
|
362 if (gdkEventKey !is null && gdkEventKey !is cast(GdkEventKey*)-1) {
|
|
363 auto imContext = imContext ();
|
|
364 if (imContext !is null) {
|
|
365 OS.gtk_im_context_filter_keypress (imContext, gdkEventKey);
|
|
366 gdkEventKey = cast(GdkEventKey*)-1;
|
|
367 return;
|
|
368 }
|
|
369 }
|
|
370 gdkEventKey = null;
|
|
371 }
|
|
372
|
|
373 override GdkColor* getBackgroundColor () {
|
|
374 return getBaseColor ();
|
|
375 }
|
|
376
|
|
377 public override int getBorderWidth () {
|
|
378 checkWidget();
|
|
379 auto style = OS.gtk_widget_get_style (handle);
|
|
380 if ((this.style & SWT.BORDER) !is 0) {
|
|
381 return OS.gtk_style_get_xthickness (style);
|
|
382 }
|
|
383 return 0;
|
|
384 }
|
|
385
|
|
386 override GdkColor* getForegroundColor () {
|
|
387 return getTextColor ();
|
|
388 }
|
|
389
|
|
390 /**
|
|
391 * Returns the amount that the receiver's value will be
|
|
392 * modified by when the up/down arrows are pressed.
|
|
393 *
|
|
394 * @return the increment
|
|
395 *
|
|
396 * @exception SWTException <ul>
|
|
397 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
398 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
399 * </ul>
|
|
400 */
|
|
401 public int getIncrement () {
|
|
402 checkWidget ();
|
|
403 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
404 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
405 auto value = adjustment.step_increment;
|
|
406 for (int i = 0; i < digits; i++) value *= 10;
|
|
407 return cast(int) (value > 0 ? value + 0.5 : value - 0.5);
|
|
408 }
|
|
409
|
|
410 /**
|
|
411 * Returns the maximum value which the receiver will allow.
|
|
412 *
|
|
413 * @return the maximum
|
|
414 *
|
|
415 * @exception SWTException <ul>
|
|
416 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
417 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
418 * </ul>
|
|
419 */
|
|
420 public int getMaximum () {
|
|
421 checkWidget ();
|
|
422 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
423 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
424 auto value = adjustment.upper;
|
|
425 for (int i = 0; i < digits; i++) value *= 10;
|
|
426 return cast(int) (value > 0 ? value + 0.5 : value - 0.5);
|
|
427 }
|
|
428
|
|
429 /**
|
|
430 * Returns the minimum value which the receiver will allow.
|
|
431 *
|
|
432 * @return the minimum
|
|
433 *
|
|
434 * @exception SWTException <ul>
|
|
435 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
436 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
437 * </ul>
|
|
438 */
|
|
439 public int getMinimum () {
|
|
440 checkWidget ();
|
|
441 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
442 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
443 auto value = adjustment.lower;
|
|
444 for (int i = 0; i < digits; i++) value *= 10;
|
|
445 return cast(int) (value > 0 ? value + 0.5 : value - 0.5);
|
|
446 }
|
|
447
|
|
448 /**
|
|
449 * Returns the amount that the receiver's position will be
|
|
450 * modified by when the page up/down keys are pressed.
|
|
451 *
|
|
452 * @return the page increment
|
|
453 *
|
|
454 * @exception SWTException <ul>
|
|
455 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
456 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
457 * </ul>
|
|
458 */
|
|
459 public int getPageIncrement () {
|
|
460 checkWidget ();
|
|
461 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
462 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
463 auto value = adjustment.page_increment;
|
|
464 for (int i = 0; i < digits; i++) value *= 10;
|
|
465 return cast(int) (value > 0 ? value + 0.5 : value - 0.5);
|
|
466 }
|
|
467
|
|
468 /**
|
|
469 * Returns the <em>selection</em>, which is the receiver's position.
|
|
470 *
|
|
471 * @return the selection
|
|
472 *
|
|
473 * @exception SWTException <ul>
|
|
474 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
475 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
476 * </ul>
|
|
477 */
|
|
478 public int getSelection () {
|
|
479 checkWidget ();
|
|
480 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
481 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
482 auto value = adjustment.value;
|
|
483 for (int i = 0; i < digits; i++) value *= 10;
|
|
484 return cast(int) (value > 0 ? value + 0.5 : value - 0.5);
|
|
485 }
|
|
486
|
|
487 /**
|
|
488 * Returns a string containing a copy of the contents of the
|
|
489 * receiver's text field, or an empty string if there are no
|
|
490 * contents.
|
|
491 *
|
|
492 * @return the receiver's text
|
|
493 *
|
|
494 * @exception SWTException <ul>
|
|
495 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
496 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
497 * </ul>
|
|
498 *
|
|
499 * @since 3.4
|
|
500 */
|
|
501 public String getText () {
|
|
502 checkWidget ();
|
|
503 auto str = OS.gtk_entry_get_text (handle);
|
|
504 return fromStringz(str).dup;
|
|
505 }
|
|
506
|
|
507 /**
|
|
508 * Returns the maximum number of characters that the receiver's
|
|
509 * text field is capable of holding. If this has not been changed
|
|
510 * by <code>setTextLimit()</code>, it will be the constant
|
|
511 * <code>Spinner.LIMIT</code>.
|
|
512 *
|
|
513 * @return the text limit
|
|
514 *
|
|
515 * @exception SWTException <ul>
|
|
516 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
517 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
518 * </ul>
|
|
519 *
|
|
520 * @see #LIMIT
|
|
521 *
|
|
522 * @since 3.4
|
|
523 */
|
|
524 public int getTextLimit () {
|
|
525 checkWidget ();
|
|
526 int limit = OS.gtk_entry_get_max_length (handle);
|
|
527 return limit is 0 ? 0xFFFF : limit;
|
|
528 }
|
|
529
|
|
530 /**
|
|
531 * Returns the number of decimal places used by the receiver.
|
|
532 *
|
|
533 * @return the digits
|
|
534 *
|
|
535 * @exception SWTException <ul>
|
|
536 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
537 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
538 * </ul>
|
|
539 */
|
|
540 public int getDigits () {
|
|
541 checkWidget ();
|
|
542 return OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
543 }
|
|
544
|
|
545 String getDecimalSeparator () {
|
|
546 auto ptr = OS.localeconv_decimal_point ();
|
|
547 return fromStringz( ptr ).dup;
|
|
548 }
|
|
549
|
|
550 override int /*long*/ gtk_activate (GtkWidget* widget) {
|
|
551 postEvent (SWT.DefaultSelection);
|
|
552 return 0;
|
|
553 }
|
|
554
|
|
555 override int /*long*/ gtk_changed (GtkWidget* widget) {
|
|
556 auto str = OS.gtk_entry_get_text (cast(GtkEntry*)handle);
|
|
557 int length = tango.stdc.string.strlen (str);
|
|
558 if (length > 0) {
|
|
559 char* endptr;
|
|
560 double value = OS.g_strtod (str, &endptr);
|
|
561 if (endptr is str + length) {
|
|
562 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
563 if (value !is adjustment.value && adjustment.lower <= value && value <= adjustment.upper) {
|
|
564 OS.gtk_spin_button_update (cast(GtkSpinButton*)handle);
|
|
565 }
|
|
566 }
|
|
567 }
|
|
568
|
|
569 /*
|
|
570 * Feature in GTK. When the user types, GTK positions
|
|
571 * the caret after sending the changed signal. This
|
|
572 * means that application code that attempts to position
|
|
573 * the caret during a changed signal will fail. The fix
|
|
574 * is to post the modify event when the user is typing.
|
|
575 */
|
|
576 bool keyPress = false;
|
|
577 auto eventPtr = OS.gtk_get_current_event ();
|
|
578 if (eventPtr !is null) {
|
|
579 GdkEventKey* gdkEvent = cast(GdkEventKey*)eventPtr;
|
|
580 switch (gdkEvent.type) {
|
|
581 case OS.GDK_KEY_PRESS:
|
|
582 keyPress = true;
|
|
583 break;
|
|
584 default:
|
|
585 }
|
|
586 OS.gdk_event_free (eventPtr);
|
|
587 }
|
|
588 if (keyPress) {
|
|
589 postEvent (SWT.Modify);
|
|
590 } else {
|
|
591 sendEvent (SWT.Modify);
|
|
592 }
|
|
593 return 0;
|
|
594 }
|
|
595
|
|
596 int /*long*/ gtk_commit (GtkIMContext* imContext, char* text) {
|
|
597 if (text is null) return 0;
|
|
598 if (!OS.gtk_editable_get_editable (cast(GtkEditable*)handle)) return 0;
|
|
599 char [] chars = fromStringz( text ).dup;
|
|
600 if (chars.length is 0) return 0;
|
|
601 char [] newChars = sendIMKeyEvent (SWT.KeyDown, null, chars);
|
|
602 if (newChars is null) return 0;
|
|
603 /*
|
|
604 * Feature in GTK. For a GtkEntry, during the insert-text signal,
|
|
605 * GTK allows the programmer to change only the caret location,
|
|
606 * not the selection. If the programmer changes the selection,
|
|
607 * the new selection is lost. The fix is to detect a selection
|
|
608 * change and set it after the insert-text signal has completed.
|
|
609 */
|
|
610 fixStart = fixEnd = -1;
|
|
611 OS.g_signal_handlers_block_matched (imContext, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCOMMIT);
|
|
612 int id = OS.g_signal_lookup (OS.commit.ptr, OS.gtk_im_context_get_type ());
|
|
613 int mask = OS.G_SIGNAL_MATCH_DATA | OS.G_SIGNAL_MATCH_ID;
|
|
614 OS.g_signal_handlers_unblock_matched (imContext, mask, id, 0, null, null, cast(void*)handle);
|
|
615 if (newChars is chars) {
|
|
616 OS.g_signal_emit_by_name1 (imContext, OS.commit.ptr, cast(int)text);
|
|
617 } else {
|
|
618 OS.g_signal_emit_by_name1 (imContext, OS.commit.ptr, cast(int)toStringz(newChars));
|
|
619 }
|
|
620 OS.g_signal_handlers_unblock_matched (imContext, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCOMMIT);
|
|
621 OS.g_signal_handlers_block_matched (imContext, mask, id, 0, null, null, cast(void*)handle);
|
|
622 if (fixStart !is -1 && fixEnd !is -1) {
|
|
623 OS.gtk_editable_set_position (cast(GtkEditable*)handle, fixStart);
|
|
624 OS.gtk_editable_select_region (cast(GtkEditable*)handle, fixStart, fixEnd);
|
|
625 }
|
|
626 fixStart = fixEnd = -1;
|
|
627 return 0;
|
|
628 }
|
|
629
|
|
630 override int /*long*/ gtk_delete_text (GtkWidget* widget, int start_pos, int end_pos) {
|
|
631 if (!hooks (SWT.Verify) && !filters (SWT.Verify)) return 0;
|
|
632 String newText = verifyText ("", cast(int)/*64*/start_pos, cast(int)/*64*/end_pos);
|
|
633 if (newText is null) {
|
|
634 OS.g_signal_stop_emission_by_name (handle, OS.delete_text.ptr);
|
|
635 } else {
|
|
636 if (newText.length > 0) {
|
|
637 int pos;
|
|
638 pos = cast(int)/*64*/end_pos;
|
|
639 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
|
|
640 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udINSERT_TEXT);
|
|
641 OS.gtk_editable_insert_text (cast(GtkEditable*)handle, newText.ptr, newText.length, &pos);
|
|
642 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udINSERT_TEXT);
|
|
643 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
|
|
644 OS.gtk_editable_set_position (cast(GtkEditable*)handle, pos);
|
|
645 }
|
|
646 }
|
|
647 return 0;
|
|
648 }
|
|
649
|
|
650 override int /*long*/ gtk_event_after (GtkWidget* widget, GdkEvent* gdkEvent) {
|
|
651 if (cursor !is null) gtk_setCursor (cursor.handle);
|
|
652 return super.gtk_event_after (widget, gdkEvent);
|
|
653 }
|
|
654
|
|
655 override int /*long*/ gtk_focus_out_event (GtkWidget* widget, GdkEventFocus* event) {
|
|
656 fixIM ();
|
|
657 return super.gtk_focus_out_event (widget, event);
|
|
658 }
|
|
659
|
|
660 override int /*long*/ gtk_insert_text (GtkEditable* widget, char* new_text, int new_text_length, int position) {
|
|
661 // if (!hooks (SWT.Verify) && !filters (SWT.Verify)) return 0;
|
|
662 if (new_text is null || new_text_length is 0) return 0;
|
|
663 String oldText = new_text[ 0 .. new_text_length ].dup;
|
|
664 int pos;
|
|
665 pos = position;
|
|
666 if (pos is -1) {
|
|
667 auto ptr = OS.gtk_entry_get_text (cast(GtkEntry*)handle);
|
|
668 pos = cast(int)/*64*/OS.g_utf8_strlen (ptr, -1);
|
|
669 }
|
|
670 String newText = verifyText (oldText, pos, pos);
|
|
671 if (newText !is oldText) {
|
|
672 int newStart, newEnd;
|
|
673 OS.gtk_editable_get_selection_bounds (cast(GtkEditable*)handle, &newStart, &newEnd);
|
|
674 if (newText !is null) {
|
|
675 if (newStart !is newEnd) {
|
|
676 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udDELETE_TEXT);
|
|
677 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
|
|
678 OS.gtk_editable_delete_selection (cast(GtkEditable*)handle);
|
|
679 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udDELETE_TEXT);
|
|
680 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udCHANGED);
|
|
681 }
|
|
682 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udINSERT_TEXT);
|
|
683 OS.gtk_editable_insert_text (cast(GtkEditable*)handle, newText.ptr, newText.length, &pos);
|
|
684 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udINSERT_TEXT);
|
|
685 newStart = newEnd = pos;
|
|
686 }
|
|
687 pos = newEnd;
|
|
688 if (newStart !is newEnd ) {
|
|
689 fixStart = newStart ;
|
|
690 fixEnd = newEnd ;
|
|
691 }
|
|
692 position = pos;
|
|
693 OS.g_signal_stop_emission_by_name (handle, OS.insert_text.ptr);
|
|
694 }
|
|
695 return 0;
|
|
696 }
|
|
697
|
|
698 override int /*long*/ gtk_key_press_event (GtkWidget* widget, GdkEventKey* event) {
|
|
699 auto result = super.gtk_key_press_event (widget, event);
|
|
700 if (result !is 0) fixIM ();
|
|
701 if (gdkEventKey is cast(GdkEventKey*)-1) result = 1;
|
|
702 gdkEventKey = null;
|
|
703 return result;
|
|
704 }
|
|
705
|
|
706 override int /*long*/ gtk_populate_popup (GtkWidget* widget, GtkWidget* menu) {
|
|
707 if ((style & SWT.RIGHT_TO_LEFT) !is 0) {
|
|
708 OS.gtk_widget_set_direction (menu, OS.GTK_TEXT_DIR_RTL);
|
|
709 display.doSetDirectionProc( menu, OS.GTK_TEXT_DIR_RTL);
|
|
710 }
|
|
711 return 0;
|
|
712 }
|
|
713
|
|
714 override int /*long*/ gtk_value_changed (int adjustment) {
|
|
715 postEvent (SWT.Selection);
|
|
716 return 0;
|
|
717 }
|
|
718
|
|
719 override void hookEvents () {
|
|
720 super.hookEvents();
|
|
721 OS.g_signal_connect_closure (handle, OS.changed.ptr, display.closures [CHANGED], true);
|
|
722 OS.g_signal_connect_closure (handle, OS.insert_text.ptr, display.closures [INSERT_TEXT], false);
|
|
723 OS.g_signal_connect_closure (handle, OS.delete_text.ptr, display.closures [DELETE_TEXT], false);
|
|
724 OS.g_signal_connect_closure (handle, OS.value_changed.ptr, display.closures [VALUE_CHANGED], false);
|
|
725 OS.g_signal_connect_closure (handle, OS.activate.ptr, display.closures [ACTIVATE], false);
|
|
726 OS.g_signal_connect_closure (handle, OS.populate_popup.ptr, display.closures [POPULATE_POPUP], false);
|
|
727 auto imContext = imContext ();
|
|
728 if (imContext !is null) {
|
|
729 OS.g_signal_connect_closure (imContext, OS.commit.ptr, display.closures [COMMIT], false);
|
|
730 int id = OS.g_signal_lookup (OS.commit.ptr, OS.gtk_im_context_get_type ());
|
|
731 int mask = OS.G_SIGNAL_MATCH_DATA | OS.G_SIGNAL_MATCH_ID;
|
|
732 OS.g_signal_handlers_block_matched (imContext, mask, id, 0, null, null, cast(void*)handle);
|
|
733 }
|
|
734 }
|
|
735
|
|
736 GtkIMContext* imContext () {
|
|
737 return OS.GTK_ENTRY_IM_CONTEXT (cast(GtkEntry*)handle);
|
|
738 }
|
|
739
|
|
740 override GdkDrawable* paintWindow () {
|
|
741 auto window = super.paintWindow ();
|
|
742 auto children = OS.gdk_window_get_children (window);
|
|
743 if (children !is null) window = cast(GdkDrawable*)OS.g_list_data (children);
|
|
744 OS.g_list_free (children);
|
|
745 return window;
|
|
746 }
|
|
747
|
|
748 /**
|
|
749 * Pastes text from clipboard.
|
|
750 * <p>
|
|
751 * The selected text is deleted from the widget
|
|
752 * and new text inserted from the clipboard.
|
|
753 * </p>
|
|
754 *
|
|
755 * @exception SWTException <ul>
|
|
756 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
757 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
758 * </ul>
|
|
759 */
|
|
760 public void paste () {
|
|
761 checkWidget ();
|
|
762 OS.gtk_editable_paste_clipboard (cast(GtkEditable*)handle);
|
|
763 }
|
|
764
|
|
765 override void register () {
|
|
766 super.register ();
|
|
767 auto imContext = imContext ();
|
|
768 if (imContext !is null) display.addWidget (cast(GtkWidget*)imContext, this);
|
|
769 }
|
|
770
|
|
771 override void releaseWidget () {
|
|
772 super.releaseWidget ();
|
|
773 fixIM ();
|
|
774 }
|
|
775
|
|
776 /**
|
|
777 * Removes the listener from the collection of listeners who will
|
|
778 * be notified when the receiver's text is modified.
|
|
779 *
|
|
780 * @param listener the listener which should no longer be notified
|
|
781 *
|
|
782 * @exception IllegalArgumentException <ul>
|
|
783 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
784 * </ul>
|
|
785 * @exception SWTException <ul>
|
|
786 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
787 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
788 * </ul>
|
|
789 *
|
|
790 * @see ModifyListener
|
|
791 * @see #addModifyListener
|
|
792 */
|
|
793 public void removeModifyListener (ModifyListener listener) {
|
|
794 checkWidget ();
|
|
795 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
796 if (eventTable is null) return;
|
|
797 eventTable.unhook (SWT.Modify, listener);
|
|
798 }
|
|
799
|
|
800 /**
|
|
801 * Removes the listener from the collection of listeners who will
|
|
802 * be notified when the control is selected by the user.
|
|
803 *
|
|
804 * @param listener the listener which should no longer be notified
|
|
805 *
|
|
806 * @exception IllegalArgumentException <ul>
|
|
807 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
808 * </ul>
|
|
809 * @exception SWTException <ul>
|
|
810 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
811 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
812 * </ul>
|
|
813 *
|
|
814 * @see SelectionListener
|
|
815 * @see #addSelectionListener
|
|
816 */
|
|
817 public void removeSelectionListener(SelectionListener listener) {
|
|
818 checkWidget ();
|
|
819 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
820 if (eventTable is null) return;
|
|
821 eventTable.unhook(SWT.Selection, listener);
|
|
822 eventTable.unhook(SWT.DefaultSelection,listener);
|
|
823 }
|
|
824
|
|
825 /**
|
|
826 * Removes the listener from the collection of listeners who will
|
|
827 * be notified when the control is verified.
|
|
828 *
|
|
829 * @param listener the listener which should be notified
|
|
830 *
|
|
831 * @exception IllegalArgumentException <ul>
|
|
832 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
833 * </ul>
|
|
834 * @exception SWTException <ul>
|
|
835 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
836 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
837 * </ul>
|
|
838 *
|
|
839 * @see VerifyListener
|
|
840 * @see #addVerifyListener
|
|
841 */
|
|
842 void removeVerifyListener (VerifyListener listener) {
|
|
843 checkWidget ();
|
|
844 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
845 if (eventTable is null) return;
|
|
846 eventTable.unhook (SWT.Verify, listener);
|
|
847 }
|
|
848
|
|
849 override void setBackgroundColor (GdkColor* color) {
|
|
850 super.setBackgroundColor (color);
|
|
851 OS.gtk_widget_modify_base (handle, 0, color);
|
|
852 }
|
|
853
|
|
854 override void gtk_setCursor (GdkCursor* cursor) {
|
|
855 GdkCursor* defaultCursor;
|
|
856 if (cursor is null) defaultCursor = OS.gdk_cursor_new (OS.GDK_XTERM);
|
|
857 super.gtk_setCursor (cursor !is null ? cursor : defaultCursor);
|
|
858 if (cursor is null) OS.gdk_cursor_destroy (defaultCursor);
|
|
859 }
|
|
860
|
|
861 override void setFontDescription (PangoFontDescription* font) {
|
|
862 super.setFontDescription (font);
|
|
863 }
|
|
864
|
|
865 /**
|
|
866 * Sets the amount that the receiver's value will be
|
|
867 * modified by when the up/down arrows are pressed to
|
|
868 * the argument, which must be at least one.
|
|
869 *
|
|
870 * @param value the new increment (must be greater than zero)
|
|
871 *
|
|
872 * @exception SWTException <ul>
|
|
873 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
874 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
875 * </ul>
|
|
876 */
|
|
877 public void setIncrement (int value) {
|
|
878 checkWidget ();
|
|
879 if (value < 1) return;
|
|
880 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
881 double newValue = value;
|
|
882 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
883 for (int i = 0; i < digits; i++) newValue /= 10;
|
|
884 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
885 OS.gtk_spin_button_set_increments (cast(GtkSpinButton*)handle, newValue, adjustment.page_increment);
|
|
886 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
887 }
|
|
888
|
|
889 /**
|
|
890 * Sets the maximum value that the receiver will allow. This new
|
|
891 * value will be ignored if it is not greater than the receiver's current
|
|
892 * minimum value. If the new maximum is applied then the receiver's
|
|
893 * selection value will be adjusted if necessary to fall within its new range.
|
|
894 *
|
|
895 * @param value the new maximum, which must be greater than the current minimum
|
|
896 *
|
|
897 * @exception SWTException <ul>
|
|
898 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
899 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
900 * </ul>
|
|
901 */
|
|
902 public void setMaximum (int value) {
|
|
903 checkWidget ();
|
|
904 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
905 double newValue = value;
|
|
906 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
907 for (int i = 0; i < digits; i++) newValue /= 10;
|
|
908 if (newValue <= adjustment.lower) return;
|
|
909 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
910 OS.gtk_spin_button_set_range (cast(GtkSpinButton*)handle, adjustment.lower, newValue);
|
|
911 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
912 }
|
|
913
|
|
914 /**
|
|
915 * Sets the minimum value that the receiver will allow. This new
|
|
916 * value will be ignored if it is not less than the receiver's
|
|
917 * current maximum value. If the new minimum is applied then the receiver's
|
|
918 * selection value will be adjusted if necessary to fall within its new range.
|
|
919 *
|
|
920 * @param value the new minimum, which must be less than the current maximum
|
|
921 *
|
|
922 * @exception SWTException <ul>
|
|
923 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
924 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
925 * </ul>
|
|
926 */
|
|
927 public void setMinimum (int value) {
|
|
928 checkWidget ();
|
|
929 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
930 double newValue = value;
|
|
931 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
932 for (int i = 0; i < digits; i++) newValue /= 10;
|
|
933 if (newValue >= adjustment.upper) return;
|
|
934 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
935 OS.gtk_spin_button_set_range (cast(GtkSpinButton*)handle, newValue, adjustment.upper);
|
|
936 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
937 }
|
|
938
|
|
939 /**
|
|
940 * Sets the amount that the receiver's position will be
|
|
941 * modified by when the page up/down keys are pressed
|
|
942 * to the argument, which must be at least one.
|
|
943 *
|
|
944 * @param value the page increment (must be greater than zero)
|
|
945 *
|
|
946 * @exception SWTException <ul>
|
|
947 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
948 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
949 * </ul>
|
|
950 */
|
|
951 public void setPageIncrement (int value) {
|
|
952 checkWidget ();
|
|
953 if (value < 1) return;
|
|
954 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
955 double newValue = value;
|
|
956 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
957 for (int i = 0; i < digits; i++) newValue /= 10;
|
|
958 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
959 OS.gtk_spin_button_set_increments (cast(GtkSpinButton*)handle, adjustment.step_increment, newValue);
|
|
960 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
961 }
|
|
962
|
|
963 /**
|
|
964 * Sets the <em>selection</em>, which is the receiver's
|
|
965 * position, to the argument. If the argument is not within
|
|
966 * the range specified by minimum and maximum, it will be
|
|
967 * adjusted to fall within this range.
|
|
968 *
|
|
969 * @param value the new selection (must be zero or greater)
|
|
970 *
|
|
971 * @exception SWTException <ul>
|
|
972 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
973 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
974 * </ul>
|
|
975 */
|
|
976 public void setSelection (int value) {
|
|
977 checkWidget ();
|
|
978 double newValue = value;
|
|
979 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
980 for (int i = 0; i < digits; i++) newValue /= 10;
|
|
981 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
982 OS.gtk_spin_button_set_value (cast(GtkSpinButton*)handle, newValue);
|
|
983 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
984 }
|
|
985
|
|
986 /**
|
|
987 * Sets the maximum number of characters that the receiver's
|
|
988 * text field is capable of holding to be the argument.
|
|
989 * <p>
|
|
990 * To reset this value to the default, use <code>setTextLimit(Spinner.LIMIT)</code>.
|
|
991 * Specifying a limit value larger than <code>Spinner.LIMIT</code> sets the
|
|
992 * receiver's limit to <code>Spinner.LIMIT</code>.
|
|
993 * </p>
|
|
994 * @param limit new text limit
|
|
995 *
|
|
996 * @exception IllegalArgumentException <ul>
|
|
997 * <li>ERROR_CANNOT_BE_ZERO - if the limit is zero</li>
|
|
998 * </ul>
|
|
999 * @exception SWTException <ul>
|
|
1000 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1001 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1002 * </ul>
|
|
1003 *
|
|
1004 * @see #LIMIT
|
|
1005 *
|
|
1006 * @since 3.4
|
|
1007 */
|
|
1008 public void setTextLimit (int limit) {
|
|
1009 checkWidget ();
|
|
1010 if (limit is 0) error (SWT.ERROR_CANNOT_BE_ZERO);
|
|
1011 OS.gtk_entry_set_max_length (handle, limit);
|
|
1012 }
|
|
1013
|
|
1014 /**
|
|
1015 * Sets the number of decimal places used by the receiver.
|
|
1016 * <p>
|
|
1017 * The digit setting is used to allow for floating point values in the receiver.
|
|
1018 * For example, to set the selection to a floating point value of 1.37 call setDigits() with
|
|
1019 * a value of 2 and setSelection() with a value of 137. Similarly, if getDigits() has a value
|
|
1020 * of 2 and getSelection() returns 137 this should be interpreted as 1.37. This applies to all
|
|
1021 * numeric APIs.
|
|
1022 * </p>
|
|
1023 *
|
|
1024 * @param value the new digits (must be greater than or equal to zero)
|
|
1025 *
|
|
1026 * @exception IllegalArgumentException <ul>
|
|
1027 * <li>ERROR_INVALID_ARGUMENT - if the value is less than zero</li>
|
|
1028 * </ul>
|
|
1029 * @exception SWTException <ul>
|
|
1030 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1031 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1032 * </ul>
|
|
1033 */
|
|
1034 public void setDigits (int value) {
|
|
1035 checkWidget ();
|
|
1036 if (value < 0) error (SWT.ERROR_INVALID_ARGUMENT);
|
|
1037 int digits = OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle);
|
|
1038 if (value is digits) return;
|
|
1039 auto adjustment = OS.gtk_spin_button_get_adjustment (cast(GtkSpinButton*)handle);
|
|
1040 int diff = Math.abs (value - digits);
|
|
1041 int factor = 1;
|
|
1042 for (int i = 0; i < diff; i++) factor *= 10;
|
|
1043 if (digits > value) {
|
|
1044 adjustment.value *= factor;
|
|
1045 adjustment.upper *= factor;
|
|
1046 adjustment.lower *= factor;
|
|
1047 adjustment.step_increment *= factor;
|
|
1048 adjustment.page_increment *= factor;
|
|
1049 } else {
|
|
1050 adjustment.value /= factor;
|
|
1051 adjustment.upper /= factor;
|
|
1052 adjustment.lower /= factor;
|
|
1053 adjustment.step_increment /= factor;
|
|
1054 adjustment.page_increment /= factor;
|
|
1055 }
|
|
1056 OS.gtk_spin_button_set_digits (cast(GtkSpinButton*)handle, value);
|
|
1057 }
|
|
1058
|
|
1059 /**
|
|
1060 * Sets the receiver's selection, minimum value, maximum
|
|
1061 * value, digits, increment and page increment all at once.
|
|
1062 * <p>
|
|
1063 * Note: This is similar to setting the values individually
|
|
1064 * using the appropriate methods, but may be implemented in a
|
|
1065 * more efficient fashion on some platforms.
|
|
1066 * </p>
|
|
1067 *
|
|
1068 * @param selection the new selection value
|
|
1069 * @param minimum the new minimum value
|
|
1070 * @param maximum the new maximum value
|
|
1071 * @param digits the new digits value
|
|
1072 * @param increment the new increment value
|
|
1073 * @param pageIncrement the new pageIncrement value
|
|
1074 *
|
|
1075 * @exception SWTException <ul>
|
|
1076 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1077 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1078 * </ul>
|
|
1079 *
|
|
1080 * @since 3.2
|
|
1081 */
|
|
1082 public void setValues (int selection, int minimum, int maximum, int digits, int increment, int pageIncrement) {
|
|
1083 checkWidget ();
|
|
1084 if (maximum <= minimum) return;
|
|
1085 if (digits < 0) return;
|
|
1086 if (increment < 1) return;
|
|
1087 if (pageIncrement < 1) return;
|
|
1088 selection = Math.min (Math.max (minimum, selection), maximum);
|
|
1089 double factor = 1;
|
|
1090 for (int i = 0; i < digits; i++) factor *= 10;
|
|
1091 OS.g_signal_handlers_block_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
1092 OS.gtk_spin_button_set_range (cast(GtkSpinButton*)handle, minimum / factor, maximum / factor);
|
|
1093 OS.gtk_spin_button_set_increments (cast(GtkSpinButton*)handle, increment / factor, pageIncrement / factor);
|
|
1094 OS.gtk_spin_button_set_value (cast(GtkSpinButton*)handle, selection / factor);
|
|
1095 OS.gtk_spin_button_set_digits (cast(GtkSpinButton*)handle, digits);
|
|
1096 OS.g_signal_handlers_unblock_matched (handle, OS.G_SIGNAL_MATCH_DATA, 0, 0, null, null, udVALUE_CHANGED);
|
|
1097 }
|
|
1098
|
|
1099 override bool translateTraversal (GdkEventKey* keyEvent) {
|
|
1100 int key = keyEvent.keyval;
|
|
1101 switch (key) {
|
|
1102 case OS.GDK_KP_Enter:
|
|
1103 case OS.GDK_Return: {
|
|
1104 auto imContext = imContext ();
|
|
1105 if (imContext !is null) {
|
|
1106 char* preeditString;
|
|
1107 OS.gtk_im_context_get_preedit_string (imContext, &preeditString, null, null);
|
|
1108 if (preeditString !is null) {
|
|
1109 int length = tango.stdc.string.strlen (preeditString);
|
|
1110 OS.g_free (preeditString);
|
|
1111 if (length !is 0) return false;
|
|
1112 }
|
|
1113 }
|
|
1114 default:
|
|
1115 }
|
|
1116 }
|
|
1117 return super.translateTraversal (keyEvent);
|
|
1118 }
|
|
1119
|
|
1120 String verifyText (String string, int start, int end) {
|
|
1121 if (string.length is 0 && start is end) return null;
|
|
1122 Event event = new Event ();
|
|
1123 event.text = string;
|
|
1124 event.start = start;
|
|
1125 event.end = end;
|
|
1126 auto eventPtr = OS.gtk_get_current_event ();
|
|
1127 if (eventPtr !is null) {
|
|
1128 GdkEventKey* gdkEvent = cast(GdkEventKey*)eventPtr;
|
|
1129 switch (gdkEvent.type) {
|
|
1130 case OS.GDK_KEY_PRESS:
|
|
1131 setKeyState (event, gdkEvent);
|
|
1132 break;
|
|
1133 default:
|
|
1134 }
|
|
1135 OS.gdk_event_free (eventPtr);
|
|
1136 }
|
|
1137 int index = 0;
|
|
1138 if (OS.gtk_spin_button_get_digits (cast(GtkSpinButton*)handle) > 0) {
|
|
1139 String decimalSeparator = getDecimalSeparator ();
|
|
1140 index = string.indexOf( decimalSeparator );
|
|
1141 if (index !is -1 ) {
|
|
1142 string = string.substring( 0, index ) ~ string.substring( index + 1 );
|
|
1143 }
|
|
1144 index = 0;
|
|
1145 }
|
|
1146 if (string.length > 0) {
|
|
1147 auto adjustment = OS.gtk_spin_button_get_adjustment (handle);
|
|
1148 if (adjustment.lower < 0 && string.charAt (0) is '-') index++;
|
|
1149 }
|
|
1150 while (index < string.length) {
|
|
1151 if (!CharacterIsDigit (string.charAt(index))) break;
|
|
1152 index++;
|
|
1153 }
|
|
1154 event.doit = index is string.length;
|
|
1155 /*
|
|
1156 * It is possible (but unlikely), that application
|
|
1157 * code could have disposed the widget in the verify
|
|
1158 * event. If this happens, answer null to cancel
|
|
1159 * the operation.
|
|
1160 */
|
|
1161 sendEvent (SWT.Verify, event);
|
|
1162 if (!event.doit || isDisposed ()) return null;
|
|
1163 return event.text;
|
|
1164 }
|
|
1165
|
|
1166 }
|