]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dirdlg.cpp
Fix crash in wxFileConfig when deleting last entry of the root group.
[wxWidgets.git] / src / msw / dirdlg.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
a71d815b 2// Name: src/msw/dirdlg.cpp
2bda0e17
KB
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
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
3f2711d5 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
08bce501 27#if wxUSE_DIRDLG
1e6feb95 28
141d782d
VZ
29#if wxUSE_OLE && !defined(__GNUWIN32_OLD__) && (!defined(__WXWINCE__) || \
30 (defined(__HANDHELDPC__) && (_WIN32_WCE >= 500)))
54744d3a 31
1ab440bc
WS
32#include "wx/dirdlg.h"
33
2bda0e17 34#ifndef WX_PRECOMP
3f2711d5
VZ
35 #include "wx/utils.h"
36 #include "wx/dialog.h"
77902671 37 #include "wx/log.h"
15ad98b7 38 #include "wx/app.h" // for GetComCtl32Version()
2bda0e17
KB
39#endif
40
3f2711d5 41#include "wx/msw/private.h"
6bb09706 42#include "wx/msw/wrapshl.h"
580ffdf4
VZ
43#include "wx/msw/private/comptr.h"
44#include "wx/dynlib.h"
45
46#include <initguid.h>
47
48// We can only use IFileDialog under desktop Windows and we need
49// wxDynamicLibrary for it.
50#if wxUSE_DYNLIB_CLASS && !defined(__WXWINCE__)
51 #define wxUSE_IFILEDIALOG 1
52#else
53 #define wxUSE_IFILEDIALOG
54#endif
55
56#if wxUSE_IFILEDIALOG
57// IFileDialog related declarations missing from some compilers headers.
58
59// IShellItem
60#ifndef __IShellItem_INTERFACE_DEFINED__
61
62#ifndef SIGDN_FILESYSPATH
63 #define SIGDN_FILESYSPATH 0x80058000
64#endif
65
66struct IShellItem : public IUnknown
67{
68 virtual HRESULT wxSTDCALL BindToHandler(IBindCtx*, REFGUID, REFIID, void**) = 0;
69 virtual HRESULT wxSTDCALL GetParent(IShellItem**) = 0;
70 virtual HRESULT wxSTDCALL GetDisplayName(DWORD, LPWSTR*) = 0;
71 virtual HRESULT wxSTDCALL GetAttributes(ULONG, ULONG*) = 0;
72 virtual HRESULT wxSTDCALL Compare(IShellItem*, DWORD, int*) = 0;
73};
74
75DEFINE_GUID(IID_IShellItem,
76 0x43826D1E, 0xE718, 0x42EE, 0xBC, 0x55, 0xA1, 0xE2, 0x61, 0xC3, 0x7B, 0xFE);
77
78#endif // #ifndef __IShellItem_INTERFACE_DEFINED__
79
80struct IShellItemFilter;
81struct IFileDialogEvents;
82
83// IModalWindow
84#ifndef __IModalWindow_INTERFACE_DEFINED__
85
86struct IModalWindow : public IUnknown
87{
88 virtual HRESULT wxSTDCALL Show(HWND) = 0;
89};
90
91#endif // #ifndef __IModalWindow_INTERFACE_DEFINED__
92
93// IFileDialog
94#ifndef __IFileDialog_INTERFACE_DEFINED__
95
96#ifndef FOS_PICKFOLDERS
97 #define FOS_PICKFOLDERS 0x20
98#endif
99
100#ifndef FOS_FORCEFILESYSTEM
101 #define FOS_FORCEFILESYSTEM 0x40
102#endif
103
104struct COMDLG_FILTERSPEC;
105
106struct IFileDialog : public IModalWindow
107{
108 virtual HRESULT wxSTDCALL SetFileTypes(UINT, const COMDLG_FILTERSPEC*) = 0;
109 virtual HRESULT wxSTDCALL SetFileTypeIndex(UINT) = 0;
110 virtual HRESULT wxSTDCALL GetFileTypeIndex(UINT*) = 0;
111 virtual HRESULT wxSTDCALL Advise(IFileDialogEvents*, DWORD*) = 0;
112 virtual HRESULT wxSTDCALL Unadvise(DWORD) = 0;
113 virtual HRESULT wxSTDCALL SetOptions(DWORD) = 0;
114 virtual HRESULT wxSTDCALL GetOptions(DWORD*) = 0;
115 virtual HRESULT wxSTDCALL SetDefaultFolder(IShellItem*) = 0;
116 virtual HRESULT wxSTDCALL SetFolder(IShellItem*) = 0;
117 virtual HRESULT wxSTDCALL GetFolder(IShellItem**) = 0;
118 virtual HRESULT wxSTDCALL GetCurrentSelection(IShellItem**) = 0;
119 virtual HRESULT wxSTDCALL SetFileName(LPCWSTR) = 0;
120 virtual HRESULT wxSTDCALL GetFileName(LPWSTR*) = 0;
121 virtual HRESULT wxSTDCALL SetTitle(LPCWSTR) = 0;
122 virtual HRESULT wxSTDCALL SetOkButtonLabel(LPCWSTR) = 0;
123 virtual HRESULT wxSTDCALL SetFileNameLabel(LPCWSTR) = 0;
124 virtual HRESULT wxSTDCALL GetResult(IShellItem**) = 0;
125 virtual HRESULT wxSTDCALL AddPlace(IShellItem*, DWORD) = 0;
126 virtual HRESULT wxSTDCALL SetDefaultExtension(LPCWSTR) = 0;
127 virtual HRESULT wxSTDCALL Close(HRESULT) = 0;
128 virtual HRESULT wxSTDCALL SetClientGuid(REFGUID) = 0;
129 virtual HRESULT wxSTDCALL ClearClientData() = 0;
130 virtual HRESULT wxSTDCALL SetFilter(IShellItemFilter*) = 0;
131};
132
133DEFINE_GUID(CLSID_FileOpenDialog,
134 0xDC1C5A9C, 0xE88A, 0x4dde, 0xA5, 0xA1, 0x60, 0xF8, 0x2A, 0x20, 0xAE, 0xF7);
135
136DEFINE_GUID(IID_IFileDialog,
137 0x42F85136, 0xDB7E, 0x439C, 0x85, 0xF1, 0xE4, 0x07, 0x5D, 0x13, 0x5F, 0xC8);
138
139#endif // #ifndef __IFileDialog_INTERFACE_DEFINED__
140
141#endif // wxUSE_IFILEDIALOG
2bda0e17 142
3f2711d5
VZ
143// ----------------------------------------------------------------------------
144// constants
145// ----------------------------------------------------------------------------
2bda0e17 146
cf54972e
VZ
147#ifndef BIF_NEWDIALOGSTYLE
148 #define BIF_NEWDIALOGSTYLE 0x0040
149#endif
150
151#ifndef BIF_NONEWFOLDERBUTTON
152 #define BIF_NONEWFOLDERBUTTON 0x0200
153#endif
154
94311eef
MB
155#ifndef BIF_EDITBOX
156 #define BIF_EDITBOX 16
157#endif
158
3f2711d5 159// ----------------------------------------------------------------------------
77ffb593 160// wxWidgets macros
3f2711d5 161// ----------------------------------------------------------------------------
2bda0e17 162
54744d3a 163IMPLEMENT_CLASS(wxDirDialog, wxDialog)
2bda0e17 164
3f2711d5
VZ
165// ----------------------------------------------------------------------------
166// private functions prototypes
167// ----------------------------------------------------------------------------
168
3f2711d5
VZ
169// the callback proc for the dir dlg
170static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
171 LPARAM pData);
bfa2e032 172
54744d3a 173
3f2711d5
VZ
174// ============================================================================
175// implementation
176// ============================================================================
bfa2e032 177
3f2711d5
VZ
178// ----------------------------------------------------------------------------
179// wxDirDialog
180// ----------------------------------------------------------------------------
181
182wxDirDialog::wxDirDialog(wxWindow *parent,
183 const wxString& message,
184 const wxString& defaultPath,
90a6f1cc 185 long style,
e78d4a23
VZ
186 const wxPoint& WXUNUSED(pos),
187 const wxSize& WXUNUSED(size),
188 const wxString& WXUNUSED(name))
2bda0e17
KB
189{
190 m_message = message;
2bda0e17 191 m_parent = parent;
90a6f1cc 192
141d782d 193 SetWindowStyle(style);
f3558acc
VZ
194 SetPath(defaultPath);
195}
196
197void wxDirDialog::SetPath(const wxString& path)
198{
199 m_path = path;
e7b0dcd9
VZ
200
201 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
9a83f860 202 m_path.Replace(wxT("/"), wxT("\\"));
359cde15
VZ
203
204 while ( !m_path.empty() && (*(m_path.end() - 1) == wxT('\\')) )
205 {
206 m_path.erase(m_path.length() - 1);
207 }
208
209 // but the root drive should have a trailing slash (again, this is just
210 // the way the native dialog works)
211 if ( !m_path.empty() && (*(m_path.end() - 1) == wxT(':')) )
e7b0dcd9 212 {
359cde15 213 m_path += wxT('\\');
e7b0dcd9 214 }
2bda0e17
KB
215}
216
3f2711d5 217int wxDirDialog::ShowModal()
2bda0e17 218{
580ffdf4
VZ
219 wxWindow* const parent = GetParent();
220 WXHWND hWndParent = parent ? GetHwndOf(parent) : NULL;
221
222 // Use IFileDialog under new enough Windows, it's more user-friendly.
223 int rc;
224#if wxUSE_IFILEDIALOG
225 if ( wxGetWinVersion() >= wxWinVersion_Vista )
226 {
227 rc = ShowIFileDialog(hWndParent);
228 }
229 else
230 {
231 rc = wxID_NONE;
232 }
233
234 if ( rc == wxID_NONE )
235#endif // wxUSE_IFILEDIALOG
236 {
237 rc = ShowSHBrowseForFolder(hWndParent);
238 }
239
240 // change current working directory if asked so
241 if ( rc == wxID_OK && HasFlag(wxDD_CHANGE_DIR) )
242 wxSetWorkingDirectory(m_path);
f3558acc 243
580ffdf4
VZ
244 return rc;
245}
246
247int wxDirDialog::ShowSHBrowseForFolder(WXHWND owner)
248{
2bda0e17 249 BROWSEINFO bi;
580ffdf4 250 bi.hwndOwner = owner;
3f2711d5
VZ
251 bi.pidlRoot = NULL;
252 bi.pszDisplayName = NULL;
58e0338a
JS
253 // Please don't change this without checking it compiles
254 // with eVC++ first.
255#if defined(__POCKETPC__) || defined(__SMARTPHONE__)
256 bi.lpszTitle = m_message.mb_str();
257#else
3f2711d5 258 bi.lpszTitle = m_message.c_str();
58e0338a 259#endif
3f2711d5
VZ
260 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
261 bi.lpfn = BrowseCallbackProc;
c9f78968 262 bi.lParam = (LPARAM)m_path.wx_str(); // param for the callback
3f2711d5 263
cf54972e
VZ
264 static const int verComCtl32 = wxApp::GetComCtl32Version();
265
266 // we always add the edit box (it doesn't hurt anybody, does it?) if it is
267 // supported by the system
268 if ( verComCtl32 >= 471 )
269 {
270 bi.ulFlags |= BIF_EDITBOX;
271 }
272
cf54972e
VZ
273 // to have the "New Folder" button we must use the "new" dialog style which
274 // is also the only way to have a resizable dialog
275 //
276 // "new" style is only available in the version 5.0+ of comctl32.dll
ff3e84ff 277 const bool needNewDir = !HasFlag(wxDD_DIR_MUST_EXIST);
cf54972e
VZ
278 if ( (needNewDir || HasFlag(wxRESIZE_BORDER)) && (verComCtl32 >= 500) )
279 {
03776753
JS
280 if (needNewDir)
281 {
282 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
283 }
284 else
285 {
286 // Versions < 600 doesn't support BIF_NONEWFOLDERBUTTON
287 // The only way to get rid of the Make New Folder button is use
288 // the old dialog style which doesn't have the button thus we
289 // simply don't set the New Dialog Style for such comctl versions.
290 if (verComCtl32 >= 600)
291 {
292 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
293 bi.ulFlags |= BIF_NONEWFOLDERBUTTON;
294 }
295 }
90a6f1cc
RD
296 }
297
cf54972e 298 // do show the dialog
6bb09706 299 wxItemIdList pidl(SHBrowseForFolder(&bi));
3f2711d5 300
6bb09706 301 wxItemIdList::Free((LPITEMIDLIST)bi.pidlRoot);
3f2711d5
VZ
302
303 if ( !pidl )
2bda0e17 304 {
3f2711d5 305 // Cancel button pressed
2bda0e17
KB
306 return wxID_CANCEL;
307 }
308
6bb09706 309 m_path = pidl.GetPath();
3f2711d5 310
6bb09706 311 return m_path.empty() ? wxID_CANCEL : wxID_OK;
3f2711d5
VZ
312}
313
580ffdf4
VZ
314// Function for obtaining folder name on Vista and newer.
315//
316// Returns wxID_OK on success, wxID_CANCEL if cancelled by user or wxID_NONE if
317// an error occurred and we should fall back onto the old dialog.
318#if wxUSE_IFILEDIALOG
319
320int wxDirDialog::ShowIFileDialog(WXHWND owner)
321{
322 HRESULT hr;
323 wxCOMPtr<IFileDialog> fileDialog;
324
325 hr = ::CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER,
326 wxIID_PPV_ARGS(IFileDialog, &fileDialog));
327 if ( FAILED(hr) )
328 {
329 wxLogApiError(wxS("CoCreateInstance(CLSID_FileOpenDialog)"), hr);
330 return wxID_NONE;
331 }
332
333 // allow user to select only a file system folder
334 hr = fileDialog->SetOptions(FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM);
335 if ( FAILED(hr) )
336 {
337 wxLogApiError(wxS("IFileDialog::SetOptions"), hr);
338 return wxID_NONE;
339 }
340
341 hr = fileDialog->SetTitle(m_message.wc_str());
342 if ( FAILED(hr) )
343 {
344 // This error is not serious, let's just log it and continue even
345 // without the title set.
346 wxLogApiError(wxS("IFileDialog::SetTitle"), hr);
347 }
348
349 // set the initial path
350 if ( !m_path.empty() )
351 {
352 // We need to link SHCreateItemFromParsingName() dynamically as it's
353 // not available on pre-Vista systems.
354 typedef HRESULT
355 (WINAPI *SHCreateItemFromParsingName_t)(PCWSTR,
356 IBindCtx*,
357 REFIID,
358 void**);
359
6f3320bf 360 SHCreateItemFromParsingName_t pfnSHCreateItemFromParsingName = NULL;
580ffdf4
VZ
361 wxDynamicLibrary dllShell32;
362 if ( dllShell32.Load(wxS("shell32.dll"), wxDL_VERBATIM | wxDL_QUIET) )
363 {
6f3320bf 364 wxDL_INIT_FUNC(pfn, SHCreateItemFromParsingName, dllShell32);
580ffdf4
VZ
365 }
366
6f3320bf 367 if ( !pfnSHCreateItemFromParsingName )
580ffdf4
VZ
368 {
369 wxLogLastError(wxS("SHCreateItemFromParsingName() not found"));
370 return wxID_NONE;
371 }
372
373 wxCOMPtr<IShellItem> folder;
6f3320bf 374 hr = pfnSHCreateItemFromParsingName(m_path.wc_str(),
580ffdf4
VZ
375 NULL,
376 wxIID_PPV_ARGS(IShellItem,
377 &folder));
378 if ( FAILED(hr) )
379 {
380 wxLogApiError(wxS("SHCreateItemFromParsingName"), hr);
381 return wxID_NONE;
382 }
383
384 hr = fileDialog->SetFolder(folder);
385 if ( FAILED(hr) )
386 {
387 wxLogApiError(wxS("IFileDialog::SetFolder"), hr);
388 return wxID_NONE;
389 }
390 }
391
392
393 wxString path;
394
395 hr = fileDialog->Show(owner);
396 if ( SUCCEEDED(hr) )
397 {
398 wxCOMPtr<IShellItem> folder;
399
400 hr = fileDialog->GetResult(&folder);
401 if ( SUCCEEDED(hr) )
402 {
403 LPOLESTR pathOLE = NULL;
404
405 hr = folder->GetDisplayName(SIGDN_FILESYSPATH, &pathOLE);
406 if ( SUCCEEDED(hr) )
407 {
408 path = pathOLE;
409 CoTaskMemFree(pathOLE);
410 }
411 else
412 {
413 wxLogApiError(wxS("IShellItem::GetDisplayName"), hr);
414 }
415 }
416 else
417 {
418 wxLogApiError(wxS("IFileDialog::GetResult"), hr);
419 }
420 }
421 else if ( hr == HRESULT_FROM_WIN32(ERROR_CANCELLED) )
422 {
423 return wxID_CANCEL; // the user cancelled the dialog
424 }
425 else
426 {
427 wxLogApiError(wxS("IFileDialog::Show"), hr);
428 }
429
430 if ( path.empty() )
431 {
432 // the user didn't cancel the dialog and yet the path is empty
433 // it means there was an error, already logged by wxLogApiError()
434 // now report the error to the user and return
435 wxLogSysError(_("Couldn't obtain folder name"), hr);
436 return wxID_CANCEL;
437 }
438
439 m_path = path;
440 return wxID_OK;
441}
442
443#endif // wxUSE_IFILEDIALOG
444
3f2711d5
VZ
445// ----------------------------------------------------------------------------
446// private functions
447// ----------------------------------------------------------------------------
448
449static int CALLBACK
450BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
451{
452 switch(uMsg)
453 {
6bb09706 454#ifdef BFFM_SETSELECTION
3f2711d5
VZ
455 case BFFM_INITIALIZED:
456 // sent immediately after initialisation and so we may set the
457 // initial selection here
458 //
459 // wParam = TRUE => lParam is a string and not a PIDL
d71cc120 460 ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
3f2711d5 461 break;
6bb09706
VZ
462#endif // BFFM_SETSELECTION
463
3f2711d5
VZ
464
465 case BFFM_SELCHANGED:
cf54972e
VZ
466 // note that this doesn't work with the new style UI (MSDN doesn't
467 // say anything about it, but the comments in shlobj.h do!) but we
468 // still execute this code in case it starts working again with the
469 // "new new UI" (or would it be "NewUIEx" according to tradition?)
3f2711d5
VZ
470 {
471 // Set the status window to the currently selected path.
cf54972e
VZ
472 wxString strDir;
473 if ( SHGetPathFromIDList((LPITEMIDLIST)lp,
474 wxStringBuffer(strDir, MAX_PATH)) )
3f2711d5 475 {
cf54972e
VZ
476 // NB: this shouldn't be necessary with the new style box
477 // (which is resizable), but as for now it doesn't work
478 // anyhow (see the comment above) no harm in doing it
479
480 // need to truncate or it displays incorrectly
481 static const size_t maxChars = 37;
482 if ( strDir.length() > maxChars )
6a097cd6 483 {
cf54972e 484 strDir = strDir.Right(maxChars);
6a097cd6
JS
485 strDir = wxString(wxT("...")) + strDir;
486 }
cf54972e
VZ
487
488 SendMessage(hwnd, BFFM_SETSTATUSTEXT,
c9f78968 489 0, (LPARAM)strDir.wx_str());
3f2711d5
VZ
490 }
491 }
492 break;
493
494 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
495 // if the user types in invalid dir name
496 }
497
498 return 0;
499}
500
54744d3a 501#endif // compiler/platform on which the code here compiles
1e6feb95 502
08bce501 503#endif // wxUSE_DIRDLG