| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: dirctrlg.cpp |
| 3 | // Purpose: wxGenericDirCtrl |
| 4 | // Author: Harm van der Heijden, Robert Roebling, Julian Smart |
| 5 | // Modified by: |
| 6 | // Created: 12/12/98 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) Harm van der Heijden, Robert Roebling and Julian Smart |
| 9 | // Licence: wxWindows licence |
| 10 | ///////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | #ifdef __GNUG__ |
| 13 | #pragma implementation "dirctrlg.h" |
| 14 | #endif |
| 15 | |
| 16 | // For compilers that support precompilation, includes "wx.h". |
| 17 | #include "wx/wxprec.h" |
| 18 | |
| 19 | #ifdef __BORLANDC__ |
| 20 | #pragma hdrstop |
| 21 | #endif |
| 22 | |
| 23 | #include "wx/defs.h" |
| 24 | |
| 25 | #include "wx/utils.h" |
| 26 | #include "wx/dialog.h" |
| 27 | #include "wx/button.h" |
| 28 | #include "wx/layout.h" |
| 29 | #include "wx/msgdlg.h" |
| 30 | #include "wx/textdlg.h" |
| 31 | #include "wx/filefn.h" |
| 32 | #include "wx/cmndata.h" |
| 33 | #include "wx/gdicmn.h" |
| 34 | #include "wx/intl.h" |
| 35 | #include "wx/imaglist.h" |
| 36 | #include "wx/icon.h" |
| 37 | #include "wx/log.h" |
| 38 | #include "wx/sizer.h" |
| 39 | #include "wx/tokenzr.h" |
| 40 | #include "wx/dir.h" |
| 41 | #include "wx/settings.h" |
| 42 | |
| 43 | #if wxUSE_STATLINE |
| 44 | #include "wx/statline.h" |
| 45 | #endif |
| 46 | |
| 47 | #include "wx/generic/dirctrlg.h" |
| 48 | |
| 49 | #ifdef __WXMSW__ |
| 50 | #include <windows.h> |
| 51 | |
| 52 | #ifndef __GNUWIN32__ |
| 53 | #include <direct.h> |
| 54 | #include <stdlib.h> |
| 55 | #include <ctype.h> |
| 56 | #endif |
| 57 | |
| 58 | #endif |
| 59 | |
| 60 | #ifdef __BORLANDC__ |
| 61 | #include "dos.h" |
| 62 | #endif |
| 63 | |
| 64 | // If compiled under Windows, this macro can cause problems |
| 65 | #ifdef GetFirstChild |
| 66 | #undef GetFirstChild |
| 67 | #endif |
| 68 | |
| 69 | #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW |
| 70 | /* Closed folder */ |
| 71 | static char * icon1_xpm[] = { |
| 72 | /* width height ncolors chars_per_pixel */ |
| 73 | "16 16 6 1", |
| 74 | /* colors */ |
| 75 | " s None c None", |
| 76 | ". c #000000", |
| 77 | "+ c #c0c0c0", |
| 78 | "@ c #808080", |
| 79 | "# c #ffff00", |
| 80 | "$ c #ffffff", |
| 81 | /* pixels */ |
| 82 | " ", |
| 83 | " @@@@@ ", |
| 84 | " @#+#+#@ ", |
| 85 | " @#+#+#+#@@@@@@ ", |
| 86 | " @$$$$$$$$$$$$@.", |
| 87 | " @$#+#+#+#+#+#@.", |
| 88 | " @$+#+#+#+#+#+@.", |
| 89 | " @$#+#+#+#+#+#@.", |
| 90 | " @$+#+#+#+#+#+@.", |
| 91 | " @$#+#+#+#+#+#@.", |
| 92 | " @$+#+#+#+#+#+@.", |
| 93 | " @$#+#+#+#+#+#@.", |
| 94 | " @@@@@@@@@@@@@@.", |
| 95 | " ..............", |
| 96 | " ", |
| 97 | " "}; |
| 98 | |
| 99 | /* Open folder */ |
| 100 | static char * icon2_xpm[] = { |
| 101 | /* width height ncolors chars_per_pixel */ |
| 102 | "16 16 6 1", |
| 103 | /* colors */ |
| 104 | " s None c None", |
| 105 | ". c #000000", |
| 106 | "+ c #c0c0c0", |
| 107 | "@ c #808080", |
| 108 | "# c #ffff00", |
| 109 | "$ c #ffffff", |
| 110 | /* pixels */ |
| 111 | " ", |
| 112 | " @@@@@ ", |
| 113 | " @$$$$$@ ", |
| 114 | " @$#+#+#$@@@@@@ ", |
| 115 | " @$+#+#+$$$$$$@.", |
| 116 | " @$#+#+#+#+#+#@.", |
| 117 | "@@@@@@@@@@@@@#@.", |
| 118 | "@$$$$$$$$$$@@+@.", |
| 119 | "@$#+#+#+#+##.@@.", |
| 120 | " @$#+#+#+#+#+.@.", |
| 121 | " @$+#+#+#+#+#.@.", |
| 122 | " @$+#+#+#+##@..", |
| 123 | " @@@@@@@@@@@@@.", |
| 124 | " .............", |
| 125 | " ", |
| 126 | " "}; |
| 127 | |
| 128 | /* File */ |
| 129 | static char * icon3_xpm[] = { |
| 130 | /* width height ncolors chars_per_pixel */ |
| 131 | "16 16 3 1", |
| 132 | /* colors */ |
| 133 | " s None c None", |
| 134 | ". c #000000", |
| 135 | "+ c #ffffff", |
| 136 | /* pixels */ |
| 137 | " ", |
| 138 | " ........ ", |
| 139 | " .++++++.. ", |
| 140 | " .+.+.++.+. ", |
| 141 | " .++++++.... ", |
| 142 | " .+.+.+++++. ", |
| 143 | " .+++++++++. ", |
| 144 | " .+.+.+.+.+. ", |
| 145 | " .+++++++++. ", |
| 146 | " .+.+.+.+.+. ", |
| 147 | " .+++++++++. ", |
| 148 | " .+.+.+.+.+. ", |
| 149 | " .+++++++++. ", |
| 150 | " ........... ", |
| 151 | " ", |
| 152 | " "}; |
| 153 | |
| 154 | /* Computer */ |
| 155 | static char * icon4_xpm[] = { |
| 156 | "16 16 7 1", |
| 157 | " s None c None", |
| 158 | ". c #808080", |
| 159 | "X c #c0c0c0", |
| 160 | "o c Black", |
| 161 | "O c Gray100", |
| 162 | "+ c #008080", |
| 163 | "@ c Blue", |
| 164 | " ........... ", |
| 165 | " .XXXXXXXXXX.o", |
| 166 | " .OOOOOOOOO..o", |
| 167 | " .OoooooooX..o", |
| 168 | " .Oo+...@+X..o", |
| 169 | " .Oo+XXX.+X..o", |
| 170 | " .Oo+....+X..o", |
| 171 | " .Oo++++++X..o", |
| 172 | " .OXXXXXXXX.oo", |
| 173 | " ..........o.o", |
| 174 | " ...........Xo", |
| 175 | " .XXXXXXXXXX.o", |
| 176 | " .o.o.o.o.o...o", |
| 177 | " .oXoXoXoXoXo.o ", |
| 178 | ".XOXXXXXXXXX.o ", |
| 179 | "............o "}; |
| 180 | |
| 181 | /* Drive */ |
| 182 | static char * icon5_xpm[] = { |
| 183 | "16 16 7 1", |
| 184 | " s None c None", |
| 185 | ". c #808080", |
| 186 | "X c #c0c0c0", |
| 187 | "o c Black", |
| 188 | "O c Gray100", |
| 189 | "+ c Green", |
| 190 | "@ c #008000", |
| 191 | " ", |
| 192 | " ", |
| 193 | " ", |
| 194 | " ", |
| 195 | " ............. ", |
| 196 | " .XXXXXXXXXXXX.o", |
| 197 | ".OOOOOOOOOOOO..o", |
| 198 | ".XXXXXXXXX+@X..o", |
| 199 | ".XXXXXXXXXXXX..o", |
| 200 | ".X..........X..o", |
| 201 | ".XOOOOOOOOOOX..o", |
| 202 | "..............o ", |
| 203 | " ooooooooooooo ", |
| 204 | " ", |
| 205 | " ", |
| 206 | " "}; |
| 207 | |
| 208 | /* CD-ROM */ |
| 209 | static char *icon6_xpm[] = { |
| 210 | "16 16 10 1", |
| 211 | " s None c None", |
| 212 | ". c #808080", |
| 213 | "X c #c0c0c0", |
| 214 | "o c Yellow", |
| 215 | "O c Blue", |
| 216 | "+ c Black", |
| 217 | "@ c Gray100", |
| 218 | "# c #008080", |
| 219 | "$ c Green", |
| 220 | "% c #008000", |
| 221 | " ... ", |
| 222 | " ..XoX.. ", |
| 223 | " .O.XoXXX+ ", |
| 224 | " ...O.oXXXX+ ", |
| 225 | " .O..X.XXXX+ ", |
| 226 | " ....X.+..XXX+", |
| 227 | " .XXX.+@+.XXX+", |
| 228 | " .X@XX.+.X@@X+", |
| 229 | " .....X...#XX@+ ", |
| 230 | ".@@@...XXo.O@X+ ", |
| 231 | ".@XXX..XXoXOO+ ", |
| 232 | ".@++++..XoX+++ ", |
| 233 | ".@$%@@XX+++X.+ ", |
| 234 | ".............+ ", |
| 235 | " ++++++++++++ ", |
| 236 | " "}; |
| 237 | |
| 238 | /* Floppy */ |
| 239 | static char * icon7_xpm[] = { |
| 240 | "16 16 7 1", |
| 241 | " s None c None", |
| 242 | ". c #808080", |
| 243 | "X c Gray100", |
| 244 | "o c #c0c0c0", |
| 245 | "O c Black", |
| 246 | "+ c Cyan", |
| 247 | "@ c Red", |
| 248 | " ......X", |
| 249 | " .ooooooO", |
| 250 | " .+++++OO", |
| 251 | " .++++++O", |
| 252 | " .++++++O", |
| 253 | " .ooooooO", |
| 254 | " .......o....oO", |
| 255 | " .oooooo.o.O.XoO", |
| 256 | ".XXXXXXXXOOOOOO ", |
| 257 | ".ooooooooo@o..O ", |
| 258 | ".ooo....oooo..O ", |
| 259 | ".o..OOOO...o..O ", |
| 260 | ".oooXXXXoooo..O ", |
| 261 | ".............O ", |
| 262 | " OOOOOOOOOOOO ", |
| 263 | " "}; |
| 264 | |
| 265 | /* Removeable */ |
| 266 | static char * icon8_xpm[] = { |
| 267 | "16 16 7 1", |
| 268 | " s None c None", |
| 269 | ". c #808080", |
| 270 | "X c #c0c0c0", |
| 271 | "o c Black", |
| 272 | "O c Gray100", |
| 273 | "+ c Red", |
| 274 | "@ c #800000", |
| 275 | " ", |
| 276 | " ", |
| 277 | " ", |
| 278 | " ............. ", |
| 279 | " .XXXXXXXXXXXX.o", |
| 280 | ".OOOOOOOOOOOO..o", |
| 281 | ".OXXXXXXXXXXX..o", |
| 282 | ".O+@.oooooo.X..o", |
| 283 | ".OXXOooooooOX..o", |
| 284 | ".OXXXOOOOOOXX..o", |
| 285 | ".OXXXXXXXXXXX..o", |
| 286 | ".O............o ", |
| 287 | " ooooooooooooo ", |
| 288 | " ", |
| 289 | " ", |
| 290 | " "}; |
| 291 | #endif // !wxMSW |
| 292 | |
| 293 | static const int ID_DIRCTRL = 1000; |
| 294 | static const int ID_TEXTCTRL = 1001; |
| 295 | static const int ID_OK = 1002; |
| 296 | static const int ID_CANCEL = 1003; |
| 297 | static const int ID_NEW = 1004; |
| 298 | //static const int ID_CHECK = 1005; |
| 299 | |
| 300 | #if defined(__WXMSW__) |
| 301 | static bool wxIsDriveAvailable(const wxString dirName) |
| 302 | { |
| 303 | #ifdef __WIN32__ |
| 304 | UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX); |
| 305 | #endif |
| 306 | bool success = TRUE; |
| 307 | |
| 308 | // Check if this is a root directory and if so, |
| 309 | // whether the drive is avaiable. |
| 310 | if (dirName.Len() == 3 && dirName[(size_t)1] == wxT(':')) |
| 311 | { |
| 312 | wxString dirNameLower(dirName.Lower()); |
| 313 | #if defined(__GNUWIN32__) |
| 314 | success = wxPathExists(dirNameLower); |
| 315 | #else |
| 316 | int currentDrive = _getdrive(); |
| 317 | int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ; |
| 318 | int err = _chdrive( thisDrive ) ; |
| 319 | _chdrive( currentDrive ); |
| 320 | |
| 321 | if (err == -1) |
| 322 | { |
| 323 | success = FALSE; |
| 324 | } |
| 325 | #endif |
| 326 | } |
| 327 | #ifdef __WIN32__ |
| 328 | (void) SetErrorMode(errorMode); |
| 329 | #endif |
| 330 | |
| 331 | return success; |
| 332 | } |
| 333 | #endif |
| 334 | |
| 335 | //----------------------------------------------------------------------------- |
| 336 | // wxDirItemDataEx |
| 337 | //----------------------------------------------------------------------------- |
| 338 | |
| 339 | wxDirItemDataEx::wxDirItemDataEx(const wxString& path, const wxString& name, |
| 340 | bool isDir) |
| 341 | { |
| 342 | m_path = path; |
| 343 | m_name = name; |
| 344 | /* Insert logic to detect hidden files here |
| 345 | * In UnixLand we just check whether the first char is a dot |
| 346 | * For FileNameFromPath read LastDirNameInThisPath ;-) */ |
| 347 | // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.'); |
| 348 | m_isHidden = FALSE; |
| 349 | // m_hasSubDirs is no longer needed |
| 350 | m_hasSubDirs = TRUE; // HasSubDirs(); |
| 351 | m_isExpanded = FALSE; |
| 352 | m_isDir = isDir; |
| 353 | } |
| 354 | |
| 355 | wxDirItemDataEx::~wxDirItemDataEx() |
| 356 | { |
| 357 | } |
| 358 | |
| 359 | void wxDirItemDataEx::SetNewDirName( wxString path ) |
| 360 | { |
| 361 | m_path = path; |
| 362 | m_name = wxFileNameFromPath( path ); |
| 363 | } |
| 364 | |
| 365 | //----------------------------------------------------------------------------- |
| 366 | // wxGenericDirCtrl |
| 367 | //----------------------------------------------------------------------------- |
| 368 | |
| 369 | IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl) |
| 370 | |
| 371 | BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl) |
| 372 | EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem) |
| 373 | EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem) |
| 374 | EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem) |
| 375 | EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem) |
| 376 | EVT_SIZE (wxGenericDirCtrl::OnSize) |
| 377 | END_EVENT_TABLE() |
| 378 | |
| 379 | wxGenericDirCtrl::wxGenericDirCtrl(void) |
| 380 | { |
| 381 | Init(); |
| 382 | } |
| 383 | |
| 384 | bool wxGenericDirCtrl::Create(wxWindow *parent, |
| 385 | const wxWindowID id, |
| 386 | const wxString& dir, |
| 387 | const wxPoint& pos, |
| 388 | const wxSize& size, |
| 389 | long style, |
| 390 | const wxString& filter, |
| 391 | int defaultFilter, |
| 392 | const wxString& name ) |
| 393 | { |
| 394 | if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name)) |
| 395 | return FALSE; |
| 396 | |
| 397 | SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE)); |
| 398 | |
| 399 | Init(); |
| 400 | |
| 401 | long treeStyle = wxTR_HAS_BUTTONS ; // | wxTR_EDIT_LABELS; |
| 402 | if ((style & wxDIRCTRL_3D_INTERNAL) == 0) |
| 403 | treeStyle |= wxNO_BORDER; |
| 404 | |
| 405 | long filterStyle = 0; |
| 406 | if ((style & wxDIRCTRL_3D_INTERNAL) == 0) |
| 407 | filterStyle |= wxNO_BORDER; |
| 408 | |
| 409 | m_treeCtrl = new wxTreeCtrl(this, wxID_TREECTRL, pos, size, treeStyle); |
| 410 | |
| 411 | if (!filter.IsEmpty() && (style & wxDIRCTRL_SHOW_FILTERS)) |
| 412 | m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle); |
| 413 | |
| 414 | m_defaultPath = dir; |
| 415 | m_filter = filter; |
| 416 | |
| 417 | SetFilterIndex(defaultFilter); |
| 418 | |
| 419 | if (m_filterListCtrl) |
| 420 | m_filterListCtrl->FillFilterList(filter, defaultFilter); |
| 421 | |
| 422 | m_imageList = new wxImageList(16, 16, TRUE); |
| 423 | #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW |
| 424 | m_imageList->Add(wxIcon(icon1_xpm)); |
| 425 | m_imageList->Add(wxIcon(icon2_xpm)); |
| 426 | m_imageList->Add(wxIcon(icon3_xpm)); |
| 427 | m_imageList->Add(wxIcon(icon4_xpm)); |
| 428 | m_imageList->Add(wxIcon(icon5_xpm)); |
| 429 | m_imageList->Add(wxIcon(icon6_xpm)); |
| 430 | m_imageList->Add(wxIcon(icon7_xpm)); |
| 431 | m_imageList->Add(wxIcon(icon8_xpm)); |
| 432 | #elif defined(__WXMSW__) |
| 433 | m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE)); |
| 434 | m_imageList->Add(wxIcon(wxT("wxICON_SMALL_OPEN_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE)); |
| 435 | m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE)); |
| 436 | m_imageList->Add(wxIcon(wxT("wxICON_SMALL_COMPUTER"), wxBITMAP_TYPE_ICO_RESOURCE)); |
| 437 | m_imageList->Add(wxIcon(wxT("wxICON_SMALL_DRIVE"), wxBITMAP_TYPE_ICO_RESOURCE)); |
| 438 | m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CDROM"), wxBITMAP_TYPE_ICO_RESOURCE)); |
| 439 | m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FLOPPY"), wxBITMAP_TYPE_ICO_RESOURCE)); |
| 440 | m_imageList->Add(wxIcon(wxT("wxICON_SMALL_REMOVEABLE"), wxBITMAP_TYPE_ICO_RESOURCE)); |
| 441 | #else |
| 442 | #error "Sorry, we don't have icons available for this platforms." |
| 443 | #endif |
| 444 | m_treeCtrl->SetImageList(m_imageList); |
| 445 | |
| 446 | m_showHidden = FALSE; |
| 447 | wxDirItemDataEx* rootData = new wxDirItemDataEx(wxT(""), wxT(""), TRUE); |
| 448 | |
| 449 | wxString rootName; |
| 450 | |
| 451 | #ifdef __WXMSW__ |
| 452 | rootName = _("Computer"); |
| 453 | #else |
| 454 | rootName = _("Sections"); |
| 455 | #endif |
| 456 | |
| 457 | m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData); |
| 458 | m_treeCtrl->SetItemHasChildren(m_rootId); |
| 459 | m_treeCtrl->Expand(m_rootId); // automatically expand first level |
| 460 | |
| 461 | // Expand and select the default path |
| 462 | if (!m_defaultPath.IsEmpty()) |
| 463 | ExpandPath(m_defaultPath); |
| 464 | |
| 465 | DoResize(); |
| 466 | |
| 467 | return TRUE; |
| 468 | } |
| 469 | |
| 470 | wxGenericDirCtrl::~wxGenericDirCtrl() |
| 471 | { |
| 472 | m_treeCtrl->SetImageList(NULL); |
| 473 | delete m_imageList; |
| 474 | } |
| 475 | |
| 476 | void wxGenericDirCtrl::Init() |
| 477 | { |
| 478 | m_showHidden = FALSE; |
| 479 | m_imageList = NULL; |
| 480 | m_currentFilter = 0; |
| 481 | m_currentFilterStr = wxEmptyString; // Default: any file |
| 482 | m_treeCtrl = NULL; |
| 483 | m_filterListCtrl = NULL; |
| 484 | } |
| 485 | |
| 486 | void wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId) |
| 487 | { |
| 488 | wxDirItemDataEx *dir_item = new wxDirItemDataEx(path,name,TRUE); |
| 489 | |
| 490 | #ifdef __WXMSW__ |
| 491 | // Windows: sections are displayed as drives |
| 492 | wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, imageId, -1, dir_item); |
| 493 | #else |
| 494 | // Unix: sections are displayed as folders |
| 495 | wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, 0, -1, dir_item); |
| 496 | m_treeCtrl->SetItemImage( id, 1, wxTreeItemIcon_Expanded ); |
| 497 | #endif |
| 498 | // TODO: other operating systems. |
| 499 | |
| 500 | m_treeCtrl->SetItemHasChildren(id); |
| 501 | } |
| 502 | |
| 503 | void wxGenericDirCtrl::SetupSections() |
| 504 | { |
| 505 | #ifdef __WXMSW__ |
| 506 | |
| 507 | #ifdef __WIN32__ |
| 508 | wxChar driveBuffer[256]; |
| 509 | size_t n = (size_t) GetLogicalDriveStrings(255, driveBuffer); |
| 510 | size_t i = 0; |
| 511 | while (i < n) |
| 512 | { |
| 513 | wxString path, name; |
| 514 | path.Printf(wxT("%c:\\"), driveBuffer[i]); |
| 515 | name.Printf(wxT("(%c:)"), driveBuffer[i]); |
| 516 | |
| 517 | int imageId = 4; |
| 518 | int driveType = ::GetDriveType(path); |
| 519 | switch (driveType) |
| 520 | { |
| 521 | case DRIVE_REMOVABLE: |
| 522 | if (path == wxT("a:\\") || path == wxT("b:\\")) |
| 523 | imageId = 6; // Floppy |
| 524 | else |
| 525 | imageId = 7; |
| 526 | break; |
| 527 | case DRIVE_FIXED: |
| 528 | imageId = 4; |
| 529 | break; |
| 530 | case DRIVE_REMOTE: |
| 531 | imageId = 4; |
| 532 | break; |
| 533 | case DRIVE_CDROM: |
| 534 | imageId = 5; |
| 535 | break; |
| 536 | default: |
| 537 | imageId = 4; |
| 538 | break; |
| 539 | } |
| 540 | |
| 541 | AddSection(path, name, imageId); |
| 542 | |
| 543 | while (driveBuffer[i] != wxT('\0')) |
| 544 | i ++; |
| 545 | i ++; |
| 546 | if (driveBuffer[i] == wxT('\0')) |
| 547 | break; |
| 548 | } |
| 549 | #else |
| 550 | int drive; |
| 551 | int currentDrive; |
| 552 | |
| 553 | /* If we can switch to the drive, it exists. */ |
| 554 | for( drive = 1; drive <= 26; drive++ ) |
| 555 | { |
| 556 | wxString path, name; |
| 557 | path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1)); |
| 558 | name.Printf(wxT("(%c:)"), (char) (drive + 'a' - 1)); |
| 559 | |
| 560 | if (wxIsDriveAvailable(path)) |
| 561 | { |
| 562 | |
| 563 | AddSection(path, name); |
| 564 | } |
| 565 | } |
| 566 | #endif |
| 567 | |
| 568 | #else |
| 569 | AddSection(wxT("/"), _("The Computer"), 0); |
| 570 | AddSection(wxGetHomeDir(), _("My Home"), 0 ); |
| 571 | AddSection(wxT("/mnt"), _("Mounted Devices"), 0 ); |
| 572 | AddSection(wxT("/usr/local"), _("User Local"), 0 ); |
| 573 | AddSection(wxT("/usr"), _("User"), 0 ); |
| 574 | AddSection(wxT("/var"), _("Variables"), 0 ); |
| 575 | AddSection(wxT("/etc"), _("Etcetera"), 0 ); |
| 576 | AddSection(wxT("/tmp"), _("Temporary"), 0 ); |
| 577 | #endif |
| 578 | } |
| 579 | |
| 580 | void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event) |
| 581 | { |
| 582 | // don't rename the main entry "Sections" |
| 583 | if (event.GetItem() == m_rootId) |
| 584 | { |
| 585 | event.Veto(); |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | // don't rename the individual sections |
| 590 | if (m_treeCtrl->GetParent( event.GetItem() ) == m_rootId) |
| 591 | { |
| 592 | event.Veto(); |
| 593 | return; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event) |
| 598 | { |
| 599 | if ((event.GetLabel().IsEmpty()) || |
| 600 | (event.GetLabel() == _(".")) || |
| 601 | (event.GetLabel() == _("..")) || |
| 602 | (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND)) |
| 603 | { |
| 604 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); |
| 605 | dialog.ShowModal(); |
| 606 | event.Veto(); |
| 607 | return; |
| 608 | } |
| 609 | |
| 610 | wxTreeItemId id = event.GetItem(); |
| 611 | wxDirItemDataEx *data = (wxDirItemDataEx*)m_treeCtrl->GetItemData( id ); |
| 612 | wxASSERT( data ); |
| 613 | |
| 614 | wxString new_name( wxPathOnly( data->m_path ) ); |
| 615 | new_name += wxString(wxFILE_SEP_PATH); |
| 616 | new_name += event.GetLabel(); |
| 617 | |
| 618 | wxLogNull log; |
| 619 | |
| 620 | if (wxFileExists(new_name)) |
| 621 | { |
| 622 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); |
| 623 | dialog.ShowModal(); |
| 624 | event.Veto(); |
| 625 | } |
| 626 | |
| 627 | if (wxRenameFile(data->m_path,new_name)) |
| 628 | { |
| 629 | data->SetNewDirName( new_name ); |
| 630 | } |
| 631 | else |
| 632 | { |
| 633 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); |
| 634 | dialog.ShowModal(); |
| 635 | event.Veto(); |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event) |
| 640 | { |
| 641 | wxTreeItemId parentId = event.GetItem(); |
| 642 | |
| 643 | ExpandDir(parentId); |
| 644 | } |
| 645 | |
| 646 | void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event ) |
| 647 | { |
| 648 | wxTreeItemId child, parent = event.GetItem(); |
| 649 | |
| 650 | wxDirItemDataEx *data = (wxDirItemDataEx *) m_treeCtrl->GetItemData(event.GetItem()); |
| 651 | if (!data->m_isExpanded) |
| 652 | return; |
| 653 | |
| 654 | data->m_isExpanded = FALSE; |
| 655 | long cookie; |
| 656 | /* Workaround because DeleteChildren has disapeared (why?) and |
| 657 | * CollapseAndReset doesn't work as advertised (deletes parent too) */ |
| 658 | child = m_treeCtrl->GetFirstChild(parent, cookie); |
| 659 | while (child.IsOk()) |
| 660 | { |
| 661 | m_treeCtrl->Delete(child); |
| 662 | /* Not GetNextChild below, because the cookie mechanism can't |
| 663 | * handle disappearing children! */ |
| 664 | child = m_treeCtrl->GetFirstChild(parent, cookie); |
| 665 | } |
| 666 | } |
| 667 | |
| 668 | void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId) |
| 669 | { |
| 670 | wxDirItemDataEx *data = (wxDirItemDataEx *) m_treeCtrl->GetItemData(parentId); |
| 671 | |
| 672 | if (data->m_isExpanded) |
| 673 | return; |
| 674 | |
| 675 | data->m_isExpanded = TRUE; |
| 676 | |
| 677 | if (parentId == m_rootId) |
| 678 | { |
| 679 | SetupSections(); |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | wxASSERT(data); |
| 684 | |
| 685 | wxString search,path,filename; |
| 686 | |
| 687 | wxString dirName(data->m_path); |
| 688 | |
| 689 | #ifdef __WXMSW__ |
| 690 | // Check if this is a root directory and if so, |
| 691 | // whether the drive is avaiable. |
| 692 | if (!wxIsDriveAvailable(dirName)) |
| 693 | { |
| 694 | data->m_isExpanded = FALSE; |
| 695 | //wxMessageBox(wxT("Sorry, this drive is not available.")); |
| 696 | return; |
| 697 | } |
| 698 | #endif |
| 699 | |
| 700 | // This may take a longish time. Go to busy cursor |
| 701 | wxBusyCursor busy; |
| 702 | |
| 703 | #ifdef __WXMSW__ |
| 704 | if (dirName.Last() == ':') |
| 705 | dirName += wxString(wxFILE_SEP_PATH); |
| 706 | #endif |
| 707 | |
| 708 | wxArrayString dirs; |
| 709 | wxArrayString filenames; |
| 710 | |
| 711 | wxDir d; |
| 712 | wxString eachFilename; |
| 713 | |
| 714 | wxLogNull log; |
| 715 | d.Open(dirName); |
| 716 | |
| 717 | if (d.IsOpened()) |
| 718 | { |
| 719 | if (d.GetFirst(& eachFilename, wxEmptyString, wxDIR_DIRS)) |
| 720 | { |
| 721 | do |
| 722 | { |
| 723 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) |
| 724 | { |
| 725 | dirs.Add(eachFilename); |
| 726 | } |
| 727 | } |
| 728 | while (d.GetNext(& eachFilename)) ; |
| 729 | } |
| 730 | } |
| 731 | dirs.Sort(); |
| 732 | |
| 733 | // Now do the filenames -- but only if we're allowed to |
| 734 | if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) |
| 735 | { |
| 736 | wxLogNull log; |
| 737 | |
| 738 | d.Open(dirName); |
| 739 | |
| 740 | if (d.IsOpened()) |
| 741 | { |
| 742 | if (d.GetFirst(& eachFilename, m_currentFilterStr, wxDIR_FILES)) |
| 743 | { |
| 744 | do |
| 745 | { |
| 746 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) |
| 747 | { |
| 748 | filenames.Add(eachFilename); |
| 749 | } |
| 750 | } |
| 751 | while (d.GetNext(& eachFilename)) ; |
| 752 | } |
| 753 | } |
| 754 | filenames.Sort(); |
| 755 | } |
| 756 | |
| 757 | // Add the sorted dirs |
| 758 | size_t i; |
| 759 | for (i = 0; i < dirs.Count(); i++) |
| 760 | { |
| 761 | wxString eachFilename(dirs[i]); |
| 762 | path = dirName; |
| 763 | if (path.Last() != wxFILE_SEP_PATH) |
| 764 | path += wxString(wxFILE_SEP_PATH); |
| 765 | path += eachFilename; |
| 766 | |
| 767 | wxDirItemDataEx *dir_item = new wxDirItemDataEx(path,eachFilename,TRUE); |
| 768 | wxTreeItemId id = m_treeCtrl->AppendItem( parentId, eachFilename, 0, -1, dir_item); |
| 769 | m_treeCtrl->SetItemImage( id, 1, wxTreeItemIcon_Expanded ); |
| 770 | |
| 771 | // Has this got any children? If so, make it expandable. |
| 772 | int options = wxDIR_DEFAULT; |
| 773 | if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY) // If only showing dirs, then we specify dirs only here |
| 774 | { |
| 775 | options = wxDIR_DIRS; |
| 776 | } |
| 777 | |
| 778 | wxLogNull log; |
| 779 | wxDir dir2; |
| 780 | if (dir2.Open(path)) |
| 781 | { |
| 782 | wxString str; |
| 783 | // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and |
| 784 | // and filters out any directories |
| 785 | if (dir2.GetFirst(& str, m_currentFilterStr, options) || dir2.GetFirst(& str, wxEmptyString, wxDIR_DIRS)) |
| 786 | { |
| 787 | m_treeCtrl->SetItemHasChildren(id); |
| 788 | } |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | // Add the sorted filenames |
| 793 | if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) |
| 794 | { |
| 795 | for (i = 0; i < filenames.Count(); i++) |
| 796 | { |
| 797 | wxString eachFilename(filenames[i]); |
| 798 | path = dirName; |
| 799 | if (path.Last() != wxFILE_SEP_PATH) |
| 800 | path += wxString(wxFILE_SEP_PATH); |
| 801 | path += eachFilename; |
| 802 | //path = dirName + wxString(wxT("/")) + eachFilename; |
| 803 | wxDirItemDataEx *dir_item = new wxDirItemDataEx(path,eachFilename,FALSE); |
| 804 | (void)m_treeCtrl->AppendItem( parentId, eachFilename, 2, -1, dir_item); |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
| 809 | // Find the child that matches the first part of 'path'. |
| 810 | // E.g. if a child path is "/usr" and 'path' is "/usr/include" |
| 811 | // then the child for /usr is returned. |
| 812 | wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done) |
| 813 | { |
| 814 | wxString path2(path); |
| 815 | |
| 816 | // Make sure all separators are as per the current platform |
| 817 | path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH)); |
| 818 | path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH)); |
| 819 | |
| 820 | // Append a separator to foil bogus substring matching |
| 821 | path2 += wxString(wxFILE_SEP_PATH); |
| 822 | |
| 823 | // In MSW, case is not significant |
| 824 | #ifdef __WXMSW__ |
| 825 | path2.MakeLower(); |
| 826 | #endif |
| 827 | |
| 828 | long cookie; |
| 829 | wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie); |
| 830 | while (childId != 0) |
| 831 | { |
| 832 | wxDirItemDataEx* data = (wxDirItemDataEx*) m_treeCtrl->GetItemData(childId); |
| 833 | |
| 834 | if (data && data->m_path != "") |
| 835 | { |
| 836 | wxString childPath(data->m_path); |
| 837 | if (childPath.Last() != wxFILE_SEP_PATH) |
| 838 | childPath += wxString(wxFILE_SEP_PATH); |
| 839 | |
| 840 | // In MSW, case is not significant |
| 841 | #ifdef __WXMSW__ |
| 842 | childPath.MakeLower(); |
| 843 | #endif |
| 844 | |
| 845 | if (childPath.Len() <= path2.Len()) |
| 846 | { |
| 847 | wxString path3 = path2.Mid(0, childPath.Len()); |
| 848 | if (childPath == path3) |
| 849 | { |
| 850 | if (path3.Len() == path2.Len()) |
| 851 | done = TRUE; |
| 852 | else |
| 853 | done = FALSE; |
| 854 | return childId; |
| 855 | } |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | childId = m_treeCtrl->GetNextChild(childId, cookie); |
| 860 | } |
| 861 | wxTreeItemId invalid; |
| 862 | return invalid; |
| 863 | } |
| 864 | |
| 865 | // Try to expand as much of the given path as possible, |
| 866 | // and select the given tree item. |
| 867 | bool wxGenericDirCtrl::ExpandPath(const wxString& path) |
| 868 | { |
| 869 | bool done = FALSE; |
| 870 | wxTreeItemId id = FindChild(m_rootId, path, done); |
| 871 | wxTreeItemId lastId = id; // The last non-zero id |
| 872 | while ((id > 0) && !done) |
| 873 | { |
| 874 | ExpandDir(id); |
| 875 | |
| 876 | id = FindChild(id, path, done); |
| 877 | if (id != 0) |
| 878 | lastId = id; |
| 879 | } |
| 880 | if (lastId > 0) |
| 881 | { |
| 882 | wxDirItemDataEx *data = (wxDirItemDataEx *) m_treeCtrl->GetItemData(lastId); |
| 883 | if (data->m_isDir) |
| 884 | { |
| 885 | m_treeCtrl->Expand(lastId); |
| 886 | } |
| 887 | if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir) |
| 888 | { |
| 889 | // Find the first file in this directory |
| 890 | long cookie; |
| 891 | wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie); |
| 892 | bool selectedChild = FALSE; |
| 893 | while (childId != 0) |
| 894 | { |
| 895 | wxDirItemDataEx* data = (wxDirItemDataEx*) m_treeCtrl->GetItemData(childId); |
| 896 | |
| 897 | if (data && data->m_path != "" && !data->m_isDir) |
| 898 | { |
| 899 | m_treeCtrl->SelectItem(childId); |
| 900 | m_treeCtrl->EnsureVisible(childId); |
| 901 | selectedChild = TRUE; |
| 902 | break; |
| 903 | } |
| 904 | childId = m_treeCtrl->GetNextChild(lastId, cookie); |
| 905 | } |
| 906 | if (!selectedChild) |
| 907 | { |
| 908 | m_treeCtrl->SelectItem(lastId); |
| 909 | m_treeCtrl->EnsureVisible(lastId); |
| 910 | } |
| 911 | } |
| 912 | else |
| 913 | { |
| 914 | m_treeCtrl->SelectItem(lastId); |
| 915 | m_treeCtrl->EnsureVisible(lastId); |
| 916 | } |
| 917 | |
| 918 | return TRUE; |
| 919 | } |
| 920 | else |
| 921 | return FALSE; |
| 922 | } |
| 923 | |
| 924 | wxString wxGenericDirCtrl::GetPath() const |
| 925 | { |
| 926 | wxTreeItemId id = m_treeCtrl->GetSelection(); |
| 927 | if (id) |
| 928 | { |
| 929 | wxDirItemDataEx* data = (wxDirItemDataEx*) m_treeCtrl->GetItemData(id); |
| 930 | return data->m_path; |
| 931 | } |
| 932 | else |
| 933 | return wxEmptyString; |
| 934 | } |
| 935 | |
| 936 | wxString wxGenericDirCtrl::GetFilePath() const |
| 937 | { |
| 938 | wxTreeItemId id = m_treeCtrl->GetSelection(); |
| 939 | if (id) |
| 940 | { |
| 941 | wxDirItemDataEx* data = (wxDirItemDataEx*) m_treeCtrl->GetItemData(id); |
| 942 | if (data->m_isDir) |
| 943 | return wxEmptyString; |
| 944 | else |
| 945 | return data->m_path; |
| 946 | } |
| 947 | else |
| 948 | return wxEmptyString; |
| 949 | } |
| 950 | |
| 951 | void wxGenericDirCtrl::SetPath(const wxString& path) |
| 952 | { |
| 953 | m_defaultPath = path; |
| 954 | if (m_rootId) |
| 955 | ExpandPath(path); |
| 956 | } |
| 957 | |
| 958 | // Not used |
| 959 | #if 0 |
| 960 | void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames) |
| 961 | { |
| 962 | wxDirItemDataEx *data = (wxDirItemDataEx *) m_treeCtrl->GetItemData(id); |
| 963 | |
| 964 | // This may take a longish time. Go to busy cursor |
| 965 | wxBusyCursor busy; |
| 966 | |
| 967 | wxASSERT(data); |
| 968 | |
| 969 | wxString search,path,filename; |
| 970 | |
| 971 | wxString dirName(data->m_path); |
| 972 | |
| 973 | #ifdef __WXMSW__ |
| 974 | if (dirName.Last() == ':') |
| 975 | dirName += wxString(wxFILE_SEP_PATH); |
| 976 | #endif |
| 977 | |
| 978 | wxDir d; |
| 979 | wxString eachFilename; |
| 980 | |
| 981 | wxLogNull log; |
| 982 | d.Open(dirName); |
| 983 | |
| 984 | if (d.IsOpened()) |
| 985 | { |
| 986 | if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags)) |
| 987 | { |
| 988 | do |
| 989 | { |
| 990 | if ((eachFilename != wxT(".")) && (eachFilename != wxT(".."))) |
| 991 | { |
| 992 | filenames.Add(eachFilename); |
| 993 | } |
| 994 | } |
| 995 | while (d.GetNext(& eachFilename)) ; |
| 996 | } |
| 997 | } |
| 998 | } |
| 999 | #endif |
| 1000 | |
| 1001 | void wxGenericDirCtrl::SetFilterIndex(int n) |
| 1002 | { |
| 1003 | m_currentFilter = n; |
| 1004 | |
| 1005 | wxString f, d; |
| 1006 | if (ExtractWildcard(m_filter, n, f, d)) |
| 1007 | m_currentFilterStr = f; |
| 1008 | else |
| 1009 | m_currentFilterStr = wxT("*.*"); |
| 1010 | } |
| 1011 | |
| 1012 | void wxGenericDirCtrl::SetFilter(const wxString& filter) |
| 1013 | { |
| 1014 | m_filter = filter; |
| 1015 | |
| 1016 | wxString f, d; |
| 1017 | if (ExtractWildcard(m_filter, m_currentFilter, f, d)) |
| 1018 | m_currentFilterStr = f; |
| 1019 | else |
| 1020 | m_currentFilterStr = wxT("*.*"); |
| 1021 | } |
| 1022 | |
| 1023 | // Extract description and actual filter from overall filter string |
| 1024 | bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description) |
| 1025 | { |
| 1026 | wxArrayString filters, descriptions; |
| 1027 | int count = ParseFilter(filterStr, filters, descriptions); |
| 1028 | if (count > 0 && n < count) |
| 1029 | { |
| 1030 | filter = filters[n]; |
| 1031 | description = descriptions[n]; |
| 1032 | return TRUE; |
| 1033 | } |
| 1034 | else |
| 1035 | return FALSE; |
| 1036 | } |
| 1037 | |
| 1038 | // Parses the global filter, returning the number of filters. |
| 1039 | // Returns 0 if none or if there's a problem. |
| 1040 | // filterStr is in the form: |
| 1041 | // |
| 1042 | // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg" |
| 1043 | |
| 1044 | int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions) |
| 1045 | { |
| 1046 | wxString str(filterStr); |
| 1047 | |
| 1048 | wxString description, filter; |
| 1049 | int pos; |
| 1050 | bool finished = FALSE; |
| 1051 | do |
| 1052 | { |
| 1053 | pos = str.Find(wxT('|')); |
| 1054 | if (pos == -1) |
| 1055 | return 0; // Problem |
| 1056 | description = str.Left(pos); |
| 1057 | str = str.Mid(pos+1); |
| 1058 | pos = str.Find(wxT('|')); |
| 1059 | if (pos == -1) |
| 1060 | { |
| 1061 | filter = str; |
| 1062 | finished = TRUE; |
| 1063 | } |
| 1064 | else |
| 1065 | { |
| 1066 | filter = str.Left(pos); |
| 1067 | str = str.Mid(pos+1); |
| 1068 | } |
| 1069 | descriptions.Add(description); |
| 1070 | filters.Add(filter); |
| 1071 | } |
| 1072 | while (!finished) ; |
| 1073 | |
| 1074 | return filters.Count(); |
| 1075 | } |
| 1076 | |
| 1077 | void wxGenericDirCtrl::DoResize() |
| 1078 | { |
| 1079 | wxSize sz = GetClientSize(); |
| 1080 | int verticalSpacing = 3; |
| 1081 | if (m_treeCtrl) |
| 1082 | { |
| 1083 | wxSize filterSz ; |
| 1084 | if (m_filterListCtrl) |
| 1085 | { |
| 1086 | filterSz = m_filterListCtrl->GetSize(); |
| 1087 | sz.y -= (filterSz.y + verticalSpacing); |
| 1088 | } |
| 1089 | m_treeCtrl->SetSize(0, 0, sz.x, sz.y); |
| 1090 | if (m_filterListCtrl) |
| 1091 | { |
| 1092 | m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y); |
| 1093 | // Don't know why, but this needs refreshing after a resize (wxMSW) |
| 1094 | m_filterListCtrl->Refresh(); |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | void wxGenericDirCtrl::OnSize(wxSizeEvent &event) |
| 1101 | { |
| 1102 | DoResize(); |
| 1103 | } |
| 1104 | |
| 1105 | //----------------------------------------------------------------------------- |
| 1106 | // wxDirFilterListCtrl |
| 1107 | //----------------------------------------------------------------------------- |
| 1108 | |
| 1109 | IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice) |
| 1110 | |
| 1111 | BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice) |
| 1112 | EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter) |
| 1113 | END_EVENT_TABLE() |
| 1114 | |
| 1115 | bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id, |
| 1116 | const wxPoint& pos, |
| 1117 | const wxSize& size, |
| 1118 | long style) |
| 1119 | { |
| 1120 | m_dirCtrl = parent; |
| 1121 | return wxChoice::Create(parent, id, pos, size, 0, NULL, style); |
| 1122 | } |
| 1123 | |
| 1124 | void wxDirFilterListCtrl::Init() |
| 1125 | { |
| 1126 | m_dirCtrl = NULL; |
| 1127 | } |
| 1128 | |
| 1129 | void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& event) |
| 1130 | { |
| 1131 | int sel = GetSelection(); |
| 1132 | |
| 1133 | wxString currentPath = m_dirCtrl->GetPath(); |
| 1134 | |
| 1135 | m_dirCtrl->SetFilterIndex(sel); |
| 1136 | |
| 1137 | // If the filter has changed, the view is out of date, so |
| 1138 | // collapse the tree. |
| 1139 | m_dirCtrl->GetTreeCtrl()->Collapse(m_dirCtrl->GetRootId()); |
| 1140 | m_dirCtrl->GetTreeCtrl()->Expand(m_dirCtrl->GetRootId()); |
| 1141 | |
| 1142 | // Try to restore the selection, or at least the directory |
| 1143 | m_dirCtrl->ExpandPath(currentPath); |
| 1144 | } |
| 1145 | |
| 1146 | void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter) |
| 1147 | { |
| 1148 | Clear(); |
| 1149 | wxArrayString descriptions, filters; |
| 1150 | size_t n = (size_t) m_dirCtrl->ParseFilter(filter, filters, descriptions); |
| 1151 | |
| 1152 | if (n > 0 && defaultFilter < (int) n) |
| 1153 | { |
| 1154 | size_t i = 0; |
| 1155 | for (i = 0; i < n; i++) |
| 1156 | Append(descriptions[i]); |
| 1157 | SetSelection(defaultFilter); |
| 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | // wxGenericDirDialog implementation |
| 1162 | // This should be moved into dirdlgg.cpp eventually |
| 1163 | |
| 1164 | BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog) |
| 1165 | EVT_BUTTON(wxID_OK, wxGenericDirDialog::OnOK) |
| 1166 | EVT_BUTTON(wxID_NEW, wxGenericDirDialog::OnNew) |
| 1167 | EVT_BUTTON (wxID_NEW, wxGenericDirDialog::OnNew) |
| 1168 | EVT_CLOSE(wxGenericDirDialog::OnCloseWindow) |
| 1169 | EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown) |
| 1170 | EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected) |
| 1171 | EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK) |
| 1172 | END_EVENT_TABLE() |
| 1173 | |
| 1174 | wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title, |
| 1175 | const wxString& defaultPath, long style, const wxPoint& pos, const wxSize& sz, const wxString& name): |
| 1176 | wxDialog(parent, ID_DIRCTRL, title, pos, sz, style, name) |
| 1177 | { |
| 1178 | m_dirCtrl = NULL; |
| 1179 | m_path = defaultPath; |
| 1180 | |
| 1181 | wxBusyCursor cursor; |
| 1182 | |
| 1183 | wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL ); |
| 1184 | |
| 1185 | // 1) dir ctrl |
| 1186 | m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL, |
| 1187 | defaultPath, wxPoint(5, 5), |
| 1188 | wxSize(300, 200), wxDIRCTRL_DIR_ONLY|wxSUNKEN_BORDER); |
| 1189 | |
| 1190 | topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 ); |
| 1191 | |
| 1192 | // 2) text ctrl |
| 1193 | m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition ); |
| 1194 | topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 ); |
| 1195 | |
| 1196 | #if wxUSE_STATLINE |
| 1197 | // 3) Static line |
| 1198 | topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 ); |
| 1199 | #endif |
| 1200 | |
| 1201 | // 4) Buttons |
| 1202 | wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL ); |
| 1203 | wxButton* okButton = new wxButton(this, wxID_OK, _("OK")); |
| 1204 | buttonsizer->Add( okButton, 0, wxLEFT|wxRIGHT, 10 ); |
| 1205 | wxButton* cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel")); |
| 1206 | buttonsizer->Add( cancelButton, 0, wxLEFT|wxRIGHT, 10 ); |
| 1207 | |
| 1208 | // I'm not convinced we need a New button, and we tend to get annoying |
| 1209 | // accidental-editing with label editing enabled. |
| 1210 | #if 0 |
| 1211 | wxButton* newButton = new wxButton( this, wxID_NEW, _("New...") ); |
| 1212 | buttonsizer->Add( newButton, 0, wxLEFT|wxRIGHT, 10 ); |
| 1213 | #endif |
| 1214 | |
| 1215 | topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 ); |
| 1216 | |
| 1217 | okButton->SetDefault(); |
| 1218 | m_dirCtrl->SetFocus(); |
| 1219 | |
| 1220 | SetAutoLayout( TRUE ); |
| 1221 | SetSizer( topsizer ); |
| 1222 | |
| 1223 | topsizer->SetSizeHints( this ); |
| 1224 | topsizer->Fit( this ); |
| 1225 | |
| 1226 | Centre( wxBOTH ); |
| 1227 | } |
| 1228 | |
| 1229 | void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& event) |
| 1230 | { |
| 1231 | EndModal(wxID_CANCEL); |
| 1232 | } |
| 1233 | |
| 1234 | void wxGenericDirDialog::OnOK(wxCommandEvent& event) |
| 1235 | { |
| 1236 | m_path = m_input->GetValue(); |
| 1237 | // Does the path exist? (User may have typed anything in m_input) |
| 1238 | if (wxPathExists(m_path)) { |
| 1239 | // OK, path exists, we're done. |
| 1240 | EndModal(wxID_OK); |
| 1241 | return; |
| 1242 | } |
| 1243 | // Interact with user, find out if the dir is a typo or to be created |
| 1244 | wxString msg( _("The directory ") ); |
| 1245 | msg = msg + m_path; |
| 1246 | msg = msg + _("\ndoes not exist\nCreate it now?") ; |
| 1247 | wxMessageDialog dialog(this, msg, _("Directory does not exist"), wxYES_NO | wxICON_WARNING ); |
| 1248 | if ( dialog.ShowModal() == wxID_YES ) { |
| 1249 | // Okay, let's make it |
| 1250 | wxLogNull log; |
| 1251 | if (wxMkdir(m_path)) { |
| 1252 | // The new dir was created okay. |
| 1253 | EndModal(wxID_OK); |
| 1254 | return; |
| 1255 | } |
| 1256 | else { |
| 1257 | // Trouble... |
| 1258 | msg = _("Failed to create directory ")+m_path+ |
| 1259 | _("\n(Do you have the required permissions?)"); |
| 1260 | wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR); |
| 1261 | errmsg.ShowModal(); |
| 1262 | // We still don't have a valid dir. Back to the main dialog. |
| 1263 | } |
| 1264 | } |
| 1265 | // User has answered NO to create dir. |
| 1266 | } |
| 1267 | |
| 1268 | void wxGenericDirDialog::SetPath(const wxString& path) |
| 1269 | { |
| 1270 | m_dirCtrl->SetPath(path); |
| 1271 | m_path = path; |
| 1272 | } |
| 1273 | |
| 1274 | wxString wxGenericDirDialog::GetPath(void) const |
| 1275 | { |
| 1276 | return m_path; |
| 1277 | } |
| 1278 | |
| 1279 | int wxGenericDirDialog::ShowModal() |
| 1280 | { |
| 1281 | //m_input->SetValue( m_path ); |
| 1282 | return wxDialog::ShowModal(); |
| 1283 | } |
| 1284 | |
| 1285 | void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event ) |
| 1286 | { |
| 1287 | if (!m_dirCtrl) |
| 1288 | return; |
| 1289 | |
| 1290 | wxDirItemDataEx *data = (wxDirItemDataEx*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem()); |
| 1291 | if (data) |
| 1292 | m_input->SetValue( data->m_path ); |
| 1293 | }; |
| 1294 | |
| 1295 | void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) ) |
| 1296 | { |
| 1297 | if (!m_dirCtrl) |
| 1298 | return; |
| 1299 | |
| 1300 | wxDirItemDataEx *data = (wxDirItemDataEx*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection()); |
| 1301 | if (data) |
| 1302 | m_input->SetValue( data->m_path ); |
| 1303 | }; |
| 1304 | |
| 1305 | void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) ) |
| 1306 | { |
| 1307 | wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection(); |
| 1308 | if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) || |
| 1309 | (m_dirCtrl->GetTreeCtrl()->GetParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem())) |
| 1310 | { |
| 1311 | wxMessageDialog msg(this, _("You cannot add a new directory to this section."), |
| 1312 | _("Create directory"), wxOK | wxICON_INFORMATION ); |
| 1313 | msg.ShowModal(); |
| 1314 | return; |
| 1315 | } |
| 1316 | |
| 1317 | wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetParent( id ); |
| 1318 | wxDirItemDataEx *data = (wxDirItemDataEx*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent ); |
| 1319 | wxASSERT( data ); |
| 1320 | |
| 1321 | wxString new_name( wxT("NewName") ); |
| 1322 | wxString path( data->m_path ); |
| 1323 | if (path.Last() != wxFILE_SEP_PATH) |
| 1324 | path += wxFILE_SEP_PATH; |
| 1325 | path += new_name; |
| 1326 | if (wxFileExists(path)) |
| 1327 | { |
| 1328 | // try NewName0, NewName1 etc. |
| 1329 | int i = 0; |
| 1330 | do { |
| 1331 | new_name = wxT("NewName"); |
| 1332 | wxString num; |
| 1333 | num.Printf( wxT("%d"), i ); |
| 1334 | new_name += num; |
| 1335 | |
| 1336 | path = data->m_path; |
| 1337 | if (path.Last() != wxFILE_SEP_PATH) |
| 1338 | path += wxFILE_SEP_PATH; |
| 1339 | path += new_name; |
| 1340 | i++; |
| 1341 | } while (wxFileExists(path)); |
| 1342 | } |
| 1343 | |
| 1344 | wxLogNull log; |
| 1345 | if (!wxMkdir(path)) |
| 1346 | { |
| 1347 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); |
| 1348 | dialog.ShowModal(); |
| 1349 | return; |
| 1350 | } |
| 1351 | |
| 1352 | wxDirItemDataEx *new_data = new wxDirItemDataEx( path, new_name, TRUE ); |
| 1353 | |
| 1354 | // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child |
| 1355 | // of the parent. |
| 1356 | wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data ); |
| 1357 | m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id ); |
| 1358 | m_dirCtrl->GetTreeCtrl()->EditLabel( new_id ); |
| 1359 | } |