summaryrefslogtreecommitdiff
path: root/plugins/Alarms/alarm_win.cpp
diff options
context:
space:
mode:
authorKirill Volinsky <mataes2007@gmail.com>2012-05-19 14:38:34 +0000
committerKirill Volinsky <mataes2007@gmail.com>2012-05-19 14:38:34 +0000
commit4392b08fedcfc61eb3ec40a956516637d5abbf38 (patch)
tree16061c2ae482ea45b070fe4171b080edc1cf5c1b /plugins/Alarms/alarm_win.cpp
parent162e60d66c78bd51aa44c691fe4f4e2f1deb990f (diff)
added Alarms
git-svn-id: http://svn.miranda-ng.org/main/trunk@77 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Alarms/alarm_win.cpp')
-rw-r--r--plugins/Alarms/alarm_win.cpp430
1 files changed, 430 insertions, 0 deletions
diff --git a/plugins/Alarms/alarm_win.cpp b/plugins/Alarms/alarm_win.cpp
new file mode 100644
index 0000000000..250dcdaf31
--- /dev/null
+++ b/plugins/Alarms/alarm_win.cpp
@@ -0,0 +1,430 @@
+#include "common.h"
+#include "alarm_win.h"
+
+#define ID_TIMER_SOUND 10101
+#define SOUND_REPEAT_PERIOD 5000 // milliseconds
+#define SPEACH_REPEAT_PERIOD 15000 // milliseconds
+HANDLE hAlarmWindowList = 0;
+
+HMODULE hUserDll;
+BOOL (WINAPI *MySetLayeredWindowAttributes)(HWND,COLORREF,BYTE,DWORD) = 0;
+BOOL (WINAPI *MyAnimateWindow)(HWND hWnd,DWORD dwTime,DWORD dwFlags) = 0;
+
+FontID title_font_id, window_font_id;
+ColourID bk_colour_id;
+HFONT hTitleFont = 0, hWindowFont = 0;
+COLORREF title_font_colour, window_font_colour;
+HBRUSH hBackgroundBrush = 0;
+
+#define WMU_SETFONTS (WM_USER + 61)
+#define WMU_REFRESH (WM_USER + 62)
+#define WMU_ADDSNOOZER (WM_USER + 63)
+
+int win_num = 0;
+typedef struct WindowData_tag {
+ ALARM *alarm;
+ POINT p;
+ bool moving;
+ int win_num;
+} WindowData;
+
+void SetAlarmWinOptions() {
+ WindowList_Broadcast(hAlarmWindowList, WMU_SETOPT, IDC_SNOOZE, 0);
+}
+
+bool TransparencyEnabled() {
+ return MySetLayeredWindowAttributes != 0;
+}
+
+BOOL CALLBACK DlgProcAlarm(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam) {
+
+ switch ( msg ) {
+ case WM_INITDIALOG:
+ {
+ TranslateDialogDefault( hwndDlg );
+ Utils_RestoreWindowPositionNoSize(hwndDlg, 0, MODULE, "Notify");
+ SetFocus(GetDlgItem(hwndDlg, IDC_SNOOZE));
+
+ WindowData *wd = new WindowData;
+ wd->moving = false;
+ wd->alarm = 0;
+ wd->win_num = win_num++;
+
+ if(wd->win_num > 0) {
+ RECT r;
+ GetWindowRect(hwndDlg, &r);
+ r.top += 20;
+ r.left += 20;
+
+ SetWindowPos(hwndDlg, 0, r.left, r.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE);
+ Utils_SaveWindowPosition(hwndDlg, 0, MODULE, "Notify");
+ }
+
+ SetWindowLong(hwndDlg, GWL_USERDATA, (LONG)wd);
+
+ // options
+ SendMessage(hwndDlg, WMU_SETOPT, 0, 0);
+
+ // fonts
+ SendMessage(hwndDlg, WMU_SETFONTS, 0, 0);
+
+ }
+ return FALSE;
+ case WMU_REFRESH:
+ InvalidateRect(hwndDlg, 0, TRUE);
+ return TRUE;
+ case WM_CTLCOLORSTATIC:
+ {
+ HDC hdc = (HDC)wParam;
+ HWND hwndCtrl = (HWND)lParam;
+
+ if(hBackgroundBrush) {
+ if(hTitleFont && GetDlgItem(hwndDlg, IDC_TITLE) == hwndCtrl)
+ SetTextColor(hdc, title_font_colour);
+ else if(hWindowFont)
+ SetTextColor(hdc, window_font_colour);
+
+ SetBkMode(hdc, TRANSPARENT);
+ return (BOOL)hBackgroundBrush;
+ }
+ }
+ break;
+ case WM_CTLCOLORDLG:
+ if(hBackgroundBrush)
+ return (BOOL)hBackgroundBrush;
+ break;
+ case WMU_SETFONTS:
+ // fonts
+ if(hWindowFont)
+ SendMessage(hwndDlg, WM_SETFONT, (WPARAM)hWindowFont, (LPARAM)TRUE);
+ if(hTitleFont)
+ SendDlgItemMessage(hwndDlg, IDC_TITLE, WM_SETFONT, (WPARAM)hTitleFont, (LPARAM)TRUE);
+
+ if(hBackgroundBrush) {
+ SetClassLong(hwndDlg, GCL_HBRBACKGROUND, (LONG)hBackgroundBrush);
+ InvalidateRect(hwndDlg, 0, TRUE);
+ }
+
+ return TRUE;
+ case WMU_SETOPT:
+ {
+ Options *opt;
+ if(lParam) opt = (Options *)lParam;
+ else opt = &options;
+
+ // round corners
+ if(opt->aw_roundcorners) {
+ HRGN hRgn1;
+ RECT r;
+ int v,h;
+ int w=10;
+ GetWindowRect(hwndDlg,&r);
+ h=(r.right-r.left)>(w*2)?w:(r.right-r.left);
+ v=(r.bottom-r.top)>(w*2)?w:(r.bottom-r.top);
+ h=(h<v)?h:v;
+ hRgn1=CreateRoundRectRgn(0,0,(r.right-r.left+1),(r.bottom-r.top+1),h,h);
+ SetWindowRgn(hwndDlg,hRgn1,1);
+ } else {
+ HRGN hRgn1;
+ RECT r;
+ int v,h;
+ int w=10;
+ GetWindowRect(hwndDlg,&r);
+ h=(r.right-r.left)>(w*2)?w:(r.right-r.left);
+ v=(r.bottom-r.top)>(w*2)?w:(r.bottom-r.top);
+ h=(h<v)?h:v;
+ hRgn1=CreateRectRgn(0,0,(r.right-r.left+1),(r.bottom-r.top+1));
+ SetWindowRgn(hwndDlg,hRgn1,1);
+ }
+ // transparency
+
+#ifdef WS_EX_LAYERED
+ SetWindowLong(hwndDlg, GWL_EXSTYLE, GetWindowLong(hwndDlg, GWL_EXSTYLE) | WS_EX_LAYERED);
+#endif
+#ifdef LWA_ALPHA
+ if(MySetLayeredWindowAttributes)
+ MySetLayeredWindowAttributes(hwndDlg, RGB(0,0,0), (int)((100 - opt->aw_trans) / 100.0 * 255), LWA_ALPHA);
+#endif
+ }
+ return TRUE;
+ case WMU_SETALARM:
+ {
+ ALARM *data = (ALARM *)lParam;
+ SetWindowText(hwndDlg, data->szTitle);
+ HWND hw = GetDlgItem(hwndDlg, IDC_TITLE);
+ SetWindowText(hw, data->szTitle);
+
+ SetDlgItemText(hwndDlg, IDC_ED_DESC, data->szDesc);
+ ((WindowData *)GetWindowLong(hwndDlg, GWL_USERDATA))->alarm = data;
+
+ if(data->action & AAF_SOUND && options.loop_sound) {
+ if(data->sound_num <= 3)
+ SetTimer(hwndDlg, ID_TIMER_SOUND, SOUND_REPEAT_PERIOD, 0);
+ else if(data->sound_num == 4)
+ SetTimer(hwndDlg, ID_TIMER_SOUND, SPEACH_REPEAT_PERIOD, 0);
+ }
+
+ hw = GetDlgItem(hwndDlg, IDC_SNOOZE);
+ EnableWindow(hw, !(data->flags & ALF_NOSNOOZE));
+ ShowWindow(hw, (data->flags & ALF_NOSNOOZE) ? SW_HIDE : SW_SHOWNA);
+ }
+ return TRUE;
+ case WMU_FAKEALARM:
+ {
+ SetWindowText(hwndDlg, Translate("Example Alarm"));
+ HWND hw = GetDlgItem(hwndDlg, IDC_TITLE);
+ SetWindowText(hw, Translate("Example Alarm"));
+ SetDlgItemText(hwndDlg, IDC_ED_DESC, Translate("Some example text. Example, example, example."));
+ }
+ return TRUE;
+ case WM_TIMER:
+ {
+ if(wParam == ID_TIMER_SOUND) {
+ WindowData *dw = (WindowData *)GetWindowLong(hwndDlg, GWL_USERDATA);
+ if(dw) {
+ ALARM *data = dw->alarm;
+ if(data && data->action & AAF_SOUND) {
+ if(data->sound_num <= 3) {
+ char buff[128];
+ sprintf(buff, "Triggered%d", data->sound_num);
+ SkinPlaySound(buff);
+ } else if(data->sound_num == 4) {
+ if (data->szTitle != NULL && data->szTitle[0] != '\0') {
+ if (ServiceExists("Speak/Say")) {
+ CallService("Speak/Say", 0, (LPARAM)data->szTitle);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ return TRUE;
+ case WM_MOVE:
+ {
+ //WindowData *wd = (WindowData *)GetWindowLong(hwndDlg, GWL_USERDATA);
+ Utils_SaveWindowPosition(hwndDlg, 0, MODULE, "Notify");
+ }
+ break;
+
+ case WMU_ADDSNOOZER:
+ {
+ WindowData *wd = (WindowData *)GetWindowLong(hwndDlg, GWL_USERDATA);
+ if(wd) {
+ ALARM *data = wd->alarm;
+
+ if(data) {
+ // add snooze minutes to current time
+ FILETIME ft;
+ GetLocalTime(&data->time);
+ SystemTimeToFileTime(&data->time, &ft);
+ ULARGE_INTEGER uli;
+ uli.LowPart = ft.dwLowDateTime;
+ uli.HighPart = ft.dwHighDateTime;
+
+ // there are 10000000 100-nanosecond blocks in a second...
+ uli.QuadPart += mult.QuadPart * (int)(wParam);
+
+ ft.dwHighDateTime = uli.HighPart;
+ ft.dwLowDateTime = uli.LowPart;
+
+ FileTimeToSystemTime(&ft, &data->time);
+
+ data->occurrence = OC_ONCE;
+ data->snoozer = true;
+ data->flags = data->flags & ~ALF_NOSTARTUP;
+
+ data->id = next_alarm_id++;
+
+ append_to_list(data);
+ }
+ }
+
+ }
+ return TRUE;
+ case WM_COMMAND:
+ if ( HIWORD( wParam ) == BN_CLICKED ) {
+ switch( LOWORD( wParam )) {
+ case IDCANCEL: // no button - esc pressed
+ case IDOK: // space?
+ case IDC_SNOOZE:
+ SendMessage(hwndDlg, WMU_ADDSNOOZER, (WPARAM)options.snooze_minutes, 0);
+ //drop through
+ case IDC_DISMISS:
+ {
+ WindowData *window_data = (WindowData *)GetWindowLong(hwndDlg, GWL_USERDATA);
+ KillTimer(hwndDlg, ID_TIMER_SOUND);
+ if(window_data) {
+ if(window_data->alarm) {
+ free_alarm_data(window_data->alarm);
+ delete window_data->alarm;
+ }
+ delete window_data;
+ }
+ SetWindowLong(hwndDlg, GWL_USERDATA, 0);
+
+ win_num--;
+ //EndDialog(hwndDlg, IDOK); // not modal!
+ WindowList_Remove(hAlarmWindowList, hwndDlg);
+ DestroyWindow(hwndDlg);
+ }
+ break;
+ case IDC_SNOOZELIST:
+ {
+ POINT pt, pt_rel;
+ GetCursorPos(&pt);
+ pt_rel = pt;
+ ScreenToClient(hwndDlg,&pt_rel);
+
+ HMENU hMenu = CreatePopupMenu();
+ MENUITEMINFO mmi = {0};
+ mmi.cbSize = sizeof(mmi);
+ mmi.fMask = MIIM_ID | MIIM_STRING;
+
+#define AddItem(x) \
+ mmi.wID++; \
+ mmi.dwTypeData = Translate(x); \
+ mmi.cch = strlen(mmi.dwTypeData); \
+ InsertMenuItem(hMenu, mmi.wID, FALSE, &mmi);
+
+ AddItem("5 mins");
+ AddItem("15 mins");
+ AddItem("30 mins");
+ AddItem("1 hour");
+ AddItem("1 day");
+ AddItem("1 week");
+
+ TPMPARAMS tpmp = {0};
+ tpmp.cbSize = sizeof(tpmp);
+ LRESULT ret = (LRESULT)TrackPopupMenuEx(hMenu, TPM_RETURNCMD, pt.x, pt.y, hwndDlg, &tpmp);
+ switch(ret) {
+ case 0: DestroyMenu(hMenu); return 0; // dismis menu
+ case 1: SendMessage(hwndDlg, WMU_ADDSNOOZER, (WPARAM)5, 0); break;
+ case 2: SendMessage(hwndDlg, WMU_ADDSNOOZER, (WPARAM)15, 0); break;
+ case 3: SendMessage(hwndDlg, WMU_ADDSNOOZER, (WPARAM)30, 0); break;
+ case 4: SendMessage(hwndDlg, WMU_ADDSNOOZER, (WPARAM)60, 0); break;
+ case 5: SendMessage(hwndDlg, WMU_ADDSNOOZER, (WPARAM)(60 * 24), 0); break;
+ case 6: SendMessage(hwndDlg, WMU_ADDSNOOZER, (WPARAM)(60 * 24 * 7), 0); break;
+ }
+
+ DestroyMenu(hMenu);
+
+ SendMessage(hwndDlg, WM_COMMAND, IDC_DISMISS, 0);
+ }
+ break;
+ }
+ }
+ return TRUE;
+
+ case WM_MOUSEMOVE:
+ if(wParam & MK_LBUTTON) {
+ SetCapture(hwndDlg);
+
+ POINT newp;
+ newp.x = (short)LOWORD(lParam);
+ newp.y = (short)HIWORD(lParam);
+
+ ClientToScreen(hwndDlg, &newp);
+
+ WindowData *window_data = (WindowData *)GetWindowLong(hwndDlg, GWL_USERDATA);
+ if(!window_data->moving) {
+ window_data->moving = true;
+ } else {
+ RECT r;
+ GetWindowRect(hwndDlg, &r);
+
+ SetWindowPos(hwndDlg, 0, r.left + (newp.x - window_data->p.x), r.top + (newp.y - window_data->p.y), 0, 0, SWP_NOSIZE | SWP_NOZORDER);
+ }
+ window_data->p.x = newp.x;
+ window_data->p.y = newp.y;
+ } else {
+ ReleaseCapture();
+ WindowData *window_data = (WindowData *)GetWindowLong(hwndDlg, GWL_USERDATA);
+ window_data->moving = false;
+ }
+ return TRUE;
+ }
+
+ return FALSE;
+}
+
+int ReloadFonts(WPARAM wParam, LPARAM lParam) {
+ LOGFONT log_font;
+ title_font_colour = CallService(MS_FONT_GET, (WPARAM)&title_font_id, (LPARAM)&log_font);
+ DeleteObject(hTitleFont);
+ hTitleFont = CreateFontIndirect(&log_font);
+
+ window_font_colour = CallService(MS_FONT_GET, (WPARAM)&window_font_id, (LPARAM)&log_font);
+ DeleteObject(hWindowFont);
+ hWindowFont = CreateFontIndirect(&log_font);
+
+ COLORREF bkCol = CallService(MS_COLOUR_GET, (WPARAM)&bk_colour_id, 0);
+ DeleteObject(hBackgroundBrush);
+ hBackgroundBrush = CreateSolidBrush(bkCol);
+
+ WindowList_Broadcast(hAlarmWindowList, WMU_REFRESH, 0, 0);
+ return 0;
+}
+
+int AlarmWinModulesLoaded(WPARAM wParam, LPARAM lParam) {
+ if(ServiceExists(MS_FONT_REGISTER)) {
+ title_font_id.cbSize = sizeof(FontID);
+ strcpy(title_font_id.group, Translate("Alarms"));
+ strcpy(title_font_id.name, Translate("Title"));
+ strcpy(title_font_id.dbSettingsGroup, MODULE);
+ strcpy(title_font_id.prefix, "FontTitle");
+ title_font_id.flags = 0;
+ title_font_id.order = 0;
+
+ CallService(MS_FONT_REGISTER, (WPARAM)&title_font_id, 0);
+
+ window_font_id.cbSize = sizeof(FontID);
+ strcpy(window_font_id.group, Translate("Alarms"));
+ strcpy(window_font_id.name, Translate("Window"));
+ strcpy(window_font_id.dbSettingsGroup, MODULE);
+ strcpy(window_font_id.prefix, "FontWindow");
+ window_font_id.flags = 0;
+ window_font_id.order = 1;
+
+ CallService(MS_FONT_REGISTER, (WPARAM)&window_font_id, 0);
+
+ bk_colour_id.cbSize = sizeof(ColourID);
+ strcpy(bk_colour_id.dbSettingsGroup, MODULE);
+ strcpy(bk_colour_id.group, Translate("Alarms"));
+ strcpy(bk_colour_id.name, Translate("Background"));
+ strcpy(bk_colour_id.setting, "BkColour");
+ bk_colour_id.defcolour = GetSysColor(COLOR_3DFACE);
+ bk_colour_id.flags = 0;
+ bk_colour_id.order = 0;
+
+ CallService(MS_COLOUR_REGISTER, (WPARAM)&bk_colour_id, 0);
+
+ ReloadFonts(0, 0);
+ HookEvent(ME_FONT_RELOAD, ReloadFonts);
+ }
+ return 0;
+}
+
+void InitAlarmWin() {
+ hUserDll = LoadLibrary("user32.dll");
+ if (hUserDll) {
+ MySetLayeredWindowAttributes = (BOOL (WINAPI *)(HWND,COLORREF,BYTE,DWORD))GetProcAddress(hUserDll, "SetLayeredWindowAttributes");
+ //MyAnimateWindow=(BOOL (WINAPI*)(HWND,DWORD,DWORD))GetProcAddress(hUserDll,"AnimateWindow");
+ }
+
+ hAlarmWindowList = (HANDLE)CallService(MS_UTILS_ALLOCWINDOWLIST, 0, 0);
+
+ HookEvent(ME_SYSTEM_MODULESLOADED, AlarmWinModulesLoaded);
+}
+
+void DeinitAlarmWin() {
+
+ WindowList_Broadcast(hAlarmWindowList, WM_COMMAND, IDC_SNOOZE, 0);
+
+ FreeLibrary(hUserDll);
+
+ if(hBackgroundBrush) DeleteObject(hBackgroundBrush);
+ if(hTitleFont) DeleteObject(hTitleFont);
+ if(hWindowFont) DeleteObject(hWindowFont);
+}
+