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