Debian Codegraph @

Contrib/nsDialogs/nsDialogs.c

raw
1 // Unicode support by Jim Park -- 08/10/2007
2
3 #include <windows.h>
4
5 #include <nsis/pluginapi.h> // nsis plugin
6
7 #include "defs.h"
8 #include "input.h"
9 #include "rtl.h"
10
11 #ifndef ODS_NOACCEL
12 #define ODS_NOACCEL 0x0100
13 #define ODS_NOFOCUSRECT 0x0200
14 #endif
15 #ifndef DT_HIDEPREFIX
16 #define DT_HIDEPREFIX 0x00100000
17 #endif
18
19 HINSTANCE g_hInstance;
20 struct nsDialog g_dialog;
21 extra_parameters* g_pluginParms;
22 LPTSTR g_callbackRetVar;
23
24 static COLORREF GetLinkColor()
25 {
26 COLORREF clr = GetSysColor(COLOR_HOTLIGHT);
27 #ifndef _WIN64
28 // COLOR_HOTLIGHT is Win98/2000+. GetSysColorBrush is the correct way to
29 // detect valid colors but here we just assume nobody uses black.
30 if (!clr) clr = RGB(0,0,255);
31 #endif
32 return clr;
33 }
34
35 struct nsControl* NSDFUNC GetControl(HWND hwCtl)
36 {
37 unsigned id = (unsigned)(UINT_PTR) GetProp(hwCtl, NSCONTROL_ID_PROP);
38
39 if (id == 0 || id > g_dialog.controlCount)
40 {
41 return NULL;
42 }
43
44 return &g_dialog.controls[id - 1];
45 }
46
47 INT_PTR CALLBACK ParentProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
48 {
49 INT_PTR res;
50
51 if (message == WM_NOTIFY_OUTER_NEXT)
52 {
53 if (wParam == (WPARAM)-1)
54 {
55 if (g_pluginParms->ExecuteCodeSegment(g_dialog.callbacks.onBack - 1, 0))
56 {
57 return 0;
58 }
59 }
60 }
61
62 res = CallWindowProc(g_dialog.parentOriginalWndproc, hwnd, message, wParam, lParam);
63
64 if (message == WM_NOTIFY_OUTER_NEXT && !res)
65 {
66 DestroyWindow(g_dialog.hwDialog);
67 HeapFree(GetProcessHeap(), 0, g_dialog.controls);
68 g_dialog.hwDialog = NULL;
69 g_dialog.controls = NULL;
70 }
71
72 return res;
73 }
74
75 LRESULT CALLBACK LinkWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
76 {
77 struct nsControl* ctl = GetControl(hwnd);
78
79 if(ctl == NULL)
80 return 0;
81
82 if(message == WM_SETCURSOR)
83 {
84 SetCursor(LoadCursor(NULL, IDC_HAND));
85 return TRUE;
86 }
87
88 return CallWindowProc(ctl->oldWndProc, hwnd, message, wParam, lParam);
89 }
90
91 INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
92 {
93 switch (uMsg)
94 {
95 // handle notifications
96 case WM_COMMAND:
97 {
98 HWND hwCtl = GetDlgItem(hwndDlg, LOWORD(wParam));
99 struct nsControl* ctl = GetControl(hwCtl);
100
101 if (ctl == NULL)
102 break;
103
104 if (HIWORD(wParam) == BN_CLICKED && (ctl->type == NSCTL_BUTTON || ctl->type == NSCTL_LINK))
105 {
106 if (ctl->callbacks.onClick)
107 {
108 pushintptr((INT_PTR) hwCtl);
109 g_pluginParms->ExecuteCodeSegment(ctl->callbacks.onClick - 1, 0);
110 }
111 }
112 else if (HIWORD(wParam) == EN_CHANGE && ctl->type == NSCTL_EDIT)
113 {
114 if (ctl->callbacks.onChange)
115 {
116 pushintptr((INT_PTR) hwCtl);
117 g_pluginParms->ExecuteCodeSegment(ctl->callbacks.onChange - 1, 0);
118 }
119 }
120 else if (HIWORD(wParam) == LBN_SELCHANGE && ctl->type == NSCTL_LISTBOX)
121 {
122 if (ctl->callbacks.onChange)
123 {
124 pushintptr((INT_PTR) hwCtl);
125 g_pluginParms->ExecuteCodeSegment(ctl->callbacks.onChange - 1, 0);
126 }
127 }
128 else if ((HIWORD(wParam) == CBN_EDITUPDATE || HIWORD(wParam) == CBN_SELCHANGE)
129 && ctl->type == NSCTL_COMBOBOX)
130 {
131 if (ctl->callbacks.onChange)
132 {
133 pushintptr((INT_PTR) hwCtl);
134 g_pluginParms->ExecuteCodeSegment(ctl->callbacks.onChange - 1, 0);
135 }
136 }
137 else if (HIWORD(wParam) == STN_CLICKED && ctl->type == NSCTL_STATIC)
138 {
139 if (ctl->callbacks.onClick)
140 {
141 pushintptr((INT_PTR) hwCtl);
142 g_pluginParms->ExecuteCodeSegment(ctl->callbacks.onClick - 1, 0);
143 }
144 }
145
146 break;
147 }
148
149 case WM_NOTIFY:
150 {
151 LPNMHDR nmhdr = (LPNMHDR) lParam;
152 struct nsControl* ctl = GetControl(nmhdr->hwndFrom);
153 int *pFlag = &g_pluginParms->exec_flags->silent, orgFlag, ret; // The silent flag can only be changed in .onInit and custom pages will not be displayed in silent mode so we can use this flag in the callback
154
155 if (ctl == NULL)
156 break;
157
158 if (!ctl->callbacks.onNotify)
159 break;
160
161 orgFlag = *pFlag, *pFlag = 0;
162 pushintptr((INT_PTR) nmhdr);
163 pushintptr(nmhdr->code);
164 pushintptr((INT_PTR) nmhdr->hwndFrom);
165 g_pluginParms->ExecuteCodeSegment(ctl->callbacks.onNotify - 1, 0);
166 ret = *pFlag, *pFlag = orgFlag;
167 if (ret)
168 return DlgRet(hwndDlg, StrToIntPtr(g_callbackRetVar));
169 }
170 break;
171
172 // handle links
173 case WM_DRAWITEM:
174 {
175 DRAWITEMSTRUCT* lpdis = (DRAWITEMSTRUCT*)lParam;
176 RECT rc;
177 TCHAR text[1024];
178
179 // http://blogs.msdn.com/oldnewthing/archive/2005/05/03/414317.aspx#414357
180 // says we should call SystemParametersInfo(SPI_GETKEYBOARDCUES,...) to make
181 // sure, does not seem to be required, might be a win2k bug, or it might
182 // only apply to menus
183 BOOL hideFocus = (lpdis->itemState & ODS_NOFOCUSRECT);
184 BOOL hideAccel = (lpdis->itemState & ODS_NOACCEL);
185
186 struct nsControl* ctl = GetControl(lpdis->hwndItem);
187 if (ctl == NULL)
188 break;
189
190 // We need lpdis->rcItem later
191 rc = lpdis->rcItem;
192
193 // Get button's text
194 text[0] = _T('\0');
195 GetWindowText(lpdis->hwndItem, text, 1024);
196
197 // Calculate needed size of the control
198 DrawText(lpdis->hDC, text, -1, &rc, DT_VCENTER | DT_WORDBREAK | DT_CALCRECT);
199
200 // Make some more room so the focus rect won't cut letters off
201 rc.right = min(rc.right + 2, lpdis->rcItem.right);
202
203 // Move rect to right if in RTL mode
204 if (g_dialog.rtl)
205 {
206 rc.left += lpdis->rcItem.right - rc.right;
207 rc.right += lpdis->rcItem.right - rc.right;
208 }
209
210 if (lpdis->itemAction & ODA_DRAWENTIRE)
211 {
212 DWORD xtraDrawStyle = (g_dialog.rtl ? DT_RTLREADING : 0);
213 if (hideAccel)
214 xtraDrawStyle |= DT_HIDEPREFIX;
215
216 // Use the system color unless the user has set another using SetCtlColors
217 if (!GetWindowLongPtr(lpdis->hwndItem, GWLP_USERDATA))
218 SetTextColor(lpdis->hDC, GetLinkColor());
219
220 // Draw the text
221 DrawText(lpdis->hDC, text, -1, &rc, xtraDrawStyle | DT_CENTER | DT_VCENTER | DT_WORDBREAK);
222 }
223
224 // Draw the focus rect if needed
225 if (((lpdis->itemState & ODS_FOCUS) && (lpdis->itemAction & ODA_DRAWENTIRE)) || (lpdis->itemAction & ODA_FOCUS))
226 {
227 // NB: when not in DRAWENTIRE mode, this will actually toggle the focus
228 // rectangle since it's drawn in a XOR way
229 if (!hideFocus)
230 DrawFocusRect(lpdis->hDC, &rc);
231 }
232
233 return TRUE;
234 }
235
236 // handle colors
237 case WM_CTLCOLORSTATIC:
238 case WM_CTLCOLOREDIT:
239 case WM_CTLCOLORDLG:
240 case WM_CTLCOLORBTN:
241 case WM_CTLCOLORLISTBOX:
242 // let the NSIS window handle colors, it knows best
243 return SendMessage(g_dialog.hwParent, uMsg, wParam, lParam);
244
245 // bye bye
246 case WM_DESTROY:
247 {
248 unsigned i;
249 for (i = 0; i < g_dialog.controlCount; i++)
250 {
251 RemoveProp(g_dialog.controls[i].window, NSCONTROL_ID_PROP);
252 }
253 break;
254 }
255 }
256
257 return FALSE;
258 }
259
260 static UINT_PTR PluginCallback(enum NSPIM msg)
261 {
262 return 0;
263 }
264
265 void __declspec(dllexport) Create(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
266 {
267 HWND hwPlacementRect;
268 RECT rcPlacement;
269
270 EXDLL_INIT();
271
272 extra->RegisterPluginCallback(g_hInstance, PluginCallback);
273
274 g_dialog.hwParent = hwndParent;
275 g_pluginParms = extra;
276 g_callbackRetVar = GetVar(variables, string_size, 31); // The undocumented $_OUTDIR variable
277
278 hwPlacementRect = GetDlgItem(hwndParent, popint());
279 GetWindowRect(hwPlacementRect, &rcPlacement);
280 MapWindowPoints(NULL, hwndParent, (LPPOINT) &rcPlacement, 2);
281
282 g_dialog.hwDialog = CreateDialog(g_hInstance, MAKEINTRESOURCE(1), hwndParent, DialogProc);
283
284 if (g_dialog.hwDialog == NULL)
285 {
286 pushstring(_T("error"));
287 return;
288 }
289
290 SetWindowPos(
291 g_dialog.hwDialog,
292 0,
293 rcPlacement.left,
294 rcPlacement.top,
295 rcPlacement.right - rcPlacement.left,
296 rcPlacement.bottom - rcPlacement.top,
297 SWP_NOZORDER | SWP_NOACTIVATE
298 );
299
300 g_dialog.parentOriginalWndproc = (WNDPROC) SetWindowLongPtr(hwndParent, DWLP_DLGPROC, (LONG_PTR) ParentProc);
301
302 g_dialog.rtl = FALSE;
303
304 g_dialog.controlCount = 0;
305 g_dialog.controls = (struct nsControl*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 0);
306
307 g_dialog.callbacks.onBack = 0;
308
309 pushintptr((INT_PTR) g_dialog.hwDialog);
310 }
311
312 void __declspec(dllexport) CreateControl(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
313 {
314 TCHAR *className;
315 TCHAR *text;
316
317 HWND hwItem;
318 int x, y, width, height;
319 DWORD style, exStyle;
320 size_t id;
321
322 // get info from stack
323
324 className = (TCHAR *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (g_stringsize * 2)*sizeof(TCHAR));
325 text = &className[g_stringsize];
326
327 if (!className)
328 {
329 pushstring(_T("error"));
330 return;
331 }
332
333 if (popstringn(className, 0))
334 {
335 pushstring(_T("error"));
336 HeapFree(GetProcessHeap(), 0, className);
337 return;
338 }
339
340 style = (DWORD) popint_or();
341 exStyle = (DWORD) popint_or();
342
343 PopPlacement(&x, &y, &width, &height);
344
345 if (popstringn(text, 0))
346 {
347 pushstring(_T("error"));
348 HeapFree(GetProcessHeap(), 0, className);
349 return;
350 }
351
352 // create item descriptor
353
354 id = g_dialog.controlCount;
355 g_dialog.controlCount++;
356 g_dialog.controls = (struct nsControl*) HeapReAlloc(
357 GetProcessHeap(),
358 HEAP_ZERO_MEMORY,
359 g_dialog.controls,
360 g_dialog.controlCount * sizeof(struct nsControl));
361
362 if (!lstrcmpi(className, _T("BUTTON")))
363 g_dialog.controls[id].type = NSCTL_BUTTON;
364 else if (!lstrcmpi(className, _T("EDIT")))
365 g_dialog.controls[id].type = NSCTL_EDIT;
366 else if (!lstrcmpi(className, _T("COMBOBOX")))
367 g_dialog.controls[id].type = NSCTL_COMBOBOX;
368 else if (!lstrcmpi(className, _T("LISTBOX")))
369 g_dialog.controls[id].type = NSCTL_LISTBOX;
370 else if (!lstrcmpi(className, _T("RichEdit")))
371 g_dialog.controls[id].type = NSCTL_RICHEDIT;
372 else if (!lstrcmpi(className, _T("RICHEDIT_CLASS")))
373 g_dialog.controls[id].type = NSCTL_RICHEDIT2;
374 else if (!lstrcmpi(className, _T("STATIC")))
375 g_dialog.controls[id].type = NSCTL_STATIC;
376 else if (!lstrcmpi(className, _T("LINK")))
377 g_dialog.controls[id].type = NSCTL_LINK;
378 else
379 g_dialog.controls[id].type = NSCTL_UNKNOWN;
380
381 // apply rtl to style
382
383 ConvertStyleToRTL(g_dialog.controls[id].type, &style, &exStyle);
384
385 // create item's window
386
387 hwItem = CreateWindowEx(
388 exStyle,
389 lstrcmpi(className, _T("LINK")) ? className : _T("BUTTON"),
390 text,
391 style,
392 x, y, width, height,
393 g_dialog.hwDialog,
394 (HMENU) (1200 + id),
395 g_hInstance,
396 NULL);
397
398 g_dialog.controls[id].window = hwItem;
399
400 // remember id
401
402 SetProp(hwItem, NSCONTROL_ID_PROP, (HANDLE) (id + 1));
403
404 // set font
405
406 SendMessage(hwItem, WM_SETFONT, SendMessage(g_dialog.hwParent, WM_GETFONT, 0, 0), TRUE);
407
408 // set the WndProc for the link control
409
410 if(g_dialog.controls[id].type == NSCTL_LINK)
411 g_dialog.controls[id].oldWndProc = (WNDPROC) SetWindowLongPtr(hwItem, GWLP_WNDPROC, (LONG_PTR) LinkWndProc);
412
413 // push back result
414
415 pushintptr((INT_PTR) hwItem);
416
417 // done
418
419 HeapFree(GetProcessHeap(), 0, className);
420 }
421
422 // for backward compatibility (2.29 had CreateItem)
423 void __declspec(dllexport) CreateItem(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
424 {
425 CreateControl(hwndParent, string_size, variables, stacktop, extra);
426 }
427
428 void __declspec(dllexport) SetUserData(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
429 {
430 HWND hwCtl;
431 struct nsControl* ctl;
432
433 // get info from stack
434
435 hwCtl = (HWND) popintptr();
436
437 if (!IsWindow(hwCtl))
438 {
439 popintptr(); // remove user data from stack
440 return;
441 }
442
443 // get descriptor
444
445 ctl = GetControl(hwCtl);
446
447 if (ctl == NULL)
448 return;
449
450 // set user data
451
452 popstringn(ctl->userData, USERDATA_SIZE);
453 }
454
455 void __declspec(dllexport) GetUserData(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
456 {
457 HWND hwCtl;
458 struct nsControl* ctl;
459
460 // get info from stack
461
462 hwCtl = (HWND) popintptr();
463
464 if (!IsWindow(hwCtl))
465 {
466 pushstring(_T(""));
467 return;
468 }
469
470 // get descriptor
471
472 ctl = GetControl(hwCtl);
473
474 if (ctl == NULL)
475 {
476 pushstring(_T(""));
477 return;
478 }
479
480 // return user data
481
482 pushstring(ctl->userData);
483 }
484
485 void CALLBACK TimerProc(HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime)
486 {
487 // we use a timer proc instead of WM_TIMER to make sure no one messes with the ids but us
488 g_pluginParms->ExecuteCodeSegment((int)(idEvent - 1), 0);
489 }
490
491 void __declspec(dllexport) CreateTimer(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
492 {
493 UINT callback;
494 UINT interval;
495
496 // get info from stack
497
498 callback = popint();
499 interval = popint();
500
501 if (!callback || !interval)
502 return;
503
504 // create timer
505
506 SetTimer(
507 g_dialog.hwDialog,
508 callback,
509 interval,
510 TimerProc);
511 }
512
513 void nsdKillTimer(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
514 {
515 UINT id;
516
517 // get timer id from stack
518
519 id = popint();
520
521 // kill timer
522
523 KillTimer(g_dialog.hwDialog, id);
524 }
525
526 void NSDFUNC SetControlCallback(size_t callbackIdx)
527 {
528 HWND hwCtl;
529 nsFunction callback;
530 nsFunction* callbacks;
531 struct nsControl* ctl;
532
533 // get info from stack
534
535 hwCtl = (HWND) popintptr();
536 callback = (nsFunction) popint();
537
538 if (!IsWindow(hwCtl))
539 return;
540
541 // get descriptor
542
543 ctl = GetControl(hwCtl);
544
545 if (ctl == NULL)
546 return;
547
548 // set callback
549
550 callbacks = (nsFunction*) &ctl->callbacks;
551 callbacks[callbackIdx] = callback;
552 }
553
554 void NSDFUNC SetDialogCallback(size_t callbackIdx)
555 {
556 nsFunction callback;
557 nsFunction* callbacks;
558
559 // get info from stack
560
561 callback = (nsFunction) popint();
562
563 // set callback
564
565 callbacks = (nsFunction*) &g_dialog.callbacks;
566 callbacks[callbackIdx] = callback;
567 }
568
569
570 void __declspec(dllexport) OnClick(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
571 {
572 SetControlCallback(CTL_CALLBACK_IDX(onClick));
573 }
574
575 void __declspec(dllexport) OnChange(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
576 {
577 SetControlCallback(CTL_CALLBACK_IDX(onChange));
578 }
579
580 void __declspec(dllexport) OnNotify(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
581 {
582 SetControlCallback(CTL_CALLBACK_IDX(onNotify));
583 }
584
585 void __declspec(dllexport) OnBack(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
586 {
587 SetDialogCallback(DLG_CALLBACK_IDX(onBack));
588 }
589
590 void __declspec(dllexport) Show(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
591 {
592 // tell NSIS to remove old inner dialog and pass handle of the new inner dialog
593
594 SendMessage(hwndParent, WM_NOTIFY_CUSTOM_READY, (WPARAM) g_dialog.hwDialog, 0);
595 ShowWindow(g_dialog.hwDialog, SW_SHOWNA);
596
597 // message loop
598
599 while (g_dialog.hwDialog)
600 {
601 MSG msg;
602 GetMessage(&msg, NULL, 0, 0);
603 if (!IsDialogMessage(g_dialog.hwDialog, &msg) && !IsDialogMessage(g_dialog.hwParent, &msg))
604 {
605 TranslateMessage(&msg);
606 DispatchMessage(&msg);
607 }
608 }
609
610 // reset wndproc
611
612 SetWindowLongPtr(hwndParent, DWLP_DLGPROC, (LONG_PTR) g_dialog.parentOriginalWndproc);
613 }
614
615 BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
616 {
617 g_hInstance = hInst;
618 return TRUE;
619 }