]>
Commit | Line | Data |
---|---|---|
40e8ee37 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: msw/stdpaths.cpp | |
3 | // Purpose: wxStandardPaths implementation for Win32 | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 2004-10-19 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 2004 Vadim Zeitlin <vadim@wxwindows.org> | |
9 | // License: wxWindows license | |
10 | /////////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // for compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
cd9a8d40 VZ |
27 | #if wxUSE_STDPATHS |
28 | ||
40e8ee37 VZ |
29 | #ifndef WX_PRECOMP |
30 | #include "wx/app.h" | |
31 | #endif //WX_PRECOMP | |
32 | ||
33 | #include "wx/dynlib.h" | |
34 | #include "wx/filename.h" | |
35 | ||
36 | #include "wx/stdpaths.h" | |
37 | ||
38 | #include "wx/msw/private.h" | |
39 | #include "wx/msw/wrapshl.h" | |
40 | ||
41 | // ---------------------------------------------------------------------------- | |
42 | // types | |
43 | // ---------------------------------------------------------------------------- | |
44 | ||
45 | typedef HRESULT (WINAPI *SHGetFolderPath_t)(HWND, int, HANDLE, DWORD, LPTSTR); | |
46 | typedef HRESULT (WINAPI *SHGetSpecialFolderPath_t)(HWND, LPTSTR, int, BOOL); | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
49 | // constants | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | // used in our wxLogTrace messages | |
53 | static const wxChar *TRACE_MASK = _T("stdpaths"); | |
54 | ||
61c2afed WS |
55 | #ifndef CSIDL_APPDATA |
56 | #define CSIDL_APPDATA 0x001a | |
57 | #endif | |
58 | ||
40e8ee37 VZ |
59 | #ifndef CSIDL_LOCAL_APPDATA |
60 | #define CSIDL_LOCAL_APPDATA 0x001c | |
61 | #endif | |
62 | ||
63 | #ifndef CSIDL_COMMON_APPDATA | |
64 | #define CSIDL_COMMON_APPDATA 0x0023 | |
65 | #endif | |
66 | ||
67 | #ifndef CSIDL_PROGRAM_FILES | |
68 | #define CSIDL_PROGRAM_FILES 0x0026 | |
69 | #endif | |
70 | ||
cd0c4800 WS |
71 | #ifndef SHGFP_TYPE_CURRENT |
72 | #define SHGFP_TYPE_CURRENT 0 | |
73 | #endif | |
74 | ||
75 | #ifndef SHGFP_TYPE_DEFAULT | |
76 | #define SHGFP_TYPE_DEFAULT 1 | |
77 | #endif | |
78 | ||
40e8ee37 VZ |
79 | // ---------------------------------------------------------------------------- |
80 | // module globals | |
81 | // ---------------------------------------------------------------------------- | |
82 | ||
83 | struct ShellFunctions | |
84 | { | |
85 | ShellFunctions() | |
86 | { | |
87 | pSHGetFolderPath = NULL; | |
88 | pSHGetSpecialFolderPath = NULL; | |
89 | initialized = false; | |
90 | } | |
91 | ||
92 | SHGetFolderPath_t pSHGetFolderPath; | |
93 | SHGetSpecialFolderPath_t pSHGetSpecialFolderPath; | |
94 | ||
95 | bool initialized; | |
96 | }; | |
97 | ||
98 | // in spite of using a static variable, this is MT-safe as in the worst case it | |
99 | // results in initializing the function pointer several times -- but this is | |
100 | // harmless | |
101 | static ShellFunctions gs_shellFuncs; | |
102 | ||
103 | // ---------------------------------------------------------------------------- | |
104 | // private functions | |
105 | // ---------------------------------------------------------------------------- | |
106 | ||
107 | static void ResolveShellFunctions() | |
108 | { | |
40e8ee37 | 109 | // start with the newest functions, fall back to the oldest ones |
780d7317 JS |
110 | #ifdef __WXWINCE__ |
111 | wxString shellDllName(_T("coredll")); | |
112 | #else | |
40e8ee37 | 113 | // first check for SHGetFolderPath (shell32.dll 5.0) |
780d7317 JS |
114 | wxString shellDllName(_T("shell32")); |
115 | #endif | |
116 | ||
117 | wxDynamicLibrary dllShellFunctions( shellDllName ); | |
118 | if ( !dllShellFunctions.IsLoaded() ) | |
40e8ee37 | 119 | { |
780d7317 JS |
120 | wxString traceMessage = wxString::Format( _T("Failed to load %s.dll"), shellDllName ); |
121 | wxLogTrace(TRACE_MASK, traceMessage ); | |
40e8ee37 VZ |
122 | } |
123 | ||
780d7317 JS |
124 | // don't give errors if the functions are unavailable, we're ready to deal |
125 | // with this | |
126 | wxLogNull noLog; | |
127 | ||
40e8ee37 | 128 | #if wxUSE_UNICODE |
780d7317 JS |
129 | #ifdef __WXWINCE__ |
130 | static const wchar_t UNICODE_SUFFIX = L''; // WinCE SH functions don't seem to have 'W' | |
131 | #else | |
132 | static const wchar_t UNICODE_SUFFIX = L'W'; | |
133 | #endif | |
40e8ee37 VZ |
134 | #else // !Unicode |
135 | static const char UNICODE_SUFFIX = 'A'; | |
136 | #endif // Unicode/!Unicode | |
137 | ||
138 | wxString funcname(_T("SHGetFolderPath")); | |
139 | gs_shellFuncs.pSHGetFolderPath = | |
780d7317 | 140 | (SHGetFolderPath_t)dllShellFunctions.GetSymbol(funcname + UNICODE_SUFFIX); |
40e8ee37 VZ |
141 | |
142 | // then for SHGetSpecialFolderPath (shell32.dll 4.71) | |
143 | if ( !gs_shellFuncs.pSHGetFolderPath ) | |
144 | { | |
145 | funcname = _T("SHGetSpecialFolderPath"); | |
146 | gs_shellFuncs.pSHGetSpecialFolderPath = (SHGetSpecialFolderPath_t) | |
780d7317 | 147 | dllShellFunctions.GetSymbol(funcname + UNICODE_SUFFIX); |
40e8ee37 VZ |
148 | } |
149 | ||
150 | // finally we fall back on SHGetSpecialFolderLocation (shell32.dll 4.0), | |
151 | // but we don't need to test for it -- it is available even under Win95 | |
152 | ||
153 | // shell32.dll is going to be unloaded, but it still remains in memory | |
154 | // because we also link to it statically, so it's ok | |
155 | ||
156 | gs_shellFuncs.initialized = true; | |
157 | } | |
158 | ||
159 | // ============================================================================ | |
160 | // wxStandardPaths implementation | |
161 | // ============================================================================ | |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
164 | // private helpers | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
167 | /* static */ | |
168 | wxString wxStandardPaths::DoGetDirectory(int csidl) | |
169 | { | |
170 | if ( !gs_shellFuncs.initialized ) | |
171 | ResolveShellFunctions(); | |
172 | ||
173 | wxString dir; | |
174 | HRESULT hr = E_FAIL; | |
175 | ||
176 | // test whether the function is available during compile-time (it must be | |
177 | // defined as either "SHGetFolderPathA" or "SHGetFolderPathW") | |
178 | #ifdef SHGetFolderPath | |
179 | // and now test whether we have it during run-time | |
180 | if ( gs_shellFuncs.pSHGetFolderPath ) | |
181 | { | |
182 | hr = gs_shellFuncs.pSHGetFolderPath | |
183 | ( | |
184 | NULL, // parent window, not used | |
185 | csidl, | |
186 | NULL, // access token (current user) | |
187 | SHGFP_TYPE_CURRENT, // current path, not just default value | |
188 | wxStringBuffer(dir, MAX_PATH) | |
189 | ); | |
190 | ||
191 | // somewhat incredibly, the error code in the Unicode version is | |
192 | // different from the one in ASCII version for this function | |
193 | #if wxUSE_UNICODE | |
194 | if ( hr == E_FAIL ) | |
195 | #else | |
196 | if ( hr == S_FALSE ) | |
197 | #endif | |
198 | { | |
199 | // directory doesn't exist, maybe we can get its default value? | |
200 | hr = gs_shellFuncs.pSHGetFolderPath | |
201 | ( | |
202 | NULL, | |
203 | csidl, | |
204 | NULL, | |
205 | SHGFP_TYPE_DEFAULT, | |
206 | wxStringBuffer(dir, MAX_PATH) | |
207 | ); | |
208 | } | |
209 | } | |
210 | #endif // SHGetFolderPath | |
211 | ||
212 | #ifdef SHGetSpecialFolderPath | |
213 | if ( FAILED(hr) && gs_shellFuncs.pSHGetSpecialFolderPath ) | |
214 | { | |
215 | hr = gs_shellFuncs.pSHGetSpecialFolderPath | |
216 | ( | |
217 | NULL, // parent window | |
218 | wxStringBuffer(dir, MAX_PATH), | |
219 | csidl, | |
220 | FALSE // don't create if doesn't exist | |
221 | ); | |
222 | } | |
223 | #endif // SHGetSpecialFolderPath | |
224 | ||
225 | // SHGetSpecialFolderLocation should be available with all compilers and | |
226 | // under all Win32 systems, so don't test for it (and as it doesn't exist | |
227 | // in "A" and "W" versions anyhow, testing would be more involved, too) | |
228 | if ( FAILED(hr) ) | |
229 | { | |
230 | LPITEMIDLIST pidl; | |
231 | hr = SHGetSpecialFolderLocation(NULL, csidl, &pidl); | |
232 | ||
233 | if ( SUCCEEDED(hr) ) | |
234 | { | |
235 | // creating this temp object has (nice) side effect of freeing pidl | |
236 | dir = wxItemIdList(pidl).GetPath(); | |
237 | } | |
238 | } | |
239 | ||
240 | return dir; | |
241 | } | |
242 | ||
40e8ee37 VZ |
243 | // ---------------------------------------------------------------------------- |
244 | // public functions | |
245 | // ---------------------------------------------------------------------------- | |
246 | ||
247 | wxString wxStandardPaths::GetConfigDir() const | |
248 | { | |
249 | return AppendAppName(DoGetDirectory(CSIDL_COMMON_APPDATA)); | |
250 | } | |
251 | ||
252 | wxString wxStandardPaths::GetUserConfigDir() const | |
253 | { | |
254 | return DoGetDirectory(CSIDL_APPDATA); | |
255 | } | |
256 | ||
257 | wxString wxStandardPaths::GetDataDir() const | |
258 | { | |
90537299 VZ |
259 | // under Windows each program is usually installed in its own directory and |
260 | // so its datafiles are in the same directory as its main executable | |
261 | return wxFileName(wxGetFullModuleName()).GetPath(); | |
40e8ee37 VZ |
262 | } |
263 | ||
264 | wxString wxStandardPaths::GetUserDataDir() const | |
265 | { | |
266 | return AppendAppName(GetUserConfigDir()); | |
267 | } | |
268 | ||
269 | wxString wxStandardPaths::GetUserLocalDataDir() const | |
270 | { | |
271 | return AppendAppName(DoGetDirectory(CSIDL_LOCAL_APPDATA)); | |
272 | } | |
273 | ||
274 | wxString wxStandardPaths::GetPluginsDir() const | |
275 | { | |
276 | return wxFileName(wxGetFullModuleName()).GetPath(); | |
277 | } | |
278 | ||
279 | ||
280 | // ============================================================================ | |
281 | // wxStandardPathsWin16 implementation | |
282 | // ============================================================================ | |
283 | ||
284 | wxString wxStandardPathsWin16::GetConfigDir() const | |
285 | { | |
286 | // this is for compatibility with earlier wxFileConfig versions | |
287 | // which used the Windows directory for the global files | |
288 | wxString dir; | |
e2072525 | 289 | #ifndef __WXWINCE__ |
40e8ee37 VZ |
290 | if ( !::GetWindowsDirectory(wxStringBuffer(dir, MAX_PATH), MAX_PATH) ) |
291 | { | |
292 | wxLogLastError(_T("GetWindowsDirectory")); | |
293 | } | |
e2072525 | 294 | #else |
503602df | 295 | // TODO: use CSIDL_WINDOWS (eVC4, possibly not eVC3) |
4fb9d560 | 296 | dir = wxT("\\Windows"); |
e2072525 | 297 | #endif |
40e8ee37 VZ |
298 | |
299 | return dir; | |
300 | } | |
301 | ||
302 | wxString wxStandardPathsWin16::GetUserConfigDir() const | |
303 | { | |
304 | // again, for wxFileConfig which uses $HOME for its user config file | |
305 | return wxGetHomeDir(); | |
306 | } | |
307 | ||
cd9a8d40 | 308 | #endif // wxUSE_STDPATHS |