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