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