Debian Codegraph @

Contrib/StartMenu/StartMenu.c

raw
1 #include <windows.h>
2 #include <shlobj.h>
3 #include <nsis/pluginapi.h> // nsis plugin
4 #include "resource.h"
5
6 HINSTANCE g_hInstance;
7
8 HWND hwParent;
9 HWND hwChild;
10 HWND g_hwStartMenuSelect;
11 HWND g_hwDirList;
12
13 TCHAR buf[1024];
14 TCHAR text[1024];
15 TCHAR progname[1024];
16 TCHAR lastused[1024];
17 TCHAR checkbox[1024];
18
19 int autoadd;
20 int g_done;
21 int noicon;
22 int rtl;
23
24 WNDPROC lpWndProcOld;
25
26 void (__stdcall *validate_filename)(LPTSTR);
27
28 INT_PTR CALLBACK dlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
29 static INT_PTR CALLBACK ParentWndProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
30 void AddFolderFromReg(int nFolder);
31
32 static UINT_PTR PluginCallback(enum NSPIM msg)
33 {
34 return 0;
35 }
36
37 void __declspec(dllexport) Init(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
38 {
39 HWND hwStartMenuSelect;
40
41 hwParent = hwndParent;
42
43 validate_filename = extra->validate_filename;
44
45 EXDLL_INIT();
46
47 extra->RegisterPluginCallback(g_hInstance, PluginCallback);
48
49 g_done = 0;
50 noicon = 0;
51 rtl = 0;
52 autoadd = 0;
53
54 text[0] = 0;
55 progname[0] = 0;
56 lastused[0] = 0;
57 checkbox[0] = 0;
58
59 g_hwStartMenuSelect = NULL;
60
61 {
62 hwChild = GetDlgItem(hwndParent, 1018);
63 if (!hwChild)
64 {
65 pushstring(_T("error finding childwnd"));
66 return;
67 }
68
69 popstring(buf);
70
71 while (buf[0] == _T('/'))
72 {
73 if (!lstrcmpi(buf+1, _T("noicon")))
74 {
75 noicon = 1;
76 }
77 else if (!lstrcmpi(buf+1, _T("rtl")))
78 {
79 rtl = 1;
80 }
81 else if (!lstrcmpi(buf+1, _T("text")))
82 {
83 popstring(text);
84 }
85 else if (!lstrcmpi(buf+1, _T("autoadd")))
86 {
87 autoadd = 1;
88 }
89 else if (!lstrcmpi(buf+1, _T("lastused")))
90 {
91 popstring(lastused);
92 }
93 else if (!lstrcmpi(buf+1, _T("checknoshortcuts")))
94 {
95 popstring(checkbox);
96 }
97
98 if (popstring(buf))
99 {
100 *buf = 0;
101 }
102 }
103
104 if (*buf)
105 {
106 lstrcpy(progname, buf);
107 }
108 else
109 {
110 pushstring(_T("error reading parameters"));
111 return;
112 }
113
114 hwStartMenuSelect = CreateDialog(g_hInstance, MAKEINTRESOURCE(IDD_DIALOG), hwndParent, dlgProc);
115 g_hwStartMenuSelect = hwStartMenuSelect;
116 if (!hwStartMenuSelect)
117 {
118 pushstring(_T("error creating dialog"));
119 return;
120 }
121 else
122 {
123 lpWndProcOld = (WNDPROC) SetWindowLongPtr(hwndParent, DWLP_DLGPROC, (LONG_PTR) ParentWndProc);
124 wsprintf(buf, _T("%u"), hwStartMenuSelect);
125 pushstring(buf);
126 }
127 }
128 }
129
130 void __declspec(dllexport) Show(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop)
131 {
132 HWND hwStartMenuSelect = g_hwStartMenuSelect;
133
134 if (!hwStartMenuSelect)
135 {
136 return;
137 }
138
139 while (!g_done)
140 {
141 MSG msg;
142 GetMessage(&msg, NULL, 0, 0);
143 if (!IsDialogMessage(hwStartMenuSelect,&msg) && !IsDialogMessage(hwndParent,&msg) && !TranslateMessage(&msg))
144 DispatchMessage(&msg);
145 }
146 DestroyWindow(hwStartMenuSelect);
147
148 SetWindowLongPtr(hwndParent, DWLP_DLGPROC, (LONG_PTR) lpWndProcOld);
149 }
150
151 void __declspec(dllexport) Select(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
152 {
153 Init(hwndParent, string_size, variables, stacktop, extra);
154 if (g_hwStartMenuSelect)
155 {
156 popstring(buf);
157 Show(hwndParent, string_size, variables, stacktop);
158 }
159 }
160
161 static INT_PTR CALLBACK ParentWndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
162 {
163 INT_PTR bRes = CallWindowProc(lpWndProcOld,hwnd,message,wParam,lParam);
164 if (message == WM_NOTIFY_OUTER_NEXT && !bRes)
165 {
166 // if leave function didn't abort (lRes != 0 in that case)
167 PostMessage(g_hwStartMenuSelect,WM_USER+666,wParam,0);
168 }
169 return bRes;
170 }
171
172 void AddRTLStyle(HWND hWnd, long dwStyle)
173 {
174 LONG_PTR s;
175
176 s = GetWindowLongPtr(hWnd, GWL_STYLE);
177 SetWindowLongPtr(hWnd, GWL_STYLE, s | dwStyle);
178 s = GetWindowLongPtr(hWnd, GWL_EXSTYLE);
179 SetWindowLongPtr(hWnd, GWL_EXSTYLE, s | WS_EX_RIGHT | WS_EX_RTLREADING);
180 }
181
182 #define ProgressiveSetWindowPos(hwWindow, x, cx, cy) \
183 MoveWindow( \
184 hwWindow, \
185 x, \
186 y_offset, \
187 cx, \
188 cy, \
189 FALSE \
190 ); \
191 \
192 y_offset += cy + 3;
193
194 INT_PTR CALLBACK dlgProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
195 {
196 HWND hwLocation = GetDlgItem(hwndDlg, IDC_LOCATION);
197 HWND hwDirList = GetDlgItem(hwndDlg, IDC_DIRLIST);
198 HWND hwCheckBox = GetDlgItem(hwndDlg, IDC_CHECK);
199
200 switch (uMsg)
201 {
202 case WM_INITDIALOG:
203 {
204 HWND hwIcon;
205 HWND hwText;
206
207 RECT dialog_r, temp_r;
208
209 HFONT hFont = (HFONT) SendMessage(hwParent, WM_GETFONT, 0, 0);
210
211 int y_offset = 0;
212
213 int width, height;
214
215 int baseUnitY;
216
217 // Init dialog unit conversion
218
219 {
220 TEXTMETRIC tm;
221 HDC hDC;
222
223 hDC = GetDC(hwndDlg);
224 SelectObject(hDC, hFont);
225
226 GetTextMetrics(hDC, &tm);
227 baseUnitY = tm.tmHeight;
228
229 ReleaseDC(hwndDlg, hDC);
230 }
231
232 GetWindowRect(hwChild, &dialog_r);
233 ScreenToClient(hwParent, (LPPOINT) &dialog_r);
234 ScreenToClient(hwParent, ((LPPOINT) &dialog_r) + 1);
235
236 width = dialog_r.right - dialog_r.left;
237 height = dialog_r.bottom - dialog_r.top;
238
239 MoveWindow(
240 hwndDlg,
241 dialog_r.left,
242 dialog_r.top,
243 width,
244 height,
245 FALSE
246 );
247
248 hwIcon = GetDlgItem(hwndDlg, IDC_NSISICON);
249 hwText = GetDlgItem(hwndDlg, IDC_TEXT);
250 g_hwDirList = hwDirList;
251
252 SendMessage(hwndDlg, WM_SETFONT, (WPARAM) hFont, TRUE);
253 SendMessage(hwIcon, WM_SETFONT, (WPARAM) hFont, TRUE);
254 SendMessage(hwText, WM_SETFONT, (WPARAM) hFont, TRUE);
255 SendMessage(hwLocation, WM_SETFONT, (WPARAM) hFont, TRUE);
256 SendMessage(hwDirList, WM_SETFONT, (WPARAM) hFont, TRUE);
257 SendMessage(hwCheckBox, WM_SETFONT, (WPARAM) hFont, TRUE);
258
259 if (rtl)
260 {
261 AddRTLStyle(hwText, SS_RIGHT);
262 AddRTLStyle(hwLocation, ES_RIGHT);
263 AddRTLStyle(hwDirList, 0);
264 AddRTLStyle(hwCheckBox, BS_RIGHT | BS_LEFTTEXT);
265 }
266
267 GetClientRect(hwIcon, &temp_r);
268
269 if (!noicon)
270 {
271 SendMessage(
272 hwIcon,
273 STM_SETIMAGE,
274 IMAGE_ICON,
275 (LPARAM)LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(103))
276 );
277
278 MoveWindow(
279 hwIcon,
280 rtl ? width - temp_r.right : 0,
281 0,
282 temp_r.right,
283 temp_r.bottom,
284 FALSE
285 );
286
287 temp_r.right += 3;
288 }
289 else
290 {
291 ShowWindow(hwIcon, SW_HIDE);
292
293 temp_r.right = 0;
294 }
295
296 if (rtl)
297 {
298 ProgressiveSetWindowPos(
299 hwText,
300 0,
301 width - temp_r.right,
302 3 * baseUnitY //MulDiv(24, baseUnitY, 8);
303 );
304 }
305 else
306 {
307 ProgressiveSetWindowPos(
308 hwText,
309 temp_r.right,
310 width - temp_r.right + 3,
311 3 * baseUnitY //MulDiv(24, baseUnitY, 8);
312 );
313 }
314
315 SetWindowText(hwText, *text ? text : _T("Select the Start Menu folder in which you would like to create the program's shortcuts:"));
316
317 ProgressiveSetWindowPos(
318 hwLocation,
319 0,
320 width,
321 MulDiv(12, baseUnitY, 8)
322 );
323
324 if (*lastused == _T('>'))
325 {
326 CheckDlgButton(hwndDlg, IDC_CHECK, BST_CHECKED);
327 lstrcpy(lastused, lstrcpy(buf, lastused) + 1);
328 EnableWindow(hwDirList, FALSE);
329 EnableWindow(hwLocation, FALSE);
330 }
331
332 SetWindowText(hwLocation, *lastused ? lastused : progname);
333
334 temp_r.bottom = MulDiv(8, baseUnitY, 8);
335
336 ProgressiveSetWindowPos(
337 hwDirList,
338 0,
339 width,
340 height - y_offset - (*checkbox ? temp_r.bottom + 3 : 0)
341 );
342
343 if (*checkbox)
344 {
345 ProgressiveSetWindowPos(
346 hwCheckBox,
347 0,
348 width,
349 temp_r.bottom
350 );
351
352 ShowWindow(hwCheckBox, SW_SHOWNA);
353 SetWindowText(hwCheckBox, checkbox);
354 }
355
356 AddFolderFromReg(CSIDL_COMMON_PROGRAMS);
357 AddFolderFromReg(CSIDL_PROGRAMS);
358
359 // Tell NSIS to remove old inner dialog and pass handle of the new inner dialog
360 SendMessage(hwParent, WM_NOTIFY_CUSTOM_READY, (WPARAM)hwndDlg, 0);
361 ShowWindow(hwndDlg, SW_SHOWNA);
362 if (IsDlgButtonChecked(hwndDlg, IDC_CHECK) == BST_CHECKED)
363 SendMessage(hwndDlg, WM_NEXTDLGCTL, (WPARAM) hwCheckBox, TRUE);
364 else
365 SendMessage(hwndDlg, WM_NEXTDLGCTL, (WPARAM) hwLocation, TRUE);
366 }
367 break;
368 case WM_COMMAND:
369 if (LOWORD(wParam) == IDC_DIRLIST && HIWORD(wParam) == LBN_SELCHANGE)
370 {
371 LRESULT selection = SendMessage(hwDirList, LB_GETCURSEL, 0, 0);
372 if (selection != LB_ERR)
373 {
374 SendMessage(hwDirList, LB_GETTEXT, selection, (WPARAM)buf);
375 if (autoadd)
376 lstrcat(lstrcat(buf, _T("\\")), progname);
377 SetWindowText(hwLocation, buf);
378 }
379 }
380 else if (LOWORD(wParam) == IDC_CHECK && HIWORD(wParam) == BN_CLICKED)
381 {
382 BOOL bEnable = IsDlgButtonChecked(hwndDlg, IDC_CHECK) != BST_CHECKED;
383 EnableWindow(hwDirList, bEnable);
384 EnableWindow(hwLocation, bEnable);
385 if (bEnable)
386 goto ValidateLocation;
387 *buf = _T('!'); //This only needs to be != 0, actual value does not matter
388 goto SetOkBtn;
389 }
390 else if (LOWORD(wParam) == IDC_LOCATION && HIWORD(wParam) == EN_CHANGE)
391 {
392 ValidateLocation:
393 GetWindowText(hwLocation, buf, MAX_PATH);
394 validate_filename(buf);
395 SetOkBtn:
396 EnableWindow(GetDlgItem(hwParent, IDOK), *buf != _T('\0'));
397 }
398 break;
399 case WM_USER+666:
400 g_done = 1;
401 if (wParam == NOTIFY_BYE_BYE)
402 pushstring(_T("cancel"));
403 else
404 {
405 GetWindowText(hwLocation, buf + 1, MAX_PATH);
406 validate_filename(buf);
407 if (IsDlgButtonChecked(hwndDlg, IDC_CHECK) == BST_CHECKED)
408 {
409 buf[0] = _T('>');
410 pushstring(buf);
411 }
412 else
413 {
414 pushstring(buf + 1);
415 }
416 pushstring(_T("success"));
417 }
418 case WM_CTLCOLORSTATIC:
419 case WM_CTLCOLOREDIT:
420 case WM_CTLCOLORDLG:
421 case WM_CTLCOLORBTN:
422 case WM_CTLCOLORLISTBOX:
423 // let the NSIS window handle colors, it knows best
424 return SendMessage(hwParent, uMsg, wParam, lParam);
425 break;
426 }
427 return FALSE;
428 }
429
430 BOOL WINAPI DllMain(HINSTANCE hInst, ULONG ul_reason_for_call, LPVOID lpReserved)
431 {
432 g_hInstance=hInst;
433 return TRUE;
434 }
435
436 void AddFolderFromReg(int nFolder)
437 {
438 //DWORD idx;
439 WIN32_FIND_DATA FileData;
440 HANDLE hSearch;
441 LPITEMIDLIST ppidl;
442
443 buf[0] = 0;
444 if (SHGetSpecialFolderLocation(hwParent, nFolder, &ppidl) == S_OK)
445 {
446 SHGetPathFromIDList(ppidl, buf);
447 CoTaskMemFree(ppidl);
448 }
449
450 if (!buf[0])
451 return;
452
453 lstrcat(buf, _T("\\*.*"));
454 hSearch = FindFirstFile(buf, &FileData);
455 if (hSearch != INVALID_HANDLE_VALUE)
456 {
457 do
458 {
459 if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
460 {
461 if (lstrcmp(FileData.cFileName, _T(".")) != 0)
462 {
463 if (lstrcmp(FileData.cFileName, _T("..")) != 0)
464 {
465 if (SendMessage(g_hwDirList, LB_FINDSTRINGEXACT, (WPARAM) -1, (LPARAM)FileData.cFileName) == LB_ERR)
466 SendMessage(g_hwDirList, LB_ADDSTRING, 0, (LPARAM)FileData.cFileName);
467 /*idx = */
468 /*SendMessage(hwDirList, LB_SETITEMDATA, (WPARAM)idx,
469 (LPARAM)ExtractAssociatedIcon(g_hInstance, FileData.cFileName, (WORD*)&idx));*/
470 }
471 }
472 }
473 } while (FindNextFile(hSearch, &FileData));
474 FindClose(hSearch);
475 }
476 }