1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Shows wxDirPickerCtrl
5 // Author: Francesco Montorsi
8 // Copyright: (c) 2006 Francesco Montorsi
9 // License: wxWindows license
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"
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/dirpicker.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 PickerPage_Reset
= wxID_HIGHEST
,
59 // ----------------------------------------------------------------------------
60 // DirPickerWidgetsPage
61 // ----------------------------------------------------------------------------
63 class DirPickerWidgetsPage
: public WidgetsPage
66 DirPickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
67 virtual ~DirPickerWidgetsPage(){};
69 virtual wxControl
*GetWidget() const { return m_dirPicker
; }
70 virtual void RecreateWidget() { RecreatePicker(); }
72 // lazy creation of the content
73 virtual void CreateContent();
77 // called only once at first construction
80 // called to recreate an existing control
81 void RecreatePicker();
83 // restore the checkboxes state to the initial values
86 // get the initial style for the picker of the given kind
87 long GetPickerStyle();
90 void OnDirChange(wxFileDirPickerEvent
&ev
);
91 void OnCheckBox(wxCommandEvent
&ev
);
92 void OnButtonReset(wxCommandEvent
&ev
);
95 wxDirPickerCtrl
*m_dirPicker
;
101 wxCheckBox
*m_chkDirTextCtrl
,
107 DECLARE_EVENT_TABLE()
108 DECLARE_WIDGETS_PAGE(DirPickerWidgetsPage
)
111 // ----------------------------------------------------------------------------
113 // ----------------------------------------------------------------------------
115 BEGIN_EVENT_TABLE(DirPickerWidgetsPage
, WidgetsPage
)
116 EVT_BUTTON(PickerPage_Reset
, DirPickerWidgetsPage::OnButtonReset
)
118 EVT_DIRPICKER_CHANGED(PickerPage_Dir
, DirPickerWidgetsPage::OnDirChange
)
120 EVT_CHECKBOX(wxID_ANY
, DirPickerWidgetsPage::OnCheckBox
)
123 // ============================================================================
125 // ============================================================================
127 #if defined(__WXGTK24__)
128 #define FAMILY_CTRLS NATIVE_CTRLS
130 #define FAMILY_CTRLS GENERIC_CTRLS
133 IMPLEMENT_WIDGETS_PAGE(DirPickerWidgetsPage
, _T("DirPicker"),
134 PICKER_CTRLS
| FAMILY_CTRLS
);
136 DirPickerWidgetsPage::DirPickerWidgetsPage(WidgetsBookCtrl
*book
,
137 wxImageList
*imaglist
)
138 : WidgetsPage(book
, imaglist
, dirpicker_xpm
)
142 void DirPickerWidgetsPage::CreateContent()
145 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
147 wxStaticBoxSizer
*dirbox
= new wxStaticBoxSizer(wxVERTICAL
, this, _T("&DirPicker style"));
148 m_chkDirTextCtrl
= CreateCheckBoxAndAddToSizer(dirbox
, _T("With textctrl"), false);
149 m_chkDirMustExist
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Dir must exist"), false);
150 m_chkDirChangeDir
= CreateCheckBoxAndAddToSizer(dirbox
, _T("Change working dir"), false);
151 boxleft
->Add(dirbox
, 0, wxALL
|wxGROW
, 5);
153 boxleft
->Add(new wxButton(this, PickerPage_Reset
, _T("&Reset")),
154 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
156 Reset(); // set checkboxes state
163 m_sizer
= new wxBoxSizer(wxVERTICAL
);
164 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
165 m_sizer
->Add(m_dirPicker
, 0, wxALIGN_CENTER
|wxALL
, 5);
166 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
169 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
170 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
171 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
176 void DirPickerWidgetsPage::CreatePicker()
180 m_dirPicker
= new wxDirPickerCtrl(this, PickerPage_Dir
,
181 wxGetHomeDir(), wxT("Hello!"),
182 wxDefaultPosition
, wxDefaultSize
,
186 long DirPickerWidgetsPage::GetPickerStyle()
190 if ( m_chkDirTextCtrl
->GetValue() )
191 style
|= wxDIRP_USE_TEXTCTRL
;
193 if ( m_chkDirMustExist
->GetValue() )
194 style
|= wxDIRP_DIR_MUST_EXIST
;
196 if ( m_chkDirChangeDir
->GetValue() )
197 style
|= wxDIRP_CHANGE_DIR
;
202 void DirPickerWidgetsPage::RecreatePicker()
206 m_sizer
->Insert(1, m_dirPicker
, 0, wxALIGN_CENTER
|wxALL
, 5);
211 void DirPickerWidgetsPage::Reset()
213 m_chkDirTextCtrl
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_USE_TEXTCTRL
) != 0);
214 m_chkDirMustExist
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_DIR_MUST_EXIST
) != 0);
215 m_chkDirChangeDir
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_CHANGE_DIR
) != 0);
219 // ----------------------------------------------------------------------------
221 // ----------------------------------------------------------------------------
223 void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
229 void DirPickerWidgetsPage::OnDirChange(wxFileDirPickerEvent
& event
)
231 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
232 event
.GetPath().c_str(), wxGetCwd().c_str());
235 void DirPickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
237 if (event
.GetEventObject() == m_chkDirTextCtrl
||
238 event
.GetEventObject() == m_chkDirChangeDir
||
239 event
.GetEventObject() == m_chkDirMustExist
)
243 #endif // wxUSE_DIRPICKERCTRL