
/*******************************************************\
{                                                       }
{       Microsoft Visual C++ Version 4.X                }
{       Alarm Clock 1.0                                 }
{                                                       }
{       Copyright (C) 1996, CAT Computing Center (R).   }
{                                                       }
\*******************************************************/

#define STRICT
#include <Windows.h>
#include <Windowsx.h>
#include <commctrl.h>
#include <tchar.h>
#include "Resource.h"
#include "Alarm.h"

int GetCurrentIndex(HWND hWnd);
void ExecuteMonitor(LPSCHDATA lpSetData);
void DeleteItem(HWND hWnd);
void UpdateItem(HWND hWnd, LPSCHDATA lpSetData);
void FiilList(HWND hWnd);
void InitList(HWND hWnd);
void SaveList(HWND hWnd);
void AddToList(HWND hWnd, LPSCHDATA lpSetData);
void SetButtonState(HWND hWnd, int nIndex = -1);
BOOL GetSelListData(HWND hWnd, LPSCHDATA lpSchData);

BOOL WINAPI Shedul_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

static BOOL s_fSound = FALSE;

void EnableWindows(HWND hWnd, UINT uMode, HWND hWndCtl = NULL)
{
	switch(uMode)
	{
		case IDC_CHECK2:
		{
			if(hWndCtl)
			{
				BOOL fCk2 = !Button_GetCheck(hWndCtl);
				EnableWindow(GetDlgItem(hWnd, IDC_EDIT1), s_fSound ? fCk2 : FALSE);
				EnableWindow(GetDlgItem(hWnd, IDC_BUTTON1), s_fSound ? fCk2 : FALSE);
				EnableWindow(GetDlgItem(hWnd, IDC_STATIC1), s_fSound ? fCk2 : FALSE);
			}
			return;
		}
		case IDC_CHECK3:
		{
			if(hWndCtl)
			{
				BOOL fCk3 = !Button_GetCheck(hWndCtl);
				EnableWindow(GetDlgItem(hWnd, IDC_EDIT3), fCk3);
				EnableWindow(GetDlgItem(hWnd, IDC_STATIC3), fCk3);
			}
			return;
		}
	}

	if(hWndCtl && !Button_GetCheck(hWndCtl))
		return;

	switch(uMode)
	{
		case IDC_RADIO1:
		{
			BOOL fCk2 = !Button_GetCheck(GetDlgItem(hWnd, IDC_CHECK2));
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT1), s_fSound ? fCk2 : FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_BUTTON1), s_fSound ? fCk2 : FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC1), s_fSound);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK2), s_fSound);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_BUTTON2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT3), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK3), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC3), FALSE);
			return;
		}
		case IDC_RADIO2:
		{
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT2), TRUE);
			EnableWindow(GetDlgItem(hWnd, IDC_BUTTON2), TRUE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC2), TRUE);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_BUTTON1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT3), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK3), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC3), FALSE);
			return;
		}
		case IDC_RADIO3:
		{
			BOOL fCk3 = !Button_GetCheck(GetDlgItem(hWnd, IDC_CHECK3));
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT3), fCk3);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK3), TRUE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC3), fCk3);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_BUTTON1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_BUTTON2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC2), FALSE);
			return;
		}
		case IDC_RADIO4:
		{
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK1), TRUE);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_BUTTON1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_BUTTON2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_EDIT3), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_CHECK3), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC1), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC2), FALSE);
			EnableWindow(GetDlgItem(hWnd, IDC_STATIC3), FALSE);
			return;
		}
	}
}

