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