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