1 /*
2  * colorsel.c -- TODAY for Windows - color selector
3  *
4  * Copyright (C) 2003 by Yoshifumi Mori
5  *
6  * tab:4
7  */
8 
9 #include "cdefs.h"
10 #include "extern.h"
11 #if !defined(INCLUDE_CALENDAR)
12 #include "todayfw.rh"
13 #else
14 #include "calfw.rh"
15 #endif
16 #include "commres.rh"
17 
18 #define MAX_COLOR_COLUMNS	8
19 #define MAX_COLOR_ROWS		(MaxColorMap / MAX_COLOR_COLUMNS)
20 
21 static HINSTANCE	ghInstance;
22 static int	MaxColorMap;
23 static int	*ColorMapTbl;
24 static int	iSelNo;
25 static HWND	hwndParent;
26 static WNDPROC	lpfnOldDlgProc;
27 
28 static
drawSelectMark(HWND hwnd,int no,BOOL flag)29 void drawSelectMark(HWND hwnd, int no, BOOL flag)
30 {
31 	HWND	hwndItem;
32 	RECT	rcItem;
33 	RECT	rcMark;
34 	POINT	pt;
35 	HDC	hdc;
36 	HBRUSH	hBrush;
37 
38 	hwndItem = GetDlgItem(hwnd, IDC_COLORSELNO_00 + no);
39 	GetWindowRect(hwndItem, &rcItem);
40 	pt.x = rcItem.left;
41 	pt.y = rcItem.top;
42 	ScreenToClient(hwnd, &pt);
43 	rcMark.left = pt.x;
44 	rcMark.top = pt.y;
45 	pt.x = rcItem.right;
46 	pt.y = rcItem.bottom;
47 	ScreenToClient(hwnd, &pt);
48 	rcMark.right = pt.x;
49 	rcMark.bottom = pt.y;
50 	InflateRect(&rcMark, 2, 2);
51 
52 	hdc = GetDC(hwnd);
53 	if (flag == TRUE) {
54 		hBrush = GetStockObject(BLACK_BRUSH);
55 	} else {
56 		hBrush = (HBRUSH)SendMessage(hwnd, WM_CTLCOLORDLG, (WPARAM)hdc, (LPARAM)hwndItem);
57 	}
58 	FrameRect(hdc, &rcMark, hBrush);
59 	ReleaseDC(hwnd, hdc);
60 }
61 
subStaticWndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)62 LRESULT CALLBACK subStaticWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
63 {
64 	int	newiSelNo;
65 	int	col;
66 	int	row;
67 	int	step_col;
68 	int	step_row;
69 
70 	switch (uMsg) {
71 	case WM_DESTROY:
72 		SetWindowLong(hwnd, GWL_WNDPROC, (DWORD)lpfnOldDlgProc);
73 		break;
74 	case WM_GETDLGCODE:
75 		return (DLGC_WANTARROWS);
76 	case WM_KEYDOWN:
77 		switch (wParam) {
78 		case VK_UP:
79 			step_row = -1;
80 			step_col = 0;
81 			goto move_cursor;
82 		case VK_DOWN:
83 			step_row = 1;
84 			step_col = 0;
85 			goto move_cursor;
86 		case VK_LEFT:
87 			step_row = 0;
88 			step_col = -1;
89 			goto move_cursor;
90 		case VK_RIGHT:
91 			step_row = 0;
92 			step_col = 1;
93 move_cursor:
94 			col = iSelNo % MAX_COLOR_COLUMNS;
95 			col = (col + step_col + MAX_COLOR_COLUMNS) % MAX_COLOR_COLUMNS;
96 			row = iSelNo / MAX_COLOR_COLUMNS;
97 			row = (row + step_row + MAX_COLOR_ROWS) % MAX_COLOR_ROWS;
98 			newiSelNo = col + row * MAX_COLOR_COLUMNS;
99 			if (iSelNo != newiSelNo) {
100 				drawSelectMark(hwndParent, iSelNo, FALSE);
101 				iSelNo = newiSelNo;
102 				drawSelectMark(hwndParent, iSelNo, TRUE);
103 			}
104 			return (TRUE);
105 		}
106 		break;
107 	}
108 
109 	return (CallWindowProc(lpfnOldDlgProc, hwnd, uMsg, wParam, lParam));
110 }
111 
112 /*
113  * ���������������(���ֹ�) main
114  */
selectColornoDlgProc(HWND hDlg,UINT iMessage,WPARAM wParam,LPARAM lParam)115 BOOL CALLBACK selectColornoDlgProc(HWND hDlg, UINT iMessage, WPARAM wParam, LPARAM lParam)
116 {
117 	static int	*ipSelNo;
118 	HWND	hwndItem;
119 	UINT	idCtrl;
120 	LPDRAWITEMSTRUCT	lpDIS;
121 	HBRUSH	hBrush;
122 
123 	switch (iMessage) {
124 	case WM_INITDIALOG:
125 		SetClassLong(hDlg, GCL_HICON, (LONG)LoadIcon(ghInstance, MAKEINTRESOURCE(IDI_ICON1)));
126 		ipSelNo = (int *)lParam;
127 		iSelNo = *ipSelNo;
128 		if (iSelNo < 0 || iSelNo >= MaxColorMap) {
129 			iSelNo = 0;
130 		}
131 		hwndParent = hDlg;
132 		hwndItem = GetDlgItem(hDlg, IDC_COLORSELNO_BASE);
133 		SetFocus(hwndItem);
134 		lpfnOldDlgProc = (WNDPROC)SetWindowLong(hwndItem, GWL_WNDPROC, (DWORD)subStaticWndProc);
135 		return (FALSE);
136 	case WM_DRAWITEM:
137 		idCtrl = (UINT)wParam;
138 		lpDIS = (LPDRAWITEMSTRUCT)lParam;
139 		if (idCtrl >= IDC_COLORSELNO_00 && idCtrl < (UINT)(IDC_COLORSELNO_00 + MaxColorMap)) {
140 			idCtrl -= IDC_COLORSELNO_00;
141 			hBrush = CreateSolidBrush(RGB2PALETTERGB(ColorMapTbl[idCtrl]));
142 			FillRect(lpDIS->hDC, &lpDIS->rcItem, hBrush);
143 			DeleteObject(hBrush);
144 			return (TRUE);
145 		} else if (idCtrl == IDC_COLORSELNO_BASE) {
146 			hBrush = (HBRUSH)SendMessage(hDlg, WM_CTLCOLORDLG, (WPARAM)lpDIS->hDC, (LPARAM)lpDIS->hwndItem);
147 			FillRect(lpDIS->hDC, &lpDIS->rcItem, hBrush);
148 			drawSelectMark(hDlg, iSelNo, TRUE);
149 			return (TRUE);
150 		}
151 		break;
152 	case WM_COMMAND:
153 		idCtrl = LOWORD(wParam);
154 		if (HIWORD(wParam) == STN_CLICKED &&
155 		    IDC_COLORSELNO_00 <= idCtrl && idCtrl < (UINT)(IDC_COLORSELNO_00 + MaxColorMap)) {
156 			idCtrl -= IDC_COLORSELNO_00;
157 			if (idCtrl != (UINT)iSelNo) {
158 				drawSelectMark(hDlg, iSelNo, FALSE);
159 				iSelNo = (int)idCtrl;
160 				drawSelectMark(hDlg, iSelNo, TRUE);
161 			}
162 			return (TRUE);
163 		} else {
164 			switch (idCtrl) {
165 			case IDOK:
166 				*ipSelNo = iSelNo;
167 				EndDialog(hDlg, TRUE);
168 				return (TRUE);
169 			case IDCANCEL:
170 				EndDialog(hDlg, FALSE);
171 				return (TRUE);
172 			}
173 		}
174 		break;
175 	case WM_CLOSE:
176 		EndDialog(hDlg, FALSE);
177 		return (TRUE);
178 	}
179 
180 	return (FALSE);
181 }
182 
183 /*
184  * ���������������(���ֹ�)
185  *
186  * ��������max_colormap �� 16 �������ݡ��Ȥ��Ƥ��ʤ���
187  */
SelectColorMapNo(HWND hwnd,HINSTANCE hInstance,int * colno,const int colormap[],const int max_colormap)188 BOOL SelectColorMapNo(HWND hwnd, HINSTANCE hInstance, int *colno, const int colormap[], const int max_colormap)
189 {
190 	BOOL	bResult;
191 
192 	ghInstance = hInstance;
193 
194 	MaxColorMap = max_colormap;
195 	ColorMapTbl = malloc(sizeof(int) * MaxColorMap);
196 	if (ColorMapTbl == NULL) {
197 		return (FALSE);
198 	}
199 	memcpy(ColorMapTbl, colormap, sizeof(int) * MaxColorMap);
200 
201 	bResult = DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_COLORSELNO), hwnd, (DLGPROC)selectColornoDlgProc, (LPARAM)colno);
202 
203 	free(ColorMapTbl);
204 
205 	return (bResult);
206 }
207