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