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