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: dirctrl.cpp 43755 2006-12-03 13:43:44Z VZ $
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 #endif
39 
40 #include "wx/generic/dirctrlg.h"
41 
42 #include "wx/wupdlock.h"
43 #include "wx/stdpaths.h"
44 
45 #include "widgets.h"
46 
47 #include "icons/dirctrl.xpm"
48 
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52 
53 // control ids
54 enum
55 {
56     DirCtrlPage_Reset = wxID_HIGHEST,
57     DirCtrlPage_SetPath,
58     DirCtrlPage_Ctrl
59 };
60 
61 static const wxString stdPaths[] =
62 {
63     _T("&none"),
64     _T("&config"),
65     _T("&data"),
66     _T("&documents"),
67     _T("&local data"),
68     _T("&plugins"),
69     _T("&resources"),
70     _T("&user config"),
71     _T("&user data"),
72     _T("&user local data")
73 };
74 
75 enum
76 {
77     stdPathUnknown = 0,
78     stdPathConfig,
79     stdPathData,
80     stdPathDocuments,
81     stdPathLocalData,
82     stdPathPlugins,
83     stdPathResources,
84     stdPathUserConfig,
85     stdPathUserData,
86     stdPathUserLocalData,
87     stdPathMax
88 };
89 
90 // ----------------------------------------------------------------------------
91 // CheckBoxWidgetsPage
92 // ----------------------------------------------------------------------------
93 
94 class DirCtrlWidgetsPage : public WidgetsPage
95 {
96 public:
97     DirCtrlWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
~DirCtrlWidgetsPage()98     virtual ~DirCtrlWidgetsPage() {}
99 
GetWidget() const100     virtual wxControl *GetWidget() const { return m_dirCtrl; }
RecreateWidget()101     virtual void RecreateWidget() { CreateDirCtrl(); }
102 
103     // lazy creation of the content
104     virtual void CreateContent();
105 
106 protected:
107     // event handlers
108     void OnButtonSetPath(wxCommandEvent& event);
109     void OnButtonReset(wxCommandEvent& event);
110     void OnStdPath(wxCommandEvent& event);
111     void OnCheckBox(wxCommandEvent& event);
112     void OnRadioBox(wxCommandEvent& event);
113 
114     // reset the control parameters
115     void Reset();
116 
117     // (re)create the m_dirCtrl
118     void CreateDirCtrl();
119 
120     // the controls
121     // ------------
122 
123     // the control itself and the sizer it is in
124     wxGenericDirCtrl *m_dirCtrl;
125 
126     // the text entries for command parameters
127     wxTextCtrl *m_path;
128 
129     wxRadioBox *m_radioStdPath;
130 
131     // flags
132     wxCheckBox *m_chkDirOnly,
133                *m_chk3D,
134                *m_chkFirst,
135                *m_chkFilters,
136                *m_chkLabels;
137 
138 private:
139     DECLARE_EVENT_TABLE()
140     DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage)
141 };
142 
143 // ----------------------------------------------------------------------------
144 // event tables
145 // ----------------------------------------------------------------------------
146 
147 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage, WidgetsPage)
148     EVT_BUTTON(DirCtrlPage_Reset, DirCtrlWidgetsPage::OnButtonReset)
149     EVT_BUTTON(DirCtrlPage_SetPath, DirCtrlWidgetsPage::OnButtonSetPath)
150     EVT_CHECKBOX(wxID_ANY, DirCtrlWidgetsPage::OnCheckBox)
151     EVT_RADIOBOX(wxID_ANY, DirCtrlWidgetsPage::OnRadioBox)
152 END_EVENT_TABLE()
153 
154 // ============================================================================
155 // implementation
156 // ============================================================================
157 
158 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage, wxT("DirCtrl"),
159                        GENERIC_CTRLS
160                        );
161 
DirCtrlWidgetsPage(WidgetsBookCtrl * book,wxImageList * imaglist)162 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book,
163                                        wxImageList *imaglist)
164                    :WidgetsPage(book, imaglist, dirctrl_xpm)
165 {
166 }
167 
CreateContent()168 void DirCtrlWidgetsPage::CreateContent()
169 {
170     wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
171 
172     // left pane
173     wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Dir control details"));
174 
175     wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
176 
177     sizerLeft->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath , wxT("Set &path"), wxID_ANY, &m_path ),
178                     0, wxALL | wxALIGN_RIGHT , 5 );
179 
180     wxSizer *sizerUseFlags =
181         new wxStaticBoxSizer(wxVERTICAL, this, _T("&Flags"));
182     m_chkDirOnly = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_DIR_ONLY"));
183     m_chk3D      = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_3D_INTERNAL"));
184     m_chkFirst   = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_SELECT_FIRST"));
185     m_chkFilters = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_SHOW_FILTERS"));
186     m_chkLabels  = CreateCheckBoxAndAddToSizer(sizerUseFlags, _T("wxDIRCTRL_EDIT_LABELS"));
187     sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
188 
189     wxButton *btn = new wxButton(this, DirCtrlPage_Reset, _T("&Reset"));
190     sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
191 
192     // keep consistency between enum and labels of radiobox
193     wxCOMPILE_TIME_ASSERT( stdPathMax == WXSIZEOF(stdPaths), EnumForRadioBoxMismatch);
194 
195     // middle pane
196     m_radioStdPath = new wxRadioBox(this, wxID_ANY, _T("Standard path"),
197                                     wxDefaultPosition, wxDefaultSize,
198                                     WXSIZEOF(stdPaths), stdPaths, 1);
199 
200     // right pane
201     m_dirCtrl = new wxGenericDirCtrl(
202         this,
203         DirCtrlPage_Ctrl,
204         wxDirDialogDefaultFolderStr,
205         wxDefaultPosition,
206         wxDefaultSize,
207         0
208     );
209 
210     // the 3 panes panes compose the window
211     sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
212     sizerTop->Add(m_radioStdPath, 0, wxGROW | wxALL , 10);
213     sizerTop->Add(m_dirCtrl, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
214 
215     // final initializations
216     Reset();
217 
218     SetSizer(sizerTop);
219 }
220 
Reset()221 void DirCtrlWidgetsPage::Reset()
222 {
223     m_path->SetValue(m_dirCtrl->GetPath());
224 }
225 
CreateDirCtrl()226 void DirCtrlWidgetsPage::CreateDirCtrl()
227 {
228     wxWindowUpdateLocker noUpdates(this);
229 
230     wxGenericDirCtrl *dirCtrl = new wxGenericDirCtrl(
231         this,
232         DirCtrlPage_Ctrl,
233         wxDirDialogDefaultFolderStr,
234         wxDefaultPosition,
235         wxDefaultSize,
236         ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
237         ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
238         ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
239         ( m_chkFilters->IsChecked() ? wxDIRCTRL_SHOW_FILTERS : 0 ) |
240         ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 )
241     );
242 
243     // update sizer's child window
244     GetSizer()->Replace(m_dirCtrl, dirCtrl, true);
245 
246     // update our pointer
247     delete m_dirCtrl;
248     m_dirCtrl = dirCtrl;
249 
250     // relayout the sizer
251     GetSizer()->Layout();
252 }
253 
254 // ----------------------------------------------------------------------------
255 // event handlers
256 // ----------------------------------------------------------------------------
257 
OnButtonSetPath(wxCommandEvent & WXUNUSED (event))258 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent& WXUNUSED(event))
259 {
260     m_dirCtrl->SetPath(m_path->GetValue());
261 }
262 
OnButtonReset(wxCommandEvent & WXUNUSED (event))263 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
264 {
265     Reset();
266 
267     CreateDirCtrl();
268 }
269 
OnCheckBox(wxCommandEvent & WXUNUSED (event))270 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
271 {
272     CreateDirCtrl();
273 }
274 
OnRadioBox(wxCommandEvent & WXUNUSED (event))275 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event))
276 {
277     wxString path;
278 
279     wxTheApp->SetAppName(_T("widgets"));
280     wxStandardPathsBase& stdp = wxStandardPaths::Get();
281 
282     switch ( m_radioStdPath->GetSelection() )
283     {
284         default:
285         case stdPathUnknown:
286         case stdPathMax:
287             // leave path
288             break;
289 
290         case stdPathConfig:
291             path = stdp.GetConfigDir();
292             break;
293 
294         case stdPathData:
295             path = stdp.GetDataDir();
296             break;
297 
298         case stdPathDocuments:
299             path = stdp.GetDocumentsDir();
300             break;
301 
302         case stdPathLocalData:
303             path = stdp.GetLocalDataDir();
304             break;
305 
306         case stdPathPlugins:
307             path = stdp.GetPluginsDir();
308             break;
309 
310         case stdPathResources:
311             path = stdp.GetResourcesDir();
312             break;
313 
314         case stdPathUserConfig:
315             path = stdp.GetUserConfigDir();
316             break;
317 
318         case stdPathUserData:
319             path = stdp.GetUserDataDir();
320             break;
321 
322         case stdPathUserLocalData:
323             path = stdp.GetUserLocalDataDir();
324             break;
325     }
326 
327     m_dirCtrl->SetPath(path);
328     if(!m_dirCtrl->GetPath().IsSameAs(path))
329     {
330         wxLogMessage(_T("Selected standard path and path from control do not match!"));
331         m_radioStdPath->SetSelection(stdPathUnknown);
332     }
333 }
334 
335 #endif // wxUSE_DIRDLG
336