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