1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/////////////////////////////////////////////////////////////////////////////
// Program: wxWidgets Widgets Sample
// Name: dirctrl.cpp
// Purpose: Part of the widgets sample showing wxGenericDirCtrl
// Author: Wlodzimierz 'ABX' Skiba
// Created: 4 Oct 2006
// Copyright: (c) 2006 wxWindows team
// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
// ============================================================================
// declarations
// ============================================================================
// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------
// for compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#if wxUSE_DIRDLG
// for all others, include the necessary headers
#ifndef WX_PRECOMP
#include "wx/app.h"
#include "wx/log.h"
#include "wx/sizer.h"
#include "wx/statbox.h"
#include "wx/radiobox.h"
#include "wx/checkbox.h"
#include "wx/button.h"
#include "wx/filedlg.h"
#endif
#include "wx/generic/dirctrlg.h"
#include "wx/wupdlock.h"
#include "wx/stdpaths.h"
#include "wx/filename.h"
#include "widgets.h"
#include "icons/dirctrl.xpm"
// ----------------------------------------------------------------------------
// constants
// ----------------------------------------------------------------------------
// control ids
enum
{
DirCtrlPage_Reset = wxID_HIGHEST,
DirCtrlPage_SetPath,
DirCtrlPage_Ctrl
};
static const wxString stdPaths[] =
{
wxT("&none"),
wxT("&config"),
wxT("&data"),
wxT("&documents"),
wxT("&local data"),
wxT("&plugins"),
wxT("&resources"),
wxT("&user config"),
wxT("&user data"),
wxT("&user local data")
};
enum
{
stdPathUnknown = 0,
stdPathConfig,
stdPathData,
stdPathDocuments,
stdPathLocalData,
stdPathPlugins,
stdPathResources,
stdPathUserConfig,
stdPathUserData,
stdPathUserLocalData,
stdPathMax
};
// ----------------------------------------------------------------------------
// CheckBoxWidgetsPage
// ----------------------------------------------------------------------------
class DirCtrlWidgetsPage : public WidgetsPage
{
public:
DirCtrlWidgetsPage(WidgetsBookCtrl *book, wxImageList *imaglist);
virtual ~DirCtrlWidgetsPage() {}
virtual wxControl *GetWidget() const { return m_dirCtrl; }
virtual void RecreateWidget() { CreateDirCtrl(); }
// lazy creation of the content
virtual void CreateContent();
protected:
// event handlers
void OnButtonSetPath(wxCommandEvent& event);
void OnButtonReset(wxCommandEvent& event);
void OnStdPath(wxCommandEvent& event);
void OnCheckBox(wxCommandEvent& event);
void OnRadioBox(wxCommandEvent& event);
void OnSelChanged(wxTreeEvent& event);
void OnFileActivated(wxTreeEvent& event);
// reset the control parameters
void Reset();
// (re)create the m_dirCtrl
void CreateDirCtrl();
// the controls
// ------------
// the control itself and the sizer it is in
wxGenericDirCtrl *m_dirCtrl;
// the text entries for command parameters
wxTextCtrl *m_path;
wxRadioBox *m_radioStdPath;
// flags
wxCheckBox *m_chkDirOnly,
*m_chk3D,
*m_chkFirst,
*m_chkFilters,
*m_chkLabels,
*m_chkMulti;
// filters
wxCheckBox *m_fltr[3];
private:
wxDECLARE_EVENT_TABLE();
DECLARE_WIDGETS_PAGE(DirCtrlWidgetsPage)
};
// ----------------------------------------------------------------------------
// event tables
// ----------------------------------------------------------------------------
wxBEGIN_EVENT_TABLE(DirCtrlWidgetsPage, WidgetsPage)
EVT_BUTTON(DirCtrlPage_Reset, DirCtrlWidgetsPage::OnButtonReset)
EVT_BUTTON(DirCtrlPage_SetPath, DirCtrlWidgetsPage::OnButtonSetPath)
EVT_CHECKBOX(wxID_ANY, DirCtrlWidgetsPage::OnCheckBox)
EVT_RADIOBOX(wxID_ANY, DirCtrlWidgetsPage::OnRadioBox)
EVT_DIRCTRL_SELECTIONCHANGED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnSelChanged)
EVT_DIRCTRL_FILEACTIVATED(DirCtrlPage_Ctrl, DirCtrlWidgetsPage::OnFileActivated)
wxEND_EVENT_TABLE()
// ============================================================================
// implementation
// ============================================================================
IMPLEMENT_WIDGETS_PAGE(DirCtrlWidgetsPage, wxT("DirCtrl"),
GENERIC_CTRLS
);
DirCtrlWidgetsPage::DirCtrlWidgetsPage(WidgetsBookCtrl *book,
wxImageList *imaglist)
:WidgetsPage(book, imaglist, dirctrl_xpm)
{
m_dirCtrl = NULL;
}
void DirCtrlWidgetsPage::CreateContent()
{
wxSizer *sizerTop = new wxBoxSizer(wxHORIZONTAL);
// left pane
wxStaticBox *box = new wxStaticBox(this, wxID_ANY, wxT("Dir control details"));
wxSizer *sizerLeft = new wxStaticBoxSizer(box, wxVERTICAL);
sizerLeft->Add( CreateSizerWithTextAndButton( DirCtrlPage_SetPath , wxT("Set &path"), wxID_ANY, &m_path ),
0, wxALL | wxALIGN_RIGHT , 5 );
wxSizer *sizerUseFlags =
new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Flags"));
m_chkDirOnly = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_DIR_ONLY"));
m_chk3D = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_3D_INTERNAL"));
m_chkFirst = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SELECT_FIRST"));
m_chkFilters = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_SHOW_FILTERS"));
m_chkLabels = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_EDIT_LABELS"));
m_chkMulti = CreateCheckBoxAndAddToSizer(sizerUseFlags, wxT("wxDIRCTRL_MULTIPLE"));
sizerLeft->Add(sizerUseFlags, wxSizerFlags().Expand().Border());
wxSizer *sizerFilters =
new wxStaticBoxSizer(wxVERTICAL, this, wxT("&Filters"));
m_fltr[0] = CreateCheckBoxAndAddToSizer(sizerFilters, wxString::Format(wxT("all files (%s)|%s"),
wxFileSelectorDefaultWildcardStr, wxFileSelectorDefaultWildcardStr));
m_fltr[1] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("C++ files (*.cpp; *.h)|*.cpp;*.h"));
m_fltr[2] = CreateCheckBoxAndAddToSizer(sizerFilters, wxT("PNG images (*.png)|*.png"));
sizerLeft->Add(sizerFilters, wxSizerFlags().Expand().Border());
wxButton *btn = new wxButton(this, DirCtrlPage_Reset, wxT("&Reset"));
sizerLeft->Add(btn, 0, wxALIGN_CENTRE_HORIZONTAL | wxALL, 15);
// keep consistency between enum and labels of radiobox
wxCOMPILE_TIME_ASSERT( stdPathMax == WXSIZEOF(stdPaths), EnumForRadioBoxMismatch);
// middle pane
m_radioStdPath = new wxRadioBox(this, wxID_ANY, wxT("Standard path"),
wxDefaultPosition, wxDefaultSize,
WXSIZEOF(stdPaths), stdPaths, 1);
// right pane
m_dirCtrl = new wxGenericDirCtrl(
this,
DirCtrlPage_Ctrl,
wxDirDialogDefaultFolderStr,
wxDefaultPosition,
wxDefaultSize,
0
);
// the 3 panes panes compose the window
sizerTop->Add(sizerLeft, 0, (wxALL & ~wxLEFT), 10);
sizerTop->Add(m_radioStdPath, 0, wxGROW | wxALL , 10);
sizerTop->Add(m_dirCtrl, 1, wxGROW | (wxALL & ~wxRIGHT), 10);
// final initializations
Reset();
SetSizer(sizerTop);
}
void DirCtrlWidgetsPage::Reset()
{
m_path->SetValue(m_dirCtrl->GetPath());
}
void DirCtrlWidgetsPage::CreateDirCtrl()
{
wxWindowUpdateLocker noUpdates(this);
wxGenericDirCtrl *dirCtrl = new wxGenericDirCtrl(
this,
DirCtrlPage_Ctrl,
wxDirDialogDefaultFolderStr,
wxDefaultPosition,
wxDefaultSize,
( m_chkDirOnly->IsChecked() ? wxDIRCTRL_DIR_ONLY : 0 ) |
( m_chk3D->IsChecked() ? wxDIRCTRL_3D_INTERNAL : 0 ) |
( m_chkFirst->IsChecked() ? wxDIRCTRL_SELECT_FIRST : 0 ) |
( m_chkFilters->IsChecked() ? wxDIRCTRL_SHOW_FILTERS : 0 ) |
( m_chkLabels->IsChecked() ? wxDIRCTRL_EDIT_LABELS : 0 ) |
( m_chkMulti->IsChecked() ? wxDIRCTRL_MULTIPLE : 0)
);
wxString filter;
for (int i = 0; i < 3; ++i)
{
if (m_fltr[i]->IsChecked())
{
if (!filter.IsEmpty())
filter += wxT("|");
filter += m_fltr[i]->GetLabel();
}
}
dirCtrl->SetFilter(filter);
// update sizer's child window
GetSizer()->Replace(m_dirCtrl, dirCtrl, true);
// update our pointer
delete m_dirCtrl;
m_dirCtrl = dirCtrl;
// relayout the sizer
GetSizer()->Layout();
}
// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------
void DirCtrlWidgetsPage::OnButtonSetPath(wxCommandEvent& WXUNUSED(event))
{
m_dirCtrl->SetPath(m_path->GetValue());
}
void DirCtrlWidgetsPage::OnButtonReset(wxCommandEvent& WXUNUSED(event))
{
Reset();
CreateDirCtrl();
}
void DirCtrlWidgetsPage::OnCheckBox(wxCommandEvent& WXUNUSED(event))
{
CreateDirCtrl();
}
void DirCtrlWidgetsPage::OnRadioBox(wxCommandEvent& WXUNUSED(event))
{
wxString path;
wxTheApp->SetAppName(wxT("widgets"));
wxStandardPathsBase& stdp = wxStandardPaths::Get();
switch ( m_radioStdPath->GetSelection() )
{
default:
case stdPathUnknown:
case stdPathMax:
// leave path
break;
case stdPathConfig:
path = stdp.GetConfigDir();
break;
case stdPathData:
path = stdp.GetDataDir();
break;
case stdPathDocuments:
path = stdp.GetDocumentsDir();
break;
case stdPathLocalData:
path = stdp.GetLocalDataDir();
break;
case stdPathPlugins:
path = stdp.GetPluginsDir();
break;
case stdPathResources:
path = stdp.GetResourcesDir();
break;
case stdPathUserConfig:
path = stdp.GetUserConfigDir();
break;
case stdPathUserData:
path = stdp.GetUserDataDir();
break;
case stdPathUserLocalData:
path = stdp.GetUserLocalDataDir();
break;
}
m_dirCtrl->SetPath(path);
// Notice that we must use wxFileName comparison instead of simple wxString
// comparison as the paths returned may differ by case only.
if ( wxFileName(m_dirCtrl->GetPath()) != path )
{
wxLogMessage("Failed to go to \"%s\", the current path is \"%s\".",
path, m_dirCtrl->GetPath());
}
}
void DirCtrlWidgetsPage::OnSelChanged(wxTreeEvent& event)
{
if ( m_dirCtrl )
{
wxLogMessage("Selection changed to \"%s\"",
m_dirCtrl->GetPath(event.GetItem()));
}
event.Skip();
}
void DirCtrlWidgetsPage::OnFileActivated(wxTreeEvent& event)
{
if ( m_dirCtrl )
{
wxLogMessage("File activated \"%s\"",
m_dirCtrl->GetPath(event.GetItem()));
}
event.Skip();
}
#endif // wxUSE_DIRDLG
|