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 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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"
50 #include "wx/filedlg.h" // for wxFileDialogBase::ParseWildcard
53 #include "wx/statline.h"
56 #if defined(__WXMAC__)
57 #include "wx/mac/private.h" // includes mac headers
63 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
64 // older releases don't, but it should be verified and the checks modified
66 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
67 #if !defined(__WXWINCE__)
85 extern bool wxIsDriveAvailable(const wxString
& dirName
);
88 #if defined(__WXMAC__)
90 # include "MoreFilesX.h"
92 # include "MoreFilesExtras.h"
100 // If compiled under Windows, this macro can cause problems
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 // No logical drives; return "\"
115 paths
.Add(wxT("\\"));
116 names
.Add(wxT("\\"));
118 #elif defined(__WIN32__)
119 wxChar driveBuffer
[256];
120 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
125 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
126 name
.Printf(wxT("%c:"), driveBuffer
[i
]);
128 int imageId
= wxFileIconsTable::drive
;
129 int driveType
= ::GetDriveType(path
);
132 case DRIVE_REMOVABLE
:
133 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
134 imageId
= wxFileIconsTable::floppy
;
136 imageId
= wxFileIconsTable::removeable
;
139 imageId
= wxFileIconsTable::cdrom
;
144 imageId
= wxFileIconsTable::drive
;
150 icon_ids
.Add(imageId
);
152 while (driveBuffer
[i
] != wxT('\0'))
155 if (driveBuffer
[i
] == wxT('\0'))
161 /* If we can switch to the drive, it exists. */
162 for( drive
= 1; drive
<= 26; drive
++ )
165 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
166 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
168 if (wxIsDriveAvailable(path
))
172 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
175 #endif // __WIN32__/!__WIN32__
177 #elif defined(__WXMAC__)
180 ItemCount theVolCount
;
181 char thePath
[FILENAME_MAX
];
183 if (FSGetMountedVolumes(&theVolRefs
, &theVolCount
) == noErr
) {
185 ::HLock( (Handle
)theVolRefs
) ;
186 for (index
= 0; index
< theVolCount
; ++index
) {
187 // get the POSIX path associated with the FSRef
188 if ( FSRefMakePath(&((*theVolRefs
)[index
]),
189 (UInt8
*)thePath
, sizeof(thePath
)) != noErr
) {
192 // add path separator at end if necessary
193 wxString
path( thePath
, wxConvLocal
) ;
194 if (path
.Last() != wxFILE_SEP_PATH
) {
195 path
+= wxFILE_SEP_PATH
;
197 // get Mac volume name for display
198 FSVolumeRefNum vRefNum
;
199 HFSUniStr255 volumeName
;
201 if ( FSGetVRefNum(&((*theVolRefs
)[index
]), &vRefNum
) != noErr
) {
204 if ( FSGetVInfo(vRefNum
, &volumeName
, NULL
, NULL
) != noErr
) {
207 // get C string from Unicode HFS name
208 // see: http://developer.apple.com/carbon/tipsandtricks.html
209 CFStringRef cfstr
= CFStringCreateWithCharacters( kCFAllocatorDefault
,
212 // Do something with str
213 char *cstr
= NewPtr(CFStringGetLength(cfstr
) + 1);
214 if (( cstr
== NULL
) ||
215 !CFStringGetCString(cfstr
, cstr
, CFStringGetLength(cfstr
) + 1,
216 kCFStringEncodingMacRoman
))
221 wxString
name( cstr
, wxConvLocal
);
225 GetVolParmsInfoBuffer volParmsInfo
;
227 if ( FSGetVolParms(vRefNum
, sizeof(volParmsInfo
), &volParmsInfo
, &actualSize
) != noErr
) {
234 if ( VolIsEjectable(&volParmsInfo
) )
235 icon_ids
.Add(wxFileIconsTable::cdrom
);
237 icon_ids
.Add(wxFileIconsTable::drive
);
239 ::HUnlock( (Handle
)theVolRefs
);
240 ::DisposeHandle( (Handle
)theVolRefs
);
247 short actualCount
= 0 ;
248 if (OnLine(&volume
, 1, &actualCount
, &index
) != noErr
|| actualCount
==0)
253 wxString name
= wxMacFSSpec2MacFilename( &volume
);
254 paths
.Add(name
+ wxFILE_SEP_PATH
);
256 icon_ids
.Add(wxFileIconsTable::drive
);
260 #elif defined(__UNIX__)
263 icon_ids
.Add(wxFileIconsTable::computer
);
265 #error "Unsupported platform in wxGenericDirCtrl!"
267 return paths
.GetCount();
270 // ----------------------------------------------------------------------------
271 // wxIsDriveAvailable
272 // ----------------------------------------------------------------------------
276 bool wxIsDriveAvailable(const wxString
& dirName
)
278 // FIXME_MGL - this method leads to hang up under Watcom for some reason
280 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
282 wxString
dirNameLower(dirName
.Lower());
283 // VS: always return TRUE for removable media, since Win95 doesn't
284 // like it when MS-DOS app accesses empty floppy drive
285 return (dirNameLower
[0u] == wxT('a') ||
286 dirNameLower
[0u] == wxT('b') ||
287 wxPathExists(dirNameLower
));
294 #elif defined(__WINDOWS__) || defined(__WXPM__)
296 int setdrive(int drive
)
300 #elif defined(__GNUWIN32__) && \
301 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
302 return _chdrive(drive
);
306 if (drive
< 1 || drive
> 31)
308 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
309 newdrive
[1] = wxT(':');
310 newdrive
[2] = wxT('\0');
311 #if defined(__WXMSW__)
312 if (::SetCurrentDirectory(newdrive
))
314 // VA doesn't know what LPSTR is and has its own set
315 if (DosSetCurrentDir((PSZ
)newdrive
))
323 bool wxIsDriveAvailable(const wxString
& dirName
)
329 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
333 // Check if this is a root directory and if so,
334 // whether the drive is available.
335 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
337 wxString
dirNameLower(dirName
.Lower());
338 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
339 success
= wxPathExists(dirNameLower
);
341 int currentDrive
= _getdrive();
342 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
343 int err
= setdrive( thisDrive
) ;
344 setdrive( currentDrive
);
353 (void) SetErrorMode(errorMode
);
359 #endif // __WINDOWS__ || __WXPM__
362 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
363 // and sort regardless of case.
364 static int LINKAGEMODE
wxDirCtrlStringCompareFunction(wxString
* strFirst
, wxString
* strSecond
)
366 return strFirst
->CmpNoCase(*strSecond
);
369 //-----------------------------------------------------------------------------
371 //-----------------------------------------------------------------------------
373 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
378 /* Insert logic to detect hidden files here
379 * In UnixLand we just check whether the first char is a dot
380 * For FileNameFromPath read LastDirNameInThisPath ;-) */
381 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
383 m_isExpanded
= FALSE
;
387 wxDirItemData::~wxDirItemData()
391 void wxDirItemData::SetNewDirName(const wxString
& path
)
394 m_name
= wxFileNameFromPath(path
);
397 bool wxDirItemData::HasSubDirs() const
399 if (m_path
.IsEmpty())
405 if ( !dir
.Open(m_path
) )
409 return dir
.HasSubDirs();
412 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
414 if (m_path
.IsEmpty())
420 if ( !dir
.Open(m_path
) )
424 return dir
.HasFiles();
427 //-----------------------------------------------------------------------------
429 //-----------------------------------------------------------------------------
432 #if wxUSE_EXTENDED_RTTI
433 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle
)
435 wxBEGIN_FLAGS( wxGenericDirCtrlStyle
)
436 // new style border flags, we put them first to
437 // use them for streaming out
438 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
439 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
440 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
441 wxFLAGS_MEMBER(wxBORDER_RAISED
)
442 wxFLAGS_MEMBER(wxBORDER_STATIC
)
443 wxFLAGS_MEMBER(wxBORDER_NONE
)
445 // old style border flags
446 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
447 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
448 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
449 wxFLAGS_MEMBER(wxRAISED_BORDER
)
450 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
451 wxFLAGS_MEMBER(wxBORDER
)
453 // standard window styles
454 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
455 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
456 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
457 wxFLAGS_MEMBER(wxWANTS_CHARS
)
458 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
459 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
460 wxFLAGS_MEMBER(wxVSCROLL
)
461 wxFLAGS_MEMBER(wxHSCROLL
)
463 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY
)
464 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL
)
465 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST
)
466 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS
)
468 wxEND_FLAGS( wxGenericDirCtrlStyle
)
470 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl
, wxControl
,"wx/dirctrl.h")
472 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl
)
473 wxHIDE_PROPERTY( Children
)
474 wxPROPERTY( DefaultPath
, wxString
, SetDefaultPath
, GetDefaultPath
, , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
475 wxPROPERTY( Filter
, wxString
, SetFilter
, GetFilter
,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
476 wxPROPERTY( DefaultFilter
, int , SetFilterIndex
, GetFilterIndex
,, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
477 wxEND_PROPERTIES_TABLE()
479 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl
)
480 wxEND_HANDLERS_TABLE()
482 wxCONSTRUCTOR_8( wxGenericDirCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, DefaultPath
,
483 wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
, wxString
, Filter
, int , DefaultFilter
)
485 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
488 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
489 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem
)
490 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem
)
491 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem
)
492 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem
)
493 EVT_SIZE (wxGenericDirCtrl::OnSize
)
496 wxGenericDirCtrl::wxGenericDirCtrl(void)
501 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
507 const wxString
& filter
,
509 const wxString
& name
)
511 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
514 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
518 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_HIDE_ROOT
;
520 if (style
& wxDIRCTRL_EDIT_LABELS
)
521 treeStyle
|= wxTR_EDIT_LABELS
;
523 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
524 treeStyle
|= wxNO_BORDER
;
526 treeStyle
|= wxBORDER_SUNKEN
;
528 long filterStyle
= 0;
529 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
530 filterStyle
|= wxNO_BORDER
;
532 filterStyle
|= wxBORDER_SUNKEN
;
534 m_treeCtrl
= new wxTreeCtrl(this, wxID_TREECTRL
, pos
, size
, treeStyle
);
536 if (!filter
.IsEmpty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
537 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
542 SetFilterIndex(defaultFilter
);
544 if (m_filterListCtrl
)
545 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
547 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
549 m_showHidden
= FALSE
;
550 wxDirItemData
* rootData
= new wxDirItemData(wxT(""), wxT(""), TRUE
);
554 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
555 rootName
= _("Computer");
557 rootName
= _("Sections");
560 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
561 m_treeCtrl
->SetItemHasChildren(m_rootId
);
562 ExpandDir(m_rootId
); // automatically expand first level
564 // Expand and select the default path
565 if (!m_defaultPath
.IsEmpty())
566 ExpandPath(m_defaultPath
);
573 wxGenericDirCtrl::~wxGenericDirCtrl()
577 void wxGenericDirCtrl::Init()
579 m_showHidden
= FALSE
;
581 m_currentFilterStr
= wxEmptyString
; // Default: any file
583 m_filterListCtrl
= NULL
;
586 void wxGenericDirCtrl::ShowHidden( bool show
)
590 wxString path
= GetPath();
596 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
598 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,TRUE
);
600 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
602 m_treeCtrl
->SetItemHasChildren(id
);
607 void wxGenericDirCtrl::SetupSections()
609 wxArrayString paths
, names
;
612 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
614 for (n
= 0; n
< count
; n
++)
616 AddSection(paths
[n
], names
[n
], icons
[n
]);
620 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
622 // don't rename the main entry "Sections"
623 if (event
.GetItem() == m_rootId
)
629 // don't rename the individual sections
630 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
637 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
639 if ((event
.GetLabel().IsEmpty()) ||
640 (event
.GetLabel() == _(".")) ||
641 (event
.GetLabel() == _("..")) ||
642 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
643 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
644 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
646 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
652 wxTreeItemId id
= event
.GetItem();
653 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
656 wxString
new_name( wxPathOnly( data
->m_path
) );
657 new_name
+= wxString(wxFILE_SEP_PATH
);
658 new_name
+= event
.GetLabel();
662 if (wxFileExists(new_name
))
664 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
669 if (wxRenameFile(data
->m_path
,new_name
))
671 data
->SetNewDirName( new_name
);
675 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
681 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
683 wxTreeItemId parentId
= event
.GetItem();
685 // VS: this is needed because the event handler is called from wxTreeCtrl
686 // ctor when wxTR_HIDE_ROOT was specified
688 if (!m_rootId
.IsOk())
690 m_rootId
= m_treeCtrl
->GetRootItem();
695 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
697 CollapseDir(event
.GetItem());
700 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
704 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
705 if (!data
->m_isExpanded
)
708 data
->m_isExpanded
= FALSE
;
709 wxTreeItemIdValue cookie
;
710 /* Workaround because DeleteChildren has disapeared (why?) and
711 * CollapseAndReset doesn't work as advertised (deletes parent too) */
712 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
715 m_treeCtrl
->Delete(child
);
716 /* Not GetNextChild below, because the cookie mechanism can't
717 * handle disappearing children! */
718 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
722 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
724 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
726 if (data
->m_isExpanded
)
729 data
->m_isExpanded
= TRUE
;
731 if (parentId
== m_treeCtrl
->GetRootItem())
739 wxString search
,path
,filename
;
741 wxString
dirName(data
->m_path
);
743 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
744 // Check if this is a root directory and if so,
745 // whether the drive is avaiable.
746 if (!wxIsDriveAvailable(dirName
))
748 data
->m_isExpanded
= FALSE
;
749 //wxMessageBox(wxT("Sorry, this drive is not available."));
754 // This may take a longish time. Go to busy cursor
757 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
758 if (dirName
.Last() == ':')
759 dirName
+= wxString(wxFILE_SEP_PATH
);
763 wxArrayString filenames
;
766 wxString eachFilename
;
773 int style
= wxDIR_DIRS
;
774 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
775 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
779 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
781 dirs
.Add(eachFilename
);
784 while (d
.GetNext(& eachFilename
));
787 dirs
.Sort(wxDirCtrlStringCompareFunction
);
789 // Now do the filenames -- but only if we're allowed to
790 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
798 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, wxDIR_FILES
))
802 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
804 filenames
.Add(eachFilename
);
807 while (d
.GetNext(& eachFilename
));
810 filenames
.Sort(wxDirCtrlStringCompareFunction
);
813 // Add the sorted dirs
815 for (i
= 0; i
< dirs
.Count(); i
++)
817 wxString
eachFilename(dirs
[i
]);
819 if (!wxEndsWithPathSeparator(path
))
820 path
+= wxString(wxFILE_SEP_PATH
);
821 path
+= eachFilename
;
823 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,TRUE
);
824 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
825 wxFileIconsTable::folder
, -1, dir_item
);
826 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
827 wxTreeItemIcon_Expanded
);
829 // Has this got any children? If so, make it expandable.
830 // (There are two situations when a dir has children: either it
831 // has subdirectories or it contains files that weren't filtered
832 // out. The latter only applies to dirctrl with files.)
833 if ( dir_item
->HasSubDirs() ||
834 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
835 dir_item
->HasFiles(m_currentFilterStr
)) )
837 m_treeCtrl
->SetItemHasChildren(id
);
841 // Add the sorted filenames
842 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
844 for (i
= 0; i
< filenames
.Count(); i
++)
846 wxString
eachFilename(filenames
[i
]);
848 if (!wxEndsWithPathSeparator(path
))
849 path
+= wxString(wxFILE_SEP_PATH
);
850 path
+= eachFilename
;
851 //path = dirName + wxString(wxT("/")) + eachFilename;
852 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,FALSE
);
853 int image_id
= wxFileIconsTable::file
;
854 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
855 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
856 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
861 void wxGenericDirCtrl::ReCreateTree()
863 CollapseDir(m_treeCtrl
->GetRootItem());
864 ExpandDir(m_treeCtrl
->GetRootItem());
867 // Find the child that matches the first part of 'path'.
868 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
869 // then the child for /usr is returned.
870 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
872 wxString
path2(path
);
874 // Make sure all separators are as per the current platform
875 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
876 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
878 // Append a separator to foil bogus substring matching
879 path2
+= wxString(wxFILE_SEP_PATH
);
881 // In MSW or PM, case is not significant
882 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
886 wxTreeItemIdValue cookie
;
887 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
888 while (childId
.IsOk())
890 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
892 if (data
&& !data
->m_path
.IsEmpty())
894 wxString
childPath(data
->m_path
);
895 if (!wxEndsWithPathSeparator(childPath
))
896 childPath
+= wxString(wxFILE_SEP_PATH
);
898 // In MSW and PM, case is not significant
899 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
900 childPath
.MakeLower();
903 if (childPath
.Len() <= path2
.Len())
905 wxString path3
= path2
.Mid(0, childPath
.Len());
906 if (childPath
== path3
)
908 if (path3
.Len() == path2
.Len())
917 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
919 wxTreeItemId invalid
;
923 // Try to expand as much of the given path as possible,
924 // and select the given tree item.
925 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
928 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
929 wxTreeItemId lastId
= id
; // The last non-zero id
930 while (id
.IsOk() && !done
)
934 id
= FindChild(id
, path
, done
);
940 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
943 m_treeCtrl
->Expand(lastId
);
945 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
947 // Find the first file in this directory
948 wxTreeItemIdValue cookie
;
949 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
950 bool selectedChild
= FALSE
;
951 while (childId
.IsOk())
953 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
955 if (data
&& data
->m_path
!= wxT("") && !data
->m_isDir
)
957 m_treeCtrl
->SelectItem(childId
);
958 m_treeCtrl
->EnsureVisible(childId
);
959 selectedChild
= TRUE
;
962 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
966 m_treeCtrl
->SelectItem(lastId
);
967 m_treeCtrl
->EnsureVisible(lastId
);
972 m_treeCtrl
->SelectItem(lastId
);
973 m_treeCtrl
->EnsureVisible(lastId
);
982 wxString
wxGenericDirCtrl::GetPath() const
984 wxTreeItemId id
= m_treeCtrl
->GetSelection();
987 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
991 return wxEmptyString
;
994 wxString
wxGenericDirCtrl::GetFilePath() const
996 wxTreeItemId id
= m_treeCtrl
->GetSelection();
999 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1001 return wxEmptyString
;
1003 return data
->m_path
;
1006 return wxEmptyString
;
1009 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1011 m_defaultPath
= path
;
1018 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1020 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1022 // This may take a longish time. Go to busy cursor
1027 wxString search
,path
,filename
;
1029 wxString
dirName(data
->m_path
);
1031 #if defined(__WXMSW__) || defined(__WXPM__)
1032 if (dirName
.Last() == ':')
1033 dirName
+= wxString(wxFILE_SEP_PATH
);
1037 wxString eachFilename
;
1044 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1048 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1050 filenames
.Add(eachFilename
);
1053 while (d
.GetNext(& eachFilename
)) ;
1059 void wxGenericDirCtrl::SetFilterIndex(int n
)
1061 m_currentFilter
= n
;
1064 if (ExtractWildcard(m_filter
, n
, f
, d
))
1065 m_currentFilterStr
= f
;
1067 m_currentFilterStr
= wxT("*.*");
1070 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1075 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1076 m_currentFilterStr
= f
;
1078 m_currentFilterStr
= wxT("*.*");
1081 // Extract description and actual filter from overall filter string
1082 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1084 wxArrayString filters
, descriptions
;
1085 int count
= ParseFilter(filterStr
, filters
, descriptions
);
1086 if (count
> 0 && n
< count
)
1088 filter
= filters
[n
];
1089 description
= descriptions
[n
];
1096 // Parses the global filter, returning the number of filters.
1097 // Returns 0 if none or if there's a problem.
1098 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1100 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1102 return wxFileDialogBase::ParseWildcard(filterStr
, descriptions
, filters
);
1105 void wxGenericDirCtrl::DoResize()
1107 wxSize sz
= GetClientSize();
1108 int verticalSpacing
= 3;
1112 if (m_filterListCtrl
)
1115 // For some reason, this is required in order for the
1116 // correct control height to always be returned, rather
1117 // than the drop-down list height which is sometimes returned.
1118 wxSize oldSize
= m_filterListCtrl
->GetSize();
1119 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
+10, -1, wxSIZE_USE_EXISTING
);
1120 m_filterListCtrl
->SetSize(-1, -1, oldSize
.x
, -1, wxSIZE_USE_EXISTING
);
1122 filterSz
= m_filterListCtrl
->GetSize();
1123 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1125 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1126 if (m_filterListCtrl
)
1128 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1129 // Don't know why, but this needs refreshing after a resize (wxMSW)
1130 m_filterListCtrl
->Refresh();
1136 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1141 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1142 const wxString
& text
,
1143 int image
, int selectedImage
,
1144 wxTreeItemData
* data
)
1146 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1148 wxASSERT (treeCtrl
);
1152 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1156 return wxTreeItemId();
1161 //-----------------------------------------------------------------------------
1162 // wxDirFilterListCtrl
1163 //-----------------------------------------------------------------------------
1165 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1167 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1168 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter
)
1171 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1177 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1180 void wxDirFilterListCtrl::Init()
1185 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1187 int sel
= GetSelection();
1189 wxString currentPath
= m_dirCtrl
->GetPath();
1191 m_dirCtrl
->SetFilterIndex(sel
);
1193 // If the filter has changed, the view is out of date, so
1194 // collapse the tree.
1195 m_dirCtrl
->ReCreateTree();
1197 // Try to restore the selection, or at least the directory
1198 m_dirCtrl
->ExpandPath(currentPath
);
1201 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1204 wxArrayString descriptions
, filters
;
1205 size_t n
= (size_t) m_dirCtrl
->ParseFilter(filter
, filters
, descriptions
);
1207 if (n
> 0 && defaultFilter
< (int) n
)
1210 for (i
= 0; i
< n
; i
++)
1211 Append(descriptions
[i
]);
1212 SetSelection(defaultFilter
);
1217 // ----------------------------------------------------------------------------
1218 // wxFileIconsTable icons
1219 // ----------------------------------------------------------------------------
1222 static const char * file_icons_tbl_folder_open_xpm
[] = {
1223 /* width height ncolors chars_per_pixel */
1251 static const char * file_icons_tbl_computer_xpm
[] = {
1278 static const char * file_icons_tbl_drive_xpm
[] = {
1305 static const char *file_icons_tbl_cdrom_xpm
[] = {
1335 static const char * file_icons_tbl_floppy_xpm
[] = {
1362 static const char * file_icons_tbl_removeable_xpm
[] = {
1388 // ----------------------------------------------------------------------------
1389 // wxFileIconsTable & friends
1390 // ----------------------------------------------------------------------------
1392 // global instance of a wxFileIconsTable
1393 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1395 // A module to allow icons table cleanup
1397 class wxFileIconsTableModule
: public wxModule
1399 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1401 wxFileIconsTableModule() {}
1402 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return TRUE
; }
1405 if (wxTheFileIconsTable
)
1407 delete wxTheFileIconsTable
;
1408 wxTheFileIconsTable
= NULL
;
1413 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1415 class wxFileIconEntry
: public wxObject
1418 wxFileIconEntry(int i
) { id
= i
; }
1423 wxFileIconsTable::wxFileIconsTable()
1426 m_smallImageList
= NULL
;
1429 wxFileIconsTable::~wxFileIconsTable()
1433 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1436 if (m_smallImageList
) delete m_smallImageList
;
1439 // delayed initialization - wait until first use (wxArtProv not created yet)
1440 void wxFileIconsTable::Create()
1442 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1443 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1444 m_smallImageList
= new wxImageList(16, 16);
1447 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
, wxART_CMN_DIALOG
));
1449 m_smallImageList
->Add(wxIcon(file_icons_tbl_folder_open_xpm
));
1451 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1453 m_smallImageList
->Add(wxIcon(file_icons_tbl_drive_xpm
));
1455 m_smallImageList
->Add(wxIcon(file_icons_tbl_cdrom_xpm
));
1457 m_smallImageList
->Add(wxIcon(file_icons_tbl_floppy_xpm
));
1459 m_smallImageList
->Add(wxIcon(file_icons_tbl_removeable_xpm
));
1461 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
, wxART_CMN_DIALOG
));
1463 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1465 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
, wxART_CMN_DIALOG
));
1466 delete m_HashTable
->Get(_T("exe"));
1467 m_HashTable
->Delete(_T("exe"));
1468 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1470 /* else put into list by GetIconID
1471 (KDE defines application/x-executable for *.exe and has nice icon)
1475 wxImageList
*wxFileIconsTable::GetSmallImageList()
1477 if (!m_smallImageList
)
1480 return m_smallImageList
;
1484 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1485 // one icon and we won't resize it
1487 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1489 wxImage
smallimg (16, 16);
1490 unsigned char *p1
, *p2
, *ps
;
1491 unsigned char mr
= img
.GetMaskRed(),
1492 mg
= img
.GetMaskGreen(),
1493 mb
= img
.GetMaskBlue();
1496 unsigned sr
, sg
, sb
, smask
;
1498 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= smallimg
.GetData();
1499 smallimg
.SetMaskColour(mr
, mr
, mr
);
1501 for (y
= 0; y
< 16; y
++)
1503 for (x
= 0; x
< 16; x
++)
1505 sr
= sg
= sb
= smask
= 0;
1506 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1507 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1510 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1511 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1514 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1515 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1518 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1519 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1524 ps
[0] = ps
[1] = ps
[2] = mr
;
1526 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
1529 p1
+= 32 * 3, p2
+= 32 * 3;
1532 return wxBitmap(smallimg
);
1535 // finds empty borders and return non-empty area of image:
1536 static wxImage
CutEmptyBorders(const wxImage
& img
)
1538 unsigned char mr
= img
.GetMaskRed(),
1539 mg
= img
.GetMaskGreen(),
1540 mb
= img
.GetMaskBlue();
1541 unsigned char *dt
= img
.GetData(), *dttmp
;
1542 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1544 unsigned top
, bottom
, left
, right
, i
;
1547 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1548 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
1550 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
1553 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1556 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1558 MK_DTTMP(0, bottom
);
1559 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1562 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
1565 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1568 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
1571 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1574 top
--, left
--, bottom
++, right
++;
1576 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1578 #endif // wxUSE_MIMETYPE
1580 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1582 if (!m_smallImageList
)
1586 if (!extension
.IsEmpty())
1588 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1589 if (entry
) return (entry
-> id
);
1592 wxFileType
*ft
= (mime
.IsEmpty()) ?
1593 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1594 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1596 wxIconLocation iconLoc
;
1598 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1600 ic
= wxIcon(iconLoc
);
1608 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1613 tmpBmp
.CopyFromIcon(ic
);
1614 wxImage img
= tmpBmp
.ConvertToImage();
1616 int id
= m_smallImageList
->GetImageCount();
1617 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
1618 m_smallImageList
->Add(wxBitmap(img
));
1621 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
1622 m_smallImageList
->Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
1624 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1626 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1629 #else // !wxUSE_MIMETYPE
1631 if (extension
== wxT("exe"))
1635 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1638 #endif // wxUSE_DIRDLG