summaryrefslogtreecommitdiff
path: root/plugins/Alarms/src/frame.cpp
blob: 6ebb459026ad4263d984b4ea7e985b67b90c1a84 (plain)
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#include "common.h"
#include "frame.h"

HWND hwnd_plugin = 0;
HWND hwnd_frame = 0;
HWND hwnd_list = 0;

int frame_id = -1;

FontID font_id;
ColourID framebk_colour_id;
HFONT hFont = 0;
COLORREF fontColour, framebk;
HBRUSH bk_brush = 0;

AlarmList alarm_list;
CRITICAL_SECTION list_cs;

HANDLE hMenuShowReminders = 0;

#define ID_FRAME_UPDATE_TIMER						1011
#define ID_FRAME_SHOWHIDE_TIMER						1012

#define WMU_FILL_LIST		(WM_USER + 10)
#define WMU_SIZE_LIST		(WM_USER + 11)
#define WMU_INITIALIZE		(WM_USER + 12)

void FixMainMenu();

int height_client_to_frame(int client_height, LONG style, LONG ex_style) {
	RECT tr;
	tr.top = tr.right = tr.left = 0;
	tr.bottom = client_height;
	if (AdjustWindowRectEx(&tr, style, FALSE, ex_style))
		return tr.bottom - tr.top;
	return 0;
}

LRESULT CALLBACK FrameContainerWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	switch(msg) {
		case WM_CREATE:
			return TRUE;
		case WM_SHOWWINDOW:
			if ((BOOL)wParam) {
				DBWriteContactSettingByte(0, MODULE, "ReminderFrameVisible", 1);
				Utils_RestoreWindowPosition(hwnd, 0, MODULE, "reminders_window");
				PostMessage(hwnd, WM_SIZE, 0, 0);
			} else {
				DBWriteContactSettingByte(0, MODULE, "ReminderFrameVisible", 0);
				Utils_SaveWindowPosition(hwnd, 0, MODULE, "reminders_window");
			}
			break;
		case WM_SIZE:
			{
				HWND child = (HWND)GetWindowLongPtr(hwnd, GWLP_USERDATA);
				RECT r;
				GetClientRect(hwnd, &r);

				SetWindowPos(child, 0, r.left, r.top, r.right - r.left, r.bottom - r.top, SWP_NOZORDER | SWP_NOACTIVATE);
				InvalidateRect(child, 0, TRUE);
				InvalidateRect(hwnd, 0, TRUE);
			}
			break;

		case WM_CLOSE:
			Utils_SaveWindowPosition(hwnd, 0, MODULE, "reminders_window");
			ShowWindow(hwnd, SW_HIDE);
			FixMainMenu();
			return TRUE;
	}

	return DefWindowProc(hwnd, msg, wParam, lParam);
}

bool FrameIsFloating() {
	if (frame_id == -1) 
		return true; // no frames, always floating
	
	return (CallService(MS_CLIST_FRAMES_GETFRAMEOPTIONS, MAKEWPARAM(FO_FLOATING, frame_id), 0) != 0);
}

ALARM context_menu_alarm = {0};

