Debian Codegraph @

Contrib/InstallOptions/InstallerOptions.cpp

raw
1 /*********************************************************
2 *
3 * InstallOptions version 2.0 - Plugin for custom pages
4 *
5 * See Readme.html for documentation and license
6 *
7 * Unicode support by Jim Park -- 08/01/2007
8 *
9 *********************************************************/
10
11 #include <windows.h>
12 #include <windowsx.h>
13 #include <shlobj.h>
14 #include <commdlg.h>
15 #include <cderr.h>
16 #include <shellapi.h>
17 #include "resource.h"
18
19 #include <nsis/pluginapi.h> // nsis plugin
20
21 #define COUNTOF(a) (sizeof(a)/sizeof(a[0]))
22 #ifndef min
23 #include <algorithm>
24 #define min std::min // mingw64?
25 #endif
26
27 // Use for functions only called from one place to possibly reduce some code
28 // size. Allows the source code to remain readable by leaving the function
29 // intact.
30 #ifdef _MSC_VER
31 #define INLINE __forceinline
32 #else
33 #define INLINE inline
34 #endif
35
36 void *WINAPI MALLOC(int len) { return (void*)GlobalAlloc(GPTR,len); }
37 void WINAPI FREE(void *d) { if (d) GlobalFree((HGLOBAL)d); }
38
39 #define strcpy(x,y) lstrcpy(x,y)
40 //#define strncpy(x,y,z) lstrcpyn(x,y,z)
41 #define strdup(x) STRDUP(x)
42 #define stricmp(x,y) lstrcmpi(x,y)
43 //#define abs(x) ((x) < 0 ? -(x) : (x))
44
45 TCHAR *WINAPI STRDUP(const TCHAR *c)
46 {
47 TCHAR *t=(TCHAR*)MALLOC((lstrlen(c)+1)*sizeof(TCHAR));
48 return lstrcpy(t,c);
49 }
50
51 // Field types
52 // NB - the order of this list is important - see below
53
54 #define FIELD_INVALID (0)
55 #define FIELD_HLINE (1)
56 #define FIELD_VLINE (2)
57 #define FIELD_LABEL (3)
58 #define FIELD_ICON (4)
59 #define FIELD_BITMAP (5)
60 #define FIELD_BROWSEBUTTON (6)
61 #define FIELD_LINK (7)
62 #define FIELD_BUTTON (8)
63 #define FIELD_GROUPBOX (9)
64 #define FIELD_CHECKBOX (10)
65 #define FIELD_RADIOBUTTON (11)
66 #define FIELD_TEXT (12)
67 #define FIELD_FILEREQUEST (13)
68 #define FIELD_DIRREQUEST (14)
69 #define FIELD_COMBOBOX (15)
70 #define FIELD_LISTBOX (16)
71
72 #define FIELD_SETFOCUS FIELD_CHECKBOX // First field that qualifies for having the initial keyboard focus
73 #define FIELD_CHECKLEN FIELD_TEXT // First field to have length of state value checked against MinLen/MaxLen
74
75 //---------------------------------------------------------------------
76 // settings
77 #define IO_ENABLE_LINK
78
79 //#define IO_LINK_UNDERLINED // Uncomment to show links text underlined
80 //---------------------------------------------------------------------
81
82 // Flags
83
84 // LBS_NOTIFY 0x00000001 // LISTBOX/CHECKBOX/RADIOBUTTON/BUTTON/LINK - Notify NSIS script when control is "activated" (exact meaning depends on the type of control)
85 // OFN_OVERWRITEPROMPT 0x00000002 // FILEREQUEST
86 // OFN_HIDEREADONLY 0x00000004 // FILEREQUEST
87 // LBS_MULTIPLESEL 0x00000008 // LISTBOX
88 #define FLAG_READONLY 0x00000010 // TEXT/FILEREQUEST/DIRREQUEST
89 // BS_LEFTTEXT 0x00000020 // CHECKBOX/RADIOBUTTON
90 #define TRANSPARENT_BMP 0x00000020 // BITMAP
91 #define FLAG_PASSWORD 0x00000040 // TEXT/FILEREQUEST/DIRREQUEST
92 #define FLAG_ONLYNUMBERS 0x00000080 // TEXT/FILEREQUEST/DIRREQUEST
93 #define FLAG_MULTILINE 0x00000100 // TEXT/FILEREQUEST/DIRREQUEST
94 #define FLAG_NOWORDWRAP 0x00000200 // TEXT/FILEREQUEST/DIRREQUEST - Disable word-wrap in multi-line text boxes
95 #define FLAG_WANTRETURN 0x00000400 // TEXT/FILEREQUEST/DIRREQUEST
96 // LBS_EXTENDEDSEL 0x00000800 // LISTBOX
97 // OFN_PATHMUSTEXIST 0x00000800 // FILEREQUEST
98 // OFN_FILEMUSTEXIST 0x00001000 // FILEREQUEST
99 // OFN_CREATEPROMPT 0x00002000 // FILEREQUEST
100 #define FLAG_DROPLIST 0x00004000 // COMBOBOX
101 #define FLAG_RESIZETOFIT 0x00008000 // BITMAP
102 // WS_TABSTOP 0x00010000 // *ALL*
103 // WS_GROUP 0x00020000 // *ALL*
104 #define FLAG_SAVEAS 0x00040000 // FILEREQUEST - Show "Save As" instead of "Open" for FileRequest field
105 // OFN_EXPLORER 0x00080000 // FILEREQUEST
106 // WS_HSCROLL 0x00100000 // *ALL*
107 // WS_VSCROLL 0x00200000 // *ALL*
108 // WS_DISABLED 0x08000000 // *ALL*
109 #define FLAG_FOCUS 0x10000000 // Controls that can receive focus
110
111 struct TableEntry {
112 const TCHAR *pszName;
113 int nValue;
114 };
115
116 int WINAPI LookupToken(TableEntry*, TCHAR*);
117 int WINAPI LookupTokens(TableEntry*, TCHAR*);
118
119 void WINAPI ConvertNewLines(TCHAR *str);
120
121 // all allocated buffers must be first in the struct
122 // when adding more allocated buffers to FieldType, don't forget to change this define
123 #define FIELD_BUFFERS 6
124 struct FieldType {
125 TCHAR *pszText;
126 TCHAR *pszState;
127 TCHAR *pszRoot;
128
129 TCHAR *pszListItems;
130 TCHAR *pszFilter;
131
132 TCHAR *pszValidateText;
133 int nMinLength;
134 int nMaxLength;
135
136 int nType;
137 RECT rect;
138
139 int nFlags;
140
141 HWND hwnd;
142 UINT nControlID;
143
144 INT_PTR nParentIdx; // this is used to store original windowproc for LINK
145 HANDLE hImage; // this is used by image/icon field to save the handle to the image
146
147 int nField; // field number in INI file
148 const TCHAR *pszHwndEntry; // "HWND" or "HWND2"
149
150 WNDPROC wndProc;
151 };
152
153 // initial buffer size. buffers will grow as required.
154 // use a value larger than MAX_PATH to prevent need for excessive growing.
155 #define BUFFER_SIZE 8192 // 8kb of mem is max char count in multiedit
156
157 TCHAR szBrowseButtonCaption[] = _T("...");
158
159 HWND hConfigWindow = NULL;
160 HWND hMainWindow = NULL;
161 HWND hCancelButton = NULL;
162 HWND hNextButton = NULL;
163 HWND hBackButton = NULL;
164
165 HINSTANCE m_hInstance = NULL;
166
167 struct _stack_t *pFilenameStackEntry = NULL;
168
169 TCHAR *pszFilename = NULL;
170 TCHAR *pszTitle = NULL;
171 TCHAR *pszCancelButtonText = NULL;
172 TCHAR *pszNextButtonText = NULL;
173 TCHAR *pszBackButtonText = NULL;
174
175 int bBackEnabled = FALSE;
176 int bCancelEnabled = FALSE; // by ORTIM: 13-August-2002
177 int bCancelShow = FALSE; // by ORTIM: 13-August-2002
178
179 int bRTL = FALSE;
180
181 FieldType *pFields = NULL;
182 #define DEFAULT_RECT 1018
183 int nRectId = 0;
184 int nNumFields = 0;
185 int g_done;
186 int g_NotifyField; // Field number of notifying control
187
188 int WINAPI FindControlIdx(UINT id)
189 {
190 for (int nIdx = 0; nIdx < nNumFields; nIdx++)
191 if (id == pFields[nIdx].nControlID)
192 return nIdx;
193 return -1;
194 }
195
196 LRESULT WINAPI mySendMessage(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
197 {
198 return SendMessage(hWnd, Msg, wParam, lParam);
199 }
200
201 void WINAPI mySetFocus(HWND hWnd)
202 {
203 mySendMessage(hMainWindow, WM_NEXTDLGCTL, (WPARAM)hWnd, TRUE);
204 }
205
206 void WINAPI mySetWindowText(HWND hWnd, LPCTSTR pszText)
207 {
208 if (pszText)
209 SetWindowText(hWnd, pszText);
210 }
211
212 static COLORREF GetLinkColor()
213 {
214 COLORREF clr = GetSysColor(COLOR_HOTLIGHT);
215 #ifndef _WIN64
216 // COLOR_HOTLIGHT is Win98/2000+. GetSysColorBrush is the correct way to
217 // detect valid colors but here we just assume nobody uses black.
218 if (!clr) clr = RGB(0,0,255);
219 #endif
220 return clr;
221 }
222
223 int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) {
224 static TCHAR szDir[MAX_PATH];
225
226 if (uMsg == BFFM_INITIALIZED &&
227 GetWindowText(pFields[(int)pData].hwnd, szDir, MAX_PATH) > 0)
228 mySendMessage(hwnd, BFFM_SETSELECTION, TRUE, (LPARAM)szDir);
229 return 0;
230 }
231
232
233 bool INLINE ValidateFields() {
234 int nIdx;
235 INT_PTR nLength;
236
237 // In the unlikely event we can't allocate memory, go ahead and return true so we can get out of here.
238 // May cause problems for the install script, but no memory is problems for us.
239 for (nIdx = 0; nIdx < nNumFields; nIdx++) {
240 FieldType *pField = pFields + nIdx;
241 // this if statement prevents a stupid bug where a min/max length is assigned to a label control
242 // where the user obviously has no way of changing what is displayed. (can you say, "infinite loop"?)
243 if (pField->nType >= FIELD_CHECKLEN) {
244 nLength = mySendMessage(pField->hwnd, WM_GETTEXTLENGTH, 0, 0);
245
246 if (((pField->nMaxLength > 0) && (nLength > pField->nMaxLength)) ||
247 ((pField->nMinLength > 0) && (nLength < pField->nMinLength))) {
248 if (pField->pszValidateText) {
249 TCHAR szTitle[1024];
250 GetWindowText(hMainWindow, szTitle, COUNTOF(szTitle));
251 MessageBox(hConfigWindow, pField->pszValidateText, szTitle, MB_OK|MB_ICONWARNING);
252 }
253 mySetFocus(pField->hwnd);
254 return false;
255 }
256
257 }
258 }
259 return true;
260 }
261
262 bool WINAPI SaveSettings(void) {
263 static TCHAR szField[25];
264 int nBufLen = BUFFER_SIZE;
265 TCHAR *pszBuffer = (TCHAR*)MALLOC(nBufLen*sizeof(TCHAR));
266 if (!pszBuffer) return false;
267
268 int nIdx;
269 int CurrField;
270 for (nIdx = 0, CurrField = 1; nIdx < nNumFields; nIdx++, CurrField++) {
271 FieldType *pField = pFields + nIdx;
272 HWND hwnd = pField->hwnd;
273 switch (pField->nType) {
274 case FIELD_BROWSEBUTTON:
275 if (g_NotifyField > CurrField)
276 --g_NotifyField;
277 --CurrField;
278 default:
279 continue;
280
281 case FIELD_CHECKBOX:
282 case FIELD_RADIOBUTTON:
283 wsprintf(pszBuffer, _T("%d"), !!mySendMessage(hwnd, BM_GETCHECK, 0, 0));
284 break;
285
286 case FIELD_LISTBOX:
287 {
288 // Ok, this one requires a bit of work.
289 // First, we allocate a buffer long enough to hold every item.
290 // Then, we loop through every item and if it's selected we add it to our buffer.
291 // If there is already an item in the list, then we prepend a | character before the new item.
292 // We could simplify for single-select boxes, but using one piece of code saves some space.
293 int nLength = lstrlen(pField->pszListItems) + 10;
294 if (nLength > nBufLen) {
295 FREE(pszBuffer);
296 nBufLen = nLength;
297 pszBuffer = (TCHAR*)MALLOC(nBufLen*sizeof(TCHAR));
298 if (!pszBuffer) return false;
299 }
300 TCHAR *pszItem = (TCHAR*)MALLOC(nBufLen*sizeof(TCHAR));
301 if (!pszItem) return false;
302
303 *pszBuffer = _T('\0');
304 INT_PTR nNumItems = mySendMessage(hwnd, LB_GETCOUNT, 0, 0);
305 for (int nIdx2 = 0; nIdx2 < nNumItems; nIdx2++) {
306 if (mySendMessage(hwnd, LB_GETSEL, nIdx2, 0) > 0) {
307 if (*pszBuffer) lstrcat(pszBuffer, _T("|"));
308 mySendMessage(hwnd, LB_GETTEXT, (WPARAM)nIdx2, (LPARAM)pszItem);
309 lstrcat(pszBuffer, pszItem);
310 }
311 }
312
313 FREE(pszItem);
314 break;
315 }
316
317 case FIELD_TEXT:
318 case FIELD_FILEREQUEST:
319 case FIELD_DIRREQUEST:
320 case FIELD_COMBOBOX:
321 {
322 int nLength = (int) mySendMessage(pField->hwnd, WM_GETTEXTLENGTH, 0, 0);
323 if (nLength > nBufLen) {
324 FREE(pszBuffer);
325 // add a bit extra so we do this less often
326 nBufLen = nLength + 20;
327 pszBuffer = (TCHAR*)MALLOC(nBufLen*sizeof(TCHAR));
328 if (!pszBuffer) return false;
329 }
330 *pszBuffer=_T('"');
331 GetWindowText(hwnd, pszBuffer+1, nBufLen-1);
332 pszBuffer[nLength+1]=_T('"');
333 pszBuffer[nLength+2]=_T('\0');
334
335 if (pField->nType == FIELD_TEXT && (pField->nFlags & FLAG_MULTILINE))
336 {
337 TCHAR *pszBuf2 = (TCHAR*)MALLOC(nBufLen*2*sizeof(TCHAR)); // double the size, consider the worst case, all chars are \r\n
338 TCHAR *p1, *p2;
339 for (p1 = pszBuffer, p2 = pszBuf2; *p1; p1 = CharNext(p1), p2 = CharNext(p2))
340 {
341 switch (*p1) {
342 case _T('\t'):
343 *p2++ = _T('\\');
344 *p2 = _T('t');
345 break;
346 case _T('\n'):
347 *p2++ = _T('\\');
348 *p2 = _T('n');
349 break;
350 case _T('\r'):
351 *p2++ = _T('\\');
352 *p2 = _T('n');
353 break;
354 case _T('\\'):
355 *p2 = _T('\\');
356 // Jim Park: used to be p2++ but that's a bug that works because
357 // CharNext()'s behavior at terminating null char. But still
358 // definitely, unsafe.
359 default:
360 lstrcpyn(p2, p1, (int)(CharNext(p1) - p1) + 1);
361 break;
362 }
363 }
364 *p2 = 0;
365 nBufLen = nBufLen*2;
366 FREE(pszBuffer);
367 pszBuffer=pszBuf2;
368 }
369 break;
370 }
371 }
372 wsprintf(szField, _T("Field %d"), CurrField);
373 WritePrivateProfileString(szField, _T("State"), pszBuffer, pszFilename);
374 }
375
376 // Tell NSIS which control was activated, if any
377 wsprintf(pszBuffer, _T("%d"), g_NotifyField);
378 WritePrivateProfileString(_T("Settings"), _T("State"), pszBuffer, pszFilename);
379
380 FREE(pszBuffer);
381
382 return true;
383 }
384
385 #define BROWSE_WIDTH 15
386
387 static TCHAR szResult[BUFFER_SIZE];
388 const TCHAR *pszAppName;
389
390 DWORD WINAPI myGetProfileString(LPCTSTR lpKeyName)
391 {
392 *szResult = _T('\0');
393 return GetPrivateProfileString(pszAppName, lpKeyName, _T(""), szResult, BUFFER_SIZE, pszFilename);
394 }
395
396 TCHAR * WINAPI myGetProfileStringDup(LPCTSTR lpKeyName)
397 {
398 int nSize = myGetProfileString(lpKeyName);
399 if (nSize)
400 return strdup(szResult); // uses STRDUP
401 else
402 return NULL;
403 }
404
405 UINT WINAPI myGetProfileInt(LPCTSTR lpKeyName, INT nDefault)
406 {
407 return GetPrivateProfileInt(pszAppName, lpKeyName, nDefault, pszFilename);
408 }
409
410 int WINAPI ReadSettings(void) {
411 static TCHAR szField[25];
412 int nIdx, nCtrlIdx;
413
414 pszAppName = _T("Settings");
415 pszTitle = myGetProfileStringDup(_T("Title"));
416 pszCancelButtonText = myGetProfileStringDup(_T("CancelButtonText"));
417 pszNextButtonText = myGetProfileStringDup(_T("NextButtonText"));
418 pszBackButtonText = myGetProfileStringDup(_T("BackButtonText"));
419
420 nNumFields = myGetProfileInt(_T("NumFields"), 0);
421
422 nRectId = myGetProfileInt(_T("Rect"), DEFAULT_RECT);
423
424 bBackEnabled = myGetProfileInt(_T("BackEnabled"), -1);
425 // by ORTIM: 13-August-2002
426 bCancelEnabled = myGetProfileInt(_T("CancelEnabled"), -1);
427 bCancelShow = myGetProfileInt(_T("CancelShow"), -1);
428
429 bRTL = myGetProfileInt(_T("RTL"), 0);
430
431 if (nNumFields > 0) {
432 // make this twice as large for the worst case that every control is a browse button.
433 // the structure is small enough that this won't waste much memory.
434 // if the structure gets much larger, we should switch to a linked list.
435 pFields = (FieldType *)MALLOC(sizeof(FieldType)*2*nNumFields);
436 }
437
438 for (nIdx = 0, nCtrlIdx = 0; nCtrlIdx < nNumFields; nCtrlIdx++, nIdx++) {
439 // Control types
440 static TableEntry TypeTable[] = {
441 { _T("LABEL"), FIELD_LABEL },
442 { _T("TEXT"), FIELD_TEXT },
443 { _T("PASSWORD"), FIELD_TEXT },
444 { _T("LISTBOX"), FIELD_LISTBOX },
445 { _T("COMBOBOX"), FIELD_COMBOBOX },
446 { _T("DROPLIST"), FIELD_COMBOBOX },
447 { _T("FILEREQUEST"), FIELD_FILEREQUEST },
448 { _T("DIRREQUEST"), FIELD_DIRREQUEST },
449 { _T("CHECKBOX"), FIELD_CHECKBOX },
450 { _T("RADIOBUTTON"), FIELD_RADIOBUTTON },
451 { _T("ICON"), FIELD_ICON },
452 { _T("BITMAP"), FIELD_BITMAP },
453 { _T("GROUPBOX"), FIELD_GROUPBOX },
454 #ifdef IO_ENABLE_LINK
455 { _T("LINK"), FIELD_LINK },
456 #else
457 { _T("LINK"), FIELD_LABEL },
458 #endif
459 { _T("BUTTON"), FIELD_BUTTON },
460 { _T("HLINE"), FIELD_HLINE },
461 { _T("VLINE"), FIELD_VLINE },
462 { NULL, 0 }
463 };
464 // Control flags
465 static TableEntry FlagTable[] = {
466 { _T("NOTIFY"), LBS_NOTIFY },
467 { _T("WARN_IF_EXIST"), OFN_OVERWRITEPROMPT },
468 { _T("FILE_HIDEREADONLY"), OFN_HIDEREADONLY },
469 { _T("MULTISELECT"), LBS_MULTIPLESEL },
470 { _T("READONLY"), FLAG_READONLY },
471 { _T("RIGHT"), BS_LEFTTEXT },
472 { _T("PASSWORD"), FLAG_PASSWORD },
473 { _T("ONLY_NUMBERS"), FLAG_ONLYNUMBERS },
474 { _T("MULTILINE"), FLAG_MULTILINE },
475 { _T("NOWORDWRAP"), FLAG_NOWORDWRAP },
476 { _T("WANTRETURN"), FLAG_WANTRETURN },
477 { _T("EXTENDEDSELCT"), LBS_EXTENDEDSEL },
478 { _T("PATH_MUST_EXIST"), OFN_PATHMUSTEXIST },
479 { _T("FILE_MUST_EXIST"), OFN_FILEMUSTEXIST },
480 { _T("PROMPT_CREATE"), OFN_CREATEPROMPT },
481 { _T("DROPLIST"), FLAG_DROPLIST },
482 { _T("RESIZETOFIT"), FLAG_RESIZETOFIT },
483 { _T("NOTABSTOP"), WS_TABSTOP },
484 { _T("GROUP"), WS_GROUP },
485 { _T("REQ_SAVE"), FLAG_SAVEAS },
486 { _T("FILE_EXPLORER"), OFN_EXPLORER },
487 { _T("HSCROLL"), WS_HSCROLL },
488 { _T("VSCROLL"), WS_VSCROLL },
489 { _T("DISABLED"), WS_DISABLED },
490 { _T("TRANSPARENT"), TRANSPARENT_BMP },
491 { _T("FOCUS"), FLAG_FOCUS },
492 { NULL, 0 }
493 };
494 FieldType *pField = pFields + nIdx;
495
496 pField->nField = nCtrlIdx + 1;
497 pField->pszHwndEntry = _T("HWND");
498
499 wsprintf(szField, _T("Field %d"), nCtrlIdx + 1);
500 pszAppName = szField;
501
502 // Get the control type
503 myGetProfileString(_T("TYPE"));
504 pField->nType = LookupToken(TypeTable, szResult);
505 if (pField->nType == FIELD_INVALID)
506 continue;
507
508 // Lookup flags associated with the control type
509 pField->nFlags = LookupToken(FlagTable, szResult);
510 myGetProfileString(_T("Flags"));
511 pField->nFlags |= LookupTokens(FlagTable, szResult);
512
513 // pszState must not be NULL!
514 myGetProfileString(_T("State"));
515 pField->pszState = strdup(szResult); // uses STRDUP
516
517 // ListBox items list
518 {
519 int nResult = myGetProfileString(_T("ListItems"));
520 if (nResult) {
521 // add an extra | character to the end to simplify the loop where we add the items.
522 pField->pszListItems = (TCHAR*)MALLOC((nResult + 2)*sizeof(TCHAR));
523 lstrcpy(pField->pszListItems, szResult);
524 pField->pszListItems[nResult] = _T('|');
525 pField->pszListItems[nResult + 1] = _T('\0');
526 }
527 }
528
529 // Label Text - convert newline
530 pField->pszText = myGetProfileStringDup(_T("TEXT"));
531 if (pField->nType == FIELD_LABEL || pField->nType == FIELD_LINK)
532 ConvertNewLines(pField->pszText);
533
534 // Dir request - root folder
535 pField->pszRoot = myGetProfileStringDup(_T("ROOT"));
536
537 // ValidateText - convert newline
538 pField->pszValidateText = myGetProfileStringDup(_T("ValidateText"));
539 ConvertNewLines(pField->pszValidateText);
540
541 {
542 int nResult = GetPrivateProfileString(szField, _T("Filter"), _T("All Files|*.*"), szResult, COUNTOF(szResult), pszFilename);
543 if (nResult) {
544 // Convert the filter to the format required by Windows: NULL after each
545 // item followed by a terminating NULL
546 pField->pszFilter = (TCHAR*)MALLOC((nResult + 2)*sizeof(TCHAR));
547 lstrcpy(pField->pszFilter, szResult);
548 TCHAR *pszPos = pField->pszFilter;
549 while (*pszPos)
550 {
551 if (*pszPos == _T('|'))
552 *pszPos++ = 0;
553 else
554 pszPos = CharNext(pszPos);
555 }
556 }
557 }
558
559 pField->rect.left = myGetProfileInt(_T("LEFT"), 0);
560 pField->rect.top = myGetProfileInt(_T("TOP"), 0);
561 pField->rect.right = myGetProfileInt(_T("RIGHT"), 0);
562 pField->rect.bottom = myGetProfileInt(_T("BOTTOM"), 0);
563 pField->nMinLength = myGetProfileInt(_T("MinLen"), 0);
564 pField->nMaxLength = myGetProfileInt(_T("MaxLen"), 0);
565
566 // Text color for LINK control, default is the system default link color
567 pField->hImage = (HANDLE)(UINT_PTR) myGetProfileInt(_T("TxtColor"), GetLinkColor());
568
569 pField->nControlID = 1200 + nIdx;
570 if (pField->nType == FIELD_FILEREQUEST || pField->nType == FIELD_DIRREQUEST)
571 {
572 FieldType *pNewField = &pFields[nIdx+1];
573 pNewField->nControlID = 1200 + nIdx + 1;
574 pNewField->nType = FIELD_BROWSEBUTTON;
575 pNewField->nFlags = pField->nFlags & (WS_DISABLED | WS_TABSTOP);
576 pNewField->pszText = STRDUP(szBrowseButtonCaption); // needed for generic FREE
577 pNewField->rect.right = pField->rect.right;
578 pNewField->rect.left = pNewField->rect.right - BROWSE_WIDTH;
579 pNewField->rect.bottom = pField->rect.bottom;
580 pNewField->rect.top = pField->rect.top;
581 pField->rect.right = pNewField->rect.left - 3;
582 pNewField->nField = nCtrlIdx + 1;
583 pNewField->pszHwndEntry = _T("HWND2");
584 nNumFields++;
585 nIdx++;
586 }
587 }
588
589 return nNumFields;
590 }
591
592 LRESULT WINAPI WMCommandProc(HWND hWnd, UINT id, HWND hwndCtl, UINT codeNotify) {
593 int nIdx = FindControlIdx(id);
594 // Ignore if the dialog is in the process of being created
595 if (g_done || nIdx < 0)
596 return 0;
597
598 switch (pFields[nIdx].nType)
599 {
600 case FIELD_BROWSEBUTTON:
601 --nIdx;
602 case FIELD_LINK:
603 case FIELD_BUTTON:
604 case FIELD_CHECKBOX:
605 case FIELD_RADIOBUTTON:
606 if (codeNotify != BN_CLICKED)
607 return 0;
608 break;
609 case FIELD_COMBOBOX:
610 case FIELD_LISTBOX:
611 if (codeNotify != LBN_SELCHANGE) // LBN_SELCHANGE == CBN_SELCHANGE
612 return 0;
613 break;
614 default:
615 return 0;
616 }
617
618 FieldType *pField = pFields + nIdx;
619
620 TCHAR szBrowsePath[MAX_PATH];
621
622 switch (pField->nType) {
623 case FIELD_FILEREQUEST: {
624 OPENFILENAME ofn={0,};
625
626 ofn.lStructSize = sizeof(ofn);
627 ofn.hwndOwner = hConfigWindow;
628 ofn.lpstrFilter = pField->pszFilter;
629 ofn.lpstrFile = szBrowsePath;
630 ofn.nMaxFile = COUNTOF(szBrowsePath);
631 ofn.Flags = pField->nFlags & (OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_CREATEPROMPT | OFN_EXPLORER);
632
633 GetWindowText(pField->hwnd, szBrowsePath, COUNTOF(szBrowsePath));
634
635 tryagain:
636 GetCurrentDirectory(BUFFER_SIZE, szResult); // save working dir
637 if ((pField->nFlags & FLAG_SAVEAS) ? GetSaveFileName(&ofn) : GetOpenFileName(&ofn)) {
638 mySetWindowText(pField->hwnd, szBrowsePath);
639 SetCurrentDirectory(szResult); // restore working dir
640 // OFN_NOCHANGEDIR doesn't always work (see MSDN)
641 break;
642 }
643 else if (szBrowsePath[0] && CommDlgExtendedError() == FNERR_INVALIDFILENAME) {
644 szBrowsePath[0] = _T('\0');
645 goto tryagain;
646 }
647
648 break;
649 }
650
651 case FIELD_DIRREQUEST: {
652 BROWSEINFO bi;
653
654 bi.hwndOwner = hConfigWindow;
655 bi.pidlRoot = NULL;
656 bi.pszDisplayName = szBrowsePath;
657 bi.lpszTitle = pField->pszText;
658 #ifndef BIF_NEWDIALOGSTYLE
659 #define BIF_NEWDIALOGSTYLE 0x0040
660 #endif
661 bi.ulFlags = BIF_STATUSTEXT | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
662 bi.lpfn = BrowseCallbackProc;
663 bi.lParam = nIdx;
664 bi.iImage = 0;
665
666 if (pField->pszRoot) {
667 LPSHELLFOLDER sf;
668 ULONG eaten;
669 LPITEMIDLIST root;
670 SHGetDesktopFolder(&sf);
671 #ifdef _UNICODE
672 sf->ParseDisplayName(hConfigWindow, NULL, pField->pszRoot, &eaten, &root, NULL);
673 #else
674 int ccRoot = (lstrlen(pField->pszRoot) * 2) + 2;
675 LPWSTR pwszRoot = (LPWSTR) MALLOC(ccRoot);
676 MultiByteToWideChar(CP_ACP, 0, pField->pszRoot, -1, pwszRoot, ccRoot);
677 sf->ParseDisplayName(hConfigWindow, NULL, pwszRoot, &eaten, &root, NULL);
678 FREE(pwszRoot);
679 #endif
680 bi.pidlRoot = root;
681 sf->Release();
682 }
683 //CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
684 LPITEMIDLIST pResult = SHBrowseForFolder(&bi);
685 if (!pResult)
686 break;
687
688 if (SHGetPathFromIDList(pResult, szBrowsePath)) {
689 mySetWindowText(pField->hwnd, szBrowsePath);
690 }
691
692 CoTaskMemFree(pResult);
693
694 break;
695 }
696
697 case FIELD_LINK:
698 case FIELD_BUTTON:
699 // Allow the state to be empty - this might be useful in conjunction
700 // with the NOTIFY flag
701 if (*pField->pszState)
702 ShellExecute(hMainWindow, NULL, pField->pszState, NULL, NULL, SW_SHOWDEFAULT);
703 break;
704 }
705
706 if (pField->nFlags & LBS_NOTIFY) {
707 // Remember which control was activated then pretend the user clicked Next
708 g_NotifyField = nIdx + 1;
709 mySendMessage(hMainWindow, WM_NOTIFY_OUTER_NEXT, 1, 0);
710 }
711
712 return 0;
713 }
714
715
716 static WNDPROC lpWndProcOld;
717
718 int g_is_cancel,g_is_back;
719
720 INT_PTR CALLBACK ParentWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
721 {
722 INT_PTR bRes;
723 if (message == WM_NOTIFY_OUTER_NEXT && wParam == 1)
724 {
725 // Don't call leave function if fields aren't valid
726 if (!g_NotifyField && !ValidateFields())
727 return 0;
728 // Get the settings ready for the leave function verification
729 SaveSettings();
730 // Reset the record of activated control
731 g_NotifyField = 0;
732 }
733 bRes = CallWindowProc(lpWndProcOld,hwnd,message,wParam,lParam);
734 if (message == WM_NOTIFY_OUTER_NEXT && !bRes)
735 {
736 // if leave function didn't abort (bRes != 0 in that case)
737 if (wParam == (WPARAM)-1)
738 g_is_back++;
739 if (wParam == NOTIFY_BYE_BYE)
740 g_is_cancel++;
741 g_done++;
742 PostMessage(hConfigWindow,WM_CLOSE,0,0);
743 }
744 return bRes;
745 }
746
747 INT_PTR CALLBACK cfgDlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
748 {
749 switch (uMsg)
750 {
751 HANDLE_MSG(hwndDlg, WM_COMMAND, WMCommandProc);
752 case WM_DRAWITEM:
753 {
754 DRAWITEMSTRUCT* lpdis = (DRAWITEMSTRUCT*)lParam;
755 int nIdx = FindControlIdx(lpdis->CtlID);
756 #ifdef IO_LINK_UNDERLINED
757 HFONT OldFont;
758 LOGFONT lf;
759 #endif
760
761 if (nIdx < 0)
762 break;
763 FieldType *pField = pFields + nIdx;
764
765 #ifdef IO_LINK_UNDERLINED
766 GetObject(GetCurrentObject(lpdis->hDC, OBJ_FONT), sizeof(lf), &lf);
767 lf.lfUnderline = TRUE;
768 OldFont = (HFONT)SelectObject(lpdis->hDC, CreateFontIndirect(&lf));
769 #endif
770
771 // We need lpdis->rcItem later
772 RECT rc = lpdis->rcItem;
773
774 // Calculate needed size of the control
775 DrawText(lpdis->hDC, pField->pszText, -1, &rc, DT_VCENTER | DT_WORDBREAK | DT_CALCRECT);
776
777 // Make some more room so the focus rect won't cut letters off
778 rc.right = min(rc.right + 2, lpdis->rcItem.right);
779
780 // Move rect to right if in RTL mode
781 if (bRTL)
782 {
783 rc.left += lpdis->rcItem.right - rc.right;
784 rc.right += lpdis->rcItem.right - rc.right;
785 }
786
787 if (lpdis->itemAction & ODA_DRAWENTIRE)
788 {
789 // Get TxtColor unless the user has set another using SetCtlColors
790 if (!GetWindowLongPtr(lpdis->hwndItem, GWLP_USERDATA))
791 SetTextColor(lpdis->hDC, (COLORREF)(INT_PTR) pField->hImage);
792
793 // Draw the text
794 DrawText(lpdis->hDC, pField->pszText, -1, &rc, DT_CENTER | DT_VCENTER | DT_WORDBREAK | (bRTL ? DT_RTLREADING : 0));
795 }
796
797 // Draw the focus rect if needed
798 if (((lpdis->itemState & ODS_FOCUS) && (lpdis->itemAction & ODA_DRAWENTIRE)) || (lpdis->itemAction & ODA_FOCUS))
799 {
800 // NB: when not in DRAWENTIRE mode, this will actually toggle the focus
801 // rectangle since it's drawn in a XOR way
802 DrawFocusRect(lpdis->hDC, &rc);
803 }
804
805 pField->rect = rc;
806
807 #ifdef IO_LINK_UNDERLINED
808 DeleteObject(SelectObject(lpdis->hDC, OldFont));
809 #endif
810 break;
811 }
812 case WM_CTLCOLORSTATIC:
813 case WM_CTLCOLOREDIT:
814 case WM_CTLCOLORDLG:
815 case WM_CTLCOLORBTN:
816 case WM_CTLCOLORLISTBOX:
817 // let the NSIS window handle colors, it knows best
818 return mySendMessage(hMainWindow, uMsg, wParam, lParam);
819 }
820 return 0;
821 }
822
823 #ifdef IO_ENABLE_LINK
824
825 #ifndef IDC_HAND
826 #define IDC_HAND MAKEINTRESOURCE(32649)
827 #endif
828
829 #ifndef BS_TYPEMASK
830 #define BS_TYPEMASK 0x0000000FL
831 #endif
832
833 // pFields[nIdx].nParentIdx is used to store original windowproc
834 LRESULT CALLBACK StaticLINKWindowProc(HWND hWin, UINT uMsg, WPARAM wParam, LPARAM lParam)
835 {
836 int StaticField = FindControlIdx(GetDlgCtrlID(hWin));
837 if (StaticField < 0)
838 return 0;
839 FieldType *pField = pFields + StaticField;
840
841 switch(uMsg)
842 {
843 case WM_GETDLGCODE:
844 // Pretend we are a normal button/default button as appropriate
845 return DLGC_BUTTON | ((pField->nFlags & FLAG_WANTRETURN) ? DLGC_DEFPUSHBUTTON : DLGC_UNDEFPUSHBUTTON);
846
847 case BM_SETSTYLE:
848 // Detect when we are becoming the default button but don't lose the owner-draw style
849 if ((wParam & BS_TYPEMASK) == BS_DEFPUSHBUTTON)
850 pField->nFlags |= FLAG_WANTRETURN; // Hijack this flag to indicate default button status
851 else
852 pField->nFlags &= ~FLAG_WANTRETURN;
853 wParam = (wParam & ~BS_TYPEMASK) | BS_OWNERDRAW;
854 break;
855
856 case WM_NCHITTEST:
857 {
858 POINT pt = {GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam)};
859 MapWindowPoints(0, hWin, &pt, 1);
860 if (PtInRect(&pField->rect, pt))
861 return HTCLIENT;
862 else
863 return HTNOWHERE;
864 }
865
866 case WM_SETCURSOR:
867 {
868 if ((HWND)wParam == hWin && LOWORD(lParam) == HTCLIENT)
869 {
870 HCURSOR hCur = LoadCursor(NULL, IDC_HAND);
871 if (hCur)
872 {
873 SetCursor(hCur);
874 return 1; // halt further processing
875 }
876 }
877 }
878 }
879 return CallWindowProc((WNDPROC)pField->nParentIdx, hWin, uMsg, wParam, lParam);
880 }
881 #endif
882
883 LRESULT CALLBACK NumbersOnlyPasteWndProc(HWND hWin, UINT uMsg, WPARAM wParam, LPARAM lParam)
884 {
885 int nIdx = FindControlIdx(GetDlgCtrlID(hWin));
886 if (nIdx < 0)
887 return 0;
888
889 FieldType *pField = pFields + nIdx;
890
891 if (uMsg == WM_PASTE)
892 {
893 if (OpenClipboard(hWin))
894 {
895 #ifdef _UNICODE
896 HGLOBAL hData = GetClipboardData(CF_UNICODETEXT);
897 #else
898 HGLOBAL hData = GetClipboardData(CF_TEXT);
899 #endif
900
901 if (hData)
902 {
903 TCHAR *lpData = (TCHAR *) GlobalLock(hData);
904 if (lpData)
905 {
906 int iLen = lstrlen(lpData);
907 TCHAR *lpFilteredData = (TCHAR *) MALLOC((iLen + 1)*sizeof(TCHAR));
908
909 if (lpFilteredData)
910 {
911 for (int i = 0, j = 0; i < iLen; i++)
912 {
913 if (lpData[i] >= _T('0') && lpData[i] <= _T('9'))
914 {
915 lpFilteredData[j] = lpData[i];
916 j++;
917 }
918 lpFilteredData[j] = 0;
919 }
920
921 SendMessage(hWin, EM_REPLACESEL, TRUE, (LPARAM) lpFilteredData);
922 FREE(lpFilteredData);
923 }
924
925 GlobalUnlock(hData);
926 }
927 }
928
929 CloseClipboard();
930
931 return 0;
932 }
933 }
934
935 return CallWindowProc(pField->wndProc, hWin, uMsg, wParam, lParam);
936 }
937
938 int old_cancel_visible;
939
940 int WINAPI createCfgDlg()
941 {
942 g_is_back=0;
943 g_is_cancel=0;
944
945 HWND mainwnd = hMainWindow;
946 if (!mainwnd)
947 {
948 popstring(NULL);
949 pushstring(_T("error finding mainwnd"));
950 return 1; // cannot be used in silent mode unfortunately.
951 }
952
953 if (!g_stacktop || !*g_stacktop || !(pszFilename = (*g_stacktop)->text) || !pszFilename[0] || !ReadSettings())
954 {
955 popstring(NULL);
956 pushstring(_T("error finding config"));
957 return 1;
958 }
959
960 HWND childwnd=GetDlgItem(mainwnd,nRectId);
961 if (!childwnd)
962 {
963 popstring(NULL);
964 pushstring(_T("error finding childwnd"));
965 return 1;
966 }
967
968 hCancelButton = GetDlgItem(mainwnd,IDCANCEL);
969 hNextButton = GetDlgItem(mainwnd,IDOK);
970 hBackButton = GetDlgItem(mainwnd,3);
971
972 mySetWindowText(hCancelButton,pszCancelButtonText);
973 mySetWindowText(hNextButton,pszNextButtonText);
974 mySetWindowText(hBackButton,pszBackButtonText);
975
976 if (bBackEnabled!=-1) EnableWindow(hBackButton,bBackEnabled);
977 if (bCancelEnabled!=-1)
978 {
979 EnableWindow(hCancelButton,bCancelEnabled);
980 if (bCancelEnabled)
981 EnableMenuItem(GetSystemMenu(mainwnd, FALSE), SC_CLOSE, MF_BYCOMMAND | MF_ENABLED);
982 else
983 EnableMenuItem(GetSystemMenu(mainwnd, FALSE), SC_CLOSE, MF_BYCOMMAND | MF_GRAYED);
984 }
985 if (bCancelShow!=-1) old_cancel_visible=ShowWindow(hCancelButton,bCancelShow?SW_SHOWNA:SW_HIDE);
986
987 HFONT hFont = (HFONT)mySendMessage(mainwnd, WM_GETFONT, 0, 0);
988
989 // Prevent WM_COMMANDs from being processed while we are building
990 g_done = 1;
991
992 int mainWndWidth, mainWndHeight;
993 hConfigWindow=CreateDialog(m_hInstance,MAKEINTRESOURCE(IDD_DIALOG1),mainwnd,cfgDlgProc);
994 if (hConfigWindow)
995 {
996 RECT dialog_r;
997 GetWindowRect(childwnd,&dialog_r);
998 MapWindowPoints(0, mainwnd, (LPPOINT) &dialog_r, 2);
999 mainWndWidth = dialog_r.right - dialog_r.left;
1000 mainWndHeight = dialog_r.bottom - dialog_r.top;
1001 SetWindowPos(
1002 hConfigWindow,
1003 0,
1004 dialog_r.left,
1005 dialog_r.top,
1006 mainWndWidth,
1007 mainWndHeight,
1008 SWP_NOZORDER|SWP_NOACTIVATE
1009 );
1010 // Sets the font of IO window to be the same as the main window
1011 mySendMessage(hConfigWindow, WM_SETFONT, (WPARAM)hFont, TRUE);
1012 }
1013 else
1014 {
1015 popstring(NULL);
1016 pushstring(_T("error creating dialog"));
1017 return 1;
1018 }
1019
1020 BOOL fFocused = FALSE;
1021 BOOL fFocusedByFlag = FALSE;
1022
1023 #define DEFAULT_STYLES (WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS)
1024 #define RTL_EX_STYLES (WS_EX_RTLREADING | WS_EX_LEFTSCROLLBAR)
1025
1026 for (int nIdx = 0; nIdx < nNumFields; nIdx++) {
1027 static struct {
1028 const TCHAR* pszClass;
1029 DWORD dwStyle;
1030 DWORD dwRTLStyle;
1031 DWORD dwExStyle;
1032 DWORD dwRTLExStyle;
1033 } ClassTable[] = {
1034 { _T("STATIC"), // FIELD_HLINE
1035 DEFAULT_STYLES | SS_ETCHEDHORZ | SS_SUNKEN,
1036 DEFAULT_STYLES | SS_ETCHEDHORZ | SS_SUNKEN,
1037 WS_EX_TRANSPARENT,
1038 WS_EX_TRANSPARENT | RTL_EX_STYLES },
1039 { _T("STATIC"), // FIELD_VLINE
1040 DEFAULT_STYLES | SS_ETCHEDVERT | SS_SUNKEN,
1041 DEFAULT_STYLES | SS_ETCHEDVERT | SS_SUNKEN,
1042 WS_EX_TRANSPARENT,
1043 WS_EX_TRANSPARENT | RTL_EX_STYLES },
1044 { _T("STATIC"), // FIELD_LABEL
1045 DEFAULT_STYLES,
1046 DEFAULT_STYLES | SS_RIGHT,
1047 WS_EX_TRANSPARENT,
1048 WS_EX_TRANSPARENT | RTL_EX_STYLES },
1049 { _T("STATIC"), // FIELD_ICON
1050 DEFAULT_STYLES | SS_ICON,
1051 DEFAULT_STYLES | SS_ICON,
1052 0,
1053 RTL_EX_STYLES },
1054 { _T("STATIC"), // FIELD_BITMAP
1055 DEFAULT_STYLES | SS_BITMAP,
1056 DEFAULT_STYLES | SS_BITMAP,
1057 0,
1058 RTL_EX_STYLES },
1059 { _T("BUTTON"), // FIELD_BROWSEBUTTON
1060 DEFAULT_STYLES | WS_TABSTOP,
1061 DEFAULT_STYLES | WS_TABSTOP,
1062 0,
1063 RTL_EX_STYLES },
1064 { _T("BUTTON"), // FIELD_LINK
1065 DEFAULT_STYLES | WS_TABSTOP | BS_OWNERDRAW,
1066 DEFAULT_STYLES | WS_TABSTOP | BS_OWNERDRAW | BS_RIGHT,
1067 0,
1068 RTL_EX_STYLES },
1069 { _T("BUTTON"), // FIELD_BUTTON
1070 DEFAULT_STYLES | WS_TABSTOP,
1071 DEFAULT_STYLES | WS_TABSTOP,
1072 0,
1073 RTL_EX_STYLES },
1074 { _T("BUTTON"), // FIELD_GROUPBOX
1075 DEFAULT_STYLES | BS_GROUPBOX,
1076 DEFAULT_STYLES | BS_GROUPBOX | BS_RIGHT,
1077 WS_EX_TRANSPARENT,
1078 WS_EX_TRANSPARENT | RTL_EX_STYLES },
1079 { _T("BUTTON"), // FIELD_CHECKBOX
1080 DEFAULT_STYLES | WS_TABSTOP | BS_TEXT | BS_VCENTER | BS_AUTOCHECKBOX | BS_MULTILINE,
1081 DEFAULT_STYLES | WS_TABSTOP | BS_TEXT | BS_VCENTER | BS_AUTOCHECKBOX | BS_MULTILINE | BS_RIGHT | BS_LEFTTEXT,
1082 0,
1083 RTL_EX_STYLES },
1084 { _T("BUTTON"), // FIELD_RADIOBUTTON
1085 DEFAULT_STYLES | WS_TABSTOP | BS_TEXT | BS_VCENTER | BS_AUTORADIOBUTTON | BS_MULTILINE,
1086 DEFAULT_STYLES | WS_TABSTOP | BS_TEXT | BS_VCENTER | BS_AUTORADIOBUTTON | BS_MULTILINE | BS_RIGHT | BS_LEFTTEXT,
1087 0,
1088 RTL_EX_STYLES },
1089 { _T("EDIT"), // FIELD_TEXT
1090 DEFAULT_STYLES | WS_TABSTOP | ES_AUTOHSCROLL,
1091 DEFAULT_STYLES | WS_TABSTOP | ES_AUTOHSCROLL | ES_RIGHT,
1092 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
1093 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | RTL_EX_STYLES },
1094 { _T("EDIT"), // FIELD_FILEREQUEST
1095 DEFAULT_STYLES | WS_TABSTOP | ES_AUTOHSCROLL,
1096 DEFAULT_STYLES | WS_TABSTOP | ES_AUTOHSCROLL | ES_RIGHT,
1097 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
1098 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | RTL_EX_STYLES },
1099 { _T("EDIT"), // FIELD_DIRREQUEST
1100 DEFAULT_STYLES | WS_TABSTOP | ES_AUTOHSCROLL,
1101 DEFAULT_STYLES | WS_TABSTOP | ES_AUTOHSCROLL | ES_RIGHT,
1102 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
1103 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | RTL_EX_STYLES },
1104 { _T("COMBOBOX"), // FIELD_COMBOBOX
1105 DEFAULT_STYLES | WS_TABSTOP | WS_VSCROLL | WS_CLIPCHILDREN | CBS_AUTOHSCROLL | CBS_HASSTRINGS,
1106 DEFAULT_STYLES | WS_TABSTOP | WS_VSCROLL | WS_CLIPCHILDREN | CBS_AUTOHSCROLL | CBS_HASSTRINGS,
1107 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
1108 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_RIGHT | RTL_EX_STYLES },
1109 { _T("LISTBOX"), // FIELD_LISTBOX
1110 DEFAULT_STYLES | WS_TABSTOP | WS_VSCROLL | LBS_DISABLENOSCROLL | LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT,
1111 DEFAULT_STYLES | WS_TABSTOP | WS_VSCROLL | LBS_DISABLENOSCROLL | LBS_HASSTRINGS | LBS_NOINTEGRALHEIGHT,
1112 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE,
1113 WS_EX_WINDOWEDGE | WS_EX_CLIENTEDGE | WS_EX_RIGHT | RTL_EX_STYLES }
1114 };
1115
1116 FieldType *pField = pFields + nIdx;
1117
1118 #undef DEFAULT_STYLES
1119
1120 if (pField->nType < 1 || pField->nType > (int)(COUNTOF(ClassTable)))
1121 continue;
1122
1123 DWORD dwStyle, dwExStyle;
1124 if (bRTL) {
1125 dwStyle = ClassTable[pField->nType - 1].dwRTLStyle;
1126 dwExStyle = ClassTable[pField->nType - 1].dwRTLExStyle;
1127 }
1128 else {
1129 dwStyle = ClassTable[pField->nType - 1].dwStyle;
1130 dwExStyle = ClassTable[pField->nType - 1].dwExStyle;
1131 }
1132
1133 // Convert from dialog units
1134
1135 RECT rect = pField->rect;
1136 // MapDialogRect uses the font used when a dialog is created, and ignores
1137 // any subsequent WM_SETFONT messages (like we used above); so use the main
1138 // NSIS window for the conversion, instead of this one.
1139 MapDialogRect(mainwnd, &rect);
1140
1141 if (pField->rect.left < 0)
1142 rect.left += mainWndWidth;
1143 if (pField->rect.right < 0)
1144 rect.right += mainWndWidth;
1145 if (pField->rect.top < 0)
1146 rect.top += mainWndHeight;
1147 if (pField->rect.bottom < 0)
1148 rect.bottom += mainWndHeight;
1149
1150 if (bRTL) {
1151 int right = rect.right;
1152 rect.right = mainWndWidth - rect.left;
1153 rect.left = mainWndWidth - right;
1154 }
1155
1156 TCHAR *title = pField->pszText;
1157 switch (pField->nType) {
1158 case FIELD_ICON:
1159 case FIELD_BITMAP:
1160 title = NULL; // otherwise it is treated as the name of a resource
1161 break;
1162 case FIELD_CHECKBOX:
1163 case FIELD_RADIOBUTTON:
1164 dwStyle ^= pField->nFlags & BS_LEFTTEXT;
1165 break;
1166 case FIELD_TEXT:
1167 case FIELD_FILEREQUEST:
1168 case FIELD_DIRREQUEST:
1169 if (pField->nFlags & FLAG_PASSWORD)
1170 dwStyle |= ES_PASSWORD;
1171 if (pField->nFlags & FLAG_ONLYNUMBERS)
1172 dwStyle |= ES_NUMBER;
1173 if (pField->nFlags & FLAG_WANTRETURN)
1174 dwStyle |= ES_WANTRETURN;
1175 if (pField->nFlags & FLAG_READONLY)
1176 dwStyle |= ES_READONLY;
1177 title = pField->pszState;
1178 if (pField->nFlags & FLAG_MULTILINE)
1179 {
1180 dwStyle |= ES_MULTILINE | ES_AUTOVSCROLL;
1181 // Enable word-wrap unless we have a horizontal scroll bar
1182 // or it has been explicitly disallowed
1183 if (!(pField->nFlags & (WS_HSCROLL | FLAG_NOWORDWRAP)))
1184 dwStyle &= ~ES_AUTOHSCROLL;
1185 ConvertNewLines(pField->pszState);
1186 // If multiline-readonly then hold the text back until after the
1187 // initial focus has been set. This is so the text is not initially
1188 // selected - useful for License Page look-a-likes.
1189 if (pField->nFlags & FLAG_READONLY)
1190 title = NULL;
1191 }
1192 break;
1193 case FIELD_COMBOBOX:
1194 dwStyle |= (pField->nFlags & FLAG_DROPLIST) ? CBS_DROPDOWNLIST : CBS_DROPDOWN;
1195 title = pField->pszState;
1196 break;
1197 case FIELD_LISTBOX:
1198 dwStyle |= pField->nFlags & (LBS_NOTIFY | LBS_MULTIPLESEL | LBS_EXTENDEDSEL);
1199 break;
1200 }
1201
1202 dwStyle |= pField->nFlags & (WS_GROUP | WS_HSCROLL | WS_VSCROLL | WS_DISABLED);
1203 if (pField->nFlags & WS_TABSTOP) dwStyle &= ~WS_TABSTOP;
1204
1205 HWND hwCtrl = pField->hwnd = CreateWindowEx(
1206 dwExStyle,
1207 ClassTable[pField->nType - 1].pszClass,
1208 title,
1209 dwStyle,
1210 rect.left,
1211 rect.top,
1212 rect.right - rect.left,
1213 rect.bottom - rect.top,
1214 hConfigWindow,
1215 (HMENU)(UINT_PTR) pField->nControlID,
1216 m_hInstance,
1217 NULL
1218 );
1219
1220 {
1221 TCHAR szField[64];
1222 TCHAR szHwnd[64];
1223 wsprintf(szField, _T("Field %d"), pField->nField);
1224 wsprintf(szHwnd, _T("%d"), hwCtrl);
1225 WritePrivateProfileString(szField, pField->pszHwndEntry, szHwnd, pszFilename);
1226 }
1227
1228 if (hwCtrl) {
1229 // Sets the font of IO window to be the same as the main window
1230 mySendMessage(hwCtrl, WM_SETFONT, (WPARAM)hFont, TRUE);
1231 // make sure we created the window, then set additional attributes
1232 switch (pField->nType) {
1233 case FIELD_TEXT:
1234 case FIELD_FILEREQUEST:
1235 case FIELD_DIRREQUEST:
1236 mySendMessage(hwCtrl, EM_LIMITTEXT, (WPARAM)pField->nMaxLength, (LPARAM)0);
1237 if (dwStyle & ES_NUMBER)
1238 {
1239 pField->wndProc = (WNDPROC) GetWindowLongPtr(hwCtrl, GWLP_WNDPROC);
1240 SetWindowLongPtr(hwCtrl, GWLP_WNDPROC, (LONG_PTR) NumbersOnlyPasteWndProc);
1241 }
1242 break;
1243
1244 case FIELD_CHECKBOX:
1245 case FIELD_RADIOBUTTON:
1246 if (pField->pszState[0] == _T('1'))
1247 mySendMessage(hwCtrl, BM_SETCHECK, (WPARAM)BST_CHECKED, 0);
1248 break;
1249
1250 case FIELD_COMBOBOX:
1251 case FIELD_LISTBOX:
1252 // if this is a listbox or combobox, we need to add the list items.
1253 if (pField->pszListItems) {
1254 UINT nAddMsg, nFindMsg, nSetSelMsg;
1255 if (pField->nType == FIELD_COMBOBOX) {
1256 nAddMsg = CB_ADDSTRING;
1257 nFindMsg = CB_FINDSTRINGEXACT;
1258 nSetSelMsg = CB_SETCURSEL;
1259 }
1260 else {
1261 nAddMsg = LB_ADDSTRING;
1262 nFindMsg = LB_FINDSTRINGEXACT;
1263 nSetSelMsg = LB_SETCURSEL;
1264 }
1265 TCHAR *pszStart, *pszEnd, *pszList;
1266 pszStart = pszEnd = pszList = STRDUP(pField->pszListItems);
1267 // pszListItems has a trailing pipe
1268 while (*pszEnd) {
1269 if (*pszEnd == _T('|')) {
1270 *pszEnd = _T('\0');
1271 if (*pszStart)
1272 mySendMessage(hwCtrl, nAddMsg, 0, (LPARAM) pszStart);
1273 pszStart = ++pszEnd;
1274 }
1275 else
1276 pszEnd = CharNext(pszEnd);
1277 }
1278 FREE(pszList);
1279 if (pField->pszState) {
1280 if (pField->nFlags & (LBS_MULTIPLESEL|LBS_EXTENDEDSEL) && nFindMsg == LB_FINDSTRINGEXACT) {
1281 mySendMessage(hwCtrl, LB_SETSEL, FALSE, (LPARAM)-1);
1282 pszStart = pszEnd = pField->pszState;
1283 for (;;) {
1284 TCHAR c = *pszEnd;
1285 if (c == _T('|') || c == _T('\0')) {
1286 *pszEnd = _T('\0');
1287 if (*pszStart)
1288 {
1289 INT_PTR nItem = mySendMessage(hwCtrl, LB_FINDSTRINGEXACT, (WPARAM)-1, (LPARAM)pszStart);
1290 if (nItem != LB_ERR)
1291 mySendMessage(hwCtrl, LB_SETSEL, TRUE, nItem);
1292 }
1293 if (!c)
1294 break;
1295 pszStart = ++pszEnd;
1296 }
1297 else
1298 pszEnd = CharNext(pszEnd);
1299 }
1300 }
1301 else {
1302 INT_PTR nItem = mySendMessage(hwCtrl, nFindMsg, (WPARAM)-1, (LPARAM)pField->pszState);
1303 if (nItem != CB_ERR) { // CB_ERR == LB_ERR == -1
1304 mySendMessage(hwCtrl, nSetSelMsg, nItem, 0);
1305 }
1306 }
1307 }
1308 }
1309 break;
1310
1311 case FIELD_ICON:
1312 case FIELD_BITMAP:
1313 {
1314 UINT nImageType = pField->nType == FIELD_BITMAP ? IMAGE_BITMAP : IMAGE_ICON;
1315 LPARAM nImage = 0;
1316
1317 if (pField->pszText) {
1318 pField->hImage = LoadImage(
1319 m_hInstance,
1320 pField->pszText,
1321 nImageType,
1322 (pField->nFlags & FLAG_RESIZETOFIT)
1323 ? (rect.right - rect.left)
1324 : 0,
1325 (pField->nFlags & FLAG_RESIZETOFIT)
1326 ? (rect.bottom - rect.top)
1327 : 0,
1328 LR_LOADFROMFILE
1329 );
1330 nImage = (LPARAM)pField->hImage;
1331 }
1332 else
1333 nImage = (LPARAM)LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(103));
1334
1335 if ((pField->nFlags & TRANSPARENT_BMP) && nImageType == IMAGE_BITMAP)
1336 {
1337 // based on AdvSplash's SetTransparentRegion
1338 BITMAP bm;
1339 HBITMAP hBitmap = (HBITMAP) nImage;
1340
1341 if (GetObject(hBitmap, sizeof(bm), &bm))
1342 {
1343 HDC dc;
1344 int x, y;
1345 HRGN region, cutrgn;
1346 BITMAPINFO bmi;
1347 int size = bm.bmWidth * bm.bmHeight * sizeof(int);
1348 int *bmp = (int *) MALLOC(size);
1349 if (bmp)
1350 {
1351 bmi.bmiHeader.biBitCount = 32;
1352 bmi.bmiHeader.biCompression = BI_RGB;
1353 bmi.bmiHeader.biHeight = bm.bmHeight;
1354 bmi.bmiHeader.biPlanes = 1;
1355 bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1356 bmi.bmiHeader.biWidth = bm.bmWidth;
1357 bmi.bmiHeader.biClrUsed = 0;
1358 bmi.bmiHeader.biClrImportant = 0;
1359
1360 dc = CreateCompatibleDC(NULL);
1361 SelectObject(dc, hBitmap);
1362
1363 x = GetDIBits(dc, hBitmap, 0, bm.bmHeight, bmp, &bmi, DIB_RGB_COLORS);
1364
1365 region = CreateRectRgn(0, 0, bm.bmWidth, bm.bmHeight);
1366
1367 int keycolor = *bmp & 0xFFFFFF;
1368
1369 // Search for transparent pixels
1370 for (y = bm.bmHeight - 1; y >= 0; y--) {
1371 for (x = 0; x < bm.bmWidth;) {
1372 if ((*bmp & 0xFFFFFF) == keycolor) {
1373 int j = x;
1374 while ((x < bm.bmWidth) && ((*bmp & 0xFFFFFF) == keycolor)) {
1375 bmp++, x++;
1376 }
1377
1378 // Cut transparent pixels from the original region
1379 cutrgn = CreateRectRgn(j, y, x, y + 1);
1380 CombineRgn(region, region, cutrgn, RGN_XOR);
1381 DeleteObject(cutrgn);
1382 } else {
1383 bmp++, x++;
1384 }
1385 }
1386 }
1387
1388 // Set resulting region.
1389 SetWindowRgn(hwCtrl, region, TRUE);
1390 DeleteObject(region);
1391 DeleteObject(dc);
1392 FREE(bmp);
1393 }
1394 }
1395 }
1396
1397 mySendMessage(
1398 hwCtrl,
1399 STM_SETIMAGE,
1400 nImageType,
1401 nImage
1402 );
1403
1404 if (pField->nType == FIELD_BITMAP)
1405 {
1406 // Centre the image in the requested space.
1407 // Cannot use SS_CENTERIMAGE because it behaves differently on XP to
1408 // everything else. (Thank you Microsoft.)
1409 RECT bmp_rect;
1410 GetClientRect(hwCtrl, &bmp_rect);
1411 bmp_rect.left = (rect.left + rect.right - bmp_rect.right) / 2;
1412 bmp_rect.top = (rect.top + rect.bottom - bmp_rect.bottom) / 2;
1413 SetWindowPos(hwCtrl, NULL, bmp_rect.left, bmp_rect.top, 0, 0,
1414 SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER);
1415 }
1416
1417 break;
1418 }
1419
1420 #ifdef IO_ENABLE_LINK
1421 case FIELD_LINK:
1422 pField->nParentIdx = (INT_PTR) SetWindowLongPtr(hwCtrl, GWLP_WNDPROC, (LONG_PTR)StaticLINKWindowProc);
1423 break;
1424 #endif
1425 }
1426
1427 // Set initial focus to the first appropriate field ( with FOCUS flag)
1428 if (!fFocusedByFlag && (dwStyle & (WS_TABSTOP | WS_DISABLED)) == WS_TABSTOP && pField->nType >= FIELD_SETFOCUS) {
1429 if (pField->nFlags & FLAG_FOCUS) {
1430 fFocusedByFlag = TRUE;
1431 }
1432 if (!fFocused || fFocusedByFlag) {
1433 fFocused = TRUE;
1434 mySetFocus(hwCtrl);
1435 }
1436 }
1437
1438 // If multiline-readonly then hold the text back until after the
1439 // initial focus has been set. This is so the text is not initially
1440 // selected - useful for License Page look-a-likes.
1441 if ((pField->nFlags & (FLAG_MULTILINE | FLAG_READONLY)) == (FLAG_MULTILINE | FLAG_READONLY))
1442 mySetWindowText(hwCtrl, pField->pszState);
1443 }
1444 }
1445
1446 if (!fFocused)
1447 mySetFocus(hNextButton);
1448
1449 mySetWindowText(mainwnd,pszTitle);
1450 pFilenameStackEntry = *g_stacktop;
1451 *g_stacktop = (*g_stacktop)->next;
1452 static TCHAR tmp[32];
1453 wsprintf(tmp,_T("%d"),hConfigWindow);
1454 pushstring(tmp);
1455 return 0;
1456 }
1457
1458 void WINAPI showCfgDlg()
1459 {
1460 lpWndProcOld = (WNDPROC) SetWindowLongPtr(hMainWindow,DWLP_DLGPROC,(LONG_PTR)ParentWndProc);
1461
1462 // Tell NSIS to remove old inner dialog and pass handle of the new inner dialog
1463 mySendMessage(hMainWindow, WM_NOTIFY_CUSTOM_READY, (WPARAM)hConfigWindow, 0);
1464 ShowWindow(hConfigWindow, SW_SHOWNA);
1465
1466 g_done = g_NotifyField = 0;
1467
1468 while (!g_done) {
1469 MSG msg;
1470 GetMessage(&msg, NULL, 0, 0);
1471 if (!IsDialogMessage(hConfigWindow,&msg) && !IsDialogMessage(hMainWindow,&msg))
1472 {
1473 TranslateMessage(&msg);
1474 DispatchMessage(&msg);
1475 }
1476 }
1477
1478 // we don't save settings on cancel since that means your installer will likely
1479 // quit soon, which means the ini might get flushed late and cause delete issues?
1480 if (!g_is_cancel) SaveSettings();
1481
1482 SetWindowLongPtr(hMainWindow,DWLP_DLGPROC,(LONG_PTR)lpWndProcOld);
1483 DestroyWindow(hConfigWindow);
1484
1485 // by ORTIM: 13-August-2002
1486 if (bCancelShow!=-1) ShowWindow(hCancelButton,old_cancel_visible?SW_SHOWNA:SW_HIDE);
1487
1488 FREE(pFilenameStackEntry);
1489 FREE(pszTitle);
1490 FREE(pszCancelButtonText);
1491 FREE(pszNextButtonText);
1492 FREE(pszBackButtonText);
1493
1494 int i = nNumFields;
1495 while (i--) {
1496 FieldType *pField = pFields + i;
1497
1498 int j = FIELD_BUFFERS;
1499 while (j--)
1500 FREE(((TCHAR **) pField)[j]);
1501
1502 if (pField->nType == FIELD_BITMAP) {
1503 DeleteObject(pField->hImage);
1504 }
1505 if (pField->nType == FIELD_ICON) {
1506 DestroyIcon((HICON)pField->hImage);
1507 }
1508 }
1509 FREE(pFields);
1510
1511 pushstring(g_is_cancel?_T("cancel"):g_is_back?_T("back"):_T("success"));
1512 }
1513
1514 int initCalled;
1515
1516 #ifdef _UNICODE
1517 // convert ini file to Unicode so that WritePrivateProfileString can write Unicode strings in it
1518 extern "C" void __declspec(dllexport) make_unicode(HWND hwndParent, int string_size,
1519 TCHAR *variables, stack_t **stacktop)
1520 {
1521 EXDLL_INIT();
1522 TCHAR filename[MAX_PATH];
1523 popstring(filename);
1524 HANDLE hFile = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
1525 if (hFile != INVALID_HANDLE_VALUE)
1526 {
1527 DWORD fSize = GetFileSize(hFile, NULL);
1528 LPSTR lpBuffer = (LPSTR) MALLOC(fSize);
1529 if (lpBuffer)
1530 {
1531 DWORD bytes;
1532 ReadFile(hFile, lpBuffer, fSize, &bytes, NULL);
1533 if ((bytes < 2) || (lpBuffer[0] != '\xFF') || (lpBuffer[1] != '\xFE')) // file is not already Unicode
1534 {
1535 LPWSTR lpWide = (LPWSTR) MALLOC((bytes+1)*2);
1536 if (lpWide)
1537 {
1538 int cch = MultiByteToWideChar(CP_ACP, 0, lpBuffer, bytes, lpWide, bytes+1);
1539 if (cch)
1540 {
1541 SetFilePointer(hFile, 0, NULL, FILE_BEGIN);
1542 WriteFile(hFile, "\xFF\xFE", 2, &bytes, NULL); // write Unicode BOM
1543 WriteFile(hFile, lpWide, cch*2, &bytes, NULL);
1544 SetEndOfFile(hFile);
1545 }
1546 FREE(lpWide);
1547 }
1548 }
1549 FREE(lpBuffer);
1550 }
1551 CloseHandle(hFile);
1552 }
1553 }
1554 #endif
1555
1556 extern "C" void __declspec(dllexport) dialog(HWND hwndParent, int string_size,
1557 TCHAR *variables, stack_t **stacktop)
1558 {
1559 hMainWindow=hwndParent;
1560 EXDLL_INIT();
1561 if (initCalled) {
1562 pushstring(_T("error"));
1563 return;
1564 }
1565 if (createCfgDlg())
1566 return;
1567 popstring(NULL);
1568 showCfgDlg();
1569 }
1570
1571 static UINT_PTR PluginCallback(enum NSPIM msg)
1572 {
1573 return 0;
1574 }
1575
1576 extern "C" void __declspec(dllexport) initDialog(HWND hwndParent, int string_size,
1577 TCHAR *variables, stack_t **stacktop,
1578 extra_parameters *extra)
1579 {
1580 hMainWindow=hwndParent;
1581 EXDLL_INIT();
1582
1583 extra->RegisterPluginCallback(m_hInstance, PluginCallback);
1584
1585 if (initCalled) {
1586 pushstring(_T("error"));
1587 return;
1588 }
1589 if (createCfgDlg())
1590 return;
1591 initCalled++;
1592 }
1593
1594 extern "C" void __declspec(dllexport) show(HWND hwndParent, int string_size,
1595 TCHAR *variables, stack_t **stacktop)
1596 {
1597 EXDLL_INIT();
1598 if (!initCalled) {
1599 pushstring(_T("error"));
1600 return;
1601 }
1602 initCalled--;
1603 showCfgDlg();
1604 }
1605
1606 extern "C" BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
1607 {
1608 m_hInstance=hInst;
1609 return TRUE;
1610 }
1611
1612
1613 /**
1614 * Looks up a single token in the psTable_ and returns its mapped numerical value.
1615 *
1616 * @param psTable_ The lookup table.
1617 * @param pszToken_ The token to lookup.
1618 * @return The integer value related to the token, otherwise 0.
1619 */
1620 int WINAPI LookupToken(TableEntry* psTable_, TCHAR* pszToken_)
1621 {
1622 for (int i = 0; psTable_[i].pszName; i++)
1623 if (!lstrcmpi(pszToken_, psTable_[i].pszName))
1624 return psTable_[i].nValue;
1625 return 0;
1626 }
1627
1628 /**
1629 * In a string of tokens separated by vertical bars '|', look them up in the
1630 * Lookup Table psTable and return their logical OR of their subsequent
1631 * integer values.
1632 *
1633 * @param psTable_ The lookup table to search in.
1634 * @param pszToken String of tokens separated by '|' whose values are to be
1635 * ORed together.
1636 * @return The ORed value of the token values. If no tokens were found, it
1637 * will return 0.
1638 */
1639 int WINAPI LookupTokens(TableEntry* psTable_, TCHAR* pszTokens_)
1640 {
1641 int n = 0;
1642 TCHAR *pszStart = pszTokens_;
1643 TCHAR *pszEnd = pszTokens_;
1644 for (;;) {
1645 TCHAR c = *pszEnd;
1646 if (c == _T('|') || c == _T('\0')) {
1647 *pszEnd = _T('\0');
1648 n |= LookupToken(psTable_, pszStart);
1649 *pszEnd = c;
1650 if (!c)
1651 break;
1652 pszStart = ++pszEnd;
1653 }
1654 else
1655 pszEnd = CharNext(pszEnd);
1656 }
1657 return n;
1658 }
1659
1660 /**
1661 * ConvertNewLines takes a string and turns escape sequences written
1662 * as separate chars e.g. "\\t" into the special char they represent
1663 * '\t'. The transformation is done in place.
1664 *
1665 * @param str [in/out] The string to convert.
1666 */
1667 void WINAPI ConvertNewLines(TCHAR *str) {
1668 TCHAR *p1, *p2, *p3;
1669 TCHAR tch0, tch1, nch;
1670
1671 if (!str)
1672 return;
1673
1674 p1 = p2 = str;
1675
1676 while ((tch0 = *p1) != 0)
1677 {
1678 nch = 0; // new translated char
1679 if (tch0 == _T('\\'))
1680 {
1681 tch1 = *(p1+1);
1682
1683 if (tch1 == _T('t')) nch = _T('\t');
1684 else if (tch1 == _T('n')) nch = _T('\n');
1685 else if (tch1 == _T('r')) nch = _T('\r');
1686 else if (tch1 == _T('\\')) nch = _T('\\');
1687 }
1688
1689 // Was it a special char?
1690 if (nch)
1691 {
1692 *p2++ = nch;
1693 p1 += 2;
1694 }
1695 else
1696 {
1697 // For MBCS
1698 p3 = CharNext(p1);
1699 while (p1 < p3)
1700 *p2++ = *p1++;
1701 }
1702 }
1703
1704 *p2 = 0;
1705 }