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