ceca8d1fe91582f87214bc8d9501af5b4518d5ed
[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 // ----------------------------------------------------------------------------
64 // wxWindows macros
65 // ----------------------------------------------------------------------------
66
67 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
68
69 // ----------------------------------------------------------------------------
70 // private functions prototypes
71 // ----------------------------------------------------------------------------
72
73 // free the parameter
74 static void ItemListFree(LPITEMIDLIST pidl);
75
76 // the callback proc for the dir dlg
77 static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
78 LPARAM pData);
79
80
81 // ============================================================================
82 // implementation
83 // ============================================================================
84
85 // ----------------------------------------------------------------------------
86 // wxDirDialog
87 // ----------------------------------------------------------------------------
88
89 wxDirDialog::wxDirDialog(wxWindow *parent,
90 const wxString& message,
91 const wxString& defaultPath,
92 long style,
93 const wxPoint& WXUNUSED(pos),
94 const wxSize& WXUNUSED(size),
95 const wxString& WXUNUSED(name))
96 {
97 m_message = message;
98 m_parent = parent;
99
100 SetStyle(style);
101 SetPath(defaultPath);
102 }
103
104 void wxDirDialog::SetPath(const wxString& path)
105 {
106 m_path = path;
107
108 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
109 m_path.Replace(_T("/"), _T("\\"));
110 if ( !m_path.empty() )
111 {
112 while ( *(m_path.end() - 1) == _T('\\') )
113 {
114 m_path.erase(m_path.length() - 1);
115 }
116
117 // but the root drive should have a trailing slash (again, this is just
118 // the way the native dialog works)
119 if ( *(m_path.end() - 1) == _T(':') )
120 {
121 m_path += _T('\\');
122 }
123 }
124 }
125
126 int wxDirDialog::ShowModal()
127 {
128 wxWindow *parent = GetParent();
129
130 BROWSEINFO bi;
131 bi.hwndOwner = parent ? GetHwndOf(parent) : NULL;
132 bi.pidlRoot = NULL;
133 bi.pszDisplayName = NULL;
134 bi.lpszTitle = m_message.c_str();
135 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
136 bi.lpfn = BrowseCallbackProc;
137 bi.lParam = (LPARAM)m_path.c_str(); // param for the callback
138
139 static const int verComCtl32 = wxApp::GetComCtl32Version();
140
141 // we always add the edit box (it doesn't hurt anybody, does it?) if it is
142 // supported by the system
143 if ( verComCtl32 >= 471 )
144 {
145 bi.ulFlags |= BIF_EDITBOX;
146 }
147
148 // normally the commented out part should work -- but in practice
149 // BIF_NONEWFOLDERBUTTON doesn't have any effect (Win2k, comctl 5.81) so I
150 // have to disable it [for now]
151 #if 0
152 // to have the "New Folder" button we must use the "new" dialog style which
153 // is also the only way to have a resizable dialog
154 //
155 // "new" style is only available in the version 5.0+ of comctl32.dll
156 const bool needNewDir = HasFlag(wxDD_NEW_DIR_BUTTON);
157 if ( (needNewDir || HasFlag(wxRESIZE_BORDER)) && (verComCtl32 >= 500) )
158 {
159 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
160
161 // we'll get the "New Folder" button by default now, don't show it if
162 // not needed
163 if ( !needNewDir )
164 bi.ulFlags |= BIF_NONEWFOLDERBUTTON;
165 }
166 #else
167 if ( HasFlag(wxDD_NEW_DIR_BUTTON) && verComCtl32 >= 500 )
168 {
169 // use the new style to make the "New Folder" button appear
170 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
171 }
172 #endif
173
174 // do show the dialog
175 LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
176
177 if ( bi.pidlRoot )
178 {
179 ItemListFree((LPITEMIDLIST)bi.pidlRoot);
180 }
181
182 if ( !pidl )
183 {
184 // Cancel button pressed
185 return wxID_CANCEL;
186 }
187
188 BOOL ok = SHGetPathFromIDList(pidl, wxStringBuffer(m_path, MAX_PATH));
189
190 ItemListFree(pidl);
191
192 if ( !ok )
193 {
194 wxLogLastError(wxT("SHGetPathFromIDList"));
195
196 return wxID_CANCEL;
197 }
198
199 return wxID_OK;
200 }
201
202 // ----------------------------------------------------------------------------
203 // private functions
204 // ----------------------------------------------------------------------------
205
206 static int CALLBACK
207 BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
208 {
209 switch(uMsg)
210 {
211 case BFFM_INITIALIZED:
212 // sent immediately after initialisation and so we may set the
213 // initial selection here
214 //
215 // wParam = TRUE => lParam is a string and not a PIDL
216 SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
217 break;
218
219 case BFFM_SELCHANGED:
220 // note that this doesn't work with the new style UI (MSDN doesn't
221 // say anything about it, but the comments in shlobj.h do!) but we
222 // still execute this code in case it starts working again with the
223 // "new new UI" (or would it be "NewUIEx" according to tradition?)
224 {
225 // Set the status window to the currently selected path.
226 wxString strDir;
227 if ( SHGetPathFromIDList((LPITEMIDLIST)lp,
228 wxStringBuffer(strDir, MAX_PATH)) )
229 {
230 // NB: this shouldn't be necessary with the new style box
231 // (which is resizable), but as for now it doesn't work
232 // anyhow (see the comment above) no harm in doing it
233
234 // need to truncate or it displays incorrectly
235 static const size_t maxChars = 37;
236 if ( strDir.length() > maxChars )
237 {
238 strDir = strDir.Right(maxChars);
239 strDir = wxString(wxT("...")) + strDir;
240 }
241
242 SendMessage(hwnd, BFFM_SETSTATUSTEXT,
243 0, (LPARAM)strDir.c_str());
244 }
245 }
246 break;
247
248 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
249 // if the user types in invalid dir name
250 }
251
252 return 0;
253 }
254
255
256 static void ItemListFree(LPITEMIDLIST pidl)
257 {
258 if ( pidl )
259 {
260 LPMALLOC pMalloc;
261 SHGetMalloc(&pMalloc);
262 if ( pMalloc )
263 {
264 pMalloc->Free(pidl);
265 pMalloc->Release();
266 }
267 else
268 {
269 wxLogLastError(wxT("SHGetMalloc"));
270 }
271 }
272 }
273
274 #else
275 #include "../generic/dirdlgg.cpp"
276 #endif // compiler/platform on which the code here compiles
277
278 #endif // wxUSE_DIRDLG