Merge in from trunk r68684 - r69046
[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 // Licence: wxWindows licence
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 *m_chkSmall;
105 wxBoxSizer *m_sizer;
106
107 private:
108 DECLARE_EVENT_TABLE()
109 DECLARE_WIDGETS_PAGE(DirPickerWidgetsPage)
110 };
111
112 // ----------------------------------------------------------------------------
113 // event tables
114 // ----------------------------------------------------------------------------
115
116 BEGIN_EVENT_TABLE(DirPickerWidgetsPage, WidgetsPage)
117 EVT_BUTTON(PickerPage_Reset, DirPickerWidgetsPage::OnButtonReset)
118
119 EVT_DIRPICKER_CHANGED(PickerPage_Dir, DirPickerWidgetsPage::OnDirChange)
120
121 EVT_CHECKBOX(wxID_ANY, DirPickerWidgetsPage::OnCheckBox)
122 END_EVENT_TABLE()
123
124 // ============================================================================
125 // implementation
126 // ============================================================================
127
128 #if defined(__WXGTK24__)
129 #define FAMILY_CTRLS NATIVE_CTRLS
130 #else
131 #define FAMILY_CTRLS GENERIC_CTRLS
132 #endif
133
134 IMPLEMENT_WIDGETS_PAGE(DirPickerWidgetsPage, wxT("DirPicker"),
135 PICKER_CTRLS | FAMILY_CTRLS);
136
137 DirPickerWidgetsPage::DirPickerWidgetsPage(WidgetsBookCtrl *book,
138 wxImageList *imaglist)
139 : WidgetsPage(book, imaglist, dirpicker_xpm)
140 {
141 }
142
143 void DirPickerWidgetsPage::CreateContent()
144 {
145 // left pane
146 wxSizer *boxleft = new wxBoxSizer(wxVERTICAL);
147
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);
154
155 boxleft->Add(new wxButton(this, PickerPage_Reset, wxT("&Reset")),
156 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
157
158 Reset(); // set checkboxes state
159
160 // create pickers
161 m_dirPicker = NULL;
162 CreatePicker();
163
164 // right pane
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
169
170 // global pane
171 wxSizer *sz = new wxBoxSizer(wxHORIZONTAL);
172 sz->Add(boxleft, 0, wxGROW|wxALL, 5);
173 sz->Add(m_sizer, 1, wxGROW|wxALL, 5);
174
175 SetSizer(sz);
176 }
177
178 void DirPickerWidgetsPage::CreatePicker()
179 {
180 delete m_dirPicker;
181
182 m_dirPicker = new wxDirPickerCtrl(this, PickerPage_Dir,
183 wxGetHomeDir(), wxT("Hello!"),
184 wxDefaultPosition, wxDefaultSize,
185 GetPickerStyle());
186 }
187
188 long DirPickerWidgetsPage::GetPickerStyle()
189 {
190 long style = 0;
191
192 if ( m_chkDirTextCtrl->GetValue() )
193 style |= wxDIRP_USE_TEXTCTRL;
194
195 if ( m_chkDirMustExist->GetValue() )
196 style |= wxDIRP_DIR_MUST_EXIST;
197
198 if ( m_chkDirChangeDir->GetValue() )
199 style |= wxDIRP_CHANGE_DIR;
200
201 if ( m_chkSmall->GetValue() )
202 style |= wxDIRP_SMALL;
203
204 return style;
205 }
206
207 void DirPickerWidgetsPage::RecreatePicker()
208 {
209 m_sizer->Remove(1);
210 CreatePicker();
211 m_sizer->Insert(1, m_dirPicker, 0, wxEXPAND|wxALL, 5);
212
213 m_sizer->Layout();
214 }
215
216 void DirPickerWidgetsPage::Reset()
217 {
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);
222 }
223
224
225 // ----------------------------------------------------------------------------
226 // event handlers
227 // ----------------------------------------------------------------------------
228
229 void DirPickerWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
230 {
231 Reset();
232 RecreatePicker();
233 }
234
235 void DirPickerWidgetsPage::OnDirChange(wxFileDirPickerEvent& event)
236 {
237 wxLogMessage(wxT("The directory changed to '%s' ! The current working directory is '%s'"),
238 event.GetPath().c_str(), wxGetCwd().c_str());
239 }
240
241 void DirPickerWidgetsPage::OnCheckBox(wxCommandEvent &event)
242 {
243 if (event.GetEventObject() == m_chkDirTextCtrl ||
244 event.GetEventObject() == m_chkDirChangeDir ||
245 event.GetEventObject() == m_chkDirMustExist ||
246 event.GetEventObject() == m_chkSmall)
247 RecreatePicker();
248 }
249
250 #endif // wxUSE_DIRPICKERCTRL