1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: filepicker.cpp
4 // Purpose: Part of the widgets sample showing wx*PickerCtrl
5 // Author: Francesco Montorsi
8 // Copyright: (c) 2006 Francesco Montorsi
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 #if wxUSE_FILEPICKERCTRL
29 // for all others, include the necessary headers
33 #include "wx/radiobox.h"
34 #include "wx/textctrl.h"
37 #include "wx/artprov.h"
39 #include "wx/stattext.h"
40 #include "wx/checkbox.h"
41 #include "wx/imaglist.h"
43 #include "wx/filepicker.h"
46 #include "icons/filepicker.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
54 FilePickerMode_Open
= 0,
61 PickerPage_Reset
= wxID_HIGHEST
,
64 PickerPage_CurrentPath
68 // ----------------------------------------------------------------------------
69 // FilePickerWidgetsPage
70 // ----------------------------------------------------------------------------
72 class FilePickerWidgetsPage
: public WidgetsPage
75 FilePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
76 virtual ~FilePickerWidgetsPage(){};
78 virtual wxControl
*GetWidget() const { return m_filePicker
; }
79 virtual void RecreateWidget() { RecreatePicker(); }
81 // lazy creation of the content
82 virtual void CreateContent();
86 // called only once at first construction
89 // called to recreate an existing control
90 void RecreatePicker();
92 // restore the checkboxes state to the initial values
95 // get the initial style for the picker of the given kind
96 long GetPickerStyle();
98 // update filepicker radiobox
99 void UpdateFilePickerMode();
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
);
110 wxFilePickerCtrl
*m_filePicker
;
116 wxCheckBox
*m_chkFileTextCtrl
,
117 *m_chkFileOverwritePrompt
,
121 wxRadioBox
*m_radioFilePickerMode
;
122 wxStaticText
*m_labelPath
;
123 wxTextCtrl
*m_textInitialDir
;
128 DECLARE_EVENT_TABLE()
129 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage
)
132 // ----------------------------------------------------------------------------
134 // ----------------------------------------------------------------------------
136 BEGIN_EVENT_TABLE(FilePickerWidgetsPage
, WidgetsPage
)
137 EVT_BUTTON(PickerPage_Reset
, FilePickerWidgetsPage::OnButtonReset
)
138 EVT_BUTTON(PickerPage_SetDir
, FilePickerWidgetsPage::OnButtonSetDir
)
140 EVT_FILEPICKER_CHANGED(PickerPage_File
, FilePickerWidgetsPage::OnFileChange
)
142 EVT_CHECKBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
143 EVT_RADIOBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
145 EVT_UPDATE_UI(PickerPage_CurrentPath
, FilePickerWidgetsPage::OnUpdatePath
)
148 // ============================================================================
150 // ============================================================================
152 #if defined(__WXGTK24__)
153 #define FAMILY_CTRLS NATIVE_CTRLS
155 #define FAMILY_CTRLS GENERIC_CTRLS
158 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage
, wxT("FilePicker"),
159 PICKER_CTRLS
| FAMILY_CTRLS
);
161 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl
*book
,
162 wxImageList
*imaglist
)
163 : WidgetsPage(book
, imaglist
, filepicker_xpm
)
167 void FilePickerWidgetsPage::CreateContent()
170 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
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);
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);
185 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
187 boxleft
->Add(CreateSizerWithTextAndButton
190 "&Initial directory",
193 ), wxSizerFlags().Expand().Border());
195 boxleft
->AddSpacer(10);
197 boxleft
->Add(new wxButton(this, PickerPage_Reset
, wxT("&Reset")),
198 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
200 Reset(); // set checkboxes state
202 // create the picker and the static text displaying its current value
203 m_labelPath
= new wxStaticText(this, PickerPage_CurrentPath
, "");
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();
217 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
218 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
219 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
224 void FilePickerWidgetsPage::CreatePicker()
228 // pass an empty string as initial file
229 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
231 wxT("Hello!"), wxT("*"),
232 wxDefaultPosition
, wxDefaultSize
,
236 long FilePickerWidgetsPage::GetPickerStyle()
240 if ( m_chkFileTextCtrl
->GetValue() )
241 style
|= wxFLP_USE_TEXTCTRL
;
243 if ( m_chkFileOverwritePrompt
->GetValue() )
244 style
|= wxFLP_OVERWRITE_PROMPT
;
246 if ( m_chkFileMustExist
->GetValue() )
247 style
|= wxFLP_FILE_MUST_EXIST
;
249 if ( m_chkFileChangeDir
->GetValue() )
250 style
|= wxFLP_CHANGE_DIR
;
252 if ( m_chkSmall
->GetValue() )
253 style
|= wxFLP_SMALL
;
255 if (m_radioFilePickerMode
->GetSelection() == FilePickerMode_Open
)
263 void FilePickerWidgetsPage::RecreatePicker()
267 m_sizer
->Insert(1, m_filePicker
, 0, wxEXPAND
|wxALL
, 5);
272 void FilePickerWidgetsPage::Reset()
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);
282 UpdateFilePickerMode();
285 void FilePickerWidgetsPage::UpdateFilePickerMode()
287 switch (m_radioFilePickerMode
->GetSelection())
289 case FilePickerMode_Open
:
290 m_chkFileOverwritePrompt
->SetValue(false);
291 m_chkFileOverwritePrompt
->Disable();
292 m_chkFileMustExist
->Enable();
294 case FilePickerMode_Save
:
295 m_chkFileMustExist
->SetValue(false);
296 m_chkFileMustExist
->Disable();
297 m_chkFileOverwritePrompt
->Enable();
303 // ----------------------------------------------------------------------------
305 // ----------------------------------------------------------------------------
307 void FilePickerWidgetsPage::OnButtonSetDir(wxCommandEvent
& WXUNUSED(event
))
309 const wxString
& dir
= m_textInitialDir
->GetValue();
310 m_filePicker
->SetInitialDirectory(dir
);
311 wxLogMessage("Initial directory set to \"%s\"", dir
);
314 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
321 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
323 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
324 event
.GetPath().c_str(), wxGetCwd().c_str());
327 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
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
)
336 if (event
.GetEventObject() == m_radioFilePickerMode
)
338 UpdateFilePickerMode();
343 void FilePickerWidgetsPage::OnUpdatePath(wxUpdateUIEvent
& ev
)
345 ev
.SetText( "Current path: " + m_filePicker
->GetPath() );
348 #endif // wxUSE_FILEPICKERCTRL