[ 1509599 ] 'Split pickers page in widgets sample' with more icons and rebaking.
[wxWidgets.git] / samples / widgets / dirpicker.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: dirpicker.cpp
4 // Purpose: Shows wxDirPickerCtrl
5 // Author: Francesco Montorsi
6 // Created: 20/6/2006
7 // Id: $Id$
8 // Copyright: (c) 2006 Francesco Montorsi
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DIRPICKERCTRL
28
29 // for all others, include the necessary headers
30 #ifndef WX_PRECOMP
31 #include "wx/app.h"
32 #include "wx/log.h"
33 #include "wx/radiobox.h"
34 #endif
35
36 #include "wx/artprov.h"
37 #include "wx/sizer.h"
38 #include "wx/stattext.h"
39 #include "wx/checkbox.h"
40 #include "wx/imaglist.h"
41
42 #include "wx/filepicker.h"
43 #include "widgets.h"
44
45 #include "icons/dirpicker.xpm"
46
47 // ----------------------------------------------------------------------------
48 // constants
49 // ----------------------------------------------------------------------------
50
51 // control ids
52 enum
53 {
54 PickerPage_Reset = wxID_HIGHEST,
55 PickerPage_Dir
56 };
57
58
59 // ----------------------------------------------------------------------------
60 // DirPickerWidgetsPage
61 // ----------------------------------------------------------------------------
62
63 class DirPickerWidgetsPage : public WidgetsPage
64 {
65 public:
66 DirPickerWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
67 virtual ~DirPickerWidgetsPage(){};
68
69 virtual wxControl *GetWidget() const { return m_dirPicker; }
70 virtual void RecreateWidget() { RecreatePicker(); }
71
72 // lazy creation of the content
73 virtual void CreateContent();
74
75 protected:
76
77 // called only once at first construction
78 void CreatePicker();
79
80 // called to recreate an existing control
81 void RecreatePicker();
82
83 // restore the checkboxes state to the initial values
84 void Reset();
85
86 // get the initial style for the picker of the given kind
87 long GetPickerStyle();
88
89
90 void OnDirChange(wxFileDirPickerEvent &ev);
91 void OnCheckBox(wxCommandEvent &ev);
92 void OnButtonReset(wxCommandEvent &ev);
93
94 // the picker
95 wxDirPickerCtrl *m_dirPicker;
96
97
98 // other controls
99 // --------------
100
101 wxCheckBox *m_chkDirTextCtrl,
102 *m_chkDirChangeDir,
103 *m_chkDirMustExist;
104 wxBoxSizer *m_sizer;
105
106 private:
107 DECLARE_EVENT_TABLE()
108 DECLARE_WIDGETS_PAGE(DirPickerWidgetsPage)
109 };
110
111 // ----------------------------------------------------------------------------
112 // event tables
113 // ----------------------------------------------------------------------------
114
115 BEGIN_EVENT_TABLE(DirPickerWidgetsPage, WidgetsPage)
116 EVT_BUTTON(PickerPage_Reset, DirPickerWidgetsPage::OnButtonReset)
117
118 EVT_DIRPICKER_CHANGED(PickerPage_Dir, DirPickerWidgetsPage::OnDirChange)
119
120 EVT_CHECKBOX(wxID_ANY, DirPickerWidgetsPage::OnCheckBox)
121 END_EVENT_TABLE()
122
123 // ============================================================================
124 // implementation
125 // ============================================================================
126
127 #if defined(__WXGTK24__)
128 #define FAMILY_CTRLS NATIVE_CTRLS
129 #else
130 #define FAMILY_CTRLS GENERIC_CTRLS
131 #endif
132
133 IMPLEMENT_WIDGETS_PAGE(DirPickerWidgetsPage, _T("DirPicker"),
134 PICKER_CTRLS | FAMILY_CTRLS);
135
136 DirPickerWidgetsPage::DirPickerWidgetsPage(WidgetsBookCtrl *book,
137 wxImageList *imaglist)
138 : WidgetsPage(book, imaglist, dirpicker_xpm)
139 {
140 }
141
142 void DirPickerWidgetsPage::CreateContent()
143 {
144 // left pane
145 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
146
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);
152
153 boxleft->Add(new wxButton(this, PickerPage_Reset, _T("&Reset")),
154 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
155
156 Reset(); // set checkboxes state
157
158 // create pickers
159 m_dirPicker = NULL;
160 CreatePicker();
161
162 // right pane
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
167
168 // global pane
169 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
170 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
171 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
172
173 SetSizerAndFit(sz);
174 }
175
176 void DirPickerWidgetsPage::CreatePicker()
177 {
178 delete m_dirPicker;
179
180 m_dirPicker = new wxDirPickerCtrl(this, PickerPage_Dir,
181 wxGetHomeDir(), wxT("Hello!"),
182 wxDefaultPosition, wxDefaultSize,
183 GetPickerStyle());
184 }
185
186 long DirPickerWidgetsPage::GetPickerStyle()
187 {
188 long style = 0;
189
190 if ( m_chkDirTextCtrl->GetValue() )
191 style |= wxDIRP_USE_TEXTCTRL;
192
193 if ( m_chkDirMustExist->GetValue() )
194 style |= wxDIRP_DIR_MUST_EXIST;
195
196 if ( m_chkDirChangeDir->GetValue() )
197 style |= wxDIRP_CHANGE_DIR;
198
199 return style;
200 }
201
202 void DirPickerWidgetsPage::RecreatePicker()
203 {
204 m_sizer->Remove(1);
205 CreatePicker();
206 m_sizer->Insert(1, m_dirPicker, 0, wxALIGN_CENTER|wxALL, 5);
207
208 m_sizer->Layout();
209 }
210
211 void DirPickerWidgetsPage::Reset()
212 {
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);
216 }
217
218
219 // ----------------------------------------------------------------------------
220 // event handlers
221 // ----------------------------------------------------------------------------
222
223 void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
224 {
225 Reset();
226 RecreatePicker();
227 }
228
229 void DirPickerWidgetsPage::OnDirChange(wxFileDirPickerEvent& event)
230 {
231 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
232 event.GetPath().c_str(), wxGetCwd().c_str());
233 }
234
235 void DirPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
236 {
237 if (event.GetEventObject() == m_chkDirTextCtrl ||
238 event.GetEventObject() == m_chkDirChangeDir ||
239 event.GetEventObject() == m_chkDirMustExist)
240 RecreatePicker();
241 }
242
243 #endif // wxUSE_DIRPICKERCTRL