]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/filepicker.cpp
Reorganize and improve wxSizer::SetItemMinSize() documentation.
[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 // License: wxWindows license
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 wxRadioBox *m_radioFilePickerMode;
116
117 wxBoxSizer *m_sizer;
118
119 private:
120 DECLARE_EVENT_TABLE()
121 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage)
122 };
123
124 // ----------------------------------------------------------------------------
125 // event tables
126 // ----------------------------------------------------------------------------
127
128 BEGIN_EVENT_TABLE(FilePickerWidgetsPage, WidgetsPage)
129 EVT_BUTTON(PickerPage_Reset, FilePickerWidgetsPage::OnButtonReset)
130
131 EVT_FILEPICKER_CHANGED(PickerPage_File, FilePickerWidgetsPage::OnFileChange)
132
133 EVT_CHECKBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
134 EVT_RADIOBOX(wxID_ANY, FilePickerWidgetsPage::OnCheckBox)
135 END_EVENT_TABLE()
136
137 // ============================================================================
138 // implementation
139 // ============================================================================
140
141 #if defined(__WXGTK24__)
142 #define FAMILY_CTRLS NATIVE_CTRLS
143 #else
144 #define FAMILY_CTRLS GENERIC_CTRLS
145 #endif
146
147 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage, wxT("FilePicker"),
148 PICKER_CTRLS | FAMILY_CTRLS);
149
150 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl *book,
151 wxImageList *imaglist)
152 : WidgetsPage(book, imaglist, filepicker_xpm)
153 {
154 }
155
156 void FilePickerWidgetsPage::CreateContent()
157 {
158 // left pane
159 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
160
161 static const wxString mode[] = { wxT("open"), wxT("save") };
162 m_radioFilePickerMode = new wxRadioBox(this, wxID_ANY, wxT("wxFilePicker mode"),
163 wxDefaultPosition, wxDefaultSize,
164 WXSIZEOF(mode), mode);
165 boxleft->Add(m_radioFilePickerMode, 0, wxALL|wxGROW, 5);
166
167 wxStaticBoxSizer *filebox = new wxStaticBoxSizer(wxVERTICAL, this, wxT("&FilePicker style"));
168 m_chkFileTextCtrl = CreateCheckBoxAndAddToSizer(filebox, wxT("With textctrl"), false);
169 m_chkFileOverwritePrompt = CreateCheckBoxAndAddToSizer(filebox, wxT("Overwrite prompt"), false);
170 m_chkFileMustExist = CreateCheckBoxAndAddToSizer(filebox, wxT("File must exist"), false);
171 m_chkFileChangeDir = CreateCheckBoxAndAddToSizer(filebox, wxT("Change working dir"), false);
172 boxleft->Add(filebox, 0, wxALL|wxGROW, 5);
173
174 boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
175 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
176
177 Reset(); // set checkboxes state
178
179 // create pickers
180 m_filePicker = NULL;
181 CreatePicker();
182
183 // right pane
184 m_sizer = new wxBoxSizer(wxVERTICAL);
185 m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
186 m_sizer->Add(m_filePicker, 0, wxALIGN_CENTER|wxALL, 5);
187 m_sizer->Add(1, 1, 1, wxGROW | wxALL, 5); // spacer
188
189 // global pane
190 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
191 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
192 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
193
194 SetSizer(sz);
195 }
196
197 void FilePickerWidgetsPage::CreatePicker()
198 {
199 delete m_filePicker;
200
201 // pass an empty string as initial file
202 m_filePicker = new wxFilePickerCtrl(this, PickerPage_File,
203 wxEmptyString,
204 wxT("Hello!"), wxT("*"),
205 wxDefaultPosition, wxDefaultSize,
206 GetPickerStyle());
207 }
208
209 long FilePickerWidgetsPage::GetPickerStyle()
210 {
211 long style = 0;
212
213 if ( m_chkFileTextCtrl->GetValue() )
214 style |= wxFLP_USE_TEXTCTRL;
215
216 if ( m_chkFileOverwritePrompt->GetValue() )
217 style |= wxFLP_OVERWRITE_PROMPT;
218
219 if ( m_chkFileMustExist->GetValue() )
220 style |= wxFLP_FILE_MUST_EXIST;
221
222 if ( m_chkFileChangeDir->GetValue() )
223 style |= wxFLP_CHANGE_DIR;
224
225 if (m_radioFilePickerMode->GetSelection() == FilePickerMode_Open)
226 style |= wxFLP_OPEN;
227 else
228 style |= wxFLP_SAVE;
229
230 return style;
231 }
232
233 void FilePickerWidgetsPage::RecreatePicker()
234 {
235 m_sizer->Remove(1);
236 CreatePicker();
237 m_sizer->Insert(1, m_filePicker, 0, wxALIGN_CENTER||wxALL, 5);
238
239 m_sizer->Layout();
240 }
241
242 void FilePickerWidgetsPage::Reset()
243 {
244 m_radioFilePickerMode->SetSelection((wxFLP_DEFAULT_STYLE & wxFLP_OPEN) ?
245 FilePickerMode_Open : FilePickerMode_Save);
246 m_chkFileTextCtrl->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_USE_TEXTCTRL) != 0);
247 m_chkFileOverwritePrompt->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_OVERWRITE_PROMPT) != 0);
248 m_chkFileMustExist->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_FILE_MUST_EXIST) != 0);
249 m_chkFileChangeDir->SetValue((wxFLP_DEFAULT_STYLE & wxFLP_CHANGE_DIR) != 0);
250
251 UpdateFilePickerMode();
252 }
253
254 void FilePickerWidgetsPage::UpdateFilePickerMode()
255 {
256 switch (m_radioFilePickerMode->GetSelection())
257 {
258 case FilePickerMode_Open:
259 m_chkFileOverwritePrompt->SetValue(false);
260 m_chkFileOverwritePrompt->Disable();
261 m_chkFileMustExist->Enable();
262 break;
263 case FilePickerMode_Save:
264 m_chkFileMustExist->SetValue(false);
265 m_chkFileMustExist->Disable();
266 m_chkFileOverwritePrompt->Enable();
267 break;
268 }
269 }
270
271
272 // ----------------------------------------------------------------------------
273 // event handlers
274 // ----------------------------------------------------------------------------
275
276 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
277 {
278 Reset();
279
280 RecreatePicker();
281 }
282
283 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent& event)
284 {
285 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
286 event.GetPath().c_str(), wxGetCwd().c_str());
287 }
288
289 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
290 {
291 if (event.GetEventObject() == m_chkFileTextCtrl ||
292 event.GetEventObject() == m_chkFileOverwritePrompt ||
293 event.GetEventObject() == m_chkFileMustExist ||
294 event.GetEventObject() == m_chkFileChangeDir)
295 RecreatePicker();
296
297 if (event.GetEventObject() == m_radioFilePickerMode)
298 {
299 UpdateFilePickerMode();
300 RecreatePicker();
301 }
302 }
303
304 #endif // wxUSE_FILEPICKERCTRL