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