BOOL UpdateData(HWND hWnd, BOOL bSaveAndValidate = TRUE)
{
	if(bSaveAndValidate)
	{
		g_AppOption.AlarmSet.fConfirm = Button_GetCheck(GetDlgItem(hWnd, IDC_CHECK1));
		g_AppOption.AlarmSet.fMultimedia = Button_GetCheck(GetDlgItem(hWnd, IDC_CHECK2));
		g_AppOption.AlarmSet.fMessage = Button_GetCheck(GetDlgItem(hWnd, IDC_CHECK3));
		GetDlgItemText(hWnd, IDC_EDIT1, g_AppOption.AlarmSet.szFileName1, MAX_PATH);
		GetDlgItemText(hWnd, IDC_EDIT2, g_AppOption.AlarmSet.szFileName2, MAX_PATH);
		GetDlgItemText(hWnd, IDC_EDIT3, g_AppOption.AlarmSet.szMesText, 255);

		if(SendMessage(GetDlgItem(hWnd, IDC_RADIO2), BM_GETCHECK, 0, 0))
			g_AppOption.AlarmSet.uAlarmNot = 1;
		else if(SendMessage(GetDlgItem(hWnd, IDC_RADIO3), BM_GETCHECK, 0, 0))
			g_AppOption.AlarmSet.uAlarmNot = 2;
		else if(SendMessage(GetDlgItem(hWnd, IDC_RADIO4), BM_GETCHECK, 0, 0))
			g_AppOption.AlarmSet.uAlarmNot = 3;
		else if(SendMessage(GetDlgItem(hWnd, IDC_RADIO1), BM_GETCHECK, 0, 0))
			g_AppOption.AlarmSet.uAlarmNot = 0;
	}
	else
	{
		SetDlgItemText(hWnd, IDC_EDIT1, g_AppOption.AlarmSet.szFileName1);
		SetDlgItemText(hWnd, IDC_EDIT2, g_AppOption.AlarmSet.szFileName2);
		SetDlgItemText(hWnd, IDC_EDIT3, g_AppOption.AlarmSet.szMesText);
		Button_SetCheck(GetDlgItem(hWnd, IDC_CHECK1), g_AppOption.AlarmSet.fConfirm);
		Button_SetCheck(GetDlgItem(hWnd, IDC_CHECK2), s_fSound ? g_AppOption.AlarmSet.fMultimedia : 1);
		Button_SetCheck(GetDlgItem(hWnd, IDC_CHECK3), g_AppOption.AlarmSet.fMessage);
		switch(g_AppOption.AlarmSet.uAlarmNot)
		{
			case 1:
			{
				Button_SetCheck(GetDlgItem(hWnd, IDC_RADIO2), 1);
				EnableWindows(hWnd, IDC_RADIO2);
				break;
			}
			case 2:
			{
				Button_SetCheck(GetDlgItem(hWnd, IDC_RADIO3), 1);
				EnableWindows(hWnd, IDC_RADIO3);
				break;
			}
			case 3:
			{
				Button_SetCheck(GetDlgItem(hWnd, IDC_RADIO4), 1);
				EnableWindows(hWnd, IDC_RADIO4);
				break;
			}
			default:
			{
				Button_SetCheck(GetDlgItem(hWnd, IDC_RADIO1), 1);
				EnableWindows(hWnd, IDC_RADIO1);
			}
		}
	}
	return TRUE;
}

BOOL OnSelectFile(HWND hWnd, UINT uMode)
{
	OPENFILENAME ofn;
	TCHAR szFileName[MAX_PATH] = _T("");
	TCHAR szFilter[256];

	if(uMode == IDC_BUTTON1)
		LoadString(g_AppData.hInstance, IDS_FILTER1, szFilter, 255);
	else
		LoadString(g_AppData.hInstance, IDS_FILTER2, szFilter, 255);

	LPTSTR lpszTemp = &szFilter[-1];
    while(1)
	{
		lpszTemp = _tcschr(&lpszTemp[1], _T('#'));
		if(lpszTemp)
			lpszTemp[0] = 0;
		else
			break;
	}

	ofn.lStructSize = sizeof(OPENFILENAME); 
	ofn.hwndOwner = hWnd; 
	ofn.hInstance = g_AppData.hInstance; 
	ofn.lpstrFilter = szFilter; 
	ofn.lpstrCustomFilter = NULL; 
	ofn.nMaxCustFilter = NULL; 
	ofn.nFilterIndex = 1;; 
	ofn.lpstrFile = szFileName; 
	ofn.nMaxFile = MAX_PATH; 
	ofn.lpstrFileTitle = NULL;
	ofn.nMaxFileTitle = NULL;
	ofn.lpstrInitialDir = NULL; 
	ofn.lpstrTitle = NULL; 
	ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_EXPLORER; 
	ofn.nFileOffset = NULL; 
	ofn.nFileExtension = NULL; 
	ofn.lpstrDefExt = NULL; 
	ofn.lCustData = NULL; 
	ofn.lpfnHook = NULL; 
	ofn.lpTemplateName = MAKEINTRESOURCE(IDD_OPENFILE); 
	if(GetOpenFileName(&ofn))
	{
		if(uMode == IDC_BUTTON1)
			SetDlgItemText(hWnd, IDC_EDIT1, szFileName);
		else
			SetDlgItemText(hWnd, IDC_EDIT2, szFileName);
		return TRUE;
	}
	return FALSE;
}

void AlarmSet_OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
{
	switch(id)
	{
		case IDC_BUTTON1:
		{
			OnSelectFile(hWnd, IDC_BUTTON1);
			return;
		}
		case IDC_BUTTON2:
		{
			OnSelectFile(hWnd, IDC_BUTTON2);
			return;
		}
	}
	if(codeNotify == BN_CLICKED)
		EnableWindows(hWnd, id, hWndCtl);
}

BOOL AlarmSet_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
{
	if(IsMultimedia() & AC_WAVE)
		s_fSound = TRUE;
	else
		s_fSound = FALSE;

	UpdateData(hWnd, FALSE);
	return TRUE;
}

LRESULT AlarmSet_OnNotify(HWND hWnd, UINT nID, NMHDR * pNmhdr)
{
	if(pNmhdr->code == PSN_APPLY)
		UpdateData(hWnd, TRUE);
	else if(pNmhdr->code == PSN_SETACTIVE)
		g_AppOption.AlarmSet.uLastPage = 0;
	return FALSE;
}

BOOL WINAPI AlarmSet_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		adgHANDLE_DLGMSG(hWnd, WM_INITDIALOG, AlarmSet_OnInitDialog);
		adgHANDLE_DLGMSG(hWnd, WM_COMMAND, AlarmSet_OnCommand);
		adgHANDLE_DLGMSG(hWnd, WM_NOTIFY, AlarmSet_OnNotify);
	}
	return FALSE;
}

void OnSelChange(HWND hWnd, HWND hWndCtl, int nIndex = -1 )
{
	DWORD dwIndex = (DWORD)nIndex;
	
	if(dwIndex == (DWORD)-1)
		dwIndex = ComboBox_GetCurSel(hWndCtl);

	if(dwIndex == 0 || dwIndex == 3)
	{
		EnableWindow(GetDlgItem(hWnd, IDC_EDIT1), FALSE);
		EnableWindow(GetDlgItem(hWnd, IDC_STATIC1), FALSE);
		EnableWindow(GetDlgItem(hWnd, IDC_BUTTON1), FALSE);
	}
	else
	{
		EnableWindow(GetDlgItem(hWnd, IDC_EDIT1), s_fSound);
		EnableWindow(GetDlgItem(hWnd, IDC_STATIC1), s_fSound);
		EnableWindow(GetDlgItem(hWnd, IDC_BUTTON1), s_fSound);
	}
}

BOOL UpdateData2(HWND hWnd, BOOL bSaveAndValidate = TRUE)
{
	if(bSaveAndValidate)
	{
		HWND hList = GetDlgItem(hWnd, IDC_COMBO1);
		GetDlgItemText(hWnd, IDC_EDIT1, g_AppOption.AlarmSet.szFileName3, MAX_PATH);
		g_AppOption.AlarmSet.uClockNot = ComboBox_GetCurSel(hList);
	}
	else
	{
		HWND hList = GetDlgItem(hWnd, IDC_COMBO1);
		TCHAR szText[256];
		LoadString(g_AppData.hInstance, IDS_NONE, szText, 255);
		ComboBox_AddString(hList,szText);
		LoadString(g_AppData.hInstance, IDS_30MIN, szText, 255);
		ComboBox_AddString(hList,szText);
		LoadString(g_AppData.hInstance, IDS_1HOUR, szText, 255);
		ComboBox_AddString(hList,szText);
		LoadString(g_AppData.hInstance, IDS_STROKE, szText, 255);
		ComboBox_AddString(hList,szText);

		ComboBox_SetCurSel(hList, g_AppOption.AlarmSet.uClockNot);
		SetDlgItemText(hWnd, IDC_EDIT1, g_AppOption.AlarmSet.szFileName3);
		OnSelChange(hWnd, NULL, g_AppOption.AlarmSet.uClockNot);
	}
	return TRUE;
}

BOOL ClockSet_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
{
	if(IsMultimedia() & AC_WAVE)
		s_fSound = TRUE;
	else
		s_fSound = FALSE;

	UpdateData2(hWnd, FALSE);
	return TRUE;
}

void ClockSet_OnPaint(HWND hWnd)
{
	PAINTSTRUCT ps;
	HDC hDC = BeginPaint(hWnd, &ps);

    BITMAP bm;
	COLORMAP ColorMap;
	ColorMap.from = RGB(192, 192, 192);
	ColorMap.to = GetSysColor(COLOR_3DFACE);

	HBITMAP hBitmap = CreateMappedBitmap(g_AppData.hInstance, IDB_FLAG1, 0, &ColorMap, 1);
    HDC hdcMem = CreateCompatibleDC(hDC);

    HGDIOBJ hbmOld = SelectObject(hdcMem, hBitmap);
    GetObject(hBitmap, sizeof(bm), &bm);

    RECT Rect;
	SetRect(&Rect, 200, 15, 0, 0);
	MapDialogRect(hWnd, &Rect);
	BitBlt(hDC, Rect.left, Rect.top, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);
	DeleteObject(hBitmap);

	EndPaint(hWnd, &ps);
}

void ClockSet_OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
{
	switch(id)
	{
		case IDC_COMBO1:
		{
			if(codeNotify == CBN_SELCHANGE)
			{
				OnSelChange(hWnd, hWndCtl);
			}
			return;
		}
		case IDC_BUTTON1:
		{
			OnSelectFile(hWnd, IDC_BUTTON1);
			return;
		}
	}
}

LRESULT ClockSet_OnNotify(HWND hWnd, UINT nID, NMHDR * pNmhdr)
{
	if(pNmhdr->code == PSN_APPLY)
		UpdateData2(hWnd, TRUE);
	else if(pNmhdr->code == PSN_SETACTIVE)
		g_AppOption.AlarmSet.uLastPage = 1;
	return FALSE;
}

BOOL WINAPI ClockSet_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		adgHANDLE_DLGMSG(hWnd, WM_INITDIALOG, ClockSet_OnInitDialog);
		adgHANDLE_DLGMSG(hWnd, WM_COMMAND, ClockSet_OnCommand);
		adgHANDLE_DLGMSG(hWnd, WM_NOTIFY, ClockSet_OnNotify);
		adgHANDLE_DLGMSG(hWnd, WM_PAINT, ClockSet_OnPaint);
	}
	return FALSE;
}

BOOL ClockScheduler_OnInitDialog(HWND hWnd, HWND hWndFocus, LPARAM lParam)
{
	InitList(hWnd);
	FiilList(hWnd);
	SetButtonState(hWnd, 0);
	return TRUE;
}

void InitSchData(LPSCHDATA lpSchData)
{
	SYSTEMTIME st;
	GetLocalTime(&st);
	memcpy(&lpSchData->st00, &st, sizeof(SYSTEMTIME));
	memcpy(&lpSchData->st01, &st, sizeof(SYSTEMTIME));
	memcpy(&lpSchData->st20, &st, sizeof(SYSTEMTIME));
	memcpy(&lpSchData->st31, &st, sizeof(SYSTEMTIME));
	memcpy(&lpSchData->st41, &st, sizeof(SYSTEMTIME));
	memset(&lpSchData->st50, 0, sizeof(SYSTEMTIME));
	lpSchData->st50.wMinute = 15;
	lpSchData->nMinutes10 = st.wMinute;
	lpSchData->nDayOfWeek30 = st.wDayOfWeek;
	lpSchData->nDayOfMonth40 = st.wDay;
	lpSchData->nCommand = -1;
	lpSchData->nFrecq = 0;
	lpSchData->bEnable = 1;
	lpSchData->szPath[0] = 0;
	lpSchData->szDesc[0] = 0;
	lpSchData->dwHotKey = 0;
}

void SetButtonState(HWND hWnd, int nIndex /*= -1*/)
{
	HWND ListhWnd = GetDlgItem(hWnd, IDC_EVENTS_LIST);
	if(!ListhWnd)
		return;

	long nCount = ListView_GetItemCount(ListhWnd);
	if(!nCount)	
	{
		EnableWindow(GetDlgItem(hWnd, IDC_DELETE), FALSE);
		EnableWindow(GetDlgItem(hWnd, IDC_EDIT), FALSE);
		EnableWindow(GetDlgItem(hWnd, IDC_COPY), FALSE);
		EnableWindow(GetDlgItem(hWnd, IDC_LAUNCH), FALSE);
	}
	else
	{
		EnableWindow(GetDlgItem(hWnd, IDC_DELETE), TRUE);
		EnableWindow(GetDlgItem(hWnd, IDC_EDIT), TRUE);
		EnableWindow(GetDlgItem(hWnd, IDC_COPY), TRUE);
		EnableWindow(GetDlgItem(hWnd, IDC_LAUNCH), TRUE);

		if(nIndex >= nCount)
			nIndex = nCount - 1;

		LV_ITEM lvItem;
		memset(&lvItem, 0, sizeof(LV_ITEM));
		lvItem.mask = LVIF_STATE;
		lvItem.iItem = nIndex == -1 ? nCount - 1 : nIndex;
		lvItem.stateMask = LVIS_FOCUSED|LVIS_SELECTED;
		lvItem.state = LVIS_FOCUSED|LVIS_SELECTED;
		ListView_SetItem(ListhWnd, &lvItem);
	}
}

void ClockScheduler_OnCommand(HWND hWnd, int id, HWND hWndCtl, UINT codeNotify)
{
	LPSCHDATA lpSchData = NULL;
	switch(id)
	{
		case IDC_LAUNCH:
		{
			lpSchData = new SCHDATA;
			GetSelListData(hWnd, lpSchData);
			ExecuteMonitor(lpSchData);
			delete lpSchData;
			return;
		}
		case IDC_DELETE:
		{
			int nIndex = GetCurrentIndex(hWnd);
			DeleteItem(hWnd);
			SetButtonState(hWnd, nIndex);
			return;
		}
		case IDC_ADD:
			lpSchData = new SCHDATA;
			InitSchData(lpSchData);
			break;
		case IDC_EDIT:
		case IDC_COPY:
			lpSchData = new SCHDATA;
			GetSelListData(hWnd, lpSchData);
		break;
	}
	lpSchData->nCommand = id;
	if(IDOK == DialogBoxParam(g_AppData.hInstance, MAKEINTRESOURCE(IDD_SCHEDULDLG), hWnd, Shedul_WndProc, (LPARAM)lpSchData))
	{
		if(id != IDC_EDIT)
			AddToList(hWnd, lpSchData);
		else
			UpdateItem(hWnd, lpSchData);
	}
	else
	{
		if(lpSchData)
			delete lpSchData;
	}
	SetButtonState(hWnd, id != IDC_EDIT ? -1 : GetCurrentIndex(hWnd));
}

