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