#include "common.h" #include "time_display.h" #define ID_UPDATE_TIMER 101101 #define UPDATE_TIMER 250 #define WM_UPDATE_DATA (WM_USER + 10) extern HINSTANCE hInst; HFONT hBigFont = 0, hNormalFont = 0; void LoadFonts() { if(!hBigFont) { char facename[]="Tahoma"; LOGFONT logfont = {0}; memcpy(logfont.lfFaceName,facename,sizeof(facename)); logfont.lfWeight=FW_NORMAL; logfont.lfHeight=-40; hBigFont=CreateFontIndirect(&logfont); } if(!hNormalFont) { char facename[]="Tahoma"; LOGFONT logfont = {0}; memcpy(logfont.lfFaceName,facename,sizeof(facename)); logfont.lfWeight=FW_NORMAL; logfont.lfHeight=-12; hNormalFont=CreateFontIndirect(&logfont); } } void UnloadFonts() { if(hBigFont) { DeleteObject(hBigFont); hBigFont = 0; } if(hNormalFont) { DeleteObject(hNormalFont); hNormalFont = 0; } } typedef struct WindowData_tag { HANDLE hContact; int timezone_list_index; char time_buff[32]; char date_buff[128]; char nick_buff[256]; TIME_ZONE_INFORMATION tzi; } WindowData; INT_PTR CALLBACK DlgProcDisplay(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) { RECT r; HDC hdc; PAINTSTRUCT ps; HFONT oldFont; switch ( msg ) { case WM_INITDIALOG: TranslateDialogDefault( hwndDlg ); SetTimer(hwndDlg, ID_UPDATE_TIMER, UPDATE_TIMER, 0); return TRUE; case WM_ERASEBKGND: return TRUE; case WM_PAINT: if(GetUpdateRect(hwndDlg, &r, FALSE)) { hdc = BeginPaint(hwndDlg, &ps); GetClientRect(hwndDlg, &r); FillRect(hdc, &r, GetSysColorBrush(COLOR_WINDOW)); WindowData *wd = (WindowData *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA); if(wd) { RECT br = r, lr = r; br.bottom -= (r.bottom - r.top) / 4; oldFont = (HFONT)SelectObject(hdc, hBigFont); DrawText(hdc, wd->time_buff, strlen(wd->time_buff), &br, DT_SINGLELINE | DT_VCENTER | DT_CENTER); SelectObject(hdc, hNormalFont); lr.top = (r.bottom - r.top) * 3 / 4; DrawText(hdc, wd->date_buff, strlen(wd->date_buff), &lr, DT_SINGLELINE | DT_VCENTER | DT_CENTER); SelectObject(hdc, oldFont); } EndPaint(hwndDlg, &ps); } return TRUE; case WM_TIMER: SendMessage(hwndDlg, WM_UPDATE_DATA, 0, 0); return TRUE; case WM_UPDATE_DATA: { SYSTEMTIME st, other_st; WindowData *wd = (WindowData *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA); MyGetSystemTime(&st); MySystemTimeToTzSpecificLocalTime(&wd->tzi, &st, &other_st); //GetTimeFormat(LOCALE_USER_DEFAULT, TIME_NOSECONDS, &other_st, 0, wd->time_buff, 32); GetTimeFormat(LOCALE_USER_DEFAULT, 0, &other_st, 0, wd->time_buff, 32); GetDateFormat(LOCALE_USER_DEFAULT, DATE_LONGDATE, &other_st, 0, wd->date_buff, 128); if(IsIconic(hwndDlg)) { char buff[255 + 32]; strcpy(buff, wd->nick_buff); strcat(buff, " - "); strcat(buff, wd->time_buff); SetWindowText(hwndDlg, buff); } else SetWindowText(hwndDlg, wd->nick_buff); } InvalidateRect(hwndDlg, 0, FALSE); return TRUE; case WM_CLOSE: DestroyWindow(hwndDlg); return TRUE; case WM_DESTROY: { KillTimer(hwndDlg, ID_UPDATE_TIMER); WindowData *wd = (WindowData *)GetWindowLongPtr(hwndDlg, GWLP_USERDATA); HANDLE hContact = wd->hContact; if(CallService(MS_DB_CONTACT_IS, (WPARAM)hContact, 0)) Utils_SaveWindowPosition(hwndDlg, hContact, PROTO, "timewnd"); DBDeleteContactSetting(hContact, PROTO, "WindowHandle"); delete wd; } break; } return FALSE; } int show_time(HANDLE hContact) { HWND hwnd = (HWND)DBGetContactSettingDword(hContact, PROTO, "WindowHandle", 0); if(hwnd) { ShowWindow(hwnd, SW_SHOW); SetWindowPos(hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); return 0; } hwnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DISPLAY), GetDesktopWindow(), DlgProcDisplay); WindowData *wd = new WindowData; wd->hContact = hContact; wd->timezone_list_index = DBGetContactSettingDword(hContact, PROTO, "TimezoneListIndex", -1); wd->time_buff[0] = 0; wd->date_buff[0] = 0; wd->tzi.Bias = timezone_list[wd->timezone_list_index].TZI.Bias; wd->tzi.DaylightBias = timezone_list[wd->timezone_list_index].TZI.DaylightBias; wd->tzi.DaylightDate = timezone_list[wd->timezone_list_index].TZI.DaylightDate; wd->tzi.StandardBias = timezone_list[wd->timezone_list_index].TZI.StandardBias; wd->tzi.StandardDate = timezone_list[wd->timezone_list_index].TZI.StandardDate; DBVARIANT dbv; if(!DBGetContactSetting(wd->hContact, PROTO, "Nick", &dbv)) { strncpy(wd->nick_buff, dbv.pszVal, 255); SetWindowText(hwnd, dbv.pszVal); DBFreeVariant(&dbv); } DBWriteContactSettingDword(hContact, PROTO, "WindowHandle", (DWORD)hwnd); SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR)wd); SendMessage(hwnd, WM_UPDATE_DATA, 0, 0); if(CallService(MS_DB_CONTACT_IS, (WPARAM)hContact, 0)) Utils_RestoreWindowPosition(hwnd, hContact, PROTO, "timewnd"); if(!IsWindowVisible(hwnd)) { ShowWindow(hwnd, SW_SHOW); SendMessage(hwnd, WM_UPDATE_DATA, 0, 0); } return 0; } // save window positions, close windows void time_windows_cleanup() { HWND hwnd; char *proto; HANDLE hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDFIRST, 0, 0 ); while ( hContact != NULL ) { proto = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0); if (proto && lstrcmp( PROTO, proto) == 0) { if((hwnd = (HWND)DBGetContactSettingDword(hContact, PROTO, "WindowHandle", 0)) != 0) { DestroyWindow(hwnd); DBWriteContactSettingByte(hContact, PROTO, "WindowWasOpen", 1); } } hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDNEXT,( WPARAM )hContact, 0 ); } UnloadFonts(); } // restore windows that were open when cleanup was called last void time_windows_init() { LoadFonts(); char *proto; HANDLE hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDFIRST, 0, 0 ); while ( hContact != NULL ) { proto = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0); if (proto && lstrcmp( PROTO, proto) == 0) { if(DBGetContactSettingByte(hContact, PROTO, "WindowWasOpen", 0) != 0) { show_time(hContact); DBWriteContactSettingByte(hContact, PROTO, "WindowWasOpen", 0); } } hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDNEXT,( WPARAM )hContact, 0 ); } }