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