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"
28 #include "wx/button.h"
29 #include "wx/layout.h"
30 #include "wx/msgdlg.h"
31 #include "wx/textctrl.h"
32 #include "wx/textdlg.h"
33 #include "wx/filefn.h"
34 #include "wx/cmndata.h"
35 #include "wx/gdicmn.h"
37 #include "wx/imaglist.h"
41 #include "wx/tokenzr.h"
43 #include "wx/settings.h"
44 #include "wx/artprov.h"
46 #include "wx/mimetype.h"
48 #include "wx/choice.h"
51 #include "wx/statline.h"
54 #if defined(__WXMAC__)
55 #include "wx/mac/private.h" // includes mac headers
61 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
62 // older releases don't, but it should be verified and the checks modified
64 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
84 #if defined(__WXMAC__)
86 # include "MoreFilesX.h"
88 # include "MoreFilesExtras.h"
96 // If compiled under Windows, this macro can cause problems
101 // ----------------------------------------------------------------------------
102 // wxGetAvailableDrives, for WINDOWS, DOS, WXPM, MAC, UNIX (returns "/")
103 // ----------------------------------------------------------------------------
105 size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
)
107 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
110 wxChar driveBuffer
[256];
111 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
116 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
117 name
.Printf(wxT("%c:"), driveBuffer
[i
]);
119 int imageId
= wxFileIconsTable::drive
;
120 int driveType
= ::GetDriveType(path
);
123 case DRIVE_REMOVABLE
:
124 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
125 imageId
= wxFileIconsTable::floppy
;
127 imageId
= wxFileIconsTable::removeable
;
130 imageId
= wxFileIconsTable::cdrom
;
135 imageId
= wxFileIconsTable::drive
;
141 icon_ids
.Add(imageId
);
143 while (driveBuffer
[i
] != wxT('\0'))
146 if (driveBuffer
[i
] == wxT('\0'))
152 /* If we can switch to the drive, it exists. */
153 for( drive
= 1; drive
<= 26; drive
++ )
156 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
157 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
159 if (wxIsDriveAvailable(path
))
163 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
166 #endif // __WIN32__/!__WIN32__
168 #elif defined(__WXMAC__)
171 ItemCount theVolCount
;
172 char thePath
[FILENAME_MAX
];
174 if (FSGetMountedVolumes(&theVolRefs
, &theVolCount
) == noErr
) {
176 ::HLock( (Handle
)theVolRefs
) ;
177 for (index
= 0; index
< theVolCount
; ++index
) {
178 // get the POSIX path associated with the FSRef
179 if ( FSRefMakePath(&((*theVolRefs
)[index
]),
180 (UInt8
*)thePath
, sizeof(thePath
)) != noErr
) {
183 // add path separator at end if necessary
184 wxString
path( thePath
) ;
185 if (path
.Last() != wxFILE_SEP_PATH
) {
186 path
+= wxFILE_SEP_PATH
;
188 // get Mac volume name for display
189 FSVolumeRefNum vRefNum
;
190 HFSUniStr255 volumeName
;
192 if ( FSGetVRefNum(&((*theVolRefs
)[index
]), &vRefNum
) != noErr
) {
195 if ( FSGetVInfo(vRefNum
, &volumeName
, NULL
, NULL
) != noErr
) {
198 // get C string from Unicode HFS name
199 // see: http://developer.apple.com/carbon/tipsandtricks.html
200 CFStringRef cfstr
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
203 // Do something with str
204 char *cstr
= NewPtr(CFStringGetLength(cfstr
) + 1);
205 if (( cstr
== NULL
) ||
206 !CFStringGetCString(cfstr
, cstr
, CFStringGetLength(cfstr
) + 1,
207 kCFStringEncodingMacRoman
))
212 wxString
name( cstr
);
216 GetVolParmsInfoBuffer volParmsInfo
;
218 if ( FSGetVolParms(vRefNum
, sizeof(volParmsInfo
), &volParmsInfo
, &actualSize
) != noErr
) {
225 if ( VolIsEjectable(&volParmsInfo
) )
226 icon_ids
.Add(wxFileIconsTable::cdrom
);
228 icon_ids
.Add(wxFileIconsTable::drive
);
230 ::HUnlock( (Handle
)theVolRefs
);
231 ::DisposeHandle( (Handle
)theVolRefs
);
238 short actualCount
= 0 ;
239 if (OnLine(&volume
, 1, &actualCount
, &index
) != noErr
|| actualCount
==0)
244 wxString name
= wxMacFSSpec2MacFilename( &volume
);
245 paths
.Add(name
+ wxFILE_SEP_PATH
);
247 icon_ids
.Add(wxFileIconsTable::drive
);
251 #elif defined(__UNIX__)
254 icon_ids
.Add(wxFileIconsTable::computer
);
256 #error "Unsupported platform in wxGenericDirCtrl!"
258 return paths
.GetCount();
261 // ----------------------------------------------------------------------------
262 // wxIsDriveAvailable
263 // ----------------------------------------------------------------------------
267 bool wxIsDriveAvailable(const wxString
& dirName
)
269 // FIXME_MGL - this method leads to hang up under Watcom for some reason
271 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
273 wxString
dirNameLower(dirName
.Lower());
274 // VS: always return TRUE for removable media, since Win95 doesn't
275 // like it when MS-DOS app accesses empty floppy drive
276 return (dirNameLower
[0u] == wxT('a') ||
277 dirNameLower
[0u] == wxT('b') ||
278 wxPathExists(dirNameLower
));
285 #elif defined(__WINDOWS__) || defined(__WXPM__)
287 int setdrive(int drive
)
289 #if defined(__GNUWIN32__) && \
290 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
291 return _chdrive(drive
);
295 if (drive
< 1 || drive
> 31)
297 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
298 newdrive
[1] = wxT(':');
299 newdrive
[2] = wxT('\0');
300 #if defined(__WXMSW__)
302 if (wxSetWorkingDirectory(newdrive
))
304 if (::SetCurrentDirectory(newdrive
))
307 // VA doesn't know what LPSTR is and has its own set
308 if (DosSetCurrentDir((PSZ
)newdrive
))
316 bool wxIsDriveAvailable(const wxString
& dirName
)
319 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
323 // Check if this is a root directory and if so,
324 // whether the drive is available.
325 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
327 wxString
dirNameLower(dirName
.Lower());
328 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
329 success
= wxPathExists(dirNameLower
);
331 int currentDrive
= _getdrive();
332 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
333 int err
= setdrive( thisDrive
) ;
334 setdrive( currentDrive
);
343 (void) SetErrorMode(errorMode
);
348 #endif // __WINDOWS__ || __WXPM__
350 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
351 // and sort regardless of case.
352 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(const void *first
, const void *second
)
354 wxString
*strFirst
= (wxString
*)first
;
355 wxString
*strSecond
= (wxString
*)second
;
357 return strFirst
->CmpNoCase(*strSecond
);
360 //-----------------------------------------------------------------------------
362 //-----------------------------------------------------------------------------
364 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
369 /* Insert logic to detect hidden files here
370 * In UnixLand we just check whether the first char is a dot
371 * For FileNameFromPath read LastDirNameInThisPath ;-) */
372 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
374 m_isExpanded
= FALSE
;
378 wxDirItemData::~wxDirItemData()
382 void wxDirItemData::SetNewDirName(const wxString
& path
)
385 m_name
= wxFileNameFromPath(path
);
388 bool wxDirItemData::HasSubDirs() const
390 if (m_path
.IsEmpty())
396 if ( !dir
.Open(m_path
) )
400 return dir
.HasSubDirs();
403 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
405 if (m_path
.IsEmpty())
411 if ( !dir
.Open(m_path
) )
415 return dir
.HasFiles();
418 //-----------------------------------------------------------------------------
420 //-----------------------------------------------------------------------------
422 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
424 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
425 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
426 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
427 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
428 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
429 EVT_SIZE (wxGenericDirCtrl::OnSize
)
432 wxGenericDirCtrl::wxGenericDirCtrl(void)
437 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
443 const wxString
& filter
,
445 const wxString
& name
)
447 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
450 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
454 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_HIDE_ROOT
;
456 if (style
& wxDIRCTRL_EDIT_LABELS
)
457 treeStyle
|= wxTR_EDIT_LABELS
;
459 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
460 treeStyle
|= wxNO_BORDER
;
462 long filterStyle
= 0;
463 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
464 filterStyle
|= wxNO_BORDER
;
466 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
468 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
469 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
474 SetFilterIndex(defaultFilter
);
476 if (m_filterListCtrl
)
477 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
479 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
481 m_showHidden
= FALSE
;
482 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
486 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
487 rootName
= _("Computer");
489 rootName
= _("Sections");
492 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
493 m_treeCtrl
->SetItemHasChildren(m_rootId
);
494 ExpandDir(m_rootId
); // automatically expand first level
496 // Expand and select the default path
497 if (!m_defaultPath
.IsEmpty())
498 ExpandPath(m_defaultPath
);
505 wxGenericDirCtrl::~wxGenericDirCtrl()
509 void wxGenericDirCtrl::Init()
511 m_showHidden
= FALSE
;
513 m_currentFilterStr
= wxEmptyString
; // Default: any file
515 m_filterListCtrl
= NULL
;
518 void wxGenericDirCtrl::ShowHidden( bool show
)
522 wxString path
= GetPath();
527 void wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
529 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
531 wxTreeItemId id
= m_treeCtrl
->AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
533 m_treeCtrl
->SetItemHasChildren(id
);
536 void wxGenericDirCtrl::SetupSections()
538 wxArrayString paths
, names
;
541 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
543 for (n
= 0; n
< count
; n
++)
545 AddSection(paths
[n
], names
[n
], icons
[n
]);
549 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
551 // don't rename the main entry "Sections"
552 if (event
.GetItem() == m_rootId
)
558 // don't rename the individual sections
559 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
566 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
568 if ((event
.GetLabel().IsEmpty()) ||
569 (event
.GetLabel() == _(".")) ||
570 (event
.GetLabel() == _("..")) ||
571 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
572 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
573 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
575 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
581 wxTreeItemId id
= event
.GetItem();
582 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
585 wxString
new_name( wxPathOnly( data
->m_path
) );
586 new_name
+= wxString(wxFILE_SEP_PATH
);
587 new_name
+= event
.GetLabel();
591 if (wxFileExists(new_name
))
593 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
598 if (wxRenameFile(data
->m_path
,new_name
))
600 data
->SetNewDirName( new_name
);
604 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
610 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
612 wxTreeItemId parentId
= event
.GetItem();
614 // VS: this is needed because the event handler is called from wxTreeCtrl
615 // ctor when wxTR_HIDE_ROOT was specified
617 m_rootId
= m_treeCtrl
->GetRootItem();
622 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
624 CollapseDir(event
.GetItem());
627 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
631 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
632 if (!data
->m_isExpanded
)
635 data
->m_isExpanded
= FALSE
;
637 /* Workaround because DeleteChildren has disapeared (why?) and
638 * CollapseAndReset doesn't work as advertised (deletes parent too) */
639 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
642 m_treeCtrl
->Delete(child
);
643 /* Not GetNextChild below, because the cookie mechanism can't
644 * handle disappearing children! */
645 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
649 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
651 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
653 if (data
->m_isExpanded
)
656 data
->m_isExpanded
= TRUE
;
658 if (parentId
== m_treeCtrl
->GetRootItem())
666 wxString search
,path
,filename
;
668 wxString
dirName(data
->m_path
);
670 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
671 // Check if this is a root directory and if so,
672 // whether the drive is avaiable.
673 if (!wxIsDriveAvailable(dirName
))
675 data
->m_isExpanded
= FALSE
;
676 //wxMessageBox(wxT("Sorry, this drive is not available."));
681 // This may take a longish time. Go to busy cursor
684 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
685 if (dirName
.Last() == ':')
686 dirName
+= wxString(wxFILE_SEP_PATH
);
690 wxArrayString filenames
;
693 wxString eachFilename
;
700 int style
= wxDIR_DIRS
;
701 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
702 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
706 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
708 dirs
.Add(eachFilename
);
711 while (d
.GetNext(& eachFilename
));
714 dirs
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
716 // Now do the filenames -- but only if we're allowed to
717 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
725 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
729 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
731 filenames
.Add(eachFilename
);
734 while (d
.GetNext(& eachFilename
));
737 filenames
.Sort((wxArrayString::CompareFunction
) wxDirCtrlStringCompareFunction
);
740 // Add the sorted dirs
742 for (i
= 0; i
< dirs
.Count(); i
++)
744 wxString
eachFilename(dirs
[i
]);
746 if (!wxEndsWithPathSeparator(path
))
747 path
+= wxString(wxFILE_SEP_PATH
);
748 path
+= eachFilename
;
750 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
751 wxTreeItemId id
= m_treeCtrl
->AppendItem( parentId
, eachFilename
,
752 wxFileIconsTable::folder
, -1, dir_item
);
753 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
754 wxTreeItemIcon_Expanded
);
756 // Has this got any children? If so, make it expandable.
757 // (There are two situations when a dir has children: either it
758 // has subdirectories or it contains files that weren't filtered
759 // out. The latter only applies to dirctrl with files.)
760 if ( dir_item
->HasSubDirs() ||
761 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
762 dir_item
->HasFiles(m_currentFilterStr
)) )
764 m_treeCtrl
->SetItemHasChildren(id
);
768 // Add the sorted filenames
769 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
771 for (i
= 0; i
< filenames
.Count(); i
++)
773 wxString
eachFilename(filenames
[i
]);
775 if (!wxEndsWithPathSeparator(path
))
776 path
+= wxString(wxFILE_SEP_PATH
);
777 path
+= eachFilename
;
778 //path = dirName + wxString(wxT("/")) + eachFilename;
779 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
780 int image_id
= wxFileIconsTable::file
;
781 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
782 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
783 (void)m_treeCtrl
->AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
788 void wxGenericDirCtrl::ReCreateTree()
790 CollapseDir(m_treeCtrl
->GetRootItem());
791 ExpandDir(m_treeCtrl
->GetRootItem());
794 // Find the child that matches the first part of 'path'.
795 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
796 // then the child for /usr is returned.
797 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
799 wxString
path2(path
);
801 // Make sure all separators are as per the current platform
802 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
803 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
805 // Append a separator to foil bogus substring matching
806 path2
+= wxString(wxFILE_SEP_PATH
);
808 // In MSW or PM, case is not significant
809 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
814 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
815 while (childId
.IsOk())
817 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
819 if (data
&& !data
->m_path
.IsEmpty())
821 wxString
childPath(data
->m_path
);
822 if (!wxEndsWithPathSeparator(childPath
))
823 childPath
+= wxString(wxFILE_SEP_PATH
);
825 // In MSW and PM, case is not significant
826 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
827 childPath
.MakeLower();
830 if (childPath
.Len() <= path2
.Len())
832 wxString path3
= path2
.Mid(0, childPath
.Len());
833 if (childPath
== path3
)
835 if (path3
.Len() == path2
.Len())
844 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
846 wxTreeItemId invalid
;
850 // Try to expand as much of the given path as possible,
851 // and select the given tree item.
852 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
855 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
856 wxTreeItemId lastId
= id
; // The last non-zero id
857 while (id
.IsOk() && !done
)
861 id
= FindChild(id
, path
, done
);
867 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
870 m_treeCtrl
->Expand(lastId
);
872 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
874 // Find the first file in this directory
876 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
877 bool selectedChild
= FALSE
;
878 while (childId
.IsOk())
880 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
882 if (data
&& data
->m_path
!= wxT("") && !data
->m_isDir
)
884 m_treeCtrl
->SelectItem(childId
);
885 m_treeCtrl
->EnsureVisible(childId
);
886 selectedChild
= TRUE
;
889 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
893 m_treeCtrl
->SelectItem(lastId
);
894 m_treeCtrl
->EnsureVisible(lastId
);
899 m_treeCtrl
->SelectItem(lastId
);
900 m_treeCtrl
->EnsureVisible(lastId
);
909 wxString
wxGenericDirCtrl::GetPath() const
911 wxTreeItemId id
= m_treeCtrl
->GetSelection();
914 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
918 return wxEmptyString
;
921 wxString
wxGenericDirCtrl::GetFilePath() const
923 wxTreeItemId id
= m_treeCtrl
->GetSelection();
926 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
928 return wxEmptyString
;
933 return wxEmptyString
;
936 void wxGenericDirCtrl::SetPath(const wxString
& path
)
938 m_defaultPath
= path
;
945 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
947 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
949 // This may take a longish time. Go to busy cursor
954 wxString search
,path
,filename
;
956 wxString
dirName(data
->m_path
);
958 #if defined(__WXMSW__) || defined(__WXPM__)
959 if (dirName
.Last() == ':')
960 dirName
+= wxString(wxFILE_SEP_PATH
);
964 wxString eachFilename
;
971 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
975 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
977 filenames
.Add(eachFilename
);
980 while (d
.GetNext(& eachFilename
)) ;
986 void wxGenericDirCtrl::SetFilterIndex(int n
)
991 if (ExtractWildcard(m_filter
, n
, f
, d
))
992 m_currentFilterStr
= f
;
994 m_currentFilterStr
= wxT("*.*");
997 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1002 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1003 m_currentFilterStr
= f
;
1005 m_currentFilterStr
= wxT("*.*");
1008 // Extract description and actual filter from overall filter string
1009 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1011 wxArrayString filters
, descriptions
;
1012 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1013 if (count
> 0 && n
< count
)
1015 filter
= filters
[n
];
1016 description
= descriptions
[n
];
1023 // Parses the global filter, returning the number of filters.
1024 // Returns 0 if none or if there's a problem.
1025 // filterStr is in the form:
1027 // "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1029 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1031 wxString
str(filterStr
);
1033 wxString description
, filter
;
1035 bool finished
= FALSE
;
1038 pos
= str
.Find(wxT('|'));
1040 return 0; // Problem
1041 description
= str
.Left(pos
);
1042 str
= str
.Mid(pos
+1);
1043 pos
= str
.Find(wxT('|'));
1051 filter
= str
.Left(pos
);
1052 str
= str
.Mid(pos
+1);
1054 descriptions
.Add(description
);
1055 filters
.Add(filter
);
1059 return filters
.Count();
1062 void wxGenericDirCtrl::DoResize()
1064 wxSize sz
= GetClientSize();
1065 int verticalSpacing
= 3;
1069 if (m_filterListCtrl
)
1072 // For some reason, this is required in order for the
1073 // correct control height to always be returned, rather
1074 // than the drop-down list height which is sometimes returned.
1075 wxSize oldSize
= m_filterListCtrl
->GetSize();
1076 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
+10, -1, wxSIZE_USE_EXISTING
);
1077 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
, -1, wxSIZE_USE_EXISTING
);
1079 filterSz
= m_filterListCtrl
->GetSize();
1080 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1082 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1083 if (m_filterListCtrl
)
1085 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1086 // Don't know why, but this needs refreshing after a resize (wxMSW)
1087 m_filterListCtrl
->Refresh();
1093 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1098 //-----------------------------------------------------------------------------
1099 // wxDirFilterListCtrl
1100 //-----------------------------------------------------------------------------
1102 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1104 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1105 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1108 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1114 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1117 void wxDirFilterListCtrl::Init()
1122 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1124 int sel
= GetSelection();
1126 wxString currentPath
= m_dirCtrl
->GetPath();
1128 m_dirCtrl
->SetFilterIndex(sel
);
1130 // If the filter has changed, the view is out of date, so
1131 // collapse the tree.
1132 m_dirCtrl
->ReCreateTree();
1134 // Try to restore the selection, or at least the directory
1135 m_dirCtrl
->ExpandPath(currentPath
);
1138 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1141 wxArrayString descriptions
, filters
;
1142 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1144 if (n
> 0 && defaultFilter
< (int) n
)
1147 for (i
= 0; i
< n
; i
++)
1148 Append(descriptions
[i
]);
1149 SetSelection(defaultFilter
);
1153 // ----------------------------------------------------------------------------
1154 // wxFileIconsTable icons
1155 // ----------------------------------------------------------------------------
1158 static const char * file_icons_tbl_folder_open_xpm
[] = {
1159 /* width height ncolors chars_per_pixel */
1187 static const char * file_icons_tbl_computer_xpm
[] = {
1214 static const char * file_icons_tbl_drive_xpm
[] = {
1241 static const char *file_icons_tbl_cdrom_xpm
[] = {
1271 static const char * file_icons_tbl_floppy_xpm
[] = {
1298 static const char * file_icons_tbl_removeable_xpm
[] = {
1324 // ----------------------------------------------------------------------------
1325 // wxFileIconsTable & friends
1326 // ----------------------------------------------------------------------------
1328 // global instance of a wxFileIconsTable
1329 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1331 // A module to allow icons table cleanup
1333 class wxFileIconsTableModule
: public wxModule
1335 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1337 wxFileIconsTableModule() {}
1338 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return TRUE
; }
1341 if (wxTheFileIconsTable
)
1343 delete wxTheFileIconsTable
;
1344 wxTheFileIconsTable
= NULL
;
1349 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1351 class wxFileIconEntry
: public wxObject
1354 wxFileIconEntry(int i
) { id
= i
; }
1359 wxFileIconsTable::wxFileIconsTable()
1362 m_smallImageList
= NULL
;
1365 wxFileIconsTable::~wxFileIconsTable()
1367 if (m_HashTable
) delete m_HashTable
;
1368 if (m_smallImageList
) delete m_smallImageList
;
1371 // delayed initialization - wait until first use (wxArtProv not created yet)
1372 void wxFileIconsTable::Create()
1374 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1375 printf("creating \n"); fflush(stdout
);
1376 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1377 m_smallImageList
= new wxImageList(16, 16);
1379 m_HashTable
->DeleteContents(TRUE
);
1381 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
, wxART_CMN_DIALOG
));
1383 m_smallImageList
->Add(wxIcon(file_icons_tbl_folder_open_xpm
));
1385 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1387 m_smallImageList
->Add(wxIcon(file_icons_tbl_drive_xpm
));
1389 m_smallImageList
->Add(wxIcon(file_icons_tbl_cdrom_xpm
));
1391 m_smallImageList
->Add(wxIcon(file_icons_tbl_floppy_xpm
));
1393 m_smallImageList
->Add(wxIcon(file_icons_tbl_removeable_xpm
));
1395 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
, wxART_CMN_DIALOG
));
1397 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1399 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
, wxART_CMN_DIALOG
));
1400 m_HashTable
->Delete(_T("exe"));
1401 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1403 /* else put into list by GetIconID
1404 (KDE defines application/x-executable for *.exe and has nice icon)
1408 wxImageList
*wxFileIconsTable::GetSmallImageList()
1410 if (!m_smallImageList
)
1413 return m_smallImageList
;
1417 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1418 // one icon and we won't resize it
1420 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1422 wxImage
smallimg (16, 16);
1423 unsigned char *p1
, *p2
, *ps
;
1424 unsigned char mr
= img
.GetMaskRed(),
1425 mg
= img
.GetMaskGreen(),
1426 mb
= img
.GetMaskBlue();
1429 unsigned sr
, sg
, sb
, smask
;
1431 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= smallimg
.GetData();
1432 smallimg
.SetMaskColour(mr
, mr
, mr
);
1434 for (y
= 0; y
< 16; y
++)
1436 for (x
= 0; x
< 16; x
++)
1438 sr
= sg
= sb
= smask
= 0;
1439 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1440 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1443 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1444 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1447 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1448 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1451 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1452 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1457 ps
[0] = ps
[1] = ps
[2] = mr
;
1459 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
1462 p1
+= 32 * 3, p2
+= 32 * 3;
1465 return wxBitmap(smallimg
);
1468 // finds empty borders and return non-empty area of image:
1469 static wxImage
CutEmptyBorders(const wxImage
& img
)
1471 unsigned char mr
= img
.GetMaskRed(),
1472 mg
= img
.GetMaskGreen(),
1473 mb
= img
.GetMaskBlue();
1474 unsigned char *dt
= img
.GetData(), *dttmp
;
1475 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1477 unsigned top
, bottom
, left
, right
, i
;
1480 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1481 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
1483 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
1486 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1489 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1491 MK_DTTMP(0, bottom
);
1492 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1495 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
1498 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1501 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
1504 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1507 top
--, left
--, bottom
++, right
++;
1509 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1511 #endif // wxUSE_MIMETYPE
1513 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1515 if (!m_smallImageList
)
1519 if (!extension
.IsEmpty())
1521 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1522 if (entry
) return (entry
-> id
);
1525 wxFileType
*ft
= (mime
.IsEmpty()) ?
1526 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1527 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1529 if (ft
== NULL
|| (!ft
->GetIcon(&ic
)) || (!ic
.Ok()))
1532 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1537 tmpBmp
.CopyFromIcon(ic
);
1538 wxImage img
= tmpBmp
.ConvertToImage();
1542 int id
= m_smallImageList
->GetImageCount();
1543 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
1544 m_smallImageList
->Add(wxBitmap(img
));
1547 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
1548 m_smallImageList
->Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
1550 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1552 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1555 #else // !wxUSE_MIMETYPE
1557 if (extension
== wxT("exe"))
1561 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1564 #endif // wxUSE_DIRDLG