1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxGenericDirCtrl
5 // Author: Wlodzimierz 'ABX' Skiba
8 // Copyright: (c) 2006 wxWindows team
9 // License: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // for compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
29 // for all others, include the necessary headers
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"
41 #include "wx/generic/dirctrlg.h"
43 #include "wx/wupdlock.h"
44 #include "wx/stdpaths.h"
48 #include "icons/dirctrl.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 DirCtrlPage_Reset
= wxID_HIGHEST
,
62 static const wxString stdPaths
[] =
73 _T("&user local data")
91 // ----------------------------------------------------------------------------
92 // CheckBoxWidgetsPage
93 // ----------------------------------------------------------------------------
95 class DirCtrlWidgetsPage
: public WidgetsPage
98 DirCtrlWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
99 virtual ~DirCtrlWidgetsPage() {}
101 virtual wxControl
*GetWidget() const { return m_dirCtrl
; }
102 virtual void RecreateWidget() { CreateDirCtrl(); }
104 // lazy creation of the content
105 virtual void CreateContent();
109 void OnButtonSetPath(wxCommandEvent
& event
);
110 void OnButtonReset(wxCommandEvent
& event
);
111 void OnStdPath(wxCommandEvent
& event
);
112 void OnCheckBox(wxCommandEvent
& event
);
113 void OnRadioBox(wxCommandEvent
& event
);
115 // reset the control parameters
118 // (re)create the m_dirCtrl
119 void CreateDirCtrl();
124 // the control itself and the sizer it is in
125 wxGenericDirCtrl
*m_dirCtrl
;
127 // the text entries for command parameters
130 wxRadioBox
*m_radioStdPath
;
133 wxCheckBox
*m_chkDirOnly
,
138 wxCheckBox
*m_fltr
[3];
141 DECLARE_EVENT_TABLE()
142 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage
)
145 // ----------------------------------------------------------------------------
147 // ----------------------------------------------------------------------------
149 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage
, WidgetsPage
)
150 EVT_BUTTON(DirCtrlPage_Reset
, DirCtrlWidgetsPage::OnButtonReset
)
151 EVT_BUTTON(DirCtrlPage_SetPath
, DirCtrlWidgetsPage::OnButtonSetPath
)
152 EVT_CHECKBOX(wxID_ANY
, DirCtrlWidgetsPage::OnCheckBox
)
153 EVT_RADIOBOX(wxID_ANY
, DirCtrlWidgetsPage::OnRadioBox
)
156 // ============================================================================
158 // ============================================================================
160 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage
, wxT("DirCtrl"),
164 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl
*book
,
165 wxImageList
*imaglist
)
166 :WidgetsPage(book
, imaglist
, dirctrl_xpm
)
170 void DirCtrlWidgetsPage::CreateContent()
172 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
175 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Dir control details"));
177 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
179 sizerLeft
->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath
, wxT("Set &path"), wxID_ANY
, &m_path
),
180 0, wxALL
| wxALIGN_RIGHT
, 5 );
182 wxSizer
*sizerUseFlags
=
183 new wxStaticBoxSizer(wxVERTICAL
, this, _T("&Flags"));
184 m_chkDirOnly
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, _T("wxDIRCTRL_DIR_ONLY"));
185 m_chk3D
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, _T("wxDIRCTRL_3D_INTERNAL"));
186 m_chkFirst
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, _T("wxDIRCTRL_SELECT_FIRST"));
187 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, _T("wxDIRCTRL_EDIT_LABELS"));
188 sizerLeft
->Add(sizerUseFlags
, wxSizerFlags().Expand().Border());
190 wxSizer
*sizerFilters
=
191 new wxStaticBoxSizer(wxVERTICAL
, this, _T("&Filters"));
192 m_fltr
[0] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxString::Format(wxT("all files (%s)|%s"),
193 wxFileSelectorDefaultWildcardStr
, wxFileSelectorDefaultWildcardStr
));
194 m_fltr
[1] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
195 m_fltr
[2] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxT("PNG images (*.png)|*.png"));
196 sizerLeft
->Add(sizerFilters
, wxSizerFlags().Expand().Border());
198 wxButton
*btn
= new wxButton(this, DirCtrlPage_Reset
, _T("&Reset"));
199 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
201 // keep consistency between enum and labels of radiobox
202 wxCOMPILE_TIME_ASSERT( stdPathMax
== WXSIZEOF(stdPaths
), EnumForRadioBoxMismatch
);
205 m_radioStdPath
= new wxRadioBox(this, wxID_ANY
, _T("Standard path"),
206 wxDefaultPosition
, wxDefaultSize
,
207 WXSIZEOF(stdPaths
), stdPaths
, 1);
210 m_dirCtrl
= new wxGenericDirCtrl(
213 wxDirDialogDefaultFolderStr
,
219 // the 3 panes panes compose the window
220 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
221 sizerTop
->Add(m_radioStdPath
, 0, wxGROW
| wxALL
, 10);
222 sizerTop
->Add(m_dirCtrl
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
224 // final initializations
230 void DirCtrlWidgetsPage::Reset()
232 m_path
->SetValue(m_dirCtrl
->GetPath());
235 void DirCtrlWidgetsPage::CreateDirCtrl()
237 wxWindowUpdateLocker
noUpdates(this);
239 wxGenericDirCtrl
*dirCtrl
= new wxGenericDirCtrl(
242 wxDirDialogDefaultFolderStr
,
245 ( m_chkDirOnly
->IsChecked() ? wxDIRCTRL_DIR_ONLY
: 0 ) |
246 ( m_chk3D
->IsChecked() ? wxDIRCTRL_3D_INTERNAL
: 0 ) |
247 ( m_chkFirst
->IsChecked() ? wxDIRCTRL_SELECT_FIRST
: 0 ) |
248 ( m_chkLabels
->IsChecked() ? wxDIRCTRL_EDIT_LABELS
: 0 )
252 for (int i
= 0; i
< 3; ++i
)
254 if (m_fltr
[i
]->IsChecked())
256 if (!filter
.IsEmpty())
258 filter
+= m_fltr
[i
]->GetLabel();
261 dirCtrl
->SetFilter(filter
);
263 // update sizer's child window
264 GetSizer()->Replace(m_dirCtrl
, dirCtrl
, true);
266 // update our pointer
270 // relayout the sizer
271 GetSizer()->Layout();
274 // ----------------------------------------------------------------------------
276 // ----------------------------------------------------------------------------
278 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent
& WXUNUSED(event
))
280 m_dirCtrl
->SetPath(m_path
->GetValue());
283 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
290 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent
& WXUNUSED(event
))
295 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent
& WXUNUSED(event
))
299 wxTheApp
->SetAppName(_T("widgets"));
300 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
302 switch ( m_radioStdPath
->GetSelection() )
311 path
= stdp
.GetConfigDir();
315 path
= stdp
.GetDataDir();
318 case stdPathDocuments
:
319 path
= stdp
.GetDocumentsDir();
322 case stdPathLocalData
:
323 path
= stdp
.GetLocalDataDir();
327 path
= stdp
.GetPluginsDir();
330 case stdPathResources
:
331 path
= stdp
.GetResourcesDir();
334 case stdPathUserConfig
:
335 path
= stdp
.GetUserConfigDir();
338 case stdPathUserData
:
339 path
= stdp
.GetUserDataDir();
342 case stdPathUserLocalData
:
343 path
= stdp
.GetUserLocalDataDir();
347 m_dirCtrl
->SetPath(path
);
348 if(!m_dirCtrl
->GetPath().IsSameAs(path
))
350 wxLogMessage(_T("Selected standard path and path from control do not match!"));
351 m_radioStdPath
->SetSelection(stdPathUnknown
);
355 #endif // wxUSE_DIRDLG