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