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