1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: filepicker.cpp
4 // Purpose: Part of the widgets sample showing wx*PickerCtrl
5 // Author: Francesco Montorsi
7 // Copyright: (c) 2006 Francesco Montorsi
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 #if wxUSE_FILEPICKERCTRL
28 // for all others, include the necessary headers
32 #include "wx/radiobox.h"
33 #include "wx/textctrl.h"
36 #include "wx/artprov.h"
38 #include "wx/stattext.h"
39 #include "wx/checkbox.h"
40 #include "wx/imaglist.h"
42 #include "wx/filepicker.h"
45 #include "icons/filepicker.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
53 FilePickerMode_Open
= 0,
60 PickerPage_Reset
= wxID_HIGHEST
,
63 PickerPage_CurrentPath
67 // ----------------------------------------------------------------------------
68 // FilePickerWidgetsPage
69 // ----------------------------------------------------------------------------
71 class FilePickerWidgetsPage
: public WidgetsPage
74 FilePickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
75 virtual ~FilePickerWidgetsPage(){};
77 virtual wxControl
*GetWidget() const { return m_filePicker
; }
78 virtual void RecreateWidget() { RecreatePicker(); }
80 // lazy creation of the content
81 virtual void CreateContent();
85 // called only once at first construction
88 // called to recreate an existing control
89 void RecreatePicker();
91 // restore the checkboxes state to the initial values
94 // get the initial style for the picker of the given kind
95 long GetPickerStyle();
97 // update filepicker radiobox
98 void UpdateFilePickerMode();
100 // the pickers and the relative event handlers
101 void OnFileChange(wxFileDirPickerEvent
&ev
);
102 void OnCheckBox(wxCommandEvent
&ev
);
103 void OnButtonReset(wxCommandEvent
&ev
);
104 void OnButtonSetDir(wxCommandEvent
&ev
);
105 void OnUpdatePath(wxUpdateUIEvent
&ev
);
109 wxFilePickerCtrl
*m_filePicker
;
115 wxCheckBox
*m_chkFileTextCtrl
,
116 *m_chkFileOverwritePrompt
,
120 wxRadioBox
*m_radioFilePickerMode
;
121 wxStaticText
*m_labelPath
;
122 wxTextCtrl
*m_textInitialDir
;
127 DECLARE_EVENT_TABLE()
128 DECLARE_WIDGETS_PAGE(FilePickerWidgetsPage
)
131 // ----------------------------------------------------------------------------
133 // ----------------------------------------------------------------------------
135 BEGIN_EVENT_TABLE(FilePickerWidgetsPage
, WidgetsPage
)
136 EVT_BUTTON(PickerPage_Reset
, FilePickerWidgetsPage::OnButtonReset
)
137 EVT_BUTTON(PickerPage_SetDir
, FilePickerWidgetsPage::OnButtonSetDir
)
139 EVT_FILEPICKER_CHANGED(PickerPage_File
, FilePickerWidgetsPage::OnFileChange
)
141 EVT_CHECKBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
142 EVT_RADIOBOX(wxID_ANY
, FilePickerWidgetsPage::OnCheckBox
)
144 EVT_UPDATE_UI(PickerPage_CurrentPath
, FilePickerWidgetsPage::OnUpdatePath
)
147 // ============================================================================
149 // ============================================================================
151 #if defined(__WXGTK24__)
152 #define FAMILY_CTRLS NATIVE_CTRLS
154 #define FAMILY_CTRLS GENERIC_CTRLS
157 IMPLEMENT_WIDGETS_PAGE(FilePickerWidgetsPage
, wxT("FilePicker"),
158 PICKER_CTRLS
| FAMILY_CTRLS
);
160 FilePickerWidgetsPage::FilePickerWidgetsPage(WidgetsBookCtrl
*book
,
161 wxImageList
*imaglist
)
162 : WidgetsPage(book
, imaglist
, filepicker_xpm
)
166 void FilePickerWidgetsPage::CreateContent()
169 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
171 static const wxString mode
[] = { wxT("open"), wxT("save") };
172 m_radioFilePickerMode
= new wxRadioBox(this, wxID_ANY
, wxT("wxFilePicker mode"),
173 wxDefaultPosition
, wxDefaultSize
,
174 WXSIZEOF(mode
), mode
);
175 boxleft
->Add(m_radioFilePickerMode
, 0, wxALL
|wxGROW
, 5);
177 wxStaticBoxSizer
*filebox
= new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&FilePicker style"));
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");
184 boxleft
->Add(filebox
, 0, wxALL
|wxGROW
, 5);
186 boxleft
->Add(CreateSizerWithTextAndButton
189 "&Initial directory",
192 ), wxSizerFlags().Expand().Border());
194 boxleft
->AddSpacer(10);
196 boxleft
->Add(new wxButton(this, PickerPage_Reset
, wxT("&Reset")),
197 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
199 Reset(); // set checkboxes state
201 // create the picker and the static text displaying its current value
202 m_labelPath
= new wxStaticText(this, PickerPage_CurrentPath
, "");
208 m_sizer
= new wxBoxSizer(wxVERTICAL
);
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();
216 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
217 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
218 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
223 void FilePickerWidgetsPage::CreatePicker()
227 // pass an empty string as initial file
228 m_filePicker
= new wxFilePickerCtrl(this, PickerPage_File
,
230 wxT("Hello!"), wxT("*"),
231 wxDefaultPosition
, wxDefaultSize
,
235 long FilePickerWidgetsPage::GetPickerStyle()
239 if ( m_chkFileTextCtrl
->GetValue() )
240 style
|= wxFLP_USE_TEXTCTRL
;
242 if ( m_chkFileOverwritePrompt
->GetValue() )
243 style
|= wxFLP_OVERWRITE_PROMPT
;
245 if ( m_chkFileMustExist
->GetValue() )
246 style
|= wxFLP_FILE_MUST_EXIST
;
248 if ( m_chkFileChangeDir
->GetValue() )
249 style
|= wxFLP_CHANGE_DIR
;
251 if ( m_chkSmall
->GetValue() )
252 style
|= wxFLP_SMALL
;
254 if (m_radioFilePickerMode
->GetSelection() == FilePickerMode_Open
)
262 void FilePickerWidgetsPage::RecreatePicker()
266 m_sizer
->Insert(1, m_filePicker
, 0, wxEXPAND
|wxALL
, 5);
271 void FilePickerWidgetsPage::Reset()
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);
279 m_chkSmall
->SetValue((wxFLP_DEFAULT_STYLE
& wxFLP_SMALL
) != 0);
281 UpdateFilePickerMode();
284 void FilePickerWidgetsPage::UpdateFilePickerMode()
286 switch (m_radioFilePickerMode
->GetSelection())
288 case FilePickerMode_Open
:
289 m_chkFileOverwritePrompt
->SetValue(false);
290 m_chkFileOverwritePrompt
->Disable();
291 m_chkFileMustExist
->Enable();
293 case FilePickerMode_Save
:
294 m_chkFileMustExist
->SetValue(false);
295 m_chkFileMustExist
->Disable();
296 m_chkFileOverwritePrompt
->Enable();
302 // ----------------------------------------------------------------------------
304 // ----------------------------------------------------------------------------
306 void FilePickerWidgetsPage::OnButtonSetDir(wxCommandEvent
& WXUNUSED(event
))
308 const wxString
& dir
= m_textInitialDir
->GetValue();
309 m_filePicker
->SetInitialDirectory(dir
);
310 wxLogMessage("Initial directory set to \"%s\"", dir
);
313 void FilePickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
320 void FilePickerWidgetsPage::OnFileChange(wxFileDirPickerEvent
& event
)
322 wxLogMessage(wxT("The file changed to '%s' ! The current working directory is '%s'"),
323 event
.GetPath().c_str(), wxGetCwd().c_str());
326 void FilePickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
328 if (event
.GetEventObject() == m_chkFileTextCtrl
||
329 event
.GetEventObject() == m_chkFileOverwritePrompt
||
330 event
.GetEventObject() == m_chkFileMustExist
||
331 event
.GetEventObject() == m_chkFileChangeDir
||
332 event
.GetEventObject() == m_chkSmall
)
335 if (event
.GetEventObject() == m_radioFilePickerMode
)
337 UpdateFilePickerMode();
342 void FilePickerWidgetsPage::OnUpdatePath(wxUpdateUIEvent
& ev
)
344 ev
.SetText( "Current path: " + m_filePicker
->GetPath() );
347 #endif // wxUSE_FILEPICKERCTRL