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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
|
/*
"Spam Filter"-Plugin for Miranda IM
Copyright 2003-2006 Heiko Herkenrath
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 ("SpamFilter-License.txt"); if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// -- Includes
#include "common.h"
// -- Defines
#define PROP_FILEEDIT_ASSO_BTN _T("FileEditBrowse_AssociatedButton")
// -----------------------------------------
/*
BOOL ResizeWindow(HWND hWnd, int cx, int cy, BOOL bRepaint)
{
RECT rc;
if (GetWindowRect(hWnd, &rc))
return SetWindowPos(hWnd, NULL, 0, 0, (rc.right-rc.left)+cx, (rc.bottom-rc.top)+cy, SWP_NOSIZE|SWP_NOZORDER|SWP_NOOWNERZORDER|SWP_NOACTIVATE|(bRepaint?0:SWP_NOREDRAW));
else
return FALSE;
}
*/
int GetCtrlTextExtent(HWND hwndCtrl, const WCHAR* pszText, BOOL bAddAveChar)
{
HDC hDC;
HFONT hFont;
SIZE size;
HGDIOBJ hOldObj;
if (!hwndCtrl || !pszText) return 0;
hFont = (HFONT)SendMessage(hwndCtrl, WM_GETFONT, 0, 0);
hDC = GetDC(hwndCtrl);
if (!hDC) return 0;
// If non-system font used select it first
if (hFont)
hOldObj = SelectObject(hDC, hFont);
else
hOldObj = NULL;
if (!GetTextExtentPoint32(hDC, pszText, lstrlen(pszText), &size))
size.cx = 0;
if (hFont)
SelectObject(hDC, hOldObj);
ReleaseDC(hwndCtrl, hDC);
if (bAddAveChar)
{
TEXTMETRIC tm;
if (GetTextMetrics(hDC, &tm))
size.cx += tm.tmAveCharWidth;
}
return size.cx;
}
BOOL ShowDlgItem(HWND hwndDlg, int iIDCtrl, BOOL bShow)
{
HWND hwndCtrl = GetDlgItem(hwndDlg, iIDCtrl);
if (!hwndCtrl) return FALSE;
// Avoid flickering
if (bShow && IsWindowVisible(hwndCtrl))
return TRUE;
return ShowWindow(hwndCtrl, (bShow ? SW_SHOW : SW_HIDE));
}
BOOL EnableDlgItem(HWND hwndDlg, int iIDCtrl, BOOL bEnable)
{
HWND hwndCtrl = GetDlgItem(hwndDlg, iIDCtrl);
if (!hwndCtrl) return FALSE;
// Avoid flickering
if (IsWindowEnabled(hwndCtrl) == bEnable)
return (bEnable == FALSE);
return EnableWindow(hwndCtrl, bEnable);
}
/*
BOOL SetDlgItemPrintf(HWND hwndDlg, int iIDCtrl, unsigned int cbArgMaxLen, ...)
{
HWND hwndCtrl;
BOOL bReturn;
int iMaxLen;
TCHAR* pszFmt;
TCHAR* pszOut;
va_list arglist;
hwndCtrl = GetDlgItem(hwndDlg, iIDCtrl);
if (!hwndCtrl) return FALSE;
// Prepare fmt buffer
iMaxLen = GetWindowTextLength(hwndCtrl);
pszFmt = (TCHAR*)mir_alloc((iMaxLen+1)*sizeof(TCHAR));
if (!pszFmt) return FALSE;
// Get fmt text
if (GetWindowText(hwndCtrl, pszFmt, iMaxLen+1) == 0)
{
mir_free(pszFmt);
return FALSE;
}
// Prepare out buffer
if (cbArgMaxLen > 0) iMaxLen = iMaxLen + cbArgMaxLen;
pszOut = (TCHAR*)mir_alloc((iMaxLen+1)*sizeof(TCHAR));
if (!pszOut)
{
mir_free(pszFmt);
return FALSE;
}
// Generate out text
va_start(arglist, cbArgMaxLen);
mir_vsnprintf(pszOut, iMaxLen+1, pszFmt, arglist);
va_end(arglist);
mir_free(pszFmt);
bReturn = SetWindowText(hwndCtrl, pszOut);
mir_free(pszOut);
return bReturn;
}
*/
BOOL SetSpinCtrlRange(HWND hwndSpin, int iRangeMin, int iRangeMax, int iDefault, BOOL bForceDefault)
{
HWND hwndDlg, hwndEdit;
int iIDEdit;
int iOldValue, iNewValue;
int iOldRangeMin, iOldRangeMax;
BOOL bIsEnabled;
hwndDlg = GetParent(hwndSpin);
if (!hwndDlg) return FALSE;
hwndEdit = (HWND)SendMessage(hwndSpin, UDM_GETBUDDY, 0 , 0);
if (!hwndEdit || (GetParent(hwndEdit) != hwndDlg))
return FALSE;
iIDEdit = GetDlgCtrlID(hwndEdit);
iOldValue = (int)GetDlgItemInt(hwndDlg, iIDEdit, NULL, TRUE);
//if (iRangeMin < UD_MINVAL) iRangeMin = UD_MINVAL;
//if (iRangeMax > UD_MAXVAL) iRangeMax = UD_MAXVAL;
// Get data
if (iRangeMax < iRangeMin) iRangeMax = iRangeMin;
bIsEnabled = (iRangeMin != iRangeMax);
SendMessage(hwndSpin, UDM_GETRANGE32, (WPARAM)&iOldRangeMin, (LPARAM)&iOldRangeMax);
// Set default if necessary
if (bForceDefault
|| ((iOldValue==iOldRangeMax) && (iDefault > iOldRangeMax))
|| ((iOldValue==iOldRangeMin) && (iDefault < iOldRangeMin))
|| (bIsEnabled && !IsWindowEnabled(hwndSpin) && !IsWindowEnabled(hwndEdit)) )
iNewValue = iDefault;
else
iNewValue = iOldValue;
if (iNewValue < iRangeMin) iNewValue = iRangeMin;
if (iNewValue > iRangeMax) iNewValue = iRangeMax;
// Set edit
if (iNewValue != iOldValue)
SetDlgItemInt(hwndDlg, iIDEdit, iNewValue, TRUE);
// Set spin
if ((iRangeMin != iOldRangeMin) || iRangeMax != iOldRangeMax)
{
WCHAR szRangeMin[MAX_INT_LENGTH];
WCHAR szRangeMax[MAX_INT_LENGTH];
mir_sntprintf(szRangeMin, ARRAYSIZE(szRangeMin), _T("%i"), iRangeMin);
mir_sntprintf(szRangeMax, ARRAYSIZE(szRangeMax), _T("%i"), iRangeMax);
SendMessage(hwndSpin, UDM_SETRANGE32, (WPARAM)iRangeMin, (LPARAM)iRangeMax);
PostMessage(hwndEdit, EM_SETLIMITTEXT, (WPARAM)max(lstrlen(szRangeMin), lstrlen(szRangeMax)), 0);
}
if (iNewValue != iOldValue)
SendMessage(hwndSpin, UDM_SETPOS32, 0, (LPARAM)iNewValue);
// Avoiding flicker
if (IsWindowEnabled(hwndSpin) != bIsEnabled) EnableWindow(hwndSpin, bIsEnabled);
if (IsWindowEnabled(hwndEdit) != bIsEnabled) EnableWindow(hwndEdit, bIsEnabled);
return bIsEnabled;
}
LRESULT CALLBACK PathEditSubclassProc(HWND hwndEdit, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
LRESULT lResult = CallWindowProc((WNDPROC)GetWindowLongPtr(hwndEdit, GWLP_USERDATA), hwndEdit, uMsg, wParam, lParam);
switch(uMsg)
{
case WM_NCCALCSIZE:
{
if ((BOOL)wParam == TRUE)
{
// Resizing the client rect of the edit control
// and moving the button in the space.
HWND hwndButton = (HWND)GetProp(hwndEdit, PROP_FILEEDIT_ASSO_BTN);
NCCALCSIZE_PARAMS* pnccsp = (NCCALCSIZE_PARAMS*)lParam;
RECT rc;
if (hwndButton)
if (GetWindowRect(hwndButton, &rc))
{
rc.right -= rc.left;
rc.bottom = pnccsp->rgrc[0].bottom-pnccsp->rgrc[0].top;
pnccsp->rgrc[0].right -= (rc.right+1);
rc.left = pnccsp->rgrc[0].right+1;
rc.top = pnccsp->rgrc[0].top;
MoveWindow(hwndButton, rc.left, rc.top, rc.right, rc.bottom, TRUE);
}
}
break;
}
case WM_NCHITTEST:
{
// When the mouse is hovering the button
// HTTRANSPARENT needs to be returned
// else no mouse messages will be sent to the button
if (lResult == HTNOWHERE)
{
HWND hwndButton = (HWND)GetProp(hwndEdit, PROP_FILEEDIT_ASSO_BTN);
RECT rc;
POINT pt;
if (hwndButton)
if (GetWindowRect(hwndButton, &rc))
{
POINTSTOPOINT(pt, lParam);
if (PtInRect(&rc, pt))
lResult = HTTRANSPARENT;
}
}
break;
}
case WM_DROPFILES:
{
HDROP hDrop = (HDROP)wParam;
WCHAR szFileName[MAX_PATH];
DragQueryFile(hDrop, 0, szFileName, ARRAYSIZE(szFileName));
// Insert the text and Mark it
SetWindowText(hwndEdit, szFileName);
DragFinish(hDrop);
// Set focus to file editbox (SetFocus is not allowed in Dialogs)
if (GetParent(hwndEdit))
PostMessage(GetParent(hwndEdit), WM_NEXTDLGCTL, (WPARAM)hwndEdit, (LPARAM)TRUE);
break;
}
case WM_DESTROY: // before the destroy
{
// Remove assoicated browse button
RemoveProp(hwndEdit, PROP_FILEEDIT_ASSO_BTN);
break;
}
case WM_NCDESTROY: // after the destroy
{
// Uninit auto complete
if (GetProcAddress(GetModuleHandle(_T("SHLWAPI")), "SHAutoComplete"))
CoUninitialize();
break;
}
case WM_CHAR:
{
// Only allow valid path chars to be entered
if (PathGetCharType((WCHAR)wParam)&GCT_INVALID)
return FALSE;
else
break;
}
}
return lResult;
}
BOOL MakePathEditCtrl(HWND hwndEdit, HWND hwndButton)
{
if (!hwndEdit || !hwndButton) return FALSE;
// Don't make a new mask if there is already one available
if((WNDPROC)GetWindowLongPtr(hwndEdit, GWLP_WNDPROC) == PathEditSubclassProc)
return FALSE;
// Remember associated button control
if (!SetProp(hwndEdit, PROP_FILEEDIT_ASSO_BTN, (HANDLE)hwndButton))
{
ShowWindow(hwndButton, SW_HIDE);
return FALSE;
}
// Ansi/OEM codepage converting
#if !defined(UNICODE)
// Allow later converting to OEM encoding for some functions (see docs)
SetWindowLongPtr(hwndEdit, GWL_STYLE, ES_OEMCONVERT|GetWindowLongPtr(hwndEdit, GWL_STYLE));
#endif
// Limit text
PostMessage(hwndEdit, EM_SETLIMITTEXT, (WPARAM)MAX_PATH, 0);
// Subclass
SetWindowLongPtr(hwndEdit, GWLP_USERDATA, SetWindowLongPtr(hwndEdit, GWLP_WNDPROC, (LONG_PTR)PathEditSubclassProc));
// Init auto complete
{
HRESULT (STDAPICALLTYPE *MySHAutoComplete)(HWND, DWORD);
*(FARPROC*)&MySHAutoComplete = GetProcAddress(GetModuleHandle(_T("SHLWAPI")), "SHAutoComplete");
if (MySHAutoComplete) {
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
MySHAutoComplete(hwndEdit, SHACF_FILESYSTEM|SHACF_FILESYS_ONLY|SHACF_USETAB);
} else OutputDebugString(_T("Spam Filter: SHAutoComplete in SHLWAPI.DLL not available.\r\n"));
}
// Accept drag & drop
DragAcceptFiles(hwndEdit, TRUE);
{
RECT rcEdit;
RECT rcButton;
if (GetClientRect(hwndEdit, &rcEdit))
if (GetClientRect(hwndButton, &rcButton))
{
// Send WM_NCCALCSIZE message to edit control and resize it to fit button
SetWindowPos(hwndEdit, NULL, 0, 0, (rcEdit.right-rcEdit.left), (rcButton.bottom-rcButton.top)+2*GetSystemMetrics(SM_CYBORDER), SWP_NOMOVE|SWP_NOZORDER|SWP_DRAWFRAME);
}
}
return TRUE;
}
|