]> git.saurik.com Git - wxWidgets.git/blame - samples/widgets/dirctrl.cpp
Don't set cell value in wxDataViewEvent in one place only.
[wxWidgets.git] / samples / widgets / dirctrl.cpp
CommitLineData
c4f6d998
WS
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
526954c5 9// Licence: wxWindows licence
c4f6d998
WS
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
67bb3c88
WS
31 #include "wx/app.h"
32 #include "wx/log.h"
c4f6d998
WS
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"
ae04c0f1 38 #include "wx/filedlg.h"
c4f6d998
WS
39#endif
40
41#include "wx/generic/dirctrlg.h"
42
43#include "wx/wupdlock.h"
44#include "wx/stdpaths.h"
a7156681 45#include "wx/filename.h"
c4f6d998
WS
46
47#include "widgets.h"
48
49#include "icons/dirctrl.xpm"
50
51// ----------------------------------------------------------------------------
52// constants
53// ----------------------------------------------------------------------------
54
55// control ids
56enum
57{
58 DirCtrlPage_Reset = wxID_HIGHEST,
59 DirCtrlPage_SetPath,
60 DirCtrlPage_Ctrl
61};
62
63static const wxString stdPaths[] =
64{
9a83f860
VZ
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")
c4f6d998
WS
75};
76
77enum
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
96class DirCtrlWidgetsPage : public WidgetsPage
97{
98public:
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
108protected:
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);
84605707 115 void OnSelChanged(wxTreeEvent& event);
c4f6d998
WS
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,
aa9453d6 138 *m_chkFilters,
80f624ec
VZ
139 *m_chkLabels,
140 *m_chkMulti;
141
d0bc78e2
VZ
142 // filters
143 wxCheckBox *m_fltr[3];
c4f6d998
WS
144
145private:
146 DECLARE_EVENT_TABLE()
147 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage)
148};
149
150// ----------------------------------------------------------------------------
151// event tables
152// ----------------------------------------------------------------------------
153
154BEGIN_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)
84605707 159 EVT_DIRCTRL_CHANGED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnSelChanged)
c4f6d998
WS
160END_EVENT_TABLE()
161
162// ============================================================================
163// implementation
164// ============================================================================
165
166IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage, wxT("DirCtrl"),
167 GENERIC_CTRLS
168 );
169
170DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book,
171 wxImageList *imaglist)
172 :WidgetsPage(book, imaglist, dirctrl_xpm)
173{
84605707 174 m_dirCtrl = NULL;
c4f6d998
WS
175}
176
177void DirCtrlWidgetsPage::CreateContent()
178{
179 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
180
181 // left pane
182 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Dir control details"));
183
184 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
185
186 sizerLeft->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath , wxT("Set &path"), wxID_ANY, &m_path ),
187 0, wxALL | wxALIGN_RIGHT , 5 );
188
189 wxSizer *sizerUseFlags =
9a83f860
VZ
190 new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Flags"));
191 m_chkDirOnly = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_DIR_ONLY"));
192 m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_3D_INTERNAL"));
193 m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SELECT_FIRST"));
aa9453d6 194 m_chkFilters = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SHOW_FILTERS"));
9a83f860
VZ
195 m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_EDIT_LABELS"));
196 m_chkMulti = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_MULTIPLE"));
c4f6d998
WS
197 sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
198
d0bc78e2 199 wxSizer *sizerFilters =
9a83f860 200 new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Filters"));
d0bc78e2
VZ
201 m_fltr[0] = CreateCheckBoxAndAddToSizer(sizerFilters, wxString::Format(wxT("all files (%s)|%s"),
202 wxFileSelectorDefaultWildcardStr, wxFileSelectorDefaultWildcardStr));
203 m_fltr[1] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
204 m_fltr[2] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("PNG images (*.png)|*.png"));
205 sizerLeft->Add(sizerFilters, wxSizerFlags().Expand().Border());
206
9a83f860 207 wxButton *btn = new wxButton(this, DirCtrlPage_Reset, wxT("&Reset"));
c4f6d998
WS
208 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
209
210 // keep consistency between enum and labels of radiobox
211 wxCOMPILE_TIME_ASSERT( stdPathMax == WXSIZEOF(stdPaths), EnumForRadioBoxMismatch);
212
213 // middle pane
9a83f860 214 m_radioStdPath = new wxRadioBox(this, wxID_ANY, wxT("Standard path"),
c4f6d998
WS
215 wxDefaultPosition, wxDefaultSize,
216 WXSIZEOF(stdPaths), stdPaths, 1);
217
218 // right pane
219 m_dirCtrl = new wxGenericDirCtrl(
220 this,
221 DirCtrlPage_Ctrl,
222 wxDirDialogDefaultFolderStr,
223 wxDefaultPosition,
224 wxDefaultSize,
225 0
226 );
227
228 // the 3 panes panes compose the window
229 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
230 sizerTop->Add(m_radioStdPath, 0, wxGROW | wxALL , 10);
231 sizerTop->Add(m_dirCtrl, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
232
233 // final initializations
234 Reset();
235
236 SetSizer(sizerTop);
c4f6d998
WS
237}
238
239void DirCtrlWidgetsPage::Reset()
240{
241 m_path->SetValue(m_dirCtrl->GetPath());
242}
243
244void DirCtrlWidgetsPage::CreateDirCtrl()
245{
246 wxWindowUpdateLocker noUpdates(this);
247
248 wxGenericDirCtrl *dirCtrl = new wxGenericDirCtrl(
249 this,
250 DirCtrlPage_Ctrl,
251 wxDirDialogDefaultFolderStr,
252 wxDefaultPosition,
253 wxDefaultSize,
254 ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
255 ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
256 ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
aa9453d6 257 ( m_chkFilters->IsChecked() ? wxDIRCTRL_SHOW_FILTERS : 0 ) |
80f624ec
VZ
258 ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) |
259 ( m_chkMulti->IsChecked() ? wxDIRCTRL_MULTIPLE : 0)
c4f6d998
WS
260 );
261
d0bc78e2 262 wxString filter;
80f624ec 263 for (int i = 0; i < 3; ++i)
d0bc78e2 264 {
80f624ec 265 if (m_fltr[i]->IsChecked())
d0bc78e2
VZ
266 {
267 if (!filter.IsEmpty())
268 filter += wxT("|");
269 filter += m_fltr[i]->GetLabel();
270 }
271 }
272 dirCtrl->SetFilter(filter);
273
c4f6d998
WS
274 // update sizer's child window
275 GetSizer()->Replace(m_dirCtrl, dirCtrl, true);
276
277 // update our pointer
278 delete m_dirCtrl;
279 m_dirCtrl = dirCtrl;
280
281 // relayout the sizer
282 GetSizer()->Layout();
283}
284
285// ----------------------------------------------------------------------------
286// event handlers
287// ----------------------------------------------------------------------------
288
289void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent& WXUNUSED(event))
290{
291 m_dirCtrl->SetPath(m_path->GetValue());
292}
293
294void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
295{
296 Reset();
297
298 CreateDirCtrl();
299}
300
301void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
302{
303 CreateDirCtrl();
304}
305
306void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event))
307{
308 wxString path;
309
9a83f860 310 wxTheApp->SetAppName(wxT("widgets"));
c4f6d998
WS
311 wxStandardPathsBase& stdp = wxStandardPaths::Get();
312
313 switch ( m_radioStdPath->GetSelection() )
314 {
315 default:
316 case stdPathUnknown:
317 case stdPathMax:
318 // leave path
319 break;
320
321 case stdPathConfig:
322 path = stdp.GetConfigDir();
323 break;
324
325 case stdPathData:
326 path = stdp.GetDataDir();
327 break;
328
329 case stdPathDocuments:
330 path = stdp.GetDocumentsDir();
331 break;
332
333 case stdPathLocalData:
334 path = stdp.GetLocalDataDir();
335 break;
336
337 case stdPathPlugins:
338 path = stdp.GetPluginsDir();
339 break;
340
341 case stdPathResources:
342 path = stdp.GetResourcesDir();
343 break;
344
345 case stdPathUserConfig:
346 path = stdp.GetUserConfigDir();
347 break;
348
349 case stdPathUserData:
350 path = stdp.GetUserDataDir();
351 break;
352
353 case stdPathUserLocalData:
354 path = stdp.GetUserLocalDataDir();
355 break;
356 }
357
358 m_dirCtrl->SetPath(path);
a7156681
VZ
359
360 // Notice that we must use wxFileName comparison instead of simple wxString
361 // comparison as the paths returned may differ by case only.
362 if ( wxFileName(m_dirCtrl->GetPath()) != path )
c4f6d998 363 {
a7156681
VZ
364 wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
365 path, m_dirCtrl->GetPath());
c4f6d998
WS
366 }
367}
368
84605707
VZ
369void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent& event)
370{
371 if ( m_dirCtrl )
372 {
373 wxLogMessage("Selection changed to \"%s\"",
374 m_dirCtrl->GetPath(event.GetItem()));
375 }
376
377 event.Skip();
378}
379
c4f6d998 380#endif // wxUSE_DIRDLG