1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Shows wxDirPickerCtrl
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_DIRPICKERCTRL
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/dirpicker.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
54 PickerPage_Reset
= wxID_HIGHEST
,
60 // ----------------------------------------------------------------------------
61 // DirPickerWidgetsPage
62 // ----------------------------------------------------------------------------
64 class DirPickerWidgetsPage
: public WidgetsPage
67 DirPickerWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
68 virtual ~DirPickerWidgetsPage(){};
70 virtual wxControl
*GetWidget() const { return m_dirPicker
; }
71 virtual void RecreateWidget() { RecreatePicker(); }
73 // lazy creation of the content
74 virtual void CreateContent();
78 // called only once at first construction
81 // called to recreate an existing control
82 void RecreatePicker();
84 // restore the checkboxes state to the initial values
87 // get the initial style for the picker of the given kind
88 long GetPickerStyle();
91 void OnDirChange(wxFileDirPickerEvent
&ev
);
92 void OnCheckBox(wxCommandEvent
&ev
);
93 void OnButtonReset(wxCommandEvent
&ev
);
94 void OnButtonSetDir(wxCommandEvent
&ev
);
98 wxDirPickerCtrl
*m_dirPicker
;
104 wxCheckBox
*m_chkDirTextCtrl
,
108 wxTextCtrl
*m_textInitialDir
;
113 DECLARE_EVENT_TABLE()
114 DECLARE_WIDGETS_PAGE(DirPickerWidgetsPage
)
117 // ----------------------------------------------------------------------------
119 // ----------------------------------------------------------------------------
121 BEGIN_EVENT_TABLE(DirPickerWidgetsPage
, WidgetsPage
)
122 EVT_BUTTON(PickerPage_Reset
, DirPickerWidgetsPage::OnButtonReset
)
123 EVT_BUTTON(PickerPage_SetDir
, DirPickerWidgetsPage::OnButtonSetDir
)
125 EVT_DIRPICKER_CHANGED(PickerPage_Dir
, DirPickerWidgetsPage::OnDirChange
)
127 EVT_CHECKBOX(wxID_ANY
, DirPickerWidgetsPage::OnCheckBox
)
130 // ============================================================================
132 // ============================================================================
134 #if defined(__WXGTK24__)
135 #define FAMILY_CTRLS NATIVE_CTRLS
137 #define FAMILY_CTRLS GENERIC_CTRLS
140 IMPLEMENT_WIDGETS_PAGE(DirPickerWidgetsPage
, wxT("DirPicker"),
141 PICKER_CTRLS
| FAMILY_CTRLS
);
143 DirPickerWidgetsPage::DirPickerWidgetsPage(WidgetsBookCtrl
*book
,
144 wxImageList
*imaglist
)
145 : WidgetsPage(book
, imaglist
, dirpicker_xpm
)
149 void DirPickerWidgetsPage::CreateContent()
152 wxSizer
*boxleft
= new wxBoxSizer(wxVERTICAL
);
154 wxStaticBoxSizer
*dirbox
= new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&DirPicker style"));
155 m_chkDirTextCtrl
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("With textctrl"));
156 m_chkDirMustExist
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("Dir must exist"));
157 m_chkDirChangeDir
= CreateCheckBoxAndAddToSizer(dirbox
, wxT("Change working dir"));
158 m_chkSmall
= CreateCheckBoxAndAddToSizer(dirbox
, "&Small version");
159 boxleft
->Add(dirbox
, 0, wxALL
|wxGROW
, 5);
161 boxleft
->Add(CreateSizerWithTextAndButton
164 "&Initial directory",
167 ), wxSizerFlags().Expand().Border());
169 boxleft
->AddSpacer(10);
171 boxleft
->Add(new wxButton(this, PickerPage_Reset
, wxT("&Reset")),
172 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
174 Reset(); // set checkboxes state
181 m_sizer
= new wxBoxSizer(wxVERTICAL
);
182 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
183 m_sizer
->Add(m_dirPicker
, 0, wxEXPAND
|wxALL
, 5);
184 m_sizer
->Add(1, 1, 1, wxGROW
| wxALL
, 5); // spacer
187 wxSizer
*sz
= new wxBoxSizer(wxHORIZONTAL
);
188 sz
->Add(boxleft
, 0, wxGROW
|wxALL
, 5);
189 sz
->Add(m_sizer
, 1, wxGROW
|wxALL
, 5);
194 void DirPickerWidgetsPage::CreatePicker()
198 m_dirPicker
= new wxDirPickerCtrl(this, PickerPage_Dir
,
199 wxGetHomeDir(), wxT("Hello!"),
200 wxDefaultPosition
, wxDefaultSize
,
204 long DirPickerWidgetsPage::GetPickerStyle()
208 if ( m_chkDirTextCtrl
->GetValue() )
209 style
|= wxDIRP_USE_TEXTCTRL
;
211 if ( m_chkDirMustExist
->GetValue() )
212 style
|= wxDIRP_DIR_MUST_EXIST
;
214 if ( m_chkDirChangeDir
->GetValue() )
215 style
|= wxDIRP_CHANGE_DIR
;
217 if ( m_chkSmall
->GetValue() )
218 style
|= wxDIRP_SMALL
;
223 void DirPickerWidgetsPage::RecreatePicker()
227 m_sizer
->Insert(1, m_dirPicker
, 0, wxEXPAND
|wxALL
, 5);
232 void DirPickerWidgetsPage::Reset()
234 m_chkDirTextCtrl
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_USE_TEXTCTRL
) != 0);
235 m_chkDirMustExist
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_DIR_MUST_EXIST
) != 0);
236 m_chkDirChangeDir
->SetValue((wxDIRP_DEFAULT_STYLE
& wxDIRP_CHANGE_DIR
) != 0);
237 m_chkSmall
->SetValue((wxFLP_DEFAULT_STYLE
& wxDIRP_SMALL
) != 0);
241 // ----------------------------------------------------------------------------
243 // ----------------------------------------------------------------------------
245 void DirPickerWidgetsPage::OnButtonSetDir(wxCommandEvent
& WXUNUSED(event
))
247 m_dirPicker
->SetInitialDirectory(m_textInitialDir
->GetValue());
250 void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
256 void DirPickerWidgetsPage::OnDirChange(wxFileDirPickerEvent
& event
)
258 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
259 event
.GetPath().c_str(), wxGetCwd().c_str());
262 void DirPickerWidgetsPage::OnCheckBox(wxCommandEvent
&event
)
264 if (event
.GetEventObject() == m_chkDirTextCtrl
||
265 event
.GetEventObject() == m_chkDirChangeDir
||
266 event
.GetEventObject() == m_chkDirMustExist
||
267 event
.GetEventObject() == m_chkSmall
)
271 #endif // wxUSE_DIRPICKERCTRL