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(wxString
* strFirst
, wxString
* strSecond
)
359 return strFirst
->CmpNoCase(*strSecond
);
362 //-----------------------------------------------------------------------------
364 //-----------------------------------------------------------------------------
366 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
371 /* Insert logic to detect hidden files here
372 * In UnixLand we just check whether the first char is a dot
373 * For FileNameFromPath read LastDirNameInThisPath ;-) */
374 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
376 m_isExpanded
= FALSE
;
380 wxDirItemData::~wxDirItemData()
384 void wxDirItemData::SetNewDirName(const wxString
& path
)
387 m_name
= wxFileNameFromPath(path
);
390 bool wxDirItemData::HasSubDirs() const
392 if (m_path
.IsEmpty())
398 if ( !dir
.Open(m_path
) )
402 return dir
.HasSubDirs();
405 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
407 if (m_path
.IsEmpty())
413 if ( !dir
.Open(m_path
) )
417 return dir
.HasFiles();
420 //-----------------------------------------------------------------------------
422 //-----------------------------------------------------------------------------
424 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
426 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
427 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
428 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
429 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
430 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
431 EVT_SIZE (wxGenericDirCtrl::OnSize
)
434 wxGenericDirCtrl::wxGenericDirCtrl(void)
439 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
445 const wxString
& filter
,
447 const wxString
& name
)
449 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
452 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
456 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_HIDE_ROOT
;
458 if (style
& wxDIRCTRL_EDIT_LABELS
)
459 treeStyle
|= wxTR_EDIT_LABELS
;
461 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
462 treeStyle
|= wxNO_BORDER
;
464 treeStyle
|= wxBORDER_SUNKEN
;
466 long filterStyle
= 0;
467 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
468 filterStyle
|= wxNO_BORDER
;
470 filterStyle
|= wxBORDER_SUNKEN
;
472 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
474 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
475 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
480 SetFilterIndex(defaultFilter
);
482 if (m_filterListCtrl
)
483 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
485 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
487 m_showHidden
= FALSE
;
488 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
492 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
493 rootName
= _("Computer");
495 rootName
= _("Sections");
498 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
499 m_treeCtrl
->SetItemHasChildren(m_rootId
);
500 ExpandDir(m_rootId
); // automatically expand first level
502 // Expand and select the default path
503 if (!m_defaultPath
.IsEmpty())
504 ExpandPath(m_defaultPath
);
511 wxGenericDirCtrl::~wxGenericDirCtrl()
515 void wxGenericDirCtrl::Init()
517 m_showHidden
= FALSE
;
519 m_currentFilterStr
= wxEmptyString
; // Default: any file
521 m_filterListCtrl
= NULL
;
524 void wxGenericDirCtrl::ShowHidden( bool show
)
528 wxString path
= GetPath();
534 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
536 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
538 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
540 m_treeCtrl
->SetItemHasChildren(id
);
545 void wxGenericDirCtrl::SetupSections()
547 wxArrayString paths
, names
;
550 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
552 for (n
= 0; n
< count
; n
++)
554 AddSection(paths
[n
], names
[n
], icons
[n
]);
558 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
560 // don't rename the main entry "Sections"
561 if (event
.GetItem() == m_rootId
)
567 // don't rename the individual sections
568 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
575 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
577 if ((event
.GetLabel().IsEmpty()) ||
578 (event
.GetLabel() == _(".")) ||
579 (event
.GetLabel() == _("..")) ||
580 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
581 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
582 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
584 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
590 wxTreeItemId id
= event
.GetItem();
591 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
594 wxString
new_name( wxPathOnly( data
->m_path
) );
595 new_name
+= wxString(wxFILE_SEP_PATH
);
596 new_name
+= event
.GetLabel();
600 if (wxFileExists(new_name
))
602 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
607 if (wxRenameFile(data
->m_path
,new_name
))
609 data
->SetNewDirName( new_name
);
613 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
619 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
621 wxTreeItemId parentId
= event
.GetItem();
623 // VS: this is needed because the event handler is called from wxTreeCtrl
624 // ctor when wxTR_HIDE_ROOT was specified
626 if (!m_rootId
.IsOk())
628 m_rootId
= m_treeCtrl
->GetRootItem();
633 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
635 CollapseDir(event
.GetItem());
638 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
642 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
643 if (!data
->m_isExpanded
)
646 data
->m_isExpanded
= FALSE
;
648 /* Workaround because DeleteChildren has disapeared (why?) and
649 * CollapseAndReset doesn't work as advertised (deletes parent too) */
650 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
653 m_treeCtrl
->Delete(child
);
654 /* Not GetNextChild below, because the cookie mechanism can't
655 * handle disappearing children! */
656 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
660 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
662 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
664 if (data
->m_isExpanded
)
667 data
->m_isExpanded
= TRUE
;
669 if (parentId
== m_treeCtrl
->GetRootItem())
677 wxString search
,path
,filename
;
679 wxString
dirName(data
->m_path
);
681 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
682 // Check if this is a root directory and if so,
683 // whether the drive is avaiable.
684 if (!wxIsDriveAvailable(dirName
))
686 data
->m_isExpanded
= FALSE
;
687 //wxMessageBox(wxT("Sorry, this drive is not available."));
692 // This may take a longish time. Go to busy cursor
695 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
696 if (dirName
.Last() == ':')
697 dirName
+= wxString(wxFILE_SEP_PATH
);
701 wxArrayString filenames
;
704 wxString eachFilename
;
711 int style
= wxDIR_DIRS
;
712 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
713 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
717 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
719 dirs
.Add(eachFilename
);
722 while (d
.GetNext(& eachFilename
));
725 dirs
.Sort(wxDirCtrlStringCompareFunction
);
727 // Now do the filenames -- but only if we're allowed to
728 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
736 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
740 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
742 filenames
.Add(eachFilename
);
745 while (d
.GetNext(& eachFilename
));
748 filenames
.Sort(wxDirCtrlStringCompareFunction
);
751 // Add the sorted dirs
753 for (i
= 0; i
< dirs
.Count(); i
++)
755 wxString
eachFilename(dirs
[i
]);
757 if (!wxEndsWithPathSeparator(path
))
758 path
+= wxString(wxFILE_SEP_PATH
);
759 path
+= eachFilename
;
761 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
762 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
763 wxFileIconsTable::folder
, -1, dir_item
);
764 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
765 wxTreeItemIcon_Expanded
);
767 // Has this got any children? If so, make it expandable.
768 // (There are two situations when a dir has children: either it
769 // has subdirectories or it contains files that weren't filtered
770 // out. The latter only applies to dirctrl with files.)
771 if ( dir_item
->HasSubDirs() ||
772 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
773 dir_item
->HasFiles(m_currentFilterStr
)) )
775 m_treeCtrl
->SetItemHasChildren(id
);
779 // Add the sorted filenames
780 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
782 for (i
= 0; i
< filenames
.Count(); i
++)
784 wxString
eachFilename(filenames
[i
]);
786 if (!wxEndsWithPathSeparator(path
))
787 path
+= wxString(wxFILE_SEP_PATH
);
788 path
+= eachFilename
;
789 //path = dirName + wxString(wxT("/")) + eachFilename;
790 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
791 int image_id
= wxFileIconsTable::file
;
792 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
793 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
794 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
799 void wxGenericDirCtrl::ReCreateTree()
801 CollapseDir(m_treeCtrl
->GetRootItem());
802 ExpandDir(m_treeCtrl
->GetRootItem());
805 // Find the child that matches the first part of 'path'.
806 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
807 // then the child for /usr is returned.
808 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
810 wxString
path2(path
);
812 // Make sure all separators are as per the current platform
813 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
814 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
816 // Append a separator to foil bogus substring matching
817 path2
+= wxString(wxFILE_SEP_PATH
);
819 // In MSW or PM, case is not significant
820 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
825 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
826 while (childId
.IsOk())
828 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
830 if (data
&& !data
->m_path
.IsEmpty())
832 wxString
childPath(data
->m_path
);
833 if (!wxEndsWithPathSeparator(childPath
))
834 childPath
+= wxString(wxFILE_SEP_PATH
);
836 // In MSW and PM, case is not significant
837 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
838 childPath
.MakeLower();
841 if (childPath
.Len() <= path2
.Len())
843 wxString path3
= path2
.Mid(0, childPath
.Len());
844 if (childPath
== path3
)
846 if (path3
.Len() == path2
.Len())
855 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
857 wxTreeItemId invalid
;
861 // Try to expand as much of the given path as possible,
862 // and select the given tree item.
863 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
866 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
867 wxTreeItemId lastId
= id
; // The last non-zero id
868 while (id
.IsOk() && !done
)
872 id
= FindChild(id
, path
, done
);
878 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
881 m_treeCtrl
->Expand(lastId
);
883 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
885 // Find the first file in this directory
887 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
888 bool selectedChild
= FALSE
;
889 while (childId
.IsOk())
891 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
893 if (data
&& data
->m_path
!= wxT("") && !data
->m_isDir
)
895 m_treeCtrl
->SelectItem(childId
);
896 m_treeCtrl
->EnsureVisible(childId
);
897 selectedChild
= TRUE
;
900 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
904 m_treeCtrl
->SelectItem(lastId
);
905 m_treeCtrl
->EnsureVisible(lastId
);
910 m_treeCtrl
->SelectItem(lastId
);
911 m_treeCtrl
->EnsureVisible(lastId
);
920 wxString
wxGenericDirCtrl::GetPath() const
922 wxTreeItemId id
= m_treeCtrl
->GetSelection();
925 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
929 return wxEmptyString
;
932 wxString
wxGenericDirCtrl::GetFilePath() const
934 wxTreeItemId id
= m_treeCtrl
->GetSelection();
937 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
939 return wxEmptyString
;
944 return wxEmptyString
;
947 void wxGenericDirCtrl::SetPath(const wxString
& path
)
949 m_defaultPath
= path
;
956 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
958 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
960 // This may take a longish time. Go to busy cursor
965 wxString search
,path
,filename
;
967 wxString
dirName(data
->m_path
);
969 #if defined(__WXMSW__) || defined(__WXPM__)
970 if (dirName
.Last() == ':')
971 dirName
+= wxString(wxFILE_SEP_PATH
);
975 wxString eachFilename
;
982 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
986 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
988 filenames
.Add(eachFilename
);
991 while (d
.GetNext(& eachFilename
)) ;
997 void wxGenericDirCtrl::SetFilterIndex(int n
)
1002 if (ExtractWildcard(m_filter
, n
, f
, d
))
1003 m_currentFilterStr
= f
;
1005 m_currentFilterStr
= wxT("*.*");
1008 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1013 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1014 m_currentFilterStr
= f
;
1016 m_currentFilterStr
= wxT("*.*");
1019 // Extract description and actual filter from overall filter string
1020 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1022 wxArrayString filters
, descriptions
;
1023 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1024 if (count
> 0 && n
< count
)
1026 filter
= filters
[n
];
1027 description
= descriptions
[n
];
1034 // Parses the global filter, returning the number of filters.
1035 // Returns 0 if none or if there's a problem.
1036 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1038 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1040 return wxParseFileFilter(filterStr
, descriptions
, filters
);
1043 void wxGenericDirCtrl::DoResize()
1045 wxSize sz
= GetClientSize();
1046 int verticalSpacing
= 3;
1050 if (m_filterListCtrl
)
1053 // For some reason, this is required in order for the
1054 // correct control height to always be returned, rather
1055 // than the drop-down list height which is sometimes returned.
1056 wxSize oldSize
= m_filterListCtrl
->GetSize();
1057 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
+10, -1, wxSIZE_USE_EXISTING
);
1058 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
, -1, wxSIZE_USE_EXISTING
);
1060 filterSz
= m_filterListCtrl
->GetSize();
1061 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1063 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1064 if (m_filterListCtrl
)
1066 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1067 // Don't know why, but this needs refreshing after a resize (wxMSW)
1068 m_filterListCtrl
->Refresh();
1074 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1079 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1080 const wxString
& text
,
1081 int image
, int selectedImage
,
1082 wxTreeItemData
* data
)
1084 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1086 wxASSERT (treeCtrl
);
1090 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1094 return wxTreeItemId();
1099 //-----------------------------------------------------------------------------
1100 // wxDirFilterListCtrl
1101 //-----------------------------------------------------------------------------
1103 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1105 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1106 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1109 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1115 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1118 void wxDirFilterListCtrl::Init()
1123 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1125 int sel
= GetSelection();
1127 wxString currentPath
= m_dirCtrl
->GetPath();
1129 m_dirCtrl
->SetFilterIndex(sel
);
1131 // If the filter has changed, the view is out of date, so
1132 // collapse the tree.
1133 m_dirCtrl
->ReCreateTree();
1135 // Try to restore the selection, or at least the directory
1136 m_dirCtrl
->ExpandPath(currentPath
);
1139 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1142 wxArrayString descriptions
, filters
;
1143 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1145 if (n
> 0 && defaultFilter
< (int) n
)
1148 for (i
= 0; i
< n
; i
++)
1149 Append(descriptions
[i
]);
1150 SetSelection(defaultFilter
);
1155 // ----------------------------------------------------------------------------
1156 // wxFileIconsTable icons
1157 // ----------------------------------------------------------------------------
1160 static const char * file_icons_tbl_folder_open_xpm
[] = {
1161 /* width height ncolors chars_per_pixel */
1189 static const char * file_icons_tbl_computer_xpm
[] = {
1216 static const char * file_icons_tbl_drive_xpm
[] = {
1243 static const char *file_icons_tbl_cdrom_xpm
[] = {
1273 static const char * file_icons_tbl_floppy_xpm
[] = {
1300 static const char * file_icons_tbl_removeable_xpm
[] = {
1326 // ----------------------------------------------------------------------------
1327 // wxFileIconsTable & friends
1328 // ----------------------------------------------------------------------------
1330 // global instance of a wxFileIconsTable
1331 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1333 // A module to allow icons table cleanup
1335 class wxFileIconsTableModule
: public wxModule
1337 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1339 wxFileIconsTableModule() {}
1340 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return TRUE
; }
1343 if (wxTheFileIconsTable
)
1345 delete wxTheFileIconsTable
;
1346 wxTheFileIconsTable
= NULL
;
1351 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1353 class wxFileIconEntry
: public wxObject
1356 wxFileIconEntry(int i
) { id
= i
; }
1361 wxFileIconsTable::wxFileIconsTable()
1364 m_smallImageList
= NULL
;
1367 wxFileIconsTable::~wxFileIconsTable()
1371 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1374 if (m_smallImageList
) delete m_smallImageList
;
1377 // delayed initialization - wait until first use (wxArtProv not created yet)
1378 void wxFileIconsTable::Create()
1380 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1381 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1382 m_smallImageList
= new wxImageList(16, 16);
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 delete m_HashTable
->Get(_T("exe"));
1405 m_HashTable
->Delete(_T("exe"));
1406 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1408 /* else put into list by GetIconID
1409 (KDE defines application/x-executable for *.exe and has nice icon)
1413 wxImageList
*wxFileIconsTable::GetSmallImageList()
1415 if (!m_smallImageList
)
1418 return m_smallImageList
;
1422 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1423 // one icon and we won't resize it
1425 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1427 wxImage
smallimg (16, 16);
1428 unsigned char *p1
, *p2
, *ps
;
1429 unsigned char mr
= img
.GetMaskRed(),
1430 mg
= img
.GetMaskGreen(),
1431 mb
= img
.GetMaskBlue();
1434 unsigned sr
, sg
, sb
, smask
;
1436 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= smallimg
.GetData();
1437 smallimg
.SetMaskColour(mr
, mr
, mr
);
1439 for (y
= 0; y
< 16; y
++)
1441 for (x
= 0; x
< 16; x
++)
1443 sr
= sg
= sb
= smask
= 0;
1444 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1445 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1448 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1449 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1452 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1453 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1456 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1457 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1462 ps
[0] = ps
[1] = ps
[2] = mr
;
1464 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
1467 p1
+= 32 * 3, p2
+= 32 * 3;
1470 return wxBitmap(smallimg
);
1473 // finds empty borders and return non-empty area of image:
1474 static wxImage
CutEmptyBorders(const wxImage
& img
)
1476 unsigned char mr
= img
.GetMaskRed(),
1477 mg
= img
.GetMaskGreen(),
1478 mb
= img
.GetMaskBlue();
1479 unsigned char *dt
= img
.GetData(), *dttmp
;
1480 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1482 unsigned top
, bottom
, left
, right
, i
;
1485 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1486 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
1488 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
1491 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1494 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1496 MK_DTTMP(0, bottom
);
1497 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1500 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
1503 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1506 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
1509 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1512 top
--, left
--, bottom
++, right
++;
1514 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1516 #endif // wxUSE_MIMETYPE
1518 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1520 if (!m_smallImageList
)
1524 if (!extension
.IsEmpty())
1526 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1527 if (entry
) return (entry
-> id
);
1530 wxFileType
*ft
= (mime
.IsEmpty()) ?
1531 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1532 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1534 wxIconLocation iconLoc
;
1536 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1538 ic
= wxIcon(iconLoc
);
1546 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1551 tmpBmp
.CopyFromIcon(ic
);
1552 wxImage img
= tmpBmp
.ConvertToImage();
1554 int id
= m_smallImageList
->GetImageCount();
1555 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
1556 m_smallImageList
->Add(wxBitmap(img
));
1559 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
1560 m_smallImageList
->Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
1562 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1564 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1567 #else // !wxUSE_MIMETYPE
1569 if (extension
== wxT("exe"))
1573 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1576 #endif // wxUSE_DIRDLG