]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirctrlg.cpp
patch from Dimitri fixing crashes when decoding the invalid GIFs
[wxWidgets.git] / src / generic / dirctrlg.cpp
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(__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 #include <direct.h>
74 #include <stdlib.h>
75 #include <ctype.h>
76
77 #endif // __WXPM__
78
79 #if defined(__WXMAC__)
80 # include "MoreFilesExtras.h"
81 #endif
82
83 #ifdef __BORLANDC__
84 #include "dos.h"
85 #endif
86
87 // If compiled under Windows, this macro can cause problems
88 #ifdef GetFirstChild
89 #undef GetFirstChild
90 #endif
91
92 /* Closed folder */
93 static 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 */
122 static 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 */
151 static char * icon3_xpm[] = {
152 /* width height ncolors chars_per_pixel */
153 "16 16 3 1",
154 /* colors */
155 " s None c None",
156 ". c #000000",
157 "+ c #ffffff",
158 /* pixels */
159 " ",
160 " ........ ",
161 " .++++++.. ",
162 " .+.+.++.+. ",
163 " .++++++.... ",
164 " .+.+.+++++. ",
165 " .+++++++++. ",
166 " .+.+.+.+.+. ",
167 " .+++++++++. ",
168 " .+.+.+.+.+. ",
169 " .+++++++++. ",
170 " .+.+.+.+.+. ",
171 " .+++++++++. ",
172 " ........... ",
173 " ",
174 " "};
175
176 /* Computer */
177 static char * icon4_xpm[] = {
178 "16 16 7 1",
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",
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 */
204 static char * icon5_xpm[] = {
205 "16 16 7 1",
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",
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 */
231 static char *icon6_xpm[] = {
232 "16 16 10 1",
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",
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 */
261 static char * icon7_xpm[] = {
262 "16 16 7 1",
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",
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 */
288 static char * icon8_xpm[] = {
289 "16 16 7 1",
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",
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 " "};
313
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__)
323 int setdrive(int drive)
324 {
325 #if defined(__GNUWIN32__) && \
326 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
327 return _chdrive(drive);
328 #else
329 wxChar newdrive[3];
330
331 if (drive < 1 || drive > 31)
332 return -1;
333 newdrive[0] = (wxChar)(wxT('A') + drive - 1);
334 newdrive[1] = wxT(':');
335 newdrive[2] = wxT('\0');
336 #if defined(__WXMSW__)
337 #ifdef __WIN16__
338 if (wxSetWorkingDirectory(newdrive))
339 #else
340 if (::SetCurrentDirectory(newdrive))
341 #endif
342 #else
343 // VA doesn't know what LPSTR is and has its own set
344 if (DosSetCurrentDir((PSZ)newdrive))
345 #endif
346 return 0;
347 else
348 return -1;
349 #endif // !GNUWIN32
350 }
351
352 static bool wxIsDriveAvailable(const wxString dirName)
353 {
354 #ifdef __WIN32__
355 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
356 #endif
357 bool success = TRUE;
358
359 // Check if this is a root directory and if so,
360 // whether the drive is avaiable.
361 if (dirName.Len() == 3 && dirName[(size_t)1] == wxT(':'))
362 {
363 wxString dirNameLower(dirName.Lower());
364 #if defined(__GNUWIN32__) && \
365 !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
366 success = wxPathExists(dirNameLower);
367 #else
368 int currentDrive = _getdrive();
369 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
370 int err = setdrive( thisDrive ) ;
371 setdrive( currentDrive );
372
373 if (err == -1)
374 {
375 success = FALSE;
376 }
377 #endif
378 }
379 #ifdef __WIN32__
380 (void) SetErrorMode(errorMode);
381 #endif
382
383 return success;
384 }
385 #endif
386
387 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
388 // and sort regardless of case.
389 static 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
397 //-----------------------------------------------------------------------------
398 // wxDirItemData
399 //-----------------------------------------------------------------------------
400
401 wxDirItemData::wxDirItemData(const wxString& path, const wxString& name,
402 bool isDir)
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;
411 m_isExpanded = FALSE;
412 m_isDir = isDir;
413 }
414
415 wxDirItemData::~wxDirItemData()
416 {
417 }
418
419 void wxDirItemData::SetNewDirName(const wxString& path)
420 {
421 m_path = path;
422 m_name = wxFileNameFromPath(path);
423 }
424
425 bool 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
440 bool 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();
453 }
454
455 //-----------------------------------------------------------------------------
456 // wxGenericDirCtrl
457 //-----------------------------------------------------------------------------
458
459 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
460
461 BEGIN_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)
467 END_EVENT_TABLE()
468
469 wxGenericDirCtrl::wxGenericDirCtrl(void)
470 {
471 Init();
472 }
473
474 bool wxGenericDirCtrl::Create(wxWindow *parent,
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)
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
491 long treeStyle = wxTR_HAS_BUTTONS | wxTR_EDIT_LABELS | wxTR_HIDE_ROOT;
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);
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));
521 m_treeCtrl->AssignImageList(m_imageList);
522
523 m_showHidden = FALSE;
524 wxDirItemData* rootData = new wxDirItemData(wxT(""), wxT(""), TRUE);
525
526 wxString rootName;
527
528 #if defined(__WXMSW__) || defined(__WXPM__)
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
547 wxGenericDirCtrl::~wxGenericDirCtrl()
548 {
549 }
550
551 void wxGenericDirCtrl::Init()
552 {
553 m_showHidden = FALSE;
554 m_imageList = NULL;
555 m_currentFilter = 0;
556 m_currentFilterStr = wxEmptyString; // Default: any file
557 m_treeCtrl = NULL;
558 m_filterListCtrl = NULL;
559 }
560
561 void wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
562 {
563 wxDirItemData *dir_item = new wxDirItemData(path,name,TRUE);
564
565 wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, imageId, -1, dir_item);
566
567 m_treeCtrl->SetItemHasChildren(id);
568 }
569
570 void wxGenericDirCtrl::SetupSections()
571 {
572 #if defined(__WXMSW__) || defined(__WXPM__)
573
574 #ifdef __WIN32__
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 }
616 # else
617 int drive;
618 int currentDrive;
619
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
627 if (wxIsDriveAvailable(path))
628 {
629
630 AddSection(path, name);
631 }
632 }
633 # endif
634 #elif defined(__WXMAC__)
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 ) ;
643 AddSection(name + wxFILE_SEP_PATH, name, 0);
644 }
645 #else
646 AddSection(wxT("/"), wxT("/"), 3/*computer icon*/);
647 #endif
648 }
649
650 void 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
667 void 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();
681 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
682 wxASSERT( data );
683
684 wxString new_name( wxPathOnly( data->m_path ) );
685 new_name += wxString(wxFILE_SEP_PATH);
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
709 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
710 {
711 wxTreeItemId parentId = event.GetItem();
712
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
718 ExpandDir(parentId);
719 }
720
721 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
722 {
723 wxTreeItemId child, parent = event.GetItem();
724
725 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(event.GetItem());
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
743 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
744 {
745 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
746
747 if (data->m_isExpanded)
748 return;
749
750 data->m_isExpanded = TRUE;
751
752 if (parentId == m_treeCtrl->GetRootItem())
753 {
754 SetupSections();
755 return;
756 }
757
758 wxASSERT(data);
759
760 wxString search,path,filename;
761
762 wxString dirName(data->m_path);
763
764 #if defined(__WXMSW__) || defined(__WXPM__)
765 // Check if this is a root directory and if so,
766 // whether the drive is avaiable.
767 if (!wxIsDriveAvailable(dirName))
768 {
769 data->m_isExpanded = FALSE;
770 //wxMessageBox(wxT("Sorry, this drive is not available."));
771 return;
772 }
773 #endif
774
775 // This may take a longish time. Go to busy cursor
776 wxBusyCursor busy;
777
778 #if defined(__WXMSW__) || defined(__WXPM__)
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
789 wxLogNull log;
790 d.Open(dirName);
791
792 if (d.IsOpened())
793 {
794 if (d.GetFirst(& eachFilename, wxEmptyString, wxDIR_DIRS | wxDIR_HIDDEN))
795 {
796 do
797 {
798 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
799 {
800 dirs.Add(eachFilename);
801 }
802 }
803 while (d.GetNext(& eachFilename));
804 }
805 }
806 dirs.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
807
808 // Now do the filenames -- but only if we're allowed to
809 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
810 {
811 wxLogNull log;
812
813 d.Open(dirName);
814
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 }
826 while (d.GetNext(& eachFilename));
827 }
828 }
829 filenames.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
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
842 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,TRUE);
843 wxTreeItemId id = m_treeCtrl->AppendItem( parentId, eachFilename, 0, -1, dir_item);
844 m_treeCtrl->SetItemImage( id, 1, wxTreeItemIcon_Expanded );
845
846 // Has this got any children? If so, make it expandable.
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)) )
853 {
854 m_treeCtrl->SetItemHasChildren(id);
855 }
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;
869 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,FALSE);
870 (void)m_treeCtrl->AppendItem( parentId, eachFilename, 2, -1, dir_item);
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.
878 wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
879 {
880 wxString path2(path);
881
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));
885
886 // Append a separator to foil bogus substring matching
887 path2 += wxString(wxFILE_SEP_PATH);
888
889 // In MSW or PM, case is not significant
890 #if defined(__WXMSW__) || defined(__WXPM__)
891 path2.MakeLower();
892 #endif
893
894 long cookie;
895 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
896 while (childId.IsOk())
897 {
898 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
899
900 if (data && !data->m_path.IsEmpty())
901 {
902 wxString childPath(data->m_path);
903 if (childPath.Last() != wxFILE_SEP_PATH)
904 childPath += wxString(wxFILE_SEP_PATH);
905
906 // In MSW and PM, case is not significant
907 #if defined(__WXMSW__) || defined(__WXPM__)
908 childPath.MakeLower();
909 #endif
910
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 }
924
925 childId = m_treeCtrl->GetNextChild(parentId, cookie);
926 }
927 wxTreeItemId invalid;
928 return invalid;
929 }
930
931 // Try to expand as much of the given path as possible,
932 // and select the given tree item.
933 bool 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
938 while (id.IsOk() && !done)
939 {
940 ExpandDir(id);
941
942 id = FindChild(id, path, done);
943 if (id.IsOk())
944 lastId = id;
945 }
946 if (lastId.IsOk())
947 {
948 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
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;
959 while (childId.IsOk())
960 {
961 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
962
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
990 wxString wxGenericDirCtrl::GetPath() const
991 {
992 wxTreeItemId id = m_treeCtrl->GetSelection();
993 if (id)
994 {
995 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
996 return data->m_path;
997 }
998 else
999 return wxEmptyString;
1000 }
1001
1002 wxString wxGenericDirCtrl::GetFilePath() const
1003 {
1004 wxTreeItemId id = m_treeCtrl->GetSelection();
1005 if (id)
1006 {
1007 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
1008 if (data->m_isDir)
1009 return wxEmptyString;
1010 else
1011 return data->m_path;
1012 }
1013 else
1014 return wxEmptyString;
1015 }
1016
1017 void 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
1026 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
1027 {
1028 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
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
1039 #if defined(__WXMSW__) || defined(__WXPM__)
1040 if (dirName.Last() == ':')
1041 dirName += wxString(wxFILE_SEP_PATH);
1042 #endif
1043
1044 wxDir d;
1045 wxString eachFilename;
1046
1047 wxLogNull log;
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
1067 void 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
1078 void 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
1090 bool 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 }
1100
1101 return FALSE;
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
1110 int 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
1143 void 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
1166 void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
1167 {
1168 DoResize();
1169 }
1170
1171 //-----------------------------------------------------------------------------
1172 // wxDirFilterListCtrl
1173 //-----------------------------------------------------------------------------
1174
1175 IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1176
1177 BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1178 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter)
1179 END_EVENT_TABLE()
1180
1181 bool 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
1190 void wxDirFilterListCtrl::Init()
1191 {
1192 m_dirCtrl = NULL;
1193 }
1194
1195 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
1196 {
1197 int sel = GetSelection();
1198
1199 wxString currentPath = m_dirCtrl->GetPath();
1200
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
1212 void 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
1227
1228 #endif // wxUSE_DIRDLG