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