]> git.saurik.com Git - wxWidgets.git/blob - samples/widgets/dirctrl.cpp
Support using GetTextExtent() with empty string to get descent in wxOSX.
[wxWidgets.git] / samples / widgets / dirctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
3 // Name: dirctrl.cpp
4 // Purpose: Part of the widgets sample showing wxGenericDirCtrl
5 // Author: Wlodzimierz 'ABX' Skiba
6 // Created: 4 Oct 2006
7 // Id: $Id$
8 // Copyright: (c) 2006 wxWindows team
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_DIRDLG
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/sizer.h"
34 #include "wx/statbox.h"
35 #include "wx/radiobox.h"
36 #include "wx/checkbox.h"
37 #include "wx/button.h"
38 #include "wx/filedlg.h"
39 #endif
40
41 #include "wx/generic/dirctrlg.h"
42
43 #include "wx/wupdlock.h"
44 #include "wx/stdpaths.h"
45 #include "wx/filename.h"
46
47 #include "widgets.h"
48
49 #include "icons/dirctrl.xpm"
50
51 // ----------------------------------------------------------------------------
52 // constants
53 // ----------------------------------------------------------------------------
54
55 // control ids
56 enum
57 {
58 DirCtrlPage_Reset = wxID_HIGHEST,
59 DirCtrlPage_SetPath,
60 DirCtrlPage_Ctrl
61 };
62
63 static const wxString stdPaths[] =
64 {
65 wxT("&none"),
66 wxT("&config"),
67 wxT("&data"),
68 wxT("&documents"),
69 wxT("&local data"),
70 wxT("&plugins"),
71 wxT("&resources"),
72 wxT("&user config"),
73 wxT("&user data"),
74 wxT("&user local data")
75 };
76
77 enum
78 {
79 stdPathUnknown = 0,
80 stdPathConfig,
81 stdPathData,
82 stdPathDocuments,
83 stdPathLocalData,
84 stdPathPlugins,
85 stdPathResources,
86 stdPathUserConfig,
87 stdPathUserData,
88 stdPathUserLocalData,
89 stdPathMax
90 };
91
92 // ----------------------------------------------------------------------------
93 // CheckBoxWidgetsPage
94 // ----------------------------------------------------------------------------
95
96 class DirCtrlWidgetsPage : public WidgetsPage
97 {
98 public:
99 DirCtrlWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
100 virtual ~DirCtrlWidgetsPage() {}
101
102 virtual wxControl *GetWidget() const { return m_dirCtrl; }
103 virtual void RecreateWidget() { CreateDirCtrl(); }
104
105 // lazy creation of the content
106 virtual void CreateContent();
107
108 protected:
109 // event handlers
110 void OnButtonSetPath(wxCommandEvent& event);
111 void OnButtonReset(wxCommandEvent& event);
112 void OnStdPath(wxCommandEvent& event);
113 void OnCheckBox(wxCommandEvent& event);
114 void OnRadioBox(wxCommandEvent& event);
115 void OnSelChanged(wxTreeEvent& event);
116 void OnFileActivated(wxTreeEvent& event);
117
118 // reset the control parameters
119 void Reset();
120
121 // (re)create the m_dirCtrl
122 void CreateDirCtrl();
123
124 // the controls
125 // ------------
126
127 // the control itself and the sizer it is in
128 wxGenericDirCtrl *m_dirCtrl;
129
130 // the text entries for command parameters
131 wxTextCtrl *m_path;
132
133 wxRadioBox *m_radioStdPath;
134
135 // flags
136 wxCheckBox *m_chkDirOnly,
137 *m_chk3D,
138 *m_chkFirst,
139 *m_chkFilters,
140 *m_chkLabels,
141 *m_chkMulti;
142
143 // filters
144 wxCheckBox *m_fltr[3];
145
146 private:
147 DECLARE_EVENT_TABLE()
148 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage)
149 };
150
151 // ----------------------------------------------------------------------------
152 // event tables
153 // ----------------------------------------------------------------------------
154
155 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage, WidgetsPage)
156 EVT_BUTTON(DirCtrlPage_Reset, DirCtrlWidgetsPage::OnButtonReset)
157 EVT_BUTTON(DirCtrlPage_SetPath, DirCtrlWidgetsPage::OnButtonSetPath)
158 EVT_CHECKBOX(wxID_ANY, DirCtrlWidgetsPage::OnCheckBox)
159 EVT_RADIOBOX(wxID_ANY, DirCtrlWidgetsPage::OnRadioBox)
160 EVT_DIRCTRL_SELECTIONCHANGED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnSelChanged)
161 EVT_DIRCTRL_FILEACTIVATED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnFileActivated)
162 END_EVENT_TABLE()
163
164 // ============================================================================
165 // implementation
166 // ============================================================================
167
168 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage, wxT("DirCtrl"),
169 GENERIC_CTRLS
170 );
171
172 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book,
173 wxImageList *imaglist)
174 :WidgetsPage(book, imaglist, dirctrl_xpm)
175 {
176 m_dirCtrl = NULL;
177 }
178
179 void DirCtrlWidgetsPage::CreateContent()
180 {
181 wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
182
183 // left pane
184 wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Dir control details"));
185
186 wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
187
188 sizerLeft->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath , wxT("Set &path"), wxID_ANY, &m_path ),
189 0, wxALL | wxALIGN_RIGHT , 5 );
190
191 wxSizer *sizerUseFlags =
192 new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Flags"));
193 m_chkDirOnly = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_DIR_ONLY"));
194 m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_3D_INTERNAL"));
195 m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SELECT_FIRST"));
196 m_chkFilters = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SHOW_FILTERS"));
197 m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_EDIT_LABELS"));
198 m_chkMulti = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_MULTIPLE"));
199 sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
200
201 wxSizer *sizerFilters =
202 new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Filters"));
203 m_fltr[0] = CreateCheckBoxAndAddToSizer(sizerFilters, wxString::Format(wxT("all files (%s)|%s"),
204 wxFileSelectorDefaultWildcardStr, wxFileSelectorDefaultWildcardStr));
205 m_fltr[1] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
206 m_fltr[2] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("PNG images (*.png)|*.png"));
207 sizerLeft->Add(sizerFilters, wxSizerFlags().Expand().Border());
208
209 wxButton *btn = new wxButton(this, DirCtrlPage_Reset, wxT("&Reset"));
210 sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
211
212 // keep consistency between enum and labels of radiobox
213 wxCOMPILE_TIME_ASSERT( stdPathMax == WXSIZEOF(stdPaths), EnumForRadioBoxMismatch);
214
215 // middle pane
216 m_radioStdPath = new wxRadioBox(this, wxID_ANY, wxT("Standard path"),
217 wxDefaultPosition, wxDefaultSize,
218 WXSIZEOF(stdPaths), stdPaths, 1);
219
220 // right pane
221 m_dirCtrl = new wxGenericDirCtrl(
222 this,
223 DirCtrlPage_Ctrl,
224 wxDirDialogDefaultFolderStr,
225 wxDefaultPosition,
226 wxDefaultSize,
227 0
228 );
229
230 // the 3 panes panes compose the window
231 sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
232 sizerTop->Add(m_radioStdPath, 0, wxGROW | wxALL , 10);
233 sizerTop->Add(m_dirCtrl, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
234
235 // final initializations
236 Reset();
237
238 SetSizer(sizerTop);
239 }
240
241 void DirCtrlWidgetsPage::Reset()
242 {
243 m_path->SetValue(m_dirCtrl->GetPath());
244 }
245
246 void DirCtrlWidgetsPage::CreateDirCtrl()
247 {
248 wxWindowUpdateLocker noUpdates(this);
249
250 wxGenericDirCtrl *dirCtrl = new wxGenericDirCtrl(
251 this,
252 DirCtrlPage_Ctrl,
253 wxDirDialogDefaultFolderStr,
254 wxDefaultPosition,
255 wxDefaultSize,
256 ( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
257 ( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
258 ( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
259 ( m_chkFilters->IsChecked() ? wxDIRCTRL_SHOW_FILTERS : 0 ) |
260 ( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) |
261 ( m_chkMulti->IsChecked() ? wxDIRCTRL_MULTIPLE : 0)
262 );
263
264 wxString filter;
265 for (int i = 0; i < 3; ++i)
266 {
267 if (m_fltr[i]->IsChecked())
268 {
269 if (!filter.IsEmpty())
270 filter += wxT("|");
271 filter += m_fltr[i]->GetLabel();
272 }
273 }
274 dirCtrl->SetFilter(filter);
275
276 // update sizer's child window
277 GetSizer()->Replace(m_dirCtrl, dirCtrl, true);
278
279 // update our pointer
280 delete m_dirCtrl;
281 m_dirCtrl = dirCtrl;
282
283 // relayout the sizer
284 GetSizer()->Layout();
285 }
286
287 // ----------------------------------------------------------------------------
288 // event handlers
289 // ----------------------------------------------------------------------------
290
291 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent& WXUNUSED(event))
292 {
293 m_dirCtrl->SetPath(m_path->GetValue());
294 }
295
296 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
297 {
298 Reset();
299
300 CreateDirCtrl();
301 }
302
303 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
304 {
305 CreateDirCtrl();
306 }
307
308 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event))
309 {
310 wxString path;
311
312 wxTheApp->SetAppName(wxT("widgets"));
313 wxStandardPathsBase& stdp = wxStandardPaths::Get();
314
315 switch ( m_radioStdPath->GetSelection() )
316 {
317 default:
318 case stdPathUnknown:
319 case stdPathMax:
320 // leave path
321 break;
322
323 case stdPathConfig:
324 path = stdp.GetConfigDir();
325 break;
326
327 case stdPathData:
328 path = stdp.GetDataDir();
329 break;
330
331 case stdPathDocuments:
332 path = stdp.GetDocumentsDir();
333 break;
334
335 case stdPathLocalData:
336 path = stdp.GetLocalDataDir();
337 break;
338
339 case stdPathPlugins:
340 path = stdp.GetPluginsDir();
341 break;
342
343 case stdPathResources:
344 path = stdp.GetResourcesDir();
345 break;
346
347 case stdPathUserConfig:
348 path = stdp.GetUserConfigDir();
349 break;
350
351 case stdPathUserData:
352 path = stdp.GetUserDataDir();
353 break;
354
355 case stdPathUserLocalData:
356 path = stdp.GetUserLocalDataDir();
357 break;
358 }
359
360 m_dirCtrl->SetPath(path);
361
362 // Notice that we must use wxFileName comparison instead of simple wxString
363 // comparison as the paths returned may differ by case only.
364 if ( wxFileName(m_dirCtrl->GetPath()) != path )
365 {
366 wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
367 path, m_dirCtrl->GetPath());
368 }
369 }
370
371 void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent& event)
372 {
373 if ( m_dirCtrl )
374 {
375 wxLogMessage("Selection changed to \"%s\"",
376 m_dirCtrl->GetPath(event.GetItem()));
377 }
378
379 event.Skip();
380 }
381
382 void DirCtrlWidgetsPage::OnFileActivated(wxTreeEvent& event)
383 {
384 if ( m_dirCtrl )
385 {
386 wxLogMessage("File activated \"%s\"",
387 m_dirCtrl->GetPath(event.GetItem()));
388 }
389
390 event.Skip();
391 }
392
393 #endif // wxUSE_DIRDLG