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