]>
Commit | Line | Data |
---|---|---|
2bda0e17 KB |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: 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 and Markus Holzem | |
3f2711d5 | 9 | // Licence: wxWindows licence |
2bda0e17 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
3f2711d5 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
1e6feb95 | 19 | |
2bda0e17 | 20 | #ifdef __GNUG__ |
3f2711d5 | 21 | #pragma implementation "dirdlg.h" |
2bda0e17 KB |
22 | #endif |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
3f2711d5 | 28 | #pragma hdrstop |
2bda0e17 KB |
29 | #endif |
30 | ||
1e6feb95 VZ |
31 | #if wxUSE_DIRDLG |
32 | ||
7fc0bd1c | 33 | #if defined(__WIN95__) && !defined(__GNUWIN32_OLD__) && wxUSE_OLE |
54744d3a | 34 | |
2bda0e17 | 35 | #ifndef WX_PRECOMP |
3f2711d5 VZ |
36 | #include "wx/utils.h" |
37 | #include "wx/dialog.h" | |
38 | #include "wx/dirdlg.h" | |
77902671 | 39 | #include "wx/log.h" |
2bda0e17 KB |
40 | #endif |
41 | ||
3f2711d5 VZ |
42 | #include "wx/msw/private.h" |
43 | ||
e7b0dcd9 | 44 | #include <shlobj.h> // Win95 shell |
2bda0e17 | 45 | |
3f2711d5 VZ |
46 | // ---------------------------------------------------------------------------- |
47 | // constants | |
48 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 49 | |
3f2711d5 | 50 | #ifndef MAX_PATH |
54744d3a | 51 | #define MAX_PATH 4096 // be generous |
3f2711d5 VZ |
52 | #endif |
53 | ||
54 | // ---------------------------------------------------------------------------- | |
55 | // wxWindows macros | |
56 | // ---------------------------------------------------------------------------- | |
2bda0e17 | 57 | |
54744d3a | 58 | IMPLEMENT_CLASS(wxDirDialog, wxDialog) |
2bda0e17 | 59 | |
3f2711d5 VZ |
60 | // ---------------------------------------------------------------------------- |
61 | // private functions prototypes | |
62 | // ---------------------------------------------------------------------------- | |
63 | ||
64 | // free the parameter | |
65 | static void ItemListFree(LPITEMIDLIST pidl); | |
bfa2e032 | 66 | |
3f2711d5 VZ |
67 | // the callback proc for the dir dlg |
68 | static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, | |
69 | LPARAM pData); | |
bfa2e032 | 70 | |
54744d3a | 71 | |
3f2711d5 VZ |
72 | // ============================================================================ |
73 | // implementation | |
74 | // ============================================================================ | |
bfa2e032 | 75 | |
3f2711d5 VZ |
76 | // ---------------------------------------------------------------------------- |
77 | // wxDirDialog | |
78 | // ---------------------------------------------------------------------------- | |
79 | ||
80 | wxDirDialog::wxDirDialog(wxWindow *parent, | |
81 | const wxString& message, | |
82 | const wxString& defaultPath, | |
90a6f1cc | 83 | long style, |
e78d4a23 VZ |
84 | const wxPoint& WXUNUSED(pos), |
85 | const wxSize& WXUNUSED(size), | |
86 | const wxString& WXUNUSED(name)) | |
2bda0e17 KB |
87 | { |
88 | m_message = message; | |
2bda0e17 | 89 | m_parent = parent; |
90a6f1cc RD |
90 | |
91 | SetStyle(style); | |
f3558acc VZ |
92 | SetPath(defaultPath); |
93 | } | |
94 | ||
95 | void wxDirDialog::SetPath(const wxString& path) | |
96 | { | |
97 | m_path = path; | |
e7b0dcd9 VZ |
98 | |
99 | // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes | |
100 | m_path.Replace(_T("/"), _T("\\")); | |
f3558acc | 101 | if ( !m_path.empty() ) |
e7b0dcd9 | 102 | { |
f3558acc VZ |
103 | while ( *(m_path.end() - 1) == _T('\\') ) |
104 | { | |
da03e330 VZ |
105 | m_path.erase(m_path.length() - 1); |
106 | } | |
107 | ||
108 | // but the root drive should have a trailing slash (again, this is just | |
109 | // the way the native dialog works) | |
110 | if ( *(m_path.end() - 1) == _T(':') ) | |
111 | { | |
112 | m_path += _T('\\'); | |
f3558acc | 113 | } |
e7b0dcd9 | 114 | } |
2bda0e17 KB |
115 | } |
116 | ||
90a6f1cc RD |
117 | #ifndef BIF_NEWDIALOGSTYLE |
118 | #define BIF_NEWDIALOGSTYLE 0x0040 | |
119 | #endif | |
120 | ||
3f2711d5 | 121 | int wxDirDialog::ShowModal() |
2bda0e17 | 122 | { |
f3558acc VZ |
123 | wxWindow *parent = GetParent(); |
124 | ||
2bda0e17 | 125 | BROWSEINFO bi; |
f3558acc | 126 | bi.hwndOwner = parent ? GetHwndOf(parent) : NULL; |
3f2711d5 VZ |
127 | bi.pidlRoot = NULL; |
128 | bi.pszDisplayName = NULL; | |
129 | bi.lpszTitle = m_message.c_str(); | |
130 | bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT; | |
131 | bi.lpfn = BrowseCallbackProc; | |
132 | bi.lParam = (LPARAM)m_path.c_str(); // param for the callback | |
133 | ||
90a6f1cc RD |
134 | if ((GetStyle() & wxDD_NEW_DIR_BUTTON) && |
135 | (wxApp::GetComCtl32Version() >= 500)) | |
136 | { | |
137 | bi.ulFlags |= BIF_NEWDIALOGSTYLE; | |
138 | } | |
139 | ||
3f2711d5 VZ |
140 | LPITEMIDLIST pidl = SHBrowseForFolder(&bi); |
141 | ||
142 | if ( bi.pidlRoot ) | |
143 | { | |
144 | ItemListFree((LPITEMIDLIST)bi.pidlRoot); | |
145 | } | |
146 | ||
147 | if ( !pidl ) | |
2bda0e17 | 148 | { |
3f2711d5 | 149 | // Cancel button pressed |
2bda0e17 KB |
150 | return wxID_CANCEL; |
151 | } | |
152 | ||
3f2711d5 VZ |
153 | BOOL ok = SHGetPathFromIDList(pidl, m_path.GetWriteBuf(MAX_PATH)); |
154 | m_path.UngetWriteBuf(); | |
155 | ||
156 | ItemListFree(pidl); | |
157 | ||
158 | if ( !ok ) | |
159 | { | |
f6bcfd97 | 160 | wxLogLastError(wxT("SHGetPathFromIDList")); |
3f2711d5 | 161 | |
2bda0e17 | 162 | return wxID_CANCEL; |
2bda0e17 | 163 | } |
3f2711d5 VZ |
164 | |
165 | return wxID_OK; | |
3f2711d5 VZ |
166 | } |
167 | ||
168 | // ---------------------------------------------------------------------------- | |
169 | // private functions | |
170 | // ---------------------------------------------------------------------------- | |
171 | ||
172 | static int CALLBACK | |
173 | BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData) | |
174 | { | |
175 | switch(uMsg) | |
176 | { | |
177 | case BFFM_INITIALIZED: | |
178 | // sent immediately after initialisation and so we may set the | |
179 | // initial selection here | |
180 | // | |
181 | // wParam = TRUE => lParam is a string and not a PIDL | |
182 | SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData); | |
183 | break; | |
184 | ||
185 | case BFFM_SELCHANGED: | |
186 | { | |
187 | // Set the status window to the currently selected path. | |
188 | TCHAR szDir[MAX_PATH]; | |
189 | if ( SHGetPathFromIDList((LPITEMIDLIST)lp, szDir) ) | |
190 | { | |
6a097cd6 JS |
191 | wxString strDir(szDir); |
192 | int maxChars = 40; // Have to truncate string else it displays incorrectly | |
193 | if (strDir.Len() > (size_t) (maxChars - 3)) | |
194 | { | |
195 | strDir = strDir.Right(maxChars - 3); | |
196 | strDir = wxString(wxT("...")) + strDir; | |
197 | } | |
198 | SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0, (LPARAM) (const wxChar*) strDir); | |
3f2711d5 VZ |
199 | } |
200 | } | |
201 | break; | |
202 | ||
203 | //case BFFM_VALIDATEFAILED: -- might be used to provide custom message | |
204 | // if the user types in invalid dir name | |
205 | } | |
206 | ||
207 | return 0; | |
208 | } | |
209 | ||
210 | ||
211 | static void ItemListFree(LPITEMIDLIST pidl) | |
212 | { | |
213 | if ( pidl ) | |
214 | { | |
215 | LPMALLOC pMalloc; | |
216 | SHGetMalloc(&pMalloc); | |
217 | if ( pMalloc ) | |
218 | { | |
219 | pMalloc->Free(pidl); | |
220 | pMalloc->Release(); | |
221 | } | |
222 | else | |
223 | { | |
f6bcfd97 | 224 | wxLogLastError(wxT("SHGetMalloc")); |
3f2711d5 VZ |
225 | } |
226 | } | |
2bda0e17 KB |
227 | } |
228 | ||
54744d3a VZ |
229 | #else |
230 | #include "../generic/dirdlgg.cpp" | |
231 | #endif // compiler/platform on which the code here compiles | |
1e6feb95 VZ |
232 | |
233 | #endif // wxUSE_DIRDLG |