Added wxFilePickerCtrl::SetInitialDirectory().
[wxWidgets.git] / samples / widgets / dirpicker.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: dirpicker.cpp
4 // Purpose: Shows wxDirPickerCtrl
5 // Author: Francesco Montorsi
6 // Created: 20/6/2006
7 // Id: $Id$
8 // Copyright: (c) 2006 Francesco Montorsi
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_DIRPICKERCTRL
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/radiobox.h"
34 #include "wx/textctrl.h"
35 #endif
36
37 #include "wx/artprov.h"
38 #include "wx/sizer.h"
39 #include "wx/stattext.h"
40 #include "wx/checkbox.h"
41 #include "wx/imaglist.h"
42
43 #include "wx/filepicker.h"
44 #include "widgets.h"
45
46 #include "icons/dirpicker.xpm"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // control ids
53 enum
54 {
55 PickerPage_Reset = wxID_HIGHEST,
56 PickerPage_Dir,
57 PickerPage_SetDir
58 };
59
60
61 // ----------------------------------------------------------------------------
62 // DirPickerWidgetsPage
63 // ----------------------------------------------------------------------------
64
65 class DirPickerWidgetsPage : public WidgetsPage
66 {
67 public:
68 DirPickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
69 virtual ~DirPickerWidgetsPage(){};
70
71 virtual wxControl *GetWidget() const { return m_dirPicker; }
72 virtual void RecreateWidget() { RecreatePicker(); }
73
74 // lazy creation of the content
75 virtual void CreateContent();
76
77 protected:
78
79 // called only once at first construction
80 void CreatePicker();
81
82 // called to recreate an existing control
83 void RecreatePicker();
84
85 // restore the checkboxes state to the initial values
86 void Reset();
87
88 // get the initial style for the picker of the given kind
89 long GetPickerStyle();
90
91
92 void OnDirChange(wxFileDirPickerEvent &ev);
93 void OnCheckBox(wxCommandEvent &ev);
94 void OnButtonReset(wxCommandEvent &ev);
95 void OnButtonSetDir(wxCommandEvent &ev);
96
97
98 // the picker
99 wxDirPickerCtrl *m_dirPicker;
100
101
102 // other controls
103 // --------------
104
105 wxCheckBox *m_chkDirTextCtrl,
106 *m_chkDirChangeDir,
107 *m_chkDirMustExist,
108 *m_chkSmall;
109 wxTextCtrl *m_textInitialDir;
110
111 wxBoxSizer *m_sizer;
112
113 private:
114 DECLARE_EVENT_TABLE()
115 DECLARE_WIDGETS_PAGE(DirPickerWidgetsPage)
116 };
117
118 // ----------------------------------------------------------------------------
119 // event tables
120 // ----------------------------------------------------------------------------
121
122 BEGIN_EVENT_TABLE(DirPickerWidgetsPage, WidgetsPage)
123 EVT_BUTTON(PickerPage_Reset, DirPickerWidgetsPage::OnButtonReset)
124 EVT_BUTTON(PickerPage_SetDir, DirPickerWidgetsPage::OnButtonSetDir)
125
126 EVT_DIRPICKER_CHANGED(PickerPage_Dir, DirPickerWidgetsPage::OnDirChange)
127
128 EVT_CHECKBOX(wxID_ANY, DirPickerWidgetsPage::OnCheckBox)
129 END_EVENT_TABLE()
130
131 // ============================================================================
132 // implementation
133 // ============================================================================
134
135 #if defined(__WXGTK24__)
136 #define FAMILY_CTRLS NATIVE_CTRLS
137 #else
138 #define FAMILY_CTRLS GENERIC_CTRLS
139 #endif
140
141 IMPLEMENT_WIDGETS_PAGE(DirPickerWidgetsPage, wxT("DirPicker"),
142 PICKER_CTRLS | FAMILY_CTRLS);
143
144 DirPickerWidgetsPage::DirPickerWidgetsPage(WidgetsBookCtrl *book,
145 wxImageList *imaglist)
146 : WidgetsPage(book, imaglist, dirpicker_xpm)
147 {
148 }
149
150 void DirPickerWidgetsPage::CreateContent()
151 {
152 // left pane
153 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
154
155 wxStaticBoxSizer *dirbox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&DirPicker style"));
156 m_chkDirTextCtrl = CreateCheckBoxAndAddToSizer(dirbox, wxT("With textctrl"), false);
157 m_chkDirMustExist = CreateCheckBoxAndAddToSizer(dirbox, wxT("Dir must exist"), false);
158 m_chkDirChangeDir = CreateCheckBoxAndAddToSizer(dirbox, wxT("Change working dir"), false);
159 m_chkSmall = CreateCheckBoxAndAddToSizer(dirbox, "&Small version", false);
160 boxleft->Add(dirbox, 0, wxALL|wxGROW, 5);
161
162 boxleft->Add(CreateSizerWithTextAndButton
163 (
164 PickerPage_SetDir,
165 "&Initial directory",
166 wxID_ANY,
167 &m_textInitialDir
168 ), wxSizerFlags().Expand().Border());
169
170 boxleft->AddSpacer(10);
171
172 boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
173 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
174
175 Reset(); // set checkboxes state
176
177 // create pickers
178 m_dirPicker = NULL;
179 CreatePicker();
180
181 // right pane
182 m_sizer = new wxBoxSizer(wxVERTICAL);
183 m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
184 m_sizer->Add(m_dirPicker, 0, wxEXPAND|wxALL, 5);
185 m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
186
187 // global pane
188 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
189 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
190 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
191
192 SetSizer(sz);
193 }
194
195 void DirPickerWidgetsPage::CreatePicker()
196 {
197 delete m_dirPicker;
198
199 m_dirPicker = new wxDirPickerCtrl(this, PickerPage_Dir,
200 wxGetHomeDir(), wxT("Hello!"),
201 wxDefaultPosition, wxDefaultSize,
202 GetPickerStyle());
203 }
204
205 long DirPickerWidgetsPage::GetPickerStyle()
206 {
207 long style = 0;
208
209 if ( m_chkDirTextCtrl->GetValue() )
210 style |= wxDIRP_USE_TEXTCTRL;
211
212 if ( m_chkDirMustExist->GetValue() )
213 style |= wxDIRP_DIR_MUST_EXIST;
214
215 if ( m_chkDirChangeDir->GetValue() )
216 style |= wxDIRP_CHANGE_DIR;
217
218 if ( m_chkSmall->GetValue() )
219 style |= wxDIRP_SMALL;
220
221 return style;
222 }
223
224 void DirPickerWidgetsPage::RecreatePicker()
225 {
226 m_sizer->Remove(1);
227 CreatePicker();
228 m_sizer->Insert(1, m_dirPicker, 0, wxEXPAND|wxALL, 5);
229
230 m_sizer->Layout();
231 }
232
233 void DirPickerWidgetsPage::Reset()
234 {
235 m_chkDirTextCtrl->SetValue((wxDIRP_DEFAULT_STYLE & wxDIRP_USE_TEXTCTRL) != 0);
236 m_chkDirMustExist->SetValue((wxDIRP_DEFAULT_STYLE & wxDIRP_DIR_MUST_EXIST) != 0);
237 m_chkDirChangeDir->SetValue((wxDIRP_DEFAULT_STYLE & wxDIRP_CHANGE_DIR) != 0);
238 m_chkSmall->SetValue((wxFLP_DEFAULT_STYLE & wxDIRP_SMALL) != 0);
239 }
240
241
242 // ----------------------------------------------------------------------------
243 // event handlers
244 // ----------------------------------------------------------------------------
245
246 void DirPickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event))
247 {
248 m_dirPicker->SetInitialDirectory(m_textInitialDir->GetValue());
249 }
250
251 void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
252 {
253 Reset();
254 RecreatePicker();
255 }
256
257 void DirPickerWidgetsPage::OnDirChange(wxFileDirPickerEvent& event)
258 {
259 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
260 event.GetPath().c_str(), wxGetCwd().c_str());
261 }
262
263 void DirPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
264 {
265 if (event.GetEventObject() == m_chkDirTextCtrl ||
266 event.GetEventObject() == m_chkDirChangeDir ||
267 event.GetEventObject() == m_chkDirMustExist ||
268 event.GetEventObject() == m_chkSmall)
269 RecreatePicker();
270 }
271
272 #endif // wxUSE_DIRPICKERCTRL