]> git.saurik.com Git - wxWidgets.git/blame - src/msw/dirdlg.cpp
Fix for wxMemoryDC::GetAsBitmap() not working on Windows.
[wxWidgets.git] / src / msw / dirdlg.cpp
CommitLineData
2bda0e17 1/////////////////////////////////////////////////////////////////////////////
a71d815b 2// Name: src/msw/dirdlg.cpp
2bda0e17
KB
3// Purpose: wxDirDialog
4// Author: Julian Smart
5// Modified by:
6// Created: 01/02/97
7// RCS-ID: $Id$
6c9a19aa 8// Copyright: (c) Julian Smart
65571936 9// Licence: wxWindows licence
2bda0e17
KB
10/////////////////////////////////////////////////////////////////////////////
11
3f2711d5
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
1e6feb95 19
2bda0e17
KB
20// For compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
3f2711d5 24 #pragma hdrstop
2bda0e17
KB
25#endif
26
08bce501 27#if wxUSE_DIRDLG
1e6feb95 28
141d782d
VZ
29#if wxUSE_OLE && !defined(__GNUWIN32_OLD__) && (!defined(__WXWINCE__) || \
30 (defined(__HANDHELDPC__) && (_WIN32_WCE >= 500)))
54744d3a 31
1ab440bc
WS
32#include "wx/dirdlg.h"
33
2bda0e17 34#ifndef WX_PRECOMP
3f2711d5
VZ
35 #include "wx/utils.h"
36 #include "wx/dialog.h"
77902671 37 #include "wx/log.h"
15ad98b7 38 #include "wx/app.h" // for GetComCtl32Version()
2bda0e17
KB
39#endif
40
3f2711d5 41#include "wx/msw/private.h"
6bb09706 42#include "wx/msw/wrapshl.h"
2bda0e17 43
3f2711d5
VZ
44// ----------------------------------------------------------------------------
45// constants
46// ----------------------------------------------------------------------------
2bda0e17 47
cf54972e
VZ
48#ifndef BIF_NEWDIALOGSTYLE
49 #define BIF_NEWDIALOGSTYLE 0x0040
50#endif
51
52#ifndef BIF_NONEWFOLDERBUTTON
53 #define BIF_NONEWFOLDERBUTTON 0x0200
54#endif
55
94311eef
MB
56#ifndef BIF_EDITBOX
57 #define BIF_EDITBOX 16
58#endif
59
3f2711d5 60// ----------------------------------------------------------------------------
77ffb593 61// wxWidgets macros
3f2711d5 62// ----------------------------------------------------------------------------
2bda0e17 63
54744d3a 64IMPLEMENT_CLASS(wxDirDialog, wxDialog)
2bda0e17 65
3f2711d5
VZ
66// ----------------------------------------------------------------------------
67// private functions prototypes
68// ----------------------------------------------------------------------------
69
3f2711d5
VZ
70// the callback proc for the dir dlg
71static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp,
72 LPARAM pData);
bfa2e032 73
54744d3a 74
3f2711d5
VZ
75// ============================================================================
76// implementation
77// ============================================================================
bfa2e032 78
3f2711d5
VZ
79// ----------------------------------------------------------------------------
80// wxDirDialog
81// ----------------------------------------------------------------------------
82
83wxDirDialog::wxDirDialog(wxWindow *parent,
84 const wxString& message,
85 const wxString& defaultPath,
90a6f1cc 86 long style,
e78d4a23
VZ
87 const wxPoint& WXUNUSED(pos),
88 const wxSize& WXUNUSED(size),
89 const wxString& WXUNUSED(name))
2bda0e17
KB
90{
91 m_message = message;
2bda0e17 92 m_parent = parent;
90a6f1cc 93
141d782d 94 SetWindowStyle(style);
f3558acc
VZ
95 SetPath(defaultPath);
96}
97
98void wxDirDialog::SetPath(const wxString& path)
99{
100 m_path = path;
e7b0dcd9
VZ
101
102 // SHBrowseForFolder doesn't like '/'s nor the trailing backslashes
103 m_path.Replace(_T("/"), _T("\\"));
f3558acc 104 if ( !m_path.empty() )
e7b0dcd9 105 {
f3558acc
VZ
106 while ( *(m_path.end() - 1) == _T('\\') )
107 {
da03e330
VZ
108 m_path.erase(m_path.length() - 1);
109 }
110
111 // but the root drive should have a trailing slash (again, this is just
112 // the way the native dialog works)
113 if ( *(m_path.end() - 1) == _T(':') )
114 {
115 m_path += _T('\\');
f3558acc 116 }
e7b0dcd9 117 }
2bda0e17
KB
118}
119
3f2711d5 120int wxDirDialog::ShowModal()
2bda0e17 121{
f3558acc
VZ
122 wxWindow *parent = GetParent();
123
2bda0e17 124 BROWSEINFO bi;
f3558acc 125 bi.hwndOwner = parent ? GetHwndOf(parent) : NULL;
3f2711d5
VZ
126 bi.pidlRoot = NULL;
127 bi.pszDisplayName = NULL;
58e0338a
JS
128 // Please don't change this without checking it compiles
129 // with eVC++ first.
130#if defined(__POCKETPC__) || defined(__SMARTPHONE__)
131 bi.lpszTitle = m_message.mb_str();
132#else
3f2711d5 133 bi.lpszTitle = m_message.c_str();
58e0338a 134#endif
3f2711d5
VZ
135 bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
136 bi.lpfn = BrowseCallbackProc;
c9f78968 137 bi.lParam = (LPARAM)m_path.wx_str(); // param for the callback
3f2711d5 138
cf54972e
VZ
139 static const int verComCtl32 = wxApp::GetComCtl32Version();
140
141 // we always add the edit box (it doesn't hurt anybody, does it?) if it is
142 // supported by the system
143 if ( verComCtl32 >= 471 )
144 {
145 bi.ulFlags |= BIF_EDITBOX;
146 }
147
cf54972e
VZ
148 // to have the "New Folder" button we must use the "new" dialog style which
149 // is also the only way to have a resizable dialog
150 //
151 // "new" style is only available in the version 5.0+ of comctl32.dll
ff3e84ff 152 const bool needNewDir = !HasFlag(wxDD_DIR_MUST_EXIST);
cf54972e
VZ
153 if ( (needNewDir || HasFlag(wxRESIZE_BORDER)) && (verComCtl32 >= 500) )
154 {
03776753
JS
155 if (needNewDir)
156 {
157 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
158 }
159 else
160 {
161 // Versions < 600 doesn't support BIF_NONEWFOLDERBUTTON
162 // The only way to get rid of the Make New Folder button is use
163 // the old dialog style which doesn't have the button thus we
164 // simply don't set the New Dialog Style for such comctl versions.
165 if (verComCtl32 >= 600)
166 {
167 bi.ulFlags |= BIF_NEWDIALOGSTYLE;
168 bi.ulFlags |= BIF_NONEWFOLDERBUTTON;
169 }
170 }
90a6f1cc
RD
171 }
172
cf54972e 173 // do show the dialog
6bb09706 174 wxItemIdList pidl(SHBrowseForFolder(&bi));
3f2711d5 175
6bb09706 176 wxItemIdList::Free((LPITEMIDLIST)bi.pidlRoot);
3f2711d5
VZ
177
178 if ( !pidl )
2bda0e17 179 {
3f2711d5 180 // Cancel button pressed
2bda0e17
KB
181 return wxID_CANCEL;
182 }
183
6bb09706 184 m_path = pidl.GetPath();
3f2711d5 185
141d782d
VZ
186 // change current working directory if asked so
187 if (HasFlag(wxDD_CHANGE_DIR))
188 wxSetWorkingDirectory(m_path);
189
6bb09706 190 return m_path.empty() ? wxID_CANCEL : wxID_OK;
3f2711d5
VZ
191}
192
193// ----------------------------------------------------------------------------
194// private functions
195// ----------------------------------------------------------------------------
196
197static int CALLBACK
198BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lp, LPARAM pData)
199{
200 switch(uMsg)
201 {
6bb09706 202#ifdef BFFM_SETSELECTION
3f2711d5
VZ
203 case BFFM_INITIALIZED:
204 // sent immediately after initialisation and so we may set the
205 // initial selection here
206 //
207 // wParam = TRUE => lParam is a string and not a PIDL
d71cc120 208 ::SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
3f2711d5 209 break;
6bb09706
VZ
210#endif // BFFM_SETSELECTION
211
3f2711d5
VZ
212
213 case BFFM_SELCHANGED:
cf54972e
VZ
214 // note that this doesn't work with the new style UI (MSDN doesn't
215 // say anything about it, but the comments in shlobj.h do!) but we
216 // still execute this code in case it starts working again with the
217 // "new new UI" (or would it be "NewUIEx" according to tradition?)
3f2711d5
VZ
218 {
219 // Set the status window to the currently selected path.
cf54972e
VZ
220 wxString strDir;
221 if ( SHGetPathFromIDList((LPITEMIDLIST)lp,
222 wxStringBuffer(strDir, MAX_PATH)) )
3f2711d5 223 {
cf54972e
VZ
224 // NB: this shouldn't be necessary with the new style box
225 // (which is resizable), but as for now it doesn't work
226 // anyhow (see the comment above) no harm in doing it
227
228 // need to truncate or it displays incorrectly
229 static const size_t maxChars = 37;
230 if ( strDir.length() > maxChars )
6a097cd6 231 {
cf54972e 232 strDir = strDir.Right(maxChars);
6a097cd6
JS
233 strDir = wxString(wxT("...")) + strDir;
234 }
cf54972e
VZ
235
236 SendMessage(hwnd, BFFM_SETSTATUSTEXT,
c9f78968 237 0, (LPARAM)strDir.wx_str());
3f2711d5
VZ
238 }
239 }
240 break;
241
242 //case BFFM_VALIDATEFAILED: -- might be used to provide custom message
243 // if the user types in invalid dir name
244 }
245
246 return 0;
247}
248
54744d3a 249#endif // compiler/platform on which the code here compiles
1e6feb95 250
08bce501 251#endif // wxUSE_DIRDLG