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.DateTime;
|
|
14
|
|
15 import org.eclipse.swt.SWT;
|
|
16 import org.eclipse.swt.SWTException;
|
|
17 import org.eclipse.swt.events.SelectionEvent;
|
|
18 import org.eclipse.swt.events.SelectionListener;
|
|
19 import org.eclipse.swt.graphics.Color;
|
|
20 import org.eclipse.swt.graphics.Font;
|
|
21 import org.eclipse.swt.graphics.GC;
|
|
22 import org.eclipse.swt.graphics.Point;
|
|
23 import org.eclipse.swt.graphics.Rectangle;
|
|
24 import org.eclipse.swt.internal.gtk.OS;
|
|
25 import org.eclipse.swt.internal.Compatibility;
|
|
26
|
|
27 import org.eclipse.swt.widgets.Composite;
|
|
28 import org.eclipse.swt.widgets.Listener;
|
|
29 import org.eclipse.swt.widgets.Button;
|
|
30 import org.eclipse.swt.widgets.Event;
|
|
31 import org.eclipse.swt.widgets.Text;
|
|
32 import org.eclipse.swt.widgets.TypedListener;
|
|
33
|
48
|
34 version(Tango){
|
25
|
35 import tango.util.Convert;
|
|
36 import java.lang.all;
|
|
37
|
|
38 static import tango.text.Util;
|
|
39 //static import tango.text.locale.Core;
|
|
40 static import tango.time.Time;
|
|
41 static import tango.time.WallClock;
|
|
42 static import tango.time.chrono.Gregorian;
|
|
43 static import tango.time.chrono.Calendar;
|
48
|
44 } else { // Phobos
|
|
45 }
|
25
|
46
|
|
47
|
|
48 private class Calendar{
|
|
49 enum {
|
|
50 AM,
|
|
51 PM
|
|
52 }
|
|
53 enum {
|
|
54 AM_PM,
|
|
55 HOUR,
|
|
56 MINUTE,
|
|
57 SECOND,
|
|
58 MONTH,
|
|
59 YEAR,
|
|
60 DAY_OF_MONTH,
|
|
61 DAY_SELECTED,
|
|
62 MONTH_CHANGED,
|
|
63 HOUR_OF_DAY,
|
|
64 }
|
|
65 private static const int[] MONTH_DAYS = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
|
|
66 static private Calendar instance;
|
|
67
|
|
68 private int second;
|
|
69 private int minute;
|
|
70 private int hour;
|
|
71 private int dayofmonth;
|
|
72 private int month;
|
|
73 private int year;
|
|
74
|
|
75 static Calendar getInstance(){
|
|
76 if( instance is null ){
|
|
77 synchronized {
|
|
78 if( instance is null ){
|
|
79 instance = new Calendar;
|
|
80 }
|
|
81 }
|
|
82 }
|
|
83 return instance;
|
|
84 }
|
|
85
|
|
86 public this(){
|
|
87 tango.time.Time.Time time = tango.time.WallClock.WallClock.now();
|
|
88 tango.time.Time.TimeSpan span = time.time.span;
|
|
89 this.second = span.seconds % 60;
|
|
90 this.minute = span.minutes % 60;
|
|
91 this.hour = span.hours;
|
|
92 auto greg = tango.time.chrono.Gregorian.Gregorian.generic;
|
|
93 this.dayofmonth = greg.getDayOfMonth( time );
|
|
94 this.month = greg.getMonth( time );
|
|
95 this.year = greg.getYear( time );
|
|
96 }
|
|
97 int getActualMaximum(int field){
|
|
98 switch( field ){
|
|
99 case YEAR:
|
|
100 return 2100;
|
|
101 case MONTH:
|
|
102 return MONTH_DAYS.length -1;
|
|
103 case DAY_OF_MONTH:
|
|
104 return MONTH_DAYS[month];
|
|
105 case HOUR:
|
|
106 return 11;
|
|
107 case HOUR_OF_DAY:
|
|
108 return 23;
|
|
109 case MINUTE:
|
|
110 return 59;
|
|
111 case SECOND:
|
|
112 return 59;
|
|
113 case AM_PM:
|
|
114 return PM;
|
|
115 default: assert( false, Format( "no matching switch case for field {}.", field ));
|
|
116 }
|
|
117 }
|
|
118
|
|
119 int getActualMinimum(int field){
|
|
120 switch( field ){
|
|
121 case YEAR:
|
|
122 return 1900;
|
|
123 case MONTH:
|
|
124 return 0;
|
|
125 case DAY_OF_MONTH:
|
|
126 return 1;
|
|
127 case HOUR:
|
|
128 case HOUR_OF_DAY:
|
|
129 return 0;
|
|
130 case MINUTE:
|
|
131 return 0;
|
|
132 case SECOND:
|
|
133 return 0;
|
|
134 case AM_PM:
|
|
135 return AM;
|
|
136 default: assert( false, Format( "no matching switch case for field {}.", field ));
|
|
137 }
|
|
138 }
|
|
139
|
|
140 int getMaximum(int field){
|
|
141 switch( field ){
|
|
142 case YEAR:
|
|
143 return 2100;
|
|
144 case MONTH:
|
|
145 return 11;
|
|
146 case DAY_OF_MONTH:
|
|
147 return 31;
|
|
148 case HOUR:
|
|
149 return 11;
|
|
150 case HOUR_OF_DAY:
|
|
151 return 23;
|
|
152 case MINUTE:
|
|
153 return 59;
|
|
154 case SECOND:
|
|
155 return 59;
|
|
156 case AM_PM:
|
|
157 return PM;
|
|
158 default: assert( false, Format( "no matching switch case for field {}.", field ));
|
|
159 }
|
|
160 }
|
|
161
|
|
162 int getMinimum(int field){
|
|
163 switch( field ){
|
|
164 case YEAR:
|
|
165 return 1900;
|
|
166 case MONTH:
|
|
167 return 0;
|
|
168 case DAY_OF_MONTH:
|
|
169 return 1;
|
|
170 case HOUR:
|
|
171 case HOUR_OF_DAY:
|
|
172 return 0;
|
|
173 case MINUTE:
|
|
174 return 0;
|
|
175 case SECOND:
|
|
176 return 0;
|
|
177 case AM_PM:
|
|
178 return AM;
|
|
179 default: assert( false, Format( "no matching switch case for field {}.", field ));
|
|
180 }
|
|
181 }
|
|
182 int get(int field){
|
|
183 switch( field ){
|
|
184 case YEAR:
|
|
185 return year;
|
|
186 case MONTH:
|
|
187 return month;
|
|
188 case DAY_OF_MONTH:
|
|
189 return dayofmonth;
|
|
190 case HOUR:
|
|
191 return hour;
|
|
192 case HOUR_OF_DAY:
|
|
193 return hour % 12;
|
|
194 case MINUTE:
|
|
195 return minute;
|
|
196 case SECOND:
|
|
197 return second;
|
|
198 case AM_PM:
|
|
199 return ( hour < 12 ) ? AM : PM;
|
|
200 default: assert( false, Format( "no matching switch case for field {}.", field ));
|
|
201 }
|
|
202 }
|
|
203 void set( int year, int month, int day ){
|
|
204 this.year = year;
|
|
205 this.month = month;
|
|
206 this.dayofmonth = day;
|
|
207 }
|
|
208 void set(int field, int value){
|
|
209 switch( field ){
|
|
210 case YEAR:
|
|
211 year = value;
|
|
212 break;
|
|
213 case MONTH:
|
|
214 assert( value >= 0 && value < 12 );
|
|
215 month = value;
|
|
216 break;
|
|
217 case DAY_OF_MONTH:
|
|
218 assert( value > 0 && value <= getActualMaximum( DAY_OF_MONTH ) );
|
|
219 dayofmonth = value;
|
|
220 break;
|
|
221 case HOUR:
|
|
222 assert( value >= 0 && value < 12 );
|
|
223 hour = value;
|
|
224 break;
|
|
225 case HOUR_OF_DAY:
|
|
226 assert( value >= 0 && value < 24 );
|
|
227 hour = value;
|
|
228 break;
|
|
229 case MINUTE:
|
|
230 assert( value >= 0 && value < 60 );
|
|
231 minute = value;
|
|
232 break;
|
|
233 case SECOND:
|
|
234 assert( value >= 0 && value < 60 );
|
|
235 second = value;
|
|
236 break;
|
|
237 case AM_PM:
|
|
238 if( get(field) is AM ){
|
|
239 if( value is AM ){
|
|
240 return;
|
|
241 }
|
|
242 else{
|
|
243 hour += 12;
|
|
244 }
|
|
245 }
|
|
246 else{ // PM
|
|
247 if( value is AM ){
|
|
248 hour -= 12;
|
|
249 }
|
|
250 else{
|
|
251 return;
|
|
252 }
|
|
253 }
|
|
254 break;
|
|
255 default: assert( false, Format( "no matching switch case for field {}.", field ));
|
|
256 }
|
|
257 }
|
|
258
|
|
259 void roll(int field, int value){
|
|
260 switch( field ){
|
|
261 case YEAR:
|
|
262 year = value;
|
|
263 break;
|
|
264 case MONTH:
|
|
265 month += value;
|
|
266 month %= 12;
|
|
267 break;
|
|
268 case DAY_OF_MONTH:
|
|
269 dayofmonth += value;
|
|
270 dayofmonth %= getActualMaximum( DAY_OF_MONTH );
|
|
271 break;
|
|
272 case HOUR:
|
|
273 case HOUR_OF_DAY:
|
|
274 hour += value;
|
|
275 hour %= 24;
|
|
276 break;
|
|
277 case MINUTE:
|
|
278 minute += value;
|
|
279 minute %= 60;
|
|
280 break;
|
|
281 case SECOND:
|
|
282 second += value;
|
|
283 second %= 60;
|
|
284 break;
|
|
285 case AM_PM:
|
|
286 set( AM_PM, get( AM_PM ) is AM ? PM : AM );
|
|
287 break;
|
|
288 default: assert( false, Format( "no matching switch case for field {}.", field ));
|
|
289 }
|
|
290 }
|
|
291 }
|
|
292
|
|
293
|
|
294 private class DateFormatSymbols {
|
|
295 private const String[] ampm = [ "AM"[], "PM" ];
|
|
296 String[] getAmPmStrings(){
|
|
297 return ampm;
|
|
298 }
|
|
299 }
|
|
300
|
|
301
|
|
302 /**
|
|
303 * Instances of this class are selectable user interface
|
|
304 * objects that allow the user to enter and modify date
|
|
305 * or time values.
|
|
306 * <p>
|
|
307 * Note that although this class is a subclass of <code>Composite</code>,
|
|
308 * it does not make sense to add children to it, or set a layout on it.
|
|
309 * </p>
|
|
310 * <dl>
|
|
311 * <dt><b>Styles:</b></dt>
|
|
312 * <dd>DATE, TIME, CALENDAR, SHORT, MEDIUM, LONG</dd>
|
|
313 * <dt><b>Events:</b></dt>
|
|
314 * <dd>Selection</dd>
|
|
315 * </dl>
|
|
316 * <p>
|
|
317 * Note: Only one of the styles DATE, TIME, or CALENDAR may be specified,
|
|
318 * and only one of the styles SHORT, MEDIUM, or LONG may be specified.
|
|
319 * </p><p>
|
|
320 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
|
|
321 * </p>
|
|
322 *
|
|
323 * @see <a href="http://www.eclipse.org/swt/snippets/#datetime">DateTime snippets</a>
|
|
324 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Example: ControlExample</a>
|
|
325 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
326 *
|
|
327 * @since 3.3
|
|
328 */
|
|
329 public class DateTime : Composite {
|
|
330 int day, month, year, hours, minutes, seconds;
|
|
331
|
|
332 static const int MIN_YEAR = 1752; // Gregorian switchover in North America: September 19, 1752
|
|
333 static const int MAX_YEAR = 9999;
|
|
334
|
|
335 /* Emulated DATE and TIME variables */
|
|
336 Calendar calendar;
|
|
337 DateFormatSymbols formatSymbols;
|
|
338 Button down, up;
|
|
339 Text text;
|
|
340 String format;
|
|
341 Point[] fieldIndices;
|
|
342 int[] fieldNames;
|
|
343 int fieldCount, currentField = 0, characterCount = 0;
|
|
344 bool ignoreVerify = false;
|
|
345 static const String DEFAULT_SHORT_DATE_FORMAT = "MM/YYYY";
|
|
346 static const String DEFAULT_MEDIUM_DATE_FORMAT = "MM/DD/YYYY";
|
|
347 static const String DEFAULT_LONG_DATE_FORMAT = "MM/DD/YYYY";
|
|
348 static const String DEFAULT_SHORT_TIME_FORMAT = "HH:MM AM";
|
|
349 static const String DEFAULT_MEDIUM_TIME_FORMAT = "HH:MM:SS AM";
|
|
350 static const String DEFAULT_LONG_TIME_FORMAT = "HH:MM:SS AM";
|
|
351
|
|
352
|
|
353
|
|
354 /**
|
|
355 * Constructs a new instance of this class given its parent
|
|
356 * and a style value describing its behavior and appearance.
|
|
357 * <p>
|
|
358 * The style value is either one of the style constants defined in
|
|
359 * class <code>SWT</code> which is applicable to instances of this
|
|
360 * class, or must be built by <em>bitwise OR</em>'ing together
|
|
361 * (that is, using the <code>int</code> "|" operator) two or more
|
|
362 * of those <code>SWT</code> style constants. The class description
|
|
363 * lists the style constants that are applicable to the class.
|
|
364 * Style bits are also inherited from superclasses.
|
|
365 * </p>
|
|
366 *
|
|
367 * @param parent a composite control which will be the parent of the new instance (cannot be null)
|
|
368 * @param style the style of control to construct
|
|
369 *
|
|
370 * @exception IllegalArgumentException <ul>
|
|
371 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
|
|
372 * </ul>
|
|
373 * @exception SWTException <ul>
|
|
374 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
|
|
375 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
|
|
376 * </ul>
|
|
377 *
|
|
378 * @see SWT#DATE
|
|
379 * @see SWT#TIME
|
|
380 * @see SWT#CALENDAR
|
|
381 * @see Widget#checkSubclass
|
|
382 * @see Widget#getStyle
|
|
383 */
|
|
384 public this (Composite parent, int style) {
|
|
385 super (parent, checkStyle (style));
|
|
386 if ((this.style & SWT.CALENDAR) is 0) {
|
|
387 /* SWT.DATE and SWT.TIME */
|
|
388 calendar = Calendar.getInstance();
|
|
389 formatSymbols = new DateFormatSymbols();
|
|
390
|
|
391 text = new Text(this, SWT.SINGLE);
|
|
392 /* disable the native drag and drop for the date/time text field */
|
|
393 OS.gtk_drag_dest_unset(text.handle);
|
|
394 if ((this.style & SWT.DATE) !is 0) {
|
|
395 setFormat((this.style & SWT.SHORT) !is 0 ? DEFAULT_SHORT_DATE_FORMAT : (this.style & SWT.LONG) !is 0 ? DEFAULT_LONG_DATE_FORMAT : DEFAULT_MEDIUM_DATE_FORMAT);
|
|
396 } else { // SWT.TIME
|
|
397 setFormat((this.style & SWT.SHORT) !is 0 ? DEFAULT_SHORT_TIME_FORMAT : (this.style & SWT.LONG) !is 0 ? DEFAULT_LONG_TIME_FORMAT : DEFAULT_MEDIUM_TIME_FORMAT);
|
|
398 }
|
|
399 text.setText(getFormattedString(this.style));
|
|
400 Listener listener = new class () Listener {
|
|
401 public void handleEvent(Event event) {
|
|
402 switch(event.type) {
|
|
403 case SWT.KeyDown: onKeyDown(event); break;
|
|
404 case SWT.FocusIn: onFocusIn(event); break;
|
|
405 case SWT.FocusOut: onFocusOut(event); break;
|
|
406 case SWT.MouseDown: onMouseClick(event); break;
|
|
407 case SWT.MouseUp: onMouseClick(event); break;
|
|
408 case SWT.Verify: onVerify(event); break;
|
|
409 default:
|
|
410 }
|
|
411 }
|
|
412 };
|
|
413 text.addListener(SWT.KeyDown, listener);
|
|
414 text.addListener(SWT.FocusIn, listener);
|
|
415 text.addListener(SWT.FocusOut, listener);
|
|
416 text.addListener(SWT.MouseDown, listener);
|
|
417 text.addListener(SWT.MouseUp, listener);
|
|
418 text.addListener(SWT.Verify, listener);
|
|
419 up = new Button(this, SWT.ARROW | SWT.UP);
|
|
420 //up.setToolTipText(SWT.getMessage ("SWT_Up")); //$NON-NLS-1$
|
|
421 down = new Button(this, SWT.ARROW | SWT.DOWN);
|
|
422 //down.setToolTipText(SWT.getMessage ("SWT_Down")); //$NON-NLS-1$
|
|
423 up.addListener(SWT.Selection, new class() Listener {
|
|
424 public void handleEvent(Event event) {
|
|
425 incrementField(+1);
|
|
426 text.setFocus();
|
|
427 }
|
|
428 });
|
|
429 down.addListener(SWT.Selection, new class() Listener {
|
|
430 public void handleEvent(Event event) {
|
|
431 incrementField(-1);
|
|
432 text.setFocus();
|
|
433 }
|
|
434 });
|
|
435 addListener(SWT.Resize, new class() Listener {
|
|
436 public void handleEvent(Event event) {
|
|
437 onResize(event);
|
|
438 }
|
|
439 });
|
|
440 }
|
|
441 }
|
|
442
|
|
443 static int checkStyle (int style) {
|
|
444 /*
|
|
445 * Even though it is legal to create this widget
|
|
446 * with scroll bars, they serve no useful purpose
|
|
447 * because they do not automatically scroll the
|
|
448 * widget's client area. The fix is to clear
|
|
449 * the SWT style.
|
|
450 */
|
|
451 style &= ~(SWT.H_SCROLL | SWT.V_SCROLL);
|
|
452 style = checkBits (style, SWT.DATE, SWT.TIME, SWT.CALENDAR, 0, 0, 0);
|
|
453 return checkBits (style, SWT.MEDIUM, SWT.SHORT, SWT.LONG, 0, 0, 0);
|
|
454 }
|
|
455
|
|
456 /**
|
|
457 * Adds the listener to the collection of listeners who will
|
|
458 * be notified when the control is selected by the user, by sending
|
|
459 * it one of the messages defined in the <code>SelectionListener</code>
|
|
460 * interface.
|
|
461 * <p>
|
|
462 * <code>widgetSelected</code> is called when the user changes the control's value.
|
|
463 * <code>widgetDefaultSelected</code> is not called.
|
|
464 * </p>
|
|
465 *
|
|
466 * @param listener the listener which should be notified
|
|
467 *
|
|
468 * @exception IllegalArgumentException <ul>
|
|
469 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
470 * </ul>
|
|
471 * @exception SWTException <ul>
|
|
472 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
473 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
474 * </ul>
|
|
475 *
|
|
476 * @see SelectionListener
|
|
477 * @see #removeSelectionListener
|
|
478 * @see SelectionEvent
|
|
479 */
|
|
480 public void addSelectionListener (SelectionListener listener) {
|
|
481 checkWidget ();
|
|
482 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
483 TypedListener typedListener = new TypedListener (listener);
|
|
484 addListener (SWT.Selection, typedListener);
|
|
485 addListener (SWT.DefaultSelection, typedListener);
|
|
486 }
|
|
487
|
|
488 protected void checkSubclass () {
|
|
489 if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
|
|
490 }
|
|
491
|
|
492 public Point computeSize (int wHint, int hHint, bool changed) {
|
|
493 checkWidget ();
|
|
494 int width = 0, height = 0;
|
|
495 if (wHint is SWT.DEFAULT || hHint is SWT.DEFAULT) {
|
|
496 if ((style & SWT.CALENDAR) !is 0) {
|
|
497 // TODO: CALENDAR computeSize
|
|
498 width = 300;
|
|
499 height = 200;
|
|
500 } else {
|
|
501 /* SWT.DATE and SWT.TIME */
|
|
502 GC gc = new GC(text);
|
|
503 Point textSize = gc.stringExtent(getComputeSizeString(style));
|
|
504 gc.dispose();
|
|
505 Rectangle trim = text.computeTrim(0, 0, textSize.x, textSize.y);
|
|
506 Point buttonSize = up.computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);
|
|
507 width = trim.width + buttonSize.x;
|
|
508 height = Math.max(trim.height, buttonSize.y);
|
|
509 }
|
|
510 }
|
|
511 if (width is 0) width = DEFAULT_WIDTH;
|
|
512 if (height is 0) height = DEFAULT_HEIGHT;
|
|
513 if (wHint !is SWT.DEFAULT) width = wHint;
|
|
514 if (hHint !is SWT.DEFAULT) height = hHint;
|
|
515 int border = getBorderWidth ();
|
|
516 width += border * 2; height += border * 2;
|
|
517 return new Point (width, height);
|
|
518 }
|
|
519
|
|
520 void createHandle (int index) {
|
|
521 if ((style & SWT.CALENDAR) !is 0) {
|
|
522 state |= HANDLE;
|
|
523 fixedHandle = cast(GtkWidget*)OS.g_object_new (display.gtk_fixed_get_type (), null);
|
|
524 if (fixedHandle is null) error (SWT.ERROR_NO_HANDLES);
|
|
525 OS.gtk_fixed_set_has_window (fixedHandle, true);
|
|
526 handle = cast(GtkWidget*)OS.gtk_calendar_new ();
|
|
527 if (handle is null) error (SWT.ERROR_NO_HANDLES);
|
|
528 OS.gtk_container_add (fixedHandle, handle);
|
|
529 if (OS.GTK_VERSION >= OS.buildVERSION(2, 4, 0)) {
|
|
530 OS.gtk_calendar_set_display_options(handle, OS.GTK_CALENDAR_SHOW_HEADING | OS.GTK_CALENDAR_SHOW_DAY_NAMES);
|
|
531 } else {
|
|
532 OS.gtk_calendar_display_options(handle, OS.GTK_CALENDAR_SHOW_HEADING | OS.GTK_CALENDAR_SHOW_DAY_NAMES);
|
|
533 }
|
|
534 } else {
|
|
535 super.createHandle(index);
|
|
536 }
|
|
537 }
|
|
538
|
|
539 void createWidget (int index) {
|
|
540 super.createWidget (index);
|
|
541 if ((style & SWT.CALENDAR) !is 0) {
|
|
542 getDate();
|
|
543 }
|
|
544 }
|
|
545
|
|
546 void commitCurrentField() {
|
|
547 if (characterCount > 0) {
|
|
548 characterCount = 0;
|
|
549 int fieldName = fieldNames[currentField];
|
|
550 int start = fieldIndices[currentField].x;
|
|
551 int end = fieldIndices[currentField].y;
|
|
552 String value = text.getText(start, end - 1);
|
|
553 int s = value.lastIndexOf(' ');
|
|
554 if (s !is -1) value = value.substring(s + 1);
|
|
555 int newValue = unformattedIntValue(fieldName, value, characterCount is 0, calendar.getActualMaximum(fieldName));
|
|
556 if (newValue !is -1) setTextField(fieldName, newValue, true, true);
|
|
557 }
|
|
558 }
|
|
559
|
|
560 String formattedStringValue(int fieldName, int value, bool adjust) {
|
|
561 if (fieldName is Calendar.AM_PM) {
|
|
562 String[] ampm = formatSymbols.getAmPmStrings();
|
|
563 return ampm[value];
|
|
564 }
|
|
565 if (adjust) {
|
|
566 if (fieldName is Calendar.HOUR && value is 0) {
|
|
567 return to!(String)(12);
|
|
568 }
|
|
569 if (fieldName is Calendar.MONTH) {
|
|
570 return to!(String)(value + 1);
|
|
571 }
|
|
572 }
|
|
573 return to!(String)(value);
|
|
574 }
|
|
575
|
|
576 String getComputeSizeString(int style) {
|
|
577 if ((style & SWT.DATE) !is 0) {
|
|
578 return (style & SWT.SHORT) !is 0 ? DEFAULT_SHORT_DATE_FORMAT : (style & SWT.LONG) !is 0 ? DEFAULT_LONG_DATE_FORMAT : DEFAULT_MEDIUM_DATE_FORMAT;
|
|
579 }
|
|
580 // SWT.TIME
|
|
581 return (style & SWT.SHORT) !is 0 ? DEFAULT_SHORT_TIME_FORMAT : (style & SWT.LONG) !is 0 ? DEFAULT_LONG_TIME_FORMAT : DEFAULT_MEDIUM_TIME_FORMAT;
|
|
582 }
|
|
583
|
|
584 int getFieldIndex(int fieldName) {
|
|
585 for (int i = 0; i < fieldCount; i++) {
|
|
586 if (fieldNames[i] is fieldName) {
|
|
587 return i;
|
|
588 }
|
|
589 }
|
|
590 return -1;
|
|
591 }
|
|
592
|
|
593 String getFormattedString(int style) {
|
|
594 if ((style & SWT.TIME) !is 0) {
|
|
595 String[] ampm = formatSymbols.getAmPmStrings();
|
|
596 int h = calendar.get(Calendar.HOUR); if (h is 0) h = 12;
|
|
597 int m = calendar.get(Calendar.MINUTE);
|
|
598 int s = calendar.get(Calendar.SECOND);
|
|
599 int a = calendar.get(Calendar.AM_PM);
|
|
600 if ((style & SWT.SHORT) !is 0) return "" ~ (h < 10 ? " " : "") ~ to!(String)(h) ~ ":" ~ (m < 10 ? "0" : "") ~ to!(String)(m) ~ " " ~ ampm[a];
|
|
601 return "" ~ (h < 10 ? " " : "") ~ to!(String)(h) ~ ":" ~ (m < 10 ? "0" : "") ~ to!(String)(m) ~ ":" ~ (s < 10 ? "0" : "") ~ to!(String)(s) ~ " " ~ ampm[a];
|
|
602 }
|
|
603 /* SWT.DATE */
|
|
604 int y = calendar.get(Calendar.YEAR);
|
|
605 int m = calendar.get(Calendar.MONTH) + 1;
|
|
606 int d = calendar.get(Calendar.DAY_OF_MONTH);
|
|
607 if ((style & SWT.SHORT) !is 0) return "" ~ (m < 10 ? " " : "") ~ to!(String)(m) ~ "/" ~ to!(String)(y);
|
|
608 return "" ~ (m < 10 ? " " : "") ~ to!(String)(m) ~ "/" ~ (d < 10 ? " " : "") ~ to!(String)(d) ~ "/" ~ to!(String)(y);
|
|
609 }
|
|
610
|
|
611 void getDate() {
|
|
612 uint y;
|
|
613 uint m;
|
|
614 uint d;
|
|
615 OS.gtk_calendar_get_date(handle, &y, &m, &d);
|
|
616 year = y;
|
|
617 month = m;
|
|
618 day = d;
|
|
619 }
|
|
620
|
|
621 /**
|
|
622 * Returns the receiver's date, or day of the month.
|
|
623 * <p>
|
|
624 * The first day of the month is 1, and the last day depends on the month and year.
|
|
625 * </p>
|
|
626 *
|
|
627 * @return a positive integer beginning with 1
|
|
628 *
|
|
629 * @exception SWTException <ul>
|
|
630 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
631 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
632 * </ul>
|
|
633 */
|
|
634 public int getDay () {
|
|
635 checkWidget ();
|
|
636 if ((style & SWT.CALENDAR) !is 0) {
|
|
637 getDate();
|
|
638 return day;
|
|
639 } else {
|
|
640 return calendar.get(Calendar.DAY_OF_MONTH);
|
|
641 }
|
|
642 }
|
|
643
|
|
644 /**
|
|
645 * Returns the receiver's hours.
|
|
646 * <p>
|
|
647 * Hours is an integer between 0 and 23.
|
|
648 * </p>
|
|
649 *
|
|
650 * @return an integer between 0 and 23
|
|
651 *
|
|
652 * @exception SWTException <ul>
|
|
653 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
654 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
655 * </ul>
|
|
656 */
|
|
657 public int getHours () {
|
|
658 checkWidget ();
|
|
659 if ((style & SWT.CALENDAR) !is 0) {
|
|
660 return hours;
|
|
661 } else {
|
|
662 return calendar.get(Calendar.HOUR_OF_DAY);
|
|
663 }
|
|
664 }
|
|
665
|
|
666 /**
|
|
667 * Returns the receiver's minutes.
|
|
668 * <p>
|
|
669 * Minutes is an integer between 0 and 59.
|
|
670 * </p>
|
|
671 *
|
|
672 * @return an integer between 0 and 59
|
|
673 *
|
|
674 * @exception SWTException <ul>
|
|
675 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
676 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
677 * </ul>
|
|
678 */
|
|
679 public int getMinutes () {
|
|
680 checkWidget ();
|
|
681 if ((style & SWT.CALENDAR) !is 0) {
|
|
682 return minutes;
|
|
683 } else {
|
|
684 return calendar.get(Calendar.MINUTE);
|
|
685 }
|
|
686 }
|
|
687
|
|
688 /**
|
|
689 * Returns the receiver's month.
|
|
690 * <p>
|
|
691 * The first month of the year is 0, and the last month is 11.
|
|
692 * </p>
|
|
693 *
|
|
694 * @return an integer between 0 and 11
|
|
695 *
|
|
696 * @exception SWTException <ul>
|
|
697 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
698 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
699 * </ul>
|
|
700 */
|
|
701 public int getMonth () {
|
|
702 checkWidget ();
|
|
703 if ((style & SWT.CALENDAR) !is 0) {
|
|
704 getDate();
|
|
705 return month;
|
|
706 } else {
|
|
707 return calendar.get(Calendar.MONTH);
|
|
708 }
|
|
709 }
|
|
710
|
|
711 String getNameText() {
|
|
712 if((style & SWT.TIME) !is 0){
|
|
713 return Format( "{}:{}:{}", getHours(), getMinutes(), getSeconds() );
|
|
714 }
|
|
715 else{
|
|
716 return Format( "{}/{}/{}", (getMonth() + 1), getDay(), getYear() );
|
|
717 }
|
|
718 }
|
|
719
|
|
720 /**
|
|
721 * Returns the receiver's seconds.
|
|
722 * <p>
|
|
723 * Seconds is an integer between 0 and 59.
|
|
724 * </p>
|
|
725 *
|
|
726 * @return an integer between 0 and 59
|
|
727 *
|
|
728 * @exception SWTException <ul>
|
|
729 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
730 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
731 * </ul>
|
|
732 */
|
|
733 public int getSeconds () {
|
|
734 checkWidget ();
|
|
735 if ((style & SWT.CALENDAR) !is 0) {
|
|
736 return seconds;
|
|
737 } else {
|
|
738 return calendar.get(Calendar.SECOND);
|
|
739 }
|
|
740 }
|
|
741
|
|
742 /**
|
|
743 * Returns the receiver's year.
|
|
744 * <p>
|
|
745 * The first year is 1752 and the last year is 9999.
|
|
746 * </p>
|
|
747 *
|
|
748 * @return an integer between 1752 and 9999
|
|
749 *
|
|
750 * @exception SWTException <ul>
|
|
751 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
752 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
753 * </ul>
|
|
754 */
|
|
755 public int getYear () {
|
|
756 checkWidget ();
|
|
757 if ((style & SWT.CALENDAR) !is 0) {
|
|
758 getDate();
|
|
759 return year;
|
|
760 } else {
|
|
761 return calendar.get(Calendar.YEAR);
|
|
762 }
|
|
763 }
|
|
764
|
|
765 override int gtk_day_selected (GtkWidget* widget) {
|
|
766 sendSelectionEvent ();
|
|
767 return 0;
|
|
768 }
|
|
769
|
|
770 override int gtk_month_changed (GtkWidget* widget) {
|
|
771 sendSelectionEvent ();
|
|
772 return 0;
|
|
773 }
|
|
774
|
|
775 void hookEvents () {
|
|
776 super.hookEvents();
|
|
777 if ((style & SWT.CALENDAR) !is 0) {
|
|
778 OS.g_signal_connect_closure (handle, OS.day_selected.ptr, display.closures [DAY_SELECTED], false);
|
|
779 OS.g_signal_connect_closure (handle, OS.month_changed.ptr, display.closures [MONTH_CHANGED], false);
|
|
780 }
|
|
781 }
|
|
782
|
|
783 bool isValid(int fieldName, int value) {
|
|
784 Calendar validCalendar;
|
|
785 if ((style & SWT.CALENDAR) !is 0) {
|
|
786 validCalendar = Calendar.getInstance();
|
|
787 validCalendar.set(Calendar.YEAR, year);
|
|
788 validCalendar.set(Calendar.MONTH, month);
|
|
789 } else {
|
|
790 validCalendar = calendar;
|
|
791 }
|
|
792 int min = validCalendar.getActualMinimum(fieldName);
|
|
793 int max = validCalendar.getActualMaximum(fieldName);
|
|
794 return value >= min && value <= max;
|
|
795 }
|
|
796
|
|
797 bool isValid(int year, int month, int day) {
|
|
798 Calendar valid = Calendar.getInstance();
|
|
799 valid.set(year, month, day);
|
|
800 return valid.get(Calendar.YEAR) is year && valid.get(Calendar.MONTH) is month && valid.get(Calendar.DAY_OF_MONTH) is day;
|
|
801 }
|
|
802
|
|
803 void incrementField(int amount) {
|
|
804 int fieldName = fieldNames[currentField];
|
|
805 int value = calendar.get(fieldName);
|
|
806 if (fieldName is Calendar.HOUR) {
|
|
807 int max = calendar.getMaximum(Calendar.HOUR);
|
|
808 int min = calendar.getMinimum(Calendar.HOUR);
|
|
809 if ((value is max && amount is 1) || (value is min && amount is -1)) {
|
|
810 int temp = currentField;
|
|
811 currentField = getFieldIndex(Calendar.AM_PM);
|
|
812 setTextField(Calendar.AM_PM, (calendar.get(Calendar.AM_PM) + 1) % 2, true, true);
|
|
813 currentField = temp;
|
|
814 }
|
|
815 }
|
|
816 setTextField(fieldName, value + amount, true, true);
|
|
817 }
|
|
818
|
|
819 void onKeyDown(Event event) {
|
|
820 int fieldName;
|
|
821 switch (event.keyCode) {
|
|
822 case SWT.ARROW_RIGHT:
|
|
823 case SWT.KEYPAD_DIVIDE:
|
|
824 // a right arrow or a valid separator navigates to the field on the right, with wraping
|
|
825 selectField((currentField + 1) % fieldCount);
|
|
826 break;
|
|
827 case SWT.ARROW_LEFT:
|
|
828 // navigate to the field on the left, with wrapping
|
|
829 int index = currentField - 1;
|
|
830 selectField(index < 0 ? fieldCount - 1 : index);
|
|
831 break;
|
|
832 case SWT.ARROW_UP:
|
|
833 case SWT.KEYPAD_ADD:
|
|
834 // set the value of the current field to value + 1, with wrapping
|
|
835 commitCurrentField();
|
|
836 incrementField(+1);
|
|
837 break;
|
|
838 case SWT.ARROW_DOWN:
|
|
839 case SWT.KEYPAD_SUBTRACT:
|
|
840 // set the value of the current field to value - 1, with wrapping
|
|
841 commitCurrentField();
|
|
842 incrementField(-1);
|
|
843 break;
|
|
844 case SWT.HOME:
|
|
845 // set the value of the current field to its minimum
|
|
846 fieldName = fieldNames[currentField];
|
|
847 setTextField(fieldName, calendar.getActualMinimum(fieldName), true, true);
|
|
848 break;
|
|
849 case SWT.END:
|
|
850 // set the value of the current field to its maximum
|
|
851 fieldName = fieldNames[currentField];
|
|
852 setTextField(fieldName, calendar.getActualMaximum(fieldName), true, true);
|
|
853 break;
|
|
854 default:
|
|
855 switch (event.character) {
|
|
856 case '/':
|
|
857 case ':':
|
|
858 case '-':
|
|
859 case '.':
|
|
860 // a valid separator navigates to the field on the right, with wraping
|
|
861 selectField((currentField + 1) % fieldCount);
|
|
862 break;
|
|
863 default:
|
|
864 }
|
|
865 }
|
|
866 }
|
|
867
|
|
868 void onFocusIn(Event event) {
|
|
869 selectField(currentField);
|
|
870 }
|
|
871
|
|
872 void onFocusOut(Event event) {
|
|
873 commitCurrentField();
|
|
874 }
|
|
875
|
|
876 void onMouseClick(Event event) {
|
|
877 if (event.button !is 1) return;
|
|
878 Point sel = text.getSelection();
|
|
879 for (int i = 0; i < fieldCount; i++) {
|
|
880 if (sel.x >= fieldIndices[i].x && sel.x <= fieldIndices[i].y) {
|
|
881 currentField = i;
|
|
882 break;
|
|
883 }
|
|
884 }
|
|
885 selectField(currentField);
|
|
886 }
|
|
887
|
|
888 void onResize(Event event) {
|
|
889 Rectangle rect = getClientArea ();
|
|
890 int width = rect.width;
|
|
891 int height = rect.height;
|
|
892 Point buttonSize = up.computeSize(SWT.DEFAULT, height);
|
|
893 int buttonHeight = buttonSize.y / 2;
|
|
894 text.setBounds(0, 0, width - buttonSize.x, height);
|
|
895 up.setBounds(width - buttonSize.x, 0, buttonSize.x, buttonHeight);
|
|
896 down.setBounds(width - buttonSize.x, buttonHeight, buttonSize.x, buttonHeight);
|
|
897 }
|
|
898
|
|
899 void onVerify(Event event) {
|
|
900 if (ignoreVerify) return;
|
|
901 event.doit = false;
|
|
902 int fieldName = fieldNames[currentField];
|
|
903 int start = fieldIndices[currentField].x;
|
|
904 int end = fieldIndices[currentField].y;
|
|
905 int length_ = end - start;
|
|
906 String newText = event.text;
|
|
907 if (fieldName is Calendar.AM_PM) {
|
|
908 String[] ampm = formatSymbols.getAmPmStrings();
|
|
909 if (newText.equalsIgnoreCase(ampm[Calendar.AM].substring(0, 1)) || newText.equalsIgnoreCase(ampm[Calendar.AM])) {
|
|
910 setTextField(fieldName, Calendar.AM, true, false);
|
|
911 } else if (newText.equalsIgnoreCase(ampm[Calendar.PM].substring(0, 1)) || newText.equalsIgnoreCase(ampm[Calendar.PM])) {
|
|
912 setTextField(fieldName, Calendar.PM, true, false);
|
|
913 }
|
|
914 return;
|
|
915 }
|
|
916 if (characterCount > 0) {
|
|
917 try {
|
|
918 to!(int)(newText);
|
|
919 } catch (ConversionException ex) {
|
|
920 return;
|
|
921 }
|
|
922 String value = text.getText(start, end - 1);
|
|
923 int s = value.lastIndexOf(' ');
|
|
924 if (s !is -1) value = value.substring(s + 1);
|
|
925 newText = value ~ newText;
|
|
926 }
|
|
927 int newTextLength = newText.length;
|
|
928 bool first = characterCount is 0;
|
|
929 characterCount = (newTextLength < length_) ? newTextLength : 0;
|
|
930 int max = calendar.getActualMaximum(fieldName);
|
|
931 int min = calendar.getActualMinimum(fieldName);
|
|
932 int newValue = unformattedIntValue(fieldName, newText, characterCount is 0, max);
|
|
933 if (newValue is -1) {
|
|
934 characterCount = 0;
|
|
935 return;
|
|
936 }
|
|
937 if (first && newValue is 0 && length_ > 1) {
|
|
938 setTextField(fieldName, newValue, false, false);
|
|
939 } else if (min <= newValue && newValue <= max) {
|
|
940 setTextField(fieldName, newValue, characterCount is 0, characterCount is 0);
|
|
941 } else {
|
|
942 if (newTextLength >= length_) {
|
|
943 newText = newText.substring(newTextLength - length_ + 1);
|
|
944 newValue = unformattedIntValue(fieldName, newText, characterCount is 0, max);
|
|
945 if (newValue !is -1) {
|
|
946 characterCount = length_ - 1;
|
|
947 if (min <= newValue && newValue <= max) {
|
|
948 setTextField(fieldName, newValue, characterCount is 0, true);
|
|
949 }
|
|
950 }
|
|
951 }
|
|
952 }
|
|
953 }
|
|
954
|
|
955 void releaseWidget () {
|
|
956 super.releaseWidget();
|
|
957 //TODO: need to do anything here?
|
|
958 }
|
|
959
|
|
960 /**
|
|
961 * Removes the listener from the collection of listeners who will
|
|
962 * be notified when the control is selected by the user.
|
|
963 *
|
|
964 * @param listener the listener which should no longer be notified
|
|
965 *
|
|
966 * @exception IllegalArgumentException <ul>
|
|
967 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
|
|
968 * </ul>
|
|
969 * @exception SWTException <ul>
|
|
970 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
971 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
972 * </ul>
|
|
973 *
|
|
974 * @see SelectionListener
|
|
975 * @see #addSelectionListener
|
|
976 */
|
|
977 public void removeSelectionListener (SelectionListener listener) {
|
|
978 checkWidget ();
|
|
979 if (listener is null) error (SWT.ERROR_NULL_ARGUMENT);
|
|
980 if (eventTable is null) return;
|
|
981 eventTable.unhook (SWT.Selection, listener);
|
|
982 eventTable.unhook (SWT.DefaultSelection, listener);
|
|
983 }
|
|
984
|
|
985 void selectField(int index) {
|
|
986 if (index !is currentField) {
|
|
987 commitCurrentField();
|
|
988 }
|
|
989 final int start = fieldIndices[index].x;
|
|
990 final int end = fieldIndices[index].y;
|
|
991 Point pt = text.getSelection();
|
|
992 if (index is currentField && start is pt.x && end is pt.y) return;
|
|
993 currentField = index;
|
|
994 display.asyncExec(new class( start, end ) Runnable {
|
|
995 int start, end;
|
|
996 this( int start, int end ){
|
|
997 this.start = start; this.end = end;
|
|
998 }
|
|
999 public void run() {
|
|
1000 if (!text.isDisposed()) {
|
|
1001 String value = text.getText(start, end - 1);
|
|
1002 int s = value.lastIndexOf(' ');
|
|
1003 if (s is -1 ) s = start;
|
|
1004 else s = start + s + 1;
|
|
1005 text.setSelection(s, end);
|
|
1006 }
|
|
1007 }
|
|
1008 });
|
|
1009 }
|
|
1010
|
|
1011 void sendSelectionEvent () {
|
|
1012 uint y;
|
|
1013 uint m;
|
|
1014 uint d;
|
|
1015 OS.gtk_calendar_get_date(handle, &y, &m, &d);
|
|
1016 //TODO: hours, minutes, seconds?
|
|
1017 if (d !is day ||
|
|
1018 m !is month ||
|
|
1019 y !is year) {
|
|
1020 year = y;
|
|
1021 month = m;
|
|
1022 day = d;
|
|
1023 postEvent (SWT.Selection);
|
|
1024 }
|
|
1025 }
|
|
1026
|
|
1027 public void setBackground(Color color) {
|
|
1028 checkWidget();
|
|
1029 super.setBackground(color);
|
|
1030 if (text !is null) text.setBackground(color);
|
|
1031 }
|
|
1032
|
|
1033 public void setFont(Font font) {
|
|
1034 checkWidget();
|
|
1035 super.setFont(font);
|
|
1036 if (text !is null) text.setFont(font);
|
|
1037 redraw();
|
|
1038 }
|
|
1039
|
|
1040 public void setForeground(Color color) {
|
|
1041 checkWidget();
|
|
1042 super.setForeground(color);
|
|
1043 if (text !is null) text.setForeground(color);
|
|
1044 }
|
|
1045
|
|
1046 /*public*/ void setFormat(String string) {
|
|
1047 checkWidget();
|
|
1048 // TODO: this needs to be locale sensitive
|
|
1049 fieldCount = (style & SWT.DATE) !is 0 ? ((style & SWT.SHORT) !is 0 ? 2 : 3) : ((style & SWT.SHORT) !is 0 ? 3 : 4);
|
|
1050 fieldIndices = new Point[fieldCount];
|
|
1051 fieldNames = new int[fieldCount];
|
|
1052 if ((style & SWT.DATE) !is 0) {
|
|
1053 fieldNames[0] = Calendar.MONTH;
|
|
1054 fieldIndices[0] = new Point(0, 2);
|
|
1055 if ((style & SWT.SHORT) !is 0) {
|
|
1056 fieldNames[1] = Calendar.YEAR;
|
|
1057 fieldIndices[1] = new Point(3, 7);
|
|
1058 } else {
|
|
1059 fieldNames[1] = Calendar.DAY_OF_MONTH;
|
|
1060 fieldIndices[1] = new Point(3, 5);
|
|
1061 fieldNames[2] = Calendar.YEAR;
|
|
1062 fieldIndices[2] = new Point(6, 10);
|
|
1063 }
|
|
1064 } else { /* SWT.TIME */
|
|
1065 fieldNames[0] = Calendar.HOUR;
|
|
1066 fieldIndices[0] = new Point(0, 2);
|
|
1067 fieldNames[1] = Calendar.MINUTE;
|
|
1068 fieldIndices[1] = new Point(3, 5);
|
|
1069 if ((style & SWT.SHORT) !is 0) {
|
|
1070 fieldNames[2] = Calendar.AM_PM;
|
|
1071 fieldIndices[2] = new Point(6, 8);
|
|
1072 } else {
|
|
1073 fieldNames[2] = Calendar.SECOND;
|
|
1074 fieldIndices[2] = new Point(6, 8);
|
|
1075 fieldNames[3] = Calendar.AM_PM;
|
|
1076 fieldIndices[3] = new Point(9, 11);
|
|
1077 }
|
|
1078 }
|
|
1079 }
|
|
1080
|
|
1081 void setField(int fieldName, int value) {
|
|
1082 if (calendar.get(fieldName) is value) return;
|
|
1083 if (fieldName is Calendar.AM_PM) {
|
|
1084 calendar.roll(Calendar.HOUR_OF_DAY, 12); // TODO: needs more work for setFormat and locale
|
|
1085 }
|
|
1086 calendar.set(fieldName, value);
|
|
1087 postEvent(SWT.Selection);
|
|
1088 }
|
|
1089
|
|
1090 void setTextField(int fieldName, int value, bool commit, bool adjust) {
|
|
1091 if (commit) {
|
|
1092 int max = calendar.getActualMaximum(fieldName);
|
|
1093 int min = calendar.getActualMinimum(fieldName);
|
|
1094 if (fieldName is Calendar.YEAR) {
|
|
1095 max = MAX_YEAR;
|
|
1096 min = MIN_YEAR;
|
|
1097 /* Special case: convert 1 or 2-digit years into reasonable 4-digit years. */
|
|
1098 int currentYear = Calendar.getInstance().get(Calendar.YEAR);
|
|
1099 int currentCentury = (currentYear / 100) * 100;
|
|
1100 if (value < (currentYear + 30) % 100) value += currentCentury;
|
|
1101 else if (value < 100) value += currentCentury - 100;
|
|
1102 }
|
|
1103 if (value > max) value = min; // wrap
|
|
1104 if (value < min) value = max; // wrap
|
|
1105 }
|
|
1106 int start = fieldIndices[currentField].x;
|
|
1107 int end = fieldIndices[currentField].y;
|
|
1108 text.setSelection(start, end);
|
|
1109 String newValue = formattedStringValue(fieldName, value, adjust);
|
|
1110 StringBuffer buffer = new StringBuffer(newValue);
|
|
1111 /* Convert leading 0's into spaces. */
|
|
1112 int prependCount = end - start - buffer.length();
|
|
1113 for (int i = 0; i < prependCount; i++) {
|
|
1114 switch (fieldName) {
|
|
1115 case Calendar.MINUTE:
|
|
1116 case Calendar.SECOND:
|
|
1117 buffer.insert(0, 0);
|
|
1118 break;
|
|
1119 default:
|
|
1120 buffer.insert(0, ' ');
|
|
1121 break;
|
|
1122 }
|
|
1123 }
|
|
1124 newValue = buffer.toString();
|
|
1125 ignoreVerify = true;
|
|
1126 text.insert(newValue);
|
|
1127 ignoreVerify = false;
|
|
1128 selectField(currentField);
|
|
1129 if (commit) setField(fieldName, value);
|
|
1130 }
|
|
1131
|
|
1132 /**
|
|
1133 * Sets the receiver's year, month, and day in a single operation.
|
|
1134 * <p>
|
|
1135 * This is the recommended way to set the date, because setting the year,
|
|
1136 * month, and day separately may result in invalid intermediate dates.
|
|
1137 * </p>
|
|
1138 *
|
|
1139 * @param year an integer between 1752 and 9999
|
|
1140 * @param month an integer between 0 and 11
|
|
1141 * @param day a positive integer beginning with 1
|
|
1142 *
|
|
1143 * @exception SWTException <ul>
|
|
1144 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1145 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1146 * </ul>
|
|
1147 *
|
|
1148 * @since 3.4
|
|
1149 */
|
|
1150 public void setDate (int year, int month, int day) {
|
|
1151 checkWidget ();
|
|
1152 if (!isValid(year, month, day)) return;
|
|
1153 if ((style & SWT.CALENDAR) !is 0) {
|
|
1154 this.year = year;
|
|
1155 this.month = month;
|
|
1156 this.day = day;
|
|
1157 OS.gtk_calendar_select_month(handle, month, year);
|
|
1158 OS.gtk_calendar_select_day(handle, day);
|
|
1159 } else {
|
|
1160 calendar.set(Calendar.YEAR, year);
|
|
1161 calendar.set(Calendar.MONTH, month);
|
|
1162 calendar.set(Calendar.DAY_OF_MONTH, day);
|
|
1163 updateControl();
|
|
1164 }
|
|
1165 }
|
|
1166
|
|
1167 /**
|
|
1168 * Sets the receiver's date, or day of the month, to the specified day.
|
|
1169 * <p>
|
|
1170 * The first day of the month is 1, and the last day depends on the month and year.
|
|
1171 * </p>
|
|
1172 *
|
|
1173 * @param day a positive integer beginning with 1
|
|
1174 *
|
|
1175 * @exception SWTException <ul>
|
|
1176 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1177 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1178 * </ul>
|
|
1179 */
|
|
1180 public void setDay (int day) {
|
|
1181 checkWidget ();
|
|
1182 if (!isValid(Calendar.DAY_OF_MONTH, day)) return;
|
|
1183 if ((style & SWT.CALENDAR) !is 0) {
|
|
1184 this.day = day;
|
|
1185 OS.gtk_calendar_select_day(handle, day);
|
|
1186 } else {
|
|
1187 calendar.set(Calendar.DAY_OF_MONTH, day);
|
|
1188 updateControl();
|
|
1189 }
|
|
1190 }
|
|
1191
|
|
1192 /**
|
|
1193 * Sets the receiver's hours.
|
|
1194 * <p>
|
|
1195 * Hours is an integer between 0 and 23.
|
|
1196 * </p>
|
|
1197 *
|
|
1198 * @param hours an integer between 0 and 23
|
|
1199 *
|
|
1200 * @exception SWTException <ul>
|
|
1201 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1202 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1203 * </ul>
|
|
1204 */
|
|
1205 public void setHours (int hours) {
|
|
1206 checkWidget ();
|
|
1207 if (!isValid(Calendar.HOUR_OF_DAY, hours)) return;
|
|
1208 if ((style & SWT.CALENDAR) !is 0) {
|
|
1209 this.hours = hours;
|
|
1210 } else {
|
|
1211 calendar.set(Calendar.HOUR_OF_DAY, hours);
|
|
1212 updateControl();
|
|
1213 }
|
|
1214 }
|
|
1215
|
|
1216 /**
|
|
1217 * Sets the receiver's minutes.
|
|
1218 * <p>
|
|
1219 * Minutes is an integer between 0 and 59.
|
|
1220 * </p>
|
|
1221 *
|
|
1222 * @param minutes an integer between 0 and 59
|
|
1223 *
|
|
1224 * @exception SWTException <ul>
|
|
1225 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1226 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1227 * </ul>
|
|
1228 */
|
|
1229 public void setMinutes (int minutes) {
|
|
1230 checkWidget ();
|
|
1231 if (!isValid(Calendar.MINUTE, minutes)) return;
|
|
1232 if ((style & SWT.CALENDAR) !is 0) {
|
|
1233 this.minutes = minutes;
|
|
1234 } else {
|
|
1235 calendar.set(Calendar.MINUTE, minutes);
|
|
1236 updateControl();
|
|
1237 }
|
|
1238 }
|
|
1239
|
|
1240 /**
|
|
1241 * Sets the receiver's month.
|
|
1242 * <p>
|
|
1243 * The first month of the year is 0, and the last month is 11.
|
|
1244 * </p>
|
|
1245 *
|
|
1246 * @param month an integer between 0 and 11
|
|
1247 *
|
|
1248 * @exception SWTException <ul>
|
|
1249 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1250 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1251 * </ul>
|
|
1252 */
|
|
1253 public void setMonth (int month) {
|
|
1254 checkWidget ();
|
|
1255 if (!isValid(Calendar.MONTH, month)) return;
|
|
1256 if ((style & SWT.CALENDAR) !is 0) {
|
|
1257 this.month = month;
|
|
1258 OS.gtk_calendar_select_month(handle, month, year);
|
|
1259 } else {
|
|
1260 calendar.set(Calendar.MONTH, month);
|
|
1261 updateControl();
|
|
1262 }
|
|
1263 }
|
|
1264
|
|
1265 /**
|
|
1266 * Sets the receiver's seconds.
|
|
1267 * <p>
|
|
1268 * Seconds is an integer between 0 and 59.
|
|
1269 * </p>
|
|
1270 *
|
|
1271 * @param seconds an integer between 0 and 59
|
|
1272 *
|
|
1273 * @exception SWTException <ul>
|
|
1274 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1275 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1276 * </ul>
|
|
1277 */
|
|
1278 public void setSeconds (int seconds) {
|
|
1279 checkWidget ();
|
|
1280 if (!isValid(Calendar.SECOND, seconds)) return;
|
|
1281 if ((style & SWT.CALENDAR) !is 0) {
|
|
1282 this.seconds = seconds;
|
|
1283 } else {
|
|
1284 calendar.set(Calendar.SECOND, seconds);
|
|
1285 updateControl();
|
|
1286 }
|
|
1287 }
|
|
1288
|
|
1289 /**
|
|
1290 * Sets the receiver's hours, minutes, and seconds in a single operation.
|
|
1291 *
|
|
1292 * @param hours an integer between 0 and 23
|
|
1293 * @param minutes an integer between 0 and 59
|
|
1294 * @param seconds an integer between 0 and 59
|
|
1295 *
|
|
1296 * @exception SWTException <ul>
|
|
1297 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1298 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1299 * </ul>
|
|
1300 *
|
|
1301 * @since 3.4
|
|
1302 */
|
|
1303 public void setTime (int hours, int minutes, int seconds) {
|
|
1304 checkWidget ();
|
|
1305 if (!isValid(Calendar.HOUR_OF_DAY, hours)) return;
|
|
1306 if (!isValid(Calendar.MINUTE, minutes)) return;
|
|
1307 if (!isValid(Calendar.SECOND, seconds)) return;
|
|
1308 if ((style & SWT.CALENDAR) !is 0) {
|
|
1309 this.hours = hours;
|
|
1310 this.minutes = minutes;
|
|
1311 this.seconds = seconds;
|
|
1312 } else {
|
|
1313 calendar.set(Calendar.HOUR_OF_DAY, hours);
|
|
1314 calendar.set(Calendar.MINUTE, minutes);
|
|
1315 calendar.set(Calendar.SECOND, seconds);
|
|
1316 updateControl();
|
|
1317 }
|
|
1318 }
|
|
1319
|
|
1320 /**
|
|
1321 * Sets the receiver's year.
|
|
1322 * <p>
|
|
1323 * The first year is 1752 and the last year is 9999.
|
|
1324 * </p>
|
|
1325 *
|
|
1326 * @param year an integer between 1752 and 9999
|
|
1327 *
|
|
1328 * @exception SWTException <ul>
|
|
1329 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
|
|
1330 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
|
|
1331 * </ul>
|
|
1332 */
|
|
1333 public void setYear (int year) {
|
|
1334 checkWidget ();
|
|
1335 //if (!isValid(Calendar.YEAR, year)) return;
|
|
1336 if (year < MIN_YEAR || year > MAX_YEAR) return;
|
|
1337 if ((style & SWT.CALENDAR) !is 0) {
|
|
1338 this.year = year;
|
|
1339 OS.gtk_calendar_select_month(handle, month, year);
|
|
1340 } else {
|
|
1341 calendar.set(Calendar.YEAR, year);
|
|
1342 updateControl();
|
|
1343 }
|
|
1344 }
|
|
1345
|
|
1346 int unformattedIntValue(int fieldName, String newText, bool adjust, int max) {
|
|
1347 int newValue;
|
|
1348 try {
|
|
1349 newValue = to!(int)(newText);
|
|
1350 } catch (ConversionException ex) {
|
|
1351 return -1;
|
|
1352 }
|
|
1353 if (fieldName is Calendar.MONTH && adjust) {
|
|
1354 newValue--;
|
|
1355 if (newValue is -1) newValue = max;
|
|
1356 }
|
|
1357 if (fieldName is Calendar.HOUR && adjust) {
|
|
1358 if (newValue is 12) newValue = 0; // TODO: needs more work for setFormat and locale
|
|
1359 }
|
|
1360 return newValue;
|
|
1361 }
|
|
1362
|
|
1363 public void updateControl() {
|
|
1364 if (text !is null) {
|
|
1365 String string = getFormattedString(style);
|
|
1366 ignoreVerify = true;
|
|
1367 text.setText(string);
|
|
1368 ignoreVerify = false;
|
|
1369 }
|
|
1370 redraw();
|
|
1371 }
|
|
1372 }
|