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