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