1 /////////////////////////////////////////////////////////////////////////////
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 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "dirctrlg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/generic/dirctrlg.h"
27 #include "wx/module.h"
29 #include "wx/button.h"
30 #include "wx/layout.h"
31 #include "wx/msgdlg.h"
32 #include "wx/textctrl.h"
33 #include "wx/textdlg.h"
34 #include "wx/filefn.h"
35 #include "wx/cmndata.h"
36 #include "wx/gdicmn.h"
38 #include "wx/imaglist.h"
42 #include "wx/tokenzr.h"
44 #include "wx/settings.h"
45 #include "wx/artprov.h"
47 #include "wx/mimetype.h"
49 #include "wx/choice.h"
52 #include "wx/statline.h"
55 #if defined(__WXMAC__)
56 #include "wx/mac/private.h" // includes mac headers
62 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
63 // older releases don't, but it should be verified and the checks modified
65 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
82 extern bool wxIsDriveAvailable(const wxString
& dirName
);
85 #if defined(__WXMAC__)
87 # include "MoreFilesX.h"
89 # include "MoreFilesExtras.h"
97 // If compiled under Windows, this macro can cause problems
102 // declared in filedlg.h, defined in fldlgcmn.cpp
103 extern int wxParseFileFilter(const wxString
& filterStr
, wxArrayString
& descriptions
, wxArrayString
& filters
);
105 // ----------------------------------------------------------------------------
106 // wxGetAvailableDrives, for WINDOWS, DOS, WXPM, MAC, UNIX (returns "/")
107 // ----------------------------------------------------------------------------
109 size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
)
111 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
114 wxChar driveBuffer
[256];
115 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
120 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
121 name
.Printf(wxT("%c:"), driveBuffer
[i
]);
123 int imageId
= wxFileIconsTable::drive
;
124 int driveType
= ::GetDriveType(path
);
127 case DRIVE_REMOVABLE
:
128 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
129 imageId
= wxFileIconsTable::floppy
;
131 imageId
= wxFileIconsTable::removeable
;
134 imageId
= wxFileIconsTable::cdrom
;
139 imageId
= wxFileIconsTable::drive
;
145 icon_ids
.Add(imageId
);
147 while (driveBuffer
[i
] != wxT('\0'))
150 if (driveBuffer
[i
] == wxT('\0'))
156 /* If we can switch to the drive, it exists. */
157 for( drive
= 1; drive
<= 26; drive
++ )
160 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
161 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
163 if (wxIsDriveAvailable(path
))
167 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
170 #endif // __WIN32__/!__WIN32__
172 #elif defined(__WXMAC__)
175 ItemCount theVolCount
;
176 char thePath
[FILENAME_MAX
];
178 if (FSGetMountedVolumes(&theVolRefs
, &theVolCount
) == noErr
) {
180 ::HLock( (Handle
)theVolRefs
) ;
181 for (index
= 0; index
< theVolCount
; ++index
) {
182 // get the POSIX path associated with the FSRef
183 if ( FSRefMakePath(&((*theVolRefs
)[index
]),
184 (UInt8
*)thePath
, sizeof(thePath
)) != noErr
) {
187 // add path separator at end if necessary
188 wxString
path( thePath
) ;
189 if (path
.Last() != wxFILE_SEP_PATH
) {
190 path
+= wxFILE_SEP_PATH
;
192 // get Mac volume name for display
193 FSVolumeRefNum vRefNum
;
194 HFSUniStr255 volumeName
;
196 if ( FSGetVRefNum(&((*theVolRefs
)[index
]), &vRefNum
) != noErr
) {
199 if ( FSGetVInfo(vRefNum
, &volumeName
, NULL
, NULL
) != noErr
) {
202 // get C string from Unicode HFS name
203 // see: http://developer.apple.com/carbon/tipsandtricks.html
204 CFStringRef cfstr
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
207 // Do something with str
208 char *cstr
= NewPtr(CFStringGetLength(cfstr
) + 1);
209 if (( cstr
== NULL
) ||
210 !CFStringGetCString(cfstr
, cstr
, CFStringGetLength(cfstr
) + 1,
211 kCFStringEncodingMacRoman
))
216 wxString
name( cstr
);
220 GetVolParmsInfoBuffer volParmsInfo
;
222 if ( FSGetVolParms(vRefNum
, sizeof(volParmsInfo
), &volParmsInfo
, &actualSize
) != noErr
) {
229 if ( VolIsEjectable(&volParmsInfo
) )
230 icon_ids
.Add(wxFileIconsTable::cdrom
);
232 icon_ids
.Add(wxFileIconsTable::drive
);
234 ::HUnlock( (Handle
)theVolRefs
);
235 ::DisposeHandle( (Handle
)theVolRefs
);
242 short actualCount
= 0 ;
243 if (OnLine(&volume
, 1, &actualCount
, &index
) != noErr
|| actualCount
==0)
248 wxString name
= wxMacFSSpec2MacFilename( &volume
);
249 paths
.Add(name
+ wxFILE_SEP_PATH
);
251 icon_ids
.Add(wxFileIconsTable::drive
);
255 #elif defined(__UNIX__)
258 icon_ids
.Add(wxFileIconsTable::computer
);
260 #error "Unsupported platform in wxGenericDirCtrl!"
262 return paths
.GetCount();
265 // ----------------------------------------------------------------------------
266 // wxIsDriveAvailable
267 // ----------------------------------------------------------------------------
271 bool wxIsDriveAvailable(const wxString
& dirName
)
273 // FIXME_MGL - this method leads to hang up under Watcom for some reason
275 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
277 wxString
dirNameLower(dirName
.Lower());
278 // VS: always return TRUE for removable media, since Win95 doesn't
279 // like it when MS-DOS app accesses empty floppy drive
280 return (dirNameLower
[0u] == wxT('a') ||
281 dirNameLower
[0u] == wxT('b') ||
282 wxPathExists(dirNameLower
));
289 #elif defined(__WINDOWS__) || defined(__WXPM__)
291 int setdrive(int drive
)
293 #if defined(__GNUWIN32__) && \
294 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
295 return _chdrive(drive
);
299 if (drive
< 1 || drive
> 31)
301 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
302 newdrive
[1] = wxT(':');
303 newdrive
[2] = wxT('\0');
304 #if defined(__WXMSW__)
306 if (wxSetWorkingDirectory(newdrive
))
308 if (::SetCurrentDirectory(newdrive
))
311 // VA doesn't know what LPSTR is and has its own set
312 if (DosSetCurrentDir((PSZ
)newdrive
))
320 bool wxIsDriveAvailable(const wxString
& dirName
)
323 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
327 // Check if this is a root directory and if so,
328 // whether the drive is available.
329 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
331 wxString
dirNameLower(dirName
.Lower());
332 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
333 success
= wxPathExists(dirNameLower
);
335 int currentDrive
= _getdrive();
336 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
337 int err
= setdrive( thisDrive
) ;
338 setdrive( currentDrive
);
347 (void) SetErrorMode(errorMode
);
352 #endif // __WINDOWS__ || __WXPM__
355 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
356 // and sort regardless of case.
357 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
359 wxString
*strFirst
= (wxString
*)first
;
360 wxString
*strSecond
= (wxString
*)second
;
362 return strFirst
->CmpNoCase(*strSecond
);
365 //-----------------------------------------------------------------------------
367 //-----------------------------------------------------------------------------
369 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
374 /* Insert logic to detect hidden files here
375 * In UnixLand we just check whether the first char is a dot
376 * For FileNameFromPath read LastDirNameInThisPath ;-) */
377 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
379 m_isExpanded
= FALSE
;
383 wxDirItemData::~wxDirItemData()
387 void wxDirItemData::SetNewDirName(const wxString
& path
)
390 m_name
= wxFileNameFromPath(path
);
393 bool wxDirItemData::HasSubDirs() const
395 if (m_path
.IsEmpty())
401 if ( !dir
.Open(m_path
) )
405 return dir
.HasSubDirs();
408 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
410 if (m_path
.IsEmpty())
416 if ( !dir
.Open(m_path
) )
420 return dir
.HasFiles();
423 //-----------------------------------------------------------------------------
425 //-----------------------------------------------------------------------------
427 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
429 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
430 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
431 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
432 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
433 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
434 EVT_SIZE (wxGenericDirCtrl::OnSize
)
437 wxGenericDirCtrl::wxGenericDirCtrl(void)
442 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
448 const wxString
& filter
,
450 const wxString
& name
)
452 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
455 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
459 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_HIDE_ROOT
;
461 if (style
& wxDIRCTRL_EDIT_LABELS
)
462 treeStyle
|= wxTR_EDIT_LABELS
;
464 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
465 treeStyle
|= wxNO_BORDER
;
467 treeStyle
|= wxBORDER_SUNKEN
;
469 long filterStyle
= 0;
470 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
471 filterStyle
|= wxNO_BORDER
;
473 filterStyle
|= wxBORDER_SUNKEN
;
475 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
477 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
478 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
483 SetFilterIndex(defaultFilter
);
485 if (m_filterListCtrl
)
486 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
488 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
490 m_showHidden
= FALSE
;
491 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
495 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
496 rootName
= _("Computer");
498 rootName
= _("Sections");
501 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
502 m_treeCtrl
->SetItemHasChildren(m_rootId
);
503 ExpandDir(m_rootId
); // automatically expand first level
505 // Expand and select the default path
506 if (!m_defaultPath
.IsEmpty())
507 ExpandPath(m_defaultPath
);
514 wxGenericDirCtrl::~wxGenericDirCtrl()
518 void wxGenericDirCtrl::Init()
520 m_showHidden
= FALSE
;
522 m_currentFilterStr
= wxEmptyString
; // Default: any file
524 m_filterListCtrl
= NULL
;
527 void wxGenericDirCtrl::ShowHidden( bool show
)
531 wxString path
= GetPath();
537 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
539 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
541 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
543 m_treeCtrl
->SetItemHasChildren(id
);
548 void wxGenericDirCtrl::SetupSections()
550 wxArrayString paths
, names
;
553 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
555 for (n
= 0; n
< count
; n
++)
557 AddSection(paths
[n
], names
[n
], icons
[n
]);
561 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
563 // don't rename the main entry "Sections"
564 if (event
.GetItem() == m_rootId
)
570 // don't rename the individual sections
571 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
578 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
580 if ((event
.GetLabel().IsEmpty()) ||
581 (event
.GetLabel() == _(".")) ||
582 (event
.GetLabel() == _("..")) ||
583 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
584 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
585 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
587 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
593 wxTreeItemId id
= event
.GetItem();
594 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
597 wxString
new_name( wxPathOnly( data
->m_path
) );
598 new_name
+= wxString(wxFILE_SEP_PATH
);
599 new_name
+= event
.GetLabel();
603 if (wxFileExists(new_name
))
605 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
610 if (wxRenameFile(data
->m_path
,new_name
))
612 data
->SetNewDirName( new_name
);
616 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
622 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
624 wxTreeItemId parentId
= event
.GetItem();
626 // VS: this is needed because the event handler is called from wxTreeCtrl
627 // ctor when wxTR_HIDE_ROOT was specified
629 if (!m_rootId
.IsOk())
631 m_rootId
= m_treeCtrl
->GetRootItem();
636 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
638 CollapseDir(event
.GetItem());
641 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
645 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
646 if (!data
->m_isExpanded
)
649 data
->m_isExpanded
= FALSE
;
651 /* Workaround because DeleteChildren has disapeared (why?) and
652 * CollapseAndReset doesn't work as advertised (deletes parent too) */
653 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
656 m_treeCtrl
->Delete(child
);
657 /* Not GetNextChild below, because the cookie mechanism can't
658 * handle disappearing children! */
659 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
663 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
665 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
667 if (data
->m_isExpanded
)
670 data
->m_isExpanded
= TRUE
;
672 if (parentId
== m_treeCtrl
->GetRootItem())
680 wxString search
,path
,filename
;
682 wxString
dirName(data
->m_path
);
684 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
685 // Check if this is a root directory and if so,
686 // whether the drive is avaiable.
687 if (!wxIsDriveAvailable(dirName
))
689 data
->m_isExpanded
= FALSE
;
690 //wxMessageBox(wxT("Sorry, this drive is not available."));
695 // This may take a longish time. Go to busy cursor
698 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
699 if (dirName
.Last() == ':')
700 dirName
+= wxString(wxFILE_SEP_PATH
);
704 wxArrayString filenames
;
707 wxString eachFilename
;
714 int style
= wxDIR_DIRS
;
715 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
716 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
720 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
722 dirs
.Add(eachFilename
);
725 while (d
.GetNext(& eachFilename
));
728 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
730 // Now do the filenames -- but only if we're allowed to
731 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
739 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
743 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
745 filenames
.Add(eachFilename
);
748 while (d
.GetNext(& eachFilename
));
751 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
754 // Add the sorted dirs
756 for (i
= 0; i
< dirs
.Count(); i
++)
758 wxString
eachFilename(dirs
[i
]);
760 if (!wxEndsWithPathSeparator(path
))
761 path
+= wxString(wxFILE_SEP_PATH
);
762 path
+= eachFilename
;
764 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
765 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
766 wxFileIconsTable::folder
, -1, dir_item
);
767 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
768 wxTreeItemIcon_Expanded
);
770 // Has this got any children? If so, make it expandable.
771 // (There are two situations when a dir has children: either it
772 // has subdirectories or it contains files that weren't filtered
773 // out. The latter only applies to dirctrl with files.)
774 if ( dir_item
->HasSubDirs() ||
775 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
776 dir_item
->HasFiles(m_currentFilterStr
)) )
778 m_treeCtrl
->SetItemHasChildren(id
);
782 // Add the sorted filenames
783 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
785 for (i
= 0; i
< filenames
.Count(); i
++)
787 wxString
eachFilename(filenames
[i
]);
789 if (!wxEndsWithPathSeparator(path
))
790 path
+= wxString(wxFILE_SEP_PATH
);
791 path
+= eachFilename
;
792 //path = dirName + wxString(wxT("/")) + eachFilename;
793 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
794 int image_id
= wxFileIconsTable::file
;
795 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
796 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
797 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
802 void wxGenericDirCtrl::ReCreateTree()
804 CollapseDir(m_treeCtrl
->GetRootItem());
805 ExpandDir(m_treeCtrl
->GetRootItem());
808 // Find the child that matches the first part of 'path'.
809 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
810 // then the child for /usr is returned.
811 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
813 wxString
path2(path
);
815 // Make sure all separators are as per the current platform
816 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
817 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
819 // Append a separator to foil bogus substring matching
820 path2
+= wxString(wxFILE_SEP_PATH
);
822 // In MSW or PM, case is not significant
823 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
828 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
829 while (childId
.IsOk())
831 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
833 if (data
&& !data
->m_path
.IsEmpty())
835 wxString
childPath(data
->m_path
);
836 if (!wxEndsWithPathSeparator(childPath
))
837 childPath
+= wxString(wxFILE_SEP_PATH
);
839 // In MSW and PM, case is not significant
840 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
841 childPath
.MakeLower();
844 if (childPath
.Len() <= path2
.Len())
846 wxString path3
= path2
.Mid(0, childPath
.Len());
847 if (childPath
== path3
)
849 if (path3
.Len() == path2
.Len())
858 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
860 wxTreeItemId invalid
;
864 // Try to expand as much of the given path as possible,
865 // and select the given tree item.
866 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
869 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
870 wxTreeItemId lastId
= id
; // The last non-zero id
871 while (id
.IsOk() && !done
)
875 id
= FindChild(id
, path
, done
);
881 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
884 m_treeCtrl
->Expand(lastId
);
886 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
888 // Find the first file in this directory
890 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
891 bool selectedChild
= FALSE
;
892 while (childId
.IsOk())
894 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
896 if (data
&& data
->m_path
!= wxT("") && !data
->m_isDir
)
898 m_treeCtrl
->SelectItem(childId
);
899 m_treeCtrl
->EnsureVisible(childId
);
900 selectedChild
= TRUE
;
903 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
907 m_treeCtrl
->SelectItem(lastId
);
908 m_treeCtrl
->EnsureVisible(lastId
);
913 m_treeCtrl
->SelectItem(lastId
);
914 m_treeCtrl
->EnsureVisible(lastId
);
923 wxString
wxGenericDirCtrl::GetPath() const
925 wxTreeItemId id
= m_treeCtrl
->GetSelection();
928 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
932 return wxEmptyString
;
935 wxString
wxGenericDirCtrl::GetFilePath() const
937 wxTreeItemId id
= m_treeCtrl
->GetSelection();
940 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
942 return wxEmptyString
;
947 return wxEmptyString
;
950 void wxGenericDirCtrl::SetPath(const wxString
& path
)
952 m_defaultPath
= path
;
959 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
961 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
963 // This may take a longish time. Go to busy cursor
968 wxString search
,path
,filename
;
970 wxString
dirName(data
->m_path
);
972 #if defined(__WXMSW__) || defined(__WXPM__)
973 if (dirName
.Last() == ':')
974 dirName
+= wxString(wxFILE_SEP_PATH
);
978 wxString eachFilename
;
985 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
989 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
991 filenames
.Add(eachFilename
);
994 while (d
.GetNext(& eachFilename
)) ;
1000 void wxGenericDirCtrl::SetFilterIndex(int n
)
1002 m_currentFilter
= n
;
1005 if (ExtractWildcard(m_filter
, n
, f
, d
))
1006 m_currentFilterStr
= f
;
1008 m_currentFilterStr
= wxT("*.*");
1011 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1016 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1017 m_currentFilterStr
= f
;
1019 m_currentFilterStr
= wxT("*.*");
1022 // Extract description and actual filter from overall filter string
1023 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1025 wxArrayString filters
, descriptions
;
1026 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1027 if (count
> 0 && n
< count
)
1029 filter
= filters
[n
];
1030 description
= descriptions
[n
];
1037 // Parses the global filter, returning the number of filters.
1038 // Returns 0 if none or if there's a problem.
1039 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1041 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1043 return wxParseFileFilter(filterStr
, descriptions
, filters
);
1046 void wxGenericDirCtrl::DoResize()
1048 wxSize sz
= GetClientSize();
1049 int verticalSpacing
= 3;
1053 if (m_filterListCtrl
)
1056 // For some reason, this is required in order for the
1057 // correct control height to always be returned, rather
1058 // than the drop-down list height which is sometimes returned.
1059 wxSize oldSize
= m_filterListCtrl
->GetSize();
1060 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
+10, -1, wxSIZE_USE_EXISTING
);
1061 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
, -1, wxSIZE_USE_EXISTING
);
1063 filterSz
= m_filterListCtrl
->GetSize();
1064 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1066 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1067 if (m_filterListCtrl
)
1069 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1070 // Don't know why, but this needs refreshing after a resize (wxMSW)
1071 m_filterListCtrl
->Refresh();
1077 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1082 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1083 const wxString
& text
,
1084 int image
, int selectedImage
,
1085 wxTreeItemData
* data
)
1087 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1089 wxASSERT (treeCtrl
);
1093 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1097 return wxTreeItemId();
1102 //-----------------------------------------------------------------------------
1103 // wxDirFilterListCtrl
1104 //-----------------------------------------------------------------------------
1106 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1108 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1109 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1112 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1118 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1121 void wxDirFilterListCtrl::Init()
1126 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1128 int sel
= GetSelection();
1130 wxString currentPath
= m_dirCtrl
->GetPath();
1132 m_dirCtrl
->SetFilterIndex(sel
);
1134 // If the filter has changed, the view is out of date, so
1135 // collapse the tree.
1136 m_dirCtrl
->ReCreateTree();
1138 // Try to restore the selection, or at least the directory
1139 m_dirCtrl
->ExpandPath(currentPath
);
1142 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1145 wxArrayString descriptions
, filters
;
1146 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1148 if (n
> 0 && defaultFilter
< (int) n
)
1151 for (i
= 0; i
< n
; i
++)
1152 Append(descriptions
[i
]);
1153 SetSelection(defaultFilter
);
1158 // ----------------------------------------------------------------------------
1159 // wxFileIconsTable icons
1160 // ----------------------------------------------------------------------------
1163 static const char * file_icons_tbl_folder_open_xpm
[] = {
1164 /* width height ncolors chars_per_pixel */
1192 static const char * file_icons_tbl_computer_xpm
[] = {
1219 static const char * file_icons_tbl_drive_xpm
[] = {
1246 static const char *file_icons_tbl_cdrom_xpm
[] = {
1276 static const char * file_icons_tbl_floppy_xpm
[] = {
1303 static const char * file_icons_tbl_removeable_xpm
[] = {
1329 // ----------------------------------------------------------------------------
1330 // wxFileIconsTable & friends
1331 // ----------------------------------------------------------------------------
1333 // global instance of a wxFileIconsTable
1334 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1336 // A module to allow icons table cleanup
1338 class wxFileIconsTableModule
: public wxModule
1340 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1342 wxFileIconsTableModule() {}
1343 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return TRUE
; }
1346 if (wxTheFileIconsTable
)
1348 delete wxTheFileIconsTable
;
1349 wxTheFileIconsTable
= NULL
;
1354 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1356 class wxFileIconEntry
: public wxObject
1359 wxFileIconEntry(int i
) { id
= i
; }
1364 wxFileIconsTable::wxFileIconsTable()
1367 m_smallImageList
= NULL
;
1370 wxFileIconsTable::~wxFileIconsTable()
1372 if (m_HashTable
) delete m_HashTable
;
1373 if (m_smallImageList
) delete m_smallImageList
;
1376 // delayed initialization - wait until first use (wxArtProv not created yet)
1377 void wxFileIconsTable::Create()
1379 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1380 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1381 m_smallImageList
= new wxImageList(16, 16);
1383 m_HashTable
->DeleteContents(TRUE
);
1385 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
, wxART_CMN_DIALOG
));
1387 m_smallImageList
->Add(wxIcon(file_icons_tbl_folder_open_xpm
));
1389 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1391 m_smallImageList
->Add(wxIcon(file_icons_tbl_drive_xpm
));
1393 m_smallImageList
->Add(wxIcon(file_icons_tbl_cdrom_xpm
));
1395 m_smallImageList
->Add(wxIcon(file_icons_tbl_floppy_xpm
));
1397 m_smallImageList
->Add(wxIcon(file_icons_tbl_removeable_xpm
));
1399 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
, wxART_CMN_DIALOG
));
1401 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1403 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
, wxART_CMN_DIALOG
));
1404 m_HashTable
->Delete(_T("exe"));
1405 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1407 /* else put into list by GetIconID
1408 (KDE defines application/x-executable for *.exe and has nice icon)
1412 wxImageList
*wxFileIconsTable::GetSmallImageList()
1414 if (!m_smallImageList
)
1417 return m_smallImageList
;
1421 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1422 // one icon and we won't resize it
1424 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1426 wxImage
smallimg (16, 16);
1427 unsigned char *p1
, *p2
, *ps
;
1428 unsigned char mr
= img
.GetMaskRed(),
1429 mg
= img
.GetMaskGreen(),
1430 mb
= img
.GetMaskBlue();
1433 unsigned sr
, sg
, sb
, smask
;
1435 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= smallimg
.GetData();
1436 smallimg
.SetMaskColour(mr
, mr
, mr
);
1438 for (y
= 0; y
< 16; y
++)
1440 for (x
= 0; x
< 16; x
++)
1442 sr
= sg
= sb
= smask
= 0;
1443 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1444 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1447 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1448 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1451 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1452 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1455 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1456 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1461 ps
[0] = ps
[1] = ps
[2] = mr
;
1463 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
1466 p1
+= 32 * 3, p2
+= 32 * 3;
1469 return wxBitmap(smallimg
);
1472 // finds empty borders and return non-empty area of image:
1473 static wxImage
CutEmptyBorders(const wxImage
& img
)
1475 unsigned char mr
= img
.GetMaskRed(),
1476 mg
= img
.GetMaskGreen(),
1477 mb
= img
.GetMaskBlue();
1478 unsigned char *dt
= img
.GetData(), *dttmp
;
1479 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1481 unsigned top
, bottom
, left
, right
, i
;
1484 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1485 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
1487 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
1490 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1493 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1495 MK_DTTMP(0, bottom
);
1496 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1499 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
1502 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1505 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
1508 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1511 top
--, left
--, bottom
++, right
++;
1513 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1515 #endif // wxUSE_MIMETYPE
1517 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1519 if (!m_smallImageList
)
1523 if (!extension
.IsEmpty())
1525 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1526 if (entry
) return (entry
-> id
);
1529 wxFileType
*ft
= (mime
.IsEmpty()) ?
1530 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1531 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1533 wxIconLocation iconLoc
;
1535 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1537 ic
= wxIcon(iconLoc
);
1545 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1550 tmpBmp
.CopyFromIcon(ic
);
1551 wxImage img
= tmpBmp
.ConvertToImage();
1553 int id
= m_smallImageList
->GetImageCount();
1554 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
1555 m_smallImageList
->Add(wxBitmap(img
));
1558 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
1559 m_smallImageList
->Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
1561 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1563 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1566 #else // !wxUSE_MIMETYPE
1568 if (extension
== wxT("exe"))
1572 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1575 #endif // wxUSE_DIRDLG