]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dirdlg.cpp
wxExecute may only be called from the main thread
[wxWidgets.git] / src / msw / dirdlg.cpp
CommitLineData
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$
8// Copyright: (c) Julian Smart and Markus Holzem
3f2711d5 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
3f2711d5
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
1e6feb95 19
2bda0e17 20#ifdef __GNUG__
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
1e6feb95
VZ
31#if wxUSE_DIRDLG
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
e7b0dcd9 45#include <shlobj.h> // Win95 shell
2bda0e17 46
3f2711d5
VZ
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
2bda0e17 50
3f2711d5 51#ifndef MAX_PATH
54744d3a 52 #define MAX_PATH 4096 // be generous
3f2711d5
VZ
53#endif
54
cf54972e
VZ
55#ifndef BIF_NEWDIALOGSTYLE
56 #define BIF_NEWDIALOGSTYLE 0x0040
57#endif
58
59#ifndef BIF_NONEWFOLDERBUTTON
60 #define BIF_NONEWFOLDERBUTTON 0x0200
61#endif
62
94311eef
MB
63#ifndef BIF_EDITBOX
64 #define BIF_EDITBOX 16
65#endif
66
3f2711d5
VZ
67// ----------------------------------------------------------------------------
68// wxWindows macros
69// ----------------------------------------------------------------------------
2bda0e17 70
54744d3a 71IMPLEMENT_CLASS(wxDirDialog, wxDialog)
2bda0e17 72
3f2711d5
VZ
73// ----------------------------------------------------------------------------
74// private functions prototypes
75// ----------------------------------------------------------------------------
76
77// free the parameter
78static void ItemListFree(LPITEMIDLIST pidl);
bfa2e032 79
3f2711d5
VZ
80// the callback proc for the dir dlg
81static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
82 LPARAM pData);
bfa2e032 83
54744d3a 84
3f2711d5
VZ
85// ============================================================================
86// implementation
87// ============================================================================
bfa2e032 88
3f2711d5
VZ
89// ----------------------------------------------------------------------------
90// wxDirDialog
91// ----------------------------------------------------------------------------
92
93wxDirDialog::wxDirDialog(wxWindow *parent,
94 const wxString& message,
95 const wxString& defaultPath,
90a6f1cc 96 long style,
e78d4a23
VZ
97 const wxPoint& WXUNUSED(pos),
98 const wxSize& WXUNUSED(size),
99 const wxString& WXUNUSED(name))
2bda0e17
KB
100{
101 m_message = message;
2bda0e17 102 m_parent = parent;
90a6f1cc
RD
103
104 SetStyle(style);
f3558acc
VZ
105 SetPath(defaultPath);
106}
107
108void wxDirDialog::SetPath(const wxString& path)
109{
110 m_path = path;
e7b0dcd9
VZ
111
112 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
113 m_path.Replace(_T("/"), _T("\\"));
f3558acc 114 if ( !m_path.empty() )
e7b0dcd9 115 {
f3558acc
VZ
116 while ( *(m_path.end() - 1) == _T('\\') )
117 {
da03e330
VZ
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('\\');
f3558acc 126 }
e7b0dcd9 127 }
2bda0e17
KB
128}
129
3f2711d5 130int wxDirDialog::ShowModal()
2bda0e17 131{
f3558acc
VZ
132 wxWindow *parent = GetParent();
133
2bda0e17 134 BROWSEINFO bi;
f3558acc 135 bi.hwndOwner = parent ? GetHwndOf(parent) : NULL;
3f2711d5
VZ
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
cf54972e
VZ
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 )
90a6f1cc 172 {
cf54972e 173 // use the new style to make the "New Folder" button appear
90a6f1cc
RD
174 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
175 }
cf54972e 176#endif
90a6f1cc 177
cf54972e 178 // do show the dialog
3f2711d5
VZ
179 LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
180
181 if ( bi.pidlRoot )
182 {
183 ItemListFree((LPITEMIDLIST)bi.pidlRoot);
184 }
185
186 if ( !pidl )
2bda0e17 187 {
3f2711d5 188 // Cancel button pressed
2bda0e17
KB
189 return wxID_CANCEL;
190 }
191
cf54972e 192 BOOL ok = SHGetPathFromIDList(pidl, wxStringBuffer(m_path, MAX_PATH));
3f2711d5
VZ
193
194 ItemListFree(pidl);
195
196 if ( !ok )
197 {
f6bcfd97 198 wxLogLastError(wxT("SHGetPathFromIDList"));
3f2711d5 199
2bda0e17 200 return wxID_CANCEL;
2bda0e17 201 }
3f2711d5
VZ
202
203 return wxID_OK;
3f2711d5
VZ
204}
205
206// ----------------------------------------------------------------------------
207// private functions
208// ----------------------------------------------------------------------------
209
210static int CALLBACK
211BrowseCallbackProc(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:
cf54972e
VZ
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?)
3f2711d5
VZ
228 {
229 // Set the status window to the currently selected path.
cf54972e
VZ
230 wxString strDir;
231 if ( SHGetPathFromIDList((LPITEMIDLIST)lp,
232 wxStringBuffer(strDir, MAX_PATH)) )
3f2711d5 233 {
cf54972e
VZ
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 )
6a097cd6 241 {
cf54972e 242 strDir = strDir.Right(maxChars);
6a097cd6
JS
243 strDir = wxString(wxT("...")) + strDir;
244 }
cf54972e
VZ
245
246 SendMessage(hwnd, BFFM_SETSTATUSTEXT,
247 0, (LPARAM)strDir.c_str());
3f2711d5
VZ
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
260static 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 {
f6bcfd97 273 wxLogLastError(wxT("SHGetMalloc"));
3f2711d5
VZ
274 }
275 }
2bda0e17
KB
276}
277
54744d3a
VZ
278#else
279 #include "../generic/dirdlgg.cpp"
280#endif // compiler/platform on which the code here compiles
1e6feb95
VZ
281
282#endif // wxUSE_DIRDLG