LRESULT CALLBACK FrameWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
	MEASUREITEMSTRUCT *mis;
	DRAWITEMSTRUCT *dis;
	SIZE textSize;
	SIZE timeSize;

	switch(msg) {

		case WM_CREATE: 
			{
				hwnd_list = CreateWindow(_T("LISTBOX"), _T(""),
					(WS_VISIBLE | WS_CHILD | LBS_NOINTEGRALHEIGHT | LBS_STANDARD | LBS_NOTIFY | LBS_OWNERDRAWFIXED) & ~LBS_SORT
					& ~WS_BORDER, 0, 0, 0, 0, hwnd, NULL, hInst,0);		
			}
			return FALSE;
		case WMU_INITIALIZE:
			PostMessage(hwnd, WMU_FILL_LIST, 0, 0);
			SetTimer(hwnd, ID_FRAME_UPDATE_TIMER, 5000, 0);
			SetTimer(hwnd, ID_FRAME_SHOWHIDE_TIMER, 200, 0);
			return TRUE;

		case WM_MEASUREITEM:
			mis = (MEASUREITEMSTRUCT *)lParam;
			mis->itemHeight = options.row_height;
			return TRUE;

		case WM_DRAWITEM:
			dis = (DRAWITEMSTRUCT *)lParam;
			if (dis->itemID != (DWORD)-1) {
				ALARM alarm = {0};
				EnterCriticalSection(&list_cs);
				ALARM &list_alarm = alarm_list.at(dis->itemData);
				copy_alarm_data(&alarm, &list_alarm);
				LeaveCriticalSection(&list_cs);

				RECT r;
				GetClientRect(hwnd, &r);

				int min = MinutesInFuture(alarm.time, alarm.occurrence);

				FillRect(dis->hDC, &dis->rcItem, bk_brush);

				dis->rcItem.left += options.indent;

				SetBkMode(dis->hDC, TRANSPARENT);
				SetTextColor(dis->hDC, fontColour);

				HICON hIcon = (min <= 5 ? hIconList2 : hIconList1);
				//DrawIconEx(dis->hDC,dis->rcItem.left,(dis->rcItem.top + dis->rcItem.bottom -GetSystemMetrics(SM_CYSMICON))>>1,hIcon,GetSystemMetrics(SM_CXSMICON),GetSystemMetrics(SM_CYSMICON),0,NULL,DI_NORMAL);
				DrawIconEx(dis->hDC,dis->rcItem.left,(dis->rcItem.top + dis->rcItem.bottom - 16)>>1,hIcon,0, 0, 0, NULL, DI_NORMAL);

				GetTextExtentPoint32(dis->hDC,alarm.szTitle,lstrlen(alarm.szTitle),&textSize);

				TCHAR buff[100];
				if (min >= 60) {
					mir_sntprintf(buff, 100, TranslateT("%dh %dm"), min / 60, min % 60);
				} else {
					mir_sntprintf(buff, 100, TranslateT("%dm"), min);
				}

				GetTextExtentPoint32(dis->hDC,buff,lstrlen(buff),&timeSize);

				if (textSize.cx > (dis->rcItem.right - dis->rcItem.left) - (GetSystemMetrics(SM_CXSMICON) + 4) - timeSize.cx - 2 - 4) {
					// need elipsis
					TCHAR titlebuff[512];
					int len = lstrlen(alarm.szTitle);
					if (len > 511) len = 511;
					while(len > 0 && textSize.cx > (dis->rcItem.right - dis->rcItem.left) - (GetSystemMetrics(SM_CXSMICON) + 4) - timeSize.cx - 2 - 4) {
						len--;
						_tcsncpy(titlebuff, alarm.szTitle, len);
						titlebuff[len] = 0;
						_tcscat(titlebuff, _T("..."));
						GetTextExtentPoint32(dis->hDC,titlebuff,lstrlen(titlebuff),&textSize);
					}
					TextOut(dis->hDC,dis->rcItem.left + 16 + 4,(dis->rcItem.top + dis->rcItem.bottom - textSize.cy)>>1,titlebuff,lstrlen(titlebuff));
					TextOut(dis->hDC,dis->rcItem.right - timeSize.cx - 2,(dis->rcItem.top + dis->rcItem.bottom - timeSize.cy)>>1, buff,lstrlen(buff));
				} else {				
					TextOut(dis->hDC,dis->rcItem.left + 16 + 4,(dis->rcItem.top + dis->rcItem.bottom - textSize.cy)>>1,alarm.szTitle,lstrlen(alarm.szTitle));
					TextOut(dis->hDC,dis->rcItem.right - timeSize.cx - 2,(dis->rcItem.top + dis->rcItem.bottom - timeSize.cy)>>1, buff,lstrlen(buff));
				}

				SetBkMode(dis->hDC, OPAQUE);

				free_alarm_data(&alarm);
			} else {				
				FillRect(dis->hDC, &dis->rcItem, bk_brush);
			}
			return TRUE;

		case WM_CTLCOLORLISTBOX:
			break;
		case WM_ERASEBKGND:
			{
				HDC hdc = (HDC)wParam;
				RECT r;
				GetClientRect(hwnd, &r);
				FillRect(hdc, &r, bk_brush);
			}
			return TRUE;

		case WM_PRINTCLIENT:
			{
				/*
				HDC hdc = (HDC)wParam;
				RECT r;
				GetClientRect(hwnd, &r);
				*/
			}
			return TRUE;

		case WM_PAINT:
			{
				RECT r;
				if (GetUpdateRect(hwnd, &r, FALSE)) {
					PAINTSTRUCT ps;
					HDC hdc = BeginPaint(hwnd, &ps);
					SendMessage(hwnd, WM_PRINTCLIENT, (WPARAM)hdc, (LPARAM)(PRF_CLIENT | PRF_CHILDREN));
					EndPaint(hwnd, &ps);
				}
			}
			return TRUE;

		case WM_SHOWWINDOW:
			if ((BOOL)wParam) PostMessage(hwnd, WMU_SIZE_LIST, 0, 0);
			break;

		case WM_SIZE:
			if (IsWindowVisible(hwnd)) {
				SendMessage(hwnd, WMU_SIZE_LIST, 0, 0);
			
				if (frame_id != -1) {
					//CallService(MS_CLIST_FRAMES_UPDATEFRAME, (WPARAM)frame_id, (LPARAM)(FU_TBREDRAW | FU_FMREDRAW));
					CallService(MS_CLIST_FRAMES_UPDATEFRAME, (WPARAM)frame_id, (LPARAM)FU_TBREDRAW);
				}
				InvalidateRect(hwnd, 0, TRUE);
			}
			break;

		case WMU_SIZE_LIST:
			{
				if (SendMessage(hwnd_list, LB_GETITEMHEIGHT, 0, 0) != options.row_height)
					SendMessage(hwnd_list, LB_SETITEMHEIGHT, 0, options.row_height);

				int itemheight = SendMessage(hwnd_list, LB_GETITEMHEIGHT, 0, 0),
					count = SendMessage(hwnd_list, LB_GETCOUNT, 0, 0);
				
				if (options.auto_size_vert && IsWindowVisible(hwnd)) {
					if (FrameIsFloating()) {
						
#define CLUIFrameTitleBarClassName				"CLUIFrameTitleBar"
						
						int height = height_client_to_frame(itemheight * count, GetWindowLongPtr(GetParent(hwnd), GWL_STYLE), GetWindowLongPtr(GetParent(hwnd), GWL_EXSTYLE));
						HWND titleBarHwnd = FindWindowEx(GetParent(hwnd), 0, _T(CLUIFrameTitleBarClassName), 0);
						if (titleBarHwnd) {
							RECT tbr;
							GetWindowRect(titleBarHwnd, &tbr);
							height += (tbr.bottom - tbr.top);
						}
						RECT rp_window;
						GetWindowRect(GetParent(hwnd), &rp_window);
						SetWindowPos(GetParent(hwnd), 0, 0, 0, rp_window.right - rp_window.left, height, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);

						/*
						RECT rp_client, rp_window, r_window;
						GetClientRect(GetParent(hwnd), &rp_client);
						GetWindowRect(GetParent(hwnd), &rp_window);
						GetWindowRect(hwnd, &r_window);
						int diff = (rp_window.bottom - rp_window.top) - (rp_client.bottom - rp_client.top);
						if (ServiceExists(MS_CLIST_FRAMES_ADDFRAME))
							diff += rp_window.top - r_window.top;
						SetWindowPos(GetParent(hwnd), 0, 0, 0, rp_window.right - rp_window.left, count * itemheight + diff, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
						*/
					} else if (ServiceExists(MS_CLIST_FRAMES_ADDFRAME) && frame_id != -1) {
						int flags = CallService(MS_CLIST_FRAMES_GETFRAMEOPTIONS, MAKEWPARAM(FO_FLAGS, frame_id), 0);
						CallService(MS_CLIST_FRAMES_SETFRAMEOPTIONS, MAKEWPARAM(FO_HEIGHT, frame_id), (LPARAM)(count * itemheight));
						CallService(MS_CLIST_FRAMES_UPDATEFRAME, (WPARAM)frame_id, (LPARAM)(FU_TBREDRAW | FU_FMREDRAW | FU_FMPOS));
					}
				}

				RECT r, r2;
				GetClientRect(hwnd, &r);
				GetClientRect(hwnd_list, &r2);
				int width, height, winheight;

				width = r.right - r.left;
				winheight = r.bottom - r.top;

				height = min(count * itemheight, winheight - (winheight % itemheight));
				if (r2.right - r2.left != width || (r.bottom - r.top > 0 && r2.bottom - r2.top != height)) {
					SetWindowPos(hwnd_list, 0, 0, 0, width, height, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
					InvalidateRect(hwnd_list, 0, FALSE);
				}
				
				if (options.auto_showhide) {
					if (ServiceExists(MS_CLIST_FRAMES_SHFRAME) && frame_id != -1) {
						if (IsWindowVisible(hwnd) && count == 0) {
							CallService(MS_CLIST_FRAMES_SHFRAME, (WPARAM)frame_id, 0);
						} else if (!IsWindowVisible(hwnd) && count > 0) {
							// we have reminders - show if not linked to clist or if clist is visible
							if ((!options.hide_with_clist && FrameIsFloating()) || IsWindowVisible((HWND)CallService(MS_CLUI_GETHWND, 0, 0))) {
								CallService(MS_CLIST_FRAMES_SHFRAME, (WPARAM)frame_id, 0);						
								CallService(MS_CLIST_FRAMES_UPDATEFRAME, (WPARAM)frame_id, (LPARAM)(FU_FMREDRAW | FU_FMPOS));
							}
						}
					} else {
						if (IsWindowVisible(hwnd) && count == 0)
							SetReminderFrameVisible(false);
						else if (!IsWindowVisible(hwnd) && count > 0)
							// we have reminders - show if not linked to clist or if clist is visible
							if (!options.hide_with_clist || IsWindowVisible((HWND)CallService(MS_CLUI_GETHWND, 0, 0)))
								SetReminderFrameVisible(true);
					}
				}
				
			}
			return TRUE;
		case WMU_FILL_LIST:
			{
				// calculate the period to display alarms for
				SYSTEMTIME t1, t2;
				GetLocalTime(&t1);
				TimeForMinutesInFuture(60 * options.reminder_period, &t2);

				int sel = SendMessage(hwnd_list, LB_GETCURSEL, 0, 0),
					top = SendMessage(hwnd_list, LB_GETTOPINDEX, 0, 0);

				SendMessage(hwnd_list, WM_SETREDRAW, (WPARAM)FALSE, 0);

				EnterCriticalSection(&list_cs);
				SendMessage(hwnd_list, LB_RESETCONTENT, 0, 0);
				copy_list(alarm_list, t1, t2);
				alarm_list.sort();
				int index = 0;
				ALARM *i;
				for(alarm_list.reset(); i = alarm_list.current(); alarm_list.next(), index++) {
					if (i->flags & (ALF_HIDDEN | ALF_SUSPENDED | ALF_NOREMINDER))
						continue;
					SendMessage(hwnd_list, LB_ADDSTRING, 0, (LPARAM)index);
				}
				LeaveCriticalSection(&list_cs);

				SendMessage(hwnd, WMU_SIZE_LIST, 0, 0);
				SendMessage(hwnd_list, WM_SETREDRAW, (WPARAM)TRUE, 0);

				if (sel != LB_ERR && sel < index) SendMessage(hwnd_list, LB_SETCURSEL, (WPARAM)sel, 0);
				if (top != LB_ERR && top < index) SendMessage(hwnd_list, LB_SETTOPINDEX, (WPARAM)top, 0);

			}
			return TRUE;
		
		case WM_TIMER:
			if (wParam == ID_FRAME_UPDATE_TIMER)
				SendMessage(hwnd, WMU_FILL_LIST, 0, 0);
			else if (wParam == ID_FRAME_SHOWHIDE_TIMER && options.hide_with_clist) { // link show/hide with clist
				// hide if we're visible and clist isn't (possible only when floating if frames are present)
				if (!IsWindowVisible((HWND)CallService(MS_CLUI_GETHWND, 0, 0)) && IsWindowVisible(hwnd)) {
					if (ServiceExists(MS_CLIST_FRAMES_SHFRAME))
						CallService(MS_CLIST_FRAMES_SHFRAME, (WPARAM)frame_id, 0);
					else
						SetReminderFrameVisible(false);
				}
				// we're not visible but clist is - show depending on hide_with_clist and auto_showhide options
				if (!IsWindowVisible(hwnd) && IsWindowVisible((HWND)CallService(MS_CLUI_GETHWND, 0, 0))) {
					// if not auto show/hide, show (reminders or not) if we're not visible and the clist is
					// otherwise, show only if there are reminders
					int count = SendMessage(hwnd_list, LB_GETCOUNT, 0, 0);
					if (!options.auto_showhide || count > 0) {
						if (ServiceExists(MS_CLIST_FRAMES_SHFRAME))
							CallService(MS_CLIST_FRAMES_SHFRAME, (WPARAM)frame_id, 0);
						else
							SetReminderFrameVisible(true);
					}
				}
			}
			return TRUE;

		case WM_CONTEXTMENU:
			{
				/*
				*/
				POINT pt;
				GetCursorPos(&pt);
				ScreenToClient(hwnd_list, &pt);

				EnterCriticalSection(&list_cs);
				DWORD item = SendMessage(hwnd_list, LB_ITEMFROMPOINT, 0, MAKELPARAM(pt.x, pt.y));

				HMENU menu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MENU1)), submenu = GetSubMenu(menu, 0);
				TranslateMenu(submenu);
	
				if (HIWORD(item) == 0) {
					int sel = LOWORD(item);
					if (sel >= 0) {
						// one-off alarms can't be suspended
						int index = SendMessage(hwnd_list, LB_GETITEMDATA, (WPARAM)sel, 0);
						ALARM &list_alarm = alarm_list.at(index);
						copy_alarm_data(&context_menu_alarm, &list_alarm);
						if (context_menu_alarm.occurrence == OC_ONCE)
							EnableMenuItem(submenu, ID_REMINDERFRAMECONTEXT_SUSPEND, MF_BYCOMMAND | MF_GRAYED);

					}
				} else {
					EnableMenuItem(submenu, ID_REMINDERFRAMECONTEXT_SUSPEND, MF_BYCOMMAND | MF_GRAYED);
					EnableMenuItem(submenu, ID_REMINDERFRAMECONTEXT_EDIT, MF_BYCOMMAND | MF_GRAYED);
					EnableMenuItem(submenu, ID_REMINDERFRAMECONTEXT_DELETE, MF_BYCOMMAND | MF_GRAYED);
				}
				LeaveCriticalSection(&list_cs);

				//ClientToScreen(hwnd_list, &pt);
				GetCursorPos(&pt);
				
				BOOL ret = TrackPopupMenu(submenu, TPM_TOPALIGN|TPM_LEFTALIGN|TPM_RIGHTBUTTON|TPM_RETURNCMD, pt.x, pt.y, 0, hwnd, NULL);
				DestroyMenu(menu);
				if (ret) PostMessage(hwnd, WM_COMMAND, ret, 0);
			}
			return TRUE;

		case WM_COMMAND:
			switch(LOWORD(wParam)) {
				case ID_REMINDERFRAMECONTEXT_OPTIONS:
					{
						OPENOPTIONSDIALOG oop;
						oop.cbSize = sizeof(oop);
						oop.pszGroup = Translate("Events");
						oop.pszPage = Translate("Alarms");
						oop.pszTab = 0;
						CallService(MS_OPT_OPENOPTIONS, 0, (LPARAM)&oop);
					}
					break;
				case ID_REMINDERFRAMECONTEXT_SUSPEND:
					if (context_menu_alarm.occurrence != OC_ONCE) {
						suspend(context_menu_alarm.id);
						PostMessage(hwnd, WMU_FILL_LIST, 0, 0);
						if (hwndOptionsDialog) {
							// refresh options list
							PostMessage(hwndOptionsDialog, WMU_INITOPTLIST, 0, 0);
						}
					}
					break;
				case ID_REMINDERFRAMECONTEXT_EDIT:
					EditNonModal(context_menu_alarm);
					break;
				case ID_REMINDERFRAMECONTEXT_DELETE:
					remove(context_menu_alarm.id);
					PostMessage(hwnd, WMU_FILL_LIST, 0, 0);
					if (hwndOptionsDialog) {
						// refresh options list
						PostMessage(hwndOptionsDialog, WMU_INITOPTLIST, 0, 0);
					}
					break;

				case ID_REMINDERFRAMECONTEXT_NEWALARM:
					NewAlarmMenuFunc(0, 0);
					break;
			}			
			return TRUE;

		case WM_DESTROY:
			KillTimer(hwnd, ID_FRAME_UPDATE_TIMER);
			KillTimer(hwnd, ID_FRAME_SHOWHIDE_TIMER);
			free_alarm_data(&context_menu_alarm);
			break;
	}

	return DefWindowProc(hwnd, msg, wParam, lParam);
}

