1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
#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()
{
static const TCHAR facename[] = _T("Tahoma");
if (!hBigFont)
{
LOGFONT logfont = {0};
_tcscpy(logfont.lfFaceName, facename);
logfont.lfWeight = FW_NORMAL;
logfont.lfHeight = -40;
hBigFont = CreateFontIndirect(&logfont);
}
if (!hNormalFont)
{
LOGFONT logfont = {0};
_tcscpy(logfont.lfFaceName, 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;
TCHAR time_buff[32];
TCHAR date_buff[128];
TCHAR 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, (int)_tcslen(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, (int)_tcslen(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)) {
TCHAR buff[255 + 32];
_tcscpy(buff, wd->nick_buff);
_tcscat(buff, _T(" - "));
_tcscat(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(!DBGetContactSettingTString(wd->hContact, PROTO, "Nick", &dbv)) {
_tcsncpy(wd->nick_buff, dbv.ptszVal, 255);
SetWindowText(hwnd, dbv.ptszVal);
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 && strcmp( 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();
HANDLE hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDFIRST, 0, 0 );
while ( hContact != NULL )
{
char* proto = (char *)CallService(MS_PROTO_GETCONTACTBASEPROTO, (WPARAM)hContact, 0);
if (proto && strcmp( PROTO, proto) == 0)
{
if (DBGetContactSettingByte(hContact, PROTO, "WindowWasOpen", 0) != 0)
{
show_time(hContact);
DBWriteContactSettingByte(hContact, PROTO, "WindowWasOpen", 0);
}
DBVARIANT dbv;
if (!DBGetContactSettingTString(hContact, PROTO, "TZName", &dbv))
{
int list_index = DBGetContactSettingDword(hContact, PROTO, "TimezoneListIndex", -1);
if (list_index < 0 || list_index >= timezone_list.getCount())
list_index = 0;
if (_tcscmp(timezone_list[list_index].tcName, dbv.ptszVal))
{
for (int j = 0; j < timezone_list.getCount(); ++j)
{
if (!_tcscmp(timezone_list[j].tcName, dbv.ptszVal))
{
list_index = j;
break;
}
}
DBWriteContactSettingDword(hContact, PROTO, "TimezoneListIndex", list_index);
}
DBFreeVariant(&dbv);
}
}
hContact = ( HANDLE )CallService( MS_DB_CONTACT_FINDNEXT,( WPARAM )hContact, 0 );
}
}
|