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 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
19 #if wxUSE_DIRDLG || wxUSE_FILEDLG
21 #include "wx/generic/dirctrlg.h"
22 #include "wx/module.h"
24 #include "wx/button.h"
25 #include "wx/layout.h"
26 #include "wx/msgdlg.h"
27 #include "wx/textctrl.h"
28 #include "wx/textdlg.h"
29 #include "wx/filefn.h"
30 #include "wx/cmndata.h"
31 #include "wx/gdicmn.h"
33 #include "wx/imaglist.h"
37 #include "wx/tokenzr.h"
39 #include "wx/settings.h"
40 #include "wx/artprov.h"
42 #include "wx/mimetype.h"
44 #include "wx/choice.h"
47 #include "wx/statline.h"
50 #if defined(__WXMAC__)
51 #include "wx/mac/private.h" // includes mac headers
56 #include "wx/msw/winundef.h"
58 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
59 // older releases don't, but it should be verified and the checks modified
61 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
62 #if !defined(__WXWINCE__)
71 #if defined(__OS2__) || defined(__DOS__)
81 extern bool wxIsDriveAvailable(const wxString
& dirName
);
84 #if defined(__WXMAC__)
85 # include "MoreFilesX.h"
92 // If compiled under Windows, this macro can cause problems
97 // ----------------------------------------------------------------------------
98 // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
99 // ----------------------------------------------------------------------------
101 size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
)
103 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
106 // No logical drives; return "\"
107 paths
.Add(wxT("\\"));
108 names
.Add(wxT("\\"));
109 icon_ids
.Add(wxFileIconsTable::computer
);
110 #elif defined(__WIN32__)
111 wxChar driveBuffer
[256];
112 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
117 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
118 name
.Printf(wxT("%c:"), driveBuffer
[i
]);
121 int driveType
= ::GetDriveType(path
);
124 case DRIVE_REMOVABLE
:
125 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
126 imageId
= wxFileIconsTable::floppy
;
128 imageId
= wxFileIconsTable::removeable
;
131 imageId
= wxFileIconsTable::cdrom
;
136 imageId
= wxFileIconsTable::drive
;
142 icon_ids
.Add(imageId
);
144 while (driveBuffer
[i
] != wxT('\0'))
147 if (driveBuffer
[i
] == wxT('\0'))
150 #elif defined(__OS2__)
152 ULONG ulDriveNum
= 0;
153 ULONG ulDriveMap
= 0;
154 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
160 if (ulDriveMap
& ( 1 << i
))
163 path
.Printf(wxT("%c:\\"), 'A' + i
);
164 name
.Printf(wxT("%c:"), 'A' + i
);
166 // Note: If _filesys is unsupported by some compilers,
167 // we can always replace it by DosQueryFSAttach
168 char filesysname
[20];
169 _filesys(name
.fn_str(), filesysname
, sizeof(filesysname
));
170 /* FAT, LAN, HPFS, CDFS, NFS */
172 if (path
== wxT("A:\\") || path
== wxT("B:\\"))
173 imageId
= wxFileIconsTable::floppy
;
174 else if (!strcmp(filesysname
, "CDFS"))
175 imageId
= wxFileIconsTable::cdrom
;
176 else if (!strcmp(filesysname
, "LAN") ||
177 !strcmp(filesysname
, "NFS"))
178 imageId
= wxFileIconsTable::drive
;
180 imageId
= wxFileIconsTable::drive
;
183 icon_ids
.Add(imageId
);
188 #else // !__WIN32__, !__OS2__
191 /* If we can switch to the drive, it exists. */
192 for( drive
= 1; drive
<= 26; drive
++ )
195 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
196 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
198 if (wxIsDriveAvailable(path
))
202 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
205 #endif // __WIN32__/!__WIN32__
207 #elif defined(__WXMAC__)
209 ItemCount volumeIndex
= 1;
212 while( noErr
== err
)
214 HFSUniStr255 volumeName
;
216 FSVolumeInfo volumeInfo
;
217 err
= FSGetVolumeInfo(0, volumeIndex
, NULL
, kFSVolInfoFlags
, &volumeInfo
, &volumeName
, &fsRef
);
220 wxString path
= wxMacFSRefToPath( &fsRef
) ;
221 wxString name
= wxMacHFSUniStrToString( &volumeName
) ;
223 if ( (volumeInfo
.flags
& kFSVolFlagSoftwareLockedMask
) || (volumeInfo
.flags
& kFSVolFlagHardwareLockedMask
) )
225 icon_ids
.Add(wxFileIconsTable::cdrom
);
229 icon_ids
.Add(wxFileIconsTable::drive
);
231 // todo other removable
239 #elif defined(__UNIX__)
242 icon_ids
.Add(wxFileIconsTable::computer
);
244 #error "Unsupported platform in wxGenericDirCtrl!"
246 wxASSERT_MSG( (paths
.GetCount() == names
.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
247 wxASSERT_MSG( (paths
.GetCount() == icon_ids
.GetCount()), wxT("Wrong number of icons for available drives."));
248 return paths
.GetCount();
251 // ----------------------------------------------------------------------------
252 // wxIsDriveAvailable
253 // ----------------------------------------------------------------------------
257 bool wxIsDriveAvailable(const wxString
& dirName
)
259 // FIXME_MGL - this method leads to hang up under Watcom for some reason
261 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
263 wxString
dirNameLower(dirName
.Lower());
264 // VS: always return true for removable media, since Win95 doesn't
265 // like it when MS-DOS app accesses empty floppy drive
266 return (dirNameLower
[0u] == wxT('a') ||
267 dirNameLower
[0u] == wxT('b') ||
268 wxDirExists(dirNameLower
));
275 #elif defined(__WINDOWS__) || defined(__OS2__)
277 int setdrive(int WXUNUSED_IN_WINCE(drive
))
281 #elif defined(__GNUWIN32__) && \
282 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
283 return _chdrive(drive
);
287 if (drive
< 1 || drive
> 31)
289 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
290 newdrive
[1] = wxT(':');
292 newdrive
[2] = wxT('\\');
293 newdrive
[3] = wxT('\0');
295 newdrive
[2] = wxT('\0');
297 #if defined(__WXMSW__)
298 if (::SetCurrentDirectory(newdrive
))
300 // VA doesn't know what LPSTR is and has its own set
301 if (!DosSetCurrentDir((PSZ
)newdrive
))
309 bool wxIsDriveAvailable(const wxString
& WXUNUSED_IN_WINCE(dirName
))
315 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
319 // Check if this is a root directory and if so,
320 // whether the drive is available.
321 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
323 wxString
dirNameLower(dirName
.Lower());
324 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
325 success
= wxDirExists(dirNameLower
);
328 // Avoid changing to drive since no media may be inserted.
329 if (dirNameLower
[(size_t)0] == 'a' || dirNameLower
[(size_t)0] == 'b')
332 int currentDrive
= _getdrive();
333 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
334 int err
= setdrive( thisDrive
) ;
335 setdrive( currentDrive
);
344 (void) SetErrorMode(errorMode
);
350 #endif // __WINDOWS__ || __OS2__
352 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
358 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
359 // and sort regardless of case.
360 static int wxCMPFUNC_CONV
wxDirCtrlStringCompareFunction(const wxString
& strFirst
, const wxString
& strSecond
)
362 return strFirst
.CmpNoCase(strSecond
);
365 //-----------------------------------------------------------------------------
367 //-----------------------------------------------------------------------------
369 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
374 /* Insert logic to detect hidden files here
375 * In UnixLand we just check whether the first char is a dot
376 * For FileNameFromPath read LastDirNameInThisPath ;-) */
377 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
379 m_isExpanded
= false;
383 void wxDirItemData::SetNewDirName(const wxString
& path
)
386 m_name
= wxFileNameFromPath(path
);
389 bool wxDirItemData::HasSubDirs() const
397 if ( !dir
.Open(m_path
) )
401 return dir
.HasSubDirs();
404 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
412 if ( !dir
.Open(m_path
) )
416 return dir
.HasFiles();
419 //-----------------------------------------------------------------------------
421 //-----------------------------------------------------------------------------
424 #if wxUSE_EXTENDED_RTTI
425 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle
)
427 wxBEGIN_FLAGS( wxGenericDirCtrlStyle
)
428 // new style border flags, we put them first to
429 // use them for streaming out
430 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
431 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
432 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
433 wxFLAGS_MEMBER(wxBORDER_RAISED
)
434 wxFLAGS_MEMBER(wxBORDER_STATIC
)
435 wxFLAGS_MEMBER(wxBORDER_NONE
)
437 // old style border flags
438 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
439 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
440 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
441 wxFLAGS_MEMBER(wxRAISED_BORDER
)
442 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
443 wxFLAGS_MEMBER(wxBORDER
)
445 // standard window styles
446 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
447 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
448 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
449 wxFLAGS_MEMBER(wxWANTS_CHARS
)
450 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
451 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
452 wxFLAGS_MEMBER(wxVSCROLL
)
453 wxFLAGS_MEMBER(wxHSCROLL
)
455 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY
)
456 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL
)
457 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST
)
458 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS
)
460 wxEND_FLAGS( wxGenericDirCtrlStyle
)
462 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl
, wxControl
,"wx/dirctrl.h")
464 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl
)
465 wxHIDE_PROPERTY( Children
)
466 wxPROPERTY( DefaultPath
, wxString
, SetDefaultPath
, GetDefaultPath
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
467 wxPROPERTY( Filter
, wxString
, SetFilter
, GetFilter
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
468 wxPROPERTY( DefaultFilter
, int , SetFilterIndex
, GetFilterIndex
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
469 wxPROPERTY_FLAGS( WindowStyle
, wxGenericDirCtrlStyle
, long, SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0, wxT("Helpstring"), wxT("group") )
470 wxEND_PROPERTIES_TABLE()
472 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl
)
473 wxEND_HANDLERS_TABLE()
475 wxCONSTRUCTOR_8( wxGenericDirCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, DefaultPath
,
476 wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
, wxString
, Filter
, int , DefaultFilter
)
478 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
481 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
482 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL
, wxGenericDirCtrl::OnExpandItem
)
483 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL
, wxGenericDirCtrl::OnCollapseItem
)
484 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnBeginEditItem
)
485 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnEndEditItem
)
486 EVT_SIZE (wxGenericDirCtrl::OnSize
)
489 wxGenericDirCtrl::wxGenericDirCtrl(void)
494 void wxGenericDirCtrl::ExpandRoot()
496 ExpandDir(m_rootId
); // automatically expand first level
498 // Expand and select the default path
499 if (!m_defaultPath
.empty())
501 ExpandPath(m_defaultPath
);
506 // On Unix, there's only one node under the (hidden) root node. It
507 // represents the / path, so the user would always have to expand it;
508 // let's do it ourselves
509 ExpandPath( wxT("/") );
514 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
520 const wxString
& filter
,
522 const wxString
& name
)
524 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
527 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
531 long treeStyle
= wxTR_HAS_BUTTONS
;
533 // On Windows CE, if you hide the root, you get a crash when
534 // attempting to access data for children of the root item.
536 treeStyle
|= wxTR_HIDE_ROOT
;
540 treeStyle
|= wxTR_NO_LINES
;
543 if (style
& wxDIRCTRL_EDIT_LABELS
)
544 treeStyle
|= wxTR_EDIT_LABELS
;
546 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
547 treeStyle
|= wxNO_BORDER
;
549 treeStyle
|= wxBORDER_SUNKEN
;
551 long filterStyle
= 0;
552 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
553 filterStyle
|= wxNO_BORDER
;
555 filterStyle
|= wxBORDER_SUNKEN
;
557 m_treeCtrl
= CreateTreeCtrl(this, wxID_TREECTRL
,
558 wxPoint(0,0), GetClientSize(), treeStyle
);
560 if (!filter
.empty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
561 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
566 if (m_filter
.empty())
570 m_filter
= wxT("*.*");
573 SetFilterIndex(defaultFilter
);
575 if (m_filterListCtrl
)
576 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
578 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
580 m_showHidden
= false;
581 wxDirItemData
* rootData
= new wxDirItemData(wxEmptyString
, wxEmptyString
, true);
585 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
586 rootName
= _("Computer");
588 rootName
= _("Sections");
591 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
592 m_treeCtrl
->SetItemHasChildren(m_rootId
);
602 wxGenericDirCtrl::~wxGenericDirCtrl()
606 void wxGenericDirCtrl::Init()
608 m_showHidden
= false;
610 m_currentFilterStr
= wxEmptyString
; // Default: any file
612 m_filterListCtrl
= NULL
;
615 wxTreeCtrl
* wxGenericDirCtrl::CreateTreeCtrl(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long treeStyle
)
617 return new wxTreeCtrl(parent
, id
, pos
, size
, treeStyle
);
620 void wxGenericDirCtrl::ShowHidden( bool show
)
624 wxString path
= GetPath();
630 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
632 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,true);
634 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
636 m_treeCtrl
->SetItemHasChildren(id
);
641 void wxGenericDirCtrl::SetupSections()
643 wxArrayString paths
, names
;
646 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
649 wxString home
= wxGetHomeDir();
650 AddSection( home
, _("Home directory"), 1);
651 home
+= wxT("/Desktop");
652 AddSection( home
, _("Desktop"), 1);
655 for (n
= 0; n
< count
; n
++)
656 AddSection(paths
[n
], names
[n
], icons
[n
]);
659 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
661 // don't rename the main entry "Sections"
662 if (event
.GetItem() == m_rootId
)
668 // don't rename the individual sections
669 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
676 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
678 if ((event
.GetLabel().empty()) ||
679 (event
.GetLabel() == _(".")) ||
680 (event
.GetLabel() == _("..")) ||
681 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
682 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
683 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
685 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
691 wxTreeItemId id
= event
.GetItem();
692 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
695 wxString
new_name( wxPathOnly( data
->m_path
) );
696 new_name
+= wxString(wxFILE_SEP_PATH
);
697 new_name
+= event
.GetLabel();
701 if (wxFileExists(new_name
))
703 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
708 if (wxRenameFile(data
->m_path
,new_name
))
710 data
->SetNewDirName( new_name
);
714 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
720 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
722 wxTreeItemId parentId
= event
.GetItem();
724 // VS: this is needed because the event handler is called from wxTreeCtrl
725 // ctor when wxTR_HIDE_ROOT was specified
727 if (!m_rootId
.IsOk())
729 m_rootId
= m_treeCtrl
->GetRootItem();
734 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
736 CollapseDir(event
.GetItem());
739 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
743 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
744 if (!data
->m_isExpanded
)
747 data
->m_isExpanded
= false;
748 wxTreeItemIdValue cookie
;
749 /* Workaround because DeleteChildren has disapeared (why?) and
750 * CollapseAndReset doesn't work as advertised (deletes parent too) */
751 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
754 m_treeCtrl
->Delete(child
);
755 /* Not GetNextChild below, because the cookie mechanism can't
756 * handle disappearing children! */
757 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
759 if (parentId
!= m_treeCtrl
->GetRootItem())
760 m_treeCtrl
->Collapse(parentId
);
763 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
765 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
767 if (data
->m_isExpanded
)
770 data
->m_isExpanded
= true;
772 if (parentId
== m_treeCtrl
->GetRootItem())
780 wxString search
,path
,filename
;
782 wxString
dirName(data
->m_path
);
784 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
785 // Check if this is a root directory and if so,
786 // whether the drive is avaiable.
787 if (!wxIsDriveAvailable(dirName
))
789 data
->m_isExpanded
= false;
790 //wxMessageBox(wxT("Sorry, this drive is not available."));
795 // This may take a longish time. Go to busy cursor
798 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
799 if (dirName
.Last() == ':')
800 dirName
+= wxString(wxFILE_SEP_PATH
);
804 wxArrayString filenames
;
807 wxString eachFilename
;
814 int style
= wxDIR_DIRS
;
815 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
816 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
820 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
822 dirs
.Add(eachFilename
);
825 while (d
.GetNext(&eachFilename
));
828 dirs
.Sort(wxDirCtrlStringCompareFunction
);
830 // Now do the filenames -- but only if we're allowed to
831 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
837 int style
= wxDIR_FILES
;
838 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
839 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
840 wxStringTokenizer strTok
;
842 strTok
.SetString(m_currentFilterStr
,wxT(";"));
843 while(strTok
.HasMoreTokens())
845 curFilter
= strTok
.GetNextToken();
846 if (d
.GetFirst(& eachFilename
, curFilter
, style
))
850 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
852 filenames
.Add(eachFilename
);
855 while (d
.GetNext(& eachFilename
));
859 filenames
.Sort(wxDirCtrlStringCompareFunction
);
862 // Add the sorted dirs
864 for (i
= 0; i
< dirs
.Count(); i
++)
866 eachFilename
= dirs
[i
];
868 if (!wxEndsWithPathSeparator(path
))
869 path
+= wxString(wxFILE_SEP_PATH
);
870 path
+= eachFilename
;
872 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,true);
873 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
874 wxFileIconsTable::folder
, -1, dir_item
);
875 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
876 wxTreeItemIcon_Expanded
);
878 // Has this got any children? If so, make it expandable.
879 // (There are two situations when a dir has children: either it
880 // has subdirectories or it contains files that weren't filtered
881 // out. The latter only applies to dirctrl with files.)
882 if ( dir_item
->HasSubDirs() ||
883 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
884 dir_item
->HasFiles(m_currentFilterStr
)) )
886 m_treeCtrl
->SetItemHasChildren(id
);
890 // Add the sorted filenames
891 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
893 for (i
= 0; i
< filenames
.Count(); i
++)
895 eachFilename
= filenames
[i
];
897 if (!wxEndsWithPathSeparator(path
))
898 path
+= wxString(wxFILE_SEP_PATH
);
899 path
+= eachFilename
;
900 //path = dirName + wxString(wxT("/")) + eachFilename;
901 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,false);
902 int image_id
= wxFileIconsTable::file
;
903 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
904 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
905 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
910 void wxGenericDirCtrl::ReCreateTree()
912 CollapseDir(m_treeCtrl
->GetRootItem());
916 void wxGenericDirCtrl::CollapseTree()
918 wxTreeItemIdValue cookie
;
919 wxTreeItemId child
= m_treeCtrl
->GetFirstChild(m_rootId
, cookie
);
923 child
= m_treeCtrl
->GetNextChild(m_rootId
, cookie
);
927 // Find the child that matches the first part of 'path'.
928 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
929 // then the child for /usr is returned.
930 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
932 wxString
path2(path
);
934 // Make sure all separators are as per the current platform
935 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
936 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
938 // Append a separator to foil bogus substring matching
939 path2
+= wxString(wxFILE_SEP_PATH
);
941 // In MSW or PM, case is not significant
942 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
946 wxTreeItemIdValue cookie
;
947 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
948 while (childId
.IsOk())
950 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
952 if (data
&& !data
->m_path
.empty())
954 wxString
childPath(data
->m_path
);
955 if (!wxEndsWithPathSeparator(childPath
))
956 childPath
+= wxString(wxFILE_SEP_PATH
);
958 // In MSW and PM, case is not significant
959 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
960 childPath
.MakeLower();
963 if (childPath
.Len() <= path2
.Len())
965 wxString path3
= path2
.Mid(0, childPath
.Len());
966 if (childPath
== path3
)
968 if (path3
.Len() == path2
.Len())
977 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
979 wxTreeItemId invalid
;
983 // Try to expand as much of the given path as possible,
984 // and select the given tree item.
985 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
988 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
989 wxTreeItemId lastId
= id
; // The last non-zero id
990 while (id
.IsOk() && !done
)
994 id
= FindChild(id
, path
, done
);
1001 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1004 m_treeCtrl
->Expand(lastId
);
1006 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1008 // Find the first file in this directory
1009 wxTreeItemIdValue cookie
;
1010 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1011 bool selectedChild
= false;
1012 while (childId
.IsOk())
1014 data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1016 if (data
&& data
->m_path
!= wxEmptyString
&& !data
->m_isDir
)
1018 m_treeCtrl
->SelectItem(childId
);
1019 m_treeCtrl
->EnsureVisible(childId
);
1020 selectedChild
= true;
1023 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1027 m_treeCtrl
->SelectItem(lastId
);
1028 m_treeCtrl
->EnsureVisible(lastId
);
1033 m_treeCtrl
->SelectItem(lastId
);
1034 m_treeCtrl
->EnsureVisible(lastId
);
1040 wxString
wxGenericDirCtrl::GetPath() const
1042 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1045 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1046 return data
->m_path
;
1049 return wxEmptyString
;
1052 wxString
wxGenericDirCtrl::GetFilePath() const
1054 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1057 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1059 return wxEmptyString
;
1061 return data
->m_path
;
1064 return wxEmptyString
;
1067 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1069 m_defaultPath
= path
;
1076 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1078 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1080 // This may take a longish time. Go to busy cursor
1085 wxString search
,path
,filename
;
1087 wxString
dirName(data
->m_path
);
1089 #if defined(__WXMSW__) || defined(__OS2__)
1090 if (dirName
.Last() == ':')
1091 dirName
+= wxString(wxFILE_SEP_PATH
);
1095 wxString eachFilename
;
1102 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1106 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1108 filenames
.Add(eachFilename
);
1111 while (d
.GetNext(& eachFilename
)) ;
1117 void wxGenericDirCtrl::SetFilterIndex(int n
)
1119 m_currentFilter
= n
;
1122 if (ExtractWildcard(m_filter
, n
, f
, d
))
1123 m_currentFilterStr
= f
;
1126 m_currentFilterStr
= wxT("*");
1128 m_currentFilterStr
= wxT("*.*");
1132 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1137 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1138 m_currentFilterStr
= f
;
1141 m_currentFilterStr
= wxT("*");
1143 m_currentFilterStr
= wxT("*.*");
1147 // Extract description and actual filter from overall filter string
1148 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1150 wxArrayString filters
, descriptions
;
1151 int count
= wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1152 if (count
> 0 && n
< count
)
1154 filter
= filters
[n
];
1155 description
= descriptions
[n
];
1162 #if WXWIN_COMPATIBILITY_2_4
1163 // Parses the global filter, returning the number of filters.
1164 // Returns 0 if none or if there's a problem.
1165 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1166 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1168 return wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1170 #endif // WXWIN_COMPATIBILITY_2_4
1172 void wxGenericDirCtrl::DoResize()
1174 wxSize sz
= GetClientSize();
1175 int verticalSpacing
= 3;
1179 if (m_filterListCtrl
)
1182 // For some reason, this is required in order for the
1183 // correct control height to always be returned, rather
1184 // than the drop-down list height which is sometimes returned.
1185 wxSize oldSize
= m_filterListCtrl
->GetSize();
1186 m_filterListCtrl
->SetSize(wxDefaultCoord
,
1190 wxSIZE_USE_EXISTING
);
1191 m_filterListCtrl
->SetSize(wxDefaultCoord
,
1195 wxSIZE_USE_EXISTING
);
1197 filterSz
= m_filterListCtrl
->GetSize();
1198 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1200 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1201 if (m_filterListCtrl
)
1203 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1204 // Don't know why, but this needs refreshing after a resize (wxMSW)
1205 m_filterListCtrl
->Refresh();
1211 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1216 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1217 const wxString
& text
,
1218 int image
, int selectedImage
,
1219 wxTreeItemData
* data
)
1221 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1223 wxASSERT (treeCtrl
);
1227 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1231 return wxTreeItemId();
1236 //-----------------------------------------------------------------------------
1237 // wxDirFilterListCtrl
1238 //-----------------------------------------------------------------------------
1240 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1242 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1243 EVT_CHOICE(wxID_ANY
, wxDirFilterListCtrl::OnSelFilter
)
1246 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1252 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1255 void wxDirFilterListCtrl::Init()
1260 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1262 int sel
= GetSelection();
1264 wxString currentPath
= m_dirCtrl
->GetPath();
1266 m_dirCtrl
->SetFilterIndex(sel
);
1268 // If the filter has changed, the view is out of date, so
1269 // collapse the tree.
1270 m_dirCtrl
->ReCreateTree();
1272 // Try to restore the selection, or at least the directory
1273 m_dirCtrl
->ExpandPath(currentPath
);
1276 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1279 wxArrayString descriptions
, filters
;
1280 size_t n
= (size_t) wxParseCommonDialogsFilter(filter
, descriptions
, filters
);
1282 if (n
> 0 && defaultFilter
< (int) n
)
1284 for (size_t i
= 0; i
< n
; i
++)
1285 Append(descriptions
[i
]);
1286 SetSelection(defaultFilter
);
1289 #endif // wxUSE_DIRDLG
1291 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1293 // ----------------------------------------------------------------------------
1294 // wxFileIconsTable icons
1295 // ----------------------------------------------------------------------------
1298 /* Computer (c) Julian Smart */
1299 static const char * file_icons_tbl_computer_xpm
[] = {
1300 /* columns rows colors chars-per-pixel */
1362 #endif // GTK+ < 2.4
1364 // ----------------------------------------------------------------------------
1365 // wxFileIconsTable & friends
1366 // ----------------------------------------------------------------------------
1368 // global instance of a wxFileIconsTable
1369 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1371 // A module to allow icons table cleanup
1373 class wxFileIconsTableModule
: public wxModule
1375 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1377 wxFileIconsTableModule() {}
1378 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return true; }
1381 if (wxTheFileIconsTable
)
1383 delete wxTheFileIconsTable
;
1384 wxTheFileIconsTable
= NULL
;
1389 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1391 class wxFileIconEntry
: public wxObject
1394 wxFileIconEntry(int i
) { id
= i
; }
1399 wxFileIconsTable::wxFileIconsTable()
1402 m_smallImageList
= NULL
;
1405 wxFileIconsTable::~wxFileIconsTable()
1409 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1412 if (m_smallImageList
) delete m_smallImageList
;
1415 // delayed initialization - wait until first use (wxArtProv not created yet)
1416 void wxFileIconsTable::Create()
1418 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1419 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1420 m_smallImageList
= new wxImageList(16, 16);
1423 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
,
1427 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN
,
1432 // GTK24 uses this icon in the file open dialog
1433 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1437 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1440 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1444 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_CDROM
,
1448 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FLOPPY
,
1452 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE
,
1456 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
,
1460 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1462 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
,
1465 delete m_HashTable
->Get(_T("exe"));
1466 m_HashTable
->Delete(_T("exe"));
1467 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1469 /* else put into list by GetIconID
1470 (KDE defines application/x-executable for *.exe and has nice icon)
1474 wxImageList
*wxFileIconsTable::GetSmallImageList()
1476 if (!m_smallImageList
)
1479 return m_smallImageList
;
1482 #if wxUSE_MIMETYPE && wxUSE_IMAGE
1483 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1484 // one icon and we won't resize it
1486 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1488 const unsigned int size
= 16;
1490 wxImage
smallimg (size
, size
);
1491 unsigned char *p1
, *p2
, *ps
;
1492 unsigned char mr
= img
.GetMaskRed(),
1493 mg
= img
.GetMaskGreen(),
1494 mb
= img
.GetMaskBlue();
1497 unsigned sr
, sg
, sb
, smask
;
1499 p1
= img
.GetData(), p2
= img
.GetData() + 3 * size
*2, ps
= smallimg
.GetData();
1500 smallimg
.SetMaskColour(mr
, mr
, mr
);
1502 for (y
= 0; y
< size
; y
++)
1504 for (x
= 0; x
< size
; x
++)
1506 sr
= sg
= sb
= smask
= 0;
1507 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1508 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1511 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1512 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1515 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1516 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1519 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1520 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1525 ps
[0] = ps
[1] = ps
[2] = mr
;
1528 ps
[0] = (unsigned char)(sr
>> 2);
1529 ps
[1] = (unsigned char)(sg
>> 2);
1530 ps
[2] = (unsigned char)(sb
>> 2);
1534 p1
+= size
*2 * 3, p2
+= size
*2 * 3;
1537 return wxBitmap(smallimg
);
1540 // This function is currently not unused anymore
1542 // finds empty borders and return non-empty area of image:
1543 static wxImage
CutEmptyBorders(const wxImage
& img
)
1545 unsigned char mr
= img
.GetMaskRed(),
1546 mg
= img
.GetMaskGreen(),
1547 mb
= img
.GetMaskBlue();
1548 unsigned char *dt
= img
.GetData(), *dttmp
;
1549 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1551 unsigned top
, bottom
, left
, right
, i
;
1554 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1555 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1557 for (empt
= true, top
= 0; empt
&& top
< h
; top
++)
1560 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1563 for (empt
= true, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1565 MK_DTTMP(0, bottom
);
1566 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1569 for (empt
= true, left
= 0; empt
&& left
< w
; left
++)
1572 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1575 for (empt
= true, right
= w
-1; empt
&& right
> left
; right
--)
1578 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1581 top
--, left
--, bottom
++, right
++;
1583 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1587 #endif // wxUSE_MIMETYPE
1589 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1591 if (!m_smallImageList
)
1595 if (!extension
.empty())
1597 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1598 if (entry
) return (entry
-> id
);
1601 wxFileType
*ft
= (mime
.empty()) ?
1602 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1603 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1605 wxIconLocation iconLoc
;
1610 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1612 ic
= wxIcon( iconLoc
);
1621 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1626 bmp
.CopyFromIcon(ic
);
1631 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1635 const unsigned int size
= 16;
1637 int id
= m_smallImageList
->GetImageCount();
1638 if ((bmp
.GetWidth() == (int) size
) && (bmp
.GetHeight() == (int) size
))
1640 m_smallImageList
->Add(bmp
);
1645 wxImage img
= bmp
.ConvertToImage();
1647 if ((img
.GetWidth() != size
*2) || (img
.GetHeight() != size
*2))
1648 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1649 m_smallImageList
->Add(CreateAntialiasedBitmap(img
.Rescale(size
*2, size
*2)));
1651 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1653 #endif // wxUSE_IMAGE
1655 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1658 #else // !wxUSE_MIMETYPE
1661 if (extension
== wxT("exe"))
1665 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1668 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG