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 // Licence: wxWindows licence
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"
45 #include "wx/filename.h"
49 #include "icons/dirctrl.xpm"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
58 DirCtrlPage_Reset
= wxID_HIGHEST
,
63 static const wxString stdPaths
[] =
74 wxT("&user local data")
92 // ----------------------------------------------------------------------------
93 // CheckBoxWidgetsPage
94 // ----------------------------------------------------------------------------
96 class DirCtrlWidgetsPage
: public WidgetsPage
99 DirCtrlWidgetsPage(WidgetsBookCtrl
*book
, wxImageList
*imaglist
);
100 virtual ~DirCtrlWidgetsPage() {}
102 virtual wxControl
*GetWidget() const { return m_dirCtrl
; }
103 virtual void RecreateWidget() { CreateDirCtrl(); }
105 // lazy creation of the content
106 virtual void CreateContent();
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
);
116 // reset the control parameters
119 // (re)create the m_dirCtrl
120 void CreateDirCtrl();
125 // the control itself and the sizer it is in
126 wxGenericDirCtrl
*m_dirCtrl
;
128 // the text entries for command parameters
131 wxRadioBox
*m_radioStdPath
;
134 wxCheckBox
*m_chkDirOnly
,
141 wxCheckBox
*m_fltr
[3];
144 DECLARE_EVENT_TABLE()
145 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage
)
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage
, WidgetsPage
)
153 EVT_BUTTON(DirCtrlPage_Reset
, DirCtrlWidgetsPage::OnButtonReset
)
154 EVT_BUTTON(DirCtrlPage_SetPath
, DirCtrlWidgetsPage::OnButtonSetPath
)
155 EVT_CHECKBOX(wxID_ANY
, DirCtrlWidgetsPage::OnCheckBox
)
156 EVT_RADIOBOX(wxID_ANY
, DirCtrlWidgetsPage::OnRadioBox
)
159 // ============================================================================
161 // ============================================================================
163 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage
, wxT("DirCtrl"),
167 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl
*book
,
168 wxImageList
*imaglist
)
169 :WidgetsPage(book
, imaglist
, dirctrl_xpm
)
173 void DirCtrlWidgetsPage::CreateContent()
175 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
178 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Dir control details"));
180 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
182 sizerLeft
->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath
, wxT("Set &path"), wxID_ANY
, &m_path
),
183 0, wxALL
| wxALIGN_RIGHT
, 5 );
185 wxSizer
*sizerUseFlags
=
186 new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&Flags"));
187 m_chkDirOnly
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_DIR_ONLY"));
188 m_chk3D
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_3D_INTERNAL"));
189 m_chkFirst
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_SELECT_FIRST"));
190 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_EDIT_LABELS"));
191 m_chkMulti
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_MULTIPLE"));
192 sizerLeft
->Add(sizerUseFlags
, wxSizerFlags().Expand().Border());
194 wxSizer
*sizerFilters
=
195 new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&Filters"));
196 m_fltr
[0] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxString::Format(wxT("all files (%s)|%s"),
197 wxFileSelectorDefaultWildcardStr
, wxFileSelectorDefaultWildcardStr
));
198 m_fltr
[1] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
199 m_fltr
[2] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxT("PNG images (*.png)|*.png"));
200 sizerLeft
->Add(sizerFilters
, wxSizerFlags().Expand().Border());
202 wxButton
*btn
= new wxButton(this, DirCtrlPage_Reset
, wxT("&Reset"));
203 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
205 // keep consistency between enum and labels of radiobox
206 wxCOMPILE_TIME_ASSERT( stdPathMax
== WXSIZEOF(stdPaths
), EnumForRadioBoxMismatch
);
209 m_radioStdPath
= new wxRadioBox(this, wxID_ANY
, wxT("Standard path"),
210 wxDefaultPosition
, wxDefaultSize
,
211 WXSIZEOF(stdPaths
), stdPaths
, 1);
214 m_dirCtrl
= new wxGenericDirCtrl(
217 wxDirDialogDefaultFolderStr
,
223 // the 3 panes panes compose the window
224 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
225 sizerTop
->Add(m_radioStdPath
, 0, wxGROW
| wxALL
, 10);
226 sizerTop
->Add(m_dirCtrl
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
228 // final initializations
234 void DirCtrlWidgetsPage::Reset()
236 m_path
->SetValue(m_dirCtrl
->GetPath());
239 void DirCtrlWidgetsPage::CreateDirCtrl()
241 wxWindowUpdateLocker
noUpdates(this);
243 wxGenericDirCtrl
*dirCtrl
= new wxGenericDirCtrl(
246 wxDirDialogDefaultFolderStr
,
249 ( m_chkDirOnly
->IsChecked() ? wxDIRCTRL_DIR_ONLY
: 0 ) |
250 ( m_chk3D
->IsChecked() ? wxDIRCTRL_3D_INTERNAL
: 0 ) |
251 ( m_chkFirst
->IsChecked() ? wxDIRCTRL_SELECT_FIRST
: 0 ) |
252 ( m_chkLabels
->IsChecked() ? wxDIRCTRL_EDIT_LABELS
: 0 ) |
253 ( m_chkMulti
->IsChecked() ? wxDIRCTRL_MULTIPLE
: 0)
257 for (int i
= 0; i
< 3; ++i
)
259 if (m_fltr
[i
]->IsChecked())
261 if (!filter
.IsEmpty())
263 filter
+= m_fltr
[i
]->GetLabel();
266 dirCtrl
->SetFilter(filter
);
268 // update sizer's child window
269 GetSizer()->Replace(m_dirCtrl
, dirCtrl
, true);
271 // update our pointer
275 // relayout the sizer
276 GetSizer()->Layout();
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent
& WXUNUSED(event
))
285 m_dirCtrl
->SetPath(m_path
->GetValue());
288 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
295 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent
& WXUNUSED(event
))
300 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent
& WXUNUSED(event
))
304 wxTheApp
->SetAppName(wxT("widgets"));
305 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
307 switch ( m_radioStdPath
->GetSelection() )
316 path
= stdp
.GetConfigDir();
320 path
= stdp
.GetDataDir();
323 case stdPathDocuments
:
324 path
= stdp
.GetDocumentsDir();
327 case stdPathLocalData
:
328 path
= stdp
.GetLocalDataDir();
332 path
= stdp
.GetPluginsDir();
335 case stdPathResources
:
336 path
= stdp
.GetResourcesDir();
339 case stdPathUserConfig
:
340 path
= stdp
.GetUserConfigDir();
343 case stdPathUserData
:
344 path
= stdp
.GetUserDataDir();
347 case stdPathUserLocalData
:
348 path
= stdp
.GetUserLocalDataDir();
352 m_dirCtrl
->SetPath(path
);
354 // Notice that we must use wxFileName comparison instead of simple wxString
355 // comparison as the paths returned may differ by case only.
356 if ( wxFileName(m_dirCtrl
->GetPath()) != path
)
358 wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
359 path
, m_dirCtrl
->GetPath());
363 #endif // wxUSE_DIRDLG