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