]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirctrlg.cpp
There was no icon file in this example, causing compilation with VC to fail. I just...
[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 #include "wx/defs.h"
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/textdlg.h"
31 #include "wx/filefn.h"
32 #include "wx/cmndata.h"
33 #include "wx/gdicmn.h"
34 #include "wx/intl.h"
35 #include "wx/imaglist.h"
36 #include "wx/icon.h"
37 #include "wx/log.h"
38 #include "wx/sizer.h"
39 #include "wx/tokenzr.h"
40 #include "wx/dir.h"
41 #include "wx/settings.h"
42
43 #if wxUSE_STATLINE
44 #include "wx/statline.h"
45 #endif
46
47 #include "wx/generic/dirctrlg.h"
48
49 #ifdef __WXMSW__
50 #include <windows.h>
51
52 #ifndef __GNUWIN32__
53 #include <direct.h>
54 #include <stdlib.h>
55 #include <ctype.h>
56 #endif
57
58 #endif
59
60 #ifdef __WXPM__
61
62 #define INCL_BASE
63 #include <os2.h>
64 #include <direct.h>
65 #include <stdlib.h>
66 #include <ctype.h>
67
68 #endif // __WXPM__
69
70 #ifdef __BORLANDC__
71 #include "dos.h"
72 #endif
73
74 // If compiled under Windows, this macro can cause problems
75 #ifdef GetFirstChild
76 #undef GetFirstChild
77 #endif
78
79 #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW || wxUSE_XPM_IN_OS2
80 /* Closed folder */
81 static char * icon1_xpm[] = {
82 /* width height ncolors chars_per_pixel */
83 "16 16 6 1",
84 /* colors */
85 " s None c None",
86 ". c #000000",
87 "+ c #c0c0c0",
88 "@ c #808080",
89 "# c #ffff00",
90 "$ c #ffffff",
91 /* pixels */
92 " ",
93 " @@@@@ ",
94 " @#+#+#@ ",
95 " @#+#+#+#@@@@@@ ",
96 " @$$$$$$$$$$$$@.",
97 " @$#+#+#+#+#+#@.",
98 " @$+#+#+#+#+#+@.",
99 " @$#+#+#+#+#+#@.",
100 " @$+#+#+#+#+#+@.",
101 " @$#+#+#+#+#+#@.",
102 " @$+#+#+#+#+#+@.",
103 " @$#+#+#+#+#+#@.",
104 " @@@@@@@@@@@@@@.",
105 " ..............",
106 " ",
107 " "};
108
109 /* Open folder */
110 static char * icon2_xpm[] = {
111 /* width height ncolors chars_per_pixel */
112 "16 16 6 1",
113 /* colors */
114 " s None c None",
115 ". c #000000",
116 "+ c #c0c0c0",
117 "@ c #808080",
118 "# c #ffff00",
119 "$ c #ffffff",
120 /* pixels */
121 " ",
122 " @@@@@ ",
123 " @$$$$$@ ",
124 " @$#+#+#$@@@@@@ ",
125 " @$+#+#+$$$$$$@.",
126 " @$#+#+#+#+#+#@.",
127 "@@@@@@@@@@@@@#@.",
128 "@$$$$$$$$$$@@+@.",
129 "@$#+#+#+#+##.@@.",
130 " @$#+#+#+#+#+.@.",
131 " @$+#+#+#+#+#.@.",
132 " @$+#+#+#+##@..",
133 " @@@@@@@@@@@@@.",
134 " .............",
135 " ",
136 " "};
137
138 /* File */
139 static char * icon3_xpm[] = {
140 /* width height ncolors chars_per_pixel */
141 "16 16 3 1",
142 /* colors */
143 " s None c None",
144 ". c #000000",
145 "+ c #ffffff",
146 /* pixels */
147 " ",
148 " ........ ",
149 " .++++++.. ",
150 " .+.+.++.+. ",
151 " .++++++.... ",
152 " .+.+.+++++. ",
153 " .+++++++++. ",
154 " .+.+.+.+.+. ",
155 " .+++++++++. ",
156 " .+.+.+.+.+. ",
157 " .+++++++++. ",
158 " .+.+.+.+.+. ",
159 " .+++++++++. ",
160 " ........... ",
161 " ",
162 " "};
163
164 /* Computer */
165 static char * icon4_xpm[] = {
166 "16 16 7 1",
167 " s None c None",
168 ". c #808080",
169 "X c #c0c0c0",
170 "o c Black",
171 "O c Gray100",
172 "+ c #008080",
173 "@ c Blue",
174 " ........... ",
175 " .XXXXXXXXXX.o",
176 " .OOOOOOOOO..o",
177 " .OoooooooX..o",
178 " .Oo+...@+X..o",
179 " .Oo+XXX.+X..o",
180 " .Oo+....+X..o",
181 " .Oo++++++X..o",
182 " .OXXXXXXXX.oo",
183 " ..........o.o",
184 " ...........Xo",
185 " .XXXXXXXXXX.o",
186 " .o.o.o.o.o...o",
187 " .oXoXoXoXoXo.o ",
188 ".XOXXXXXXXXX.o ",
189 "............o "};
190
191 /* Drive */
192 static char * icon5_xpm[] = {
193 "16 16 7 1",
194 " s None c None",
195 ". c #808080",
196 "X c #c0c0c0",
197 "o c Black",
198 "O c Gray100",
199 "+ c Green",
200 "@ c #008000",
201 " ",
202 " ",
203 " ",
204 " ",
205 " ............. ",
206 " .XXXXXXXXXXXX.o",
207 ".OOOOOOOOOOOO..o",
208 ".XXXXXXXXX+@X..o",
209 ".XXXXXXXXXXXX..o",
210 ".X..........X..o",
211 ".XOOOOOOOOOOX..o",
212 "..............o ",
213 " ooooooooooooo ",
214 " ",
215 " ",
216 " "};
217
218 /* CD-ROM */
219 static char *icon6_xpm[] = {
220 "16 16 10 1",
221 " s None c None",
222 ". c #808080",
223 "X c #c0c0c0",
224 "o c Yellow",
225 "O c Blue",
226 "+ c Black",
227 "@ c Gray100",
228 "# c #008080",
229 "$ c Green",
230 "% c #008000",
231 " ... ",
232 " ..XoX.. ",
233 " .O.XoXXX+ ",
234 " ...O.oXXXX+ ",
235 " .O..X.XXXX+ ",
236 " ....X.+..XXX+",
237 " .XXX.+@+.XXX+",
238 " .X@XX.+.X@@X+",
239 " .....X...#XX@+ ",
240 ".@@@...XXo.O@X+ ",
241 ".@XXX..XXoXOO+ ",
242 ".@++++..XoX+++ ",
243 ".@$%@@XX+++X.+ ",
244 ".............+ ",
245 " ++++++++++++ ",
246 " "};
247
248 /* Floppy */
249 static char * icon7_xpm[] = {
250 "16 16 7 1",
251 " s None c None",
252 ". c #808080",
253 "X c Gray100",
254 "o c #c0c0c0",
255 "O c Black",
256 "+ c Cyan",
257 "@ c Red",
258 " ......X",
259 " .ooooooO",
260 " .+++++OO",
261 " .++++++O",
262 " .++++++O",
263 " .ooooooO",
264 " .......o....oO",
265 " .oooooo.o.O.XoO",
266 ".XXXXXXXXOOOOOO ",
267 ".ooooooooo@o..O ",
268 ".ooo....oooo..O ",
269 ".o..OOOO...o..O ",
270 ".oooXXXXoooo..O ",
271 ".............O ",
272 " OOOOOOOOOOOO ",
273 " "};
274
275 /* Removeable */
276 static char * icon8_xpm[] = {
277 "16 16 7 1",
278 " s None c None",
279 ". c #808080",
280 "X c #c0c0c0",
281 "o c Black",
282 "O c Gray100",
283 "+ c Red",
284 "@ c #800000",
285 " ",
286 " ",
287 " ",
288 " ............. ",
289 " .XXXXXXXXXXXX.o",
290 ".OOOOOOOOOOOO..o",
291 ".OXXXXXXXXXXX..o",
292 ".O+@.oooooo.X..o",
293 ".OXXOooooooOX..o",
294 ".OXXXOOOOOOXX..o",
295 ".OXXXXXXXXXXX..o",
296 ".O............o ",
297 " ooooooooooooo ",
298 " ",
299 " ",
300 " "};
301 #endif // !wxMSW
302
303 static const int ID_DIRCTRL = 1000;
304 static const int ID_TEXTCTRL = 1001;
305 static const int ID_OK = 1002;
306 static const int ID_CANCEL = 1003;
307 static const int ID_NEW = 1004;
308 //static const int ID_CHECK = 1005;
309
310 #if defined(__WXMSW__) || defined(__WXPM__)
311 static bool wxIsDriveAvailable(const wxString dirName)
312 {
313 #ifdef __WIN32__
314 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
315 #endif
316 bool success = TRUE;
317
318 // Check if this is a root directory and if so,
319 // whether the drive is avaiable.
320 if (dirName.Len() == 3 && dirName[(size_t)1] == wxT(':'))
321 {
322 wxString dirNameLower(dirName.Lower());
323 #if defined(__GNUWIN32__)
324 success = wxPathExists(dirNameLower);
325 #else
326 int currentDrive = _getdrive();
327 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
328 int err = _chdrive( thisDrive ) ;
329 _chdrive( currentDrive );
330
331 if (err == -1)
332 {
333 success = FALSE;
334 }
335 #endif
336 }
337 #ifdef __WIN32__
338 (void) SetErrorMode(errorMode);
339 #endif
340
341 return success;
342 }
343 #endif
344
345 //-----------------------------------------------------------------------------
346 // wxDirItemDataEx
347 //-----------------------------------------------------------------------------
348
349 wxDirItemDataEx::wxDirItemDataEx(const wxString& path, const wxString& name,
350 bool isDir)
351 {
352 m_path = path;
353 m_name = name;
354 /* Insert logic to detect hidden files here
355 * In UnixLand we just check whether the first char is a dot
356 * For FileNameFromPath read LastDirNameInThisPath ;-) */
357 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
358 m_isHidden = FALSE;
359 // m_hasSubDirs is no longer needed
360 m_hasSubDirs = TRUE; // HasSubDirs();
361 m_isExpanded = FALSE;
362 m_isDir = isDir;
363 }
364
365 wxDirItemDataEx::~wxDirItemDataEx()
366 {
367 }
368
369 void wxDirItemDataEx::SetNewDirName( wxString path )
370 {
371 m_path = path;
372 m_name = wxFileNameFromPath( path );
373 }
374
375 //-----------------------------------------------------------------------------
376 // wxGenericDirCtrl
377 //-----------------------------------------------------------------------------
378
379 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
380
381 BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
382 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem)
383 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem)
384 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem)
385 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem)
386 EVT_SIZE (wxGenericDirCtrl::OnSize)
387 END_EVENT_TABLE()
388
389 wxGenericDirCtrl::wxGenericDirCtrl(void)
390 {
391 Init();
392 }
393
394 bool wxGenericDirCtrl::Create(wxWindow *parent,
395 const wxWindowID id,
396 const wxString& dir,
397 const wxPoint& pos,
398 const wxSize& size,
399 long style,
400 const wxString& filter,
401 int defaultFilter,
402 const wxString& name )
403 {
404 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
405 return FALSE;
406
407 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
408
409 Init();
410
411 long treeStyle = wxTR_HAS_BUTTONS ; // | wxTR_EDIT_LABELS;
412 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
413 treeStyle |= wxNO_BORDER;
414
415 long filterStyle = 0;
416 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
417 filterStyle |= wxNO_BORDER;
418
419 m_treeCtrl = new wxTreeCtrl(this, wxID_TREECTRL, pos, size, treeStyle);
420
421 if (!filter.IsEmpty() && (style & wxDIRCTRL_SHOW_FILTERS))
422 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
423
424 m_defaultPath = dir;
425 m_filter = filter;
426
427 SetFilterIndex(defaultFilter);
428
429 if (m_filterListCtrl)
430 m_filterListCtrl->FillFilterList(filter, defaultFilter);
431
432 m_imageList = new wxImageList(16, 16, TRUE);
433 #if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW
434 m_imageList->Add(wxIcon(icon1_xpm));
435 m_imageList->Add(wxIcon(icon2_xpm));
436 m_imageList->Add(wxIcon(icon3_xpm));
437 m_imageList->Add(wxIcon(icon4_xpm));
438 m_imageList->Add(wxIcon(icon5_xpm));
439 m_imageList->Add(wxIcon(icon6_xpm));
440 m_imageList->Add(wxIcon(icon7_xpm));
441 m_imageList->Add(wxIcon(icon8_xpm));
442 #elif defined(__WXMSW__)
443 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
444 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_OPEN_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
445 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE));
446 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_COMPUTER"), wxBITMAP_TYPE_ICO_RESOURCE));
447 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_DRIVE"), wxBITMAP_TYPE_ICO_RESOURCE));
448 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CDROM"), wxBITMAP_TYPE_ICO_RESOURCE));
449 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FLOPPY"), wxBITMAP_TYPE_ICO_RESOURCE));
450 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_REMOVEABLE"), wxBITMAP_TYPE_ICO_RESOURCE));
451 #else
452 #error "Sorry, we don't have icons available for this platforms."
453 #endif
454 m_treeCtrl->SetImageList(m_imageList);
455
456 m_showHidden = FALSE;
457 wxDirItemDataEx* rootData = new wxDirItemDataEx(wxT(""), wxT(""), TRUE);
458
459 wxString rootName;
460
461 #if defined(__WXMSW__) || defined(__WXPM__)
462 rootName = _("Computer");
463 #else
464 rootName = _("Sections");
465 #endif
466
467 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
468 m_treeCtrl->SetItemHasChildren(m_rootId);
469 m_treeCtrl->Expand(m_rootId); // automatically expand first level
470
471 // Expand and select the default path
472 if (!m_defaultPath.IsEmpty())
473 ExpandPath(m_defaultPath);
474
475 DoResize();
476
477 return TRUE;
478 }
479
480 wxGenericDirCtrl::~wxGenericDirCtrl()
481 {
482 m_treeCtrl->SetImageList(NULL);
483 delete m_imageList;
484 }
485
486 void wxGenericDirCtrl::Init()
487 {
488 m_showHidden = FALSE;
489 m_imageList = NULL;
490 m_currentFilter = 0;
491 m_currentFilterStr = wxEmptyString; // Default: any file
492 m_treeCtrl = NULL;
493 m_filterListCtrl = NULL;
494 }
495
496 void wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
497 {
498 wxDirItemDataEx *dir_item = new wxDirItemDataEx(path,name,TRUE);
499
500 #if defined(__WXMSW__) || defined(__WXPM__)
501 // Windows and OS/2: sections are displayed as drives
502 wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, imageId, -1, dir_item);
503 #else
504 // Unix: sections are displayed as folders
505 wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, 0, -1, dir_item);
506 m_treeCtrl->SetItemImage( id, 1, wxTreeItemIcon_Expanded );
507 #endif
508 // TODO: other operating systems.
509
510 m_treeCtrl->SetItemHasChildren(id);
511 }
512
513 void wxGenericDirCtrl::SetupSections()
514 {
515 #if defined(__WXMSW__) || defined(__WXPM__)
516
517 #ifdef __WIN32__
518 wxChar driveBuffer[256];
519 size_t n = (size_t) GetLogicalDriveStrings(255, driveBuffer);
520 size_t i = 0;
521 while (i < n)
522 {
523 wxString path, name;
524 path.Printf(wxT("%c:\\"), driveBuffer[i]);
525 name.Printf(wxT("(%c:)"), driveBuffer[i]);
526
527 int imageId = 4;
528 int driveType = ::GetDriveType(path);
529 switch (driveType)
530 {
531 case DRIVE_REMOVABLE:
532 if (path == wxT("a:\\") || path == wxT("b:\\"))
533 imageId = 6; // Floppy
534 else
535 imageId = 7;
536 break;
537 case DRIVE_FIXED:
538 imageId = 4;
539 break;
540 case DRIVE_REMOTE:
541 imageId = 4;
542 break;
543 case DRIVE_CDROM:
544 imageId = 5;
545 break;
546 default:
547 imageId = 4;
548 break;
549 }
550
551 AddSection(path, name, imageId);
552
553 while (driveBuffer[i] != wxT('\0'))
554 i ++;
555 i ++;
556 if (driveBuffer[i] == wxT('\0'))
557 break;
558 }
559 #else
560 int drive;
561 int currentDrive;
562
563 /* If we can switch to the drive, it exists. */
564 for( drive = 1; drive <= 26; drive++ )
565 {
566 wxString path, name;
567 path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1));
568 name.Printf(wxT("(%c:)"), (char) (drive + 'a' - 1));
569
570 if (wxIsDriveAvailable(path))
571 {
572
573 AddSection(path, name);
574 }
575 }
576 #endif
577
578 #else
579 AddSection(wxT("/"), _("The Computer"), 0);
580 AddSection(wxGetHomeDir(), _("My Home"), 0 );
581 AddSection(wxT("/mnt"), _("Mounted Devices"), 0 );
582 AddSection(wxT("/usr/local"), _("User Local"), 0 );
583 AddSection(wxT("/usr"), _("User"), 0 );
584 AddSection(wxT("/var"), _("Variables"), 0 );
585 AddSection(wxT("/etc"), _("Etcetera"), 0 );
586 AddSection(wxT("/tmp"), _("Temporary"), 0 );
587 #endif
588 }
589
590 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
591 {
592 // don't rename the main entry "Sections"
593 if (event.GetItem() == m_rootId)
594 {
595 event.Veto();
596 return;
597 }
598
599 // don't rename the individual sections
600 if (m_treeCtrl->GetParent( event.GetItem() ) == m_rootId)
601 {
602 event.Veto();
603 return;
604 }
605 }
606
607 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
608 {
609 if ((event.GetLabel().IsEmpty()) ||
610 (event.GetLabel() == _(".")) ||
611 (event.GetLabel() == _("..")) ||
612 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
613 {
614 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
615 dialog.ShowModal();
616 event.Veto();
617 return;
618 }
619
620 wxTreeItemId id = event.GetItem();
621 wxDirItemDataEx *data = (wxDirItemDataEx*)m_treeCtrl->GetItemData( id );
622 wxASSERT( data );
623
624 wxString new_name( wxPathOnly( data->m_path ) );
625 new_name += wxString(wxFILE_SEP_PATH);
626 new_name += event.GetLabel();
627
628 wxLogNull log;
629
630 if (wxFileExists(new_name))
631 {
632 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
633 dialog.ShowModal();
634 event.Veto();
635 }
636
637 if (wxRenameFile(data->m_path,new_name))
638 {
639 data->SetNewDirName( new_name );
640 }
641 else
642 {
643 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
644 dialog.ShowModal();
645 event.Veto();
646 }
647 }
648
649 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
650 {
651 wxTreeItemId parentId = event.GetItem();
652
653 ExpandDir(parentId);
654 }
655
656 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
657 {
658 wxTreeItemId child, parent = event.GetItem();
659
660 wxDirItemDataEx *data = (wxDirItemDataEx *) m_treeCtrl->GetItemData(event.GetItem());
661 if (!data->m_isExpanded)
662 return;
663
664 data->m_isExpanded = FALSE;
665 long cookie;
666 /* Workaround because DeleteChildren has disapeared (why?) and
667 * CollapseAndReset doesn't work as advertised (deletes parent too) */
668 child = m_treeCtrl->GetFirstChild(parent, cookie);
669 while (child.IsOk())
670 {
671 m_treeCtrl->Delete(child);
672 /* Not GetNextChild below, because the cookie mechanism can't
673 * handle disappearing children! */
674 child = m_treeCtrl->GetFirstChild(parent, cookie);
675 }
676 }
677
678 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
679 {
680 wxDirItemDataEx *data = (wxDirItemDataEx *) m_treeCtrl->GetItemData(parentId);
681
682 if (data->m_isExpanded)
683 return;
684
685 data->m_isExpanded = TRUE;
686
687 if (parentId == m_rootId)
688 {
689 SetupSections();
690 return;
691 }
692
693 wxASSERT(data);
694
695 wxString search,path,filename;
696
697 wxString dirName(data->m_path);
698
699 #if defined(__WXMSW__) || defined(__WXPM__)
700 // Check if this is a root directory and if so,
701 // whether the drive is avaiable.
702 if (!wxIsDriveAvailable(dirName))
703 {
704 data->m_isExpanded = FALSE;
705 //wxMessageBox(wxT("Sorry, this drive is not available."));
706 return;
707 }
708 #endif
709
710 // This may take a longish time. Go to busy cursor
711 wxBusyCursor busy;
712
713 #if defined(__WXMSW__) || defined(__WXPM__)
714 if (dirName.Last() == ':')
715 dirName += wxString(wxFILE_SEP_PATH);
716 #endif
717
718 wxArrayString dirs;
719 wxArrayString filenames;
720
721 wxDir d;
722 wxString eachFilename;
723
724 wxLogNull log;
725 d.Open(dirName);
726
727 if (d.IsOpened())
728 {
729 if (d.GetFirst(& eachFilename, wxEmptyString, wxDIR_DIRS))
730 {
731 do
732 {
733 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
734 {
735 dirs.Add(eachFilename);
736 }
737 }
738 while (d.GetNext(& eachFilename)) ;
739 }
740 }
741 dirs.Sort();
742
743 // Now do the filenames -- but only if we're allowed to
744 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
745 {
746 wxLogNull log;
747
748 d.Open(dirName);
749
750 if (d.IsOpened())
751 {
752 if (d.GetFirst(& eachFilename, m_currentFilterStr, wxDIR_FILES))
753 {
754 do
755 {
756 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
757 {
758 filenames.Add(eachFilename);
759 }
760 }
761 while (d.GetNext(& eachFilename)) ;
762 }
763 }
764 filenames.Sort();
765 }
766
767 // Add the sorted dirs
768 size_t i;
769 for (i = 0; i < dirs.Count(); i++)
770 {
771 wxString eachFilename(dirs[i]);
772 path = dirName;
773 if (path.Last() != wxFILE_SEP_PATH)
774 path += wxString(wxFILE_SEP_PATH);
775 path += eachFilename;
776
777 wxDirItemDataEx *dir_item = new wxDirItemDataEx(path,eachFilename,TRUE);
778 wxTreeItemId id = m_treeCtrl->AppendItem( parentId, eachFilename, 0, -1, dir_item);
779 m_treeCtrl->SetItemImage( id, 1, wxTreeItemIcon_Expanded );
780
781 // Has this got any children? If so, make it expandable.
782 int options = wxDIR_DEFAULT;
783 if (GetWindowStyle() & wxDIRCTRL_DIR_ONLY) // If only showing dirs, then we specify dirs only here
784 {
785 options = wxDIR_DIRS;
786 }
787
788 wxLogNull log;
789 wxDir dir2;
790 if (dir2.Open(path))
791 {
792 wxString str;
793 // Have to test for wxDIR_DIRS separately in case m_currentFilterStr is non-empty and
794 // and filters out any directories
795 if (dir2.GetFirst(& str, m_currentFilterStr, options) || dir2.GetFirst(& str, wxEmptyString, wxDIR_DIRS))
796 {
797 m_treeCtrl->SetItemHasChildren(id);
798 }
799 }
800 }
801
802 // Add the sorted filenames
803 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
804 {
805 for (i = 0; i < filenames.Count(); i++)
806 {
807 wxString eachFilename(filenames[i]);
808 path = dirName;
809 if (path.Last() != wxFILE_SEP_PATH)
810 path += wxString(wxFILE_SEP_PATH);
811 path += eachFilename;
812 //path = dirName + wxString(wxT("/")) + eachFilename;
813 wxDirItemDataEx *dir_item = new wxDirItemDataEx(path,eachFilename,FALSE);
814 (void)m_treeCtrl->AppendItem( parentId, eachFilename, 2, -1, dir_item);
815 }
816 }
817 }
818
819 // Find the child that matches the first part of 'path'.
820 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
821 // then the child for /usr is returned.
822 wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
823 {
824 wxString path2(path);
825
826 // Make sure all separators are as per the current platform
827 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
828 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
829
830 // Append a separator to foil bogus substring matching
831 path2 += wxString(wxFILE_SEP_PATH);
832
833 // In MSW or PM, case is not significant
834 #if defined(__WXMSW__) || defined(__WXPM__)
835 path2.MakeLower();
836 #endif
837
838 long cookie;
839 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
840 while (childId.IsOk())
841 {
842 wxDirItemDataEx* data = (wxDirItemDataEx*) m_treeCtrl->GetItemData(childId);
843
844 if (data && data->m_path != "")
845 {
846 wxString childPath(data->m_path);
847 if (childPath.Last() != wxFILE_SEP_PATH)
848 childPath += wxString(wxFILE_SEP_PATH);
849
850 // In MSW and PM, case is not significant
851 #if defined(__WXMSW__) || defined(__WXPM__)
852 childPath.MakeLower();
853 #endif
854
855 if (childPath.Len() <= path2.Len())
856 {
857 wxString path3 = path2.Mid(0, childPath.Len());
858 if (childPath == path3)
859 {
860 if (path3.Len() == path2.Len())
861 done = TRUE;
862 else
863 done = FALSE;
864 return childId;
865 }
866 }
867 }
868
869 childId = m_treeCtrl->GetNextChild(childId, cookie);
870 }
871 wxTreeItemId invalid;
872 return invalid;
873 }
874
875 // Try to expand as much of the given path as possible,
876 // and select the given tree item.
877 bool wxGenericDirCtrl::ExpandPath(const wxString& path)
878 {
879 bool done = FALSE;
880 wxTreeItemId id = FindChild(m_rootId, path, done);
881 wxTreeItemId lastId = id; // The last non-zero id
882 while (id.IsOk() && !done)
883 {
884 ExpandDir(id);
885
886 id = FindChild(id, path, done);
887 if (id.IsOk())
888 lastId = id;
889 }
890 if (lastId.IsOk())
891 {
892 wxDirItemDataEx *data = (wxDirItemDataEx *) m_treeCtrl->GetItemData(lastId);
893 if (data->m_isDir)
894 {
895 m_treeCtrl->Expand(lastId);
896 }
897 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
898 {
899 // Find the first file in this directory
900 long cookie;
901 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
902 bool selectedChild = FALSE;
903 while (childId.IsOk())
904 {
905 wxDirItemDataEx* data = (wxDirItemDataEx*) m_treeCtrl->GetItemData(childId);
906
907 if (data && data->m_path != "" && !data->m_isDir)
908 {
909 m_treeCtrl->SelectItem(childId);
910 m_treeCtrl->EnsureVisible(childId);
911 selectedChild = TRUE;
912 break;
913 }
914 childId = m_treeCtrl->GetNextChild(lastId, cookie);
915 }
916 if (!selectedChild)
917 {
918 m_treeCtrl->SelectItem(lastId);
919 m_treeCtrl->EnsureVisible(lastId);
920 }
921 }
922 else
923 {
924 m_treeCtrl->SelectItem(lastId);
925 m_treeCtrl->EnsureVisible(lastId);
926 }
927
928 return TRUE;
929 }
930 else
931 return FALSE;
932 }
933
934 wxString wxGenericDirCtrl::GetPath() const
935 {
936 wxTreeItemId id = m_treeCtrl->GetSelection();
937 if (id)
938 {
939 wxDirItemDataEx* data = (wxDirItemDataEx*) m_treeCtrl->GetItemData(id);
940 return data->m_path;
941 }
942 else
943 return wxEmptyString;
944 }
945
946 wxString wxGenericDirCtrl::GetFilePath() const
947 {
948 wxTreeItemId id = m_treeCtrl->GetSelection();
949 if (id)
950 {
951 wxDirItemDataEx* data = (wxDirItemDataEx*) m_treeCtrl->GetItemData(id);
952 if (data->m_isDir)
953 return wxEmptyString;
954 else
955 return data->m_path;
956 }
957 else
958 return wxEmptyString;
959 }
960
961 void wxGenericDirCtrl::SetPath(const wxString& path)
962 {
963 m_defaultPath = path;
964 if (m_rootId)
965 ExpandPath(path);
966 }
967
968 // Not used
969 #if 0
970 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
971 {
972 wxDirItemDataEx *data = (wxDirItemDataEx *) m_treeCtrl->GetItemData(id);
973
974 // This may take a longish time. Go to busy cursor
975 wxBusyCursor busy;
976
977 wxASSERT(data);
978
979 wxString search,path,filename;
980
981 wxString dirName(data->m_path);
982
983 #if defined(__WXMSW__) || defined(__WXPM__)
984 if (dirName.Last() == ':')
985 dirName += wxString(wxFILE_SEP_PATH);
986 #endif
987
988 wxDir d;
989 wxString eachFilename;
990
991 wxLogNull log;
992 d.Open(dirName);
993
994 if (d.IsOpened())
995 {
996 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
997 {
998 do
999 {
1000 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1001 {
1002 filenames.Add(eachFilename);
1003 }
1004 }
1005 while (d.GetNext(& eachFilename)) ;
1006 }
1007 }
1008 }
1009 #endif
1010
1011 void wxGenericDirCtrl::SetFilterIndex(int n)
1012 {
1013 m_currentFilter = n;
1014
1015 wxString f, d;
1016 if (ExtractWildcard(m_filter, n, f, d))
1017 m_currentFilterStr = f;
1018 else
1019 m_currentFilterStr = wxT("*.*");
1020 }
1021
1022 void wxGenericDirCtrl::SetFilter(const wxString& filter)
1023 {
1024 m_filter = filter;
1025
1026 wxString f, d;
1027 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1028 m_currentFilterStr = f;
1029 else
1030 m_currentFilterStr = wxT("*.*");
1031 }
1032
1033 // Extract description and actual filter from overall filter string
1034 bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1035 {
1036 wxArrayString filters, descriptions;
1037 int count = ParseFilter(filterStr, filters, descriptions);
1038 if (count > 0 && n < count)
1039 {
1040 filter = filters[n];
1041 description = descriptions[n];
1042 return TRUE;
1043 }
1044 else
1045 return FALSE;
1046 }
1047
1048 // Parses the global filter, returning the number of filters.
1049 // Returns 0 if none or if there's a problem.
1050 // filterStr is in the form:
1051 //
1052 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1053
1054 int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1055 {
1056 wxString str(filterStr);
1057
1058 wxString description, filter;
1059 int pos;
1060 bool finished = FALSE;
1061 do
1062 {
1063 pos = str.Find(wxT('|'));
1064 if (pos == -1)
1065 return 0; // Problem
1066 description = str.Left(pos);
1067 str = str.Mid(pos+1);
1068 pos = str.Find(wxT('|'));
1069 if (pos == -1)
1070 {
1071 filter = str;
1072 finished = TRUE;
1073 }
1074 else
1075 {
1076 filter = str.Left(pos);
1077 str = str.Mid(pos+1);
1078 }
1079 descriptions.Add(description);
1080 filters.Add(filter);
1081 }
1082 while (!finished) ;
1083
1084 return filters.Count();
1085 }
1086
1087 void wxGenericDirCtrl::DoResize()
1088 {
1089 wxSize sz = GetClientSize();
1090 int verticalSpacing = 3;
1091 if (m_treeCtrl)
1092 {
1093 wxSize filterSz ;
1094 if (m_filterListCtrl)
1095 {
1096 filterSz = m_filterListCtrl->GetSize();
1097 sz.y -= (filterSz.y + verticalSpacing);
1098 }
1099 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1100 if (m_filterListCtrl)
1101 {
1102 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1103 // Don't know why, but this needs refreshing after a resize (wxMSW)
1104 m_filterListCtrl->Refresh();
1105 }
1106 }
1107 }
1108
1109
1110 void wxGenericDirCtrl::OnSize(wxSizeEvent &event)
1111 {
1112 DoResize();
1113 }
1114
1115 //-----------------------------------------------------------------------------
1116 // wxDirFilterListCtrl
1117 //-----------------------------------------------------------------------------
1118
1119 IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1120
1121 BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1122 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter)
1123 END_EVENT_TABLE()
1124
1125 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1126 const wxPoint& pos,
1127 const wxSize& size,
1128 long style)
1129 {
1130 m_dirCtrl = parent;
1131 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1132 }
1133
1134 void wxDirFilterListCtrl::Init()
1135 {
1136 m_dirCtrl = NULL;
1137 }
1138
1139 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& event)
1140 {
1141 int sel = GetSelection();
1142
1143 wxString currentPath = m_dirCtrl->GetPath();
1144
1145 m_dirCtrl->SetFilterIndex(sel);
1146
1147 // If the filter has changed, the view is out of date, so
1148 // collapse the tree.
1149 m_dirCtrl->GetTreeCtrl()->Collapse(m_dirCtrl->GetRootId());
1150 m_dirCtrl->GetTreeCtrl()->Expand(m_dirCtrl->GetRootId());
1151
1152 // Try to restore the selection, or at least the directory
1153 m_dirCtrl->ExpandPath(currentPath);
1154 }
1155
1156 void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1157 {
1158 Clear();
1159 wxArrayString descriptions, filters;
1160 size_t n = (size_t) m_dirCtrl->ParseFilter(filter, filters, descriptions);
1161
1162 if (n > 0 && defaultFilter < (int) n)
1163 {
1164 size_t i = 0;
1165 for (i = 0; i < n; i++)
1166 Append(descriptions[i]);
1167 SetSelection(defaultFilter);
1168 }
1169 }
1170
1171 // wxGenericDirDialog implementation
1172 // This should be moved into dirdlgg.cpp eventually
1173
1174 BEGIN_EVENT_TABLE(wxGenericDirDialog, wxDialog)
1175 EVT_BUTTON(wxID_OK, wxGenericDirDialog::OnOK)
1176 EVT_BUTTON(wxID_NEW, wxGenericDirDialog::OnNew)
1177 EVT_BUTTON (wxID_NEW, wxGenericDirDialog::OnNew)
1178 EVT_CLOSE(wxGenericDirDialog::OnCloseWindow)
1179 EVT_TREE_KEY_DOWN (-1, wxGenericDirDialog::OnTreeKeyDown)
1180 EVT_TREE_SEL_CHANGED (-1, wxGenericDirDialog::OnTreeSelected)
1181 EVT_TEXT_ENTER (ID_TEXTCTRL, wxGenericDirDialog::OnOK)
1182 END_EVENT_TABLE()
1183
1184 wxGenericDirDialog::wxGenericDirDialog(wxWindow* parent, const wxString& title,
1185 const wxString& defaultPath, long style, const wxPoint& pos, const wxSize& sz, const wxString& name):
1186 wxDialog(parent, ID_DIRCTRL, title, pos, sz, style, name)
1187 {
1188 m_dirCtrl = NULL;
1189 m_path = defaultPath;
1190
1191 wxBusyCursor cursor;
1192
1193 wxBoxSizer *topsizer = new wxBoxSizer( wxVERTICAL );
1194
1195 // 1) dir ctrl
1196 m_dirCtrl = new wxGenericDirCtrl(this, ID_DIRCTRL,
1197 defaultPath, wxPoint(5, 5),
1198 wxSize(300, 200), wxDIRCTRL_DIR_ONLY|wxSUNKEN_BORDER);
1199
1200 topsizer->Add( m_dirCtrl, 1, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
1201
1202 // 2) text ctrl
1203 m_input = new wxTextCtrl( this, ID_TEXTCTRL, m_path, wxDefaultPosition );
1204 topsizer->Add( m_input, 0, wxTOP|wxLEFT|wxRIGHT | wxEXPAND, 10 );
1205
1206 #if wxUSE_STATLINE
1207 // 3) Static line
1208 topsizer->Add( new wxStaticLine( this, -1 ), 0, wxEXPAND | wxLEFT|wxRIGHT|wxTOP, 10 );
1209 #endif
1210
1211 // 4) Buttons
1212 wxSizer* buttonsizer = new wxBoxSizer( wxHORIZONTAL );
1213 wxButton* okButton = new wxButton(this, wxID_OK, _("OK"));
1214 buttonsizer->Add( okButton, 0, wxLEFT|wxRIGHT, 10 );
1215 wxButton* cancelButton = new wxButton(this, wxID_CANCEL, _("Cancel"));
1216 buttonsizer->Add( cancelButton, 0, wxLEFT|wxRIGHT, 10 );
1217
1218 // I'm not convinced we need a New button, and we tend to get annoying
1219 // accidental-editing with label editing enabled.
1220 #if 0
1221 wxButton* newButton = new wxButton( this, wxID_NEW, _("New...") );
1222 buttonsizer->Add( newButton, 0, wxLEFT|wxRIGHT, 10 );
1223 #endif
1224
1225 topsizer->Add( buttonsizer, 0, wxALL | wxCENTER, 10 );
1226
1227 okButton->SetDefault();
1228 m_dirCtrl->SetFocus();
1229
1230 SetAutoLayout( TRUE );
1231 SetSizer( topsizer );
1232
1233 topsizer->SetSizeHints( this );
1234 topsizer->Fit( this );
1235
1236 Centre( wxBOTH );
1237 }
1238
1239 void wxGenericDirDialog::OnCloseWindow(wxCloseEvent& event)
1240 {
1241 EndModal(wxID_CANCEL);
1242 }
1243
1244 void wxGenericDirDialog::OnOK(wxCommandEvent& event)
1245 {
1246 m_path = m_input->GetValue();
1247 // Does the path exist? (User may have typed anything in m_input)
1248 if (wxPathExists(m_path)) {
1249 // OK, path exists, we're done.
1250 EndModal(wxID_OK);
1251 return;
1252 }
1253 // Interact with user, find out if the dir is a typo or to be created
1254 wxString msg( _("The directory ") );
1255 msg = msg + m_path;
1256 msg = msg + _("\ndoes not exist\nCreate it now?") ;
1257 wxMessageDialog dialog(this, msg, _("Directory does not exist"), wxYES_NO | wxICON_WARNING );
1258 if ( dialog.ShowModal() == wxID_YES ) {
1259 // Okay, let's make it
1260 wxLogNull log;
1261 if (wxMkdir(m_path)) {
1262 // The new dir was created okay.
1263 EndModal(wxID_OK);
1264 return;
1265 }
1266 else {
1267 // Trouble...
1268 msg = _("Failed to create directory ")+m_path+
1269 _("\n(Do you have the required permissions?)");
1270 wxMessageDialog errmsg(this, msg, _("Error creating directory"), wxOK | wxICON_ERROR);
1271 errmsg.ShowModal();
1272 // We still don't have a valid dir. Back to the main dialog.
1273 }
1274 }
1275 // User has answered NO to create dir.
1276 }
1277
1278 void wxGenericDirDialog::SetPath(const wxString& path)
1279 {
1280 m_dirCtrl->SetPath(path);
1281 m_path = path;
1282 }
1283
1284 wxString wxGenericDirDialog::GetPath(void) const
1285 {
1286 return m_path;
1287 }
1288
1289 int wxGenericDirDialog::ShowModal()
1290 {
1291 //m_input->SetValue( m_path );
1292 return wxDialog::ShowModal();
1293 }
1294
1295 void wxGenericDirDialog::OnTreeSelected( wxTreeEvent &event )
1296 {
1297 if (!m_dirCtrl)
1298 return;
1299
1300 wxDirItemDataEx *data = (wxDirItemDataEx*)m_dirCtrl->GetTreeCtrl()->GetItemData(event.GetItem());
1301 if (data)
1302 m_input->SetValue( data->m_path );
1303 };
1304
1305 void wxGenericDirDialog::OnTreeKeyDown( wxTreeEvent &WXUNUSED(event) )
1306 {
1307 if (!m_dirCtrl)
1308 return;
1309
1310 wxDirItemDataEx *data = (wxDirItemDataEx*)m_dirCtrl->GetTreeCtrl()->GetItemData(m_dirCtrl->GetTreeCtrl()->GetSelection());
1311 if (data)
1312 m_input->SetValue( data->m_path );
1313 };
1314
1315 void wxGenericDirDialog::OnNew( wxCommandEvent& WXUNUSED(event) )
1316 {
1317 wxTreeItemId id = m_dirCtrl->GetTreeCtrl()->GetSelection();
1318 if ((id == m_dirCtrl->GetTreeCtrl()->GetRootItem()) ||
1319 (m_dirCtrl->GetTreeCtrl()->GetParent(id) == m_dirCtrl->GetTreeCtrl()->GetRootItem()))
1320 {
1321 wxMessageDialog msg(this, _("You cannot add a new directory to this section."),
1322 _("Create directory"), wxOK | wxICON_INFORMATION );
1323 msg.ShowModal();
1324 return;
1325 }
1326
1327 wxTreeItemId parent = id ; // m_dirCtrl->GetTreeCtrl()->GetParent( id );
1328 wxDirItemDataEx *data = (wxDirItemDataEx*)m_dirCtrl->GetTreeCtrl()->GetItemData( parent );
1329 wxASSERT( data );
1330
1331 wxString new_name( wxT("NewName") );
1332 wxString path( data->m_path );
1333 if (path.Last() != wxFILE_SEP_PATH)
1334 path += wxFILE_SEP_PATH;
1335 path += new_name;
1336 if (wxFileExists(path))
1337 {
1338 // try NewName0, NewName1 etc.
1339 int i = 0;
1340 do {
1341 new_name = wxT("NewName");
1342 wxString num;
1343 num.Printf( wxT("%d"), i );
1344 new_name += num;
1345
1346 path = data->m_path;
1347 if (path.Last() != wxFILE_SEP_PATH)
1348 path += wxFILE_SEP_PATH;
1349 path += new_name;
1350 i++;
1351 } while (wxFileExists(path));
1352 }
1353
1354 wxLogNull log;
1355 if (!wxMkdir(path))
1356 {
1357 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
1358 dialog.ShowModal();
1359 return;
1360 }
1361
1362 wxDirItemDataEx *new_data = new wxDirItemDataEx( path, new_name, TRUE );
1363
1364 // TODO: THIS CODE DOESN'T WORK YET. We need to avoid duplication of the first child
1365 // of the parent.
1366 wxTreeItemId new_id = m_dirCtrl->GetTreeCtrl()->AppendItem( parent, new_name, 0, 0, new_data );
1367 m_dirCtrl->GetTreeCtrl()->EnsureVisible( new_id );
1368 m_dirCtrl->GetTreeCtrl()->EditLabel( new_id );
1369 }