Debian Codegraph @

Contrib/nsDialogs/browse.c

raw
1 // Unicode support by Jim Park -- 08/10/2007
2
3 #include <windows.h>
4 #include <shlobj.h>
5
6 #include <nsis/pluginapi.h> // nsis plugin
7 #include <nsis/nsis_tchar.h>
8
9 #include "defs.h"
10
11 #ifndef COUNTOF
12 #define COUNTOF(a) (sizeof(a)/sizeof(a[0]))
13 #endif
14
15 int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) {
16 if (uMsg == BFFM_INITIALIZED)
17 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
18
19 return 0;
20 }
21
22 void __declspec(dllexport) SelectFolderDialog(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
23 {
24 BROWSEINFO bi;
25
26 TCHAR result[MAX_PATH];
27 TCHAR initial[MAX_PATH];
28 TCHAR title[1024];
29 LPITEMIDLIST resultPIDL;
30
31 EXDLL_INIT();
32
33 if (popstringn(title, COUNTOF(initial)))
34 {
35 pushstring(_T("error"));
36 return;
37 }
38
39 if (popstringn(initial, COUNTOF(title)))
40 {
41 pushstring(_T("error"));
42 return;
43 }
44
45 bi.hwndOwner = hwndParent;
46 bi.pidlRoot = NULL;
47 bi.pszDisplayName = result;
48 bi.lpszTitle = title;
49 #ifndef BIF_NEWDIALOGSTYLE
50 #define BIF_NEWDIALOGSTYLE 0x0040
51 #endif
52 bi.ulFlags = BIF_STATUSTEXT | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
53 bi.lpfn = BrowseCallbackProc;
54 bi.lParam = (LPARAM) initial;
55 bi.iImage = 0;
56
57 /*if (pField->pszRoot) {
58 LPSHELLFOLDER sf;
59 ULONG eaten;
60 LPITEMIDLIST root;
61 int ccRoot = (lstrlen(pField->pszRoot) * 2) + 2;
62 LPWSTR pwszRoot = (LPWSTR) MALLOC(ccRoot);
63 MultiByteToWideChar(CP_ACP, 0, pField->pszRoot, -1, pwszRoot, ccRoot);
64 SHGetDesktopFolder(&sf);
65 sf->ParseDisplayName(hConfigWindow, NULL, pwszRoot, &eaten, &root, NULL);
66 bi.pidlRoot = root;
67 sf->Release();
68 FREE(pwszRoot);
69 }*/
70
71 resultPIDL = SHBrowseForFolder(&bi);
72 if (!resultPIDL)
73 {
74 pushstring(_T("error"));
75 return;
76 }
77
78 if (SHGetPathFromIDList(resultPIDL, result))
79 {
80 pushstring(result);
81 }
82 else
83 {
84 pushstring(_T("error"));
85 }
86
87 CoTaskMemFree(resultPIDL);
88 }
89
90 void __declspec(dllexport) SelectFileDialog(HWND hwndParent, int string_size, TCHAR *variables, stack_t **stacktop, extra_parameters *extra)
91 {
92 OPENFILENAME ofn={0,};
93 int save;
94 TCHAR type[5];
95 static TCHAR path[1024];
96 static TCHAR filter[1024+1];
97 static TCHAR currentDirectory[1024];
98 static TCHAR initialDir[1024];
99 DWORD gfa;
100
101 EXDLL_INIT();
102
103 ofn.lStructSize = sizeof(OPENFILENAME);
104 ofn.hwndOwner = hwndParent;
105 ofn.lpstrFilter = filter;
106 ofn.lpstrFile = path;
107 ofn.nMaxFile = COUNTOF(path);
108 //ofn.Flags = pField->nFlags & (OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_CREATEPROMPT | OFN_EXPLORER);
109 ofn.Flags = OFN_CREATEPROMPT | OFN_EXPLORER;
110
111 popstringn(type, COUNTOF(type));
112 popstringn(path, COUNTOF(path));
113 popstringn(filter, COUNTOF(filter)-1);
114
115 save = !lstrcmpi(type, _T("save"));
116
117 // Check if the path given is a folder. If it is we initialize the
118 // ofn.lpstrInitialDir parameter
119 gfa = GetFileAttributes(path);
120 if ((gfa != INVALID_FILE_ATTRIBUTES) && (gfa & FILE_ATTRIBUTE_DIRECTORY))
121 {
122 lstrcpy(initialDir, path);
123 ofn.lpstrInitialDir = initialDir;
124 path[0] = _T('\0'); // disable initial file selection as path is actually a directory
125 }
126
127 if (!filter[0])
128 {
129 lstrcpy(filter, _T("All Files|*.*"));
130 }
131
132 {
133 // Convert the filter to the format required by Windows: NULL after each
134 // item followed by a terminating NULL
135 TCHAR *p = filter;
136 while (*p)
137 {
138 if (*p == _T('|'))
139 {
140 *p++ = 0;
141 }
142 else
143 {
144 p = CharNext(p);
145 }
146 }
147 p++;
148 *p = 0;
149 }
150
151 GetCurrentDirectory(COUNTOF(currentDirectory), currentDirectory); // save working dir
152
153 if ((save ? GetSaveFileName(&ofn) : GetOpenFileName(&ofn)))
154 {
155 pushstring(path);
156 }
157 else if (CommDlgExtendedError() == FNERR_INVALIDFILENAME)
158 {
159 *path = _T('\0');
160 if ((save ? GetSaveFileName(&ofn) : GetOpenFileName(&ofn)))
161 {
162 pushstring(path);
163 }
164 else
165 {
166 pushstring(_T(""));
167 }
168 }
169 else
170 {
171 pushstring(_T(""));
172 }
173
174 // restore working dir
175 // OFN_NOCHANGEDIR doesn't always work (see MSDN)
176 SetCurrentDirectory(currentDirectory);
177 }