]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
patch from Dimitri fixing crashes when decoding the invalid GIFs
[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__)
319 #ifdef __DJGPP__
320 #define setdrive(drive) setdisk(drive)
321 #endif
322#elif defined(__WXMSW__) || defined(__WXPM__)
33ac7e6f
KB
323int setdrive(int drive)
324{
c9ecda60
VS
325#if defined(__GNUWIN32__) && \
326 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
327 return _chdrive(drive);
328#else
e987a489 329 wxChar newdrive[3];
33ac7e6f
KB
330
331 if (drive < 1 || drive > 31)
332 return -1;
e987a489
VS
333 newdrive[0] = (wxChar)(wxT('A') + drive - 1);
334 newdrive[1] = wxT(':');
335 newdrive[2] = wxT('\0');
50c76ce1 336#if defined(__WXMSW__)
788722ac
JS
337#ifdef __WIN16__
338 if (wxSetWorkingDirectory(newdrive))
339#else
e987a489 340 if (::SetCurrentDirectory(newdrive))
788722ac 341#endif
50c76ce1
DW
342#else
343 // VA doesn't know what LPSTR is and has its own set
344 if (DosSetCurrentDir((PSZ)newdrive))
345#endif
33ac7e6f
KB
346 return 0;
347 else
348 return -1;
c9ecda60 349#endif // !GNUWIN32
33ac7e6f
KB
350}
351
ad9cd15c
JS
352static bool wxIsDriveAvailable(const wxString dirName)
353{
354#ifdef __WIN32__
33fed835 355 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
ad9cd15c 356#endif
33fed835 357 bool success = TRUE;
ad9cd15c
JS
358
359 // Check if this is a root directory and if so,
360 // whether the drive is avaiable.
33fed835 361 if (dirName.Len() == 3 && dirName[(size_t)1] == wxT(':'))
ad9cd15c 362 {
33fed835 363 wxString dirNameLower(dirName.Lower());
c9ecda60
VS
364#if defined(__GNUWIN32__) && \
365 !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
7328394a
JS
366 success = wxPathExists(dirNameLower);
367#else
ad9cd15c 368 int currentDrive = _getdrive();
33fed835 369 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
33ac7e6f
KB
370 int err = setdrive( thisDrive ) ;
371 setdrive( currentDrive );
ad9cd15c
JS
372
373 if (err == -1)
374 {
375 success = FALSE;
376 }
7328394a 377#endif
ad9cd15c
JS
378 }
379#ifdef __WIN32__
33fed835 380 (void) SetErrorMode(errorMode);
ad9cd15c
JS
381#endif
382
33fed835 383 return success;
ad9cd15c
JS
384}
385#endif
386
57e26a09
JS
387// Function which is called by quick sort. We want to override the default wxArrayString behaviour,
388// and sort regardless of case.
389static int LINKAGEMODE wxDirCtrlStringCompareFunction(const void *first, const void *second)
390{
391 wxString *strFirst = (wxString *)first;
392 wxString *strSecond = (wxString *)second;
393
394 return strFirst->CmpNoCase(*strSecond);
395}
396
51a58d8b 397//-----------------------------------------------------------------------------
748fcded 398// wxDirItemData
51a58d8b
JS
399//-----------------------------------------------------------------------------
400
748fcded
VS
401wxDirItemData::wxDirItemData(const wxString& path, const wxString& name,
402 bool isDir)
51a58d8b
JS
403{
404 m_path = path;
405 m_name = name;
406 /* Insert logic to detect hidden files here
407 * In UnixLand we just check whether the first char is a dot
408 * For FileNameFromPath read LastDirNameInThisPath ;-) */
409 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
410 m_isHidden = FALSE;
51a58d8b
JS
411 m_isExpanded = FALSE;
412 m_isDir = isDir;
413}
414
748fcded 415wxDirItemData::~wxDirItemData()
51a58d8b
JS
416{
417}
418
748fcded 419void wxDirItemData::SetNewDirName(const wxString& path)
51a58d8b
JS
420{
421 m_path = path;
748fcded
VS
422 m_name = wxFileNameFromPath(path);
423}
424
425bool wxDirItemData::HasSubDirs() const
426{
427 if (m_path.IsEmpty())
428 return FALSE;
429
430 wxDir dir;
431 {
432 wxLogNull nolog;
433 if ( !dir.Open(m_path) )
434 return FALSE;
435 }
436
437 return dir.HasSubDirs();
438}
439
440bool wxDirItemData::HasFiles(const wxString& spec) const
441{
442 if (m_path.IsEmpty())
443 return FALSE;
444
445 wxDir dir;
446 {
447 wxLogNull nolog;
448 if ( !dir.Open(m_path) )
449 return FALSE;
450 }
451
452 return dir.HasFiles();
51a58d8b
JS
453}
454
51a58d8b
JS
455//-----------------------------------------------------------------------------
456// wxGenericDirCtrl
457//-----------------------------------------------------------------------------
458
459IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
460
461BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
462 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem)
463 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem)
464 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem)
465 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem)
466 EVT_SIZE (wxGenericDirCtrl::OnSize)
467END_EVENT_TABLE()
468
469wxGenericDirCtrl::wxGenericDirCtrl(void)
470{
471 Init();
472}
473
474bool wxGenericDirCtrl::Create(wxWindow *parent,
748fcded
VS
475 const wxWindowID id,
476 const wxString& dir,
477 const wxPoint& pos,
478 const wxSize& size,
479 long style,
480 const wxString& filter,
481 int defaultFilter,
482 const wxString& name)
51a58d8b
JS
483{
484 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
485 return FALSE;
486
487 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
488
489 Init();
490
748fcded 491 long treeStyle = wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS | wxTR_HIDE_ROOT;
51a58d8b
JS
492 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
493 treeStyle |= wxNO_BORDER;
494
495 long filterStyle = 0;
496 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
497 filterStyle |= wxNO_BORDER;
498
499 m_treeCtrl = new wxTreeCtrl(this, wxID_TREECTRL, pos, size, treeStyle);
500
501 if (!filter.IsEmpty() && (style & wxDIRCTRL_SHOW_FILTERS))
502 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
503
504 m_defaultPath = dir;
505 m_filter = filter;
506
507 SetFilterIndex(defaultFilter);
508
509 if (m_filterListCtrl)
510 m_filterListCtrl->FillFilterList(filter, defaultFilter);
511
512 m_imageList = new wxImageList(16, 16, TRUE);
51a58d8b
JS
513 m_imageList->Add(wxIcon(icon1_xpm));
514 m_imageList->Add(wxIcon(icon2_xpm));
515 m_imageList->Add(wxIcon(icon3_xpm));
516 m_imageList->Add(wxIcon(icon4_xpm));
517 m_imageList->Add(wxIcon(icon5_xpm));
518 m_imageList->Add(wxIcon(icon6_xpm));
519 m_imageList->Add(wxIcon(icon7_xpm));
520 m_imageList->Add(wxIcon(icon8_xpm));
748fcded 521 m_treeCtrl->AssignImageList(m_imageList);
51a58d8b
JS
522
523 m_showHidden = FALSE;
748fcded 524 wxDirItemData* rootData = new wxDirItemData(wxT(""), wxT(""), TRUE);
51a58d8b
JS
525
526 wxString rootName;
527
ec1b28a3 528#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
529 rootName = _("Computer");
530#else
531 rootName = _("Sections");
532#endif
533
534 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
535 m_treeCtrl->SetItemHasChildren(m_rootId);
536 m_treeCtrl->Expand(m_rootId); // automatically expand first level
537
538 // Expand and select the default path
539 if (!m_defaultPath.IsEmpty())
540 ExpandPath(m_defaultPath);
541
542 DoResize();
543
544 return TRUE;
545}
546
547wxGenericDirCtrl::~wxGenericDirCtrl()
548{
51a58d8b
JS
549}
550
551void wxGenericDirCtrl::Init()
552{
553 m_showHidden = FALSE;
554 m_imageList = NULL;
51a58d8b
JS
555 m_currentFilter = 0;
556 m_currentFilterStr = wxEmptyString; // Default: any file
557 m_treeCtrl = NULL;
558 m_filterListCtrl = NULL;
559}
560
561void wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
562{
748fcded 563 wxDirItemData *dir_item = new wxDirItemData(path,name,TRUE);
51a58d8b 564
51a58d8b 565 wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, imageId, -1, dir_item);
51a58d8b
JS
566
567 m_treeCtrl->SetItemHasChildren(id);
568}
569
570void wxGenericDirCtrl::SetupSections()
571{
ec1b28a3 572#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b 573
748fcded 574#ifdef __WIN32__
51a58d8b
JS
575 wxChar driveBuffer[256];
576 size_t n = (size_t) GetLogicalDriveStrings(255, driveBuffer);
577 size_t i = 0;
578 while (i < n)
579 {
580 wxString path, name;
581 path.Printf(wxT("%c:\\"), driveBuffer[i]);
582 name.Printf(wxT("(%c:)"), driveBuffer[i]);
583
584 int imageId = 4;
585 int driveType = ::GetDriveType(path);
586 switch (driveType)
587 {
588 case DRIVE_REMOVABLE:
589 if (path == wxT("a:\\") || path == wxT("b:\\"))
590 imageId = 6; // Floppy
591 else
592 imageId = 7;
593 break;
594 case DRIVE_FIXED:
595 imageId = 4;
596 break;
597 case DRIVE_REMOTE:
598 imageId = 4;
599 break;
600 case DRIVE_CDROM:
601 imageId = 5;
602 break;
603 default:
604 imageId = 4;
605 break;
606 }
607
608 AddSection(path, name, imageId);
609
610 while (driveBuffer[i] != wxT('\0'))
611 i ++;
612 i ++;
613 if (driveBuffer[i] == wxT('\0'))
614 break;
615 }
a044cb72 616# else
51a58d8b
JS
617 int drive;
618 int currentDrive;
619
51a58d8b
JS
620 /* If we can switch to the drive, it exists. */
621 for( drive = 1; drive <= 26; drive++ )
622 {
623 wxString path, name;
624 path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1));
625 name.Printf(wxT("(%c:)"), (char) (drive + 'a' - 1));
626
33fed835 627 if (wxIsDriveAvailable(path))
51a58d8b 628 {
ec1b28a3 629
51a58d8b
JS
630 AddSection(path, name);
631 }
632 }
a044cb72 633# endif
f4ac0693 634#elif defined(__WXMAC__)
bedaf53e
SC
635 FSSpec volume ;
636 short index = 1 ;
637 while(1) {
638 short actualCount = 0 ;
639 if ( OnLine( &volume , 1 , &actualCount , &index ) != noErr || actualCount == 0 )
640 break ;
641
642 wxString name = wxMacFSSpec2MacFilename( &volume ) ;
a044cb72 643 AddSection(name + wxFILE_SEP_PATH, name, 0);
bedaf53e 644 }
51a58d8b 645#else
748fcded 646 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
51a58d8b
JS
647#endif
648}
649
650void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
651{
652 // don't rename the main entry "Sections"
653 if (event.GetItem() == m_rootId)
654 {
655 event.Veto();
656 return;
657 }
658
659 // don't rename the individual sections
660 if (m_treeCtrl->GetParent( event.GetItem() ) == m_rootId)
661 {
662 event.Veto();
663 return;
664 }
665}
666
667void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
668{
669 if ((event.GetLabel().IsEmpty()) ||
670 (event.GetLabel() == _(".")) ||
671 (event.GetLabel() == _("..")) ||
672 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
673 {
674 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
675 dialog.ShowModal();
676 event.Veto();
677 return;
678 }
679
680 wxTreeItemId id = event.GetItem();
748fcded 681 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
51a58d8b
JS
682 wxASSERT( data );
683
684 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 685 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
686 new_name += event.GetLabel();
687
688 wxLogNull log;
689
690 if (wxFileExists(new_name))
691 {
692 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
693 dialog.ShowModal();
694 event.Veto();
695 }
696
697 if (wxRenameFile(data->m_path,new_name))
698 {
699 data->SetNewDirName( new_name );
700 }
701 else
702 {
703 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
704 dialog.ShowModal();
705 event.Veto();
706 }
707}
708
709void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
710{
711 wxTreeItemId parentId = event.GetItem();
712
748fcded
VS
713 // VS: this is needed because the event handler is called from wxTreeCtrl
714 // ctor when wxTR_HIDE_ROOT was specified
715 if (m_rootId == 0)
716 m_rootId = m_treeCtrl->GetRootItem();
717
51a58d8b
JS
718 ExpandDir(parentId);
719}
720
721void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
722{
723 wxTreeItemId child, parent = event.GetItem();
724
748fcded 725 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(event.GetItem());
51a58d8b
JS
726 if (!data->m_isExpanded)
727 return;
728
729 data->m_isExpanded = FALSE;
730 long cookie;
731 /* Workaround because DeleteChildren has disapeared (why?) and
732 * CollapseAndReset doesn't work as advertised (deletes parent too) */
733 child = m_treeCtrl->GetFirstChild(parent, cookie);
734 while (child.IsOk())
735 {
736 m_treeCtrl->Delete(child);
737 /* Not GetNextChild below, because the cookie mechanism can't
738 * handle disappearing children! */
739 child = m_treeCtrl->GetFirstChild(parent, cookie);
740 }
741}
742
743void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
744{
748fcded 745 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
746
747 if (data->m_isExpanded)
748 return;
749
750 data->m_isExpanded = TRUE;
751
748fcded 752 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
753 {
754 SetupSections();
755 return;
756 }
757
758 wxASSERT(data);
759
760 wxString search,path,filename;
761
762 wxString dirName(data->m_path);
763
ec1b28a3 764#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
765 // Check if this is a root directory and if so,
766 // whether the drive is avaiable.
33fed835
MB
767 if (!wxIsDriveAvailable(dirName))
768 {
ad9cd15c 769 data->m_isExpanded = FALSE;
7328394a
JS
770 //wxMessageBox(wxT("Sorry, this drive is not available."));
771 return;
33fed835 772 }
51a58d8b
JS
773#endif
774
775 // This may take a longish time. Go to busy cursor
776 wxBusyCursor busy;
777
ec1b28a3 778#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
779 if (dirName.Last() == ':')
780 dirName += wxString(wxFILE_SEP_PATH);
781#endif
782
783 wxArrayString dirs;
784 wxArrayString filenames;
785
786 wxDir d;
787 wxString eachFilename;
788
f9c165b1 789 wxLogNull log;
51a58d8b
JS
790 d.Open(dirName);
791
792 if (d.IsOpened())
793 {
748fcded 794 if (d.GetFirst(& eachFilename, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN))
51a58d8b
JS
795 {
796 do
797 {
798 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
799 {
800 dirs.Add(eachFilename);
801 }
802 }
748fcded 803 while (d.GetNext(& eachFilename));
51a58d8b
JS
804 }
805 }
57e26a09 806 dirs.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
51a58d8b
JS
807
808 // Now do the filenames -- but only if we're allowed to
809 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
810 {
f9c165b1
JS
811 wxLogNull log;
812
51a58d8b 813 d.Open(dirName);
ec1b28a3 814
51a58d8b
JS
815 if (d.IsOpened())
816 {
817 if (d.GetFirst(& eachFilename, m_currentFilterStr, wxDIR_FILES))
818 {
819 do
820 {
821 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
822 {
823 filenames.Add(eachFilename);
824 }
825 }
748fcded 826 while (d.GetNext(& eachFilename));
51a58d8b
JS
827 }
828 }
57e26a09 829 filenames.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
51a58d8b
JS
830 }
831
832 // Add the sorted dirs
833 size_t i;
834 for (i = 0; i < dirs.Count(); i++)
835 {
836 wxString eachFilename(dirs[i]);
837 path = dirName;
838 if (path.Last() != wxFILE_SEP_PATH)
839 path += wxString(wxFILE_SEP_PATH);
840 path += eachFilename;
841
748fcded 842 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,TRUE);
51a58d8b
JS
843 wxTreeItemId id = m_treeCtrl->AppendItem( parentId, eachFilename, 0, -1, dir_item);
844 m_treeCtrl->SetItemImage( id, 1, wxTreeItemIcon_Expanded );
ec1b28a3 845
51a58d8b 846 // Has this got any children? If so, make it expandable.
748fcded
VS
847 // (There are two situations when a dir has children: either it
848 // has subdirectories or it contains files that weren't filtered
849 // out. The latter only applies to dirctrl with files.)
850 if ( dir_item->HasSubDirs() ||
851 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
852 dir_item->HasFiles(m_currentFilterStr)) )
51a58d8b 853 {
748fcded 854 m_treeCtrl->SetItemHasChildren(id);
51a58d8b 855 }
51a58d8b
JS
856 }
857
858 // Add the sorted filenames
859 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
860 {
861 for (i = 0; i < filenames.Count(); i++)
862 {
863 wxString eachFilename(filenames[i]);
864 path = dirName;
865 if (path.Last() != wxFILE_SEP_PATH)
866 path += wxString(wxFILE_SEP_PATH);
867 path += eachFilename;
868 //path = dirName + wxString(wxT("/")) + eachFilename;
748fcded 869 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,FALSE);
3fa4bd0e 870 (void)m_treeCtrl->AppendItem( parentId, eachFilename, 2, -1, dir_item);
51a58d8b
JS
871 }
872 }
873}
874
875// Find the child that matches the first part of 'path'.
876// E.g. if a child path is "/usr" and 'path' is "/usr/include"
877// then the child for /usr is returned.
878wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
879{
880 wxString path2(path);
ec1b28a3 881
51a58d8b
JS
882 // Make sure all separators are as per the current platform
883 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
884 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 885
51a58d8b
JS
886 // Append a separator to foil bogus substring matching
887 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
888
889 // In MSW or PM, case is not significant
890#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
891 path2.MakeLower();
892#endif
ec1b28a3 893
51a58d8b
JS
894 long cookie;
895 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 896 while (childId.IsOk())
51a58d8b 897 {
748fcded 898 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 899
748fcded 900 if (data && !data->m_path.IsEmpty())
51a58d8b
JS
901 {
902 wxString childPath(data->m_path);
903 if (childPath.Last() != wxFILE_SEP_PATH)
904 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
905
906 // In MSW and PM, case is not significant
907#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
908 childPath.MakeLower();
909#endif
ec1b28a3 910
51a58d8b
JS
911 if (childPath.Len() <= path2.Len())
912 {
913 wxString path3 = path2.Mid(0, childPath.Len());
914 if (childPath == path3)
915 {
916 if (path3.Len() == path2.Len())
917 done = TRUE;
918 else
919 done = FALSE;
920 return childId;
921 }
922 }
923 }
ec1b28a3 924
748fcded 925 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 926 }
3fa4bd0e
VS
927 wxTreeItemId invalid;
928 return invalid;
51a58d8b
JS
929}
930
931// Try to expand as much of the given path as possible,
932// and select the given tree item.
933bool wxGenericDirCtrl::ExpandPath(const wxString& path)
934{
935 bool done = FALSE;
936 wxTreeItemId id = FindChild(m_rootId, path, done);
937 wxTreeItemId lastId = id; // The last non-zero id
237387ad 938 while (id.IsOk() && !done)
51a58d8b
JS
939 {
940 ExpandDir(id);
941
942 id = FindChild(id, path, done);
53ccf1c0 943 if (id.IsOk())
51a58d8b
JS
944 lastId = id;
945 }
53ccf1c0 946 if (lastId.IsOk())
51a58d8b 947 {
748fcded 948 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
51a58d8b
JS
949 if (data->m_isDir)
950 {
951 m_treeCtrl->Expand(lastId);
952 }
953 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
954 {
955 // Find the first file in this directory
956 long cookie;
957 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
958 bool selectedChild = FALSE;
53ccf1c0 959 while (childId.IsOk())
51a58d8b 960 {
748fcded 961 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 962
51a58d8b
JS
963 if (data && data->m_path != "" && !data->m_isDir)
964 {
965 m_treeCtrl->SelectItem(childId);
966 m_treeCtrl->EnsureVisible(childId);
967 selectedChild = TRUE;
968 break;
969 }
970 childId = m_treeCtrl->GetNextChild(lastId, cookie);
971 }
972 if (!selectedChild)
973 {
974 m_treeCtrl->SelectItem(lastId);
975 m_treeCtrl->EnsureVisible(lastId);
976 }
977 }
978 else
979 {
980 m_treeCtrl->SelectItem(lastId);
981 m_treeCtrl->EnsureVisible(lastId);
982 }
983
984 return TRUE;
985 }
986 else
987 return FALSE;
988}
989
990wxString wxGenericDirCtrl::GetPath() const
991{
992 wxTreeItemId id = m_treeCtrl->GetSelection();
993 if (id)
994 {
748fcded 995 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
996 return data->m_path;
997 }
998 else
999 return wxEmptyString;
1000}
1001
1002wxString wxGenericDirCtrl::GetFilePath() const
1003{
1004 wxTreeItemId id = m_treeCtrl->GetSelection();
1005 if (id)
1006 {
748fcded 1007 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1008 if (data->m_isDir)
1009 return wxEmptyString;
1010 else
1011 return data->m_path;
1012 }
1013 else
1014 return wxEmptyString;
1015}
1016
1017void wxGenericDirCtrl::SetPath(const wxString& path)
1018{
1019 m_defaultPath = path;
1020 if (m_rootId)
1021 ExpandPath(path);
1022}
1023
1024// Not used
1025#if 0
1026void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
1027{
748fcded 1028 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
51a58d8b
JS
1029
1030 // This may take a longish time. Go to busy cursor
1031 wxBusyCursor busy;
1032
1033 wxASSERT(data);
1034
1035 wxString search,path,filename;
1036
1037 wxString dirName(data->m_path);
1038
ec1b28a3 1039#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
1040 if (dirName.Last() == ':')
1041 dirName += wxString(wxFILE_SEP_PATH);
1042#endif
1043
1044 wxDir d;
1045 wxString eachFilename;
1046
f9c165b1 1047 wxLogNull log;
51a58d8b
JS
1048 d.Open(dirName);
1049
1050 if (d.IsOpened())
1051 {
1052 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1053 {
1054 do
1055 {
1056 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1057 {
1058 filenames.Add(eachFilename);
1059 }
1060 }
1061 while (d.GetNext(& eachFilename)) ;
1062 }
1063 }
1064}
1065#endif
1066
1067void wxGenericDirCtrl::SetFilterIndex(int n)
1068{
1069 m_currentFilter = n;
1070
1071 wxString f, d;
1072 if (ExtractWildcard(m_filter, n, f, d))
1073 m_currentFilterStr = f;
1074 else
1075 m_currentFilterStr = wxT("*.*");
1076}
1077
1078void wxGenericDirCtrl::SetFilter(const wxString& filter)
1079{
1080 m_filter = filter;
1081
1082 wxString f, d;
1083 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1084 m_currentFilterStr = f;
1085 else
1086 m_currentFilterStr = wxT("*.*");
1087}
1088
1089// Extract description and actual filter from overall filter string
1090bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1091{
1092 wxArrayString filters, descriptions;
1093 int count = ParseFilter(filterStr, filters, descriptions);
1094 if (count > 0 && n < count)
1095 {
1096 filter = filters[n];
1097 description = descriptions[n];
1098 return TRUE;
1099 }
5716a1ab
VZ
1100
1101 return FALSE;
51a58d8b
JS
1102}
1103
1104// Parses the global filter, returning the number of filters.
1105// Returns 0 if none or if there's a problem.
1106// filterStr is in the form:
1107//
1108// "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1109
1110int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1111{
1112 wxString str(filterStr);
1113
1114 wxString description, filter;
1115 int pos;
1116 bool finished = FALSE;
1117 do
1118 {
1119 pos = str.Find(wxT('|'));
1120 if (pos == -1)
1121 return 0; // Problem
1122 description = str.Left(pos);
1123 str = str.Mid(pos+1);
1124 pos = str.Find(wxT('|'));
1125 if (pos == -1)
1126 {
1127 filter = str;
1128 finished = TRUE;
1129 }
1130 else
1131 {
1132 filter = str.Left(pos);
1133 str = str.Mid(pos+1);
1134 }
1135 descriptions.Add(description);
1136 filters.Add(filter);
1137 }
1138 while (!finished) ;
1139
1140 return filters.Count();
1141}
1142
1143void wxGenericDirCtrl::DoResize()
1144{
1145 wxSize sz = GetClientSize();
1146 int verticalSpacing = 3;
1147 if (m_treeCtrl)
1148 {
1149 wxSize filterSz ;
1150 if (m_filterListCtrl)
1151 {
1152 filterSz = m_filterListCtrl->GetSize();
1153 sz.y -= (filterSz.y + verticalSpacing);
1154 }
1155 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1156 if (m_filterListCtrl)
1157 {
1158 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1159 // Don't know why, but this needs refreshing after a resize (wxMSW)
1160 m_filterListCtrl->Refresh();
1161 }
1162 }
1163}
1164
1165
33ac7e6f 1166void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
51a58d8b
JS
1167{
1168 DoResize();
1169}
1170
1171//-----------------------------------------------------------------------------
1172// wxDirFilterListCtrl
1173//-----------------------------------------------------------------------------
1174
1175IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1176
1177BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1178 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter)
1179END_EVENT_TABLE()
1180
1181bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1182 const wxPoint& pos,
1183 const wxSize& size,
1184 long style)
1185{
1186 m_dirCtrl = parent;
1187 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1188}
1189
1190void wxDirFilterListCtrl::Init()
1191{
1192 m_dirCtrl = NULL;
1193}
1194
33ac7e6f 1195void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
51a58d8b
JS
1196{
1197 int sel = GetSelection();
1198
1199 wxString currentPath = m_dirCtrl->GetPath();
ec1b28a3 1200
51a58d8b
JS
1201 m_dirCtrl->SetFilterIndex(sel);
1202
1203 // If the filter has changed, the view is out of date, so
1204 // collapse the tree.
1205 m_dirCtrl->GetTreeCtrl()->Collapse(m_dirCtrl->GetRootId());
1206 m_dirCtrl->GetTreeCtrl()->Expand(m_dirCtrl->GetRootId());
1207
1208 // Try to restore the selection, or at least the directory
1209 m_dirCtrl->ExpandPath(currentPath);
1210}
1211
1212void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1213{
1214 Clear();
1215 wxArrayString descriptions, filters;
1216 size_t n = (size_t) m_dirCtrl->ParseFilter(filter, filters, descriptions);
1217
1218 if (n > 0 && defaultFilter < (int) n)
1219 {
1220 size_t i = 0;
1221 for (i = 0; i < n; i++)
1222 Append(descriptions[i]);
1223 SetSelection(defaultFilter);
1224 }
1225}
1226
1e6feb95
VZ
1227
1228#endif // wxUSE_DIRDLG