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