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