1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/dirctrlg.cpp
3 // Purpose: wxGenericDirCtrl
4 // Author: Harm van der Heijden, Robert Roebling, Julian Smart
8 // Copyright: (c) Harm van der Heijden, Robert Roebling and Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_DIRDLG || wxUSE_FILEDLG
21 #include "wx/generic/dirctrlg.h"
22 #include "wx/module.h"
24 #include "wx/button.h"
25 #include "wx/layout.h"
26 #include "wx/msgdlg.h"
27 #include "wx/textctrl.h"
28 #include "wx/textdlg.h"
29 #include "wx/filefn.h"
30 #include "wx/cmndata.h"
31 #include "wx/gdicmn.h"
33 #include "wx/imaglist.h"
37 #include "wx/tokenzr.h"
39 #include "wx/settings.h"
40 #include "wx/artprov.h"
42 #include "wx/mimetype.h"
44 #include "wx/choice.h"
47 #include "wx/statline.h"
50 #if defined(__WXMAC__)
51 #include "wx/mac/private.h" // includes mac headers
56 #include "wx/msw/winundef.h"
58 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
59 // older releases don't, but it should be verified and the checks modified
61 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
62 #if !defined(__WXWINCE__)
71 #if defined(__OS2__) || defined(__DOS__)
81 extern bool wxIsDriveAvailable(const wxString
& dirName
);
84 #if defined(__WXMAC__)
85 # include "MoreFilesX.h"
92 // If compiled under Windows, this macro can cause problems
97 // ----------------------------------------------------------------------------
98 // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
99 // ----------------------------------------------------------------------------
101 size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
)
103 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
106 // No logical drives; return "\"
107 paths
.Add(wxT("\\"));
108 names
.Add(wxT("\\"));
109 icon_ids
.Add(wxFileIconsTable::computer
);
110 #elif defined(__WIN32__)
111 wxChar driveBuffer
[256];
112 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
117 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
118 name
.Printf(wxT("%c:"), driveBuffer
[i
]);
120 #if !defined(__WXWINCE__)
121 wxChar pname
[52]; // FIXME: why 52 and not MAX_PATH or whatever?
122 if ( GetVolumeInformation(path
, pname
, WXSIZEOF(pname
),
123 NULL
, NULL
, NULL
, NULL
, 0) )
125 name
<< _T(' ') << pname
;
127 #endif // __WXWINCE__
130 int driveType
= ::GetDriveType(path
);
133 case DRIVE_REMOVABLE
:
134 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
135 imageId
= wxFileIconsTable::floppy
;
137 imageId
= wxFileIconsTable::removeable
;
140 imageId
= wxFileIconsTable::cdrom
;
145 imageId
= wxFileIconsTable::drive
;
151 icon_ids
.Add(imageId
);
153 while (driveBuffer
[i
] != wxT('\0'))
156 if (driveBuffer
[i
] == wxT('\0'))
159 #elif defined(__OS2__)
161 ULONG ulDriveNum
= 0;
162 ULONG ulDriveMap
= 0;
163 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
169 if (ulDriveMap
& ( 1 << i
))
172 path
.Printf(wxT("%c:\\"), 'A' + i
);
173 name
.Printf(wxT("%c:"), 'A' + i
);
175 // Note: If _filesys is unsupported by some compilers,
176 // we can always replace it by DosQueryFSAttach
177 char filesysname
[20];
179 ULONG cbBuffer
= sizeof(filesysname
);
180 PFSQBUFFER2 pfsqBuffer
= (PFSQBUFFER2
)filesysname
;
181 APIRET rc
= ::DosQueryFSAttach(name
.fn_str(),0,FSAIL_QUERYNAME
,pfsqBuffer
,&cbBuffer
);
184 filesysname
[0] = '\0';
187 _filesys(name
.fn_str(), filesysname
, sizeof(filesysname
));
189 /* FAT, LAN, HPFS, CDFS, NFS */
191 if (path
== wxT("A:\\") || path
== wxT("B:\\"))
192 imageId
= wxFileIconsTable::floppy
;
193 else if (!strcmp(filesysname
, "CDFS"))
194 imageId
= wxFileIconsTable::cdrom
;
195 else if (!strcmp(filesysname
, "LAN") ||
196 !strcmp(filesysname
, "NFS"))
197 imageId
= wxFileIconsTable::drive
;
199 imageId
= wxFileIconsTable::drive
;
202 icon_ids
.Add(imageId
);
207 #else // !__WIN32__, !__OS2__
210 /* If we can switch to the drive, it exists. */
211 for( drive
= 1; drive
<= 26; drive
++ )
214 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
215 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
217 if (wxIsDriveAvailable(path
))
221 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
224 #endif // __WIN32__/!__WIN32__
226 #elif defined(__WXMAC__)
228 ItemCount volumeIndex
= 1;
231 while( noErr
== err
)
233 HFSUniStr255 volumeName
;
235 FSVolumeInfo volumeInfo
;
236 err
= FSGetVolumeInfo(0, volumeIndex
, NULL
, kFSVolInfoFlags
, &volumeInfo
, &volumeName
, &fsRef
);
239 wxString path
= wxMacFSRefToPath( &fsRef
) ;
240 wxString name
= wxMacHFSUniStrToString( &volumeName
) ;
242 if ( (volumeInfo
.flags
& kFSVolFlagSoftwareLockedMask
) || (volumeInfo
.flags
& kFSVolFlagHardwareLockedMask
) )
244 icon_ids
.Add(wxFileIconsTable::cdrom
);
248 icon_ids
.Add(wxFileIconsTable::drive
);
250 // todo other removable
258 #elif defined(__UNIX__)
261 icon_ids
.Add(wxFileIconsTable::computer
);
263 #error "Unsupported platform in wxGenericDirCtrl!"
265 wxASSERT_MSG( (paths
.GetCount() == names
.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
266 wxASSERT_MSG( (paths
.GetCount() == icon_ids
.GetCount()), wxT("Wrong number of icons for available drives."));
267 return paths
.GetCount();
270 // ----------------------------------------------------------------------------
271 // wxIsDriveAvailable
272 // ----------------------------------------------------------------------------
276 bool wxIsDriveAvailable(const wxString
& dirName
)
278 // FIXME_MGL - this method leads to hang up under Watcom for some reason
280 wxUnusedVar(dirName
);
282 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
284 wxString
dirNameLower(dirName
.Lower());
285 // VS: always return true for removable media, since Win95 doesn't
286 // like it when MS-DOS app accesses empty floppy drive
287 return (dirNameLower
[0u] == wxT('a') ||
288 dirNameLower
[0u] == wxT('b') ||
289 wxDirExists(dirNameLower
));
296 #elif defined(__WINDOWS__) || defined(__OS2__)
298 int setdrive(int WXUNUSED_IN_WINCE(drive
))
302 #elif defined(__GNUWIN32__) && \
303 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
304 return _chdrive(drive
);
308 if (drive
< 1 || drive
> 31)
310 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
311 newdrive
[1] = wxT(':');
313 newdrive
[2] = wxT('\\');
314 newdrive
[3] = wxT('\0');
316 newdrive
[2] = wxT('\0');
318 #if defined(__WXMSW__)
319 if (::SetCurrentDirectory(newdrive
))
321 // VA doesn't know what LPSTR is and has its own set
322 if (!DosSetCurrentDir((PSZ
)newdrive
))
330 bool wxIsDriveAvailable(const wxString
& WXUNUSED_IN_WINCE(dirName
))
336 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
340 // Check if this is a root directory and if so,
341 // whether the drive is available.
342 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
344 wxString
dirNameLower(dirName
.Lower());
345 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
346 success
= wxDirExists(dirNameLower
);
349 // Avoid changing to drive since no media may be inserted.
350 if (dirNameLower
[(size_t)0] == 'a' || dirNameLower
[(size_t)0] == 'b')
353 int currentDrive
= _getdrive();
354 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
355 int err
= setdrive( thisDrive
) ;
356 setdrive( currentDrive
);
365 (void) SetErrorMode(errorMode
);
371 #endif // __WINDOWS__ || __OS2__
373 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
379 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
380 // and sort regardless of case.
381 static int wxCMPFUNC_CONV
wxDirCtrlStringCompareFunction(const wxString
& strFirst
, const wxString
& strSecond
)
383 return strFirst
.CmpNoCase(strSecond
);
386 //-----------------------------------------------------------------------------
388 //-----------------------------------------------------------------------------
390 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
395 /* Insert logic to detect hidden files here
396 * In UnixLand we just check whether the first char is a dot
397 * For FileNameFromPath read LastDirNameInThisPath ;-) */
398 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
400 m_isExpanded
= false;
404 void wxDirItemData::SetNewDirName(const wxString
& path
)
407 m_name
= wxFileNameFromPath(path
);
410 bool wxDirItemData::HasSubDirs() const
418 if ( !dir
.Open(m_path
) )
422 return dir
.HasSubDirs();
425 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
433 if ( !dir
.Open(m_path
) )
437 return dir
.HasFiles();
440 //-----------------------------------------------------------------------------
442 //-----------------------------------------------------------------------------
445 #if wxUSE_EXTENDED_RTTI
446 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle
)
448 wxBEGIN_FLAGS( wxGenericDirCtrlStyle
)
449 // new style border flags, we put them first to
450 // use them for streaming out
451 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
452 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
453 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
454 wxFLAGS_MEMBER(wxBORDER_RAISED
)
455 wxFLAGS_MEMBER(wxBORDER_STATIC
)
456 wxFLAGS_MEMBER(wxBORDER_NONE
)
458 // old style border flags
459 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
460 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
461 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
462 wxFLAGS_MEMBER(wxRAISED_BORDER
)
463 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
464 wxFLAGS_MEMBER(wxBORDER
)
466 // standard window styles
467 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
468 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
469 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
470 wxFLAGS_MEMBER(wxWANTS_CHARS
)
471 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
472 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
473 wxFLAGS_MEMBER(wxVSCROLL
)
474 wxFLAGS_MEMBER(wxHSCROLL
)
476 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY
)
477 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL
)
478 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST
)
479 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS
)
481 wxEND_FLAGS( wxGenericDirCtrlStyle
)
483 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl
, wxControl
,"wx/dirctrl.h")
485 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl
)
486 wxHIDE_PROPERTY( Children
)
487 wxPROPERTY( DefaultPath
, wxString
, SetDefaultPath
, GetDefaultPath
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
488 wxPROPERTY( Filter
, wxString
, SetFilter
, GetFilter
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
489 wxPROPERTY( DefaultFilter
, int , SetFilterIndex
, GetFilterIndex
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
490 wxPROPERTY_FLAGS( WindowStyle
, wxGenericDirCtrlStyle
, long, SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0, wxT("Helpstring"), wxT("group") )
491 wxEND_PROPERTIES_TABLE()
493 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl
)
494 wxEND_HANDLERS_TABLE()
496 wxCONSTRUCTOR_8( wxGenericDirCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, DefaultPath
,
497 wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
, wxString
, Filter
, int , DefaultFilter
)
499 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
502 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
503 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL
, wxGenericDirCtrl::OnExpandItem
)
504 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL
, wxGenericDirCtrl::OnCollapseItem
)
505 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnBeginEditItem
)
506 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnEndEditItem
)
507 EVT_SIZE (wxGenericDirCtrl::OnSize
)
510 wxGenericDirCtrl::wxGenericDirCtrl(void)
515 void wxGenericDirCtrl::ExpandRoot()
517 ExpandDir(m_rootId
); // automatically expand first level
519 // Expand and select the default path
520 if (!m_defaultPath
.empty())
522 ExpandPath(m_defaultPath
);
527 // On Unix, there's only one node under the (hidden) root node. It
528 // represents the / path, so the user would always have to expand it;
529 // let's do it ourselves
530 ExpandPath( wxT("/") );
535 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
541 const wxString
& filter
,
543 const wxString
& name
)
545 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
548 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
552 long treeStyle
= wxTR_HAS_BUTTONS
;
554 // On Windows CE, if you hide the root, you get a crash when
555 // attempting to access data for children of the root item.
557 treeStyle
|= wxTR_HIDE_ROOT
;
561 treeStyle
|= wxTR_NO_LINES
;
564 if (style
& wxDIRCTRL_EDIT_LABELS
)
565 treeStyle
|= wxTR_EDIT_LABELS
;
567 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
568 treeStyle
|= wxNO_BORDER
;
570 treeStyle
|= wxBORDER_SUNKEN
;
572 long filterStyle
= 0;
573 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
574 filterStyle
|= wxNO_BORDER
;
576 filterStyle
|= wxBORDER_SUNKEN
;
578 m_treeCtrl
= CreateTreeCtrl(this, wxID_TREECTRL
,
579 wxPoint(0,0), GetClientSize(), treeStyle
);
581 if (!filter
.empty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
582 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
587 if (m_filter
.empty())
591 m_filter
= wxT("*.*");
594 SetFilterIndex(defaultFilter
);
596 if (m_filterListCtrl
)
597 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
599 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
601 m_showHidden
= false;
602 wxDirItemData
* rootData
= new wxDirItemData(wxEmptyString
, wxEmptyString
, true);
606 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
607 rootName
= _("Computer");
609 rootName
= _("Sections");
612 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
613 m_treeCtrl
->SetItemHasChildren(m_rootId
);
623 wxGenericDirCtrl::~wxGenericDirCtrl()
627 void wxGenericDirCtrl::Init()
629 m_showHidden
= false;
631 m_currentFilterStr
= wxEmptyString
; // Default: any file
633 m_filterListCtrl
= NULL
;
636 wxTreeCtrl
* wxGenericDirCtrl::CreateTreeCtrl(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long treeStyle
)
638 return new wxTreeCtrl(parent
, id
, pos
, size
, treeStyle
);
641 void wxGenericDirCtrl::ShowHidden( bool show
)
645 wxString path
= GetPath();
651 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
653 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,true);
655 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
657 m_treeCtrl
->SetItemHasChildren(id
);
662 void wxGenericDirCtrl::SetupSections()
664 wxArrayString paths
, names
;
667 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
670 wxString home
= wxGetHomeDir();
671 AddSection( home
, _("Home directory"), 1);
672 home
+= wxT("/Desktop");
673 AddSection( home
, _("Desktop"), 1);
676 for (n
= 0; n
< count
; n
++)
677 AddSection(paths
[n
], names
[n
], icons
[n
]);
680 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
682 // don't rename the main entry "Sections"
683 if (event
.GetItem() == m_rootId
)
689 // don't rename the individual sections
690 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
697 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
699 if ((event
.GetLabel().empty()) ||
700 (event
.GetLabel() == _(".")) ||
701 (event
.GetLabel() == _("..")) ||
702 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
703 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
704 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
706 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
712 wxTreeItemId id
= event
.GetItem();
713 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
716 wxString
new_name( wxPathOnly( data
->m_path
) );
717 new_name
+= wxString(wxFILE_SEP_PATH
);
718 new_name
+= event
.GetLabel();
722 if (wxFileExists(new_name
))
724 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
729 if (wxRenameFile(data
->m_path
,new_name
))
731 data
->SetNewDirName( new_name
);
735 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
741 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
743 wxTreeItemId parentId
= event
.GetItem();
745 // VS: this is needed because the event handler is called from wxTreeCtrl
746 // ctor when wxTR_HIDE_ROOT was specified
748 if (!m_rootId
.IsOk())
750 m_rootId
= m_treeCtrl
->GetRootItem();
755 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
757 CollapseDir(event
.GetItem());
760 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
764 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
765 if (!data
->m_isExpanded
)
768 data
->m_isExpanded
= false;
769 wxTreeItemIdValue cookie
;
770 /* Workaround because DeleteChildren has disapeared (why?) and
771 * CollapseAndReset doesn't work as advertised (deletes parent too) */
772 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
775 m_treeCtrl
->Delete(child
);
776 /* Not GetNextChild below, because the cookie mechanism can't
777 * handle disappearing children! */
778 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
780 if (parentId
!= m_treeCtrl
->GetRootItem())
781 m_treeCtrl
->Collapse(parentId
);
784 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
786 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
788 if (data
->m_isExpanded
)
791 data
->m_isExpanded
= true;
793 if (parentId
== m_treeCtrl
->GetRootItem())
801 wxString search
,path
,filename
;
803 wxString
dirName(data
->m_path
);
805 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
806 // Check if this is a root directory and if so,
807 // whether the drive is avaiable.
808 if (!wxIsDriveAvailable(dirName
))
810 data
->m_isExpanded
= false;
811 //wxMessageBox(wxT("Sorry, this drive is not available."));
816 // This may take a longish time. Go to busy cursor
819 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
820 if (dirName
.Last() == ':')
821 dirName
+= wxString(wxFILE_SEP_PATH
);
825 wxArrayString filenames
;
828 wxString eachFilename
;
835 int style
= wxDIR_DIRS
;
836 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
837 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
841 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
843 dirs
.Add(eachFilename
);
846 while (d
.GetNext(&eachFilename
));
849 dirs
.Sort(wxDirCtrlStringCompareFunction
);
851 // Now do the filenames -- but only if we're allowed to
852 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
858 int style
= wxDIR_FILES
;
859 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
860 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
861 wxStringTokenizer strTok
;
863 strTok
.SetString(m_currentFilterStr
,wxT(";"));
864 while(strTok
.HasMoreTokens())
866 curFilter
= strTok
.GetNextToken();
867 if (d
.GetFirst(& eachFilename
, curFilter
, style
))
871 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
873 filenames
.Add(eachFilename
);
876 while (d
.GetNext(& eachFilename
));
880 filenames
.Sort(wxDirCtrlStringCompareFunction
);
883 // Add the sorted dirs
885 for (i
= 0; i
< dirs
.Count(); i
++)
887 eachFilename
= dirs
[i
];
889 if (!wxEndsWithPathSeparator(path
))
890 path
+= wxString(wxFILE_SEP_PATH
);
891 path
+= eachFilename
;
893 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,true);
894 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
895 wxFileIconsTable::folder
, -1, dir_item
);
896 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
897 wxTreeItemIcon_Expanded
);
899 // Has this got any children? If so, make it expandable.
900 // (There are two situations when a dir has children: either it
901 // has subdirectories or it contains files that weren't filtered
902 // out. The latter only applies to dirctrl with files.)
903 if ( dir_item
->HasSubDirs() ||
904 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
905 dir_item
->HasFiles(m_currentFilterStr
)) )
907 m_treeCtrl
->SetItemHasChildren(id
);
911 // Add the sorted filenames
912 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
914 for (i
= 0; i
< filenames
.Count(); i
++)
916 eachFilename
= filenames
[i
];
918 if (!wxEndsWithPathSeparator(path
))
919 path
+= wxString(wxFILE_SEP_PATH
);
920 path
+= eachFilename
;
921 //path = dirName + wxString(wxT("/")) + eachFilename;
922 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,false);
923 int image_id
= wxFileIconsTable::file
;
924 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
925 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
926 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
931 void wxGenericDirCtrl::ReCreateTree()
933 CollapseDir(m_treeCtrl
->GetRootItem());
937 void wxGenericDirCtrl::CollapseTree()
939 wxTreeItemIdValue cookie
;
940 wxTreeItemId child
= m_treeCtrl
->GetFirstChild(m_rootId
, cookie
);
944 child
= m_treeCtrl
->GetNextChild(m_rootId
, cookie
);
948 // Find the child that matches the first part of 'path'.
949 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
950 // then the child for /usr is returned.
951 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
953 wxString
path2(path
);
955 // Make sure all separators are as per the current platform
956 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
957 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
959 // Append a separator to foil bogus substring matching
960 path2
+= wxString(wxFILE_SEP_PATH
);
962 // In MSW or PM, case is not significant
963 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
967 wxTreeItemIdValue cookie
;
968 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
969 while (childId
.IsOk())
971 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
973 if (data
&& !data
->m_path
.empty())
975 wxString
childPath(data
->m_path
);
976 if (!wxEndsWithPathSeparator(childPath
))
977 childPath
+= wxString(wxFILE_SEP_PATH
);
979 // In MSW and PM, case is not significant
980 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
981 childPath
.MakeLower();
984 if (childPath
.Len() <= path2
.Len())
986 wxString path3
= path2
.Mid(0, childPath
.Len());
987 if (childPath
== path3
)
989 if (path3
.Len() == path2
.Len())
998 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
1000 wxTreeItemId invalid
;
1004 // Try to expand as much of the given path as possible,
1005 // and select the given tree item.
1006 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
1009 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1010 wxTreeItemId lastId
= id
; // The last non-zero id
1011 while (id
.IsOk() && !done
)
1015 id
= FindChild(id
, path
, done
);
1022 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1025 m_treeCtrl
->Expand(lastId
);
1027 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1029 // Find the first file in this directory
1030 wxTreeItemIdValue cookie
;
1031 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1032 bool selectedChild
= false;
1033 while (childId
.IsOk())
1035 data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1037 if (data
&& data
->m_path
!= wxEmptyString
&& !data
->m_isDir
)
1039 m_treeCtrl
->SelectItem(childId
);
1040 m_treeCtrl
->EnsureVisible(childId
);
1041 selectedChild
= true;
1044 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1048 m_treeCtrl
->SelectItem(lastId
);
1049 m_treeCtrl
->EnsureVisible(lastId
);
1054 m_treeCtrl
->SelectItem(lastId
);
1055 m_treeCtrl
->EnsureVisible(lastId
);
1061 wxString
wxGenericDirCtrl::GetPath() const
1063 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1066 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1067 return data
->m_path
;
1070 return wxEmptyString
;
1073 wxString
wxGenericDirCtrl::GetFilePath() const
1075 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1078 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1080 return wxEmptyString
;
1082 return data
->m_path
;
1085 return wxEmptyString
;
1088 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1090 m_defaultPath
= path
;
1097 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1099 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1101 // This may take a longish time. Go to busy cursor
1106 wxString search
,path
,filename
;
1108 wxString
dirName(data
->m_path
);
1110 #if defined(__WXMSW__) || defined(__OS2__)
1111 if (dirName
.Last() == ':')
1112 dirName
+= wxString(wxFILE_SEP_PATH
);
1116 wxString eachFilename
;
1123 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1127 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1129 filenames
.Add(eachFilename
);
1132 while (d
.GetNext(& eachFilename
)) ;
1138 void wxGenericDirCtrl::SetFilterIndex(int n
)
1140 m_currentFilter
= n
;
1143 if (ExtractWildcard(m_filter
, n
, f
, d
))
1144 m_currentFilterStr
= f
;
1147 m_currentFilterStr
= wxT("*");
1149 m_currentFilterStr
= wxT("*.*");
1153 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1158 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1159 m_currentFilterStr
= f
;
1162 m_currentFilterStr
= wxT("*");
1164 m_currentFilterStr
= wxT("*.*");
1168 // Extract description and actual filter from overall filter string
1169 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1171 wxArrayString filters
, descriptions
;
1172 int count
= wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1173 if (count
> 0 && n
< count
)
1175 filter
= filters
[n
];
1176 description
= descriptions
[n
];
1183 #if WXWIN_COMPATIBILITY_2_4
1184 // Parses the global filter, returning the number of filters.
1185 // Returns 0 if none or if there's a problem.
1186 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1187 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1189 return wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1191 #endif // WXWIN_COMPATIBILITY_2_4
1193 void wxGenericDirCtrl::DoResize()
1195 wxSize sz
= GetClientSize();
1196 int verticalSpacing
= 3;
1200 if (m_filterListCtrl
)
1202 filterSz
= m_filterListCtrl
->GetSize();
1203 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1205 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1206 if (m_filterListCtrl
)
1208 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1209 // Don't know why, but this needs refreshing after a resize (wxMSW)
1210 m_filterListCtrl
->Refresh();
1216 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1221 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1222 const wxString
& text
,
1223 int image
, int selectedImage
,
1224 wxTreeItemData
* data
)
1226 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1228 wxASSERT (treeCtrl
);
1232 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1236 return wxTreeItemId();
1241 //-----------------------------------------------------------------------------
1242 // wxDirFilterListCtrl
1243 //-----------------------------------------------------------------------------
1245 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1247 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1248 EVT_CHOICE(wxID_ANY
, wxDirFilterListCtrl::OnSelFilter
)
1251 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1257 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1260 void wxDirFilterListCtrl::Init()
1265 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1267 int sel
= GetSelection();
1269 wxString currentPath
= m_dirCtrl
->GetPath();
1271 m_dirCtrl
->SetFilterIndex(sel
);
1273 // If the filter has changed, the view is out of date, so
1274 // collapse the tree.
1275 m_dirCtrl
->ReCreateTree();
1277 // Try to restore the selection, or at least the directory
1278 m_dirCtrl
->ExpandPath(currentPath
);
1281 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1284 wxArrayString descriptions
, filters
;
1285 size_t n
= (size_t) wxParseCommonDialogsFilter(filter
, descriptions
, filters
);
1287 if (n
> 0 && defaultFilter
< (int) n
)
1289 for (size_t i
= 0; i
< n
; i
++)
1290 Append(descriptions
[i
]);
1291 SetSelection(defaultFilter
);
1294 #endif // wxUSE_DIRDLG
1296 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1298 // ----------------------------------------------------------------------------
1299 // wxFileIconsTable icons
1300 // ----------------------------------------------------------------------------
1303 /* Computer (c) Julian Smart */
1304 static const char * file_icons_tbl_computer_xpm
[] = {
1305 /* columns rows colors chars-per-pixel */
1367 #endif // GTK+ < 2.4
1369 // ----------------------------------------------------------------------------
1370 // wxFileIconsTable & friends
1371 // ----------------------------------------------------------------------------
1373 // global instance of a wxFileIconsTable
1374 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1376 // A module to allow icons table cleanup
1378 class wxFileIconsTableModule
: public wxModule
1380 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1382 wxFileIconsTableModule() {}
1383 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return true; }
1386 if (wxTheFileIconsTable
)
1388 delete wxTheFileIconsTable
;
1389 wxTheFileIconsTable
= NULL
;
1394 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1396 class wxFileIconEntry
: public wxObject
1399 wxFileIconEntry(int i
) { id
= i
; }
1404 wxFileIconsTable::wxFileIconsTable()
1407 m_smallImageList
= NULL
;
1410 wxFileIconsTable::~wxFileIconsTable()
1414 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1417 if (m_smallImageList
) delete m_smallImageList
;
1420 // delayed initialization - wait until first use (wxArtProv not created yet)
1421 void wxFileIconsTable::Create()
1423 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1424 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1425 m_smallImageList
= new wxImageList(16, 16);
1428 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
,
1432 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN
,
1437 // GTK24 uses this icon in the file open dialog
1438 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1442 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1445 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1449 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_CDROM
,
1453 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FLOPPY
,
1457 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE
,
1461 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
,
1465 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1467 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
,
1470 delete m_HashTable
->Get(_T("exe"));
1471 m_HashTable
->Delete(_T("exe"));
1472 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1474 /* else put into list by GetIconID
1475 (KDE defines application/x-executable for *.exe and has nice icon)
1479 wxImageList
*wxFileIconsTable::GetSmallImageList()
1481 if (!m_smallImageList
)
1484 return m_smallImageList
;
1487 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1488 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1489 // one icon and we won't resize it
1491 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1493 const unsigned int size
= 16;
1495 wxImage
smallimg (size
, size
);
1496 unsigned char *p1
, *p2
, *ps
;
1497 unsigned char mr
= img
.GetMaskRed(),
1498 mg
= img
.GetMaskGreen(),
1499 mb
= img
.GetMaskBlue();
1502 unsigned sr
, sg
, sb
, smask
;
1504 p1
= img
.GetData(), p2
= img
.GetData() + 3 * size
*2, ps
= smallimg
.GetData();
1505 smallimg
.SetMaskColour(mr
, mr
, mr
);
1507 for (y
= 0; y
< size
; y
++)
1509 for (x
= 0; x
< size
; x
++)
1511 sr
= sg
= sb
= smask
= 0;
1512 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1513 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1516 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1517 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1520 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1521 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1524 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1525 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1530 ps
[0] = ps
[1] = ps
[2] = mr
;
1533 ps
[0] = (unsigned char)(sr
>> 2);
1534 ps
[1] = (unsigned char)(sg
>> 2);
1535 ps
[2] = (unsigned char)(sb
>> 2);
1539 p1
+= size
*2 * 3, p2
+= size
*2 * 3;
1542 return wxBitmap(smallimg
);
1545 // This function is currently not unused anymore
1547 // finds empty borders and return non-empty area of image:
1548 static wxImage
CutEmptyBorders(const wxImage
& img
)
1550 unsigned char mr
= img
.GetMaskRed(),
1551 mg
= img
.GetMaskGreen(),
1552 mb
= img
.GetMaskBlue();
1553 unsigned char *dt
= img
.GetData(), *dttmp
;
1554 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1556 unsigned top
, bottom
, left
, right
, i
;
1559 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1560 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1562 for (empt
= true, top
= 0; empt
&& top
< h
; top
++)
1565 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1568 for (empt
= true, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1570 MK_DTTMP(0, bottom
);
1571 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1574 for (empt
= true, left
= 0; empt
&& left
< w
; left
++)
1577 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1580 for (empt
= true, right
= w
-1; empt
&& right
> left
; right
--)
1583 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1586 top
--, left
--, bottom
++, right
++;
1588 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1592 #endif // wxUSE_MIMETYPE
1594 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1596 if (!m_smallImageList
)
1600 if (!extension
.empty())
1602 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1603 if (entry
) return (entry
-> id
);
1606 wxFileType
*ft
= (mime
.empty()) ?
1607 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1608 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1610 wxIconLocation iconLoc
;
1615 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1617 ic
= wxIcon( iconLoc
);
1626 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1631 bmp
.CopyFromIcon(ic
);
1636 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1640 const unsigned int size
= 16;
1642 int id
= m_smallImageList
->GetImageCount();
1643 if ((bmp
.GetWidth() == (int) size
) && (bmp
.GetHeight() == (int) size
))
1645 m_smallImageList
->Add(bmp
);
1647 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1650 wxImage img
= bmp
.ConvertToImage();
1652 if ((img
.GetWidth() != size
*2) || (img
.GetHeight() != size
*2))
1653 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1654 m_smallImageList
->Add(CreateAntialiasedBitmap(img
.Rescale(size
*2, size
*2)));
1656 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1658 #endif // wxUSE_IMAGE
1660 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1663 #else // !wxUSE_MIMETYPE
1666 if (extension
== wxT("exe"))
1670 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1673 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG