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"
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
,
108 DECLARE_EVENT_TABLE()
109 DECLARE_WIDGETS_PAGE(DirPickerWidgetsPage
)
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 BEGIN_EVENT_TABLE(DirPickerWidgetsPage
, WidgetsPage
)
117 EVT_BUTTON(PickerPage_Reset
, DirPickerWidgetsPage::OnButtonReset
)
119 EVT_DIRPICKER_CHANGED(PickerPage_Dir
, DirPickerWidgetsPage::OnDirChange
)
121 EVT_CHECKBOX(wxID_ANY
, DirPickerWidgetsPage::OnCheckBox
)
124 // ============================================================================
126 // ============================================================================
128 #if defined(__WXGTK24__)
129 #define FAMILY_CTRLS NATIVE_CTRLS
131 #define FAMILY_CTRLS GENERIC_CTRLS
134 IMPLEMENT_WIDGETS_PAGE(DirPickerWidgetsPage
, wxT("DirPicker"),
135 PICKER_CTRLS
| FAMILY_CTRLS
);
137 DirPickerWidgetsPage::DirPickerWidgetsPage(WidgetsBookCtrl
*book
,
138 wxImageList
*imaglist
)
139 : WidgetsPage(book
, imaglist
, dirpicker_xpm
)
143 void DirPickerWidgetsPage::CreateContent()
146 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
148 wxStaticBoxSizer
*dirbox
= new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&DirPicker style"));
149 m_chkDirTextCtrl
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("With textctrl"), false);
150 m_chkDirMustExist
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("Dir must exist"), false);
151 m_chkDirChangeDir
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("Change working dir"), false);
152 m_chkSmall
= CreateCheckBoxAndAddToSizer(dirbox
, "&Small version", false);
153 boxleft
->Add(dirbox
, 0, wxALL
|wxGROW
, 5);
155 boxleft
->Add(new wxButton(this, PickerPage_Reset
, wxT("&Reset")),
156 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
158 Reset(); // set checkboxes state
165 m_sizer
= new wxBoxSizer(wxVERTICAL
);
166 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
167 m_sizer
->Add(m_dirPicker
, 0, wxEXPAND
|wxALL
, 5);
168 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
171 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
172 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
173 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
178 void DirPickerWidgetsPage::CreatePicker()
182 m_dirPicker
= new wxDirPickerCtrl(this, PickerPage_Dir
,
183 wxGetHomeDir(), wxT("Hello!"),
184 wxDefaultPosition
, wxDefaultSize
,
188 long DirPickerWidgetsPage::GetPickerStyle()
192 if ( m_chkDirTextCtrl
->GetValue() )
193 style
|= wxDIRP_USE_TEXTCTRL
;
195 if ( m_chkDirMustExist
->GetValue() )
196 style
|= wxDIRP_DIR_MUST_EXIST
;
198 if ( m_chkDirChangeDir
->GetValue() )
199 style
|= wxDIRP_CHANGE_DIR
;
201 if ( m_chkSmall
->GetValue() )
202 style
|= wxDIRP_SMALL
;
207 void DirPickerWidgetsPage::RecreatePicker()
211 m_sizer
->Insert(1, m_dirPicker
, 0, wxEXPAND
|wxALL
, 5);
216 void DirPickerWidgetsPage::Reset()
218 m_chkDirTextCtrl
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_USE_TEXTCTRL
) != 0);
219 m_chkDirMustExist
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_DIR_MUST_EXIST
) != 0);
220 m_chkDirChangeDir
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_CHANGE_DIR
) != 0);
221 m_chkSmall
->SetValue((wxFLP_DEFAULT_STYLE
& wxDIRP_SMALL
) != 0);
225 // ----------------------------------------------------------------------------
227 // ----------------------------------------------------------------------------
229 void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
235 void DirPickerWidgetsPage::OnDirChange(wxFileDirPickerEvent
& event
)
237 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
238 event
.GetPath().c_str(), wxGetCwd().c_str());
241 void DirPickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
243 if (event
.GetEventObject() == m_chkDirTextCtrl
||
244 event
.GetEventObject() == m_chkDirChangeDir
||
245 event
.GetEventObject() == m_chkDirMustExist
||
246 event
.GetEventObject() == m_chkSmall
)
250 #endif // wxUSE_DIRPICKERCTRL