wxGenericDirCtrl page for widgets sample.
[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/sizer.h"
32 #include "wx/statbox.h"
33 #include "wx/radiobox.h"
34 #include "wx/checkbox.h"
35 #include "wx/button.h"
36 #include "wx/app.h"
37 #endif
38
39 #include "wx/generic/dirctrlg.h"
40
41 #include "wx/wupdlock.h"
42 #include "wx/stdpaths.h"
43 #include "wx/msgdlg.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_chkFilters,
136 *m_chkLabels;
137
138 private:
139 DECLARE_EVENT_TABLE()
140 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage)
141 };
142
143 // ----------------------------------------------------------------------------
144 // event tables
145 // ----------------------------------------------------------------------------
146
147 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage, WidgetsPage)
148 EVT_BUTTON(DirCtrlPage_Reset, DirCtrlWidgetsPage::OnButtonReset)
149 EVT_BUTTON(DirCtrlPage_SetPath, DirCtrlWidgetsPage::OnButtonSetPath)
150 EVT_CHECKBOX(wxID_ANY, DirCtrlWidgetsPage::OnCheckBox)
151 EVT_RADIOBOX(wxID_ANY, DirCtrlWidgetsPage::OnRadioBox)
152 END_EVENT_TABLE()
153
154 // ============================================================================
155 // implementation
156 // ============================================================================
157
158 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage, wxT("DirCtrl"),
159 GENERIC_CTRLS
160 );
161
162 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book,
163 wxImageList *imaglist)
164 :WidgetsPage(book, imaglist, dirctrl_xpm)
165 {
166 }
167
168 void DirCtrlWidgetsPage::CreateContent()
169 {
170 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
171
172 // left pane
173 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Dir control details"));
174
175 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
176
177 sizerLeft->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath , wxT("Set &path"), wxID_ANY, &m_path ),
178 0, wxALL | wxALIGN_RIGHT , 5 );
179
180 wxSizer *sizerUseFlags =
181 new wxStaticBoxSizer(wxVERTICAL, this, _T("&Flags"));
182 m_chkDirOnly = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_DIR_ONLY"));
183 m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_3D_INTERNAL"));
184 m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_SELECT_FIRST"));
185 m_chkFilters = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_SHOW_FILTERS"));
186 m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_EDIT_LABELS"));
187 sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
188
189 wxButton *btn = new wxButton(this, DirCtrlPage_Reset, _T("&Reset"));
190 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
191
192 // keep consistency between enum and labels of radiobox
193 wxCOMPILE_TIME_ASSERT( stdPathMax == WXSIZEOF(stdPaths), EnumForRadioBoxMismatch);
194
195 // middle pane
196 m_radioStdPath = new wxRadioBox(this, wxID_ANY, _T("Standard path"),
197 wxDefaultPosition, wxDefaultSize,
198 WXSIZEOF(stdPaths), stdPaths, 1);
199
200 // right pane
201 m_dirCtrl = new wxGenericDirCtrl(
202 this,
203 DirCtrlPage_Ctrl,
204 wxDirDialogDefaultFolderStr,
205 wxDefaultPosition,
206 wxDefaultSize,
207 0
208 );
209
210 // the 3 panes panes compose the window
211 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
212 sizerTop->Add(m_radioStdPath, 0, wxGROW | wxALL , 10);
213 sizerTop->Add(m_dirCtrl, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
214
215 // final initializations
216 Reset();
217
218 SetSizer(sizerTop);
219
220 sizerTop->Fit(this);
221 }
222
223 void DirCtrlWidgetsPage::Reset()
224 {
225 m_path->SetValue(m_dirCtrl->GetPath());
226 }
227
228 void DirCtrlWidgetsPage::CreateDirCtrl()
229 {
230 wxWindowUpdateLocker noUpdates(this);
231
232 wxGenericDirCtrl *dirCtrl = new wxGenericDirCtrl(
233 this,
234 DirCtrlPage_Ctrl,
235 wxDirDialogDefaultFolderStr,
236 wxDefaultPosition,
237 wxDefaultSize,
238 ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
239 ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
240 ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
241 ( m_chkFilters->IsChecked() ? wxDIRCTRL_SHOW_FILTERS : 0 ) |
242 ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 )
243 );
244
245 // update sizer's child window
246 GetSizer()->Replace(m_dirCtrl, dirCtrl, true);
247
248 // update our pointer
249 delete m_dirCtrl;
250 m_dirCtrl = dirCtrl;
251
252 // relayout the sizer
253 GetSizer()->Layout();
254 }
255
256 // ----------------------------------------------------------------------------
257 // event handlers
258 // ----------------------------------------------------------------------------
259
260 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent& WXUNUSED(event))
261 {
262 m_dirCtrl->SetPath(m_path->GetValue());
263 }
264
265 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
266 {
267 Reset();
268
269 CreateDirCtrl();
270 }
271
272 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
273 {
274 CreateDirCtrl();
275 }
276
277 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event))
278 {
279 wxString path;
280
281 wxTheApp->SetAppName(_T("widgets"));
282 wxStandardPathsBase& stdp = wxStandardPaths::Get();
283
284 switch ( m_radioStdPath->GetSelection() )
285 {
286 default:
287 case stdPathUnknown:
288 case stdPathMax:
289 // leave path
290 break;
291
292 case stdPathConfig:
293 path = stdp.GetConfigDir();
294 break;
295
296 case stdPathData:
297 path = stdp.GetDataDir();
298 break;
299
300 case stdPathDocuments:
301 path = stdp.GetDocumentsDir();
302 break;
303
304 case stdPathLocalData:
305 path = stdp.GetLocalDataDir();
306 break;
307
308 case stdPathPlugins:
309 path = stdp.GetPluginsDir();
310 break;
311
312 case stdPathResources:
313 path = stdp.GetResourcesDir();
314 break;
315
316 case stdPathUserConfig:
317 path = stdp.GetUserConfigDir();
318 break;
319
320 case stdPathUserData:
321 path = stdp.GetUserDataDir();
322 break;
323
324 case stdPathUserLocalData:
325 path = stdp.GetUserLocalDataDir();
326 break;
327 }
328
329 m_dirCtrl->SetPath(path);
330 if(!m_dirCtrl->GetPath().IsSameAs(path))
331 {
332 wxLogMessage(_T("Selected standard path and path from control do not match!"));
333 m_radioStdPath->SetSelection(stdPathUnknown);
334 }
335 }
336
337 #endif // wxUSE_DIRDLG