summaryrefslogtreecommitdiff
path: root/plugins/Chat/colorchooser.cpp
blob: 0d2642165f9c2421cf5bf2be89fee267aaad3bca (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
/*
Chat module plugin for Miranda IM

Copyright (C) 2003 Jörgen Persson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


// this color chooser window is inspired by PeaCow's smiley chooser window for the Smileyadd plugin

#include "chat.h"
#include <math.h>

static int CalculateCoordinatesToButton(COLORCHOOSER * pCC, POINT pt)
{
	int iSquareRoot = (int)sqrt((double)pCC->pModule->nColorCount);
	int nCols = iSquareRoot * iSquareRoot < pCC->pModule->nColorCount?iSquareRoot+1:iSquareRoot;

	int col = pt.x / 25;
	int row = (pt.y-20) / 20;
	int pos = nCols * row + col;

	if (pt.y < 20 && pos >= pCC->pModule->nColorCount) 
		pos = -1;

	return pos;
}

static RECT CalculateButtonToCoordinates(COLORCHOOSER * pCC, int buttonPosition)
{
	RECT pt;
	int iSquareRoot = (int)sqrt((double)pCC->pModule->nColorCount);
	int nCols = iSquareRoot * iSquareRoot < pCC->pModule->nColorCount?iSquareRoot+1:iSquareRoot;

	int row = buttonPosition / nCols;
	int col = buttonPosition % nCols;

	pt.left = col * 25+1;
	pt.top = row * 20 + 20;
	pt.right = pt.left + 25-1;
	pt.bottom = pt.top + 20;

	return pt;
}

INT_PTR CALLBACK DlgProcColorToolWindow(HWND hwndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static COLORCHOOSER* pCC = NULL;
	static int iCurrentHotTrack;
	static BOOL bChoosing;
	static int iRows;
	static int iColumns;
	static HWND hPreviousActiveWindow;

	switch(msg) {
	case WM_INITDIALOG:
		{
			RECT rc;
			int iSquareRoot;
			int width ;
			int height;

			TranslateDialogDefault(hwndDlg);
			pCC = (COLORCHOOSER*) lParam;

			iCurrentHotTrack = -2;
			bChoosing = FALSE;

			iSquareRoot = (int)sqrt((double)pCC->pModule->nColorCount);

			iColumns = iSquareRoot * iSquareRoot == pCC->pModule->nColorCount?iSquareRoot:iSquareRoot+1;
			iRows = iSquareRoot;

			rc.top = rc.left = 100;
			rc.right =  100 +  iColumns * 25 + 1;
			rc.bottom = iRows * 20 + 100 + 20;

			AdjustWindowRectEx(&rc, GetWindowLongPtr(hwndDlg, GWL_STYLE), FALSE, GetWindowLongPtr(hwndDlg, GWL_EXSTYLE));

			width = rc.right - rc.left;
			height = rc.bottom - rc.top;

			pCC->yPosition -= height;


			SetDlgItemText(hwndDlg, IDC_COLORTEXT, pCC->bForeground?TranslateT("Text colour"):TranslateT("Background colour"));
			SetWindowPos(GetDlgItem(hwndDlg, IDC_COLORTEXT), NULL,  0, 0, width, 20, 0);
			SetWindowPos(hwndDlg, NULL, pCC->xPosition, pCC->yPosition, width, height, SWP_SHOWWINDOW);
		}
		break;

	case WM_CTLCOLOREDIT:
	case WM_CTLCOLORSTATIC:
		if (( HWND )lParam == GetDlgItem( hwndDlg, IDC_COLORTEXT )) {
			SetTextColor((HDC)wParam,RGB(60,60,150));
			SetBkColor((HDC)wParam,GetSysColor(COLOR_WINDOW));
			return (INT_PTR)GetSysColorBrush(COLOR_WINDOW);
		}
		break;

	case WM_COMMAND:
		switch ( LOWORD( wParam )) {
		case IDOK:
			if (iCurrentHotTrack >= 0)
				PostMessage(hwndDlg, WM_LBUTTONUP, 0, 0);
			break;
		case IDCANCEL:
			DestroyWindow(hwndDlg);
			break;
		}
		break;

	case WM_LBUTTONUP:
		if (iCurrentHotTrack >= 0 && iCurrentHotTrack < pCC->pModule->nColorCount && pCC->hWndTarget != NULL) {
			HWND hWindow;
			CHARFORMAT2 cf;
			cf.cbSize = sizeof(CHARFORMAT2);
			cf.dwMask = 0;
			cf.dwEffects = 0;
			hWindow = GetParent( pCC->hWndTarget );

			if ( pCC->bForeground ) {
				pCC->si->bFGSet = TRUE;
				pCC->si->iFG = iCurrentHotTrack;
				if ( IsDlgButtonChecked( hWindow, IDC_COLOR )) {
					cf.dwMask = CFM_COLOR;
					cf.crTextColor = pCC->pModule->crColors[iCurrentHotTrack];
					SendMessage(pCC->hWndTarget, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
				}
			}
			else {
				pCC->si->bBGSet = TRUE;
				pCC->si->iBG = iCurrentHotTrack;
				if ( IsDlgButtonChecked( hWindow, IDC_BKGCOLOR )) {
					cf.dwMask = CFM_BACKCOLOR;
					cf.crBackColor = pCC->pModule->crColors[iCurrentHotTrack];
					SendMessage(pCC->hWndTarget, EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM)&cf);
		}	}	}
		PostMessage(hwndDlg, WM_CLOSE, 0, 0);
		break;

	case WM_ACTIVATE:
		if (wParam == WA_INACTIVE)
			PostMessage(hwndDlg, WM_CLOSE, 0, 0);
		else if ((wParam == WA_ACTIVE) || (wParam == WA_CLICKACTIVE))
			hPreviousActiveWindow = (HWND)lParam;
		break;

	case WM_MOUSEMOVE:
		{
			HDC hdc = GetDC(hwndDlg);
			POINT pt;
			RECT rect;
			int but;

			pt.x = LOWORD(lParam);
			pt.y = HIWORD(lParam);

			if (iCurrentHotTrack == -2) 
				return 0; // prevent focussing when not drawn yet!

			but = CalculateCoordinatesToButton(pCC, pt);

			// weird stuff
			if (but != iCurrentHotTrack) {
				if (iCurrentHotTrack >= 0) {
					rect = CalculateButtonToCoordinates(pCC, iCurrentHotTrack);
					DrawFocusRect(hdc, &rect);
					iCurrentHotTrack = -1;
				}
				iCurrentHotTrack = but;

				if (iCurrentHotTrack >= 0) {
					rect = CalculateButtonToCoordinates(pCC, iCurrentHotTrack);
					DrawFocusRect(hdc, &rect);
			}	}
			ReleaseDC(hwndDlg, hdc);
		}
		break;

	case WM_PAINT:
		{
			PAINTSTRUCT ps; 
			HDC hdc;
			RECT rc;
			int i = 0;
			int iThisRow = 1;
			int iThisColumn = 0;

			GetClientRect(hwndDlg, &rc);

			rc.top += 20;

			hdc = BeginPaint(hwndDlg, &ps);

			// fill background
			FillRect(hdc, &rc, GetSysColorBrush(COLOR_WINDOW));

			for (i; i < pCC->pModule->nColorCount; i++) {
				HBRUSH hbr;

				// decide place to draw the color block in the window
				iThisColumn ++;
				if (iThisColumn > iColumns) {
					iThisColumn = 1;
					iThisRow++;
				}

				if ( pCC->bForeground && pCC->si->bFGSet && pCC->si->iFG == i ||
					 !pCC->bForeground && pCC->si->bBGSet && pCC->si->iBG == i ) {
					rc.top = (iThisRow-1) * 20+ 1 +20 ;
					rc.left = (iThisColumn-1) * 25 + 1 + 1 ;
					rc.bottom = iThisRow * 20- 1 + 20 ;
					rc.right = iThisColumn * 25-1 ;

					DrawEdge(hdc, &rc, EDGE_RAISED, BF_TOP|BF_LEFT|BF_RIGHT|BF_BOTTOM);
				}

				rc.top = (iThisRow-1) * 20+ 3 +20 ;
				rc.left = (iThisColumn-1) * 25 + 3 + 1 ;
				rc.bottom = iThisRow * 20- 3 + 20 ;
				rc.right = iThisColumn * 25-3 ;

				FillRect(hdc, &rc, (HBRUSH)GetStockObject(BLACK_BRUSH));

				hbr = CreateSolidBrush(pCC->pModule->crColors[i]);

				rc.top = (iThisRow-1) * 20+4 +20;
				rc.left = (iThisColumn-1) * 25+ 4 + 1;
				rc.bottom = iThisRow * 20-4 + 20;
				rc.right = iThisColumn * 25-4;

				FillRect(hdc, &rc, hbr);
				DeleteObject(hbr);
			}

			EndPaint(hwndDlg, &ps); 
			iCurrentHotTrack = -1;
		}
		break;

	case WM_CLOSE:
		SetFocus(pCC->hWndTarget);
		DestroyWindow(hwndDlg);
		break;

	case WM_DESTROY:
		mir_free( pCC );
		return TRUE;
	}

	return FALSE;
}