1 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 22 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 = '\''; static final char DAY_FORMAT_CONSTANT = 'd'; static final char MONTH_FORMAT_CONSTANT = 'M'; static final char YEAR_FORMAT_CONSTANT = 'y'; static final char HOURS_FORMAT_CONSTANT = 'h'; static final char MINUTES_FORMAT_CONSTANT = 'm'; static final char SECONDS_FORMAT_CONSTANT = 's'; static final char AMPM_FORMAT_CONSTANT = 't'; 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 112 public DateTime (Composite parent, int style) { 113 super (parent, checkStyle (style)); 114 if ((this.style & SWT.SHORT) != 0) { 115 String 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 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 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 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); 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 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 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 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 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 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 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 getComputeSizeString () { 338 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 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"; } 357 358 StringBuffer buffer = new StringBuffer (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 while (end < length && buffer.charAt (end) != YEAR_FORMAT_CONSTANT) end++; 374 break; 375 case DAY_MONTH_YEAR: 376 while (end < length && buffer.charAt (end) != MONTH_FORMAT_CONSTANT) end++; 378 break; 379 case YEAR_MONTH_DAY: 380 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 getCustomShortTimeFormat () { 393 StringBuffer buffer = new StringBuffer (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 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 getLongDateFormat () { 415 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"; } 420 421 String getShortDateFormat () { 422 TCHAR tchar = new TCHAR (getCodePage (), 80); 424 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"; } 428 429 int getShortDateFormatOrdering () { 430 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 number = tchar.toString (0, size - 1); 435 return Integer.parseInt (number); 436 } 437 return 0; 438 } 439 440 String 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"; } 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 number = tchar.toString (0, size - 1); 451 return Integer.parseInt (number) != 0; 452 } 453 return true; 454 } 455 456 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 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 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 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 getNameText () { 541 return "DateTime"; 542 } 543 544 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 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 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 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 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 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 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 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 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 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: case OS.DTN_DATETIMECHANGE: 775 sendEvent (SWT.Selection); 776 break; 777 } 778 return super.wmNotifyChild (hdr, wParam, lParam); 779 } 780 } 781 | Popular Tags |