]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dirdlg.cpp | |
3 | // Purpose: wxDirDialog | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 01/02/97 | |
7 | // RCS-ID: $Id$ | |
6c9a19aa | 8 | // Copyright: (c) Julian Smart |
65571936 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3f2711d5 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
1e6feb95 | 19 | |
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
3f2711d5 | 21 | #pragma implementation "dirdlg.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
3f2711d5 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
3180bc0e | 31 | #if wxUSE_DIRDLG && !(defined(__SMARTPHONE__) && defined(__WXWINCE__)) |
1e6feb95 | 32 | |
7fc0bd1c | 33 | #if defined(__WIN95__) && !defined(__GNUWIN32_OLD__) && wxUSE_OLE |
54744d3a | 34 | |
2bda0e17 | 35 | #ifndef WX_PRECOMP |
3f2711d5 VZ |
36 | #include "wx/utils.h" |
37 | #include "wx/dialog.h" | |
38 | #include "wx/dirdlg.h" | |
77902671 | 39 | #include "wx/log.h" |
15ad98b7 | 40 | #include "wx/app.h" // for GetComCtl32Version() |
2bda0e17 KB |
41 | #endif |
42 | ||
3f2711d5 VZ |
43 | #include "wx/msw/private.h" |
44 | ||
4676948b JS |
45 | #ifdef __WXWINCE__ |
46 | #include <winreg.h> | |
47 | #include <objbase.h> | |
48 | #include <shlguid.h> | |
49 | #endif | |
e7b0dcd9 | 50 | #include <shlobj.h> // Win95 shell |
2bda0e17 | 51 | |
3f2711d5 VZ |
52 | // ---------------------------------------------------------------------------- |
53 | // constants | |
54 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 55 | |
3f2711d5 | 56 | #ifndef MAX_PATH |
54744d3a | 57 | #define MAX_PATH 4096 // be generous |
3f2711d5 VZ |
58 | #endif |
59 | ||
cf54972e VZ |
60 | #ifndef BIF_NEWDIALOGSTYLE |
61 | #define BIF_NEWDIALOGSTYLE 0x0040 | |
62 | #endif | |
63 | ||
64 | #ifndef BIF_NONEWFOLDERBUTTON | |
65 | #define BIF_NONEWFOLDERBUTTON 0x0200 | |
66 | #endif | |
67 | ||
94311eef MB |
68 | #ifndef BIF_EDITBOX |
69 | #define BIF_EDITBOX 16 | |
70 | #endif | |
71 | ||
3f2711d5 | 72 | // ---------------------------------------------------------------------------- |
77ffb593 | 73 | // wxWidgets macros |
3f2711d5 | 74 | // ---------------------------------------------------------------------------- |
2bda0e17 | 75 | |
54744d3a | 76 | IMPLEMENT_CLASS(wxDirDialog, wxDialog) |
2bda0e17 | 77 | |
3f2711d5 VZ |
78 | // ---------------------------------------------------------------------------- |
79 | // private functions prototypes | |
80 | // ---------------------------------------------------------------------------- | |
81 | ||
82 | // free the parameter | |
83 | static void ItemListFree(LPITEMIDLIST pidl); | |
bfa2e032 | 84 | |
3f2711d5 VZ |
85 | // the callback proc for the dir dlg |
86 | static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, | |
87 | LPARAM pData); | |
bfa2e032 | 88 | |
54744d3a | 89 | |
3f2711d5 VZ |
90 | // ============================================================================ |
91 | // implementation | |
92 | // ============================================================================ | |
bfa2e032 | 93 | |
3f2711d5 VZ |
94 | // ---------------------------------------------------------------------------- |
95 | // wxDirDialog | |
96 | // ---------------------------------------------------------------------------- | |
97 | ||
98 | wxDirDialog::wxDirDialog(wxWindow *parent, | |
99 | const wxString& message, | |
100 | const wxString& defaultPath, | |
90a6f1cc | 101 | long style, |
e78d4a23 VZ |
102 | const wxPoint& WXUNUSED(pos), |
103 | const wxSize& WXUNUSED(size), | |
104 | const wxString& WXUNUSED(name)) | |
2bda0e17 KB |
105 | { |
106 | m_message = message; | |
2bda0e17 | 107 | m_parent = parent; |
90a6f1cc RD |
108 | |
109 | SetStyle(style); | |
f3558acc VZ |
110 | SetPath(defaultPath); |
111 | } | |
112 | ||
113 | void wxDirDialog::SetPath(const wxString& path) | |
114 | { | |
115 | m_path = path; | |
e7b0dcd9 VZ |
116 | |
117 | // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes | |
118 | m_path.Replace(_T("/"), _T("\\")); | |
f3558acc | 119 | if ( !m_path.empty() ) |
e7b0dcd9 | 120 | { |
f3558acc VZ |
121 | while ( *(m_path.end() - 1) == _T('\\') ) |
122 | { | |
da03e330 VZ |
123 | m_path.erase(m_path.length() - 1); |
124 | } | |
125 | ||
126 | // but the root drive should have a trailing slash (again, this is just | |
127 | // the way the native dialog works) | |
128 | if ( *(m_path.end() - 1) == _T(':') ) | |
129 | { | |
130 | m_path += _T('\\'); | |
f3558acc | 131 | } |
e7b0dcd9 | 132 | } |
2bda0e17 KB |
133 | } |
134 | ||
3f2711d5 | 135 | int wxDirDialog::ShowModal() |
2bda0e17 | 136 | { |
f3558acc VZ |
137 | wxWindow *parent = GetParent(); |
138 | ||
2bda0e17 | 139 | BROWSEINFO bi; |
f3558acc | 140 | bi.hwndOwner = parent ? GetHwndOf(parent) : NULL; |
3f2711d5 VZ |
141 | bi.pidlRoot = NULL; |
142 | bi.pszDisplayName = NULL; | |
58e0338a JS |
143 | // Please don't change this without checking it compiles |
144 | // with eVC++ first. | |
145 | #if defined(__POCKETPC__) || defined(__SMARTPHONE__) | |
146 | bi.lpszTitle = m_message.mb_str(); | |
147 | #else | |
3f2711d5 | 148 | bi.lpszTitle = m_message.c_str(); |
58e0338a | 149 | #endif |
3f2711d5 VZ |
150 | bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT; |
151 | bi.lpfn = BrowseCallbackProc; | |
152 | bi.lParam = (LPARAM)m_path.c_str(); // param for the callback | |
153 | ||
cf54972e VZ |
154 | static const int verComCtl32 = wxApp::GetComCtl32Version(); |
155 | ||
156 | // we always add the edit box (it doesn't hurt anybody, does it?) if it is | |
157 | // supported by the system | |
158 | if ( verComCtl32 >= 471 ) | |
159 | { | |
160 | bi.ulFlags |= BIF_EDITBOX; | |
161 | } | |
162 | ||
cf54972e VZ |
163 | // to have the "New Folder" button we must use the "new" dialog style which |
164 | // is also the only way to have a resizable dialog | |
165 | // | |
166 | // "new" style is only available in the version 5.0+ of comctl32.dll | |
167 | const bool needNewDir = HasFlag(wxDD_NEW_DIR_BUTTON); | |
168 | if ( (needNewDir || HasFlag(wxRESIZE_BORDER)) && (verComCtl32 >= 500) ) | |
169 | { | |
03776753 JS |
170 | if (needNewDir) |
171 | { | |
172 | bi.ulFlags |= BIF_NEWDIALOGSTYLE; | |
173 | } | |
174 | else | |
175 | { | |
176 | // Versions < 600 doesn't support BIF_NONEWFOLDERBUTTON | |
177 | // The only way to get rid of the Make New Folder button is use | |
178 | // the old dialog style which doesn't have the button thus we | |
179 | // simply don't set the New Dialog Style for such comctl versions. | |
180 | if (verComCtl32 >= 600) | |
181 | { | |
182 | bi.ulFlags |= BIF_NEWDIALOGSTYLE; | |
183 | bi.ulFlags |= BIF_NONEWFOLDERBUTTON; | |
184 | } | |
185 | } | |
90a6f1cc RD |
186 | } |
187 | ||
cf54972e | 188 | // do show the dialog |
3f2711d5 VZ |
189 | LPITEMIDLIST pidl = SHBrowseForFolder(&bi); |
190 | ||
191 | if ( bi.pidlRoot ) | |
192 | { | |
193 | ItemListFree((LPITEMIDLIST)bi.pidlRoot); | |
194 | } | |
195 | ||
196 | if ( !pidl ) | |
2bda0e17 | 197 | { |
3f2711d5 | 198 | // Cancel button pressed |
2bda0e17 KB |
199 | return wxID_CANCEL; |
200 | } | |
201 | ||
cf54972e | 202 | BOOL ok = SHGetPathFromIDList(pidl, wxStringBuffer(m_path, MAX_PATH)); |
3f2711d5 VZ |
203 | |
204 | ItemListFree(pidl); | |
205 | ||
206 | if ( !ok ) | |
207 | { | |
f6bcfd97 | 208 | wxLogLastError(wxT("SHGetPathFromIDList")); |
3f2711d5 | 209 | |
2bda0e17 | 210 | return wxID_CANCEL; |
2bda0e17 | 211 | } |
3f2711d5 VZ |
212 | |
213 | return wxID_OK; | |
3f2711d5 VZ |
214 | } |
215 | ||
216 | // ---------------------------------------------------------------------------- | |
217 | // private functions | |
218 | // ---------------------------------------------------------------------------- | |
219 | ||
220 | static int CALLBACK | |
221 | BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) | |
222 | { | |
223 | switch(uMsg) | |
224 | { | |
225 | case BFFM_INITIALIZED: | |
226 | // sent immediately after initialisation and so we may set the | |
227 | // initial selection here | |
228 | // | |
229 | // wParam = TRUE => lParam is a string and not a PIDL | |
4676948b | 230 | #ifndef __WXWINCE__ |
d71cc120 | 231 | ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData); |
4676948b | 232 | #endif |
3f2711d5 VZ |
233 | break; |
234 | ||
235 | case BFFM_SELCHANGED: | |
cf54972e VZ |
236 | // note that this doesn't work with the new style UI (MSDN doesn't |
237 | // say anything about it, but the comments in shlobj.h do!) but we | |
238 | // still execute this code in case it starts working again with the | |
239 | // "new new UI" (or would it be "NewUIEx" according to tradition?) | |
3f2711d5 VZ |
240 | { |
241 | // Set the status window to the currently selected path. | |
cf54972e VZ |
242 | wxString strDir; |
243 | if ( SHGetPathFromIDList((LPITEMIDLIST)lp, | |
244 | wxStringBuffer(strDir, MAX_PATH)) ) | |
3f2711d5 | 245 | { |
cf54972e VZ |
246 | // NB: this shouldn't be necessary with the new style box |
247 | // (which is resizable), but as for now it doesn't work | |
248 | // anyhow (see the comment above) no harm in doing it | |
249 | ||
250 | // need to truncate or it displays incorrectly | |
251 | static const size_t maxChars = 37; | |
252 | if ( strDir.length() > maxChars ) | |
6a097cd6 | 253 | { |
cf54972e | 254 | strDir = strDir.Right(maxChars); |
6a097cd6 JS |
255 | strDir = wxString(wxT("...")) + strDir; |
256 | } | |
cf54972e VZ |
257 | |
258 | SendMessage(hwnd, BFFM_SETSTATUSTEXT, | |
259 | 0, (LPARAM)strDir.c_str()); | |
3f2711d5 VZ |
260 | } |
261 | } | |
262 | break; | |
263 | ||
264 | //case BFFM_VALIDATEFAILED: -- might be used to provide custom message | |
265 | // if the user types in invalid dir name | |
266 | } | |
267 | ||
268 | return 0; | |
269 | } | |
270 | ||
271 | ||
272 | static void ItemListFree(LPITEMIDLIST pidl) | |
273 | { | |
274 | if ( pidl ) | |
275 | { | |
276 | LPMALLOC pMalloc; | |
277 | SHGetMalloc(&pMalloc); | |
278 | if ( pMalloc ) | |
279 | { | |
280 | pMalloc->Free(pidl); | |
281 | pMalloc->Release(); | |
282 | } | |
283 | else | |
284 | { | |
f6bcfd97 | 285 | wxLogLastError(wxT("SHGetMalloc")); |
3f2711d5 VZ |
286 | } |
287 | } | |
2bda0e17 KB |
288 | } |
289 | ||
54744d3a VZ |
290 | #else |
291 | #include "../generic/dirdlgg.cpp" | |
292 | #endif // compiler/platform on which the code here compiles | |
1e6feb95 | 293 | |
3180bc0e | 294 | #endif // wxUSE_DIRDLG && !(__SMARTPHONE__ && __WXWINCE__) |