int ReloadFont(WPARAM wParam, LPARAM lParam)
{
	DeleteObject(hFont);

	LOGFONT log_font;
	fontColour = CallService(MS_FONT_GET, (WPARAM)&font_id, (LPARAM)&log_font);
	hFont = CreateFontIndirect(&log_font);
	SendMessage(hwnd_list, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE);

	DeleteObject(bk_brush);
	bk_brush = CreateSolidBrush(DBGetContactSettingDword(0, "Alarm", "clFrameBack", GetSysColor(COLOR_3DFACE)));
	RefreshReminderFrame();

	return 0;
}

void FixMainMenu()
{
	CLISTMENUITEM mi = {0};
	mi.cbSize = sizeof(CLISTMENUITEM);
	if (!ServiceExists(MS_CLIST_FRAMES_ADDFRAME)) {
		if (options.hide_with_clist || options.auto_showhide)
			mi.flags = CMIM_FLAGS | CMIF_GRAYED;
		else {
			mi.flags = CMIM_NAME | CMIM_FLAGS;

			if (ReminderFrameVisible())
				mi.pszName = Translate("Hide Reminders");
			else
				mi.pszName = Translate("Show Reminders");
		}
	}
	CallService(MS_CLIST_MODIFYMENUITEM, (WPARAM)hMenuShowReminders, (LPARAM)&mi);
}

