1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/dirctrlg.cpp
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"
28 #include "wx/button.h"
30 #include "wx/settings.h"
31 #include "wx/msgdlg.h"
32 #include "wx/cmndata.h"
33 #include "wx/choice.h"
34 #include "wx/textctrl.h"
35 #include "wx/layout.h"
37 #include "wx/textdlg.h"
38 #include "wx/gdicmn.h"
40 #include "wx/module.h"
43 #include "wx/filefn.h"
44 #include "wx/imaglist.h"
45 #include "wx/tokenzr.h"
47 #include "wx/artprov.h"
48 #include "wx/mimetype.h"
51 #include "wx/statline.h"
54 #if defined(__WXMAC__)
55 #include "wx/mac/private.h" // includes mac headers
60 #include "wx/msw/winundef.h"
62 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
63 // older releases don't, but it should be verified and the checks modified
65 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
66 #if !defined(__WXWINCE__)
75 #if defined(__OS2__) || defined(__DOS__)
85 extern bool wxIsDriveAvailable(const wxString
& dirName
);
88 #if defined(__WXMAC__)
89 #include "MoreFilesX.h"
96 // If compiled under Windows, this macro can cause problems
101 // ----------------------------------------------------------------------------
102 // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
103 // ----------------------------------------------------------------------------
105 size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
)
107 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
110 // No logical drives; return "\"
111 paths
.Add(wxT("\\"));
112 names
.Add(wxT("\\"));
113 icon_ids
.Add(wxFileIconsTable::computer
);
114 #elif defined(__WIN32__)
115 wxChar driveBuffer
[256];
116 size_t n
= (size_t) GetLogicalDriveStrings(255, driveBuffer
);
121 path
.Printf(wxT("%c:\\"), driveBuffer
[i
]);
122 name
.Printf(wxT("%c:"), driveBuffer
[i
]);
124 // Do not use GetVolumeInformation to further decorate the
125 // name, since it can cause severe delays on network drives.
128 int driveType
= ::GetDriveType(path
);
131 case DRIVE_REMOVABLE
:
132 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
133 imageId
= wxFileIconsTable::floppy
;
135 imageId
= wxFileIconsTable::removeable
;
138 imageId
= wxFileIconsTable::cdrom
;
143 imageId
= wxFileIconsTable::drive
;
149 icon_ids
.Add(imageId
);
151 while (driveBuffer
[i
] != wxT('\0'))
154 if (driveBuffer
[i
] == wxT('\0'))
157 #elif defined(__OS2__)
159 ULONG ulDriveNum
= 0;
160 ULONG ulDriveMap
= 0;
161 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
167 if (ulDriveMap
& ( 1 << i
))
170 path
.Printf(wxT("%c:\\"), 'A' + i
);
171 name
.Printf(wxT("%c:"), 'A' + i
);
173 // Note: If _filesys is unsupported by some compilers,
174 // we can always replace it by DosQueryFSAttach
175 char filesysname
[20];
177 ULONG cbBuffer
= sizeof(filesysname
);
178 PFSQBUFFER2 pfsqBuffer
= (PFSQBUFFER2
)filesysname
;
179 APIRET rc
= ::DosQueryFSAttach(name
.fn_str(),0,FSAIL_QUERYNAME
,pfsqBuffer
,&cbBuffer
);
182 filesysname
[0] = '\0';
185 _filesys(name
.fn_str(), filesysname
, sizeof(filesysname
));
187 /* FAT, LAN, HPFS, CDFS, NFS */
189 if (path
== wxT("A:\\") || path
== wxT("B:\\"))
190 imageId
= wxFileIconsTable::floppy
;
191 else if (!strcmp(filesysname
, "CDFS"))
192 imageId
= wxFileIconsTable::cdrom
;
193 else if (!strcmp(filesysname
, "LAN") ||
194 !strcmp(filesysname
, "NFS"))
195 imageId
= wxFileIconsTable::drive
;
197 imageId
= wxFileIconsTable::drive
;
200 icon_ids
.Add(imageId
);
205 #else // !__WIN32__, !__OS2__
208 /* If we can switch to the drive, it exists. */
209 for( drive
= 1; drive
<= 26; drive
++ )
212 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
213 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
215 if (wxIsDriveAvailable(path
))
219 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
222 #endif // __WIN32__/!__WIN32__
224 #elif defined(__WXMAC__)
226 ItemCount volumeIndex
= 1;
229 while( noErr
== err
)
231 HFSUniStr255 volumeName
;
233 FSVolumeInfo volumeInfo
;
234 err
= FSGetVolumeInfo(0, volumeIndex
, NULL
, kFSVolInfoFlags
, &volumeInfo
, &volumeName
, &fsRef
);
237 wxString path
= wxMacFSRefToPath( &fsRef
) ;
238 wxString name
= wxMacHFSUniStrToString( &volumeName
) ;
240 if ( (volumeInfo
.flags
& kFSVolFlagSoftwareLockedMask
) || (volumeInfo
.flags
& kFSVolFlagHardwareLockedMask
) )
242 icon_ids
.Add(wxFileIconsTable::cdrom
);
246 icon_ids
.Add(wxFileIconsTable::drive
);
248 // todo other removable
256 #elif defined(__UNIX__)
259 icon_ids
.Add(wxFileIconsTable::computer
);
261 #error "Unsupported platform in wxGenericDirCtrl!"
263 wxASSERT_MSG( (paths
.GetCount() == names
.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
264 wxASSERT_MSG( (paths
.GetCount() == icon_ids
.GetCount()), wxT("Wrong number of icons for available drives."));
265 return paths
.GetCount();
268 // ----------------------------------------------------------------------------
269 // wxIsDriveAvailable
270 // ----------------------------------------------------------------------------
274 bool wxIsDriveAvailable(const wxString
& dirName
)
276 // FIXME_MGL - this method leads to hang up under Watcom for some reason
278 wxUnusedVar(dirName
);
280 if ( dirName
.length() == 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 wxDirExists(dirNameLower
));
294 #elif defined(__WINDOWS__) || defined(__OS2__)
296 int setdrive(int WXUNUSED_IN_WINCE(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(':');
311 newdrive
[2] = wxT('\\');
312 newdrive
[3] = wxT('\0');
314 newdrive
[2] = wxT('\0');
316 #if defined(__WXMSW__)
317 if (::SetCurrentDirectory(newdrive
))
319 // VA doesn't know what LPSTR is and has its own set
320 if (!DosSetCurrentDir((PSZ
)newdrive
))
328 bool wxIsDriveAvailable(const wxString
& WXUNUSED_IN_WINCE(dirName
))
334 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
338 // Check if this is a root directory and if so,
339 // whether the drive is available.
340 if (dirName
.length() == 3 && dirName
[(size_t)1] == wxT(':'))
342 wxString
dirNameLower(dirName
.Lower());
343 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
344 success
= wxDirExists(dirNameLower
);
347 // Avoid changing to drive since no media may be inserted.
348 if (dirNameLower
[(size_t)0] == 'a' || dirNameLower
[(size_t)0] == 'b')
351 int currentDrive
= _getdrive();
352 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
353 int err
= setdrive( thisDrive
) ;
354 setdrive( currentDrive
);
363 (void) SetErrorMode(errorMode
);
369 #endif // __WINDOWS__ || __OS2__
371 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
377 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
378 // and sort regardless of case.
379 static int wxCMPFUNC_CONV
wxDirCtrlStringCompareFunction(const wxString
& strFirst
, const wxString
& strSecond
)
381 return strFirst
.CmpNoCase(strSecond
);
384 //-----------------------------------------------------------------------------
386 //-----------------------------------------------------------------------------
388 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
393 /* Insert logic to detect hidden files here
394 * In UnixLand we just check whether the first char is a dot
395 * For FileNameFromPath read LastDirNameInThisPath ;-) */
396 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
398 m_isExpanded
= false;
402 void wxDirItemData::SetNewDirName(const wxString
& path
)
405 m_name
= wxFileNameFromPath(path
);
408 bool wxDirItemData::HasSubDirs() const
416 if ( !dir
.Open(m_path
) )
420 return dir
.HasSubDirs();
423 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
431 if ( !dir
.Open(m_path
) )
435 return dir
.HasFiles();
438 //-----------------------------------------------------------------------------
440 //-----------------------------------------------------------------------------
443 #if wxUSE_EXTENDED_RTTI
444 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle
)
446 wxBEGIN_FLAGS( wxGenericDirCtrlStyle
)
447 // new style border flags, we put them first to
448 // use them for streaming out
449 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
450 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
451 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
452 wxFLAGS_MEMBER(wxBORDER_RAISED
)
453 wxFLAGS_MEMBER(wxBORDER_STATIC
)
454 wxFLAGS_MEMBER(wxBORDER_NONE
)
456 // old style border flags
457 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
458 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
459 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
460 wxFLAGS_MEMBER(wxRAISED_BORDER
)
461 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
462 wxFLAGS_MEMBER(wxBORDER
)
464 // standard window styles
465 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
466 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
467 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
468 wxFLAGS_MEMBER(wxWANTS_CHARS
)
469 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
470 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
471 wxFLAGS_MEMBER(wxVSCROLL
)
472 wxFLAGS_MEMBER(wxHSCROLL
)
474 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY
)
475 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL
)
476 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST
)
477 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS
)
479 wxEND_FLAGS( wxGenericDirCtrlStyle
)
481 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl
, wxControl
,"wx/dirctrl.h")
483 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl
)
484 wxHIDE_PROPERTY( Children
)
485 wxPROPERTY( DefaultPath
, wxString
, SetDefaultPath
, GetDefaultPath
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
486 wxPROPERTY( Filter
, wxString
, SetFilter
, GetFilter
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
487 wxPROPERTY( DefaultFilter
, int , SetFilterIndex
, GetFilterIndex
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
488 wxPROPERTY_FLAGS( WindowStyle
, wxGenericDirCtrlStyle
, long, SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0, wxT("Helpstring"), wxT("group") )
489 wxEND_PROPERTIES_TABLE()
491 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl
)
492 wxEND_HANDLERS_TABLE()
494 wxCONSTRUCTOR_8( wxGenericDirCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, DefaultPath
,
495 wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
, wxString
, Filter
, int , DefaultFilter
)
497 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
500 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
501 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL
, wxGenericDirCtrl::OnExpandItem
)
502 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL
, wxGenericDirCtrl::OnCollapseItem
)
503 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnBeginEditItem
)
504 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnEndEditItem
)
505 EVT_SIZE (wxGenericDirCtrl::OnSize
)
508 wxGenericDirCtrl::wxGenericDirCtrl(void)
513 void wxGenericDirCtrl::ExpandRoot()
515 ExpandDir(m_rootId
); // automatically expand first level
517 // Expand and select the default path
518 if (!m_defaultPath
.empty())
520 ExpandPath(m_defaultPath
);
525 // On Unix, there's only one node under the (hidden) root node. It
526 // represents the / path, so the user would always have to expand it;
527 // let's do it ourselves
528 ExpandPath( wxT("/") );
533 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
539 const wxString
& filter
,
541 const wxString
& name
)
543 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
546 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
547 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT
));
551 long treeStyle
= wxTR_HAS_BUTTONS
;
553 // On Windows CE, if you hide the root, you get a crash when
554 // attempting to access data for children of the root item.
556 treeStyle
|= wxTR_HIDE_ROOT
;
560 treeStyle
|= wxTR_NO_LINES
;
563 if (style
& wxDIRCTRL_EDIT_LABELS
)
564 treeStyle
|= wxTR_EDIT_LABELS
;
566 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
567 treeStyle
|= wxNO_BORDER
;
569 treeStyle
|= wxBORDER_SUNKEN
;
571 long filterStyle
= 0;
572 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
573 filterStyle
|= wxNO_BORDER
;
575 filterStyle
|= wxBORDER_SUNKEN
;
577 m_treeCtrl
= CreateTreeCtrl(this, wxID_TREECTRL
,
578 wxPoint(0,0), GetClientSize(), treeStyle
);
580 if (!filter
.empty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
581 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
586 if (m_filter
.empty())
590 m_filter
= wxT("*.*");
593 SetFilterIndex(defaultFilter
);
595 if (m_filterListCtrl
)
596 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
598 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
600 m_showHidden
= false;
601 wxDirItemData
* rootData
= new wxDirItemData(wxEmptyString
, wxEmptyString
, true);
605 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
606 rootName
= _("Computer");
608 rootName
= _("Sections");
611 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
612 m_treeCtrl
->SetItemHasChildren(m_rootId
);
616 SetInitialSize(size
);
622 wxGenericDirCtrl::~wxGenericDirCtrl()
626 void wxGenericDirCtrl::Init()
628 m_showHidden
= false;
630 m_currentFilterStr
= wxEmptyString
; // Default: any file
632 m_filterListCtrl
= NULL
;
635 wxTreeCtrl
* wxGenericDirCtrl::CreateTreeCtrl(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long treeStyle
)
637 return new wxTreeCtrl(parent
, id
, pos
, size
, treeStyle
);
640 void wxGenericDirCtrl::ShowHidden( bool show
)
644 wxString path
= GetPath();
650 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
652 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,true);
654 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
656 m_treeCtrl
->SetItemHasChildren(id
);
661 void wxGenericDirCtrl::SetupSections()
663 wxArrayString paths
, names
;
666 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
669 wxString home
= wxGetHomeDir();
670 AddSection( home
, _("Home directory"), 1);
671 home
+= wxT("/Desktop");
672 AddSection( home
, _("Desktop"), 1);
675 for (n
= 0; n
< count
; n
++)
676 AddSection(paths
[n
], names
[n
], icons
[n
]);
679 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
681 // don't rename the main entry "Sections"
682 if (event
.GetItem() == m_rootId
)
688 // don't rename the individual sections
689 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
696 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
698 if (event
.IsEditCancelled())
701 if ((event
.GetLabel().empty()) ||
702 (event
.GetLabel() == wxT(".")) ||
703 (event
.GetLabel() == wxT("..")) ||
704 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
705 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
706 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
708 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
714 wxTreeItemId id
= event
.GetItem();
715 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
718 wxString
new_name( wxPathOnly( data
->m_path
) );
719 new_name
+= wxString(wxFILE_SEP_PATH
);
720 new_name
+= event
.GetLabel();
724 if (wxFileExists(new_name
))
726 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
731 if (wxRenameFile(data
->m_path
,new_name
))
733 data
->SetNewDirName( new_name
);
737 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
743 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
745 wxTreeItemId parentId
= event
.GetItem();
747 // VS: this is needed because the event handler is called from wxTreeCtrl
748 // ctor when wxTR_HIDE_ROOT was specified
750 if (!m_rootId
.IsOk())
752 m_rootId
= m_treeCtrl
->GetRootItem();
757 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
759 CollapseDir(event
.GetItem());
762 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
766 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
767 if (!data
->m_isExpanded
)
770 data
->m_isExpanded
= false;
771 wxTreeItemIdValue cookie
;
772 /* Workaround because DeleteChildren has disapeared (why?) and
773 * CollapseAndReset doesn't work as advertised (deletes parent too) */
774 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
777 m_treeCtrl
->Delete(child
);
778 /* Not GetNextChild below, because the cookie mechanism can't
779 * handle disappearing children! */
780 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
782 if (parentId
!= m_treeCtrl
->GetRootItem())
783 m_treeCtrl
->Collapse(parentId
);
786 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
788 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
790 if (data
->m_isExpanded
)
793 data
->m_isExpanded
= true;
795 if (parentId
== m_treeCtrl
->GetRootItem())
803 wxString search
,path
,filename
;
805 wxString
dirName(data
->m_path
);
807 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
808 // Check if this is a root directory and if so,
809 // whether the drive is avaiable.
810 if (!wxIsDriveAvailable(dirName
))
812 data
->m_isExpanded
= false;
813 //wxMessageBox(wxT("Sorry, this drive is not available."));
818 // This may take a longish time. Go to busy cursor
821 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
822 if (dirName
.Last() == ':')
823 dirName
+= wxString(wxFILE_SEP_PATH
);
827 wxArrayString filenames
;
830 wxString eachFilename
;
837 int style
= wxDIR_DIRS
;
838 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
839 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
843 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
845 dirs
.Add(eachFilename
);
848 while (d
.GetNext(&eachFilename
));
851 dirs
.Sort(wxDirCtrlStringCompareFunction
);
853 // Now do the filenames -- but only if we're allowed to
854 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
860 int style
= wxDIR_FILES
;
861 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
862 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
863 wxStringTokenizer strTok
;
865 strTok
.SetString(m_currentFilterStr
,wxT(";"));
866 while(strTok
.HasMoreTokens())
868 curFilter
= strTok
.GetNextToken();
869 if (d
.GetFirst(& eachFilename
, curFilter
, style
))
873 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
875 filenames
.Add(eachFilename
);
878 while (d
.GetNext(& eachFilename
));
882 filenames
.Sort(wxDirCtrlStringCompareFunction
);
885 // Add the sorted dirs
887 for (i
= 0; i
< dirs
.Count(); i
++)
889 eachFilename
= dirs
[i
];
891 if (!wxEndsWithPathSeparator(path
))
892 path
+= wxString(wxFILE_SEP_PATH
);
893 path
+= eachFilename
;
895 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,true);
896 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
897 wxFileIconsTable::folder
, -1, dir_item
);
898 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
899 wxTreeItemIcon_Expanded
);
901 // Has this got any children? If so, make it expandable.
902 // (There are two situations when a dir has children: either it
903 // has subdirectories or it contains files that weren't filtered
904 // out. The latter only applies to dirctrl with files.)
905 if ( dir_item
->HasSubDirs() ||
906 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
907 dir_item
->HasFiles(m_currentFilterStr
)) )
909 m_treeCtrl
->SetItemHasChildren(id
);
913 // Add the sorted filenames
914 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
916 for (i
= 0; i
< filenames
.Count(); i
++)
918 eachFilename
= filenames
[i
];
920 if (!wxEndsWithPathSeparator(path
))
921 path
+= wxString(wxFILE_SEP_PATH
);
922 path
+= eachFilename
;
923 //path = dirName + wxString(wxT("/")) + eachFilename;
924 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,false);
925 int image_id
= wxFileIconsTable::file
;
926 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
927 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
928 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
933 void wxGenericDirCtrl::ReCreateTree()
935 CollapseDir(m_treeCtrl
->GetRootItem());
939 void wxGenericDirCtrl::CollapseTree()
941 wxTreeItemIdValue cookie
;
942 wxTreeItemId child
= m_treeCtrl
->GetFirstChild(m_rootId
, cookie
);
946 child
= m_treeCtrl
->GetNextChild(m_rootId
, cookie
);
950 // Find the child that matches the first part of 'path'.
951 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
952 // then the child for /usr is returned.
953 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
955 wxString
path2(path
);
957 // Make sure all separators are as per the current platform
958 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
959 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
961 // Append a separator to foil bogus substring matching
962 path2
+= wxString(wxFILE_SEP_PATH
);
964 // In MSW or PM, case is not significant
965 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
969 wxTreeItemIdValue cookie
;
970 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
971 while (childId
.IsOk())
973 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
975 if (data
&& !data
->m_path
.empty())
977 wxString
childPath(data
->m_path
);
978 if (!wxEndsWithPathSeparator(childPath
))
979 childPath
+= wxString(wxFILE_SEP_PATH
);
981 // In MSW and PM, case is not significant
982 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
983 childPath
.MakeLower();
986 if (childPath
.length() <= path2
.length())
988 wxString path3
= path2
.Mid(0, childPath
.length());
989 if (childPath
== path3
)
991 if (path3
.length() == path2
.length())
1000 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
1002 wxTreeItemId invalid
;
1006 // Try to expand as much of the given path as possible,
1007 // and select the given tree item.
1008 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
1011 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1012 wxTreeItemId lastId
= id
; // The last non-zero id
1013 while (id
.IsOk() && !done
)
1017 id
= FindChild(id
, path
, done
);
1024 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1027 m_treeCtrl
->Expand(lastId
);
1029 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1031 // Find the first file in this directory
1032 wxTreeItemIdValue cookie
;
1033 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1034 bool selectedChild
= false;
1035 while (childId
.IsOk())
1037 data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1039 if (data
&& data
->m_path
!= wxEmptyString
&& !data
->m_isDir
)
1041 m_treeCtrl
->SelectItem(childId
);
1042 m_treeCtrl
->EnsureVisible(childId
);
1043 selectedChild
= true;
1046 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1050 m_treeCtrl
->SelectItem(lastId
);
1051 m_treeCtrl
->EnsureVisible(lastId
);
1056 m_treeCtrl
->SelectItem(lastId
);
1057 m_treeCtrl
->EnsureVisible(lastId
);
1064 bool wxGenericDirCtrl::CollapsePath(const wxString
& path
)
1067 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1068 wxTreeItemId lastId
= id
; // The last non-zero id
1070 while ( id
.IsOk() && !done
)
1074 id
= FindChild(id
, path
, done
);
1080 if ( !lastId
.IsOk() )
1083 m_treeCtrl
->SelectItem(lastId
);
1084 m_treeCtrl
->EnsureVisible(lastId
);
1090 wxString
wxGenericDirCtrl::GetPath() const
1092 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1095 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1096 return data
->m_path
;
1099 return wxEmptyString
;
1102 wxString
wxGenericDirCtrl::GetFilePath() const
1104 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1107 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1109 return wxEmptyString
;
1111 return data
->m_path
;
1114 return wxEmptyString
;
1117 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1119 m_defaultPath
= path
;
1126 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1128 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1130 // This may take a longish time. Go to busy cursor
1135 wxString search
,path
,filename
;
1137 wxString
dirName(data
->m_path
);
1139 #if defined(__WXMSW__) || defined(__OS2__)
1140 if (dirName
.Last() == ':')
1141 dirName
+= wxString(wxFILE_SEP_PATH
);
1145 wxString eachFilename
;
1152 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1156 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1158 filenames
.Add(eachFilename
);
1161 while (d
.GetNext(& eachFilename
)) ;
1167 void wxGenericDirCtrl::SetFilterIndex(int n
)
1169 m_currentFilter
= n
;
1172 if (ExtractWildcard(m_filter
, n
, f
, d
))
1173 m_currentFilterStr
= f
;
1176 m_currentFilterStr
= wxT("*");
1178 m_currentFilterStr
= wxT("*.*");
1182 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1187 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1188 m_currentFilterStr
= f
;
1191 m_currentFilterStr
= wxT("*");
1193 m_currentFilterStr
= wxT("*.*");
1197 // Extract description and actual filter from overall filter string
1198 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1200 wxArrayString filters
, descriptions
;
1201 int count
= wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1202 if (count
> 0 && n
< count
)
1204 filter
= filters
[n
];
1205 description
= descriptions
[n
];
1212 #if WXWIN_COMPATIBILITY_2_4
1213 // Parses the global filter, returning the number of filters.
1214 // Returns 0 if none or if there's a problem.
1215 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1216 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1218 return wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1220 #endif // WXWIN_COMPATIBILITY_2_4
1222 void wxGenericDirCtrl::DoResize()
1224 wxSize sz
= GetClientSize();
1225 int verticalSpacing
= 3;
1229 if (m_filterListCtrl
)
1231 filterSz
= m_filterListCtrl
->GetSize();
1232 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1234 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1235 if (m_filterListCtrl
)
1237 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1238 // Don't know why, but this needs refreshing after a resize (wxMSW)
1239 m_filterListCtrl
->Refresh();
1245 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1250 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1251 const wxString
& text
,
1252 int image
, int selectedImage
,
1253 wxTreeItemData
* data
)
1255 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1257 wxASSERT (treeCtrl
);
1261 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1265 return wxTreeItemId();
1270 //-----------------------------------------------------------------------------
1271 // wxDirFilterListCtrl
1272 //-----------------------------------------------------------------------------
1274 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1276 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1277 EVT_CHOICE(wxID_ANY
, wxDirFilterListCtrl::OnSelFilter
)
1280 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1286 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1289 void wxDirFilterListCtrl::Init()
1294 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1296 int sel
= GetSelection();
1298 wxString currentPath
= m_dirCtrl
->GetPath();
1300 m_dirCtrl
->SetFilterIndex(sel
);
1302 // If the filter has changed, the view is out of date, so
1303 // collapse the tree.
1304 m_dirCtrl
->ReCreateTree();
1306 // Try to restore the selection, or at least the directory
1307 m_dirCtrl
->ExpandPath(currentPath
);
1310 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1313 wxArrayString descriptions
, filters
;
1314 size_t n
= (size_t) wxParseCommonDialogsFilter(filter
, descriptions
, filters
);
1316 if (n
> 0 && defaultFilter
< (int) n
)
1318 for (size_t i
= 0; i
< n
; i
++)
1319 Append(descriptions
[i
]);
1320 SetSelection(defaultFilter
);
1323 #endif // wxUSE_DIRDLG
1325 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1327 // ----------------------------------------------------------------------------
1328 // wxFileIconsTable icons
1329 // ----------------------------------------------------------------------------
1332 /* Computer (c) Julian Smart */
1333 static const char * file_icons_tbl_computer_xpm
[] = {
1334 /* columns rows colors chars-per-pixel */
1396 #endif // GTK+ < 2.4
1398 // ----------------------------------------------------------------------------
1399 // wxFileIconsTable & friends
1400 // ----------------------------------------------------------------------------
1402 // global instance of a wxFileIconsTable
1403 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1405 // A module to allow icons table cleanup
1407 class wxFileIconsTableModule
: public wxModule
1409 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1411 wxFileIconsTableModule() {}
1412 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return true; }
1415 if (wxTheFileIconsTable
)
1417 delete wxTheFileIconsTable
;
1418 wxTheFileIconsTable
= NULL
;
1423 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1425 class wxFileIconEntry
: public wxObject
1428 wxFileIconEntry(int i
) { id
= i
; }
1433 wxFileIconsTable::wxFileIconsTable()
1436 m_smallImageList
= NULL
;
1439 wxFileIconsTable::~wxFileIconsTable()
1443 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1446 if (m_smallImageList
) delete m_smallImageList
;
1449 // delayed initialization - wait until first use (wxArtProv not created yet)
1450 void wxFileIconsTable::Create()
1452 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1453 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1454 m_smallImageList
= new wxImageList(16, 16);
1457 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
,
1461 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN
,
1466 // GTK24 uses this icon in the file open dialog
1467 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1471 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1474 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1478 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_CDROM
,
1482 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FLOPPY
,
1486 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE
,
1490 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
,
1494 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1496 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
,
1499 delete m_HashTable
->Get(_T("exe"));
1500 m_HashTable
->Delete(_T("exe"));
1501 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1503 /* else put into list by GetIconID
1504 (KDE defines application/x-executable for *.exe and has nice icon)
1508 wxImageList
*wxFileIconsTable::GetSmallImageList()
1510 if (!m_smallImageList
)
1513 return m_smallImageList
;
1516 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1517 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1518 // one icon and we won't resize it
1520 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1522 const unsigned int size
= 16;
1524 wxImage
smallimg (size
, size
);
1525 unsigned char *p1
, *p2
, *ps
;
1526 unsigned char mr
= img
.GetMaskRed(),
1527 mg
= img
.GetMaskGreen(),
1528 mb
= img
.GetMaskBlue();
1531 unsigned sr
, sg
, sb
, smask
;
1533 p1
= img
.GetData(), p2
= img
.GetData() + 3 * size
*2, ps
= smallimg
.GetData();
1534 smallimg
.SetMaskColour(mr
, mr
, mr
);
1536 for (y
= 0; y
< size
; y
++)
1538 for (x
= 0; x
< size
; x
++)
1540 sr
= sg
= sb
= smask
= 0;
1541 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1542 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1545 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1546 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1549 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1550 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1553 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1554 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1559 ps
[0] = ps
[1] = ps
[2] = mr
;
1562 ps
[0] = (unsigned char)(sr
>> 2);
1563 ps
[1] = (unsigned char)(sg
>> 2);
1564 ps
[2] = (unsigned char)(sb
>> 2);
1568 p1
+= size
*2 * 3, p2
+= size
*2 * 3;
1571 return wxBitmap(smallimg
);
1574 // This function is currently not unused anymore
1576 // finds empty borders and return non-empty area of image:
1577 static wxImage
CutEmptyBorders(const wxImage
& img
)
1579 unsigned char mr
= img
.GetMaskRed(),
1580 mg
= img
.GetMaskGreen(),
1581 mb
= img
.GetMaskBlue();
1582 unsigned char *dt
= img
.GetData(), *dttmp
;
1583 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1585 unsigned top
, bottom
, left
, right
, i
;
1588 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1589 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1591 for (empt
= true, top
= 0; empt
&& top
< h
; top
++)
1594 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1597 for (empt
= true, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1599 MK_DTTMP(0, bottom
);
1600 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1603 for (empt
= true, left
= 0; empt
&& left
< w
; left
++)
1606 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1609 for (empt
= true, right
= w
-1; empt
&& right
> left
; right
--)
1612 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1615 top
--, left
--, bottom
++, right
++;
1617 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1621 #endif // wxUSE_MIMETYPE
1623 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1625 if (!m_smallImageList
)
1629 if (!extension
.empty())
1631 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1632 if (entry
) return (entry
-> id
);
1635 wxFileType
*ft
= (mime
.empty()) ?
1636 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1637 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1639 wxIconLocation iconLoc
;
1644 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1646 ic
= wxIcon( iconLoc
);
1655 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1660 bmp
.CopyFromIcon(ic
);
1665 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1669 const unsigned int size
= 16;
1671 int id
= m_smallImageList
->GetImageCount();
1672 if ((bmp
.GetWidth() == (int) size
) && (bmp
.GetHeight() == (int) size
))
1674 m_smallImageList
->Add(bmp
);
1676 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1679 wxImage img
= bmp
.ConvertToImage();
1681 if ((img
.GetWidth() != size
*2) || (img
.GetHeight() != size
*2))
1682 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1683 m_smallImageList
->Add(CreateAntialiasedBitmap(img
.Rescale(size
*2, size
*2)));
1685 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1687 #endif // wxUSE_IMAGE
1689 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1692 #else // !wxUSE_MIMETYPE
1695 if (extension
== wxT("exe"))
1699 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1702 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG