
/*******************************************************\
{                                                       }
{       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"

#define	TCX		291
#define	TCY		27

#define	BCX		72
#define	BCY		26

#define	B1X		0
#define	B1Y		0

#define	B2X		73
#define	B2Y		0

#define	B3X		146
#define	B3Y		0

#define	B4X		219
#define	B4Y		0


static int nButtonPressed = 0;
static int nLastButtonPressed = 0;
static WNDPROC oldWndPros = NULL;
static BOOL fCapture = FALSE;
BOOL CenterWindow(HWND hwndChild, HWND hwndParent);
///////////////////////////////////////////////////////////////////////////////

inline BOOL HitTest(int nX1, int nY1, int nX2, int nY2, int nX, int nY)
{
	if((nX > nX1) && (nX < nX2) && (nY > nY1) && (nY < nY2))
		return TRUE;
	else
		return FALSE;
}

inline BOOL HitTest(RECT& Rect, int nX, int nY)
{
	if((nX > Rect.left) && (nX < Rect.right) && (nY > Rect.top) && (nY < Rect.bottom))
		return TRUE;
	else
		return FALSE;
}

VOID CALLBACK ToolBar_TimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
	if(!g_AppOption.uSet)
		return;

	switch(nButtonPressed)
	{
/*		case 1:
		{
			SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_SET, BN_CLICKED), (LPARAM)hWnd);
			break;
		}*/
		case 2:
		{
			SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_DATE, BN_CLICKED), (LPARAM)hWnd);
			break;
		}
		case 3:
		{
			SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_ALARM, BN_CLICKED), (LPARAM)hWnd);
			break;
		}
/*		case 4:
		{
			SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_ALTM, BN_CLICKED), (LPARAM)hWnd);
			break;
		}*/
	}

}

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

    BITMAP bm;
	COLORMAP ColorMap[4];
	ColorMap[0].from = RGB(192, 192, 192);
	ColorMap[0].to = GetSysColor(COLOR_3DFACE);
	
	ColorMap[1].from = RGB(255, 255, 255);
	ColorMap[1].to = GetSysColor(COLOR_3DHILIGHT);
	
	ColorMap[2].from = RGB(128, 128, 128);
	ColorMap[2].to = GetSysColor(COLOR_3DSHADOW);

	ColorMap[3].from = RGB(0, 0, 128);
	ColorMap[3].to = GetSysColor(COLOR_ACTIVECAPTION);
	

	HBITMAP hBitmap = CreateMappedBitmap(g_AppData.hInstance, IDB_TOOLBAR, 0, ColorMap, 4);
    HDC hdcMem = CreateCompatibleDC(hDC);

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

	BitBlt(hDC, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);

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

	if(!nButtonPressed)
	{
		DeleteDC(hdcMem);
		EndPaint(hWnd, &ps);
		return;
	}

	hBitmap = CreateMappedBitmap(g_AppData.hInstance, IDB_TOOLBAR1, 0, ColorMap, 4);
    hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap);
    GetObject(hBitmap, sizeof(bm), &bm);


	switch(nButtonPressed)
	{
		case 1:
		{
			BitBlt(hDC, B1X, B1Y, BCX, BCY, hdcMem, B1X, B1Y, SRCCOPY);
			break;
		}
		case 2:
		{
			BitBlt(hDC, B2X, B2Y, BCX, BCY, hdcMem, B2X, B2Y, SRCCOPY);
			break;
		}
		case 3:
		{
			BitBlt(hDC, B3X, B3Y, BCX, BCY, hdcMem, B3X, B3Y, SRCCOPY);
			break;
		}
		case 4:
		{
			BitBlt(hDC, B4X, B4Y, BCX, BCY, hdcMem, B4X, B4Y, SRCCOPY);
			break;
		}
	}

	SelectObject(hdcMem, hbmOld);
	DeleteObject(hBitmap);
    DeleteDC(hdcMem);
	EndPaint(hWnd, &ps);
}

void ToolBar_OnLButtonUp(HWND hWnd, int nX, int nY, UINT uKeyFlags)
{
	ReleaseCapture();
	fCapture = FALSE;

	switch(nButtonPressed)
	{
		case 0:
		{
			return;
		}
		case 1:
		{
			nButtonPressed = 0;
			RECT Rect = {B1X, B1Y, B1X + BCX, B1Y + BCY};
			InvalidateRect(hWnd, &Rect, FALSE);
			UpdateWindow(hWnd);
			KillTimer(hWnd, 777);
			break;
		}
		case 2:
		{
			nButtonPressed = 0;
			RECT Rect = {B2X, B2Y, B2X + BCX, B2Y + BCY};
			InvalidateRect(hWnd, &Rect, FALSE);
			UpdateWindow(hWnd);
			KillTimer(hWnd, 777);
			break;
		}
		case 3:
		{
			nButtonPressed = 0;
			RECT Rect = {B3X, B3Y, B3X + BCX, B3Y + BCY};
			InvalidateRect(hWnd, &Rect, FALSE);
			UpdateWindow(hWnd);
			KillTimer(hWnd, 777);
			break;
		}
		case 4:
		{
			nButtonPressed = 0;
			RECT Rect = {B4X, B4Y, B4X + BCX, B4Y + BCY};
			InvalidateRect(hWnd, &Rect, FALSE);
			UpdateWindow(hWnd);
			KillTimer(hWnd, 777);
			break;
		}
	}
}

void ToolBar_OnLButtonDown(HWND hWnd, BOOL fDoubleClick, int nX, int nY, UINT uKeyFlags)
{
	if(nButtonPressed)
		return;

	SetCapture(hWnd);
	fCapture = TRUE;

	if(HitTest(B1X, B1Y, B1X + BCX, B1Y + BCY, nX, nY))
	{
		nButtonPressed = 1;
		RECT Rect = {B1X, B1Y, B1X + BCX, B1Y + BCY};
		InvalidateRect(hWnd, &Rect, FALSE);
		UpdateWindow(hWnd);
		SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_SET, BN_CLICKED), (LPARAM)hWnd);
		SetTimer(hWnd, 777, 250, ToolBar_TimerProc);
	}
	else if(HitTest(B2X, B2Y, B2X + BCX, B2Y + BCY, nX, nY))
	{
		nButtonPressed = 2;
		RECT Rect = {B2X, B2Y, B2X + BCX, B2Y + BCY};
		InvalidateRect(hWnd, &Rect, FALSE);
		UpdateWindow(hWnd);
		SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_DATE, BN_CLICKED), (LPARAM)hWnd);
		SetTimer(hWnd, 777, 250, ToolBar_TimerProc);
	}
	else if(HitTest(B3X, B3Y, B3X + BCX, B3Y + BCY, nX, nY))
	{
		nButtonPressed = 3;
		RECT Rect = {B3X, B3Y, B3X + BCX, B3Y + BCY};
		InvalidateRect(hWnd, &Rect, FALSE);
		UpdateWindow(hWnd);
		SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_ALARM, BN_CLICKED), (LPARAM)hWnd);
		SetTimer(hWnd, 777, 250, ToolBar_TimerProc);
	}
	else if(HitTest(B4X, B4Y, B4X + BCX, B4Y + BCY, nX, nY))
	{
		nButtonPressed = 4;
		RECT Rect = {B4X, B4Y, B4X + BCX, B4Y + BCY};
		InvalidateRect(hWnd, &Rect, FALSE);
		UpdateWindow(hWnd);
		SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_ALTM, BN_CLICKED), (LPARAM)hWnd);
		SetTimer(hWnd, 777, 250, ToolBar_TimerProc);
	}
}

void ToolBar_OnMouseMove(HWND hWnd, int nX, int nY, UINT uKeyFlags)
{
	switch(nButtonPressed)
	{
		case 0:
		{
			break;
		}
		case 1:
		{
			if(!HitTest(B1X, B1Y, B1X + BCX, B1Y + BCY, nX, nY))
			{
				nLastButtonPressed = nButtonPressed;
				nButtonPressed = 0;
				RECT Rect = {B1X, B1Y, B1X + BCX, B1Y + BCY};
				InvalidateRect(hWnd, &Rect, FALSE);
				UpdateWindow(hWnd);
				KillTimer(hWnd, 777);
			}
			return;
		}
		case 2:
		{
			if(!HitTest(B2X, B2Y, B2X + BCX, B2Y + BCY, nX, nY))
			{
				nLastButtonPressed = nButtonPressed;
				nButtonPressed = 0;
				RECT Rect = {B2X, B2Y, B2X + BCX, B2Y + BCY};
				InvalidateRect(hWnd, &Rect, FALSE);
				UpdateWindow(hWnd);
				KillTimer(hWnd, 777);
			}
			return;
		}
		case 3:
		{
			if(!HitTest(B3X, B3Y, B3X + BCX, B3Y + BCY, nX, nY))
			{
				nLastButtonPressed = nButtonPressed;
				nButtonPressed = 0;
				RECT Rect = {B3X, B3Y, B3X + BCX, B3Y + BCY};
				InvalidateRect(hWnd, &Rect, FALSE);
				UpdateWindow(hWnd);
				KillTimer(hWnd, 777);
			}
			return;
		}
		case 4:
		{
			if(!HitTest(B4X, B4Y, B4X + BCX, B4Y + BCY, nX, nY))
			{
				nLastButtonPressed = nButtonPressed;
				nButtonPressed = 0;
				RECT Rect = {B4X, B4Y, B4X + BCX, B4Y + BCY};
				InvalidateRect(hWnd, &Rect, FALSE);
				UpdateWindow(hWnd);
				KillTimer(hWnd, 777);
			}
			return;
		}
	}

	if(!(fCapture && (uKeyFlags == MK_LBUTTON)))
		return;

	if(HitTest(B1X, B1Y, B1X + BCX, B1Y + BCY, nX, nY) && (nLastButtonPressed == 1))
	{
		nButtonPressed = 1;
		RECT Rect = {B1X, B1Y, B1X + BCX, B1Y + BCY};
		InvalidateRect(hWnd, &Rect, FALSE);
		UpdateWindow(hWnd);
		SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_SET, BN_CLICKED), (LPARAM)hWnd);
		SetTimer(hWnd, 777, 250, ToolBar_TimerProc);
	}
	else if(HitTest(B2X, B2Y, B2X + BCX, B2Y + BCY, nX, nY) && (nLastButtonPressed == 2))
	{
		nButtonPressed = 2;
		RECT Rect = {B2X, B2Y, B2X + BCX, B2Y + BCY};
		InvalidateRect(hWnd, &Rect, FALSE);
		UpdateWindow(hWnd);
		SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_DATE, BN_CLICKED), (LPARAM)hWnd);
		SetTimer(hWnd, 777, 250, ToolBar_TimerProc);
	}
	else if(HitTest(B3X, B3Y, B3X + BCX, B3Y + BCY, nX, nY) && (nLastButtonPressed == 3))
	{
		nButtonPressed = 3;
		RECT Rect = {B3X, B3Y, B3X + BCX, B3Y + BCY};
		InvalidateRect(hWnd, &Rect, FALSE);
		UpdateWindow(hWnd);
		SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_ALARM, BN_CLICKED), (LPARAM)hWnd);
		SetTimer(hWnd, 777, 250, ToolBar_TimerProc);
	}
	else if(HitTest(B4X, B4Y, B4X + BCX, B4Y + BCY, nX, nY) && (nLastButtonPressed == 4))
	{
		nButtonPressed = 4;
		RECT Rect = {B4X, B4Y, B4X + BCX, B4Y + BCY};
		InvalidateRect(hWnd, &Rect, FALSE);
		UpdateWindow(hWnd);
		SendMessage(g_AppData.hMainWnd, WM_COMMAND, MAKELONG(ID_ALTM, BN_CLICKED), (LPARAM)hWnd);
		SetTimer(hWnd, 777, 250, ToolBar_TimerProc);
	}
}

HCURSOR g_hCursor = NULL;

BOOL ToolBar_OnSetCursor(HWND hWnd, HWND hwndCursor, UINT codeHitTest, UINT nMsg)
{
	if(g_hCursor)
		SetCursor(g_hCursor);
	return TRUE;
}


static HWND s_hWnd = NULL;

LRESULT CALLBACK ToolBar_WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	if(hWnd == s_hWnd)
	{
		switch(uMsg)
		{
			HANDLE_MSG(hWnd, WM_PAINT, ToolBar_OnPaint);
			HANDLE_MSG(hWnd, WM_LBUTTONDOWN, ToolBar_OnLButtonDown);
			HANDLE_MSG(hWnd, WM_LBUTTONUP, ToolBar_OnLButtonUp);
			HANDLE_MSG(hWnd, WM_MOUSEMOVE, ToolBar_OnMouseMove);
			HANDLE_MSG(hWnd, WM_SETCURSOR, ToolBar_OnSetCursor);
		}
	}
	return CallWindowProc(oldWndPros, hWnd, uMsg, wParam, lParam);
}

inline void GetWindowPos(HWND hParentWnd, HWND hChildWnd, LPRECT lpRect)
{
	GetWindowRect(hChildWnd, lpRect);
	ScreenToClient(hParentWnd, (LPPOINT)lpRect);
	ScreenToClient(hParentWnd, ((LPPOINT)lpRect)+1);
}

BOOL InitToolBar(HWND hWnd)
{
	if(oldWndPros || !hWnd)
		return FALSE;

	s_hWnd = hWnd;
	oldWndPros = (WNDPROC)SetWindowLong(hWnd, GWL_WNDPROC, (LONG)ToolBar_WndProc);
	
	SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, TCX, TCY, SWP_NOMOVE | SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOACTIVATE);

	RECT Rect, ParentRect;
	GetClientRect(GetParent(hWnd), &ParentRect);
	GetWindowPos(GetParent(hWnd), hWnd, &Rect);
	SetWindowPos(hWnd, HWND_NOTOPMOST, ((ParentRect.right - ParentRect.left) / 2) - ((Rect.right - Rect.left) / 2),
		Rect.top, 0, 0, SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOZORDER | SWP_NOACTIVATE);

	return TRUE;
}

BOOL FreeToolBar(HWND hWnd)
{
	if(!oldWndPros || !hWnd)
		return FALSE;

	SetWindowLong(hWnd, GWL_WNDPROC, (LONG)oldWndPros);
	return TRUE;
}

BOOL CenterWindow(HWND hwndChild, HWND hwndParent)
{ 
    RECT    rcChild, rcParent;
    int     cxChild, cyChild, cxParent, cyParent;
    int     cxScreen, cyScreen, xNew, yNew;
    HDC     hdc;

    // Get the Height and Width of the child window
    GetWindowRect(hwndChild, &rcChild);
    cxChild = rcChild.right - rcChild.left;
    cyChild = rcChild.bottom - rcChild.top;


    if (hwndParent)
    {
    // Get the Height and Width of the parent window
       GetWindowRect(hwndParent, &rcParent);
       cxParent = rcParent.right - rcParent.left;
       cyParent = rcParent.bottom - rcParent.top;
    }

    else
    {
       cxParent = GetSystemMetrics (SM_CXSCREEN);
       cyParent = GetSystemMetrics (SM_CYSCREEN);
       rcParent.left = 0;
       rcParent.top  = 0;
       rcParent.right = cxParent;
       rcParent.bottom= cyParent;
    }

    // Get the display limits
    hdc = GetDC(hwndChild);
    cxScreen = GetDeviceCaps(hdc, HORZRES);
    cyScreen = GetDeviceCaps(hdc, VERTRES);
    ReleaseDC(hwndChild, hdc);

    // Calculate new X position, then adjust for screen
    xNew = rcParent.left + ((cxParent - cxChild) / 2);
    if (xNew < 0)
    {
        xNew = 0;
    }
    else if ((xNew + cxChild) > cxScreen)
    {
        xNew = cxScreen - cxChild;
    }

    // Calculate new Y position, then adjust for screen
    yNew = rcParent.top  + ((cyParent - cyChild) / 2);
    if (yNew < 0)
    {
        yNew = 0;
    }
    else if ((yNew + cyChild) > cyScreen)
    {
        yNew = cyScreen - cyChild;
    }

    // Set it, and return
    return SetWindowPos(hwndChild,
                        NULL,
                        xNew, yNew,
                        0, 0,
                        SWP_NOSIZE | SWP_NOZORDER);
}
