]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/dirctrl.cpp
Make storing non-trivial data in wxThreadSpecificInfo possible.
[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 // Copyright: (c) 2006 wxWindows team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_DIRDLG
27
28 // for all others, include the necessary headers
29 #ifndef WX_PRECOMP
30 #include "wx/app.h"
31 #include "wx/log.h"
32 #include "wx/sizer.h"
33 #include "wx/statbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/checkbox.h"
36 #include "wx/button.h"
37 #include "wx/filedlg.h"
38 #endif
39
40 #include "wx/generic/dirctrlg.h"
41
42 #include "wx/wupdlock.h"
43 #include "wx/stdpaths.h"
44 #include "wx/filename.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 wxT("&none"),
65 wxT("&config"),
66 wxT("&data"),
67 wxT("&documents"),
68 wxT("&local data"),
69 wxT("&plugins"),
70 wxT("&resources"),
71 wxT("&user config"),
72 wxT("&user data"),
73 wxT("&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 void OnSelChanged(wxTreeEvent& event);
115 void OnFileActivated(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_chkFilters,
139 *m_chkLabels,
140 *m_chkMulti;
141
142 // filters
143 wxCheckBox *m_fltr[3];
144
145 private:
146 DECLARE_EVENT_TABLE()
147 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage)
148 };
149
150 // ----------------------------------------------------------------------------
151 // event tables
152 // ----------------------------------------------------------------------------
153
154 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage, WidgetsPage)
155 EVT_BUTTON(DirCtrlPage_Reset, DirCtrlWidgetsPage::OnButtonReset)
156 EVT_BUTTON(DirCtrlPage_SetPath, DirCtrlWidgetsPage::OnButtonSetPath)
157 EVT_CHECKBOX(wxID_ANY, DirCtrlWidgetsPage::OnCheckBox)
158 EVT_RADIOBOX(wxID_ANY, DirCtrlWidgetsPage::OnRadioBox)
159 EVT_DIRCTRL_SELECTIONCHANGED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnSelChanged)
160 EVT_DIRCTRL_FILEACTIVATED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnFileActivated)
161 END_EVENT_TABLE()
162
163 // ============================================================================
164 // implementation
165 // ============================================================================
166
167 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage, wxT("DirCtrl"),
168 GENERIC_CTRLS
169 );
170
171 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book,
172 wxImageList *imaglist)
173 :WidgetsPage(book, imaglist, dirctrl_xpm)
174 {
175 m_dirCtrl = NULL;
176 }
177
178 void DirCtrlWidgetsPage::CreateContent()
179 {
180 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
181
182 // left pane
183 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Dir control details"));
184
185 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
186
187 sizerLeft->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath , wxT("Set &path"), wxID_ANY, &m_path ),
188 0, wxALL | wxALIGN_RIGHT , 5 );
189
190 wxSizer *sizerUseFlags =
191 new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Flags"));
192 m_chkDirOnly = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_DIR_ONLY"));
193 m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_3D_INTERNAL"));
194 m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SELECT_FIRST"));
195 m_chkFilters = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SHOW_FILTERS"));
196 m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_EDIT_LABELS"));
197 m_chkMulti = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_MULTIPLE"));
198 sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
199
200 wxSizer *sizerFilters =
201 new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Filters"));
202 m_fltr[0] = CreateCheckBoxAndAddToSizer(sizerFilters, wxString::Format(wxT("all files (%s)|%s"),
203 wxFileSelectorDefaultWildcardStr, wxFileSelectorDefaultWildcardStr));
204 m_fltr[1] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
205 m_fltr[2] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("PNG images (*.png)|*.png"));
206 sizerLeft->Add(sizerFilters, wxSizerFlags().Expand().Border());
207
208 wxButton *btn = new wxButton(this, DirCtrlPage_Reset, wxT("&Reset"));
209 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
210
211 // keep consistency between enum and labels of radiobox
212 wxCOMPILE_TIME_ASSERT( stdPathMax == WXSIZEOF(stdPaths), EnumForRadioBoxMismatch);
213
214 // middle pane
215 m_radioStdPath = new wxRadioBox(this, wxID_ANY, wxT("Standard path"),
216 wxDefaultPosition, wxDefaultSize,
217 WXSIZEOF(stdPaths), stdPaths, 1);
218
219 // right pane
220 m_dirCtrl = new wxGenericDirCtrl(
221 this,
222 DirCtrlPage_Ctrl,
223 wxDirDialogDefaultFolderStr,
224 wxDefaultPosition,
225 wxDefaultSize,
226 0
227 );
228
229 // the 3 panes panes compose the window
230 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
231 sizerTop->Add(m_radioStdPath, 0, wxGROW | wxALL , 10);
232 sizerTop->Add(m_dirCtrl, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
233
234 // final initializations
235 Reset();
236
237 SetSizer(sizerTop);
238 }
239
240 void DirCtrlWidgetsPage::Reset()
241 {
242 m_path->SetValue(m_dirCtrl->GetPath());
243 }
244
245 void DirCtrlWidgetsPage::CreateDirCtrl()
246 {
247 wxWindowUpdateLocker noUpdates(this);
248
249 wxGenericDirCtrl *dirCtrl = new wxGenericDirCtrl(
250 this,
251 DirCtrlPage_Ctrl,
252 wxDirDialogDefaultFolderStr,
253 wxDefaultPosition,
254 wxDefaultSize,
255 ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
256 ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
257 ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
258 ( m_chkFilters->IsChecked() ? wxDIRCTRL_SHOW_FILTERS : 0 ) |
259 ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) |
260 ( m_chkMulti->IsChecked() ? wxDIRCTRL_MULTIPLE : 0)
261 );
262
263 wxString filter;
264 for (int i = 0; i < 3; ++i)
265 {
266 if (m_fltr[i]->IsChecked())
267 {
268 if (!filter.IsEmpty())
269 filter += wxT("|");
270 filter += m_fltr[i]->GetLabel();
271 }
272 }
273 dirCtrl->SetFilter(filter);
274
275 // update sizer's child window
276 GetSizer()->Replace(m_dirCtrl, dirCtrl, true);
277
278 // update our pointer
279 delete m_dirCtrl;
280 m_dirCtrl = dirCtrl;
281
282 // relayout the sizer
283 GetSizer()->Layout();
284 }
285
286 // ----------------------------------------------------------------------------
287 // event handlers
288 // ----------------------------------------------------------------------------
289
290 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent& WXUNUSED(event))
291 {
292 m_dirCtrl->SetPath(m_path->GetValue());
293 }
294
295 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
296 {
297 Reset();
298
299 CreateDirCtrl();
300 }
301
302 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
303 {
304 CreateDirCtrl();
305 }
306
307 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event))
308 {
309 wxString path;
310
311 wxTheApp->SetAppName(wxT("widgets"));
312 wxStandardPathsBase& stdp = wxStandardPaths::Get();
313
314 switch ( m_radioStdPath->GetSelection() )
315 {
316 default:
317 case stdPathUnknown:
318 case stdPathMax:
319 // leave path
320 break;
321
322 case stdPathConfig:
323 path = stdp.GetConfigDir();
324 break;
325
326 case stdPathData:
327 path = stdp.GetDataDir();
328 break;
329
330 case stdPathDocuments:
331 path = stdp.GetDocumentsDir();
332 break;
333
334 case stdPathLocalData:
335 path = stdp.GetLocalDataDir();
336 break;
337
338 case stdPathPlugins:
339 path = stdp.GetPluginsDir();
340 break;
341
342 case stdPathResources:
343 path = stdp.GetResourcesDir();
344 break;
345
346 case stdPathUserConfig:
347 path = stdp.GetUserConfigDir();
348 break;
349
350 case stdPathUserData:
351 path = stdp.GetUserDataDir();
352 break;
353
354 case stdPathUserLocalData:
355 path = stdp.GetUserLocalDataDir();
356 break;
357 }
358
359 m_dirCtrl->SetPath(path);
360
361 // Notice that we must use wxFileName comparison instead of simple wxString
362 // comparison as the paths returned may differ by case only.
363 if ( wxFileName(m_dirCtrl->GetPath()) != path )
364 {
365 wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
366 path, m_dirCtrl->GetPath());
367 }
368 }
369
370 void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent& event)
371 {
372 if ( m_dirCtrl )
373 {
374 wxLogMessage("Selection changed to \"%s\"",
375 m_dirCtrl->GetPath(event.GetItem()));
376 }
377
378 event.Skip();
379 }
380
381 void DirCtrlWidgetsPage::OnFileActivated(wxTreeEvent& event)
382 {
383 if ( m_dirCtrl )
384 {
385 wxLogMessage("File activated \"%s\"",
386 m_dirCtrl->GetPath(event.GetItem()));
387 }
388
389 event.Skip();
390 }
391
392 #endif // wxUSE_DIRDLG