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