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