added multiple selections support to wxDirCtrl (closes #10830)
[wxWidgets.git] / samples / widgets / dirctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: dirctrl.cpp
4 // Purpose: Part of the widgets sample showing wxGenericDirCtrl
5 // Author: Wlodzimierz 'ABX' Skiba
6 // Created: 4 Oct 2006
7 // Id: $Id$
8 // Copyright: (c) 2006 wxWindows team
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DIRDLG
28
29 // for all others, include the necessary headers
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/log.h"
33 #include "wx/sizer.h"
34 #include "wx/statbox.h"
35 #include "wx/radiobox.h"
36 #include "wx/checkbox.h"
37 #include "wx/button.h"
38 #include "wx/filedlg.h"
39 #endif
40
41 #include "wx/generic/dirctrlg.h"
42
43 #include "wx/wupdlock.h"
44 #include "wx/stdpaths.h"
45
46 #include "widgets.h"
47
48 #include "icons/dirctrl.xpm"
49
50 // ----------------------------------------------------------------------------
51 // constants
52 // ----------------------------------------------------------------------------
53
54 // control ids
55 enum
56 {
57 DirCtrlPage_Reset = wxID_HIGHEST,
58 DirCtrlPage_SetPath,
59 DirCtrlPage_Ctrl
60 };
61
62 static const wxString stdPaths[] =
63 {
64 _T("&none"),
65 _T("&config"),
66 _T("&data"),
67 _T("&documents"),
68 _T("&local data"),
69 _T("&plugins"),
70 _T("&resources"),
71 _T("&user config"),
72 _T("&user data"),
73 _T("&user local data")
74 };
75
76 enum
77 {
78 stdPathUnknown = 0,
79 stdPathConfig,
80 stdPathData,
81 stdPathDocuments,
82 stdPathLocalData,
83 stdPathPlugins,
84 stdPathResources,
85 stdPathUserConfig,
86 stdPathUserData,
87 stdPathUserLocalData,
88 stdPathMax
89 };
90
91 // ----------------------------------------------------------------------------
92 // CheckBoxWidgetsPage
93 // ----------------------------------------------------------------------------
94
95 class DirCtrlWidgetsPage : public WidgetsPage
96 {
97 public:
98 DirCtrlWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
99 virtual ~DirCtrlWidgetsPage() {}
100
101 virtual wxControl *GetWidget() const { return m_dirCtrl; }
102 virtual void RecreateWidget() { CreateDirCtrl(); }
103
104 // lazy creation of the content
105 virtual void CreateContent();
106
107 protected:
108 // event handlers
109 void OnButtonSetPath(wxCommandEvent& event);
110 void OnButtonReset(wxCommandEvent& event);
111 void OnStdPath(wxCommandEvent& event);
112 void OnCheckBox(wxCommandEvent& event);
113 void OnRadioBox(wxCommandEvent& event);
114
115 // reset the control parameters
116 void Reset();
117
118 // (re)create the m_dirCtrl
119 void CreateDirCtrl();
120
121 // the controls
122 // ------------
123
124 // the control itself and the sizer it is in
125 wxGenericDirCtrl *m_dirCtrl;
126
127 // the text entries for command parameters
128 wxTextCtrl *m_path;
129
130 wxRadioBox *m_radioStdPath;
131
132 // flags
133 wxCheckBox *m_chkDirOnly,
134 *m_chk3D,
135 *m_chkFirst,
136 *m_chkLabels,
137 *m_chkMulti;
138
139 // filters
140 wxCheckBox *m_fltr[3];
141
142 private:
143 DECLARE_EVENT_TABLE()
144 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage)
145 };
146
147 // ----------------------------------------------------------------------------
148 // event tables
149 // ----------------------------------------------------------------------------
150
151 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage, WidgetsPage)
152 EVT_BUTTON(DirCtrlPage_Reset, DirCtrlWidgetsPage::OnButtonReset)
153 EVT_BUTTON(DirCtrlPage_SetPath, DirCtrlWidgetsPage::OnButtonSetPath)
154 EVT_CHECKBOX(wxID_ANY, DirCtrlWidgetsPage::OnCheckBox)
155 EVT_RADIOBOX(wxID_ANY, DirCtrlWidgetsPage::OnRadioBox)
156 END_EVENT_TABLE()
157
158 // ============================================================================
159 // implementation
160 // ============================================================================
161
162 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage, wxT("DirCtrl"),
163 GENERIC_CTRLS
164 );
165
166 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book,
167 wxImageList *imaglist)
168 :WidgetsPage(book, imaglist, dirctrl_xpm)
169 {
170 }
171
172 void DirCtrlWidgetsPage::CreateContent()
173 {
174 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
175
176 // left pane
177 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Dir control details"));
178
179 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
180
181 sizerLeft->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath , wxT("Set &path"), wxID_ANY, &m_path ),
182 0, wxALL | wxALIGN_RIGHT , 5 );
183
184 wxSizer *sizerUseFlags =
185 new wxStaticBoxSizer(wxVERTICAL, this, _T("&Flags"));
186 m_chkDirOnly = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_DIR_ONLY"));
187 m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_3D_INTERNAL"));
188 m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_SELECT_FIRST"));
189 m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_EDIT_LABELS"));
190 m_chkMulti = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_MULTIPLE"));
191 sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
192
193 wxSizer *sizerFilters =
194 new wxStaticBoxSizer(wxVERTICAL, this, _T("&Filters"));
195 m_fltr[0] = CreateCheckBoxAndAddToSizer(sizerFilters, wxString::Format(wxT("all files (%s)|%s"),
196 wxFileSelectorDefaultWildcardStr, wxFileSelectorDefaultWildcardStr));
197 m_fltr[1] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
198 m_fltr[2] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("PNG images (*.png)|*.png"));
199 sizerLeft->Add(sizerFilters, wxSizerFlags().Expand().Border());
200
201 wxButton *btn = new wxButton(this, DirCtrlPage_Reset, _T("&Reset"));
202 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
203
204 // keep consistency between enum and labels of radiobox
205 wxCOMPILE_TIME_ASSERT( stdPathMax == WXSIZEOF(stdPaths), EnumForRadioBoxMismatch);
206
207 // middle pane
208 m_radioStdPath = new wxRadioBox(this, wxID_ANY, _T("Standard path"),
209 wxDefaultPosition, wxDefaultSize,
210 WXSIZEOF(stdPaths), stdPaths, 1);
211
212 // right pane
213 m_dirCtrl = new wxGenericDirCtrl(
214 this,
215 DirCtrlPage_Ctrl,
216 wxDirDialogDefaultFolderStr,
217 wxDefaultPosition,
218 wxDefaultSize,
219 0
220 );
221
222 // the 3 panes panes compose the window
223 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
224 sizerTop->Add(m_radioStdPath, 0, wxGROW | wxALL , 10);
225 sizerTop->Add(m_dirCtrl, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
226
227 // final initializations
228 Reset();
229
230 SetSizer(sizerTop);
231 }
232
233 void DirCtrlWidgetsPage::Reset()
234 {
235 m_path->SetValue(m_dirCtrl->GetPath());
236 }
237
238 void DirCtrlWidgetsPage::CreateDirCtrl()
239 {
240 wxWindowUpdateLocker noUpdates(this);
241
242 wxGenericDirCtrl *dirCtrl = new wxGenericDirCtrl(
243 this,
244 DirCtrlPage_Ctrl,
245 wxDirDialogDefaultFolderStr,
246 wxDefaultPosition,
247 wxDefaultSize,
248 ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
249 ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
250 ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
251 ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) |
252 ( m_chkMulti->IsChecked() ? wxDIRCTRL_MULTIPLE : 0)
253 );
254
255 wxString filter;
256 for (int i = 0; i < 3; ++i)
257 {
258 if (m_fltr[i]->IsChecked())
259 {
260 if (!filter.IsEmpty())
261 filter += wxT("|");
262 filter += m_fltr[i]->GetLabel();
263 }
264 }
265 dirCtrl->SetFilter(filter);
266
267 // update sizer's child window
268 GetSizer()->Replace(m_dirCtrl, dirCtrl, true);
269
270 // update our pointer
271 delete m_dirCtrl;
272 m_dirCtrl = dirCtrl;
273
274 // relayout the sizer
275 GetSizer()->Layout();
276 }
277
278 // ----------------------------------------------------------------------------
279 // event handlers
280 // ----------------------------------------------------------------------------
281
282 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent& WXUNUSED(event))
283 {
284 m_dirCtrl->SetPath(m_path->GetValue());
285 }
286
287 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
288 {
289 Reset();
290
291 CreateDirCtrl();
292 }
293
294 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
295 {
296 CreateDirCtrl();
297 }
298
299 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event))
300 {
301 wxString path;
302
303 wxTheApp->SetAppName(_T("widgets"));
304 wxStandardPathsBase& stdp = wxStandardPaths::Get();
305
306 switch ( m_radioStdPath->GetSelection() )
307 {
308 default:
309 case stdPathUnknown:
310 case stdPathMax:
311 // leave path
312 break;
313
314 case stdPathConfig:
315 path = stdp.GetConfigDir();
316 break;
317
318 case stdPathData:
319 path = stdp.GetDataDir();
320 break;
321
322 case stdPathDocuments:
323 path = stdp.GetDocumentsDir();
324 break;
325
326 case stdPathLocalData:
327 path = stdp.GetLocalDataDir();
328 break;
329
330 case stdPathPlugins:
331 path = stdp.GetPluginsDir();
332 break;
333
334 case stdPathResources:
335 path = stdp.GetResourcesDir();
336 break;
337
338 case stdPathUserConfig:
339 path = stdp.GetUserConfigDir();
340 break;
341
342 case stdPathUserData:
343 path = stdp.GetUserDataDir();
344 break;
345
346 case stdPathUserLocalData:
347 path = stdp.GetUserLocalDataDir();
348 break;
349 }
350
351 m_dirCtrl->SetPath(path);
352 if(!m_dirCtrl->GetPath().IsSameAs(path))
353 {
354 wxLogMessage(_T("Selected standard path and path from control do not match!"));
355 m_radioStdPath->SetSelection(stdPathUnknown);
356 }
357 }
358
359 #endif // wxUSE_DIRDLG