LRESULT ClockScheduler_OnNotify(HWND hWnd, UINT nID, NMHDR * pNmhdr)
{
	if(pNmhdr->code == PSN_APPLY)
	{
		SaveList(hWnd);
	}
	else if(pNmhdr->code == PSN_SETACTIVE)
	{
		g_AppOption.AlarmSet.uLastPage = 2;
	}
	else if((nID == IDC_EVENTS_LIST) && (pNmhdr->code == LVN_ITEMCHANGED))
	{
		HWND ListhWnd = GetDlgItem(hWnd, IDC_EVENTS_LIST);
		if(!ListhWnd)
			return 0;
		
		int nIndex = ListView_GetNextItem(ListhWnd, -1, LVNI_FOCUSED);
		if(nIndex != -1)
			SetButtonState(hWnd, nIndex);
	}
	else if((nID == IDC_EVENTS_LIST) && (pNmhdr->code == NM_RCLICK || pNmhdr->code == NM_DBLCLK))
	{
		HWND ListhWnd = GetDlgItem(hWnd, IDC_EVENTS_LIST);
		if(!ListhWnd)
			return 0;
		
		LV_HITTESTINFO hti;
		GetCursorPos(&hti.pt);
		ScreenToClient(ListhWnd, &hti.pt);
		hti.flags = LVHT_ONITEMLABEL;
		if(ListView_HitTest(ListhWnd, &hti) == -1)
			return 0;
		if(pNmhdr->code == NM_DBLCLK)
		{
			ClockScheduler_OnCommand(hWnd, IDC_EDIT, NULL, NULL);
		}
		else
		{
			HMENU hMenuMain = LoadMenu(g_AppData.hInstance, MAKEINTRESOURCE(IDR_LISTMENU));
			HMENU hMenuContext = GetSubMenu(hMenuMain, 0);
			SetMenuDefaultItem(hMenuContext, 0, TRUE);
			POINT pt;
			GetCursorPos(&pt);
			TrackPopupMenu(hMenuContext, TPM_LEFTALIGN|TPM_TOPALIGN|TPM_RIGHTBUTTON|TPM_LEFTBUTTON, pt.x, pt.y, 0, hWnd, NULL);
			DestroyMenu(hMenuContext);
			DestroyMenu(hMenuMain);
		}
	}
	return FALSE;
}

LRESULT ClockScheduler_OnDestroy(HWND hWnd)
{
	HWND ListhWnd = GetDlgItem(hWnd, IDC_EVENTS_LIST);
	if(ListhWnd)
	{
		long nCount = ListView_GetItemCount(ListhWnd);
		for(int i = 0; i < nCount; i++)
		{
			LV_ITEM lvItem;
			memset(&lvItem, 0, sizeof(LV_ITEM));
			lvItem.mask = LVIF_PARAM;
			lvItem.iItem = i;
			if(ListView_GetItem(ListhWnd, &lvItem))
			{
				delete ((LPSCHDATA)lvItem.lParam);
			}
		}
	}
	return FALSE;
}

