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
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"
45 #include "wx/mimetype.h"
47 #include "wx/choice.h"
50 #include "wx/statline.h"
53 #if defined(__WXMAC__)
54 #include "wx/mac/private.h" // includes mac headers
59 #include "wx/msw/winundef.h"
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
]);
123 #if !defined(__WXWINCE__)
124 wxChar pname
[52]; // FIXME: why 52 and not MAX_PATH or whatever?
125 if ( GetVolumeInformation(path
, pname
, WXSIZEOF(pname
),
126 NULL
, NULL
, NULL
, NULL
, 0) )
128 name
<< _T(' ') << pname
;
130 #endif // __WXWINCE__
133 int driveType
= ::GetDriveType(path
);
136 case DRIVE_REMOVABLE
:
137 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
138 imageId
= wxFileIconsTable::floppy
;
140 imageId
= wxFileIconsTable::removeable
;
143 imageId
= wxFileIconsTable::cdrom
;
148 imageId
= wxFileIconsTable::drive
;
154 icon_ids
.Add(imageId
);
156 while (driveBuffer
[i
] != wxT('\0'))
159 if (driveBuffer
[i
] == wxT('\0'))
162 #elif defined(__OS2__)
164 ULONG ulDriveNum
= 0;
165 ULONG ulDriveMap
= 0;
166 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
172 if (ulDriveMap
& ( 1 << i
))
175 path
.Printf(wxT("%c:\\"), 'A' + i
);
176 name
.Printf(wxT("%c:"), 'A' + i
);
178 // Note: If _filesys is unsupported by some compilers,
179 // we can always replace it by DosQueryFSAttach
180 char filesysname
[20];
182 ULONG cbBuffer
= sizeof(filesysname
);
183 PFSQBUFFER2 pfsqBuffer
= (PFSQBUFFER2
)filesysname
;
184 APIRET rc
= ::DosQueryFSAttach(name
.fn_str(),0,FSAIL_QUERYNAME
,pfsqBuffer
,&cbBuffer
);
187 filesysname
[0] = '\0';
190 _filesys(name
.fn_str(), filesysname
, sizeof(filesysname
));
192 /* FAT, LAN, HPFS, CDFS, NFS */
194 if (path
== wxT("A:\\") || path
== wxT("B:\\"))
195 imageId
= wxFileIconsTable::floppy
;
196 else if (!strcmp(filesysname
, "CDFS"))
197 imageId
= wxFileIconsTable::cdrom
;
198 else if (!strcmp(filesysname
, "LAN") ||
199 !strcmp(filesysname
, "NFS"))
200 imageId
= wxFileIconsTable::drive
;
202 imageId
= wxFileIconsTable::drive
;
205 icon_ids
.Add(imageId
);
210 #else // !__WIN32__, !__OS2__
213 /* If we can switch to the drive, it exists. */
214 for( drive
= 1; drive
<= 26; drive
++ )
217 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
218 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
220 if (wxIsDriveAvailable(path
))
224 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
227 #endif // __WIN32__/!__WIN32__
229 #elif defined(__WXMAC__)
231 ItemCount volumeIndex
= 1;
234 while( noErr
== err
)
236 HFSUniStr255 volumeName
;
238 FSVolumeInfo volumeInfo
;
239 err
= FSGetVolumeInfo(0, volumeIndex
, NULL
, kFSVolInfoFlags
, &volumeInfo
, &volumeName
, &fsRef
);
242 wxString path
= wxMacFSRefToPath( &fsRef
) ;
243 wxString name
= wxMacHFSUniStrToString( &volumeName
) ;
245 if ( (volumeInfo
.flags
& kFSVolFlagSoftwareLockedMask
) || (volumeInfo
.flags
& kFSVolFlagHardwareLockedMask
) )
247 icon_ids
.Add(wxFileIconsTable::cdrom
);
251 icon_ids
.Add(wxFileIconsTable::drive
);
253 // todo other removable
261 #elif defined(__UNIX__)
264 icon_ids
.Add(wxFileIconsTable::computer
);
266 #error "Unsupported platform in wxGenericDirCtrl!"
268 wxASSERT_MSG( (paths
.GetCount() == names
.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
269 wxASSERT_MSG( (paths
.GetCount() == icon_ids
.GetCount()), wxT("Wrong number of icons for available drives."));
270 return paths
.GetCount();
273 // ----------------------------------------------------------------------------
274 // wxIsDriveAvailable
275 // ----------------------------------------------------------------------------
279 bool wxIsDriveAvailable(const wxString
& dirName
)
281 // FIXME_MGL - this method leads to hang up under Watcom for some reason
283 wxUnusedVar(dirName
);
285 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
287 wxString
dirNameLower(dirName
.Lower());
288 // VS: always return true for removable media, since Win95 doesn't
289 // like it when MS-DOS app accesses empty floppy drive
290 return (dirNameLower
[0u] == wxT('a') ||
291 dirNameLower
[0u] == wxT('b') ||
292 wxDirExists(dirNameLower
));
299 #elif defined(__WINDOWS__) || defined(__OS2__)
301 int setdrive(int WXUNUSED_IN_WINCE(drive
))
305 #elif defined(__GNUWIN32__) && \
306 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
307 return _chdrive(drive
);
311 if (drive
< 1 || drive
> 31)
313 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
314 newdrive
[1] = wxT(':');
316 newdrive
[2] = wxT('\\');
317 newdrive
[3] = wxT('\0');
319 newdrive
[2] = wxT('\0');
321 #if defined(__WXMSW__)
322 if (::SetCurrentDirectory(newdrive
))
324 // VA doesn't know what LPSTR is and has its own set
325 if (!DosSetCurrentDir((PSZ
)newdrive
))
333 bool wxIsDriveAvailable(const wxString
& WXUNUSED_IN_WINCE(dirName
))
339 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
343 // Check if this is a root directory and if so,
344 // whether the drive is available.
345 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
347 wxString
dirNameLower(dirName
.Lower());
348 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
349 success
= wxDirExists(dirNameLower
);
352 // Avoid changing to drive since no media may be inserted.
353 if (dirNameLower
[(size_t)0] == 'a' || dirNameLower
[(size_t)0] == 'b')
356 int currentDrive
= _getdrive();
357 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
358 int err
= setdrive( thisDrive
) ;
359 setdrive( currentDrive
);
368 (void) SetErrorMode(errorMode
);
374 #endif // __WINDOWS__ || __OS2__
376 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
382 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
383 // and sort regardless of case.
384 static int wxCMPFUNC_CONV
wxDirCtrlStringCompareFunction(const wxString
& strFirst
, const wxString
& strSecond
)
386 return strFirst
.CmpNoCase(strSecond
);
389 //-----------------------------------------------------------------------------
391 //-----------------------------------------------------------------------------
393 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
398 /* Insert logic to detect hidden files here
399 * In UnixLand we just check whether the first char is a dot
400 * For FileNameFromPath read LastDirNameInThisPath ;-) */
401 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
403 m_isExpanded
= false;
407 void wxDirItemData::SetNewDirName(const wxString
& path
)
410 m_name
= wxFileNameFromPath(path
);
413 bool wxDirItemData::HasSubDirs() const
421 if ( !dir
.Open(m_path
) )
425 return dir
.HasSubDirs();
428 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
436 if ( !dir
.Open(m_path
) )
440 return dir
.HasFiles();
443 //-----------------------------------------------------------------------------
445 //-----------------------------------------------------------------------------
448 #if wxUSE_EXTENDED_RTTI
449 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle
)
451 wxBEGIN_FLAGS( wxGenericDirCtrlStyle
)
452 // new style border flags, we put them first to
453 // use them for streaming out
454 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
455 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
456 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
457 wxFLAGS_MEMBER(wxBORDER_RAISED
)
458 wxFLAGS_MEMBER(wxBORDER_STATIC
)
459 wxFLAGS_MEMBER(wxBORDER_NONE
)
461 // old style border flags
462 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
463 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
464 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
465 wxFLAGS_MEMBER(wxRAISED_BORDER
)
466 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
467 wxFLAGS_MEMBER(wxBORDER
)
469 // standard window styles
470 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
471 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
472 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
473 wxFLAGS_MEMBER(wxWANTS_CHARS
)
474 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
475 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
476 wxFLAGS_MEMBER(wxVSCROLL
)
477 wxFLAGS_MEMBER(wxHSCROLL
)
479 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY
)
480 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL
)
481 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST
)
482 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS
)
484 wxEND_FLAGS( wxGenericDirCtrlStyle
)
486 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl
, wxControl
,"wx/dirctrl.h")
488 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl
)
489 wxHIDE_PROPERTY( Children
)
490 wxPROPERTY( DefaultPath
, wxString
, SetDefaultPath
, GetDefaultPath
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
491 wxPROPERTY( Filter
, wxString
, SetFilter
, GetFilter
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
492 wxPROPERTY( DefaultFilter
, int , SetFilterIndex
, GetFilterIndex
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
493 wxPROPERTY_FLAGS( WindowStyle
, wxGenericDirCtrlStyle
, long, SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0, wxT("Helpstring"), wxT("group") )
494 wxEND_PROPERTIES_TABLE()
496 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl
)
497 wxEND_HANDLERS_TABLE()
499 wxCONSTRUCTOR_8( wxGenericDirCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, DefaultPath
,
500 wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
, wxString
, Filter
, int , DefaultFilter
)
502 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
505 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
506 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL
, wxGenericDirCtrl::OnExpandItem
)
507 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL
, wxGenericDirCtrl::OnCollapseItem
)
508 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnBeginEditItem
)
509 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnEndEditItem
)
510 EVT_SIZE (wxGenericDirCtrl::OnSize
)
513 wxGenericDirCtrl::wxGenericDirCtrl(void)
518 void wxGenericDirCtrl::ExpandRoot()
520 ExpandDir(m_rootId
); // automatically expand first level
522 // Expand and select the default path
523 if (!m_defaultPath
.empty())
525 ExpandPath(m_defaultPath
);
530 // On Unix, there's only one node under the (hidden) root node. It
531 // represents the / path, so the user would always have to expand it;
532 // let's do it ourselves
533 ExpandPath( wxT("/") );
538 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
544 const wxString
& filter
,
546 const wxString
& name
)
548 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
551 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
555 long treeStyle
= wxTR_HAS_BUTTONS
;
557 // On Windows CE, if you hide the root, you get a crash when
558 // attempting to access data for children of the root item.
560 treeStyle
|= wxTR_HIDE_ROOT
;
564 treeStyle
|= wxTR_NO_LINES
;
567 if (style
& wxDIRCTRL_EDIT_LABELS
)
568 treeStyle
|= wxTR_EDIT_LABELS
;
570 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
571 treeStyle
|= wxNO_BORDER
;
573 treeStyle
|= wxBORDER_SUNKEN
;
575 long filterStyle
= 0;
576 if ((style
& wxDIRCTRL_3D_INTERNAL
) == 0)
577 filterStyle
|= wxNO_BORDER
;
579 filterStyle
|= wxBORDER_SUNKEN
;
581 m_treeCtrl
= CreateTreeCtrl(this, wxID_TREECTRL
,
582 wxPoint(0,0), GetClientSize(), treeStyle
);
584 if (!filter
.empty() && (style
& wxDIRCTRL_SHOW_FILTERS
))
585 m_filterListCtrl
= new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL
, wxDefaultPosition
, wxDefaultSize
, filterStyle
);
590 if (m_filter
.empty())
594 m_filter
= wxT("*.*");
597 SetFilterIndex(defaultFilter
);
599 if (m_filterListCtrl
)
600 m_filterListCtrl
->FillFilterList(filter
, defaultFilter
);
602 m_treeCtrl
->SetImageList(wxTheFileIconsTable
->GetSmallImageList());
604 m_showHidden
= false;
605 wxDirItemData
* rootData
= new wxDirItemData(wxEmptyString
, wxEmptyString
, true);
609 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
610 rootName
= _("Computer");
612 rootName
= _("Sections");
615 m_rootId
= m_treeCtrl
->AddRoot( rootName
, 3, -1, rootData
);
616 m_treeCtrl
->SetItemHasChildren(m_rootId
);
626 wxGenericDirCtrl::~wxGenericDirCtrl()
630 void wxGenericDirCtrl::Init()
632 m_showHidden
= false;
634 m_currentFilterStr
= wxEmptyString
; // Default: any file
636 m_filterListCtrl
= NULL
;
639 wxTreeCtrl
* wxGenericDirCtrl::CreateTreeCtrl(wxWindow
*parent
, wxWindowID id
, const wxPoint
& pos
, const wxSize
& size
, long treeStyle
)
641 return new wxTreeCtrl(parent
, id
, pos
, size
, treeStyle
);
644 void wxGenericDirCtrl::ShowHidden( bool show
)
648 wxString path
= GetPath();
654 wxGenericDirCtrl::AddSection(const wxString
& path
, const wxString
& name
, int imageId
)
656 wxDirItemData
*dir_item
= new wxDirItemData(path
,name
,true);
658 wxTreeItemId id
= AppendItem( m_rootId
, name
, imageId
, -1, dir_item
);
660 m_treeCtrl
->SetItemHasChildren(id
);
665 void wxGenericDirCtrl::SetupSections()
667 wxArrayString paths
, names
;
670 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
673 wxString home
= wxGetHomeDir();
674 AddSection( home
, _("Home directory"), 1);
675 home
+= wxT("/Desktop");
676 AddSection( home
, _("Desktop"), 1);
679 for (n
= 0; n
< count
; n
++)
680 AddSection(paths
[n
], names
[n
], icons
[n
]);
683 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent
&event
)
685 // don't rename the main entry "Sections"
686 if (event
.GetItem() == m_rootId
)
692 // don't rename the individual sections
693 if (m_treeCtrl
->GetItemParent( event
.GetItem() ) == m_rootId
)
700 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent
&event
)
702 if ((event
.GetLabel().empty()) ||
703 (event
.GetLabel() == _(".")) ||
704 (event
.GetLabel() == _("..")) ||
705 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
706 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
707 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
709 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
715 wxTreeItemId id
= event
.GetItem();
716 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
719 wxString
new_name( wxPathOnly( data
->m_path
) );
720 new_name
+= wxString(wxFILE_SEP_PATH
);
721 new_name
+= event
.GetLabel();
725 if (wxFileExists(new_name
))
727 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
732 if (wxRenameFile(data
->m_path
,new_name
))
734 data
->SetNewDirName( new_name
);
738 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
744 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
746 wxTreeItemId parentId
= event
.GetItem();
748 // VS: this is needed because the event handler is called from wxTreeCtrl
749 // ctor when wxTR_HIDE_ROOT was specified
751 if (!m_rootId
.IsOk())
753 m_rootId
= m_treeCtrl
->GetRootItem();
758 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
760 CollapseDir(event
.GetItem());
763 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
767 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
768 if (!data
->m_isExpanded
)
771 data
->m_isExpanded
= false;
772 wxTreeItemIdValue cookie
;
773 /* Workaround because DeleteChildren has disapeared (why?) and
774 * CollapseAndReset doesn't work as advertised (deletes parent too) */
775 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
778 m_treeCtrl
->Delete(child
);
779 /* Not GetNextChild below, because the cookie mechanism can't
780 * handle disappearing children! */
781 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
783 if (parentId
!= m_treeCtrl
->GetRootItem())
784 m_treeCtrl
->Collapse(parentId
);
787 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
789 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
791 if (data
->m_isExpanded
)
794 data
->m_isExpanded
= true;
796 if (parentId
== m_treeCtrl
->GetRootItem())
804 wxString search
,path
,filename
;
806 wxString
dirName(data
->m_path
);
808 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
809 // Check if this is a root directory and if so,
810 // whether the drive is avaiable.
811 if (!wxIsDriveAvailable(dirName
))
813 data
->m_isExpanded
= false;
814 //wxMessageBox(wxT("Sorry, this drive is not available."));
819 // This may take a longish time. Go to busy cursor
822 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
823 if (dirName
.Last() == ':')
824 dirName
+= wxString(wxFILE_SEP_PATH
);
828 wxArrayString filenames
;
831 wxString eachFilename
;
838 int style
= wxDIR_DIRS
;
839 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
840 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
844 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
846 dirs
.Add(eachFilename
);
849 while (d
.GetNext(&eachFilename
));
852 dirs
.Sort(wxDirCtrlStringCompareFunction
);
854 // Now do the filenames -- but only if we're allowed to
855 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
861 int style
= wxDIR_FILES
;
862 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
863 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
864 wxStringTokenizer strTok
;
866 strTok
.SetString(m_currentFilterStr
,wxT(";"));
867 while(strTok
.HasMoreTokens())
869 curFilter
= strTok
.GetNextToken();
870 if (d
.GetFirst(& eachFilename
, curFilter
, style
))
874 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
876 filenames
.Add(eachFilename
);
879 while (d
.GetNext(& eachFilename
));
883 filenames
.Sort(wxDirCtrlStringCompareFunction
);
886 // Add the sorted dirs
888 for (i
= 0; i
< dirs
.Count(); i
++)
890 eachFilename
= dirs
[i
];
892 if (!wxEndsWithPathSeparator(path
))
893 path
+= wxString(wxFILE_SEP_PATH
);
894 path
+= eachFilename
;
896 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,true);
897 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
898 wxFileIconsTable::folder
, -1, dir_item
);
899 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
900 wxTreeItemIcon_Expanded
);
902 // Has this got any children? If so, make it expandable.
903 // (There are two situations when a dir has children: either it
904 // has subdirectories or it contains files that weren't filtered
905 // out. The latter only applies to dirctrl with files.)
906 if ( dir_item
->HasSubDirs() ||
907 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
908 dir_item
->HasFiles(m_currentFilterStr
)) )
910 m_treeCtrl
->SetItemHasChildren(id
);
914 // Add the sorted filenames
915 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
917 for (i
= 0; i
< filenames
.Count(); i
++)
919 eachFilename
= filenames
[i
];
921 if (!wxEndsWithPathSeparator(path
))
922 path
+= wxString(wxFILE_SEP_PATH
);
923 path
+= eachFilename
;
924 //path = dirName + wxString(wxT("/")) + eachFilename;
925 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,false);
926 int image_id
= wxFileIconsTable::file
;
927 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
928 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
929 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
934 void wxGenericDirCtrl::ReCreateTree()
936 CollapseDir(m_treeCtrl
->GetRootItem());
940 void wxGenericDirCtrl::CollapseTree()
942 wxTreeItemIdValue cookie
;
943 wxTreeItemId child
= m_treeCtrl
->GetFirstChild(m_rootId
, cookie
);
947 child
= m_treeCtrl
->GetNextChild(m_rootId
, cookie
);
951 // Find the child that matches the first part of 'path'.
952 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
953 // then the child for /usr is returned.
954 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
956 wxString
path2(path
);
958 // Make sure all separators are as per the current platform
959 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
960 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
962 // Append a separator to foil bogus substring matching
963 path2
+= wxString(wxFILE_SEP_PATH
);
965 // In MSW or PM, case is not significant
966 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
970 wxTreeItemIdValue cookie
;
971 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
972 while (childId
.IsOk())
974 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
976 if (data
&& !data
->m_path
.empty())
978 wxString
childPath(data
->m_path
);
979 if (!wxEndsWithPathSeparator(childPath
))
980 childPath
+= wxString(wxFILE_SEP_PATH
);
982 // In MSW and PM, case is not significant
983 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
984 childPath
.MakeLower();
987 if (childPath
.Len() <= path2
.Len())
989 wxString path3
= path2
.Mid(0, childPath
.Len());
990 if (childPath
== path3
)
992 if (path3
.Len() == path2
.Len())
1001 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
1003 wxTreeItemId invalid
;
1007 // Try to expand as much of the given path as possible,
1008 // and select the given tree item.
1009 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
1012 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1013 wxTreeItemId lastId
= id
; // The last non-zero id
1014 while (id
.IsOk() && !done
)
1018 id
= FindChild(id
, path
, done
);
1025 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1028 m_treeCtrl
->Expand(lastId
);
1030 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1032 // Find the first file in this directory
1033 wxTreeItemIdValue cookie
;
1034 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1035 bool selectedChild
= false;
1036 while (childId
.IsOk())
1038 data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1040 if (data
&& data
->m_path
!= wxEmptyString
&& !data
->m_isDir
)
1042 m_treeCtrl
->SelectItem(childId
);
1043 m_treeCtrl
->EnsureVisible(childId
);
1044 selectedChild
= true;
1047 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1051 m_treeCtrl
->SelectItem(lastId
);
1052 m_treeCtrl
->EnsureVisible(lastId
);
1057 m_treeCtrl
->SelectItem(lastId
);
1058 m_treeCtrl
->EnsureVisible(lastId
);
1064 wxString
wxGenericDirCtrl::GetPath() const
1066 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1069 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1070 return data
->m_path
;
1073 return wxEmptyString
;
1076 wxString
wxGenericDirCtrl::GetFilePath() const
1078 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1081 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1083 return wxEmptyString
;
1085 return data
->m_path
;
1088 return wxEmptyString
;
1091 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1093 m_defaultPath
= path
;
1100 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1102 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1104 // This may take a longish time. Go to busy cursor
1109 wxString search
,path
,filename
;
1111 wxString
dirName(data
->m_path
);
1113 #if defined(__WXMSW__) || defined(__OS2__)
1114 if (dirName
.Last() == ':')
1115 dirName
+= wxString(wxFILE_SEP_PATH
);
1119 wxString eachFilename
;
1126 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1130 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1132 filenames
.Add(eachFilename
);
1135 while (d
.GetNext(& eachFilename
)) ;
1141 void wxGenericDirCtrl::SetFilterIndex(int n
)
1143 m_currentFilter
= n
;
1146 if (ExtractWildcard(m_filter
, n
, f
, d
))
1147 m_currentFilterStr
= f
;
1150 m_currentFilterStr
= wxT("*");
1152 m_currentFilterStr
= wxT("*.*");
1156 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1161 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1162 m_currentFilterStr
= f
;
1165 m_currentFilterStr
= wxT("*");
1167 m_currentFilterStr
= wxT("*.*");
1171 // Extract description and actual filter from overall filter string
1172 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1174 wxArrayString filters
, descriptions
;
1175 int count
= wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1176 if (count
> 0 && n
< count
)
1178 filter
= filters
[n
];
1179 description
= descriptions
[n
];
1186 #if WXWIN_COMPATIBILITY_2_4
1187 // Parses the global filter, returning the number of filters.
1188 // Returns 0 if none or if there's a problem.
1189 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1190 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1192 return wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1194 #endif // WXWIN_COMPATIBILITY_2_4
1196 void wxGenericDirCtrl::DoResize()
1198 wxSize sz
= GetClientSize();
1199 int verticalSpacing
= 3;
1203 if (m_filterListCtrl
)
1205 filterSz
= m_filterListCtrl
->GetSize();
1206 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1208 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1209 if (m_filterListCtrl
)
1211 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1212 // Don't know why, but this needs refreshing after a resize (wxMSW)
1213 m_filterListCtrl
->Refresh();
1219 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1224 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1225 const wxString
& text
,
1226 int image
, int selectedImage
,
1227 wxTreeItemData
* data
)
1229 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1231 wxASSERT (treeCtrl
);
1235 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1239 return wxTreeItemId();
1244 //-----------------------------------------------------------------------------
1245 // wxDirFilterListCtrl
1246 //-----------------------------------------------------------------------------
1248 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1250 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1251 EVT_CHOICE(wxID_ANY
, wxDirFilterListCtrl::OnSelFilter
)
1254 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1260 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1263 void wxDirFilterListCtrl::Init()
1268 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1270 int sel
= GetSelection();
1272 wxString currentPath
= m_dirCtrl
->GetPath();
1274 m_dirCtrl
->SetFilterIndex(sel
);
1276 // If the filter has changed, the view is out of date, so
1277 // collapse the tree.
1278 m_dirCtrl
->ReCreateTree();
1280 // Try to restore the selection, or at least the directory
1281 m_dirCtrl
->ExpandPath(currentPath
);
1284 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1287 wxArrayString descriptions
, filters
;
1288 size_t n
= (size_t) wxParseCommonDialogsFilter(filter
, descriptions
, filters
);
1290 if (n
> 0 && defaultFilter
< (int) n
)
1292 for (size_t i
= 0; i
< n
; i
++)
1293 Append(descriptions
[i
]);
1294 SetSelection(defaultFilter
);
1297 #endif // wxUSE_DIRDLG
1299 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1301 // ----------------------------------------------------------------------------
1302 // wxFileIconsTable icons
1303 // ----------------------------------------------------------------------------
1306 /* Computer (c) Julian Smart */
1307 static const char * file_icons_tbl_computer_xpm
[] = {
1308 /* columns rows colors chars-per-pixel */
1370 #endif // GTK+ < 2.4
1372 // ----------------------------------------------------------------------------
1373 // wxFileIconsTable & friends
1374 // ----------------------------------------------------------------------------
1376 // global instance of a wxFileIconsTable
1377 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1379 // A module to allow icons table cleanup
1381 class wxFileIconsTableModule
: public wxModule
1383 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1385 wxFileIconsTableModule() {}
1386 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return true; }
1389 if (wxTheFileIconsTable
)
1391 delete wxTheFileIconsTable
;
1392 wxTheFileIconsTable
= NULL
;
1397 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1399 class wxFileIconEntry
: public wxObject
1402 wxFileIconEntry(int i
) { id
= i
; }
1407 wxFileIconsTable::wxFileIconsTable()
1410 m_smallImageList
= NULL
;
1413 wxFileIconsTable::~wxFileIconsTable()
1417 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1420 if (m_smallImageList
) delete m_smallImageList
;
1423 // delayed initialization - wait until first use (wxArtProv not created yet)
1424 void wxFileIconsTable::Create()
1426 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1427 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1428 m_smallImageList
= new wxImageList(16, 16);
1431 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
,
1435 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN
,
1440 // GTK24 uses this icon in the file open dialog
1441 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1445 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1448 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1452 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_CDROM
,
1456 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FLOPPY
,
1460 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE
,
1464 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
,
1468 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1470 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
,
1473 delete m_HashTable
->Get(_T("exe"));
1474 m_HashTable
->Delete(_T("exe"));
1475 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1477 /* else put into list by GetIconID
1478 (KDE defines application/x-executable for *.exe and has nice icon)
1482 wxImageList
*wxFileIconsTable::GetSmallImageList()
1484 if (!m_smallImageList
)
1487 return m_smallImageList
;
1490 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1491 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1492 // one icon and we won't resize it
1494 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1496 const unsigned int size
= 16;
1498 wxImage
smallimg (size
, size
);
1499 unsigned char *p1
, *p2
, *ps
;
1500 unsigned char mr
= img
.GetMaskRed(),
1501 mg
= img
.GetMaskGreen(),
1502 mb
= img
.GetMaskBlue();
1505 unsigned sr
, sg
, sb
, smask
;
1507 p1
= img
.GetData(), p2
= img
.GetData() + 3 * size
*2, ps
= smallimg
.GetData();
1508 smallimg
.SetMaskColour(mr
, mr
, mr
);
1510 for (y
= 0; y
< size
; y
++)
1512 for (x
= 0; x
< size
; x
++)
1514 sr
= sg
= sb
= smask
= 0;
1515 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1516 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1519 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1520 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1523 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1524 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1527 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1528 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1533 ps
[0] = ps
[1] = ps
[2] = mr
;
1536 ps
[0] = (unsigned char)(sr
>> 2);
1537 ps
[1] = (unsigned char)(sg
>> 2);
1538 ps
[2] = (unsigned char)(sb
>> 2);
1542 p1
+= size
*2 * 3, p2
+= size
*2 * 3;
1545 return wxBitmap(smallimg
);
1548 // This function is currently not unused anymore
1550 // finds empty borders and return non-empty area of image:
1551 static wxImage
CutEmptyBorders(const wxImage
& img
)
1553 unsigned char mr
= img
.GetMaskRed(),
1554 mg
= img
.GetMaskGreen(),
1555 mb
= img
.GetMaskBlue();
1556 unsigned char *dt
= img
.GetData(), *dttmp
;
1557 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1559 unsigned top
, bottom
, left
, right
, i
;
1562 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1563 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1565 for (empt
= true, top
= 0; empt
&& top
< h
; top
++)
1568 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1571 for (empt
= true, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1573 MK_DTTMP(0, bottom
);
1574 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1577 for (empt
= true, left
= 0; empt
&& left
< w
; left
++)
1580 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1583 for (empt
= true, right
= w
-1; empt
&& right
> left
; right
--)
1586 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1589 top
--, left
--, bottom
++, right
++;
1591 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1595 #endif // wxUSE_MIMETYPE
1597 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1599 if (!m_smallImageList
)
1603 if (!extension
.empty())
1605 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1606 if (entry
) return (entry
-> id
);
1609 wxFileType
*ft
= (mime
.empty()) ?
1610 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1611 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1613 wxIconLocation iconLoc
;
1618 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1620 ic
= wxIcon( iconLoc
);
1629 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1634 bmp
.CopyFromIcon(ic
);
1639 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1643 const unsigned int size
= 16;
1645 int id
= m_smallImageList
->GetImageCount();
1646 if ((bmp
.GetWidth() == (int) size
) && (bmp
.GetHeight() == (int) size
))
1648 m_smallImageList
->Add(bmp
);
1650 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1653 wxImage img
= bmp
.ConvertToImage();
1655 if ((img
.GetWidth() != size
*2) || (img
.GetHeight() != size
*2))
1656 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1657 m_smallImageList
->Add(CreateAntialiasedBitmap(img
.Rescale(size
*2, size
*2)));
1659 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1661 #endif // wxUSE_IMAGE
1663 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1666 #else // !wxUSE_MIMETYPE
1669 if (extension
== wxT("exe"))
1673 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1676 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG