KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > widgets > DateTime


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 package org.eclipse.swt.widgets;
12
13
14 import org.eclipse.swt.internal.win32.*;
15 import org.eclipse.swt.*;
16 import org.eclipse.swt.events.*;
17 import org.eclipse.swt.graphics.*;
18
19 //TODO - features not yet implemented: read-only, drop-down calendar for date
20
//TODO - font, colors, background image not yet implemented (works on some platforms)
21

22 /**
23  * Instances of this class are selectable user interface
24  * objects that allow the user to enter and modify date
25  * or time values.
26  * <p>
27  * Note that although this class is a subclass of <code>Composite</code>,
28  * it does not make sense to add children to it, or set a layout on it.
29  * </p>
30  * <dl>
31  * <dt><b>Styles:</b></dt>
32  * <dd>DATE, TIME, CALENDAR, SHORT, MEDIUM, LONG</dd>
33  * <dt><b>Events:</b></dt>
34  * <dd>Selection</dd>
35  * </dl>
36  * <p>
37  * Note: Only one of the styles DATE, TIME, or CALENDAR may be specified,
38  * and only one of the styles SHORT, MEDIUM, or LONG may be specified.
39  * </p><p>
40  * IMPORTANT: This class is <em>not</em> intended to be subclassed.
41  * </p>
42  *
43  * @since 3.3
44  */

45
46 public class DateTime extends Composite {
47     static final int DateTimeProc;
48     static final TCHAR DateTimeClass = new TCHAR (0, OS.DATETIMEPICK_CLASS, true);
49     static final int CalendarProc;
50     static final TCHAR CalendarClass = new TCHAR (0, OS.MONTHCAL_CLASS, true);
51     static {
52         INITCOMMONCONTROLSEX icex = new INITCOMMONCONTROLSEX ();
53         icex.dwSize = INITCOMMONCONTROLSEX.sizeof;
54         icex.dwICC = OS.ICC_DATE_CLASSES;
55         OS.InitCommonControlsEx (icex);
56         WNDCLASS lpWndClass = new WNDCLASS ();
57         OS.GetClassInfo (0, DateTimeClass, lpWndClass);
58         DateTimeProc = lpWndClass.lpfnWndProc;
59         OS.GetClassInfo (0, CalendarClass, lpWndClass);
60         CalendarProc = lpWndClass.lpfnWndProc;
61     }
62     static final int MARGIN = 4;
63     static final int MAX_DIGIT = 9;
64     static final int MAX_DAY = 31;
65     static final int MAX_12HOUR = 12;
66     static final int MAX_24HOUR = 24;
67     static final int MAX_MINUTE = 60;
68     static final int MONTH_DAY_YEAR = 0;
69     static final int DAY_MONTH_YEAR = 1;
70     static final int YEAR_MONTH_DAY = 2;
71     static final char SINGLE_QUOTE = '\''; //$NON-NLS-1$ short date format may include quoted text
72
static final char DAY_FORMAT_CONSTANT = 'd'; //$NON-NLS-1$ 1-4 lowercase 'd's represent day
73
static final char MONTH_FORMAT_CONSTANT = 'M'; //$NON-NLS-1$ 1-4 uppercase 'M's represent month
74
static final char YEAR_FORMAT_CONSTANT = 'y'; //$NON-NLS-1$ 1-5 lowercase 'y's represent year
75
static final char HOURS_FORMAT_CONSTANT = 'h'; //$NON-NLS-1$ 1-2 upper or lowercase 'h's represent hours
76
static final char MINUTES_FORMAT_CONSTANT = 'm'; //$NON-NLS-1$ 1-2 lowercase 'm's represent minutes
77
static final char SECONDS_FORMAT_CONSTANT = 's'; //$NON-NLS-1$ 1-2 lowercase 's's represent seconds
78
static final char AMPM_FORMAT_CONSTANT = 't'; //$NON-NLS-1$ 1-2 lowercase 't's represent am/pm
79
static final int[] MONTH_NAMES = new int[] {OS.LOCALE_SMONTHNAME1, OS.LOCALE_SMONTHNAME2, OS.LOCALE_SMONTHNAME3, OS.LOCALE_SMONTHNAME4, OS.LOCALE_SMONTHNAME5, OS.LOCALE_SMONTHNAME6, OS.LOCALE_SMONTHNAME7, OS.LOCALE_SMONTHNAME8, OS.LOCALE_SMONTHNAME9, OS.LOCALE_SMONTHNAME10, OS.LOCALE_SMONTHNAME11, OS.LOCALE_SMONTHNAME12};
80
81
82 /**
83  * Constructs a new instance of this class given its parent
84  * and a style value describing its behavior and appearance.
85  * <p>
86  * The style value is either one of the style constants defined in
87  * class <code>SWT</code> which is applicable to instances of this
88  * class, or must be built by <em>bitwise OR</em>'ing together
89  * (that is, using the <code>int</code> "|" operator) two or more
90  * of those <code>SWT</code> style constants. The class description
91  * lists the style constants that are applicable to the class.
92  * Style bits are also inherited from superclasses.
93  * </p>
94  *
95  * @param parent a composite control which will be the parent of the new instance (cannot be null)
96  * @param style the style of control to construct
97  *
98  * @exception IllegalArgumentException <ul>
99  * <li>ERROR_NULL_ARGUMENT - if the parent is null</li>
100  * </ul>
101  * @exception SWTException <ul>
102  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent</li>
103  * <li>ERROR_INVALID_SUBCLASS - if this class is not an allowed subclass</li>
104  * </ul>
105  *
106  * @see SWT#DATE
107  * @see SWT#TIME
108  * @see SWT#CALENDAR
109  * @see Widget#checkSubclass
110  * @see Widget#getStyle
111  */

112 public DateTime (Composite parent, int style) {
113     super (parent, checkStyle (style));
114     if ((this.style & SWT.SHORT) != 0) {
115         String JavaDoc buffer = ((this.style & SWT.DATE) != 0) ? getCustomShortDateFormat() : getCustomShortTimeFormat();
116         TCHAR lpszFormat = new TCHAR (0, buffer, true);
117         OS.SendMessage (handle, OS.DTM_SETFORMAT, 0, lpszFormat);
118     }
119 }
120
121 /**
122  * Adds the listener to the collection of listeners who will
123  * be notified when the control is selected by the user, by sending
124  * it one of the messages defined in the <code>SelectionListener</code>
125  * interface.
126  * <p>
127  * <code>widgetSelected</code> is called when the user changes the control's value.
128  * <code>widgetDefaultSelected</code> is not called.
129  * </p>
130  *
131  * @param listener the listener which should be notified
132  *
133  * @exception IllegalArgumentException <ul>
134  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
135  * </ul>
136  * @exception SWTException <ul>
137  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
138  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
139  * </ul>
140  *
141  * @see SelectionListener
142  * @see #removeSelectionListener
143  * @see SelectionEvent
144  */

145 public void addSelectionListener (SelectionListener listener) {
146     checkWidget ();
147     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
148     TypedListener typedListener = new TypedListener (listener);
149     addListener (SWT.Selection, typedListener);
150     addListener (SWT.DefaultSelection, typedListener);
151 }
152
153 int callWindowProc (int hwnd, int msg, int wParam, int lParam) {
154     if (handle == 0) return 0;
155     return OS.CallWindowProc (windowProc (), hwnd, msg, wParam, lParam);
156 }
157
158 static int checkStyle (int style) {
159     /*
160     * Even though it is legal to create this widget
161     * with scroll bars, they serve no useful purpose
162     * because they do not automatically scroll the
163     * widget's client area. The fix is to clear
164     * the SWT style.
165     */

166     style &= ~(SWT.H_SCROLL | SWT.V_SCROLL);
167     style = checkBits (style, SWT.DATE, SWT.TIME, SWT.CALENDAR, 0, 0, 0);
168     return checkBits (style, SWT.MEDIUM, SWT.SHORT, SWT.LONG, 0, 0, 0);
169 }
170
171 protected void checkSubclass () {
172     if (!isValidSubclass ()) error (SWT.ERROR_INVALID_SUBCLASS);
173 }
174
175 public Point computeSize (int wHint, int hHint, boolean changed) {
176     checkWidget ();
177     int width = 0, height = 0;
178     if (wHint == SWT.DEFAULT || hHint == SWT.DEFAULT) {
179         if ((style & SWT.CALENDAR) != 0) {
180             RECT rect = new RECT ();
181             OS.SendMessage(handle, OS.MCM_GETMINREQRECT, 0, rect);
182             width = rect.right;
183             height = rect.bottom;
184         } else {
185             TCHAR buffer = new TCHAR (getCodePage (), 128);
186             int newFont, oldFont = 0;
187             int hDC = OS.GetDC (handle);
188             newFont = OS.SendMessage (handle, OS.WM_GETFONT, 0, 0);
189             if (newFont != 0) oldFont = OS.SelectObject (hDC, newFont);
190             RECT rect = new RECT ();
191             int flags = OS.DT_CALCRECT | OS.DT_EDITCONTROL | OS.DT_NOPREFIX;
192             SYSTEMTIME systime = new SYSTEMTIME ();
193             if ((style & SWT.DATE) != 0) {
194                 /* Determine the widest/tallest year string. */
195                 systime.wMonth = 1;
196                 systime.wDay = 1;
197                 int widest = 0, secondWidest = 0, thirdWidest = 0;
198                 for (int i = 0; i <= MAX_DIGIT; i++) {
199                     systime.wYear = (short) (2000 + i); // year 2000 + i is guaranteed to exist
200
int size = OS.GetDateFormat(OS.LOCALE_USER_DEFAULT, OS.DATE_SHORTDATE, systime, null, buffer, buffer.length ());
201                     if (size == 0) {
202                         buffer = new TCHAR (getCodePage (), size);
203                         OS.GetDateFormat(OS.LOCALE_USER_DEFAULT, OS.DATE_SHORTDATE, systime, null, buffer, buffer.length ());
204                     }
205                     rect.left = rect.top = rect.right = rect.bottom = 0;
206                     OS.DrawText (hDC, buffer, size, rect, flags);
207                     if (rect.right - rect.left >= width) {
208                         width = rect.right - rect.left;
209                         thirdWidest = secondWidest;
210                         secondWidest = widest;
211                         widest = i;
212                     }
213                     height = Math.max(height, rect.bottom - rect.top);
214                 }
215                 if (widest > 1) widest = widest * 1000 + widest * 100 + widest * 10 + widest;
216                 else if (secondWidest > 1) widest = secondWidest * 1000 + widest * 100 + widest * 10 + widest;
217                 else widest = thirdWidest * 1000 + widest * 100 + widest * 10 + widest;
218                 systime.wYear = (short) widest;
219
220                 /* Determine the widest/tallest month name string. */
221                 width = widest = 0;
222                 for (short i = 0; i < MONTH_NAMES.length; i++) {
223                     int name = MONTH_NAMES [i];
224                     int size = OS.GetLocaleInfo (OS.LOCALE_USER_DEFAULT, name, buffer, buffer.length ());
225                     if (size == 0) {
226                         buffer = new TCHAR (getCodePage (), size);
227                         OS.GetLocaleInfo (OS.LOCALE_USER_DEFAULT, name, buffer, buffer.length ());
228                     }
229                     rect.left = rect.top = rect.right = rect.bottom = 0;
230                     OS.DrawText (hDC, buffer, size, rect, flags);
231                     if (rect.right - rect.left > width) {
232                         width = rect.right - rect.left;
233                         widest = i;
234                     }
235                     height = Math.max(height, rect.bottom - rect.top);
236                 }
237                 systime.wMonth = (short) (widest + 1);
238
239                 /* Determine the widest/tallest date string in the widest month of the widest year. */
240                 int dwFlags = ((style & SWT.MEDIUM) != 0) ? OS.DATE_SHORTDATE : ((style & SWT.SHORT) != 0) ? OS.DATE_YEARMONTH : OS.DATE_LONGDATE;
241                 width = 0;
242                 for (short i = 1; i <= MAX_DAY; i++) {
243                     systime.wDay = i;
244                     int size = OS.GetDateFormat(OS.LOCALE_USER_DEFAULT, dwFlags, systime, null, buffer, buffer.length ());
245                     if (size == 0) {
246                         buffer = new TCHAR (getCodePage (), size);
247                         OS.GetDateFormat(OS.LOCALE_USER_DEFAULT, dwFlags, systime, null, buffer, buffer.length ());
248                     }
249                     rect.left = rect.top = rect.right = rect.bottom = 0;
250                     OS.DrawText (hDC, buffer, size, rect, flags);
251                     width = Math.max(width, rect.right - rect.left);
252                     height = Math.max(height, rect.bottom - rect.top);
253                     if ((style & SWT.SHORT) != 0) break;
254                 }
255             } else if ((style & SWT.TIME) != 0) {
256                 /* Determine the widest/tallest hour string. This code allows for the possibility of ligatures. */
257                 int dwFlags = ((style & SWT.SHORT) != 0) ? OS.TIME_NOSECONDS : 0;
258                 short widest = 0;
259                 int max = is24HourTime () ? MAX_24HOUR : MAX_12HOUR;
260                 for (short i = 0; i < max; i++) {
261                     systime.wHour = i;
262                     int size = OS.GetTimeFormat(OS.LOCALE_USER_DEFAULT, dwFlags, systime, null, buffer, buffer.length ());
263                     if (size == 0) {
264                         buffer = new TCHAR (getCodePage (), size);
265                         OS.GetTimeFormat(OS.LOCALE_USER_DEFAULT, dwFlags, systime, null, buffer, buffer.length ());
266                     }
267                     rect.left = rect.top = rect.right = rect.bottom = 0;
268                     OS.DrawText (hDC, buffer, size, rect, flags);
269                     if (rect.right - rect.left > width) {
270                         width = rect.right - rect.left;
271                         widest = i;
272                     }
273                     height = Math.max(height, rect.bottom - rect.top);
274                 }
275                 systime.wHour = widest;
276
277                 /* Determine the widest/tallest minute and second string. */
278                 width = widest = 0;
279                 for (short i = 0; i < MAX_MINUTE; i++) {
280                     systime.wMinute = i;
281                     int size = OS.GetTimeFormat(OS.LOCALE_USER_DEFAULT, dwFlags, systime, null, buffer, buffer.length ());
282                     if (size == 0) {
283                         buffer = new TCHAR (getCodePage (), size);
284                         OS.GetTimeFormat(OS.LOCALE_USER_DEFAULT, dwFlags, systime, null, buffer, buffer.length ());
285                     }
286                     rect.left = rect.top = rect.right = rect.bottom = 0;
287                     OS.DrawText (hDC, buffer, size, rect, flags);
288                     if (rect.right - rect.left > width) {
289                         width = rect.right - rect.left;
290                         widest = i;
291                     }
292                     height = Math.max(height, rect.bottom - rect.top);
293                 }
294                 systime.wMinute = widest;
295                 systime.wSecond = widest;
296
297                 /* Determine the widest/tallest time string for the widest hour, widest minute, and if applicable, widest second. */
298                 int size = OS.GetTimeFormat(OS.LOCALE_USER_DEFAULT, dwFlags, systime, null, buffer, buffer.length ());
299                 if (size == 0) {
300                     buffer = new TCHAR (getCodePage (), size);
301                     OS.GetTimeFormat(OS.LOCALE_USER_DEFAULT, dwFlags, systime, null, buffer, buffer.length ());
302                 }
303                 rect.left = rect.top = rect.right = rect.bottom = 0;
304                 OS.DrawText (hDC, buffer, size, rect, flags);
305                 width = rect.right - rect.left;
306                 height = Math.max(height, rect.bottom - rect.top);
307             }
308             if (newFont != 0) OS.SelectObject (hDC, oldFont);
309             OS.ReleaseDC (handle, hDC);
310             int upDownWidth = OS.GetSystemMetrics (OS.SM_CXVSCROLL);
311             width += upDownWidth + MARGIN;
312             int upDownHeight = OS.GetSystemMetrics (OS.SM_CYVSCROLL);
313             // TODO: On Vista, can send DTM_GETDATETIMEPICKERINFO to ask the Edit control what its margins are
314
if (!OS.IsWinCE && OS.WIN32_VERSION >= OS.VERSION (6, 0)) upDownHeight += 7;
315             height = Math.max (height, upDownHeight);
316         }
317     }
318     if (width == 0) width = DEFAULT_WIDTH;
319     if (height == 0) height = DEFAULT_HEIGHT;
320     if (wHint != SWT.DEFAULT) width = wHint;
321     if (hHint != SWT.DEFAULT) height = hHint;
322     int border = getBorderWidth ();
323     width += border * 2;
324     height += border * 2;
325     return new Point (width, height);
326 }
327
328 void createHandle () {
329     super.createHandle ();
330     state &= ~(CANVAS | THEME_BACKGROUND);
331 }
332
333 int defaultBackground () {
334     return OS.GetSysColor (OS.COLOR_WINDOW);
335 }
336
337 String JavaDoc getComputeSizeString () {
338     // TODO: Not currently used but might need for WinCE
339
if ((style & SWT.DATE) != 0) {
340         if ((style & SWT.SHORT) != 0) return getCustomShortDateFormat ();
341         if ((style & SWT.MEDIUM) != 0) return getShortDateFormat ();
342         if ((style & SWT.LONG) != 0) return getLongDateFormat ();
343     }
344     if ((style & SWT.TIME) != 0) {
345         if ((style & SWT.SHORT) != 0) return getCustomShortTimeFormat ();
346         return getTimeFormat ();
347     }
348     return "";
349 }
350
351 String JavaDoc getCustomShortDateFormat () {
352     if (true) {
353         TCHAR tchar = new TCHAR (getCodePage (), 80);
354         int size = OS.GetLocaleInfo (OS.LOCALE_USER_DEFAULT, OS.LOCALE_SYEARMONTH, tchar, 80);
355         return size != 0 ? tchar.toString (0, size - 1) : "M/yyyy"; //$NON-NLS-1$
356
}
357     
358     //TODO: Not currently used, but may need for WinCE (or if numeric short date is required)
359
StringBuffer JavaDoc buffer = new StringBuffer JavaDoc (getShortDateFormat ());
360     int length = buffer.length ();
361     boolean inQuotes = false;
362     int start = 0, end = 0;
363     while (start < length) {
364         char ch = buffer.charAt (start);
365         if (ch == SINGLE_QUOTE) inQuotes = !inQuotes;
366         else if (ch == DAY_FORMAT_CONSTANT && !inQuotes) {
367             end = start + 1;
368             while (end < length && buffer.charAt (end) == DAY_FORMAT_CONSTANT) end++;
369             int ordering = getShortDateFormatOrdering ();
370             switch (ordering) {
371             case MONTH_DAY_YEAR:
372                 // skip the following separator
373
while (end < length && buffer.charAt (end) != YEAR_FORMAT_CONSTANT) end++;
374                 break;
375             case DAY_MONTH_YEAR:
376                 // skip the following separator
377
while (end < length && buffer.charAt (end) != MONTH_FORMAT_CONSTANT) end++;
378                 break;
379             case YEAR_MONTH_DAY:
380                 // skip the preceding separator
381
while (start > 0 && buffer.charAt (start) != MONTH_FORMAT_CONSTANT) start--;
382                 break;
383             }
384             break;
385         }
386         start++;
387     }
388     if (start < end) buffer.delete (start, end);
389     return buffer.toString ();
390 }
391
392 String JavaDoc getCustomShortTimeFormat () {
393     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc (getTimeFormat ());
394     int length = buffer.length ();
395     boolean inQuotes = false;
396     int start = 0, end = 0;
397     while (start < length) {
398         char ch = buffer.charAt (start);
399         if (ch == SINGLE_QUOTE) inQuotes = !inQuotes;
400         else if (ch == SECONDS_FORMAT_CONSTANT && !inQuotes) {
401             end = start + 1;
402             while (end < length && buffer.charAt (end) == SECONDS_FORMAT_CONSTANT) end++;
403             // skip the preceding separator
404
while (start > 0 && buffer.charAt (start) != MINUTES_FORMAT_CONSTANT) start--;
405             start++;
406             break;
407         }
408         start++;
409     }
410     if (start < end) buffer.delete (start, end);
411     return buffer.toString ();
412 }
413
414 String JavaDoc getLongDateFormat () {
415     //TODO: Not currently used, but may need for WinCE
416
TCHAR tchar = new TCHAR (getCodePage (), 80);
417     int size = OS.GetLocaleInfo (OS.LOCALE_USER_DEFAULT, OS.LOCALE_SLONGDATE, tchar, 80);
418     return size > 0 ? tchar.toString (0, size - 1) : "dddd, MMMM dd, yyyy"; //$NON-NLS-1$
419
}
420
421 String JavaDoc getShortDateFormat () {
422     //TODO: Not currently used, but may need for WinCE
423
TCHAR tchar = new TCHAR (getCodePage (), 80);
424     //TODO: May need to OR with LOCALE_ICENTURY
425
int size = OS.GetLocaleInfo (OS.LOCALE_USER_DEFAULT, OS.LOCALE_SSHORTDATE, tchar, 80);
426     return size > 0 ? tchar.toString (0, size - 1) : "M/d/yyyy"; //$NON-NLS-1$
427
}
428
429 int getShortDateFormatOrdering () {
430     //TODO: Not currently used, but may need for WinCE
431
TCHAR tchar = new TCHAR (getCodePage (), 4);
432     int size = OS.GetLocaleInfo (OS.LOCALE_USER_DEFAULT, OS.LOCALE_IDATE, tchar, 4);
433     if (size > 0) {
434         String JavaDoc number = tchar.toString (0, size - 1);
435         return Integer.parseInt (number);
436     }
437     return 0;
438 }
439
440 String JavaDoc getTimeFormat () {
441     TCHAR tchar = new TCHAR (getCodePage (), 80);
442     int size = OS.GetLocaleInfo (OS.LOCALE_USER_DEFAULT, OS.LOCALE_STIMEFORMAT, tchar, 80);
443     return size > 0 ? tchar.toString (0, size - 1) : "h:mm:ss tt"; //$NON-NLS-1$
444
}
445
446 boolean is24HourTime () {
447     TCHAR tchar = new TCHAR (getCodePage (), 4);
448     int size = OS.GetLocaleInfo (OS.LOCALE_USER_DEFAULT, OS.LOCALE_ITIME, tchar, 4);
449     if (size > 0) {
450         String JavaDoc number = tchar.toString (0, size - 1);
451         return Integer.parseInt (number) != 0;
452     }
453     return true;
454 }
455
456 /**
457  * Returns the receiver's date, or day of the month.
458  * <p>
459  * The first day of the month is 1, and the last day depends on the month and year.
460  * </p>
461  *
462  * @return a positive integer beginning with 1
463  *
464  * @exception SWTException <ul>
465  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
466  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
467  * </ul>
468  */

469 public int getDay () {
470     checkWidget ();
471     SYSTEMTIME systime = new SYSTEMTIME ();
472     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
473     OS.SendMessage (handle, msg, 0, systime);
474     return systime.wDay;
475 }
476
477 /**
478  * Returns the receiver's hours.
479  * <p>
480  * Hours is an integer between 0 and 23.
481  * </p>
482  *
483  * @return an integer between 0 and 23
484  *
485  * @exception SWTException <ul>
486  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
487  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
488  * </ul>
489  */

490 public int getHours () {
491     checkWidget ();
492     SYSTEMTIME systime = new SYSTEMTIME ();
493     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
494     OS.SendMessage (handle, msg, 0, systime);
495     return systime.wHour;
496 }
497
498 /**
499  * Returns the receiver's minutes.
500  * <p>
501  * Minutes is an integer between 0 and 59.
502  * </p>
503  *
504  * @return an integer between 0 and 59
505  *
506  * @exception SWTException <ul>
507  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
508  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
509  * </ul>
510  */

511 public int getMinutes () {
512     checkWidget ();
513     SYSTEMTIME systime = new SYSTEMTIME ();
514     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
515     OS.SendMessage (handle, msg, 0, systime);
516     return systime.wMinute;
517 }
518
519 /**
520  * Returns the receiver's month.
521  * <p>
522  * The first month of the year is 0, and the last month is 11.
523  * </p>
524  *
525  * @return an integer between 0 and 11
526  *
527  * @exception SWTException <ul>
528  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
529  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
530  * </ul>
531  */

532 public int getMonth () {
533     checkWidget ();
534     SYSTEMTIME systime = new SYSTEMTIME ();
535     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
536     OS.SendMessage (handle, msg, 0, systime);
537     return systime.wMonth - 1;
538 }
539
540 String JavaDoc getNameText () {
541     return "DateTime";
542 }
543
544 /**
545  * Returns the receiver's seconds.
546  * <p>
547  * Seconds is an integer between 0 and 59.
548  * </p>
549  *
550  * @return an integer between 0 and 59
551  *
552  * @exception SWTException <ul>
553  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
554  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
555  * </ul>
556  */

557 public int getSeconds () {
558     checkWidget ();
559     SYSTEMTIME systime = new SYSTEMTIME ();
560     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
561     OS.SendMessage (handle, msg, 0, systime);
562     return systime.wSecond;
563 }
564
565 /**
566  * Returns the receiver's year.
567  * <p>
568  * The first year is 1752 and the last year is 9999.
569  * </p>
570  *
571  * @return an integer between 1752 and 9999
572  *
573  * @exception SWTException <ul>
574  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
575  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
576  * </ul>
577  */

578 public int getYear () {
579     checkWidget ();
580     SYSTEMTIME systime = new SYSTEMTIME ();
581     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
582     OS.SendMessage (handle, msg, 0, systime);
583     return systime.wYear;
584 }
585
586 /**
587  * Removes the listener from the collection of listeners who will
588  * be notified when the control is selected by the user.
589  *
590  * @param listener the listener which should no longer be notified
591  *
592  * @exception IllegalArgumentException <ul>
593  * <li>ERROR_NULL_ARGUMENT - if the listener is null</li>
594  * </ul>
595  * @exception SWTException <ul>
596  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
597  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
598  * </ul>
599  *
600  * @see SelectionListener
601  * @see #addSelectionListener
602  */

603 public void removeSelectionListener (SelectionListener listener) {
604     checkWidget ();
605     if (listener == null) error (SWT.ERROR_NULL_ARGUMENT);
606     if (eventTable == null) return;
607     eventTable.unhook (SWT.Selection, listener);
608     eventTable.unhook (SWT.DefaultSelection, listener);
609 }
610
611 /**
612  * Sets the receiver's date, or day of the month, to the specified day.
613  * <p>
614  * The first day of the month is 1, and the last day depends on the month and year.
615  * </p>
616  *
617  * @param day a positive integer beginning with 1
618  *
619  * @exception SWTException <ul>
620  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
621  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
622  * </ul>
623  */

624 public void setDay (int day) {
625     checkWidget ();
626     SYSTEMTIME systime = new SYSTEMTIME ();
627     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
628     OS.SendMessage (handle, msg, 0, systime);
629     msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_SETCURSEL : OS.DTM_SETSYSTEMTIME;
630     systime.wDay = (short)day;
631     OS.SendMessage (handle, msg, 0, systime);
632 }
633
634 /**
635  * Sets the receiver's hours.
636  * <p>
637  * Hours is an integer between 0 and 23.
638  * </p>
639  *
640  * @param hours an integer between 0 and 23
641  *
642  * @exception SWTException <ul>
643  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
644  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
645  * </ul>
646  */

647 public void setHours (int hours) {
648     checkWidget ();
649     SYSTEMTIME systime = new SYSTEMTIME ();
650     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
651     OS.SendMessage (handle, msg, 0, systime);
652     msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_SETCURSEL : OS.DTM_SETSYSTEMTIME;
653     systime.wHour = (short)hours;
654     OS.SendMessage (handle, msg, 0, systime);
655 }
656
657 /**
658  * Sets the receiver's minutes.
659  * <p>
660  * Minutes is an integer between 0 and 59.
661  * </p>
662  *
663  * @param minutes an integer between 0 and 59
664  *
665  * @exception SWTException <ul>
666  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
667  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
668  * </ul>
669  */

670 public void setMinutes (int minutes) {
671     checkWidget ();
672     SYSTEMTIME systime = new SYSTEMTIME ();
673     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
674     OS.SendMessage (handle, msg, 0, systime);
675     msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_SETCURSEL : OS.DTM_SETSYSTEMTIME;
676     systime.wMinute = (short)minutes;
677     OS.SendMessage (handle, msg, 0, systime);
678 }
679
680 /**
681  * Sets the receiver's month.
682  * <p>
683  * The first month of the year is 0, and the last month is 11.
684  * </p>
685  *
686  * @param month an integer between 0 and 11
687  *
688  * @exception SWTException <ul>
689  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
690  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
691  * </ul>
692  */

693 public void setMonth (int month) {
694     checkWidget ();
695     SYSTEMTIME systime = new SYSTEMTIME ();
696     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
697     OS.SendMessage (handle, msg, 0, systime);
698     msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_SETCURSEL : OS.DTM_SETSYSTEMTIME;
699     systime.wMonth = (short)(month + 1);
700     OS.SendMessage (handle, msg, 0, systime);
701 }
702
703 /**
704  * Sets the receiver's seconds.
705  * <p>
706  * Seconds is an integer between 0 and 59.
707  * </p>
708  *
709  * @param seconds an integer between 0 and 59
710  *
711  * @exception SWTException <ul>
712  * <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
713  * <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
714  * </ul>
715  */

716 public void setSeconds (int seconds) {
717     checkWidget ();
718     SYSTEMTIME systime = new SYSTEMTIME ();
719     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
720     OS.SendMessage (handle, msg, 0, systime);
721     msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_SETCURSEL : OS.DTM_SETSYSTEMTIME;
722     systime.wSecond = (short)seconds;
723     OS.SendMessage (handle, msg, 0, systime);
724 }
725
726 /**
727  * Sets the receiver's year.
728  * <p>
729  * The first year is 1752 and the last year is 9999.
730  * </p>
731  *
732  * @param year an integer between 1752 and 9999
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 void setYear (int year) {
740     checkWidget ();
741     SYSTEMTIME systime = new SYSTEMTIME ();
742     int msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_GETCURSEL : OS.DTM_GETSYSTEMTIME;
743     OS.SendMessage (handle, msg, 0, systime);
744     msg = (style & SWT.CALENDAR) != 0 ? OS.MCM_SETCURSEL : OS.DTM_SETSYSTEMTIME;
745     systime.wYear = (short)year;
746     OS.SendMessage (handle, msg, 0, systime);
747 }
748
749 int widgetStyle () {
750     int bits = super.widgetStyle () | OS.WS_TABSTOP;
751     if ((style & SWT.CALENDAR) != 0) return bits | OS.MCS_NOTODAY;
752     /*
753     * Bug in Windows: When WS_CLIPCHILDREN is set in a
754     * Date and Time Picker, the widget draws on top of
755     * the updown control. The fix is to clear the bits.
756     */

757     bits &= ~OS.WS_CLIPCHILDREN;
758     if ((style & SWT.TIME) != 0) bits |= OS.DTS_TIMEFORMAT;
759     if ((style & SWT.DATE) != 0) bits |= ((style & SWT.MEDIUM) != 0 ? OS.DTS_SHORTDATECENTURYFORMAT : OS.DTS_LONGDATEFORMAT) | OS.DTS_UPDOWN;
760     return bits;
761 }
762
763 TCHAR windowClass () {
764     return (style & SWT.CALENDAR) != 0 ? CalendarClass : DateTimeClass;
765 }
766
767 int windowProc () {
768     return (style & SWT.CALENDAR) != 0 ? CalendarProc : DateTimeProc;
769 }
770
771 LRESULT wmNotifyChild (NMHDR hdr, int wParam, int lParam) {
772     switch (hdr.code) {
773         case OS.MCN_SELCHANGE: //SENT WHEN YOU SET IT?
774
case OS.DTN_DATETIMECHANGE:
775             sendEvent (SWT.Selection);
776             break;
777     }
778     return super.wmNotifyChild (hdr, wParam, lParam);
779 }
780 }
781
Popular Tags