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"
23 #if wxUSE_DIRDLG || wxUSE_FILEDLG
25 #include "wx/generic/dirctrlg.h"
26 #include "wx/module.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)
65 #if !defined(__WXWINCE__)
74 #if defined(__OS2__) || defined(__DOS__)
84 extern bool wxIsDriveAvailable(const wxString
& dirName
);
87 #if defined(__WXMAC__)
88 # include "MoreFilesX.h"
95 // If compiled under Windows, this macro can cause problems
100 // ----------------------------------------------------------------------------
101 // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
102 // ----------------------------------------------------------------------------
104 size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
)
106 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
109 // No logical drives; return "\"
110 paths
.Add(wxT("\\"));
111 names
.Add(wxT("\\"));
112 icon_ids
.Add(wxFileIconsTable::computer
);
113 #elif defined(__WIN32__)
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
]);
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'))
153 #elif defined(__OS2__)
155 ULONG ulDriveNum
= 0;
156 ULONG ulDriveMap
= 0;
157 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
163 if (ulDriveMap
& ( 1 << i
))
166 path
.Printf(wxT("%c:\\"), 'A' + i
);
167 name
.Printf(wxT("%c:"), 'A' + i
);
170 if (path
== wxT("A:\\") || path
== wxT("B:\\"))
171 imageId
= wxFileIconsTable::floppy
;
173 imageId
= wxFileIconsTable::drive
;
176 icon_ids
.Add(imageId
);
181 #else // !__WIN32__, !__OS2__
184 /* If we can switch to the drive, it exists. */
185 for( drive
= 1; drive
<= 26; drive
++ )
188 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
189 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
191 if (wxIsDriveAvailable(path
))
195 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
198 #endif // __WIN32__/!__WIN32__
200 #elif defined(__WXMAC__)
202 ItemCount volumeIndex
= 1;
205 while( noErr
== err
)
207 HFSUniStr255 volumeName
;
209 FSVolumeInfo volumeInfo
;
210 err
= FSGetVolumeInfo(0, volumeIndex
, NULL
, kFSVolInfoFlags
, &volumeInfo
, &volumeName
, &fsRef
);
213 wxString path
= wxMacFSRefToPath( &fsRef
) ;
214 wxString name
= wxMacHFSUniStrToString( &volumeName
) ;
216 if ( (volumeInfo
.flags
& kFSVolFlagSoftwareLockedMask
) || (volumeInfo
.flags
& kFSVolFlagHardwareLockedMask
) )
218 icon_ids
.Add(wxFileIconsTable::cdrom
);
222 icon_ids
.Add(wxFileIconsTable::drive
);
224 // todo other removable
232 #elif defined(__UNIX__)
235 icon_ids
.Add(wxFileIconsTable::computer
);
237 #error "Unsupported platform in wxGenericDirCtrl!"
239 wxASSERT_MSG( (paths
.GetCount() == names
.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
240 wxASSERT_MSG( (paths
.GetCount() == icon_ids
.GetCount()), wxT("Wrong number of icons for available drives."));
241 return paths
.GetCount();
244 // ----------------------------------------------------------------------------
245 // wxIsDriveAvailable
246 // ----------------------------------------------------------------------------
250 bool wxIsDriveAvailable(const wxString
& dirName
)
252 // FIXME_MGL - this method leads to hang up under Watcom for some reason
254 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
256 wxString
dirNameLower(dirName
.Lower());
257 // VS: always return true for removable media, since Win95 doesn't
258 // like it when MS-DOS app accesses empty floppy drive
259 return (dirNameLower
[0u] == wxT('a') ||
260 dirNameLower
[0u] == wxT('b') ||
261 wxPathExists(dirNameLower
));
268 #elif defined(__WINDOWS__) || defined(__OS2__)
270 int setdrive(int drive
)
275 #elif defined(__GNUWIN32__) && \
276 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
277 return _chdrive(drive
);
281 if (drive
< 1 || drive
> 31)
283 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
284 newdrive
[1] = wxT(':');
286 newdrive
[2] = wxT('\\');
287 newdrive
[3] = wxT('\0');
289 newdrive
[2] = wxT('\0');
291 #if defined(__WXMSW__)
292 if (::SetCurrentDirectory(newdrive
))
294 // VA doesn't know what LPSTR is and has its own set
295 if (!DosSetCurrentDir((PSZ
)newdrive
))
303 bool wxIsDriveAvailable(const wxString
& dirName
)
306 wxUnusedVar(dirName
);
310 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
314 // Check if this is a root directory and if so,
315 // whether the drive is available.
316 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
318 wxString
dirNameLower(dirName
.Lower());
319 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
320 success
= wxPathExists(dirNameLower
);
323 // Avoid changing to drive since no media may be inserted.
324 if (dirNameLower
[(size_t)0] == 'a' || dirNameLower
[(size_t)0] == 'b')
327 int currentDrive
= _getdrive();
328 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
329 int err
= setdrive( thisDrive
) ;
330 setdrive( currentDrive
);
339 (void) SetErrorMode(errorMode
);
345 #endif // __WINDOWS__ || __OS2__
347 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
353 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
354 // and sort regardless of case.
355 static int wxCMPFUNC_CONV
wxDirCtrlStringCompareFunction(const wxString
& strFirst
, const wxString
& strSecond
)
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 void wxDirItemData::SetNewDirName(const wxString
& path
)
381 m_name
= wxFileNameFromPath(path
);
384 bool wxDirItemData::HasSubDirs() const
392 if ( !dir
.Open(m_path
) )
396 return dir
.HasSubDirs();
399 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
407 if ( !dir
.Open(m_path
) )
411 return dir
.HasFiles();
414 //-----------------------------------------------------------------------------
416 //-----------------------------------------------------------------------------
419 #if wxUSE_EXTENDED_RTTI
420 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle
)
422 wxBEGIN_FLAGS( wxGenericDirCtrlStyle
)
423 // new style border flags, we put them first to
424 // use them for streaming out
425 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
426 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
427 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
428 wxFLAGS_MEMBER(wxBORDER_RAISED
)
429 wxFLAGS_MEMBER(wxBORDER_STATIC
)
430 wxFLAGS_MEMBER(wxBORDER_NONE
)
432 // old style border flags
433 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
434 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
435 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
436 wxFLAGS_MEMBER(wxRAISED_BORDER
)
437 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
438 wxFLAGS_MEMBER(wxBORDER
)
440 // standard window styles
441 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
442 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
443 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
444 wxFLAGS_MEMBER(wxWANTS_CHARS
)
445 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
446 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
447 wxFLAGS_MEMBER(wxVSCROLL
)
448 wxFLAGS_MEMBER(wxHSCROLL
)
450 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY
)
451 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL
)
452 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST
)
453 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS
)
455 wxEND_FLAGS( wxGenericDirCtrlStyle
)
457 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl
, wxControl
,"wx/dirctrl.h")
459 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl
)
460 wxHIDE_PROPERTY( Children
)
461 wxPROPERTY( DefaultPath
, wxString
, SetDefaultPath
, GetDefaultPath
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
462 wxPROPERTY( Filter
, wxString
, SetFilter
, GetFilter
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
463 wxPROPERTY( DefaultFilter
, int , SetFilterIndex
, GetFilterIndex
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
464 wxPROPERTY_FLAGS( WindowStyle
, wxGenericDirCtrlStyle
, long, SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0, wxT("Helpstring"), wxT("group") )
465 wxEND_PROPERTIES_TABLE()
467 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl
)
468 wxEND_HANDLERS_TABLE()
470 wxCONSTRUCTOR_8( wxGenericDirCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, DefaultPath
,
471 wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
, wxString
, Filter
, int , DefaultFilter
)
473 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
476 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
477 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL
, wxGenericDirCtrl::OnExpandItem
)
478 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL
, wxGenericDirCtrl::OnCollapseItem
)
479 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnBeginEditItem
)
480 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnEndEditItem
)
481 EVT_SIZE (wxGenericDirCtrl::OnSize
)
484 wxGenericDirCtrl::wxGenericDirCtrl(void)
489 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
495 const wxString
& filter
,
497 const wxString
& name
)
499 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
502 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
506 long treeStyle
= wxTR_HAS_BUTTONS
| wxTR_HIDE_ROOT
;
508 if (style
& wxDIRCTRL_EDIT_LABELS
)
509 treeStyle
|= wxTR_EDIT_LABELS
;
511 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
512 treeStyle
|= wxNO_BORDER
;
514 treeStyle
|= wxBORDER_SUNKEN
;
516 long filterStyle
= 0;
517 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
518 filterStyle
|= wxNO_BORDER
;
520 filterStyle
|= wxBORDER_SUNKEN
;
522 m_treeCtrl
= CreateTreeCtrl(this, wxID_TREECTRL
,
523 wxPoint(0,0), GetClientSize(), treeStyle
);
525 if (!filter
.empty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
526 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
531 SetFilterIndex(defaultFilter
);
533 if (m_filterListCtrl
)
534 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
536 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
538 m_showHidden
= false;
539 wxDirItemData
* rootData
= new wxDirItemData(wxEmptyString
, wxEmptyString
, true);
543 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
544 rootName
= _("Computer");
546 rootName
= _("Sections");
549 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
550 m_treeCtrl
->SetItemHasChildren(m_rootId
);
551 ExpandDir(m_rootId
); // automatically expand first level
553 // Expand and select the default path
554 if (!m_defaultPath
.empty())
556 ExpandPath(m_defaultPath
);
561 // On Unix, there's only one node under the (hidden) root node. It
562 // represents the / path, so the user would always have to expand it;
563 // let's do it ourselves
564 ExpandPath(wxT("/"));
574 wxGenericDirCtrl::~wxGenericDirCtrl()
578 void wxGenericDirCtrl::Init()
580 m_showHidden
= false;
582 m_currentFilterStr
= wxEmptyString
; // Default: any file
584 m_filterListCtrl
= NULL
;
587 wxTreeCtrl
* wxGenericDirCtrl::CreateTreeCtrl(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long treeStyle
)
589 return new wxTreeCtrl(parent
, id
, pos
, size
, treeStyle
);
592 void wxGenericDirCtrl::ShowHidden( bool show
)
596 wxString path
= GetPath();
602 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
604 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,true);
606 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
608 m_treeCtrl
->SetItemHasChildren(id
);
613 void wxGenericDirCtrl::SetupSections()
615 wxArrayString paths
, names
;
618 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
620 for (n
= 0; n
< count
; n
++)
622 AddSection(paths
[n
], names
[n
], icons
[n
]);
626 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
628 // don't rename the main entry "Sections"
629 if (event
.GetItem() == m_rootId
)
635 // don't rename the individual sections
636 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
643 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
645 if ((event
.GetLabel().empty()) ||
646 (event
.GetLabel() == _(".")) ||
647 (event
.GetLabel() == _("..")) ||
648 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
649 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
650 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
652 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
658 wxTreeItemId id
= event
.GetItem();
659 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
662 wxString
new_name( wxPathOnly( data
->m_path
) );
663 new_name
+= wxString(wxFILE_SEP_PATH
);
664 new_name
+= event
.GetLabel();
668 if (wxFileExists(new_name
))
670 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
675 if (wxRenameFile(data
->m_path
,new_name
))
677 data
->SetNewDirName( new_name
);
681 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
687 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
689 wxTreeItemId parentId
= event
.GetItem();
691 // VS: this is needed because the event handler is called from wxTreeCtrl
692 // ctor when wxTR_HIDE_ROOT was specified
694 if (!m_rootId
.IsOk())
696 m_rootId
= m_treeCtrl
->GetRootItem();
701 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
703 CollapseDir(event
.GetItem());
706 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
710 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
711 if (!data
->m_isExpanded
)
714 data
->m_isExpanded
= false;
715 wxTreeItemIdValue cookie
;
716 /* Workaround because DeleteChildren has disapeared (why?) and
717 * CollapseAndReset doesn't work as advertised (deletes parent too) */
718 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
721 m_treeCtrl
->Delete(child
);
722 /* Not GetNextChild below, because the cookie mechanism can't
723 * handle disappearing children! */
724 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
728 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
730 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
732 if (data
->m_isExpanded
)
735 data
->m_isExpanded
= true;
737 if (parentId
== m_treeCtrl
->GetRootItem())
745 wxString search
,path
,filename
;
747 wxString
dirName(data
->m_path
);
749 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
750 // Check if this is a root directory and if so,
751 // whether the drive is avaiable.
752 if (!wxIsDriveAvailable(dirName
))
754 data
->m_isExpanded
= false;
755 //wxMessageBox(wxT("Sorry, this drive is not available."));
760 // This may take a longish time. Go to busy cursor
763 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
764 if (dirName
.Last() == ':')
765 dirName
+= wxString(wxFILE_SEP_PATH
);
769 wxArrayString filenames
;
772 wxString eachFilename
;
779 int style
= wxDIR_DIRS
;
780 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
781 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
785 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
787 dirs
.Add(eachFilename
);
790 while (d
.GetNext(&eachFilename
));
793 dirs
.Sort(wxDirCtrlStringCompareFunction
);
795 // Now do the filenames -- but only if we're allowed to
796 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
804 int style
= wxDIR_FILES
;
805 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
806 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
807 wxStringTokenizer strTok
;
809 strTok
.SetString(m_currentFilterStr
,wxT(";"));
810 while(strTok
.HasMoreTokens())
812 curFilter
= strTok
.GetNextToken();
813 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, style
))
817 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
819 filenames
.Add(eachFilename
);
822 while (d
.GetNext(& eachFilename
));
826 filenames
.Sort(wxDirCtrlStringCompareFunction
);
829 // Add the sorted dirs
831 for (i
= 0; i
< dirs
.Count(); i
++)
833 wxString
eachFilename(dirs
[i
]);
835 if (!wxEndsWithPathSeparator(path
))
836 path
+= wxString(wxFILE_SEP_PATH
);
837 path
+= eachFilename
;
839 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,true);
840 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
841 wxFileIconsTable::folder
, -1, dir_item
);
842 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
843 wxTreeItemIcon_Expanded
);
845 // Has this got any children? If so, make it expandable.
846 // (There are two situations when a dir has children: either it
847 // has subdirectories or it contains files that weren't filtered
848 // out. The latter only applies to dirctrl with files.)
849 if ( dir_item
->HasSubDirs() ||
850 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
851 dir_item
->HasFiles(m_currentFilterStr
)) )
853 m_treeCtrl
->SetItemHasChildren(id
);
857 // Add the sorted filenames
858 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
860 for (i
= 0; i
< filenames
.Count(); i
++)
862 wxString
eachFilename(filenames
[i
]);
864 if (!wxEndsWithPathSeparator(path
))
865 path
+= wxString(wxFILE_SEP_PATH
);
866 path
+= eachFilename
;
867 //path = dirName + wxString(wxT("/")) + eachFilename;
868 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,false);
869 int image_id
= wxFileIconsTable::file
;
870 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
871 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
872 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
877 void wxGenericDirCtrl::ReCreateTree()
879 CollapseDir(m_treeCtrl
->GetRootItem());
880 ExpandDir(m_treeCtrl
->GetRootItem());
883 // Find the child that matches the first part of 'path'.
884 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
885 // then the child for /usr is returned.
886 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
888 wxString
path2(path
);
890 // Make sure all separators are as per the current platform
891 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
892 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
894 // Append a separator to foil bogus substring matching
895 path2
+= wxString(wxFILE_SEP_PATH
);
897 // In MSW or PM, case is not significant
898 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
902 wxTreeItemIdValue cookie
;
903 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
904 while (childId
.IsOk())
906 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
908 if (data
&& !data
->m_path
.empty())
910 wxString
childPath(data
->m_path
);
911 if (!wxEndsWithPathSeparator(childPath
))
912 childPath
+= wxString(wxFILE_SEP_PATH
);
914 // In MSW and PM, case is not significant
915 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
916 childPath
.MakeLower();
919 if (childPath
.Len() <= path2
.Len())
921 wxString path3
= path2
.Mid(0, childPath
.Len());
922 if (childPath
== path3
)
924 if (path3
.Len() == path2
.Len())
933 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
935 wxTreeItemId invalid
;
939 // Try to expand as much of the given path as possible,
940 // and select the given tree item.
941 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
944 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
945 wxTreeItemId lastId
= id
; // The last non-zero id
946 while (id
.IsOk() && !done
)
950 id
= FindChild(id
, path
, done
);
956 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
959 m_treeCtrl
->Expand(lastId
);
961 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
963 // Find the first file in this directory
964 wxTreeItemIdValue cookie
;
965 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
966 bool selectedChild
= false;
967 while (childId
.IsOk())
969 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
971 if (data
&& data
->m_path
!= wxEmptyString
&& !data
->m_isDir
)
973 m_treeCtrl
->SelectItem(childId
);
974 m_treeCtrl
->EnsureVisible(childId
);
975 selectedChild
= true;
978 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
982 m_treeCtrl
->SelectItem(lastId
);
983 m_treeCtrl
->EnsureVisible(lastId
);
988 m_treeCtrl
->SelectItem(lastId
);
989 m_treeCtrl
->EnsureVisible(lastId
);
998 wxString
wxGenericDirCtrl::GetPath() const
1000 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1003 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1004 return data
->m_path
;
1007 return wxEmptyString
;
1010 wxString
wxGenericDirCtrl::GetFilePath() const
1012 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1015 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1017 return wxEmptyString
;
1019 return data
->m_path
;
1022 return wxEmptyString
;
1025 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1027 m_defaultPath
= path
;
1034 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1036 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1038 // This may take a longish time. Go to busy cursor
1043 wxString search
,path
,filename
;
1045 wxString
dirName(data
->m_path
);
1047 #if defined(__WXMSW__) || defined(__OS2__)
1048 if (dirName
.Last() == ':')
1049 dirName
+= wxString(wxFILE_SEP_PATH
);
1053 wxString eachFilename
;
1060 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1064 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1066 filenames
.Add(eachFilename
);
1069 while (d
.GetNext(& eachFilename
)) ;
1075 void wxGenericDirCtrl::SetFilterIndex(int n
)
1077 m_currentFilter
= n
;
1080 if (ExtractWildcard(m_filter
, n
, f
, d
))
1081 m_currentFilterStr
= f
;
1083 m_currentFilterStr
= wxT("*.*");
1086 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1091 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1092 m_currentFilterStr
= f
;
1094 m_currentFilterStr
= wxT("*.*");
1097 // Extract description and actual filter from overall filter string
1098 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1100 wxArrayString filters
, descriptions
;
1101 int count
= wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1102 if (count
> 0 && n
< count
)
1104 filter
= filters
[n
];
1105 description
= descriptions
[n
];
1112 #if WXWIN_COMPATIBILITY_2_4
1113 // Parses the global filter, returning the number of filters.
1114 // Returns 0 if none or if there's a problem.
1115 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1116 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1118 return wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1120 #endif // WXWIN_COMPATIBILITY_2_4
1122 void wxGenericDirCtrl::DoResize()
1124 wxSize sz
= GetClientSize();
1125 int verticalSpacing
= 3;
1129 if (m_filterListCtrl
)
1132 // For some reason, this is required in order for the
1133 // correct control height to always be returned, rather
1134 // than the drop-down list height which is sometimes returned.
1135 wxSize oldSize
= m_filterListCtrl
->GetSize();
1136 m_filterListCtrl
->SetSize(wxDefaultCoord
,
1140 wxSIZE_USE_EXISTING
);
1141 m_filterListCtrl
->SetSize(wxDefaultCoord
,
1145 wxSIZE_USE_EXISTING
);
1147 filterSz
= m_filterListCtrl
->GetSize();
1148 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1150 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1151 if (m_filterListCtrl
)
1153 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1154 // Don't know why, but this needs refreshing after a resize (wxMSW)
1155 m_filterListCtrl
->Refresh();
1161 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1166 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1167 const wxString
& text
,
1168 int image
, int selectedImage
,
1169 wxTreeItemData
* data
)
1171 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1173 wxASSERT (treeCtrl
);
1177 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1181 return wxTreeItemId();
1186 //-----------------------------------------------------------------------------
1187 // wxDirFilterListCtrl
1188 //-----------------------------------------------------------------------------
1190 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1192 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1193 EVT_CHOICE(wxID_ANY
, wxDirFilterListCtrl::OnSelFilter
)
1196 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1202 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1205 void wxDirFilterListCtrl::Init()
1210 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1212 int sel
= GetSelection();
1214 wxString currentPath
= m_dirCtrl
->GetPath();
1216 m_dirCtrl
->SetFilterIndex(sel
);
1218 // If the filter has changed, the view is out of date, so
1219 // collapse the tree.
1220 m_dirCtrl
->ReCreateTree();
1222 // Try to restore the selection, or at least the directory
1223 m_dirCtrl
->ExpandPath(currentPath
);
1226 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1229 wxArrayString descriptions
, filters
;
1230 size_t n
= (size_t) wxParseCommonDialogsFilter(filter
, descriptions
, filters
);
1232 if (n
> 0 && defaultFilter
< (int) n
)
1234 for (size_t i
= 0; i
< n
; i
++)
1235 Append(descriptions
[i
]);
1236 SetSelection(defaultFilter
);
1239 #endif // wxUSE_DIRDLG
1241 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1243 // ----------------------------------------------------------------------------
1244 // wxFileIconsTable icons
1245 // ----------------------------------------------------------------------------
1248 static const char * file_icons_tbl_folder_open_xpm
[] = {
1249 /* width height ncolors chars_per_pixel */
1277 static const char * file_icons_tbl_computer_xpm
[] = {
1304 static const char * file_icons_tbl_drive_xpm
[] = {
1331 static const char *file_icons_tbl_cdrom_xpm
[] = {
1361 static const char * file_icons_tbl_floppy_xpm
[] = {
1388 static const char * file_icons_tbl_removeable_xpm
[] = {
1414 // ----------------------------------------------------------------------------
1415 // wxFileIconsTable & friends
1416 // ----------------------------------------------------------------------------
1418 // global instance of a wxFileIconsTable
1419 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1421 // A module to allow icons table cleanup
1423 class wxFileIconsTableModule
: public wxModule
1425 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1427 wxFileIconsTableModule() {}
1428 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return true; }
1431 if (wxTheFileIconsTable
)
1433 delete wxTheFileIconsTable
;
1434 wxTheFileIconsTable
= NULL
;
1439 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1441 class wxFileIconEntry
: public wxObject
1444 wxFileIconEntry(int i
) { id
= i
; }
1449 wxFileIconsTable::wxFileIconsTable()
1452 m_smallImageList
= NULL
;
1455 wxFileIconsTable::~wxFileIconsTable()
1459 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1462 if (m_smallImageList
) delete m_smallImageList
;
1465 // delayed initialization - wait until first use (wxArtProv not created yet)
1466 void wxFileIconsTable::Create()
1468 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1469 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1470 m_smallImageList
= new wxImageList(16, 16);
1473 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
,
1477 m_smallImageList
->Add(wxIcon(file_icons_tbl_folder_open_xpm
));
1479 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1481 m_smallImageList
->Add(wxIcon(file_icons_tbl_drive_xpm
));
1483 m_smallImageList
->Add(wxIcon(file_icons_tbl_cdrom_xpm
));
1485 m_smallImageList
->Add(wxIcon(file_icons_tbl_floppy_xpm
));
1487 m_smallImageList
->Add(wxIcon(file_icons_tbl_removeable_xpm
));
1489 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
,
1493 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1495 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
,
1498 delete m_HashTable
->Get(_T("exe"));
1499 m_HashTable
->Delete(_T("exe"));
1500 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1502 /* else put into list by GetIconID
1503 (KDE defines application/x-executable for *.exe and has nice icon)
1507 wxImageList
*wxFileIconsTable::GetSmallImageList()
1509 if (!m_smallImageList
)
1512 return m_smallImageList
;
1515 #if wxUSE_MIMETYPE && wxUSE_IMAGE
1516 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1517 // one icon and we won't resize it
1519 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1521 const unsigned int size
= 16;
1523 wxImage
smallimg (size
, size
);
1524 unsigned char *p1
, *p2
, *ps
;
1525 unsigned char mr
= img
.GetMaskRed(),
1526 mg
= img
.GetMaskGreen(),
1527 mb
= img
.GetMaskBlue();
1530 unsigned sr
, sg
, sb
, smask
;
1532 p1
= img
.GetData(), p2
= img
.GetData() + 3 * size
*2, ps
= smallimg
.GetData();
1533 smallimg
.SetMaskColour(mr
, mr
, mr
);
1535 for (y
= 0; y
< size
; y
++)
1537 for (x
= 0; x
< size
; x
++)
1539 sr
= sg
= sb
= smask
= 0;
1540 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1541 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1544 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1545 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1548 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1549 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1552 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1553 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1558 ps
[0] = ps
[1] = ps
[2] = mr
;
1561 ps
[0] = (unsigned char)(sr
>> 2);
1562 ps
[1] = (unsigned char)(sg
>> 2);
1563 ps
[2] = (unsigned char)(sb
>> 2);
1567 p1
+= size
*2 * 3, p2
+= size
*2 * 3;
1570 return wxBitmap(smallimg
);
1573 // This function is currently not unused anymore
1575 // finds empty borders and return non-empty area of image:
1576 static wxImage
CutEmptyBorders(const wxImage
& img
)
1578 unsigned char mr
= img
.GetMaskRed(),
1579 mg
= img
.GetMaskGreen(),
1580 mb
= img
.GetMaskBlue();
1581 unsigned char *dt
= img
.GetData(), *dttmp
;
1582 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1584 unsigned top
, bottom
, left
, right
, i
;
1587 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1588 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1590 for (empt
= true, top
= 0; empt
&& top
< h
; top
++)
1593 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1596 for (empt
= true, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1598 MK_DTTMP(0, bottom
);
1599 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1602 for (empt
= true, left
= 0; empt
&& left
< w
; left
++)
1605 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1608 for (empt
= true, right
= w
-1; empt
&& right
> left
; right
--)
1611 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1614 top
--, left
--, bottom
++, right
++;
1616 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1620 #endif // wxUSE_MIMETYPE
1622 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1624 if (!m_smallImageList
)
1628 if (!extension
.empty())
1630 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1631 if (entry
) return (entry
-> id
);
1634 wxFileType
*ft
= (mime
.empty()) ?
1635 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1636 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1638 wxIconLocation iconLoc
;
1643 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1645 ic
= wxIcon( iconLoc
);
1654 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1659 bmp
.CopyFromIcon(ic
);
1664 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1668 const unsigned int size
= 16;
1670 int id
= m_smallImageList
->GetImageCount();
1671 if ((bmp
.GetWidth() == (int) size
) && (bmp
.GetHeight() == (int) size
))
1673 m_smallImageList
->Add(bmp
);
1678 wxImage img
= bmp
.ConvertToImage();
1680 if ((img
.GetWidth() != size
*2) || (img
.GetHeight() != size
*2))
1681 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1682 m_smallImageList
->Add(CreateAntialiasedBitmap(img
.Rescale(size
*2, size
*2)));
1684 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1686 #endif // wxUSE_IMAGE
1688 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1691 #else // !wxUSE_MIMETYPE
1694 if (extension
== wxT("exe"))
1698 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1701 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG