]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dirdlg.cpp
new wxASSERT implementation using wxAssert() helper function
[wxWidgets.git] / src / msw / dirdlg.cpp
CommitLineData
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"
15ad98b7 40 #include "wx/app.h" // for GetComCtl32Version()
2bda0e17
KB
41#endif
42
3f2711d5
VZ
43#include "wx/msw/private.h"
44
e7b0dcd9 45#include <shlobj.h> // Win95 shell
2bda0e17 46
3f2711d5
VZ
47// ----------------------------------------------------------------------------
48// constants
49// ----------------------------------------------------------------------------
2bda0e17 50
3f2711d5 51#ifndef MAX_PATH
54744d3a 52 #define MAX_PATH 4096 // be generous
3f2711d5
VZ
53#endif
54
55// ----------------------------------------------------------------------------
56// wxWindows macros
57// ----------------------------------------------------------------------------
2bda0e17 58
54744d3a 59IMPLEMENT_CLASS(wxDirDialog, wxDialog)
2bda0e17 60
3f2711d5
VZ
61// ----------------------------------------------------------------------------
62// private functions prototypes
63// ----------------------------------------------------------------------------
64
65// free the parameter
66static void ItemListFree(LPITEMIDLIST pidl);
bfa2e032 67
3f2711d5
VZ
68// the callback proc for the dir dlg
69static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
70 LPARAM pData);
bfa2e032 71
54744d3a 72
3f2711d5
VZ
73// ============================================================================
74// implementation
75// ============================================================================
bfa2e032 76
3f2711d5
VZ
77// ----------------------------------------------------------------------------
78// wxDirDialog
79// ----------------------------------------------------------------------------
80
81wxDirDialog::wxDirDialog(wxWindow *parent,
82 const wxString& message,
83 const wxString& defaultPath,
90a6f1cc 84 long style,
e78d4a23
VZ
85 const wxPoint& WXUNUSED(pos),
86 const wxSize& WXUNUSED(size),
87 const wxString& WXUNUSED(name))
2bda0e17
KB
88{
89 m_message = message;
2bda0e17 90 m_parent = parent;
90a6f1cc
RD
91
92 SetStyle(style);
f3558acc
VZ
93 SetPath(defaultPath);
94}
95
96void wxDirDialog::SetPath(const wxString& path)
97{
98 m_path = path;
e7b0dcd9
VZ
99
100 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
101 m_path.Replace(_T("/"), _T("\\"));
f3558acc 102 if ( !m_path.empty() )
e7b0dcd9 103 {
f3558acc
VZ
104 while ( *(m_path.end() - 1) == _T('\\') )
105 {
da03e330
VZ
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('\\');
f3558acc 114 }
e7b0dcd9 115 }
2bda0e17
KB
116}
117
90a6f1cc
RD
118#ifndef BIF_NEWDIALOGSTYLE
119#define BIF_NEWDIALOGSTYLE 0x0040
120#endif
121
3f2711d5 122int wxDirDialog::ShowModal()
2bda0e17 123{
f3558acc
VZ
124 wxWindow *parent = GetParent();
125
2bda0e17 126 BROWSEINFO bi;
f3558acc 127 bi.hwndOwner = parent ? GetHwndOf(parent) : NULL;
3f2711d5
VZ
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
90a6f1cc
RD
135 if ((GetStyle() & wxDD_NEW_DIR_BUTTON) &&
136 (wxApp::GetComCtl32Version() >= 500))
137 {
138 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
139 }
140
3f2711d5
VZ
141 LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
142
143 if ( bi.pidlRoot )
144 {
145 ItemListFree((LPITEMIDLIST)bi.pidlRoot);
146 }
147
148 if ( !pidl )
2bda0e17 149 {
3f2711d5 150 // Cancel button pressed
2bda0e17
KB
151 return wxID_CANCEL;
152 }
153
3f2711d5
VZ
154 BOOL ok = SHGetPathFromIDList(pidl, m_path.GetWriteBuf(MAX_PATH));
155 m_path.UngetWriteBuf();
156
157 ItemListFree(pidl);
158
159 if ( !ok )
160 {
f6bcfd97 161 wxLogLastError(wxT("SHGetPathFromIDList"));
3f2711d5 162
2bda0e17 163 return wxID_CANCEL;
2bda0e17 164 }
3f2711d5
VZ
165
166 return wxID_OK;
3f2711d5
VZ
167}
168
169// ----------------------------------------------------------------------------
170// private functions
171// ----------------------------------------------------------------------------
172
173static int CALLBACK
174BrowseCallbackProc(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 {
6a097cd6
JS
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);
3f2711d5
VZ
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
212static 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 {
f6bcfd97 225 wxLogLastError(wxT("SHGetMalloc"));
3f2711d5
VZ
226 }
227 }
2bda0e17
KB
228}
229
54744d3a
VZ
230#else
231 #include "../generic/dirdlgg.cpp"
232#endif // compiler/platform on which the code here compiles
1e6feb95
VZ
233
234#endif // wxUSE_DIRDLG