summaryrefslogtreecommitdiff
path: root/worldtime_protocol/time_display.cpp
diff options
context:
space:
mode:
authorsje <sje@4f64403b-2f21-0410-a795-97e2b3489a10>2006-11-01 14:57:42 +0000
committersje <sje@4f64403b-2f21-0410-a795-97e2b3489a10>2006-11-01 14:57:42 +0000
commitd513db102812c5dd83f94084e979bb7e24345498 (patch)
tree29f599f98d2d5fb01d0e1dcb58077459ea609961 /worldtime_protocol/time_display.cpp
parent610f48f080c6ae8c298a032cd92bb2ca9948aded (diff)
git-svn-id: https://server.scottellis.com.au/svn/mim_plugs@24 4f64403b-2f21-0410-a795-97e2b3489a10
Diffstat (limited to 'worldtime_protocol/time_display.cpp')
-rw-r--r--worldtime_protocol/time_display.cpp221
1 files changed, 221 insertions, 0 deletions
diff --git a/worldtime_protocol/time_display.cpp b/worldtime_protocol/time_display.cpp
new file mode 100644
index 0000000..2489c19
--- /dev/null
+++ b/worldtime_protocol/time_display.cpp
@@ -0,0 +1,221 @@
+#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;
+
+BOOL 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 *)GetWindowLong(hwndDlg, GWL_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 *)GetWindowLong(hwndDlg, GWL_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 *)GetWindowLong(hwndDlg, GWL_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);
+ SetWindowLong(hwnd, GWL_USERDATA, (LONG)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 );
+ }
+}