]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
Removed tabs from applet sample code.
[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
ec1b28a3 79#if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW || wxUSE_XPM_IN_OS2
51a58d8b
JS
80/* Closed folder */
81static char * icon1_xpm[] = {
82/* width height ncolors chars_per_pixel */
83"16 16 6 1",
84/* colors */
85" s None c None",
86". c #000000",
87"+ c #c0c0c0",
88"@ c #808080",
89"# c #ffff00",
90"$ c #ffffff",
91/* pixels */
92" ",
93" @@@@@ ",
94" @#+#+#@ ",
95" @#+#+#+#@@@@@@ ",
96" @$$$$$$$$$$$$@.",
97" @$#+#+#+#+#+#@.",
98" @$+#+#+#+#+#+@.",
99" @$#+#+#+#+#+#@.",
100" @$+#+#+#+#+#+@.",
101" @$#+#+#+#+#+#@.",
102" @$+#+#+#+#+#+@.",
103" @$#+#+#+#+#+#@.",
104" @@@@@@@@@@@@@@.",
105" ..............",
106" ",
107" "};
108
109/* Open folder */
110static char * icon2_xpm[] = {
111/* width height ncolors chars_per_pixel */
112"16 16 6 1",
113/* colors */
114" s None c None",
115". c #000000",
116"+ c #c0c0c0",
117"@ c #808080",
118"# c #ffff00",
119"$ c #ffffff",
120/* pixels */
121" ",
122" @@@@@ ",
123" @$$$$$@ ",
124" @$#+#+#$@@@@@@ ",
125" @$+#+#+$$$$$$@.",
126" @$#+#+#+#+#+#@.",
127"@@@@@@@@@@@@@#@.",
128"@$$$$$$$$$$@@+@.",
129"@$#+#+#+#+##.@@.",
130" @$#+#+#+#+#+.@.",
131" @$+#+#+#+#+#.@.",
132" @$+#+#+#+##@..",
133" @@@@@@@@@@@@@.",
134" .............",
135" ",
136" "};
137
138/* File */
139static char * icon3_xpm[] = {
140/* width height ncolors chars_per_pixel */
141"16 16 3 1",
142/* colors */
33fed835
MB
143" s None c None",
144". c #000000",
145"+ c #ffffff",
51a58d8b
JS
146/* pixels */
147" ",
148" ........ ",
149" .++++++.. ",
150" .+.+.++.+. ",
151" .++++++.... ",
152" .+.+.+++++. ",
153" .+++++++++. ",
154" .+.+.+.+.+. ",
155" .+++++++++. ",
156" .+.+.+.+.+. ",
157" .+++++++++. ",
158" .+.+.+.+.+. ",
159" .+++++++++. ",
160" ........... ",
161" ",
162" "};
163
164/* Computer */
165static char * icon4_xpm[] = {
166"16 16 7 1",
33fed835
MB
167" s None c None",
168". c #808080",
169"X c #c0c0c0",
170"o c Black",
171"O c Gray100",
172"+ c #008080",
173"@ c Blue",
51a58d8b
JS
174" ........... ",
175" .XXXXXXXXXX.o",
176" .OOOOOOOOO..o",
177" .OoooooooX..o",
178" .Oo+...@+X..o",
179" .Oo+XXX.+X..o",
180" .Oo+....+X..o",
181" .Oo++++++X..o",
182" .OXXXXXXXX.oo",
183" ..........o.o",
184" ...........Xo",
185" .XXXXXXXXXX.o",
186" .o.o.o.o.o...o",
187" .oXoXoXoXoXo.o ",
188".XOXXXXXXXXX.o ",
189"............o "};
190
191/* Drive */
192static char * icon5_xpm[] = {
193"16 16 7 1",
33fed835
MB
194" s None c None",
195". c #808080",
196"X c #c0c0c0",
197"o c Black",
198"O c Gray100",
199"+ c Green",
200"@ c #008000",
51a58d8b
JS
201" ",
202" ",
203" ",
204" ",
205" ............. ",
206" .XXXXXXXXXXXX.o",
207".OOOOOOOOOOOO..o",
208".XXXXXXXXX+@X..o",
209".XXXXXXXXXXXX..o",
210".X..........X..o",
211".XOOOOOOOOOOX..o",
212"..............o ",
213" ooooooooooooo ",
214" ",
215" ",
216" "};
217
218/* CD-ROM */
219static char *icon6_xpm[] = {
220"16 16 10 1",
33fed835
MB
221" s None c None",
222". c #808080",
223"X c #c0c0c0",
224"o c Yellow",
225"O c Blue",
226"+ c Black",
227"@ c Gray100",
228"# c #008080",
229"$ c Green",
230"% c #008000",
51a58d8b
JS
231" ... ",
232" ..XoX.. ",
233" .O.XoXXX+ ",
234" ...O.oXXXX+ ",
235" .O..X.XXXX+ ",
236" ....X.+..XXX+",
237" .XXX.+@+.XXX+",
238" .X@XX.+.X@@X+",
239" .....X...#XX@+ ",
240".@@@...XXo.O@X+ ",
241".@XXX..XXoXOO+ ",
242".@++++..XoX+++ ",
243".@$%@@XX+++X.+ ",
244".............+ ",
245" ++++++++++++ ",
246" "};
247
248/* Floppy */
249static char * icon7_xpm[] = {
250"16 16 7 1",
33fed835
MB
251" s None c None",
252". c #808080",
253"X c Gray100",
254"o c #c0c0c0",
255"O c Black",
256"+ c Cyan",
257"@ c Red",
51a58d8b
JS
258" ......X",
259" .ooooooO",
260" .+++++OO",
261" .++++++O",
262" .++++++O",
263" .ooooooO",
264" .......o....oO",
265" .oooooo.o.O.XoO",
266".XXXXXXXXOOOOOO ",
267".ooooooooo@o..O ",
268".ooo....oooo..O ",
269".o..OOOO...o..O ",
270".oooXXXXoooo..O ",
271".............O ",
272" OOOOOOOOOOOO ",
273" "};
274
275/* Removeable */
276static char * icon8_xpm[] = {
277"16 16 7 1",
33fed835
MB
278" s None c None",
279". c #808080",
280"X c #c0c0c0",
281"o c Black",
282"O c Gray100",
283"+ c Red",
284"@ c #800000",
51a58d8b
JS
285" ",
286" ",
287" ",
288" ............. ",
289" .XXXXXXXXXXXX.o",
290".OOOOOOOOOOOO..o",
291".OXXXXXXXXXXX..o",
292".O+@.oooooo.X..o",
293".OXXOooooooOX..o",
294".OXXXOOOOOOXX..o",
295".OXXXXXXXXXXX..o",
296".O............o ",
297" ooooooooooooo ",
298" ",
299" ",
300" "};
301#endif // !wxMSW
302
303static const int ID_DIRCTRL = 1000;
304static const int ID_TEXTCTRL = 1001;
305static const int ID_OK = 1002;
306static const int ID_CANCEL = 1003;
307static const int ID_NEW = 1004;
308//static const int ID_CHECK = 1005;
309
ec1b28a3 310#if defined(__WXMSW__) || defined(__WXPM__)
33ac7e6f
KB
311int setdrive(int drive)
312{
313 char newdrive[3];
314
315 if (drive < 1 || drive > 31)
316 return -1;
317 newdrive[0] = (char)('A' + (char)drive - (char)1);
318 newdrive[1] = ':';
319 newdrive[2] = '\0';
320 if (SetCurrentDirectory((LPSTR)newdrive))
321 return 0;
322 else
323 return -1;
324}
325
ad9cd15c
JS
326static bool wxIsDriveAvailable(const wxString dirName)
327{
328#ifdef __WIN32__
33fed835 329 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
ad9cd15c 330#endif
33fed835 331 bool success = TRUE;
ad9cd15c
JS
332
333 // Check if this is a root directory and if so,
334 // whether the drive is avaiable.
33fed835 335 if (dirName.Len() == 3 && dirName[(size_t)1] == wxT(':'))
ad9cd15c 336 {
33fed835 337 wxString dirNameLower(dirName.Lower());
7328394a
JS
338#if defined(__GNUWIN32__)
339 success = wxPathExists(dirNameLower);
340#else
ad9cd15c 341 int currentDrive = _getdrive();
33fed835 342 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
33ac7e6f
KB
343 int err = setdrive( thisDrive ) ;
344 setdrive( currentDrive );
ad9cd15c
JS
345
346 if (err == -1)
347 {
348 success = FALSE;
349 }
7328394a 350#endif
ad9cd15c
JS
351 }
352#ifdef __WIN32__
33fed835 353 (void) SetErrorMode(errorMode);
ad9cd15c
JS
354#endif
355
33fed835 356 return success;
ad9cd15c
JS
357}
358#endif
359
51a58d8b
JS
360//-----------------------------------------------------------------------------
361// wxDirItemDataEx
362//-----------------------------------------------------------------------------
363
364wxDirItemDataEx::wxDirItemDataEx(const wxString& path, const wxString& name,
365 bool isDir)
366{
367 m_path = path;
368 m_name = name;
369 /* Insert logic to detect hidden files here
370 * In UnixLand we just check whether the first char is a dot
371 * For FileNameFromPath read LastDirNameInThisPath ;-) */
372 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
373 m_isHidden = FALSE;
e63fdcd6
JS
374 // m_hasSubDirs is no longer needed
375 m_hasSubDirs = TRUE; // HasSubDirs();
51a58d8b
JS
376 m_isExpanded = FALSE;
377 m_isDir = isDir;
378}
379
380wxDirItemDataEx::~wxDirItemDataEx()
381{
382}
383
384void wxDirItemDataEx::SetNewDirName( wxString path )
385{
386 m_path = path;
387 m_name = wxFileNameFromPath( path );
388}
389
51a58d8b
JS
390//-----------------------------------------------------------------------------
391// wxGenericDirCtrl
392//-----------------------------------------------------------------------------
393
394IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
395
396BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
397 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem)
398 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem)
399 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem)
400 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem)
401 EVT_SIZE (wxGenericDirCtrl::OnSize)
402END_EVENT_TABLE()
403
404wxGenericDirCtrl::wxGenericDirCtrl(void)
405{
406 Init();
407}
408
409bool wxGenericDirCtrl::Create(wxWindow *parent,
410 const wxWindowID id,
411 const wxString& dir,
412 const wxPoint& pos,
413 const wxSize& size,
414 long style,
415 const wxString& filter,
416 int defaultFilter,
417 const wxString& name )
418{
419 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
420 return FALSE;
421
422 SetBackgroundColour(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE));
423
424 Init();
425
3438012b 426 long treeStyle = wxTR_HAS_BUTTONS ; // | wxTR_EDIT_LABELS;
51a58d8b
JS
427 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
428 treeStyle |= wxNO_BORDER;
429
430 long filterStyle = 0;
431 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
432 filterStyle |= wxNO_BORDER;
433
434 m_treeCtrl = new wxTreeCtrl(this, wxID_TREECTRL, pos, size, treeStyle);
435
436 if (!filter.IsEmpty() && (style & wxDIRCTRL_SHOW_FILTERS))
437 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
438
439 m_defaultPath = dir;
440 m_filter = filter;
441
442 SetFilterIndex(defaultFilter);
443
444 if (m_filterListCtrl)
445 m_filterListCtrl->FillFilterList(filter, defaultFilter);
446
447 m_imageList = new wxImageList(16, 16, TRUE);
448#if !defined(__WXMSW__) || wxUSE_XPM_IN_MSW
449 m_imageList->Add(wxIcon(icon1_xpm));
450 m_imageList->Add(wxIcon(icon2_xpm));
451 m_imageList->Add(wxIcon(icon3_xpm));
452 m_imageList->Add(wxIcon(icon4_xpm));
453 m_imageList->Add(wxIcon(icon5_xpm));
454 m_imageList->Add(wxIcon(icon6_xpm));
455 m_imageList->Add(wxIcon(icon7_xpm));
456 m_imageList->Add(wxIcon(icon8_xpm));
457#elif defined(__WXMSW__)
bfc38e43
JS
458 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CLOSED_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
459 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_OPEN_FOLDER"), wxBITMAP_TYPE_ICO_RESOURCE));
460 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FILE"), wxBITMAP_TYPE_ICO_RESOURCE));
461 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_COMPUTER"), wxBITMAP_TYPE_ICO_RESOURCE));
462 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_DRIVE"), wxBITMAP_TYPE_ICO_RESOURCE));
463 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_CDROM"), wxBITMAP_TYPE_ICO_RESOURCE));
464 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_FLOPPY"), wxBITMAP_TYPE_ICO_RESOURCE));
465 m_imageList->Add(wxIcon(wxT("wxICON_SMALL_REMOVEABLE"), wxBITMAP_TYPE_ICO_RESOURCE));
51a58d8b
JS
466#else
467#error "Sorry, we don't have icons available for this platforms."
468#endif
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 }
756 dirs.Sort();
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 }
779 filenames.Sort();
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}