]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/filepicker.cpp
Add wxEVT_DIRCTRL_FILEACTIVATED wxDirCtrl event.
[wxWidgets.git] / samples / widgets / filepicker.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: filepicker.cpp
4 // Purpose: Part of the widgets sample showing wx*PickerCtrl
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_FILEPICKERCTRL
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/filepicker.xpm"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 enum
53 {
54 FilePickerMode_Open = 0,
55 FilePickerMode_Save
56 };
57
58 // control ids
59 enum
60 {
61 PickerPage_Reset = wxID_HIGHEST,
62 PickerPage_File,
63 PickerPage_SetDir,
64 PickerPage_CurrentPath
65 };
66
67
68 // ----------------------------------------------------------------------------
69 // FilePickerWidgetsPage
70 // ----------------------------------------------------------------------------
71
72 class FilePickerWidgetsPage : public WidgetsPage
73 {
74 public:
75 FilePickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
76 virtual ~FilePickerWidgetsPage(){};
77
78 virtual wxControl *GetWidget() const { return m_filePicker; }
79 virtual void RecreateWidget() { RecreatePicker(); }
80
81 // lazy creation of the content
82 virtual void CreateContent();
83
84 protected:
85
86 // called only once at first construction
87 void CreatePicker();
88
89 // called to recreate an existing control
90 void RecreatePicker();
91
92 // restore the checkboxes state to the initial values
93 void Reset();
94
95 // get the initial style for the picker of the given kind
96 long GetPickerStyle();
97
98 // update filepicker radiobox
99 void UpdateFilePickerMode();
100
101 // the pickers and the relative event handlers
102 void OnFileChange(wxFileDirPickerEvent &ev);
103 void OnCheckBox(wxCommandEvent &ev);
104 void OnButtonReset(wxCommandEvent &ev);
105 void OnButtonSetDir(wxCommandEvent &ev);
106 void OnUpdatePath(wxUpdateUIEvent &ev);
107
108
109 // the picker
110 wxFilePickerCtrl *m_filePicker;
111
112
113 // other controls
114 // --------------
115
116 wxCheckBox *m_chkFileTextCtrl,
117 *m_chkFileOverwritePrompt,
118 *m_chkFileMustExist,
119 *m_chkFileChangeDir,
120 *m_chkSmall;
121 wxRadioBox *m_radioFilePickerMode;
122 wxStaticText *m_labelPath;
123 wxTextCtrl *m_textInitialDir;
124
125 wxBoxSizer *m_sizer;
126
127 private:
128 DECLARE_EVENT_TABLE()
129 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage)
130 };
131
132 // ----------------------------------------------------------------------------
133 // event tables
134 // ----------------------------------------------------------------------------
135
136 BEGIN_EVENT_TABLE(FilePickerWidgetsPage, WidgetsPage)
137 EVT_BUTTON(PickerPage_Reset, FilePickerWidgetsPage::OnButtonReset)
138 EVT_BUTTON(PickerPage_SetDir, FilePickerWidgetsPage::OnButtonSetDir)
139
140 EVT_FILEPICKER_CHANGED(PickerPage_File, FilePickerWidgetsPage::OnFileChange)
141
142 EVT_CHECKBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
143 EVT_RADIOBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
144
145 EVT_UPDATE_UI(PickerPage_CurrentPath, FilePickerWidgetsPage::OnUpdatePath)
146 END_EVENT_TABLE()
147
148 // ============================================================================
149 // implementation
150 // ============================================================================
151
152 #if defined(__WXGTK24__)
153 #define FAMILY_CTRLS NATIVE_CTRLS
154 #else
155 #define FAMILY_CTRLS GENERIC_CTRLS
156 #endif
157
158 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage, wxT("FilePicker"),
159 PICKER_CTRLS | FAMILY_CTRLS);
160
161 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl *book,
162 wxImageList *imaglist)
163 : WidgetsPage(book, imaglist, filepicker_xpm)
164 {
165 }
166
167 void FilePickerWidgetsPage::CreateContent()
168 {
169 // left pane
170 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
171
172 static const wxString mode[] = { wxT("open"), wxT("save") };
173 m_radioFilePickerMode = new wxRadioBox(this, wxID_ANY, wxT("wxFilePicker mode"),
174 wxDefaultPosition, wxDefaultSize,
175 WXSIZEOF(mode), mode);
176 boxleft->Add(m_radioFilePickerMode, 0, wxALL|wxGROW, 5);
177
178 wxStaticBoxSizer *filebox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&FilePicker style"));
179 m_chkFileTextCtrl = CreateCheckBoxAndAddToSizer(filebox, wxT("With textctrl"), false);
180 m_chkFileOverwritePrompt = CreateCheckBoxAndAddToSizer(filebox, wxT("Overwrite prompt"), false);
181 m_chkFileMustExist = CreateCheckBoxAndAddToSizer(filebox, wxT("File must exist"), false);
182 m_chkFileChangeDir = CreateCheckBoxAndAddToSizer(filebox, wxT("Change working dir"), false);
183 m_chkSmall = CreateCheckBoxAndAddToSizer(filebox, "&Small version", false);
184
185 boxleft->Add(filebox, 0, wxALL|wxGROW, 5);
186
187 boxleft->Add(CreateSizerWithTextAndButton
188 (
189 PickerPage_SetDir,
190 "&Initial directory",
191 wxID_ANY,
192 &m_textInitialDir
193 ), wxSizerFlags().Expand().Border());
194
195 boxleft->AddSpacer(10);
196
197 boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
198 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
199
200 Reset(); // set checkboxes state
201
202 // create the picker and the static text displaying its current value
203 m_labelPath = new wxStaticText(this, PickerPage_CurrentPath, "");
204
205 m_filePicker = NULL;
206 CreatePicker();
207
208 // right pane
209 m_sizer = new wxBoxSizer(wxVERTICAL);
210 m_sizer->AddStretchSpacer();
211 m_sizer->Add(m_filePicker, wxSizerFlags().Expand().Border());
212 m_sizer->AddStretchSpacer();
213 m_sizer->Add(m_labelPath, wxSizerFlags().Expand().Border());
214 m_sizer->AddStretchSpacer();
215
216 // global pane
217 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
218 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
219 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
220
221 SetSizer(sz);
222 }
223
224 void FilePickerWidgetsPage::CreatePicker()
225 {
226 delete m_filePicker;
227
228 // pass an empty string as initial file
229 m_filePicker = new wxFilePickerCtrl(this, PickerPage_File,
230 wxEmptyString,
231 wxT("Hello!"), wxT("*"),
232 wxDefaultPosition, wxDefaultSize,
233 GetPickerStyle());
234 }
235
236 long FilePickerWidgetsPage::GetPickerStyle()
237 {
238 long style = 0;
239
240 if ( m_chkFileTextCtrl->GetValue() )
241 style |= wxFLP_USE_TEXTCTRL;
242
243 if ( m_chkFileOverwritePrompt->GetValue() )
244 style |= wxFLP_OVERWRITE_PROMPT;
245
246 if ( m_chkFileMustExist->GetValue() )
247 style |= wxFLP_FILE_MUST_EXIST;
248
249 if ( m_chkFileChangeDir->GetValue() )
250 style |= wxFLP_CHANGE_DIR;
251
252 if ( m_chkSmall->GetValue() )
253 style |= wxFLP_SMALL;
254
255 if (m_radioFilePickerMode->GetSelection() == FilePickerMode_Open)
256 style |= wxFLP_OPEN;
257 else
258 style |= wxFLP_SAVE;
259
260 return style;
261 }
262
263 void FilePickerWidgetsPage::RecreatePicker()
264 {
265 m_sizer->Remove(1);
266 CreatePicker();
267 m_sizer->Insert(1, m_filePicker, 0, wxEXPAND|wxALL, 5);
268
269 m_sizer->Layout();
270 }
271
272 void FilePickerWidgetsPage::Reset()
273 {
274 m_radioFilePickerMode->SetSelection((wxFLP_DEFAULT_STYLE & wxFLP_OPEN) ?
275 FilePickerMode_Open : FilePickerMode_Save);
276 m_chkFileTextCtrl->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_USE_TEXTCTRL) != 0);
277 m_chkFileOverwritePrompt->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_OVERWRITE_PROMPT) != 0);
278 m_chkFileMustExist->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_FILE_MUST_EXIST) != 0);
279 m_chkFileChangeDir->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_CHANGE_DIR) != 0);
280 m_chkSmall->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_SMALL) != 0);
281
282 UpdateFilePickerMode();
283 }
284
285 void FilePickerWidgetsPage::UpdateFilePickerMode()
286 {
287 switch (m_radioFilePickerMode->GetSelection())
288 {
289 case FilePickerMode_Open:
290 m_chkFileOverwritePrompt->SetValue(false);
291 m_chkFileOverwritePrompt->Disable();
292 m_chkFileMustExist->Enable();
293 break;
294 case FilePickerMode_Save:
295 m_chkFileMustExist->SetValue(false);
296 m_chkFileMustExist->Disable();
297 m_chkFileOverwritePrompt->Enable();
298 break;
299 }
300 }
301
302
303 // ----------------------------------------------------------------------------
304 // event handlers
305 // ----------------------------------------------------------------------------
306
307 void FilePickerWidgetsPage::OnButtonSetDir(wxCommandEvent& WXUNUSED(event))
308 {
309 const wxString& dir = m_textInitialDir->GetValue();
310 m_filePicker->SetInitialDirectory(dir);
311 wxLogMessage("Initial directory set to \"%s\"", dir);
312 }
313
314 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
315 {
316 Reset();
317
318 RecreatePicker();
319 }
320
321 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent& event)
322 {
323 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
324 event.GetPath().c_str(), wxGetCwd().c_str());
325 }
326
327 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
328 {
329 if (event.GetEventObject() == m_chkFileTextCtrl ||
330 event.GetEventObject() == m_chkFileOverwritePrompt ||
331 event.GetEventObject() == m_chkFileMustExist ||
332 event.GetEventObject() == m_chkFileChangeDir ||
333 event.GetEventObject() == m_chkSmall)
334 RecreatePicker();
335
336 if (event.GetEventObject() == m_radioFilePickerMode)
337 {
338 UpdateFilePickerMode();
339 RecreatePicker();
340 }
341 }
342
343 void FilePickerWidgetsPage::OnUpdatePath(wxUpdateUIEvent& ev)
344 {
345 ev.SetText( "Current path: " + m_filePicker->GetPath() );
346 }
347
348 #endif // wxUSE_FILEPICKERCTRL