/////////////////////////
// only used when no multiwindow functionality is available
bool ReminderFrameVisible()
{
	return IsWindowVisible(hwnd_frame) ? true : false;
}

void SetReminderFrameVisible(bool visible)
{
	if (frame_id == -1 && hwnd_frame != 0) 
		ShowWindow(hwnd_frame, visible ? SW_SHOW : SW_HIDE);
}

INT_PTR ShowHideMenuFunc(WPARAM wParam, LPARAM lParam)
{
	if (ReminderFrameVisible())
		SendMessage(hwnd_frame, WM_CLOSE, 0, 0);
	else
		ShowWindow(hwnd_frame, SW_SHOW);

	FixMainMenu();
	return 0;
}
//////////////////////////////

int CreateFrame() 
{
	WNDCLASS wndclass;
	wndclass.style         = 0;
	wndclass.lpfnWndProc   = FrameWindowProc;
	wndclass.cbClsExtra    = 0;
	wndclass.cbWndExtra    = 0;
	wndclass.hInstance     = hInst;
	wndclass.hIcon         = NULL;
	wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
	wndclass.hbrBackground = 0; //(HBRUSH)(COLOR_3DFACE+1);
	wndclass.lpszMenuName  = NULL;
	wndclass.lpszClassName = _T("AlarmsFrame");
	RegisterClass(&wndclass);

	if (ServiceExists(MS_CLIST_FRAMES_ADDFRAME)) {

		hwnd_plugin = CreateWindow(_T("AlarmsFrame"), TranslateT("Alarms"), 
			WS_CHILD | WS_CLIPCHILDREN, 
			0,0,10,10, (HWND)CallService(MS_CLUI_GETHWND, 0, 0), NULL,hInst,NULL);

		CLISTFrame Frame = {0};
		
		Frame.cbSize=sizeof(CLISTFrame);
		Frame.name = Frame.TBname = Translate("Alarms");
		Frame.hWnd=hwnd_plugin;
		Frame.align=alBottom;
		Frame.Flags=F_VISIBLE|F_SHOWTB|F_SHOWTBTIP;
		Frame.height=30;

		frame_id=CallService(MS_CLIST_FRAMES_ADDFRAME,(WPARAM)&Frame,0);
	} else {
		wndclass.style         = 0;//CS_HREDRAW | CS_VREDRAW;
		wndclass.lpfnWndProc   = FrameContainerWindowProc;
		wndclass.cbClsExtra    = 0;
		wndclass.cbWndExtra    = 0;
		wndclass.hInstance     = hInst;
		wndclass.hIcon         = NULL;
		wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
		wndclass.hbrBackground = 0; //(HBRUSH)(COLOR_3DFACE+1);
		wndclass.lpszMenuName  = NULL;
		wndclass.lpszClassName = _T("AlarmsFrameContainer");
		RegisterClass(&wndclass);

		hwnd_frame = CreateWindowEx(WS_EX_TOOLWINDOW, _T("AlarmsFrameContainer"), TranslateT("Alarms"), 
			(WS_POPUPWINDOW | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_CLIPCHILDREN) & ~WS_VISIBLE,
			0,0,200,100, (HWND)CallService(MS_CLUI_GETHWND, 0, 0), NULL,hInst,NULL);
			//0,0,200,100, GetDesktopWindow(), NULL,hInst,NULL);
	
		hwnd_plugin = CreateWindow(_T("AlarmsFrame"), TranslateT("Alarms"), 
			WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
			0,0,10,10, hwnd_frame, NULL,hInst,NULL);

		SetWindowLongPtr(hwnd_frame, GWLP_USERDATA, (LONG)hwnd_plugin);

		///////////////////////
		// create menu item
		CreateServiceFunction(MODULE "/ShowHideReminders", ShowHideMenuFunc);

		CLISTMENUITEM mi = {0};

		mi.cbSize=sizeof(mi);
		mi.flags = CMIM_ALL;

		mi.hIcon=hIconMenuShowHide;
		mi.pszName = Translate("Show Reminders");
		mi.pszService= MODULE "/ShowHideReminders";
		mi.pszPopupName = Translate("Alarms");
		mi.position = 500010000;
		hMenuShowReminders = Menu_AddMainMenuItem(&mi);
		/////////////////////

		if (!options.auto_showhide) {
			if (options.hide_with_clist) {
				if (IsWindowVisible((HWND)CallService(MS_CLUI_GETHWND, 0, 0))) {
					ShowWindow(hwnd_frame, SW_SHOW);
					RefreshReminderFrame();
				} else {
					ShowWindow(hwnd_frame, SW_HIDE);
				}
			} else {
				if (DBGetContactSettingByte(0, MODULE, "ReminderFrameVisible", 1) == 1) {
					ShowWindow(hwnd_frame, SW_SHOW);
					RefreshReminderFrame();
				} else {
					ShowWindow(hwnd_frame, SW_HIDE);
				}
			}
		}

		FixMainMenu();
	}

	SendMessage(hwnd_plugin, WMU_INITIALIZE, 0, 0);

	font_id.cbSize = sizeof(font_id);
	strncpy(font_id.group, LPGEN("Frames"), sizeof(font_id.group));
	strncpy(font_id.name, LPGEN("Alarm Reminders"), sizeof(font_id.name));
	strncpy(font_id.dbSettingsGroup, MODULE, sizeof(font_id.dbSettingsGroup));
	strncpy(font_id.prefix, "Font", sizeof(font_id.prefix));
	font_id.order = 0;
	FontRegister(&font_id);

	framebk_colour_id.cbSize = sizeof(ColourID);
	strcpy(framebk_colour_id.dbSettingsGroup, MODULE);
	strcpy(framebk_colour_id.group, LPGEN("Frames"));
	strcpy(framebk_colour_id.name, LPGEN("Alarm Reminders"));
	strcpy(framebk_colour_id.setting, "clFrameBack");
	framebk_colour_id.defcolour = GetSysColor(COLOR_3DFACE);
	framebk_colour_id.flags = 0;
	framebk_colour_id.order = 0;
	ColourRegister(&framebk_colour_id);

	LOGFONT log_font;
	fontColour = CallService(MS_FONT_GET, (WPARAM)&font_id, (LPARAM)&log_font);
	hFont = CreateFontIndirect(&log_font);
	SendMessage(hwnd_list, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE);
	
	HookEvent(ME_FONT_RELOAD, ReloadFont);

	// create the brush used for the background in the absence of clist_modern skinning features - match clist
	bk_brush = CreateSolidBrush(DBGetContactSettingDword(0, "Alarm", "clFrameBack", GetSysColor(COLOR_3DFACE)));

	SendMessage(hwnd_list, WM_SETFONT, (WPARAM)hFont, (LPARAM)TRUE);

	return 0;
}

void RefreshReminderFrame() {
	SendMessage(hwnd_plugin, WMU_FILL_LIST, 0, 0);

	if (frame_id == -1) {
		InvalidateRect(hwnd_frame, 0, TRUE);
	} else
		InvalidateRect(hwnd_plugin, 0, TRUE);

}

void InitFrames()
{
	InitializeCriticalSection(&list_cs);
	CreateFrame();
}

void DeinitFrames()
{
	if (ServiceExists(MS_CLIST_FRAMES_REMOVEFRAME)) {
		CallService(MS_CLIST_FRAMES_REMOVEFRAME, (WPARAM)frame_id, 0);
	}
	DestroyWindow(hwnd_plugin);
	if (hwnd_frame) DestroyWindow(hwnd_frame);

	DeleteObject(bk_brush);

	DeleteCriticalSection(&list_cs);
}