ceca8d1fe91582f87214bc8d9501af5b4518d5ed
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDirDialog
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "dirdlg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #if defined(__WIN95__) && !defined(__GNUWIN32_OLD__) && wxUSE_OLE
37 #include "wx/dialog.h"
38 #include "wx/dirdlg.h"
40 #include "wx/app.h" // for GetComCtl32Version()
43 #include "wx/msw/private.h"
45 #include <shlobj.h> // Win95 shell
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
52 #define MAX_PATH 4096 // be generous
55 #ifndef BIF_NEWDIALOGSTYLE
56 #define BIF_NEWDIALOGSTYLE 0x0040
59 #ifndef BIF_NONEWFOLDERBUTTON
60 #define BIF_NONEWFOLDERBUTTON 0x0200
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 IMPLEMENT_CLASS(wxDirDialog
, wxDialog
)
69 // ----------------------------------------------------------------------------
70 // private functions prototypes
71 // ----------------------------------------------------------------------------
74 static void ItemListFree(LPITEMIDLIST pidl
);
76 // the callback proc for the dir dlg
77 static int CALLBACK
BrowseCallbackProc(HWND hwnd
, UINT uMsg
, LPARAM lp
,
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 wxDirDialog::wxDirDialog(wxWindow
*parent
,
90 const wxString
& message
,
91 const wxString
& defaultPath
,
93 const wxPoint
& WXUNUSED(pos
),
94 const wxSize
& WXUNUSED(size
),
95 const wxString
& WXUNUSED(name
))
101 SetPath(defaultPath
);
104 void wxDirDialog::SetPath(const wxString
& path
)
108 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
109 m_path
.Replace(_T("/"), _T("\\"));
110 if ( !m_path
.empty() )
112 while ( *(m_path
.end() - 1) == _T('\\') )
114 m_path
.erase(m_path
.length() - 1);
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(':') )
126 int wxDirDialog::ShowModal()
128 wxWindow
*parent
= GetParent();
131 bi
.hwndOwner
= parent
? GetHwndOf(parent
) : 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
139 static const int verComCtl32
= wxApp::GetComCtl32Version();
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 )
145 bi
.ulFlags
|= BIF_EDITBOX
;
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]
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
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) )
159 bi
.ulFlags
|= BIF_NEWDIALOGSTYLE
;
161 // we'll get the "New Folder" button by default now, don't show it if
164 bi
.ulFlags
|= BIF_NONEWFOLDERBUTTON
;
167 if ( HasFlag(wxDD_NEW_DIR_BUTTON
) && verComCtl32
>= 500 )
169 // use the new style to make the "New Folder" button appear
170 bi
.ulFlags
|= BIF_NEWDIALOGSTYLE
;
174 // do show the dialog
175 LPITEMIDLIST pidl
= SHBrowseForFolder(&bi
);
179 ItemListFree((LPITEMIDLIST
)bi
.pidlRoot
);
184 // Cancel button pressed
188 BOOL ok
= SHGetPathFromIDList(pidl
, wxStringBuffer(m_path
, MAX_PATH
));
194 wxLogLastError(wxT("SHGetPathFromIDList"));
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
207 BrowseCallbackProc(HWND hwnd
, UINT uMsg
, LPARAM lp
, LPARAM pData
)
211 case BFFM_INITIALIZED
:
212 // sent immediately after initialisation and so we may set the
213 // initial selection here
215 // wParam = TRUE => lParam is a string and not a PIDL
216 SendMessage(hwnd
, BFFM_SETSELECTION
, TRUE
, pData
);
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?)
225 // Set the status window to the currently selected path.
227 if ( SHGetPathFromIDList((LPITEMIDLIST
)lp
,
228 wxStringBuffer(strDir
, MAX_PATH
)) )
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
234 // need to truncate or it displays incorrectly
235 static const size_t maxChars
= 37;
236 if ( strDir
.length() > maxChars
)
238 strDir
= strDir
.Right(maxChars
);
239 strDir
= wxString(wxT("...")) + strDir
;
242 SendMessage(hwnd
, BFFM_SETSTATUSTEXT
,
243 0, (LPARAM
)strDir
.c_str());
248 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
249 // if the user types in invalid dir name
256 static void ItemListFree(LPITEMIDLIST pidl
)
261 SHGetMalloc(&pMalloc
);
269 wxLogLastError(wxT("SHGetMalloc"));
275 #include "../generic/dirdlgg.cpp"
276 #endif // compiler/platform on which the code here compiles
278 #endif // wxUSE_DIRDLG