1 /////////////////////////////////////////////////////////////////////////////
2 // Program: wxWidgets Widgets Sample
4 // Purpose: Part of the widgets sample showing wxGenericDirCtrl
5 // Author: Wlodzimierz 'ABX' Skiba
7 // Copyright: (c) 2006 wxWindows team
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
28 // for all others, include the necessary headers
33 #include "wx/statbox.h"
34 #include "wx/radiobox.h"
35 #include "wx/checkbox.h"
36 #include "wx/button.h"
37 #include "wx/filedlg.h"
40 #include "wx/generic/dirctrlg.h"
42 #include "wx/wupdlock.h"
43 #include "wx/stdpaths.h"
44 #include "wx/filename.h"
48 #include "icons/dirctrl.xpm"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
57 DirCtrlPage_Reset
= wxID_HIGHEST
,
62 static const wxString stdPaths
[] =
73 wxT("&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
);
114 void OnSelChanged(wxTreeEvent
& event
);
115 void OnFileActivated(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
,
143 wxCheckBox
*m_fltr
[3];
146 DECLARE_EVENT_TABLE()
147 DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage
)
150 // ----------------------------------------------------------------------------
152 // ----------------------------------------------------------------------------
154 BEGIN_EVENT_TABLE(DirCtrlWidgetsPage
, WidgetsPage
)
155 EVT_BUTTON(DirCtrlPage_Reset
, DirCtrlWidgetsPage::OnButtonReset
)
156 EVT_BUTTON(DirCtrlPage_SetPath
, DirCtrlWidgetsPage::OnButtonSetPath
)
157 EVT_CHECKBOX(wxID_ANY
, DirCtrlWidgetsPage::OnCheckBox
)
158 EVT_RADIOBOX(wxID_ANY
, DirCtrlWidgetsPage::OnRadioBox
)
159 EVT_DIRCTRL_SELECTIONCHANGED(DirCtrlPage_Ctrl
, DirCtrlWidgetsPage::OnSelChanged
)
160 EVT_DIRCTRL_FILEACTIVATED(DirCtrlPage_Ctrl
, DirCtrlWidgetsPage::OnFileActivated
)
163 // ============================================================================
165 // ============================================================================
167 IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage
, wxT("DirCtrl"),
171 DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl
*book
,
172 wxImageList
*imaglist
)
173 :WidgetsPage(book
, imaglist
, dirctrl_xpm
)
178 void DirCtrlWidgetsPage::CreateContent()
180 wxSizer
*sizerTop
= new wxBoxSizer(wxHORIZONTAL
);
183 wxStaticBox
*box
= new wxStaticBox(this, wxID_ANY
, wxT("Dir control details"));
185 wxSizer
*sizerLeft
= new wxStaticBoxSizer(box
, wxVERTICAL
);
187 sizerLeft
->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath
, wxT("Set &path"), wxID_ANY
, &m_path
),
188 0, wxALL
| wxALIGN_RIGHT
, 5 );
190 wxSizer
*sizerUseFlags
=
191 new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&Flags"));
192 m_chkDirOnly
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_DIR_ONLY"));
193 m_chk3D
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_3D_INTERNAL"));
194 m_chkFirst
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_SELECT_FIRST"));
195 m_chkFilters
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_SHOW_FILTERS"));
196 m_chkLabels
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_EDIT_LABELS"));
197 m_chkMulti
= CreateCheckBoxAndAddToSizer(sizerUseFlags
, wxT("wxDIRCTRL_MULTIPLE"));
198 sizerLeft
->Add(sizerUseFlags
, wxSizerFlags().Expand().Border());
200 wxSizer
*sizerFilters
=
201 new wxStaticBoxSizer(wxVERTICAL
, this, wxT("&Filters"));
202 m_fltr
[0] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxString::Format(wxT("all files (%s)|%s"),
203 wxFileSelectorDefaultWildcardStr
, wxFileSelectorDefaultWildcardStr
));
204 m_fltr
[1] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
205 m_fltr
[2] = CreateCheckBoxAndAddToSizer(sizerFilters
, wxT("PNG images (*.png)|*.png"));
206 sizerLeft
->Add(sizerFilters
, wxSizerFlags().Expand().Border());
208 wxButton
*btn
= new wxButton(this, DirCtrlPage_Reset
, wxT("&Reset"));
209 sizerLeft
->Add(btn
, 0, wxALIGN_CENTRE_HORIZONTAL
| wxALL
, 15);
211 // keep consistency between enum and labels of radiobox
212 wxCOMPILE_TIME_ASSERT( stdPathMax
== WXSIZEOF(stdPaths
), EnumForRadioBoxMismatch
);
215 m_radioStdPath
= new wxRadioBox(this, wxID_ANY
, wxT("Standard path"),
216 wxDefaultPosition
, wxDefaultSize
,
217 WXSIZEOF(stdPaths
), stdPaths
, 1);
220 m_dirCtrl
= new wxGenericDirCtrl(
223 wxDirDialogDefaultFolderStr
,
229 // the 3 panes panes compose the window
230 sizerTop
->Add(sizerLeft
, 0, (wxALL
& ~wxLEFT
), 10);
231 sizerTop
->Add(m_radioStdPath
, 0, wxGROW
| wxALL
, 10);
232 sizerTop
->Add(m_dirCtrl
, 1, wxGROW
| (wxALL
& ~wxRIGHT
), 10);
234 // final initializations
240 void DirCtrlWidgetsPage::Reset()
242 m_path
->SetValue(m_dirCtrl
->GetPath());
245 void DirCtrlWidgetsPage::CreateDirCtrl()
247 wxWindowUpdateLocker
noUpdates(this);
249 wxGenericDirCtrl
*dirCtrl
= new wxGenericDirCtrl(
252 wxDirDialogDefaultFolderStr
,
255 ( m_chkDirOnly
->IsChecked() ? wxDIRCTRL_DIR_ONLY
: 0 ) |
256 ( m_chk3D
->IsChecked() ? wxDIRCTRL_3D_INTERNAL
: 0 ) |
257 ( m_chkFirst
->IsChecked() ? wxDIRCTRL_SELECT_FIRST
: 0 ) |
258 ( m_chkFilters
->IsChecked() ? wxDIRCTRL_SHOW_FILTERS
: 0 ) |
259 ( m_chkLabels
->IsChecked() ? wxDIRCTRL_EDIT_LABELS
: 0 ) |
260 ( m_chkMulti
->IsChecked() ? wxDIRCTRL_MULTIPLE
: 0)
264 for (int i
= 0; i
< 3; ++i
)
266 if (m_fltr
[i
]->IsChecked())
268 if (!filter
.IsEmpty())
270 filter
+= m_fltr
[i
]->GetLabel();
273 dirCtrl
->SetFilter(filter
);
275 // update sizer's child window
276 GetSizer()->Replace(m_dirCtrl
, dirCtrl
, true);
278 // update our pointer
282 // relayout the sizer
283 GetSizer()->Layout();
286 // ----------------------------------------------------------------------------
288 // ----------------------------------------------------------------------------
290 void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent
& WXUNUSED(event
))
292 m_dirCtrl
->SetPath(m_path
->GetValue());
295 void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent
& WXUNUSED(event
))
302 void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent
& WXUNUSED(event
))
307 void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent
& WXUNUSED(event
))
311 wxTheApp
->SetAppName(wxT("widgets"));
312 wxStandardPathsBase
& stdp
= wxStandardPaths::Get();
314 switch ( m_radioStdPath
->GetSelection() )
323 path
= stdp
.GetConfigDir();
327 path
= stdp
.GetDataDir();
330 case stdPathDocuments
:
331 path
= stdp
.GetDocumentsDir();
334 case stdPathLocalData
:
335 path
= stdp
.GetLocalDataDir();
339 path
= stdp
.GetPluginsDir();
342 case stdPathResources
:
343 path
= stdp
.GetResourcesDir();
346 case stdPathUserConfig
:
347 path
= stdp
.GetUserConfigDir();
350 case stdPathUserData
:
351 path
= stdp
.GetUserDataDir();
354 case stdPathUserLocalData
:
355 path
= stdp
.GetUserLocalDataDir();
359 m_dirCtrl
->SetPath(path
);
361 // Notice that we must use wxFileName comparison instead of simple wxString
362 // comparison as the paths returned may differ by case only.
363 if ( wxFileName(m_dirCtrl
->GetPath()) != path
)
365 wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
366 path
, m_dirCtrl
->GetPath());
370 void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent
& event
)
374 wxLogMessage("Selection changed to \"%s\"",
375 m_dirCtrl
->GetPath(event
.GetItem()));
381 void DirCtrlWidgetsPage::OnFileActivated(wxTreeEvent
& event
)
385 wxLogMessage("File activated \"%s\"",
386 m_dirCtrl
->GetPath(event
.GetItem()));
392 #endif // wxUSE_DIRDLG