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
);
115 void OnSelChanged(wxTreeEvent
& event
);
117 // reset the control parameters
120 // (re)create the m_dirCtrl
121 void CreateDirCtrl();
126 // the control itself and the sizer it is in
127 wxGenericDirCtrl
*m_dirCtrl
;
129 // the text entries for command parameters
132 wxRadioBox
*m_radioStdPath
;
135 wxCheckBox
*m_chkDirOnly
,
142 wxCheckBox
*m_fltr
[3];
145 DECLARE_EVENT_TABLE()
146 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage
)
149 // ----------------------------------------------------------------------------
151 // ----------------------------------------------------------------------------
153 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage
, WidgetsPage
)
154 EVT_BUTTON(DirCtrlPage_Reset
, DirCtrlWidgetsPage::OnButtonReset
)
155 EVT_BUTTON(DirCtrlPage_SetPath
, DirCtrlWidgetsPage::OnButtonSetPath
)
156 EVT_CHECKBOX(wxID_ANY
, DirCtrlWidgetsPage::OnCheckBox
)
157 EVT_RADIOBOX(wxID_ANY
, DirCtrlWidgetsPage::OnRadioBox
)
158 EVT_DIRCTRL_CHANGED(DirCtrlPage_Ctrl
, DirCtrlWidgetsPage::OnSelChanged
)
161 // ============================================================================
163 // ============================================================================
165 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage
, wxT("DirCtrl"),
169 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl
*book
,
170 wxImageList
*imaglist
)
171 :WidgetsPage(book
, imaglist
, dirctrl_xpm
)
176 void DirCtrlWidgetsPage::CreateContent()
178 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
181 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Dir control details"));
183 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
185 sizerLeft
->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath
, wxT("Set &path"), wxID_ANY
, &m_path
),
186 0, wxALL
| wxALIGN_RIGHT
, 5 );
188 wxSizer
*sizerUseFlags
=
189 new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&Flags"));
190 m_chkDirOnly
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_DIR_ONLY"));
191 m_chk3D
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_3D_INTERNAL"));
192 m_chkFirst
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_SELECT_FIRST"));
193 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_EDIT_LABELS"));
194 m_chkMulti
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_MULTIPLE"));
195 sizerLeft
->Add(sizerUseFlags
, wxSizerFlags().Expand().Border());
197 wxSizer
*sizerFilters
=
198 new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&Filters"));
199 m_fltr
[0] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxString::Format(wxT("all files (%s)|%s"),
200 wxFileSelectorDefaultWildcardStr
, wxFileSelectorDefaultWildcardStr
));
201 m_fltr
[1] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
202 m_fltr
[2] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxT("PNG images (*.png)|*.png"));
203 sizerLeft
->Add(sizerFilters
, wxSizerFlags().Expand().Border());
205 wxButton
*btn
= new wxButton(this, DirCtrlPage_Reset
, wxT("&Reset"));
206 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
208 // keep consistency between enum and labels of radiobox
209 wxCOMPILE_TIME_ASSERT( stdPathMax
== WXSIZEOF(stdPaths
), EnumForRadioBoxMismatch
);
212 m_radioStdPath
= new wxRadioBox(this, wxID_ANY
, wxT("Standard path"),
213 wxDefaultPosition
, wxDefaultSize
,
214 WXSIZEOF(stdPaths
), stdPaths
, 1);
217 m_dirCtrl
= new wxGenericDirCtrl(
220 wxDirDialogDefaultFolderStr
,
226 // the 3 panes panes compose the window
227 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
228 sizerTop
->Add(m_radioStdPath
, 0, wxGROW
| wxALL
, 10);
229 sizerTop
->Add(m_dirCtrl
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
231 // final initializations
237 void DirCtrlWidgetsPage::Reset()
239 m_path
->SetValue(m_dirCtrl
->GetPath());
242 void DirCtrlWidgetsPage::CreateDirCtrl()
244 wxWindowUpdateLocker
noUpdates(this);
246 wxGenericDirCtrl
*dirCtrl
= new wxGenericDirCtrl(
249 wxDirDialogDefaultFolderStr
,
252 ( m_chkDirOnly
->IsChecked() ? wxDIRCTRL_DIR_ONLY
: 0 ) |
253 ( m_chk3D
->IsChecked() ? wxDIRCTRL_3D_INTERNAL
: 0 ) |
254 ( m_chkFirst
->IsChecked() ? wxDIRCTRL_SELECT_FIRST
: 0 ) |
255 ( m_chkLabels
->IsChecked() ? wxDIRCTRL_EDIT_LABELS
: 0 ) |
256 ( m_chkMulti
->IsChecked() ? wxDIRCTRL_MULTIPLE
: 0)
260 for (int i
= 0; i
< 3; ++i
)
262 if (m_fltr
[i
]->IsChecked())
264 if (!filter
.IsEmpty())
266 filter
+= m_fltr
[i
]->GetLabel();
269 dirCtrl
->SetFilter(filter
);
271 // update sizer's child window
272 GetSizer()->Replace(m_dirCtrl
, dirCtrl
, true);
274 // update our pointer
278 // relayout the sizer
279 GetSizer()->Layout();
282 // ----------------------------------------------------------------------------
284 // ----------------------------------------------------------------------------
286 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent
& WXUNUSED(event
))
288 m_dirCtrl
->SetPath(m_path
->GetValue());
291 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
298 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent
& WXUNUSED(event
))
303 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent
& WXUNUSED(event
))
307 wxTheApp
->SetAppName(wxT("widgets"));
308 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
310 switch ( m_radioStdPath
->GetSelection() )
319 path
= stdp
.GetConfigDir();
323 path
= stdp
.GetDataDir();
326 case stdPathDocuments
:
327 path
= stdp
.GetDocumentsDir();
330 case stdPathLocalData
:
331 path
= stdp
.GetLocalDataDir();
335 path
= stdp
.GetPluginsDir();
338 case stdPathResources
:
339 path
= stdp
.GetResourcesDir();
342 case stdPathUserConfig
:
343 path
= stdp
.GetUserConfigDir();
346 case stdPathUserData
:
347 path
= stdp
.GetUserDataDir();
350 case stdPathUserLocalData
:
351 path
= stdp
.GetUserLocalDataDir();
355 m_dirCtrl
->SetPath(path
);
357 // Notice that we must use wxFileName comparison instead of simple wxString
358 // comparison as the paths returned may differ by case only.
359 if ( wxFileName(m_dirCtrl
->GetPath()) != path
)
361 wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
362 path
, m_dirCtrl
->GetPath());
366 void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent
& event
)
370 wxLogMessage("Selection changed to \"%s\"",
371 m_dirCtrl
->GetPath(event
.GetItem()));
377 #endif // wxUSE_DIRDLG