comparison dwt/widgets/DateTime.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 649b8e223d5a
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
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 *******************************************************************************/
11 module dwt.widgets.DateTime;
12
13 import dwt.dwthelper.utils;
14
15 import dwt.DWT;
16 import dwt.DWTException;
17 import dwt.events.SelectionEvent;
18 import dwt.events.SelectionListener;
19 import dwt.graphics.Point;
20 import dwt.internal.cocoa.NSCalendarDate;
21 import dwt.internal.cocoa.NSColor;
22 import dwt.internal.cocoa.NSControl;
23 import dwt.internal.cocoa.NSDate;
24 import dwt.internal.cocoa.NSDatePicker;
25 import dwt.internal.cocoa.NSRect;
26 import dwt.internal.cocoa.OS;
27 import dwt.internal.cocoa.SWTDatePicker;
28
29 /**
30 * Instances of this class are selectable user interface
31 * objects that allow the user to enter and modify date
32 * or time values.
33 * <p>
34 * Note that although this class is a subclass of <code>Composite</code>,
35 * it does not make sense to add children to it, or set a layout on it.
36 * </p>
37 * <dl>
38 * <dt><b>Styles:</b></dt>
39 * <dd>DATE, TIME, CALENDAR, SHORT, MEDIUM, LONG</dd>
40 * <dt><b>Events:</b></dt>
41 * <dd>Selection</dd>
42 * </dl>
43 * <p>
44 * Note: Only one of the styles DATE, TIME, or CALENDAR may be specified,
45 * and only one of the styles SHORT, MEDIUM, or LONG may be specified.
46 * </p><p>
47 * IMPORTANT: This class is <em>not</em> intended to be subclassed.
48 * </p>
49 *
50 * @since 3.3
51 */
52 public class DateTime extends Composite {
53
54 /**
55 * Constructs a new instance of this class given its parent
56 * and a style value describing its behavior and appearance.
57 * <p>
58 * The style value is either one of the style constants defined in
59 * class <code>DWT</code> which is applicable to instances of this
60 * class, or must be built by <em>bitwise OR</em>'ing together
61 * (that is, using the <code>int</code> "|" operator) two or more
62 * of those <code>DWT</code> style constants. The class description
63 * lists the style constants that are applicable to the class.
64 * Style bits are also inherited from superclasses.
65 * </p>
66 *
67 * @param parent a composite control which will be the parent of the new instance (cannot be null)
68 * @param style the style of control to construct
69 *
70 * @exception IllegalArgumentException <ul>
71 * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
72 * </ul>
73 * @exception DWTException <ul>
74 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
75 * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
76 * </ul>
77 *
78 * @see DWT#DATE
79 * @see DWT#TIME
80 * @see DWT#CALENDAR
81 * @see Widget#checkSubclass
82 * @see Widget#getStyle
83 */
84 public DateTime (Composite parent, int style) {
85 super (parent, checkStyle (style));
86 }
87
88 static int checkStyle (int style) {
89 /*
90 * Even though it is legal to create this widget
91 * with scroll bars, they serve no useful purpose
92 * because they do not automatically scroll the
93 * widget's client area. The fix is to clear
94 * the DWT style.
95 */
96 style &= ~(DWT.H_SCROLL | DWT.V_SCROLL);
97 style = checkBits (style, DWT.MEDIUM, DWT.SHORT, DWT.LONG, 0, 0, 0);
98 return checkBits (style, DWT.DATE, DWT.TIME, DWT.CALENDAR, 0, 0, 0);
99 }
100
101 protected void checkSubclass () {
102 if (!isValidSubclass ()) error (DWT.ERROR_INVALID_SUBCLASS);
103 }
104
105 /**
106 * Adds the listener to the collection of listeners who will
107 * be notified when the control is selected by the user, by sending
108 * it one of the messages defined in the <code>SelectionListener</code>
109 * interface.
110 * <p>
111 * <code>widgetSelected</code> is called when the user changes the control's value.
112 * <code>widgetDefaultSelected</code> is not called.
113 * </p>
114 *
115 * @param listener the listener which should be notified
116 *
117 * @exception IllegalArgumentException <ul>
118 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
119 * </ul>
120 * @exception DWTException <ul>
121 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
122 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
123 * </ul>
124 *
125 * @see SelectionListener
126 * @see #removeSelectionListener
127 * @see SelectionEvent
128 */
129 public void addSelectionListener (SelectionListener listener) {
130 checkWidget ();
131 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
132 TypedListener typedListener = new TypedListener (listener);
133 addListener (DWT.Selection, typedListener);
134 addListener (DWT.DefaultSelection, typedListener);
135 }
136
137 public Point computeSize (int wHint, int hHint, bool changed) {
138 checkWidget ();
139 int width = 0, height = 0;
140 NSControl widget = (NSControl)view;
141 NSRect oldRect = widget.frame();
142 widget.sizeToFit();
143 NSRect newRect = widget.frame();
144 widget.setFrame (oldRect);
145 width = (int)newRect.width;
146 height = (int)newRect.height;
147 if (width is 0) width = DEFAULT_WIDTH;
148 if (height is 0) height = DEFAULT_HEIGHT;
149 if (wHint !is DWT.DEFAULT) width = wHint;
150 if (hHint !is DWT.DEFAULT) height = hHint;
151 int border = getBorderWidth ();
152 width += border * 2; height += border * 2;
153 return new Point (width, height);
154 }
155
156 void createHandle () {
157 NSDatePicker widget = (NSDatePicker)new SWTDatePicker().alloc();
158 widget.initWithFrame(new NSRect());
159 int pickerStyle = OS.NSTextFieldAndStepperDatePickerStyle;
160 int elementFlags = 0;
161 if ((style & DWT.CALENDAR) !is 0) {
162 pickerStyle = OS.NSClockAndCalendarDatePickerStyle;
163 elementFlags = OS.NSYearMonthDayDatePickerElementFlag;
164 } else {
165 if ((style & DWT.TIME) !is 0) {
166 elementFlags = (style & DWT.SHORT) !is 0 ? OS.NSHourMinuteDatePickerElementFlag : OS.NSHourMinuteSecondDatePickerElementFlag;
167 }
168 if ((style & DWT.DATE) !is 0) {
169 elementFlags = (style & DWT.SHORT) !is 0 ? OS.NSYearMonthDatePickerElementFlag : OS.NSYearMonthDayDatePickerElementFlag;
170 }
171 }
172 widget.setDrawsBackground(true);
173 widget.setDatePickerStyle(pickerStyle);
174 widget.setDatePickerElements(elementFlags);
175 NSDate date = NSCalendarDate.calendarDate();
176 widget.setDateValue(date);
177 widget.setTarget(widget);
178 widget.setAction(OS.sel_sendSelection);
179 widget.setTag(jniRef);
180 view = widget;
181 parent.contentView().addSubview_(widget);
182 }
183
184 /**
185 * Returns the receiver's date, or day of the month.
186 * <p>
187 * The first day of the month is 1, and the last day depends on the month and year.
188 * </p>
189 *
190 * @return a positive integer beginning with 1
191 *
192 * @exception DWTException <ul>
193 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
194 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
195 * </ul>
196 */
197 public int getDay () {
198 checkWidget ();
199 NSDate date = ((NSDatePicker)view).dateValue();
200 NSCalendarDate calendarDate = date.dateWithCalendarFormat(null, null);
201 return calendarDate.dayOfMonth();
202 }
203
204 /**
205 * Returns the receiver's hours.
206 * <p>
207 * Hours is an integer between 0 and 23.
208 * </p>
209 *
210 * @return an integer between 0 and 23
211 *
212 * @exception DWTException <ul>
213 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
214 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
215 * </ul>
216 */
217 public int getHours () {
218 checkWidget ();
219 return new NSCalendarDate(((NSDatePicker)view).dateValue().id).hourOfDay();
220 }
221
222 /**
223 * Returns the receiver's minutes.
224 * <p>
225 * Minutes is an integer between 0 and 59.
226 * </p>
227 *
228 * @return an integer between 0 and 59
229 *
230 * @exception DWTException <ul>
231 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
232 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
233 * </ul>
234 */
235 public int getMinutes () {
236 checkWidget ();
237 return new NSCalendarDate(((NSDatePicker)view).dateValue().id).minuteOfHour();
238 }
239
240 /**
241 * Returns the receiver's month.
242 * <p>
243 * The first month of the year is 0, and the last month is 11.
244 * </p>
245 *
246 * @return an integer between 0 and 11
247 *
248 * @exception DWTException <ul>
249 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
250 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
251 * </ul>
252 */
253 public int getMonth () {
254 checkWidget ();
255 return new NSCalendarDate(((NSDatePicker)view).dateValue().id).monthOfYear() - 1;
256 }
257
258 String getNameText() {
259 return (style & DWT.TIME) !is 0 ? getHours() + ":" + getMinutes() + ":" + getSeconds()
260 : (getMonth() + 1) + "/" + getDay() + "/" + getYear();
261 }
262
263 /**
264 * Returns the receiver's seconds.
265 * <p>
266 * Seconds is an integer between 0 and 59.
267 * </p>
268 *
269 * @return an integer between 0 and 59
270 *
271 * @exception DWTException <ul>
272 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
273 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
274 * </ul>
275 */
276 public int getSeconds () {
277 checkWidget ();
278 return new NSCalendarDate(((NSDatePicker)view).dateValue().id).secondOfMinute();
279 }
280
281 /**
282 * Returns the receiver's year.
283 * <p>
284 * The first year is 1752 and the last year is 9999.
285 * </p>
286 *
287 * @return an integer between 1752 and 9999
288 *
289 * @exception DWTException <ul>
290 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
291 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
292 * </ul>
293 */
294 public int getYear () {
295 checkWidget ();
296 return new NSCalendarDate(((NSDatePicker)view).dateValue().id).yearOfCommonEra();
297 }
298
299 /**
300 * Removes the listener from the collection of listeners who will
301 * be notified when the control is selected by the user.
302 *
303 * @param listener the listener which should no longer be notified
304 *
305 * @exception IllegalArgumentException <ul>
306 * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
307 * </ul>
308 * @exception DWTException <ul>
309 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
310 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
311 * </ul>
312 *
313 * @see SelectionListener
314 * @see #addSelectionListener
315 */
316 public void removeSelectionListener (SelectionListener listener) {
317 checkWidget ();
318 if (listener is null) error (DWT.ERROR_NULL_ARGUMENT);
319 if (eventTable is null) return;
320 eventTable.unhook (DWT.Selection, listener);
321 eventTable.unhook (DWT.DefaultSelection, listener);
322 }
323
324 void sendSelection () {
325 postEvent (DWT.Selection);
326 }
327
328 void setBackground (float [] color) {
329 NSColor nsColor;
330 if (color is null) {
331 return; // TODO reset to OS default
332 } else {
333 nsColor = NSColor.colorWithDeviceRed(color[0], color[1], color[2], 1);
334 }
335 ((NSDatePicker)view).setBackgroundColor(nsColor);
336 }
337
338 /**
339 * Sets the receiver's year, month, and day in a single operation.
340 * <p>
341 * This is the recommended way to set the date, because setting the year,
342 * month, and day separately may result in invalid intermediate dates.
343 * </p>
344 *
345 * @param year an integer between 1752 and 9999
346 * @param month an integer between 0 and 11
347 * @param day a positive integer beginning with 1
348 *
349 * @exception DWTException <ul>
350 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
351 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
352 * </ul>
353 *
354 * @since 3.4
355 */
356 public void setDate (int year, int month, int day) {
357 checkWidget ();
358 NSCalendarDate date = new NSCalendarDate(((NSDatePicker)view).dateValue().id);
359 NSCalendarDate newDate = NSCalendarDate.dateWithYear(year, month + 1, day,
360 date.hourOfDay(), date.minuteOfHour(), date.secondOfMinute(), date.timeZone());
361 if (newDate.yearOfCommonEra() is year && newDate.monthOfYear() is month + 1 && newDate.dayOfMonth() is day) {
362 ((NSDatePicker)view).setDateValue(newDate);
363 }
364 }
365
366 /**
367 * Sets the receiver's date, or day of the month, to the specified day.
368 * <p>
369 * The first day of the month is 1, and the last day depends on the month and year.
370 * </p>
371 *
372 * @param day a positive integer beginning with 1
373 *
374 * @exception DWTException <ul>
375 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
376 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
377 * </ul>
378 */
379 public void setDay (int day) {
380 checkWidget ();
381 NSCalendarDate date = new NSCalendarDate(((NSDatePicker)view).dateValue().id);
382 NSCalendarDate newDate = NSCalendarDate.dateWithYear(date.yearOfCommonEra(), date.monthOfYear(), day,
383 date.hourOfDay(), date.minuteOfHour(), date.secondOfMinute(), date.timeZone());
384 if (newDate.yearOfCommonEra() is date.yearOfCommonEra() && newDate.monthOfYear() is date.monthOfYear() && newDate.dayOfMonth() is day) {
385 ((NSDatePicker)view).setDateValue(newDate);
386 }
387 }
388
389 void setForeground (float [] color) {
390 NSColor nsColor;
391 if (color is null) {
392 return; // TODO reset to OS default
393 } else {
394 nsColor = NSColor.colorWithDeviceRed(color[0], color[1], color[2], 1);
395 }
396 ((NSDatePicker)view).setTextColor(nsColor);
397 }
398
399 /**
400 * Sets the receiver's hours.
401 * <p>
402 * Hours is an integer between 0 and 23.
403 * </p>
404 *
405 * @param hours an integer between 0 and 23
406 *
407 * @exception DWTException <ul>
408 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
409 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
410 * </ul>
411 */
412 public void setHours (int hours) {
413 checkWidget ();
414 if (hours < 0 || hours > 23) return;
415 NSCalendarDate date = new NSCalendarDate(((NSDatePicker)view).dateValue().id);
416 NSCalendarDate newDate = NSCalendarDate.dateWithYear(date.yearOfCommonEra(), date.monthOfYear(), date.dayOfMonth(),
417 hours, date.minuteOfHour(), date.secondOfMinute(), date.timeZone());
418 ((NSDatePicker)view).setDateValue(newDate);
419 }
420
421 /**
422 * Sets the receiver's minutes.
423 * <p>
424 * Minutes is an integer between 0 and 59.
425 * </p>
426 *
427 * @param minutes an integer between 0 and 59
428 *
429 * @exception DWTException <ul>
430 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
431 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
432 * </ul>
433 */
434 public void setMinutes (int minutes) {
435 checkWidget ();
436 if (minutes < 0 || minutes > 59) return;
437 NSCalendarDate date = new NSCalendarDate(((NSDatePicker)view).dateValue().id);
438 NSCalendarDate newDate = NSCalendarDate.dateWithYear(date.yearOfCommonEra(), date.monthOfYear(), date.dayOfMonth(),
439 date.hourOfDay(), minutes, date.secondOfMinute(), date.timeZone());
440 ((NSDatePicker)view).setDateValue(newDate);
441 }
442
443 /**
444 * Sets the receiver's month.
445 * <p>
446 * The first month of the year is 0, and the last month is 11.
447 * </p>
448 *
449 * @param month an integer between 0 and 11
450 *
451 * @exception DWTException <ul>
452 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
453 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
454 * </ul>
455 */
456 public void setMonth (int month) {
457 checkWidget ();
458 NSCalendarDate date = new NSCalendarDate(((NSDatePicker)view).dateValue().id);
459 NSCalendarDate newDate = NSCalendarDate.dateWithYear(date.yearOfCommonEra(), month + 1, date.dayOfMonth(),
460 date.hourOfDay(), date.minuteOfHour(), date.secondOfMinute(), date.timeZone());
461 if (newDate.yearOfCommonEra() is date.yearOfCommonEra() && newDate.monthOfYear() is month + 1 && newDate.dayOfMonth() is date.dayOfMonth()) {
462 ((NSDatePicker)view).setDateValue(newDate);
463 }
464 }
465
466 /**
467 * Sets the receiver's seconds.
468 * <p>
469 * Seconds is an integer between 0 and 59.
470 * </p>
471 *
472 * @param seconds an integer between 0 and 59
473 *
474 * @exception DWTException <ul>
475 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
476 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
477 * </ul>
478 */
479 public void setSeconds (int seconds) {
480 checkWidget ();
481 if (seconds < 0 || seconds > 59) return;
482 NSCalendarDate date = new NSCalendarDate(((NSDatePicker)view).dateValue().id);
483 NSCalendarDate newDate = NSCalendarDate.dateWithYear(date.yearOfCommonEra(), date.monthOfYear(), date.dayOfMonth(),
484 date.hourOfDay(), date.minuteOfHour(), seconds, date.timeZone());
485 ((NSDatePicker)view).setDateValue(newDate);
486 }
487
488 /**
489 * Sets the receiver's hours, minutes, and seconds in a single operation.
490 *
491 * @param hours an integer between 0 and 23
492 * @param minutes an integer between 0 and 59
493 * @param seconds an integer between 0 and 59
494 *
495 * @exception DWTException <ul>
496 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
497 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
498 * </ul>
499 *
500 * @since 3.4
501 */
502 public void setTime (int hours, int minutes, int seconds) {
503 checkWidget ();
504 if (hours < 0 || hours > 23 || minutes < 0 || minutes > 59 || seconds < 0 || seconds > 59) return;
505 NSCalendarDate date = new NSCalendarDate(((NSDatePicker)view).dateValue().id);
506 NSCalendarDate newDate = NSCalendarDate.dateWithYear(date.yearOfCommonEra(), date.monthOfYear(), date.dayOfMonth(),
507 hours, minutes, seconds, date.timeZone());
508 ((NSDatePicker)view).setDateValue(newDate);
509 }
510
511 /**
512 * Sets the receiver's year.
513 * <p>
514 * The first year is 1752 and the last year is 9999.
515 * </p>
516 *
517 * @param year an integer between 1752 and 9999
518 *
519 * @exception DWTException <ul>
520 * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
521 * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
522 * </ul>
523 */
524 public void setYear (int year) {
525 checkWidget ();
526 NSCalendarDate date = new NSCalendarDate(((NSDatePicker)view).dateValue().id);
527 NSCalendarDate newDate = NSCalendarDate.dateWithYear(year, date.monthOfYear(), date.dayOfMonth(),
528 date.hourOfDay(), date.minuteOfHour(), date.secondOfMinute(), date.timeZone());
529 if (newDate.yearOfCommonEra() is year && newDate.monthOfYear() is date.monthOfYear() && newDate.dayOfMonth() is date.dayOfMonth()) {
530 ((NSDatePicker)view).setDateValue(newDate);
531 }
532 }
533 }