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