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