File/dir dialog styles and other changes (patch 1488371):
[wxWidgets.git] / src / msw / dirdlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/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 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DIRDLG
28
29 #if wxUSE_OLE && !defined(__GNUWIN32_OLD__) && (!defined(__WXWINCE__) || \
30 (defined(__HANDHELDPC__) && (_WIN32_WCE >= 500)))
31
32 #ifndef WX_PRECOMP
33 #include "wx/utils.h"
34 #include "wx/dialog.h"
35 #include "wx/dirdlg.h"
36 #include "wx/log.h"
37 #include "wx/app.h" // for GetComCtl32Version()
38 #endif
39
40 #include "wx/msw/private.h"
41 #include "wx/msw/wrapshl.h"
42
43 // ----------------------------------------------------------------------------
44 // constants
45 // ----------------------------------------------------------------------------
46
47 #ifndef BIF_NEWDIALOGSTYLE
48 #define BIF_NEWDIALOGSTYLE 0x0040
49 #endif
50
51 #ifndef BIF_NONEWFOLDERBUTTON
52 #define BIF_NONEWFOLDERBUTTON 0x0200
53 #endif
54
55 #ifndef BIF_EDITBOX
56 #define BIF_EDITBOX 16
57 #endif
58
59 // ----------------------------------------------------------------------------
60 // wxWidgets macros
61 // ----------------------------------------------------------------------------
62
63 IMPLEMENT_CLASS(wxDirDialog, wxDialog)
64
65 // ----------------------------------------------------------------------------
66 // private functions prototypes
67 // ----------------------------------------------------------------------------
68
69 // the callback proc for the dir dlg
70 static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
71 LPARAM pData);
72
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // wxDirDialog
80 // ----------------------------------------------------------------------------
81
82 wxDirDialog::wxDirDialog(wxWindow *parent,
83 const wxString& message,
84 const wxString& defaultPath,
85 long style,
86 const wxPoint& WXUNUSED(pos),
87 const wxSize& WXUNUSED(size),
88 const wxString& WXUNUSED(name))
89 {
90 m_message = message;
91 m_parent = parent;
92
93 SetWindowStyle(style);
94 SetPath(defaultPath);
95 }
96
97 void wxDirDialog::SetPath(const wxString& path)
98 {
99 m_path = path;
100
101 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
102 m_path.Replace(_T("/"), _T("\\"));
103 if ( !m_path.empty() )
104 {
105 while ( *(m_path.end() - 1) == _T('\\') )
106 {
107 m_path.erase(m_path.length() - 1);
108 }
109
110 // but the root drive should have a trailing slash (again, this is just
111 // the way the native dialog works)
112 if ( *(m_path.end() - 1) == _T(':') )
113 {
114 m_path += _T('\\');
115 }
116 }
117 }
118
119 int wxDirDialog::ShowModal()
120 {
121 wxWindow *parent = GetParent();
122
123 BROWSEINFO bi;
124 bi.hwndOwner = parent ? GetHwndOf(parent) : NULL;
125 bi.pidlRoot = NULL;
126 bi.pszDisplayName = NULL;
127 // Please don't change this without checking it compiles
128 // with eVC++ first.
129 #if defined(__POCKETPC__) || defined(__SMARTPHONE__)
130 bi.lpszTitle = m_message.mb_str();
131 #else
132 bi.lpszTitle = m_message.c_str();
133 #endif
134 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
135 bi.lpfn = BrowseCallbackProc;
136 bi.lParam = (LPARAM)m_path.c_str(); // param for the callback
137
138 static const int verComCtl32 = wxApp::GetComCtl32Version();
139
140 // we always add the edit box (it doesn't hurt anybody, does it?) if it is
141 // supported by the system
142 if ( verComCtl32 >= 471 )
143 {
144 bi.ulFlags |= BIF_EDITBOX;
145 }
146
147 // to have the "New Folder" button we must use the "new" dialog style which
148 // is also the only way to have a resizable dialog
149 //
150 // "new" style is only available in the version 5.0+ of comctl32.dll
151 const bool needNewDir = !HasFlag(wxDD_DIR_MUST_EXIST);
152 if ( (needNewDir || HasFlag(wxRESIZE_BORDER)) && (verComCtl32 >= 500) )
153 {
154 if (needNewDir)
155 {
156 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
157 }
158 else
159 {
160 // Versions < 600 doesn't support BIF_NONEWFOLDERBUTTON
161 // The only way to get rid of the Make New Folder button is use
162 // the old dialog style which doesn't have the button thus we
163 // simply don't set the New Dialog Style for such comctl versions.
164 if (verComCtl32 >= 600)
165 {
166 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
167 bi.ulFlags |= BIF_NONEWFOLDERBUTTON;
168 }
169 }
170 }
171
172 // do show the dialog
173 wxItemIdList pidl(SHBrowseForFolder(&bi));
174
175 wxItemIdList::Free((LPITEMIDLIST)bi.pidlRoot);
176
177 if ( !pidl )
178 {
179 // Cancel button pressed
180 return wxID_CANCEL;
181 }
182
183 m_path = pidl.GetPath();
184
185 // change current working directory if asked so
186 if (HasFlag(wxDD_CHANGE_DIR))
187 wxSetWorkingDirectory(m_path);
188
189 return m_path.empty() ? wxID_CANCEL : wxID_OK;
190 }
191
192 // ----------------------------------------------------------------------------
193 // private functions
194 // ----------------------------------------------------------------------------
195
196 static int CALLBACK
197 BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
198 {
199 switch(uMsg)
200 {
201 #ifdef BFFM_SETSELECTION
202 case BFFM_INITIALIZED:
203 // sent immediately after initialisation and so we may set the
204 // initial selection here
205 //
206 // wParam = TRUE => lParam is a string and not a PIDL
207 ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
208 break;
209 #endif // BFFM_SETSELECTION
210
211
212 case BFFM_SELCHANGED:
213 // note that this doesn't work with the new style UI (MSDN doesn't
214 // say anything about it, but the comments in shlobj.h do!) but we
215 // still execute this code in case it starts working again with the
216 // "new new UI" (or would it be "NewUIEx" according to tradition?)
217 {
218 // Set the status window to the currently selected path.
219 wxString strDir;
220 if ( SHGetPathFromIDList((LPITEMIDLIST)lp,
221 wxStringBuffer(strDir, MAX_PATH)) )
222 {
223 // NB: this shouldn't be necessary with the new style box
224 // (which is resizable), but as for now it doesn't work
225 // anyhow (see the comment above) no harm in doing it
226
227 // need to truncate or it displays incorrectly
228 static const size_t maxChars = 37;
229 if ( strDir.length() > maxChars )
230 {
231 strDir = strDir.Right(maxChars);
232 strDir = wxString(wxT("...")) + strDir;
233 }
234
235 SendMessage(hwnd, BFFM_SETSTATUSTEXT,
236 0, (LPARAM)strDir.c_str());
237 }
238 }
239 break;
240
241 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
242 // if the user types in invalid dir name
243 }
244
245 return 0;
246 }
247
248 #endif // compiler/platform on which the code here compiles
249
250 #endif // wxUSE_DIRDLG
251