]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
no (real) changes
[wxWidgets.git] / src / generic / dirctrlg.cpp
CommitLineData
51a58d8b
JS
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__
3fa4bd0e 13#pragma implementation "dirctrlg.h"
51a58d8b
JS
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
1e6feb95 23#if wxUSE_DIRDLG
51a58d8b
JS
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"
618a5e38 30#include "wx/textctrl.h"
51a58d8b
JS
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"
3fa4bd0e 42#include "wx/settings.h"
51a58d8b
JS
43
44#if wxUSE_STATLINE
45 #include "wx/statline.h"
46#endif
47
48#include "wx/generic/dirctrlg.h"
49
76a5e5d2
SC
50#if defined(__WXMAC__)
51 #include "wx/mac/private.h" // includes mac headers
52#endif
53
51a58d8b 54#ifdef __WXMSW__
2736b3ce 55#include <windows.h>
7328394a 56
c9ecda60
VS
57// FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
58// older releases don't, but it should be verified and the checks modified
59// accordingly.
60#if !defined(__GNUWIN32__) || \
61 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
62 #include <direct.h>
63 #include <stdlib.h>
64 #include <ctype.h>
51a58d8b
JS
65#endif
66
7328394a
JS
67#endif
68
ec1b28a3
DW
69#ifdef __WXPM__
70
71#define INCL_BASE
72#include <os2.h>
73#include <direct.h>
74#include <stdlib.h>
75#include <ctype.h>
76
77#endif // __WXPM__
78
f4ac0693
GD
79#if defined(__WXMAC__)
80# include "MoreFilesExtras.h"
bedaf53e
SC
81#endif
82
267a7108
JS
83#ifdef __BORLANDC__
84#include "dos.h"
85#endif
86
51a58d8b
JS
87// If compiled under Windows, this macro can cause problems
88#ifdef GetFirstChild
89#undef GetFirstChild
90#endif
91
51a58d8b
JS
92/* Closed folder */
93static char * icon1_xpm[] = {
94/* width height ncolors chars_per_pixel */
95"16 16 6 1",
96/* colors */
97" s None c None",
98". c #000000",
99"+ c #c0c0c0",
100"@ c #808080",
101"# c #ffff00",
102"$ c #ffffff",
103/* pixels */
104" ",
105" @@@@@ ",
106" @#+#+#@ ",
107" @#+#+#+#@@@@@@ ",
108" @$$$$$$$$$$$$@.",
109" @$#+#+#+#+#+#@.",
110" @$+#+#+#+#+#+@.",
111" @$#+#+#+#+#+#@.",
112" @$+#+#+#+#+#+@.",
113" @$#+#+#+#+#+#@.",
114" @$+#+#+#+#+#+@.",
115" @$#+#+#+#+#+#@.",
116" @@@@@@@@@@@@@@.",
117" ..............",
118" ",
119" "};
120
121/* Open folder */
122static char * icon2_xpm[] = {
123/* width height ncolors chars_per_pixel */
124"16 16 6 1",
125/* colors */
126" s None c None",
127". c #000000",
128"+ c #c0c0c0",
129"@ c #808080",
130"# c #ffff00",
131"$ c #ffffff",
132/* pixels */
133" ",
134" @@@@@ ",
135" @$$$$$@ ",
136" @$#+#+#$@@@@@@ ",
137" @$+#+#+$$$$$$@.",
138" @$#+#+#+#+#+#@.",
139"@@@@@@@@@@@@@#@.",
140"@$$$$$$$$$$@@+@.",
141"@$#+#+#+#+##.@@.",
142" @$#+#+#+#+#+.@.",
143" @$+#+#+#+#+#.@.",
144" @$+#+#+#+##@..",
145" @@@@@@@@@@@@@.",
146" .............",
147" ",
148" "};
149
150/* File */
151static char * icon3_xpm[] = {
152/* width height ncolors chars_per_pixel */
153"16 16 3 1",
154/* colors */
33fed835
MB
155" s None c None",
156". c #000000",
157"+ c #ffffff",
51a58d8b
JS
158/* pixels */
159" ",
160" ........ ",
161" .++++++.. ",
162" .+.+.++.+. ",
163" .++++++.... ",
164" .+.+.+++++. ",
165" .+++++++++. ",
166" .+.+.+.+.+. ",
167" .+++++++++. ",
168" .+.+.+.+.+. ",
169" .+++++++++. ",
170" .+.+.+.+.+. ",
171" .+++++++++. ",
172" ........... ",
173" ",
174" "};
175
176/* Computer */
177static char * icon4_xpm[] = {
178"16 16 7 1",
33fed835
MB
179" s None c None",
180". c #808080",
181"X c #c0c0c0",
182"o c Black",
183"O c Gray100",
184"+ c #008080",
185"@ c Blue",
51a58d8b
JS
186" ........... ",
187" .XXXXXXXXXX.o",
188" .OOOOOOOOO..o",
189" .OoooooooX..o",
190" .Oo+...@+X..o",
191" .Oo+XXX.+X..o",
192" .Oo+....+X..o",
193" .Oo++++++X..o",
194" .OXXXXXXXX.oo",
195" ..........o.o",
196" ...........Xo",
197" .XXXXXXXXXX.o",
198" .o.o.o.o.o...o",
199" .oXoXoXoXoXo.o ",
200".XOXXXXXXXXX.o ",
201"............o "};
202
203/* Drive */
204static char * icon5_xpm[] = {
205"16 16 7 1",
33fed835
MB
206" s None c None",
207". c #808080",
208"X c #c0c0c0",
209"o c Black",
210"O c Gray100",
211"+ c Green",
212"@ c #008000",
51a58d8b
JS
213" ",
214" ",
215" ",
216" ",
217" ............. ",
218" .XXXXXXXXXXXX.o",
219".OOOOOOOOOOOO..o",
220".XXXXXXXXX+@X..o",
221".XXXXXXXXXXXX..o",
222".X..........X..o",
223".XOOOOOOOOOOX..o",
224"..............o ",
225" ooooooooooooo ",
226" ",
227" ",
228" "};
229
230/* CD-ROM */
231static char *icon6_xpm[] = {
232"16 16 10 1",
33fed835
MB
233" s None c None",
234". c #808080",
235"X c #c0c0c0",
236"o c Yellow",
237"O c Blue",
238"+ c Black",
239"@ c Gray100",
240"# c #008080",
241"$ c Green",
242"% c #008000",
51a58d8b
JS
243" ... ",
244" ..XoX.. ",
245" .O.XoXXX+ ",
246" ...O.oXXXX+ ",
247" .O..X.XXXX+ ",
248" ....X.+..XXX+",
249" .XXX.+@+.XXX+",
250" .X@XX.+.X@@X+",
251" .....X...#XX@+ ",
252".@@@...XXo.O@X+ ",
253".@XXX..XXoXOO+ ",
254".@++++..XoX+++ ",
255".@$%@@XX+++X.+ ",
256".............+ ",
257" ++++++++++++ ",
258" "};
259
260/* Floppy */
261static char * icon7_xpm[] = {
262"16 16 7 1",
33fed835
MB
263" s None c None",
264". c #808080",
265"X c Gray100",
266"o c #c0c0c0",
267"O c Black",
268"+ c Cyan",
269"@ c Red",
51a58d8b
JS
270" ......X",
271" .ooooooO",
272" .+++++OO",
273" .++++++O",
274" .++++++O",
275" .ooooooO",
276" .......o....oO",
277" .oooooo.o.O.XoO",
278".XXXXXXXXOOOOOO ",
279".ooooooooo@o..O ",
280".ooo....oooo..O ",
281".o..OOOO...o..O ",
282".oooXXXXoooo..O ",
283".............O ",
284" OOOOOOOOOOOO ",
285" "};
286
287/* Removeable */
288static char * icon8_xpm[] = {
289"16 16 7 1",
33fed835
MB
290" s None c None",
291". c #808080",
292"X c #c0c0c0",
293"o c Black",
294"O c Gray100",
295"+ c Red",
296"@ c #800000",
51a58d8b
JS
297" ",
298" ",
299" ",
300" ............. ",
301" .XXXXXXXXXXXX.o",
302".OOOOOOOOOOOO..o",
303".OXXXXXXXXXXX..o",
304".O+@.oooooo.X..o",
305".OXXOooooooOX..o",
306".OXXXOOOOOOXX..o",
307".OXXXXXXXXXXX..o",
308".O............o ",
309" ooooooooooooo ",
310" ",
311" ",
312" "};
51a58d8b 313
748fcded
VS
314
315#define wxID_TREECTRL 7000
316#define wxID_FILTERLISTCTRL 7001
317
318#if defined(__DOS__)
db5333a5
VS
319
320static bool wxIsDriveAvailable(const wxString dirName)
321{
322 if ( dirName.Len() == 3 && dirName[1u] == wxT(':') )
323 {
324 wxString dirNameLower(dirName.Lower());
325 // VS: always return TRUE for removable media, since Win95 doesn't
326 // like it when MS-DOS app accesses empty floppy drive
327 return (dirNameLower[0u] == wxT('a') ||
328 dirNameLower[0u] == wxT('b') ||
329 wxPathExists(dirNameLower));
330 }
331 else
332 return TRUE;
333}
334
335#elif defined(__WINDOWS__) || defined(__WXPM__)
336
33ac7e6f
KB
337int setdrive(int drive)
338{
c9ecda60
VS
339#if defined(__GNUWIN32__) && \
340 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
341 return _chdrive(drive);
342#else
e987a489 343 wxChar newdrive[3];
33ac7e6f
KB
344
345 if (drive < 1 || drive > 31)
346 return -1;
e987a489
VS
347 newdrive[0] = (wxChar)(wxT('A') + drive - 1);
348 newdrive[1] = wxT(':');
349 newdrive[2] = wxT('\0');
50c76ce1 350#if defined(__WXMSW__)
788722ac
JS
351#ifdef __WIN16__
352 if (wxSetWorkingDirectory(newdrive))
353#else
e987a489 354 if (::SetCurrentDirectory(newdrive))
788722ac 355#endif
50c76ce1
DW
356#else
357 // VA doesn't know what LPSTR is and has its own set
358 if (DosSetCurrentDir((PSZ)newdrive))
359#endif
33ac7e6f
KB
360 return 0;
361 else
362 return -1;
c9ecda60 363#endif // !GNUWIN32
33ac7e6f
KB
364}
365
ad9cd15c
JS
366static bool wxIsDriveAvailable(const wxString dirName)
367{
368#ifdef __WIN32__
33fed835 369 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
ad9cd15c 370#endif
33fed835 371 bool success = TRUE;
ad9cd15c
JS
372
373 // Check if this is a root directory and if so,
374 // whether the drive is avaiable.
33fed835 375 if (dirName.Len() == 3 && dirName[(size_t)1] == wxT(':'))
ad9cd15c 376 {
33fed835 377 wxString dirNameLower(dirName.Lower());
c9ecda60
VS
378#if defined(__GNUWIN32__) && \
379 !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
7328394a
JS
380 success = wxPathExists(dirNameLower);
381#else
ad9cd15c 382 int currentDrive = _getdrive();
33fed835 383 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
33ac7e6f
KB
384 int err = setdrive( thisDrive ) ;
385 setdrive( currentDrive );
ad9cd15c
JS
386
387 if (err == -1)
388 {
389 success = FALSE;
390 }
7328394a 391#endif
ad9cd15c
JS
392 }
393#ifdef __WIN32__
33fed835 394 (void) SetErrorMode(errorMode);
ad9cd15c
JS
395#endif
396
33fed835 397 return success;
ad9cd15c 398}
db5333a5 399#endif // __WINDOWS__ || __WXPM__
ad9cd15c 400
57e26a09
JS
401// Function which is called by quick sort. We want to override the default wxArrayString behaviour,
402// and sort regardless of case.
403static int LINKAGEMODE wxDirCtrlStringCompareFunction(const void *first, const void *second)
404{
405 wxString *strFirst = (wxString *)first;
406 wxString *strSecond = (wxString *)second;
407
408 return strFirst->CmpNoCase(*strSecond);
409}
410
51a58d8b 411//-----------------------------------------------------------------------------
748fcded 412// wxDirItemData
51a58d8b
JS
413//-----------------------------------------------------------------------------
414
748fcded
VS
415wxDirItemData::wxDirItemData(const wxString& path, const wxString& name,
416 bool isDir)
51a58d8b
JS
417{
418 m_path = path;
419 m_name = name;
420 /* Insert logic to detect hidden files here
421 * In UnixLand we just check whether the first char is a dot
422 * For FileNameFromPath read LastDirNameInThisPath ;-) */
423 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
424 m_isHidden = FALSE;
51a58d8b
JS
425 m_isExpanded = FALSE;
426 m_isDir = isDir;
427}
428
748fcded 429wxDirItemData::~wxDirItemData()
51a58d8b
JS
430{
431}
432
748fcded 433void wxDirItemData::SetNewDirName(const wxString& path)
51a58d8b
JS
434{
435 m_path = path;
748fcded
VS
436 m_name = wxFileNameFromPath(path);
437}
438
439bool wxDirItemData::HasSubDirs() const
440{
441 if (m_path.IsEmpty())
442 return FALSE;
443
444 wxDir dir;
445 {
446 wxLogNull nolog;
447 if ( !dir.Open(m_path) )
448 return FALSE;
449 }
450
451 return dir.HasSubDirs();
452}
453
454bool wxDirItemData::HasFiles(const wxString& spec) const
455{
456 if (m_path.IsEmpty())
457 return FALSE;
458
459 wxDir dir;
460 {
461 wxLogNull nolog;
462 if ( !dir.Open(m_path) )
463 return FALSE;
464 }
465
466 return dir.HasFiles();
51a58d8b
JS
467}
468
51a58d8b
JS
469//-----------------------------------------------------------------------------
470// wxGenericDirCtrl
471//-----------------------------------------------------------------------------
472
473IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
474
475BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
476 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem)
477 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem)
478 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem)
479 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem)
480 EVT_SIZE (wxGenericDirCtrl::OnSize)
481END_EVENT_TABLE()
482
483wxGenericDirCtrl::wxGenericDirCtrl(void)
484{
485 Init();
486}
487
488bool wxGenericDirCtrl::Create(wxWindow *parent,
748fcded
VS
489 const wxWindowID id,
490 const wxString& dir,
491 const wxPoint& pos,
492 const wxSize& size,
493 long style,
494 const wxString& filter,
495 int defaultFilter,
496 const wxString& name)
51a58d8b
JS
497{
498 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
499 return FALSE;
500
db5333a5 501 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
51a58d8b
JS
502
503 Init();
504
748fcded 505 long treeStyle = wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS | wxTR_HIDE_ROOT;
51a58d8b
JS
506 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
507 treeStyle |= wxNO_BORDER;
508
509 long filterStyle = 0;
510 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
511 filterStyle |= wxNO_BORDER;
512
513 m_treeCtrl = new wxTreeCtrl(this, wxID_TREECTRL, pos, size, treeStyle);
514
515 if (!filter.IsEmpty() && (style & wxDIRCTRL_SHOW_FILTERS))
516 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
517
518 m_defaultPath = dir;
519 m_filter = filter;
520
521 SetFilterIndex(defaultFilter);
522
523 if (m_filterListCtrl)
524 m_filterListCtrl->FillFilterList(filter, defaultFilter);
525
526 m_imageList = new wxImageList(16, 16, TRUE);
51a58d8b
JS
527 m_imageList->Add(wxIcon(icon1_xpm));
528 m_imageList->Add(wxIcon(icon2_xpm));
529 m_imageList->Add(wxIcon(icon3_xpm));
530 m_imageList->Add(wxIcon(icon4_xpm));
531 m_imageList->Add(wxIcon(icon5_xpm));
532 m_imageList->Add(wxIcon(icon6_xpm));
533 m_imageList->Add(wxIcon(icon7_xpm));
534 m_imageList->Add(wxIcon(icon8_xpm));
748fcded 535 m_treeCtrl->AssignImageList(m_imageList);
51a58d8b
JS
536
537 m_showHidden = FALSE;
748fcded 538 wxDirItemData* rootData = new wxDirItemData(wxT(""), wxT(""), TRUE);
51a58d8b
JS
539
540 wxString rootName;
541
db5333a5 542#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
51a58d8b
JS
543 rootName = _("Computer");
544#else
545 rootName = _("Sections");
546#endif
547
548 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
549 m_treeCtrl->SetItemHasChildren(m_rootId);
550 m_treeCtrl->Expand(m_rootId); // automatically expand first level
551
552 // Expand and select the default path
553 if (!m_defaultPath.IsEmpty())
554 ExpandPath(m_defaultPath);
555
556 DoResize();
557
558 return TRUE;
559}
560
561wxGenericDirCtrl::~wxGenericDirCtrl()
562{
51a58d8b
JS
563}
564
565void wxGenericDirCtrl::Init()
566{
567 m_showHidden = FALSE;
568 m_imageList = NULL;
51a58d8b
JS
569 m_currentFilter = 0;
570 m_currentFilterStr = wxEmptyString; // Default: any file
571 m_treeCtrl = NULL;
572 m_filterListCtrl = NULL;
573}
574
575void wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
576{
748fcded 577 wxDirItemData *dir_item = new wxDirItemData(path,name,TRUE);
51a58d8b 578
51a58d8b 579 wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, imageId, -1, dir_item);
51a58d8b
JS
580
581 m_treeCtrl->SetItemHasChildren(id);
582}
583
584void wxGenericDirCtrl::SetupSections()
585{
db5333a5 586#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b 587
748fcded 588#ifdef __WIN32__
51a58d8b
JS
589 wxChar driveBuffer[256];
590 size_t n = (size_t) GetLogicalDriveStrings(255, driveBuffer);
591 size_t i = 0;
592 while (i < n)
593 {
594 wxString path, name;
595 path.Printf(wxT("%c:\\"), driveBuffer[i]);
596 name.Printf(wxT("(%c:)"), driveBuffer[i]);
597
598 int imageId = 4;
599 int driveType = ::GetDriveType(path);
600 switch (driveType)
601 {
602 case DRIVE_REMOVABLE:
603 if (path == wxT("a:\\") || path == wxT("b:\\"))
604 imageId = 6; // Floppy
605 else
606 imageId = 7;
607 break;
608 case DRIVE_FIXED:
609 imageId = 4;
610 break;
611 case DRIVE_REMOTE:
612 imageId = 4;
613 break;
614 case DRIVE_CDROM:
615 imageId = 5;
616 break;
617 default:
618 imageId = 4;
619 break;
620 }
621
622 AddSection(path, name, imageId);
623
624 while (driveBuffer[i] != wxT('\0'))
625 i ++;
626 i ++;
627 if (driveBuffer[i] == wxT('\0'))
628 break;
629 }
db5333a5 630#else // !__WIN32__
51a58d8b 631 int drive;
51a58d8b 632
51a58d8b
JS
633 /* If we can switch to the drive, it exists. */
634 for( drive = 1; drive <= 26; drive++ )
635 {
636 wxString path, name;
637 path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1));
db5333a5 638 name.Printf(wxT("(%c:)"), (char) (drive + 'A' - 1));
51a58d8b 639
33fed835 640 if (wxIsDriveAvailable(path))
51a58d8b 641 {
db5333a5 642 AddSection(path, name, (drive <= 2) ? 6/*floppy*/ : 4/*disk*/);
51a58d8b
JS
643 }
644 }
db5333a5
VS
645#endif // __WIN32__/!__WIN32__
646
f4ac0693 647#elif defined(__WXMAC__)
bedaf53e
SC
648 FSSpec volume ;
649 short index = 1 ;
650 while(1) {
651 short actualCount = 0 ;
652 if ( OnLine( &volume , 1 , &actualCount , &index ) != noErr || actualCount == 0 )
653 break ;
654
655 wxString name = wxMacFSSpec2MacFilename( &volume ) ;
a044cb72 656 AddSection(name + wxFILE_SEP_PATH, name, 0);
bedaf53e 657 }
db5333a5 658#elif defined(__UNIX__)
748fcded 659 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
db5333a5
VS
660#else
661 #error "Unsupported platform in wxGenericDirCtrl!"
51a58d8b
JS
662#endif
663}
664
665void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
666{
667 // don't rename the main entry "Sections"
668 if (event.GetItem() == m_rootId)
669 {
670 event.Veto();
671 return;
672 }
673
674 // don't rename the individual sections
675 if (m_treeCtrl->GetParent( event.GetItem() ) == m_rootId)
676 {
677 event.Veto();
678 return;
679 }
680}
681
682void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
683{
684 if ((event.GetLabel().IsEmpty()) ||
685 (event.GetLabel() == _(".")) ||
686 (event.GetLabel() == _("..")) ||
687 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
688 {
689 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
690 dialog.ShowModal();
691 event.Veto();
692 return;
693 }
694
695 wxTreeItemId id = event.GetItem();
748fcded 696 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
51a58d8b
JS
697 wxASSERT( data );
698
699 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 700 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
701 new_name += event.GetLabel();
702
703 wxLogNull log;
704
705 if (wxFileExists(new_name))
706 {
707 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
708 dialog.ShowModal();
709 event.Veto();
710 }
711
712 if (wxRenameFile(data->m_path,new_name))
713 {
714 data->SetNewDirName( new_name );
715 }
716 else
717 {
718 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
719 dialog.ShowModal();
720 event.Veto();
721 }
722}
723
724void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
725{
726 wxTreeItemId parentId = event.GetItem();
727
748fcded
VS
728 // VS: this is needed because the event handler is called from wxTreeCtrl
729 // ctor when wxTR_HIDE_ROOT was specified
730 if (m_rootId == 0)
731 m_rootId = m_treeCtrl->GetRootItem();
732
51a58d8b
JS
733 ExpandDir(parentId);
734}
735
736void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
737{
738 wxTreeItemId child, parent = event.GetItem();
739
748fcded 740 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(event.GetItem());
51a58d8b
JS
741 if (!data->m_isExpanded)
742 return;
743
744 data->m_isExpanded = FALSE;
745 long cookie;
746 /* Workaround because DeleteChildren has disapeared (why?) and
747 * CollapseAndReset doesn't work as advertised (deletes parent too) */
748 child = m_treeCtrl->GetFirstChild(parent, cookie);
749 while (child.IsOk())
750 {
751 m_treeCtrl->Delete(child);
752 /* Not GetNextChild below, because the cookie mechanism can't
753 * handle disappearing children! */
754 child = m_treeCtrl->GetFirstChild(parent, cookie);
755 }
756}
757
758void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
759{
748fcded 760 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
761
762 if (data->m_isExpanded)
763 return;
764
765 data->m_isExpanded = TRUE;
766
748fcded 767 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
768 {
769 SetupSections();
770 return;
771 }
772
773 wxASSERT(data);
774
775 wxString search,path,filename;
776
777 wxString dirName(data->m_path);
778
db5333a5 779#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
780 // Check if this is a root directory and if so,
781 // whether the drive is avaiable.
33fed835
MB
782 if (!wxIsDriveAvailable(dirName))
783 {
ad9cd15c 784 data->m_isExpanded = FALSE;
7328394a
JS
785 //wxMessageBox(wxT("Sorry, this drive is not available."));
786 return;
33fed835 787 }
51a58d8b
JS
788#endif
789
790 // This may take a longish time. Go to busy cursor
791 wxBusyCursor busy;
792
db5333a5 793#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
794 if (dirName.Last() == ':')
795 dirName += wxString(wxFILE_SEP_PATH);
796#endif
797
798 wxArrayString dirs;
799 wxArrayString filenames;
800
801 wxDir d;
802 wxString eachFilename;
803
f9c165b1 804 wxLogNull log;
51a58d8b
JS
805 d.Open(dirName);
806
807 if (d.IsOpened())
808 {
748fcded 809 if (d.GetFirst(& eachFilename, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN))
51a58d8b
JS
810 {
811 do
812 {
813 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
814 {
815 dirs.Add(eachFilename);
816 }
817 }
748fcded 818 while (d.GetNext(& eachFilename));
51a58d8b
JS
819 }
820 }
57e26a09 821 dirs.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
51a58d8b
JS
822
823 // Now do the filenames -- but only if we're allowed to
824 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
825 {
f9c165b1
JS
826 wxLogNull log;
827
51a58d8b 828 d.Open(dirName);
ec1b28a3 829
51a58d8b
JS
830 if (d.IsOpened())
831 {
832 if (d.GetFirst(& eachFilename, m_currentFilterStr, wxDIR_FILES))
833 {
834 do
835 {
836 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
837 {
838 filenames.Add(eachFilename);
839 }
840 }
748fcded 841 while (d.GetNext(& eachFilename));
51a58d8b
JS
842 }
843 }
57e26a09 844 filenames.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
51a58d8b
JS
845 }
846
847 // Add the sorted dirs
848 size_t i;
849 for (i = 0; i < dirs.Count(); i++)
850 {
851 wxString eachFilename(dirs[i]);
852 path = dirName;
853 if (path.Last() != wxFILE_SEP_PATH)
854 path += wxString(wxFILE_SEP_PATH);
855 path += eachFilename;
856
748fcded 857 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,TRUE);
51a58d8b
JS
858 wxTreeItemId id = m_treeCtrl->AppendItem( parentId, eachFilename, 0, -1, dir_item);
859 m_treeCtrl->SetItemImage( id, 1, wxTreeItemIcon_Expanded );
ec1b28a3 860
51a58d8b 861 // Has this got any children? If so, make it expandable.
748fcded
VS
862 // (There are two situations when a dir has children: either it
863 // has subdirectories or it contains files that weren't filtered
864 // out. The latter only applies to dirctrl with files.)
865 if ( dir_item->HasSubDirs() ||
866 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
867 dir_item->HasFiles(m_currentFilterStr)) )
51a58d8b 868 {
748fcded 869 m_treeCtrl->SetItemHasChildren(id);
51a58d8b 870 }
51a58d8b
JS
871 }
872
873 // Add the sorted filenames
874 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
875 {
876 for (i = 0; i < filenames.Count(); i++)
877 {
878 wxString eachFilename(filenames[i]);
879 path = dirName;
880 if (path.Last() != wxFILE_SEP_PATH)
881 path += wxString(wxFILE_SEP_PATH);
882 path += eachFilename;
883 //path = dirName + wxString(wxT("/")) + eachFilename;
748fcded 884 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,FALSE);
3fa4bd0e 885 (void)m_treeCtrl->AppendItem( parentId, eachFilename, 2, -1, dir_item);
51a58d8b
JS
886 }
887 }
888}
889
890// Find the child that matches the first part of 'path'.
891// E.g. if a child path is "/usr" and 'path' is "/usr/include"
892// then the child for /usr is returned.
893wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
894{
895 wxString path2(path);
ec1b28a3 896
51a58d8b
JS
897 // Make sure all separators are as per the current platform
898 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
899 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 900
51a58d8b
JS
901 // Append a separator to foil bogus substring matching
902 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
903
904 // In MSW or PM, case is not significant
db5333a5 905#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
906 path2.MakeLower();
907#endif
ec1b28a3 908
51a58d8b
JS
909 long cookie;
910 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 911 while (childId.IsOk())
51a58d8b 912 {
748fcded 913 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 914
748fcded 915 if (data && !data->m_path.IsEmpty())
51a58d8b
JS
916 {
917 wxString childPath(data->m_path);
918 if (childPath.Last() != wxFILE_SEP_PATH)
919 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
920
921 // In MSW and PM, case is not significant
db5333a5 922#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
923 childPath.MakeLower();
924#endif
ec1b28a3 925
51a58d8b
JS
926 if (childPath.Len() <= path2.Len())
927 {
928 wxString path3 = path2.Mid(0, childPath.Len());
929 if (childPath == path3)
930 {
931 if (path3.Len() == path2.Len())
932 done = TRUE;
933 else
934 done = FALSE;
935 return childId;
936 }
937 }
938 }
ec1b28a3 939
748fcded 940 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 941 }
3fa4bd0e
VS
942 wxTreeItemId invalid;
943 return invalid;
51a58d8b
JS
944}
945
946// Try to expand as much of the given path as possible,
947// and select the given tree item.
948bool wxGenericDirCtrl::ExpandPath(const wxString& path)
949{
950 bool done = FALSE;
951 wxTreeItemId id = FindChild(m_rootId, path, done);
952 wxTreeItemId lastId = id; // The last non-zero id
237387ad 953 while (id.IsOk() && !done)
51a58d8b
JS
954 {
955 ExpandDir(id);
956
957 id = FindChild(id, path, done);
53ccf1c0 958 if (id.IsOk())
51a58d8b
JS
959 lastId = id;
960 }
53ccf1c0 961 if (lastId.IsOk())
51a58d8b 962 {
748fcded 963 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
51a58d8b
JS
964 if (data->m_isDir)
965 {
966 m_treeCtrl->Expand(lastId);
967 }
968 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
969 {
970 // Find the first file in this directory
971 long cookie;
972 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
973 bool selectedChild = FALSE;
53ccf1c0 974 while (childId.IsOk())
51a58d8b 975 {
748fcded 976 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 977
51a58d8b
JS
978 if (data && data->m_path != "" && !data->m_isDir)
979 {
980 m_treeCtrl->SelectItem(childId);
981 m_treeCtrl->EnsureVisible(childId);
982 selectedChild = TRUE;
983 break;
984 }
985 childId = m_treeCtrl->GetNextChild(lastId, cookie);
986 }
987 if (!selectedChild)
988 {
989 m_treeCtrl->SelectItem(lastId);
990 m_treeCtrl->EnsureVisible(lastId);
991 }
992 }
993 else
994 {
995 m_treeCtrl->SelectItem(lastId);
996 m_treeCtrl->EnsureVisible(lastId);
997 }
998
999 return TRUE;
1000 }
1001 else
1002 return FALSE;
1003}
1004
1005wxString wxGenericDirCtrl::GetPath() const
1006{
1007 wxTreeItemId id = m_treeCtrl->GetSelection();
1008 if (id)
1009 {
748fcded 1010 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1011 return data->m_path;
1012 }
1013 else
1014 return wxEmptyString;
1015}
1016
1017wxString wxGenericDirCtrl::GetFilePath() const
1018{
1019 wxTreeItemId id = m_treeCtrl->GetSelection();
1020 if (id)
1021 {
748fcded 1022 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1023 if (data->m_isDir)
1024 return wxEmptyString;
1025 else
1026 return data->m_path;
1027 }
1028 else
1029 return wxEmptyString;
1030}
1031
1032void wxGenericDirCtrl::SetPath(const wxString& path)
1033{
1034 m_defaultPath = path;
1035 if (m_rootId)
1036 ExpandPath(path);
1037}
1038
1039// Not used
1040#if 0
1041void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
1042{
748fcded 1043 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1044
1045 // This may take a longish time. Go to busy cursor
1046 wxBusyCursor busy;
1047
1048 wxASSERT(data);
1049
1050 wxString search,path,filename;
1051
1052 wxString dirName(data->m_path);
1053
ec1b28a3 1054#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
1055 if (dirName.Last() == ':')
1056 dirName += wxString(wxFILE_SEP_PATH);
1057#endif
1058
1059 wxDir d;
1060 wxString eachFilename;
1061
f9c165b1 1062 wxLogNull log;
51a58d8b
JS
1063 d.Open(dirName);
1064
1065 if (d.IsOpened())
1066 {
1067 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1068 {
1069 do
1070 {
1071 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1072 {
1073 filenames.Add(eachFilename);
1074 }
1075 }
1076 while (d.GetNext(& eachFilename)) ;
1077 }
1078 }
1079}
1080#endif
1081
1082void wxGenericDirCtrl::SetFilterIndex(int n)
1083{
1084 m_currentFilter = n;
1085
1086 wxString f, d;
1087 if (ExtractWildcard(m_filter, n, f, d))
1088 m_currentFilterStr = f;
1089 else
1090 m_currentFilterStr = wxT("*.*");
1091}
1092
1093void wxGenericDirCtrl::SetFilter(const wxString& filter)
1094{
1095 m_filter = filter;
1096
1097 wxString f, d;
1098 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1099 m_currentFilterStr = f;
1100 else
1101 m_currentFilterStr = wxT("*.*");
1102}
1103
1104// Extract description and actual filter from overall filter string
1105bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1106{
1107 wxArrayString filters, descriptions;
1108 int count = ParseFilter(filterStr, filters, descriptions);
1109 if (count > 0 && n < count)
1110 {
1111 filter = filters[n];
1112 description = descriptions[n];
1113 return TRUE;
1114 }
5716a1ab
VZ
1115
1116 return FALSE;
51a58d8b
JS
1117}
1118
1119// Parses the global filter, returning the number of filters.
1120// Returns 0 if none or if there's a problem.
1121// filterStr is in the form:
1122//
1123// "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1124
1125int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1126{
1127 wxString str(filterStr);
1128
1129 wxString description, filter;
1130 int pos;
1131 bool finished = FALSE;
1132 do
1133 {
1134 pos = str.Find(wxT('|'));
1135 if (pos == -1)
1136 return 0; // Problem
1137 description = str.Left(pos);
1138 str = str.Mid(pos+1);
1139 pos = str.Find(wxT('|'));
1140 if (pos == -1)
1141 {
1142 filter = str;
1143 finished = TRUE;
1144 }
1145 else
1146 {
1147 filter = str.Left(pos);
1148 str = str.Mid(pos+1);
1149 }
1150 descriptions.Add(description);
1151 filters.Add(filter);
1152 }
1153 while (!finished) ;
1154
1155 return filters.Count();
1156}
1157
1158void wxGenericDirCtrl::DoResize()
1159{
1160 wxSize sz = GetClientSize();
1161 int verticalSpacing = 3;
1162 if (m_treeCtrl)
1163 {
1164 wxSize filterSz ;
1165 if (m_filterListCtrl)
1166 {
1167 filterSz = m_filterListCtrl->GetSize();
1168 sz.y -= (filterSz.y + verticalSpacing);
1169 }
1170 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1171 if (m_filterListCtrl)
1172 {
1173 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1174 // Don't know why, but this needs refreshing after a resize (wxMSW)
1175 m_filterListCtrl->Refresh();
1176 }
1177 }
1178}
1179
1180
33ac7e6f 1181void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
51a58d8b
JS
1182{
1183 DoResize();
1184}
1185
1186//-----------------------------------------------------------------------------
1187// wxDirFilterListCtrl
1188//-----------------------------------------------------------------------------
1189
1190IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1191
1192BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1193 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter)
1194END_EVENT_TABLE()
1195
1196bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1197 const wxPoint& pos,
1198 const wxSize& size,
1199 long style)
1200{
1201 m_dirCtrl = parent;
1202 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1203}
1204
1205void wxDirFilterListCtrl::Init()
1206{
1207 m_dirCtrl = NULL;
1208}
1209
33ac7e6f 1210void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
51a58d8b
JS
1211{
1212 int sel = GetSelection();
1213
1214 wxString currentPath = m_dirCtrl->GetPath();
ec1b28a3 1215
51a58d8b
JS
1216 m_dirCtrl->SetFilterIndex(sel);
1217
1218 // If the filter has changed, the view is out of date, so
1219 // collapse the tree.
1220 m_dirCtrl->GetTreeCtrl()->Collapse(m_dirCtrl->GetRootId());
1221 m_dirCtrl->GetTreeCtrl()->Expand(m_dirCtrl->GetRootId());
1222
1223 // Try to restore the selection, or at least the directory
1224 m_dirCtrl->ExpandPath(currentPath);
1225}
1226
1227void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1228{
1229 Clear();
1230 wxArrayString descriptions, filters;
1231 size_t n = (size_t) m_dirCtrl->ParseFilter(filter, filters, descriptions);
1232
1233 if (n > 0 && defaultFilter < (int) n)
1234 {
1235 size_t i = 0;
1236 for (i = 0; i < n; i++)
1237 Append(descriptions[i]);
1238 SetSelection(defaultFilter);
1239 }
1240}
1241
1e6feb95 1242#endif // wxUSE_DIRDLG