void DrawButtonCaption(HDC hDC, LPRECT lpRect, UINT CtlID, HWND hWndItem, BOOL bDisable)
{
	TCHAR szText[256];
	GetWindowText(hWndItem, szText, 255);
	if(bDisable)
	{
		SetBkMode(hDC, TRANSPARENT);
		SetTextColor(hDC, GetSysColor(COLOR_BTNHIGHLIGHT));
		OffsetRect(lpRect, 1, 1);
		DrawText(hDC, szText, -1, lpRect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
		SetTextColor(hDC, GetSysColor(COLOR_GRAYTEXT));
		OffsetRect(lpRect, -1, -1);
		DrawText(hDC, szText, -1, lpRect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
	}
	else
		DrawText(hDC, szText, -1, lpRect, DT_SINGLELINE|DT_CENTER|DT_VCENTER);
}

void DrawPicture(HDC hDC, LPRECT lpRect, UINT CtlID, HWND hWndItem, BOOL bDisable)
{
	HBITMAP hBitmap = NULL;
	COLORMAP ColorMap[1];
	ColorMap[0].from = RGB(0, 0, 0);
	ColorMap[0].to = bDisable ? RGB(255, 255, 255) : GetSysColor(COLOR_3DFACE);

	if(CtlID == IDC_ADD)
		hBitmap = CreateMappedBitmap(g_AppData.hInstance, IDB_ADD, 0, ColorMap, 1);
	else if(CtlID == IDC_EDIT)
		hBitmap = CreateMappedBitmap(g_AppData.hInstance, IDB_EDIT, 0, ColorMap, 1);
	else if(CtlID == IDC_COPY)
		hBitmap = CreateMappedBitmap(g_AppData.hInstance, IDB_COPY, 0, ColorMap, 1);
	else if(CtlID == IDC_DELETE)
		hBitmap = CreateMappedBitmap(g_AppData.hInstance, IDB_DELETE, 0, ColorMap, 1);
	else if(CtlID == IDC_LAUNCH)
		hBitmap = CreateMappedBitmap(g_AppData.hInstance, IDB_LAUNCH, 0, ColorMap, 1);

	BITMAP bm;
	GetObject(hBitmap, sizeof(bm), &bm);
	DrawState(hDC, NULL, NULL, (LPARAM)hBitmap, (WPARAM)0, lpRect->left + 5, ((lpRect->bottom - lpRect->top) >> 1) - (bm.bmHeight >> 1), bm.bmWidth, bm.bmHeight, bDisable ? DSS_DISABLED|DST_BITMAP : DST_BITMAP);
	lpRect->left += lpRect->left + 5 + bm.bmWidth;
	DeleteObject(hBitmap);
}

void ClockScheduler_OnDrawItem(HWND hWnd, const DRAWITEMSTRUCT* lpDrawItem)
{
	RECT Rect;
	CopyRect(&Rect, &lpDrawItem->rcItem);

	if(lpDrawItem->itemState & ODS_SELECTED)
	{
		OffsetRect(&Rect, 1, 1);
		DrawFrameControl(lpDrawItem->hDC, (LPRECT)&lpDrawItem->rcItem, DFC_BUTTON, DFCS_PUSHED|DFCS_BUTTONPUSH);
	}
	else if(lpDrawItem->itemState & ODS_DISABLED)
		DrawFrameControl(lpDrawItem->hDC, (LPRECT)&lpDrawItem->rcItem, DFC_BUTTON, DFCS_INACTIVE|DFCS_BUTTONPUSH);
	else
		DrawFrameControl(lpDrawItem->hDC, (LPRECT)&lpDrawItem->rcItem, DFC_BUTTON, DFCS_BUTTONPUSH);

	if(lpDrawItem->itemState & ODS_FOCUS)
	{
		RECT Rect;
		CopyRect(&Rect, &lpDrawItem->rcItem);
		InflateRect(&Rect, -3, -3);
		DrawFocusRect(lpDrawItem->hDC, &Rect);
	}
	DrawPicture(lpDrawItem->hDC, &Rect, lpDrawItem->CtlID, lpDrawItem->hwndItem, lpDrawItem->itemState & ODS_DISABLED);
	DrawButtonCaption(lpDrawItem->hDC, &Rect, lpDrawItem->CtlID, lpDrawItem->hwndItem, lpDrawItem->itemState & ODS_DISABLED);
}

BOOL WINAPI ClockScheduler_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch (uMsg)
	{
		adgHANDLE_DLGMSG(hWnd, WM_DESTROY, ClockScheduler_OnDestroy);
		adgHANDLE_DLGMSG(hWnd, WM_INITDIALOG, ClockScheduler_OnInitDialog);
		adgHANDLE_DLGMSG(hWnd, WM_COMMAND, ClockScheduler_OnCommand);
		adgHANDLE_DLGMSG(hWnd, WM_NOTIFY, ClockScheduler_OnNotify);
		adgHANDLE_DLGMSG(hWnd, WM_DRAWITEM, ClockScheduler_OnDrawItem);
	}
	return FALSE;
}

static BOOL s_fShow = FALSE;

BOOL IsShowSettingsDlg()
{
	return s_fShow;
}

void ShowSettingsDlg(HINSTANCE hInstance, HWND hWnd)
{
	if(s_fShow)
		return;

	TCHAR szCaption[256];
	PROPSHEETPAGE PSheetPage[3];
	PROPSHEETHEADER PSheetHeader;
	LoadString(hInstance, IDS_CAPTION_SET, szCaption, 256);

	PSheetPage[0].dwSize = sizeof(PROPSHEETPAGE);
	PSheetPage[0].dwFlags = PSP_USEICONID;
	PSheetPage[0].hInstance = hInstance;
	PSheetPage[0].pszIcon = MAKEINTRESOURCE(IDI_ALARM_SET);
	PSheetPage[0].pszTemplate = MAKEINTRESOURCE(IDD_ALARM_SET);
	PSheetPage[0].pfnDlgProc = AlarmSet_WndProc;
	PSheetPage[0].pszTitle = NULL;
	PSheetPage[0].lParam = 0;

	PSheetPage[1].dwSize = sizeof(PROPSHEETPAGE);
	PSheetPage[1].dwFlags = PSP_USEICONID;
	PSheetPage[1].hInstance = hInstance;
	PSheetPage[1].pszIcon = MAKEINTRESOURCE(IDI_CLOCK_SET);
	PSheetPage[1].pszTemplate = MAKEINTRESOURCE(IDD_CLOCK_SET);
	PSheetPage[1].pfnDlgProc = ClockSet_WndProc;
	PSheetPage[1].pszTitle = NULL;
	PSheetPage[1].lParam = 0;

	PSheetPage[2].dwSize = sizeof(PROPSHEETPAGE);
	PSheetPage[2].dwFlags = PSP_USEICONID;
	PSheetPage[2].hInstance = hInstance;
	PSheetPage[2].pszIcon = MAKEINTRESOURCE(IDI_CLOCK_SCHEDULER);
	PSheetPage[2].pszTemplate = MAKEINTRESOURCE(IDD_CLOCK_SCHEDULER);
	PSheetPage[2].pfnDlgProc = ClockScheduler_WndProc;
	PSheetPage[2].pszTitle = NULL;
	PSheetPage[2].lParam = 0;

	PSheetHeader.dwSize = sizeof(PROPSHEETHEADER);
	PSheetHeader.dwFlags = PSH_USEICONID | PSH_PROPSHEETPAGE | PSH_NOAPPLYNOW;
	PSheetHeader.hInstance = hInstance;
	PSheetHeader.pszIcon = MAKEINTRESOURCE(IDI_ALARM);
	PSheetHeader.hwndParent = hWnd;
	PSheetHeader.pszCaption = szCaption;
	PSheetHeader.nPages = 3;
	PSheetHeader.nStartPage = g_AppOption.AlarmSet.uLastPage;
	PSheetHeader.ppsp = (LPPROPSHEETPAGE)&PSheetPage;

	s_fShow = TRUE;
	PropertySheet(&PSheetHeader);
	s_fShow = FALSE;
}
