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