ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

dateandtime.c
Go to the documentation of this file.
00001 /*
00002  * PROJECT:     ReactOS Timedate Control Panel
00003  * LICENSE:     GPL - See COPYING in the top level directory
00004  * FILE:        dll/cpl/timedate/dateandtime.c
00005  * PURPOSE:     Date & Time property page
00006  * COPYRIGHT:   Copyright 2004-2007 Eric Kohl
00007  *              Copyright 2006 Ged Murphy <gedmurphy@gmail.com>
00008  *              Copyright 2006 Thomas Weidenmueller <w3seek@reactos.com>
00009  *
00010  */
00011 
00012 #include <timedate.h>
00013 
00014 static WNDPROC pOldWndProc = NULL;
00015 
00016 BOOL
00017 SystemSetLocalTime(LPSYSTEMTIME lpSystemTime)
00018 {
00019     HANDLE hToken;
00020     DWORD PrevSize;
00021     TOKEN_PRIVILEGES priv, previouspriv;
00022     BOOL Ret = FALSE;
00023 
00024     /*
00025      * Enable the SeSystemtimePrivilege privilege
00026      */
00027 
00028     if (OpenProcessToken(GetCurrentProcess(),
00029                          TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
00030                          &hToken))
00031     {
00032         priv.PrivilegeCount = 1;
00033         priv.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
00034 
00035         if (LookupPrivilegeValueW(NULL,
00036                                   SE_SYSTEMTIME_NAME,
00037                                   &priv.Privileges[0].Luid))
00038         {
00039             if (AdjustTokenPrivileges(hToken,
00040                                       FALSE,
00041                                       &priv,
00042                                       sizeof(previouspriv),
00043                                       &previouspriv,
00044                                       &PrevSize) &&
00045                 GetLastError() == ERROR_SUCCESS)
00046             {
00047                 /*
00048                  * We successfully enabled it, we're permitted to change the system time
00049                  * Call SetLocalTime twice to ensure correct results
00050                  */
00051                 Ret = SetLocalTime(lpSystemTime) &&
00052                       SetLocalTime(lpSystemTime);
00053 
00054                 /*
00055                  * For the sake of security, restore the previous status again
00056                  */
00057                 if (previouspriv.PrivilegeCount > 0)
00058                 {
00059                     AdjustTokenPrivileges(hToken,
00060                                           FALSE,
00061                                           &previouspriv,
00062                                           0,
00063                                           NULL,
00064                                           0);
00065                 }
00066             }
00067         }
00068         CloseHandle(hToken);
00069     }
00070 
00071     return Ret;
00072 }
00073 
00074 
00075 static VOID
00076 SetLocalSystemTime(HWND hwnd)
00077 {
00078     SYSTEMTIME Time;
00079 
00080     if (DateTime_GetSystemtime(GetDlgItem(hwnd,
00081                                           IDC_TIMEPICKER),
00082                                &Time) == GDT_VALID &&
00083         SendMessageW(GetDlgItem(hwnd,
00084                                 IDC_MONTHCALENDAR),
00085                      MCCM_GETDATE,
00086                      (WPARAM)&Time,
00087                      0))
00088     {
00089         SystemSetLocalTime(&Time);
00090 
00091         SetWindowLongPtrW(hwnd,
00092                           DWL_MSGRESULT,
00093                           PSNRET_NOERROR);
00094 
00095         SendMessageW(GetDlgItem(hwnd,
00096                                 IDC_MONTHCALENDAR),
00097                      MCCM_RESET,
00098                      (WPARAM)&Time,
00099                      0);
00100 
00101         /* Broadcast the time change message */
00102         SendMessageW(HWND_BROADCAST,
00103                      WM_TIMECHANGE,
00104                      0,
00105                      0);
00106     }
00107 }
00108 
00109 
00110 static VOID
00111 SetTimeZoneName(HWND hwnd)
00112 {
00113     TIME_ZONE_INFORMATION TimeZoneInfo;
00114     WCHAR TimeZoneString[128];
00115     WCHAR TimeZoneText[128];
00116     WCHAR TimeZoneName[128];
00117     DWORD TimeZoneId;
00118 
00119     TimeZoneId = GetTimeZoneInformation(&TimeZoneInfo);
00120 
00121     LoadStringW(hApplet, IDS_TIMEZONETEXT, TimeZoneText, 128);
00122 
00123     switch (TimeZoneId)
00124     {
00125         case TIME_ZONE_ID_STANDARD:
00126             wcscpy(TimeZoneName, TimeZoneInfo.StandardName);
00127             break;
00128 
00129         case TIME_ZONE_ID_DAYLIGHT:
00130             wcscpy(TimeZoneName, TimeZoneInfo.DaylightName);
00131             break;
00132 
00133         case TIME_ZONE_ID_UNKNOWN:
00134             LoadStringW(hApplet, IDS_TIMEZONEUNKNOWN, TimeZoneName, 128);
00135             break;
00136 
00137         case TIME_ZONE_ID_INVALID:
00138         default:
00139             LoadStringW(hApplet, IDS_TIMEZONEINVALID, TimeZoneName, 128);
00140             break;
00141     }
00142 
00143     wsprintfW(TimeZoneString, TimeZoneText, TimeZoneName);
00144     SendDlgItemMessageW(hwnd, IDC_TIMEZONE, WM_SETTEXT, 0, (LPARAM)TimeZoneString);
00145 }
00146 
00147 
00148 static VOID
00149 FillMonthsComboBox(HWND hCombo)
00150 {
00151     SYSTEMTIME LocalDate = {0};
00152     WCHAR szBuf[64];
00153     INT i;
00154     UINT Month;
00155 
00156     GetLocalTime(&LocalDate);
00157 
00158     SendMessageW(hCombo,
00159                  CB_RESETCONTENT,
00160                  0,
00161                  0);
00162 
00163     for (Month = 1;
00164          Month <= 13;
00165          Month++)
00166     {
00167         i = GetLocaleInfoW(LOCALE_USER_DEFAULT,
00168                            ((Month < 13) ? LOCALE_SMONTHNAME1 + Month - 1 : LOCALE_SMONTHNAME13),
00169                            szBuf,
00170                            sizeof(szBuf) / sizeof(szBuf[0]));
00171         if (i > 1)
00172         {
00173             i = (INT)SendMessageW(hCombo,
00174                                   CB_ADDSTRING,
00175                                   0,
00176                                   (LPARAM)szBuf);
00177             if (i != CB_ERR)
00178             {
00179                 SendMessageW(hCombo,
00180                              CB_SETITEMDATA,
00181                              (WPARAM)i,
00182                              Month);
00183 
00184                 if (Month == (UINT)LocalDate.wMonth)
00185                 {
00186                     SendMessageW(hCombo,
00187                                  CB_SETCURSEL,
00188                                  (WPARAM)i,
00189                                  0);
00190                 }
00191             }
00192         }
00193     }
00194 }
00195 
00196 
00197 static WORD
00198 GetCBSelectedMonth(HWND hCombo)
00199 {
00200     INT i;
00201     WORD Ret = (WORD)-1;
00202 
00203     i = (INT)SendMessageW(hCombo,
00204                           CB_GETCURSEL,
00205                           0,
00206                           0);
00207     if (i != CB_ERR)
00208     {
00209         i = (INT)SendMessageW(hCombo,
00210                               CB_GETITEMDATA,
00211                               (WPARAM)i,
00212                               0);
00213 
00214         if (i >= 1 && i <= 13)
00215             Ret = (WORD)i;
00216     }
00217 
00218     return Ret;
00219 }
00220 
00221 
00222 static VOID
00223 ChangeMonthCalDate(HWND hMonthCal,
00224                    WORD Day,
00225                    WORD Month,
00226                    WORD Year)
00227 {
00228     SendMessageW(hMonthCal,
00229                  MCCM_SETDATE,
00230                  MAKEWPARAM(Day,
00231                             Month),
00232                  MAKELPARAM(Year,
00233                             0));
00234 }
00235 
00236 static VOID
00237 AutoUpdateMonthCal(HWND hwndDlg,
00238                    PNMMCCAUTOUPDATE lpAutoUpdate)
00239 {
00240     UNREFERENCED_PARAMETER(lpAutoUpdate);
00241 
00242     /* Update the controls */
00243     FillMonthsComboBox(GetDlgItem(hwndDlg,
00244                                   IDC_MONTHCB));
00245 }
00246 
00247 
00248 static INT_PTR CALLBACK
00249 DTPProc(HWND hwnd,
00250         UINT uMsg,
00251         WPARAM wParam,
00252         LPARAM lParam)
00253 {
00254     switch (uMsg)
00255     {
00256         case WM_KEYDOWN:
00257             /* Stop the timer when the user is about to change the time */
00258             if ((wParam != VK_LEFT) & (wParam != VK_RIGHT))
00259                 KillTimer(GetParent(hwnd), ID_TIMER);
00260             break;
00261     }
00262 
00263     return CallWindowProcW(pOldWndProc, hwnd, uMsg, wParam, lParam);
00264 }
00265 
00266 /* Property page dialog callback */
00267 INT_PTR CALLBACK
00268 DateTimePageProc(HWND hwndDlg,
00269          UINT uMsg,
00270          WPARAM wParam,
00271          LPARAM lParam)
00272 {
00273     SYSTEMTIME st;
00274     GetLocalTime(&st);
00275 
00276     switch (uMsg)
00277     {
00278         case WM_INITDIALOG:
00279             FillMonthsComboBox(GetDlgItem(hwndDlg,
00280                                           IDC_MONTHCB));
00281 
00282             SetTimer(hwndDlg, ID_TIMER, 1000, NULL);
00283 
00284             /* Set range and current year */
00285             SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_SETRANGE, 0, MAKELONG ((short) 9999, (short) 1900));
00286             SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_SETPOS, 0, MAKELONG( (short) st.wYear, 0));
00287 
00288             pOldWndProc = (WNDPROC) SetWindowLongPtrW(GetDlgItem(hwndDlg, IDC_TIMEPICKER), GWL_WNDPROC, (INT_PTR) DTPProc);
00289             break;
00290 
00291         case WM_TIMER:
00292             SendMessageW(GetDlgItem(hwndDlg, IDC_TIMEPICKER), DTM_SETSYSTEMTIME, GDT_VALID, (LPARAM) &st);
00293             break;
00294 
00295         case WM_COMMAND:
00296             switch (LOWORD(wParam))
00297             {
00298                 case IDC_MONTHCB:
00299                     if (HIWORD(wParam) == CBN_SELCHANGE)
00300                     {
00301                         ChangeMonthCalDate(GetDlgItem(hwndDlg,
00302                                                       IDC_MONTHCALENDAR),
00303                                                       (WORD) -1,
00304                                                       GetCBSelectedMonth((HWND)lParam),
00305                                                       (WORD) -1);
00306                     }
00307                     break;
00308             }
00309             break;
00310 
00311         case WM_CTLCOLORSTATIC:
00312             if ((HWND)lParam == GetDlgItem(hwndDlg, IDC_YEARTEXT))
00313                 return (INT_PTR)GetSysColorBrush(COLOR_WINDOW);
00314             break;
00315 
00316         case WM_NOTIFY:
00317         {
00318             LPNMHDR lpnm = (LPNMHDR)lParam;
00319 
00320             switch (lpnm->idFrom)
00321             {
00322                 case IDC_YEAR:
00323                     switch (lpnm->code)
00324                     {
00325                         case UDN_DELTAPOS:
00326                         {
00327                             SHORT wYear;
00328                             LPNMUPDOWN updown = (LPNMUPDOWN)lpnm;
00329                             wYear = (SHORT)SendMessageW(GetDlgItem(hwndDlg, IDC_YEAR), UDM_GETPOS, 0, 0);
00330                             /* Enable the 'Apply' button */
00331                             PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
00332                             ChangeMonthCalDate(GetDlgItem(hwndDlg,
00333                                                IDC_MONTHCALENDAR),
00334                                                (WORD) -1,
00335                                                (WORD) -1,
00336                                                (WORD) (wYear + updown->iDelta));
00337                         }
00338                         break;
00339                     }
00340                     break;
00341 
00342                 case IDC_TIMEPICKER:
00343                     switch (lpnm->code)
00344                     {
00345                         case DTN_DATETIMECHANGE:
00346                             /* Stop the timer */
00347                             KillTimer(hwndDlg, ID_TIMER);
00348 
00349                             /* Tell the clock to stop ticking */
00350                             SendDlgItemMessageW(hwndDlg, IDC_CLOCKWND, CLM_STOPCLOCK,
00351                                                 0, 0);
00352 
00353                             /* Enable the 'Apply' button */
00354                             PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
00355                             break;
00356                     }
00357                     break;
00358 
00359                 case IDC_MONTHCALENDAR:
00360                     switch (lpnm->code)
00361                     {
00362                         case MCCN_SELCHANGE:
00363                             /* Enable the 'Apply' button */
00364                             PropSheet_Changed(GetParent(hwndDlg), hwndDlg);
00365                             break;
00366 
00367                         case MCCN_AUTOUPDATE:
00368                             AutoUpdateMonthCal(hwndDlg,
00369                                                  (PNMMCCAUTOUPDATE)lpnm);
00370                             break;
00371                     }
00372                     break;
00373 
00374                 default:
00375                     switch (lpnm->code)
00376                     {
00377                         case PSN_SETACTIVE:
00378                             SetTimeZoneName(hwndDlg);
00379                             break;
00380 
00381                         case PSN_APPLY:
00382                             SetLocalSystemTime(hwndDlg);
00383                             SetTimer(hwndDlg, ID_TIMER, 1000, NULL);
00384 
00385                             /* Tell the clock to start ticking */
00386                             SendDlgItemMessageW(hwndDlg, IDC_CLOCKWND, CLM_STARTCLOCK,
00387                                                 0, 0);
00388                             return TRUE;
00389                     }
00390                     break;
00391             }
00392         }
00393         break;
00394 
00395         case WM_TIMECHANGE:
00396             /* FIXME: We don't get this message as we're not a top-level window... */
00397             SendMessageW(GetDlgItem(hwndDlg,
00398                                     IDC_MONTHCALENDAR),
00399                          MCCM_RESET,
00400                          0,
00401                          0);
00402             break;
00403 
00404         case WM_DESTROY:
00405             KillTimer(hwndDlg, ID_TIMER);
00406             break;
00407     }
00408 
00409     return FALSE;
00410 }

Generated on Thu May 24 2012 04:21:24 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.