1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Shows wxDirPickerCtrl
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_DIRPICKERCTRL
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/dirpicker.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 PickerPage_Reset
= wxID_HIGHEST
,
61 // ----------------------------------------------------------------------------
62 // DirPickerWidgetsPage
63 // ----------------------------------------------------------------------------
65 class DirPickerWidgetsPage
: public WidgetsPage
68 DirPickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
69 virtual ~DirPickerWidgetsPage(){};
71 virtual wxControl
*GetWidget() const { return m_dirPicker
; }
72 virtual void RecreateWidget() { RecreatePicker(); }
74 // lazy creation of the content
75 virtual void CreateContent();
79 // called only once at first construction
82 // called to recreate an existing control
83 void RecreatePicker();
85 // restore the checkboxes state to the initial values
88 // get the initial style for the picker of the given kind
89 long GetPickerStyle();
92 void OnDirChange(wxFileDirPickerEvent
&ev
);
93 void OnCheckBox(wxCommandEvent
&ev
);
94 void OnButtonReset(wxCommandEvent
&ev
);
95 void OnButtonSetDir(wxCommandEvent
&ev
);
99 wxDirPickerCtrl
*m_dirPicker
;
105 wxCheckBox
*m_chkDirTextCtrl
,
109 wxTextCtrl
*m_textInitialDir
;
114 DECLARE_EVENT_TABLE()
115 DECLARE_WIDGETS_PAGE(DirPickerWidgetsPage
)
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 BEGIN_EVENT_TABLE(DirPickerWidgetsPage
, WidgetsPage
)
123 EVT_BUTTON(PickerPage_Reset
, DirPickerWidgetsPage::OnButtonReset
)
124 EVT_BUTTON(PickerPage_SetDir
, DirPickerWidgetsPage::OnButtonSetDir
)
126 EVT_DIRPICKER_CHANGED(PickerPage_Dir
, DirPickerWidgetsPage::OnDirChange
)
128 EVT_CHECKBOX(wxID_ANY
, DirPickerWidgetsPage::OnCheckBox
)
131 // ============================================================================
133 // ============================================================================
135 #if defined(__WXGTK24__)
136 #define FAMILY_CTRLS NATIVE_CTRLS
138 #define FAMILY_CTRLS GENERIC_CTRLS
141 IMPLEMENT_WIDGETS_PAGE(DirPickerWidgetsPage
, wxT("DirPicker"),
142 PICKER_CTRLS
| FAMILY_CTRLS
);
144 DirPickerWidgetsPage::DirPickerWidgetsPage(WidgetsBookCtrl
*book
,
145 wxImageList
*imaglist
)
146 : WidgetsPage(book
, imaglist
, dirpicker_xpm
)
150 void DirPickerWidgetsPage::CreateContent()
153 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
155 wxStaticBoxSizer
*dirbox
= new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&DirPicker style"));
156 m_chkDirTextCtrl
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("With textctrl"), false);
157 m_chkDirMustExist
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("Dir must exist"), false);
158 m_chkDirChangeDir
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("Change working dir"), false);
159 m_chkSmall
= CreateCheckBoxAndAddToSizer(dirbox
, "&Small version", false);
160 boxleft
->Add(dirbox
, 0, wxALL
|wxGROW
, 5);
162 boxleft
->Add(CreateSizerWithTextAndButton
165 "&Initial directory",
168 ), wxSizerFlags().Expand().Border());
170 boxleft
->AddSpacer(10);
172 boxleft
->Add(new wxButton(this, PickerPage_Reset
, wxT("&Reset")),
173 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
175 Reset(); // set checkboxes state
182 m_sizer
= new wxBoxSizer(wxVERTICAL
);
183 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
184 m_sizer
->Add(m_dirPicker
, 0, wxEXPAND
|wxALL
, 5);
185 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
188 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
189 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
190 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
195 void DirPickerWidgetsPage::CreatePicker()
199 m_dirPicker
= new wxDirPickerCtrl(this, PickerPage_Dir
,
200 wxGetHomeDir(), wxT("Hello!"),
201 wxDefaultPosition
, wxDefaultSize
,
205 long DirPickerWidgetsPage::GetPickerStyle()
209 if ( m_chkDirTextCtrl
->GetValue() )
210 style
|= wxDIRP_USE_TEXTCTRL
;
212 if ( m_chkDirMustExist
->GetValue() )
213 style
|= wxDIRP_DIR_MUST_EXIST
;
215 if ( m_chkDirChangeDir
->GetValue() )
216 style
|= wxDIRP_CHANGE_DIR
;
218 if ( m_chkSmall
->GetValue() )
219 style
|= wxDIRP_SMALL
;
224 void DirPickerWidgetsPage::RecreatePicker()
228 m_sizer
->Insert(1, m_dirPicker
, 0, wxEXPAND
|wxALL
, 5);
233 void DirPickerWidgetsPage::Reset()
235 m_chkDirTextCtrl
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_USE_TEXTCTRL
) != 0);
236 m_chkDirMustExist
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_DIR_MUST_EXIST
) != 0);
237 m_chkDirChangeDir
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_CHANGE_DIR
) != 0);
238 m_chkSmall
->SetValue((wxFLP_DEFAULT_STYLE
& wxDIRP_SMALL
) != 0);
242 // ----------------------------------------------------------------------------
244 // ----------------------------------------------------------------------------
246 void DirPickerWidgetsPage::OnButtonSetDir(wxCommandEvent
& WXUNUSED(event
))
248 m_dirPicker
->SetInitialDirectory(m_textInitialDir
->GetValue());
251 void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
257 void DirPickerWidgetsPage::OnDirChange(wxFileDirPickerEvent
& event
)
259 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
260 event
.GetPath().c_str(), wxGetCwd().c_str());
263 void DirPickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
265 if (event
.GetEventObject() == m_chkDirTextCtrl
||
266 event
.GetEventObject() == m_chkDirChangeDir
||
267 event
.GetEventObject() == m_chkDirMustExist
||
268 event
.GetEventObject() == m_chkSmall
)
272 #endif // wxUSE_DIRPICKERCTRL