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