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
);
116 void OnFileActivated(wxTreeEvent
& event
);
118 // reset the control parameters
121 // (re)create the m_dirCtrl
122 void CreateDirCtrl();
127 // the control itself and the sizer it is in
128 wxGenericDirCtrl
*m_dirCtrl
;
130 // the text entries for command parameters
133 wxRadioBox
*m_radioStdPath
;
136 wxCheckBox
*m_chkDirOnly
,
144 wxCheckBox
*m_fltr
[3];
147 DECLARE_EVENT_TABLE()
148 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage
)
151 // ----------------------------------------------------------------------------
153 // ----------------------------------------------------------------------------
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
)
164 // ============================================================================
166 // ============================================================================
168 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage
, wxT("DirCtrl"),
172 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl
*book
,
173 wxImageList
*imaglist
)
174 :WidgetsPage(book
, imaglist
, dirctrl_xpm
)
179 void DirCtrlWidgetsPage::CreateContent()
181 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
184 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Dir control details"));
186 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
188 sizerLeft
->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath
, wxT("Set &path"), wxID_ANY
, &m_path
),
189 0, wxALL
| wxALIGN_RIGHT
, 5 );
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());
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());
209 wxButton
*btn
= new wxButton(this, DirCtrlPage_Reset
, wxT("&Reset"));
210 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
212 // keep consistency between enum and labels of radiobox
213 wxCOMPILE_TIME_ASSERT( stdPathMax
== WXSIZEOF(stdPaths
), EnumForRadioBoxMismatch
);
216 m_radioStdPath
= new wxRadioBox(this, wxID_ANY
, wxT("Standard path"),
217 wxDefaultPosition
, wxDefaultSize
,
218 WXSIZEOF(stdPaths
), stdPaths
, 1);
221 m_dirCtrl
= new wxGenericDirCtrl(
224 wxDirDialogDefaultFolderStr
,
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);
235 // final initializations
241 void DirCtrlWidgetsPage::Reset()
243 m_path
->SetValue(m_dirCtrl
->GetPath());
246 void DirCtrlWidgetsPage::CreateDirCtrl()
248 wxWindowUpdateLocker
noUpdates(this);
250 wxGenericDirCtrl
*dirCtrl
= new wxGenericDirCtrl(
253 wxDirDialogDefaultFolderStr
,
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)
265 for (int i
= 0; i
< 3; ++i
)
267 if (m_fltr
[i
]->IsChecked())
269 if (!filter
.IsEmpty())
271 filter
+= m_fltr
[i
]->GetLabel();
274 dirCtrl
->SetFilter(filter
);
276 // update sizer's child window
277 GetSizer()->Replace(m_dirCtrl
, dirCtrl
, true);
279 // update our pointer
283 // relayout the sizer
284 GetSizer()->Layout();
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent
& WXUNUSED(event
))
293 m_dirCtrl
->SetPath(m_path
->GetValue());
296 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
303 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent
& WXUNUSED(event
))
308 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent
& WXUNUSED(event
))
312 wxTheApp
->SetAppName(wxT("widgets"));
313 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
315 switch ( m_radioStdPath
->GetSelection() )
324 path
= stdp
.GetConfigDir();
328 path
= stdp
.GetDataDir();
331 case stdPathDocuments
:
332 path
= stdp
.GetDocumentsDir();
335 case stdPathLocalData
:
336 path
= stdp
.GetLocalDataDir();
340 path
= stdp
.GetPluginsDir();
343 case stdPathResources
:
344 path
= stdp
.GetResourcesDir();
347 case stdPathUserConfig
:
348 path
= stdp
.GetUserConfigDir();
351 case stdPathUserData
:
352 path
= stdp
.GetUserDataDir();
355 case stdPathUserLocalData
:
356 path
= stdp
.GetUserLocalDataDir();
360 m_dirCtrl
->SetPath(path
);
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
)
366 wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
367 path
, m_dirCtrl
->GetPath());
371 void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent
& event
)
375 wxLogMessage("Selection changed to \"%s\"",
376 m_dirCtrl
->GetPath(event
.GetItem()));
382 void DirCtrlWidgetsPage::OnFileActivated(wxTreeEvent
& event
)
386 wxLogMessage("File activated \"%s\"",
387 m_dirCtrl
->GetPath(event
.GetItem()));
393 #endif // wxUSE_DIRDLG