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"
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
]);
120 #if !defined(__WXWINCE__)
122 if (GetVolumeInformation( path
.c_str(), pname
, 52, NULL
, NULL
, NULL
, NULL
, NULL
))
124 name
.Printf(wxT("%s %s"), (const wxChar
*) name
, pname
);
129 int driveType
= ::GetDriveType(path
);
132 case DRIVE_REMOVABLE
:
133 if (path
== wxT("a:\\") || path
== wxT("b:\\"))
134 imageId
= wxFileIconsTable::floppy
;
136 imageId
= wxFileIconsTable::removeable
;
139 imageId
= wxFileIconsTable::cdrom
;
144 imageId
= wxFileIconsTable::drive
;
150 icon_ids
.Add(imageId
);
152 while (driveBuffer
[i
] != wxT('\0'))
155 if (driveBuffer
[i
] == wxT('\0'))
158 #elif defined(__OS2__)
160 ULONG ulDriveNum
= 0;
161 ULONG ulDriveMap
= 0;
162 rc
= ::DosQueryCurrentDisk(&ulDriveNum
, &ulDriveMap
);
168 if (ulDriveMap
& ( 1 << i
))
171 path
.Printf(wxT("%c:\\"), 'A' + i
);
172 name
.Printf(wxT("%c:"), 'A' + i
);
174 // Note: If _filesys is unsupported by some compilers,
175 // we can always replace it by DosQueryFSAttach
176 char filesysname
[20];
178 ULONG cbBuffer
= sizeof(filesysname
);
179 PFSQBUFFER2 pfsqBuffer
= (PFSQBUFFER2
)filesysname
;
180 APIRET rc
= ::DosQueryFSAttach(name
.fn_str(),0,FSAIL_QUERYNAME
,pfsqBuffer
,&cbBuffer
);
183 filesysname
[0] = '\0';
186 _filesys(name
.fn_str(), filesysname
, sizeof(filesysname
));
188 /* FAT, LAN, HPFS, CDFS, NFS */
190 if (path
== wxT("A:\\") || path
== wxT("B:\\"))
191 imageId
= wxFileIconsTable::floppy
;
192 else if (!strcmp(filesysname
, "CDFS"))
193 imageId
= wxFileIconsTable::cdrom
;
194 else if (!strcmp(filesysname
, "LAN") ||
195 !strcmp(filesysname
, "NFS"))
196 imageId
= wxFileIconsTable::drive
;
198 imageId
= wxFileIconsTable::drive
;
201 icon_ids
.Add(imageId
);
206 #else // !__WIN32__, !__OS2__
209 /* If we can switch to the drive, it exists. */
210 for( drive
= 1; drive
<= 26; drive
++ )
213 path
.Printf(wxT("%c:\\"), (char) (drive
+ 'a' - 1));
214 name
.Printf(wxT("%c:"), (char) (drive
+ 'A' - 1));
216 if (wxIsDriveAvailable(path
))
220 icon_ids
.Add((drive
<= 2) ? wxFileIconsTable::floppy
: wxFileIconsTable::drive
);
223 #endif // __WIN32__/!__WIN32__
225 #elif defined(__WXMAC__)
227 ItemCount volumeIndex
= 1;
230 while( noErr
== err
)
232 HFSUniStr255 volumeName
;
234 FSVolumeInfo volumeInfo
;
235 err
= FSGetVolumeInfo(0, volumeIndex
, NULL
, kFSVolInfoFlags
, &volumeInfo
, &volumeName
, &fsRef
);
238 wxString path
= wxMacFSRefToPath( &fsRef
) ;
239 wxString name
= wxMacHFSUniStrToString( &volumeName
) ;
241 if ( (volumeInfo
.flags
& kFSVolFlagSoftwareLockedMask
) || (volumeInfo
.flags
& kFSVolFlagHardwareLockedMask
) )
243 icon_ids
.Add(wxFileIconsTable::cdrom
);
247 icon_ids
.Add(wxFileIconsTable::drive
);
249 // todo other removable
257 #elif defined(__UNIX__)
260 icon_ids
.Add(wxFileIconsTable::computer
);
262 #error "Unsupported platform in wxGenericDirCtrl!"
264 wxASSERT_MSG( (paths
.GetCount() == names
.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
265 wxASSERT_MSG( (paths
.GetCount() == icon_ids
.GetCount()), wxT("Wrong number of icons for available drives."));
266 return paths
.GetCount();
269 // ----------------------------------------------------------------------------
270 // wxIsDriveAvailable
271 // ----------------------------------------------------------------------------
275 bool wxIsDriveAvailable(const wxString
& dirName
)
277 // FIXME_MGL - this method leads to hang up under Watcom for some reason
279 wxUnusedVar(dirName
);
281 if ( dirName
.Len() == 3 && dirName
[1u] == wxT(':') )
283 wxString
dirNameLower(dirName
.Lower());
284 // VS: always return true for removable media, since Win95 doesn't
285 // like it when MS-DOS app accesses empty floppy drive
286 return (dirNameLower
[0u] == wxT('a') ||
287 dirNameLower
[0u] == wxT('b') ||
288 wxDirExists(dirNameLower
));
295 #elif defined(__WINDOWS__) || defined(__OS2__)
297 int setdrive(int WXUNUSED_IN_WINCE(drive
))
301 #elif defined(__GNUWIN32__) && \
302 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
303 return _chdrive(drive
);
307 if (drive
< 1 || drive
> 31)
309 newdrive
[0] = (wxChar
)(wxT('A') + drive
- 1);
310 newdrive
[1] = wxT(':');
312 newdrive
[2] = wxT('\\');
313 newdrive
[3] = wxT('\0');
315 newdrive
[2] = wxT('\0');
317 #if defined(__WXMSW__)
318 if (::SetCurrentDirectory(newdrive
))
320 // VA doesn't know what LPSTR is and has its own set
321 if (!DosSetCurrentDir((PSZ
)newdrive
))
329 bool wxIsDriveAvailable(const wxString
& WXUNUSED_IN_WINCE(dirName
))
335 UINT errorMode
= SetErrorMode(SEM_FAILCRITICALERRORS
| SEM_NOOPENFILEERRORBOX
);
339 // Check if this is a root directory and if so,
340 // whether the drive is available.
341 if (dirName
.Len() == 3 && dirName
[(size_t)1] == wxT(':'))
343 wxString
dirNameLower(dirName
.Lower());
344 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
345 success
= wxDirExists(dirNameLower
);
348 // Avoid changing to drive since no media may be inserted.
349 if (dirNameLower
[(size_t)0] == 'a' || dirNameLower
[(size_t)0] == 'b')
352 int currentDrive
= _getdrive();
353 int thisDrive
= (int) (dirNameLower
[(size_t)0] - 'a' + 1) ;
354 int err
= setdrive( thisDrive
) ;
355 setdrive( currentDrive
);
364 (void) SetErrorMode(errorMode
);
370 #endif // __WINDOWS__ || __OS2__
372 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
378 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
379 // and sort regardless of case.
380 static int wxCMPFUNC_CONV
wxDirCtrlStringCompareFunction(const wxString
& strFirst
, const wxString
& strSecond
)
382 return strFirst
.CmpNoCase(strSecond
);
385 //-----------------------------------------------------------------------------
387 //-----------------------------------------------------------------------------
389 wxDirItemData::wxDirItemData(const wxString
& path
, const wxString
& name
,
394 /* Insert logic to detect hidden files here
395 * In UnixLand we just check whether the first char is a dot
396 * For FileNameFromPath read LastDirNameInThisPath ;-) */
397 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
399 m_isExpanded
= false;
403 void wxDirItemData::SetNewDirName(const wxString
& path
)
406 m_name
= wxFileNameFromPath(path
);
409 bool wxDirItemData::HasSubDirs() const
417 if ( !dir
.Open(m_path
) )
421 return dir
.HasSubDirs();
424 bool wxDirItemData::HasFiles(const wxString
& WXUNUSED(spec
)) const
432 if ( !dir
.Open(m_path
) )
436 return dir
.HasFiles();
439 //-----------------------------------------------------------------------------
441 //-----------------------------------------------------------------------------
444 #if wxUSE_EXTENDED_RTTI
445 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle
)
447 wxBEGIN_FLAGS( wxGenericDirCtrlStyle
)
448 // new style border flags, we put them first to
449 // use them for streaming out
450 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
451 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
452 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
453 wxFLAGS_MEMBER(wxBORDER_RAISED
)
454 wxFLAGS_MEMBER(wxBORDER_STATIC
)
455 wxFLAGS_MEMBER(wxBORDER_NONE
)
457 // old style border flags
458 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
459 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
460 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
461 wxFLAGS_MEMBER(wxRAISED_BORDER
)
462 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
463 wxFLAGS_MEMBER(wxBORDER
)
465 // standard window styles
466 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
467 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
468 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW
)
469 wxFLAGS_MEMBER(wxWANTS_CHARS
)
470 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE
)
471 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB
)
472 wxFLAGS_MEMBER(wxVSCROLL
)
473 wxFLAGS_MEMBER(wxHSCROLL
)
475 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY
)
476 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL
)
477 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST
)
478 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS
)
480 wxEND_FLAGS( wxGenericDirCtrlStyle
)
482 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl
, wxControl
,"wx/dirctrl.h")
484 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl
)
485 wxHIDE_PROPERTY( Children
)
486 wxPROPERTY( DefaultPath
, wxString
, SetDefaultPath
, GetDefaultPath
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
487 wxPROPERTY( Filter
, wxString
, SetFilter
, GetFilter
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
488 wxPROPERTY( DefaultFilter
, int , SetFilterIndex
, GetFilterIndex
, EMPTY_MACROVALUE
, 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
489 wxPROPERTY_FLAGS( WindowStyle
, wxGenericDirCtrlStyle
, long, SetWindowStyleFlag
, GetWindowStyleFlag
, EMPTY_MACROVALUE
, 0, wxT("Helpstring"), wxT("group") )
490 wxEND_PROPERTIES_TABLE()
492 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl
)
493 wxEND_HANDLERS_TABLE()
495 wxCONSTRUCTOR_8( wxGenericDirCtrl
, wxWindow
* , Parent
, wxWindowID
, Id
, wxString
, DefaultPath
,
496 wxPoint
, Position
, wxSize
, Size
, long , WindowStyle
, wxString
, Filter
, int , DefaultFilter
)
498 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl
, wxControl
)
501 BEGIN_EVENT_TABLE(wxGenericDirCtrl
, wxControl
)
502 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL
, wxGenericDirCtrl::OnExpandItem
)
503 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL
, wxGenericDirCtrl::OnCollapseItem
)
504 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnBeginEditItem
)
505 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL
, wxGenericDirCtrl::OnEndEditItem
)
506 EVT_SIZE (wxGenericDirCtrl::OnSize
)
509 wxGenericDirCtrl::wxGenericDirCtrl(void)
514 void wxGenericDirCtrl::ExpandRoot()
516 ExpandDir(m_rootId
); // automatically expand first level
518 // Expand and select the default path
519 if (!m_defaultPath
.empty())
521 ExpandPath(m_defaultPath
);
526 // On Unix, there's only one node under the (hidden) root node. It
527 // represents the / path, so the user would always have to expand it;
528 // let's do it ourselves
529 ExpandPath( wxT("/") );
534 bool wxGenericDirCtrl::Create(wxWindow
*parent
,
540 const wxString
& filter
,
542 const wxString
& name
)
544 if (!wxControl::Create(parent
, id
, pos
, size
, style
, wxDefaultValidator
, name
))
547 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
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
);
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
.GetLabel().empty()) ||
699 (event
.GetLabel() == _(".")) ||
700 (event
.GetLabel() == _("..")) ||
701 (event
.GetLabel().Find(wxT('/')) != wxNOT_FOUND
) ||
702 (event
.GetLabel().Find(wxT('\\')) != wxNOT_FOUND
) ||
703 (event
.GetLabel().Find(wxT('|')) != wxNOT_FOUND
))
705 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
711 wxTreeItemId id
= event
.GetItem();
712 wxDirItemData
*data
= (wxDirItemData
*)m_treeCtrl
->GetItemData( id
);
715 wxString
new_name( wxPathOnly( data
->m_path
) );
716 new_name
+= wxString(wxFILE_SEP_PATH
);
717 new_name
+= event
.GetLabel();
721 if (wxFileExists(new_name
))
723 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
728 if (wxRenameFile(data
->m_path
,new_name
))
730 data
->SetNewDirName( new_name
);
734 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
740 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent
&event
)
742 wxTreeItemId parentId
= event
.GetItem();
744 // VS: this is needed because the event handler is called from wxTreeCtrl
745 // ctor when wxTR_HIDE_ROOT was specified
747 if (!m_rootId
.IsOk())
749 m_rootId
= m_treeCtrl
->GetRootItem();
754 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent
&event
)
756 CollapseDir(event
.GetItem());
759 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId
)
763 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
764 if (!data
->m_isExpanded
)
767 data
->m_isExpanded
= false;
768 wxTreeItemIdValue cookie
;
769 /* Workaround because DeleteChildren has disapeared (why?) and
770 * CollapseAndReset doesn't work as advertised (deletes parent too) */
771 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
774 m_treeCtrl
->Delete(child
);
775 /* Not GetNextChild below, because the cookie mechanism can't
776 * handle disappearing children! */
777 child
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
779 if (parentId
!= m_treeCtrl
->GetRootItem())
780 m_treeCtrl
->Collapse(parentId
);
783 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId
)
785 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(parentId
);
787 if (data
->m_isExpanded
)
790 data
->m_isExpanded
= true;
792 if (parentId
== m_treeCtrl
->GetRootItem())
800 wxString search
,path
,filename
;
802 wxString
dirName(data
->m_path
);
804 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
805 // Check if this is a root directory and if so,
806 // whether the drive is avaiable.
807 if (!wxIsDriveAvailable(dirName
))
809 data
->m_isExpanded
= false;
810 //wxMessageBox(wxT("Sorry, this drive is not available."));
815 // This may take a longish time. Go to busy cursor
818 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
819 if (dirName
.Last() == ':')
820 dirName
+= wxString(wxFILE_SEP_PATH
);
824 wxArrayString filenames
;
827 wxString eachFilename
;
834 int style
= wxDIR_DIRS
;
835 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
836 if (d
.GetFirst(& eachFilename
, wxEmptyString
, style
))
840 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
842 dirs
.Add(eachFilename
);
845 while (d
.GetNext(&eachFilename
));
848 dirs
.Sort(wxDirCtrlStringCompareFunction
);
850 // Now do the filenames -- but only if we're allowed to
851 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
857 int style
= wxDIR_FILES
;
858 if (m_showHidden
) style
|= wxDIR_HIDDEN
;
859 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
860 wxStringTokenizer strTok
;
862 strTok
.SetString(m_currentFilterStr
,wxT(";"));
863 while(strTok
.HasMoreTokens())
865 curFilter
= strTok
.GetNextToken();
866 if (d
.GetFirst(& eachFilename
, curFilter
, style
))
870 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
872 filenames
.Add(eachFilename
);
875 while (d
.GetNext(& eachFilename
));
879 filenames
.Sort(wxDirCtrlStringCompareFunction
);
882 // Add the sorted dirs
884 for (i
= 0; i
< dirs
.Count(); i
++)
886 eachFilename
= dirs
[i
];
888 if (!wxEndsWithPathSeparator(path
))
889 path
+= wxString(wxFILE_SEP_PATH
);
890 path
+= eachFilename
;
892 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,true);
893 wxTreeItemId id
= AppendItem( parentId
, eachFilename
,
894 wxFileIconsTable::folder
, -1, dir_item
);
895 m_treeCtrl
->SetItemImage( id
, wxFileIconsTable::folder_open
,
896 wxTreeItemIcon_Expanded
);
898 // Has this got any children? If so, make it expandable.
899 // (There are two situations when a dir has children: either it
900 // has subdirectories or it contains files that weren't filtered
901 // out. The latter only applies to dirctrl with files.)
902 if ( dir_item
->HasSubDirs() ||
903 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0) &&
904 dir_item
->HasFiles(m_currentFilterStr
)) )
906 m_treeCtrl
->SetItemHasChildren(id
);
910 // Add the sorted filenames
911 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY
) == 0)
913 for (i
= 0; i
< filenames
.Count(); i
++)
915 eachFilename
= filenames
[i
];
917 if (!wxEndsWithPathSeparator(path
))
918 path
+= wxString(wxFILE_SEP_PATH
);
919 path
+= eachFilename
;
920 //path = dirName + wxString(wxT("/")) + eachFilename;
921 wxDirItemData
*dir_item
= new wxDirItemData(path
,eachFilename
,false);
922 int image_id
= wxFileIconsTable::file
;
923 if (eachFilename
.Find(wxT('.')) != wxNOT_FOUND
)
924 image_id
= wxTheFileIconsTable
->GetIconID(eachFilename
.AfterLast(wxT('.')));
925 (void) AppendItem( parentId
, eachFilename
, image_id
, -1, dir_item
);
930 void wxGenericDirCtrl::ReCreateTree()
932 CollapseDir(m_treeCtrl
->GetRootItem());
936 void wxGenericDirCtrl::CollapseTree()
938 wxTreeItemIdValue cookie
;
939 wxTreeItemId child
= m_treeCtrl
->GetFirstChild(m_rootId
, cookie
);
943 child
= m_treeCtrl
->GetNextChild(m_rootId
, cookie
);
947 // Find the child that matches the first part of 'path'.
948 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
949 // then the child for /usr is returned.
950 wxTreeItemId
wxGenericDirCtrl::FindChild(wxTreeItemId parentId
, const wxString
& path
, bool& done
)
952 wxString
path2(path
);
954 // Make sure all separators are as per the current platform
955 path2
.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH
));
956 path2
.Replace(wxT("/"), wxString(wxFILE_SEP_PATH
));
958 // Append a separator to foil bogus substring matching
959 path2
+= wxString(wxFILE_SEP_PATH
);
961 // In MSW or PM, case is not significant
962 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
966 wxTreeItemIdValue cookie
;
967 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(parentId
, cookie
);
968 while (childId
.IsOk())
970 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
972 if (data
&& !data
->m_path
.empty())
974 wxString
childPath(data
->m_path
);
975 if (!wxEndsWithPathSeparator(childPath
))
976 childPath
+= wxString(wxFILE_SEP_PATH
);
978 // In MSW and PM, case is not significant
979 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
980 childPath
.MakeLower();
983 if (childPath
.Len() <= path2
.Len())
985 wxString path3
= path2
.Mid(0, childPath
.Len());
986 if (childPath
== path3
)
988 if (path3
.Len() == path2
.Len())
997 childId
= m_treeCtrl
->GetNextChild(parentId
, cookie
);
999 wxTreeItemId invalid
;
1003 // Try to expand as much of the given path as possible,
1004 // and select the given tree item.
1005 bool wxGenericDirCtrl::ExpandPath(const wxString
& path
)
1008 wxTreeItemId id
= FindChild(m_rootId
, path
, done
);
1009 wxTreeItemId lastId
= id
; // The last non-zero id
1010 while (id
.IsOk() && !done
)
1014 id
= FindChild(id
, path
, done
);
1021 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(lastId
);
1024 m_treeCtrl
->Expand(lastId
);
1026 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST
) && data
->m_isDir
)
1028 // Find the first file in this directory
1029 wxTreeItemIdValue cookie
;
1030 wxTreeItemId childId
= m_treeCtrl
->GetFirstChild(lastId
, cookie
);
1031 bool selectedChild
= false;
1032 while (childId
.IsOk())
1034 data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(childId
);
1036 if (data
&& data
->m_path
!= wxEmptyString
&& !data
->m_isDir
)
1038 m_treeCtrl
->SelectItem(childId
);
1039 m_treeCtrl
->EnsureVisible(childId
);
1040 selectedChild
= true;
1043 childId
= m_treeCtrl
->GetNextChild(lastId
, cookie
);
1047 m_treeCtrl
->SelectItem(lastId
);
1048 m_treeCtrl
->EnsureVisible(lastId
);
1053 m_treeCtrl
->SelectItem(lastId
);
1054 m_treeCtrl
->EnsureVisible(lastId
);
1060 wxString
wxGenericDirCtrl::GetPath() const
1062 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1065 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1066 return data
->m_path
;
1069 return wxEmptyString
;
1072 wxString
wxGenericDirCtrl::GetFilePath() const
1074 wxTreeItemId id
= m_treeCtrl
->GetSelection();
1077 wxDirItemData
* data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1079 return wxEmptyString
;
1081 return data
->m_path
;
1084 return wxEmptyString
;
1087 void wxGenericDirCtrl::SetPath(const wxString
& path
)
1089 m_defaultPath
= path
;
1096 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id
, int dirFlags
, wxArrayString
& filenames
)
1098 wxDirItemData
*data
= (wxDirItemData
*) m_treeCtrl
->GetItemData(id
);
1100 // This may take a longish time. Go to busy cursor
1105 wxString search
,path
,filename
;
1107 wxString
dirName(data
->m_path
);
1109 #if defined(__WXMSW__) || defined(__OS2__)
1110 if (dirName
.Last() == ':')
1111 dirName
+= wxString(wxFILE_SEP_PATH
);
1115 wxString eachFilename
;
1122 if (d
.GetFirst(& eachFilename
, m_currentFilterStr
, dirFlags
))
1126 if ((eachFilename
!= wxT(".")) && (eachFilename
!= wxT("..")))
1128 filenames
.Add(eachFilename
);
1131 while (d
.GetNext(& eachFilename
)) ;
1137 void wxGenericDirCtrl::SetFilterIndex(int n
)
1139 m_currentFilter
= n
;
1142 if (ExtractWildcard(m_filter
, n
, f
, d
))
1143 m_currentFilterStr
= f
;
1146 m_currentFilterStr
= wxT("*");
1148 m_currentFilterStr
= wxT("*.*");
1152 void wxGenericDirCtrl::SetFilter(const wxString
& filter
)
1157 if (ExtractWildcard(m_filter
, m_currentFilter
, f
, d
))
1158 m_currentFilterStr
= f
;
1161 m_currentFilterStr
= wxT("*");
1163 m_currentFilterStr
= wxT("*.*");
1167 // Extract description and actual filter from overall filter string
1168 bool wxGenericDirCtrl::ExtractWildcard(const wxString
& filterStr
, int n
, wxString
& filter
, wxString
& description
)
1170 wxArrayString filters
, descriptions
;
1171 int count
= wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1172 if (count
> 0 && n
< count
)
1174 filter
= filters
[n
];
1175 description
= descriptions
[n
];
1182 #if WXWIN_COMPATIBILITY_2_4
1183 // Parses the global filter, returning the number of filters.
1184 // Returns 0 if none or if there's a problem.
1185 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1186 int wxGenericDirCtrl::ParseFilter(const wxString
& filterStr
, wxArrayString
& filters
, wxArrayString
& descriptions
)
1188 return wxParseCommonDialogsFilter(filterStr
, descriptions
, filters
);
1190 #endif // WXWIN_COMPATIBILITY_2_4
1192 void wxGenericDirCtrl::DoResize()
1194 wxSize sz
= GetClientSize();
1195 int verticalSpacing
= 3;
1199 if (m_filterListCtrl
)
1201 filterSz
= m_filterListCtrl
->GetSize();
1202 sz
.y
-= (filterSz
.y
+ verticalSpacing
);
1204 m_treeCtrl
->SetSize(0, 0, sz
.x
, sz
.y
);
1205 if (m_filterListCtrl
)
1207 m_filterListCtrl
->SetSize(0, sz
.y
+ verticalSpacing
, sz
.x
, filterSz
.y
);
1208 // Don't know why, but this needs refreshing after a resize (wxMSW)
1209 m_filterListCtrl
->Refresh();
1215 void wxGenericDirCtrl::OnSize(wxSizeEvent
& WXUNUSED(event
))
1220 wxTreeItemId
wxGenericDirCtrl::AppendItem (const wxTreeItemId
& parent
,
1221 const wxString
& text
,
1222 int image
, int selectedImage
,
1223 wxTreeItemData
* data
)
1225 wxTreeCtrl
*treeCtrl
= GetTreeCtrl ();
1227 wxASSERT (treeCtrl
);
1231 return treeCtrl
->AppendItem (parent
, text
, image
, selectedImage
, data
);
1235 return wxTreeItemId();
1240 //-----------------------------------------------------------------------------
1241 // wxDirFilterListCtrl
1242 //-----------------------------------------------------------------------------
1244 IMPLEMENT_CLASS(wxDirFilterListCtrl
, wxChoice
)
1246 BEGIN_EVENT_TABLE(wxDirFilterListCtrl
, wxChoice
)
1247 EVT_CHOICE(wxID_ANY
, wxDirFilterListCtrl::OnSelFilter
)
1250 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl
* parent
, const wxWindowID id
,
1256 return wxChoice::Create(parent
, id
, pos
, size
, 0, NULL
, style
);
1259 void wxDirFilterListCtrl::Init()
1264 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent
& WXUNUSED(event
))
1266 int sel
= GetSelection();
1268 wxString currentPath
= m_dirCtrl
->GetPath();
1270 m_dirCtrl
->SetFilterIndex(sel
);
1272 // If the filter has changed, the view is out of date, so
1273 // collapse the tree.
1274 m_dirCtrl
->ReCreateTree();
1276 // Try to restore the selection, or at least the directory
1277 m_dirCtrl
->ExpandPath(currentPath
);
1280 void wxDirFilterListCtrl::FillFilterList(const wxString
& filter
, int defaultFilter
)
1283 wxArrayString descriptions
, filters
;
1284 size_t n
= (size_t) wxParseCommonDialogsFilter(filter
, descriptions
, filters
);
1286 if (n
> 0 && defaultFilter
< (int) n
)
1288 for (size_t i
= 0; i
< n
; i
++)
1289 Append(descriptions
[i
]);
1290 SetSelection(defaultFilter
);
1293 #endif // wxUSE_DIRDLG
1295 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1297 // ----------------------------------------------------------------------------
1298 // wxFileIconsTable icons
1299 // ----------------------------------------------------------------------------
1302 /* Computer (c) Julian Smart */
1303 static const char * file_icons_tbl_computer_xpm
[] = {
1304 /* columns rows colors chars-per-pixel */
1366 #endif // GTK+ < 2.4
1368 // ----------------------------------------------------------------------------
1369 // wxFileIconsTable & friends
1370 // ----------------------------------------------------------------------------
1372 // global instance of a wxFileIconsTable
1373 wxFileIconsTable
* wxTheFileIconsTable
= (wxFileIconsTable
*)NULL
;
1375 // A module to allow icons table cleanup
1377 class wxFileIconsTableModule
: public wxModule
1379 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule
)
1381 wxFileIconsTableModule() {}
1382 bool OnInit() { wxTheFileIconsTable
= new wxFileIconsTable
; return true; }
1385 if (wxTheFileIconsTable
)
1387 delete wxTheFileIconsTable
;
1388 wxTheFileIconsTable
= NULL
;
1393 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule
, wxModule
)
1395 class wxFileIconEntry
: public wxObject
1398 wxFileIconEntry(int i
) { id
= i
; }
1403 wxFileIconsTable::wxFileIconsTable()
1406 m_smallImageList
= NULL
;
1409 wxFileIconsTable::~wxFileIconsTable()
1413 WX_CLEAR_HASH_TABLE(*m_HashTable
);
1416 if (m_smallImageList
) delete m_smallImageList
;
1419 // delayed initialization - wait until first use (wxArtProv not created yet)
1420 void wxFileIconsTable::Create()
1422 wxCHECK_RET(!m_smallImageList
&& !m_HashTable
, wxT("creating icons twice"));
1423 m_HashTable
= new wxHashTable(wxKEY_STRING
);
1424 m_smallImageList
= new wxImageList(16, 16);
1427 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER
,
1431 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN
,
1436 // GTK24 uses this icon in the file open dialog
1437 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1441 m_smallImageList
->Add(wxIcon(file_icons_tbl_computer_xpm
));
1444 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_HARDDISK
,
1448 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_CDROM
,
1452 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_FLOPPY
,
1456 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE
,
1460 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
,
1464 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == file
)
1466 m_smallImageList
->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
,
1469 delete m_HashTable
->Get(_T("exe"));
1470 m_HashTable
->Delete(_T("exe"));
1471 m_HashTable
->Put(_T("exe"), new wxFileIconEntry(executable
));
1473 /* else put into list by GetIconID
1474 (KDE defines application/x-executable for *.exe and has nice icon)
1478 wxImageList
*wxFileIconsTable::GetSmallImageList()
1480 if (!m_smallImageList
)
1483 return m_smallImageList
;
1486 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1487 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1488 // one icon and we won't resize it
1490 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
1492 const unsigned int size
= 16;
1494 wxImage
smallimg (size
, size
);
1495 unsigned char *p1
, *p2
, *ps
;
1496 unsigned char mr
= img
.GetMaskRed(),
1497 mg
= img
.GetMaskGreen(),
1498 mb
= img
.GetMaskBlue();
1501 unsigned sr
, sg
, sb
, smask
;
1503 p1
= img
.GetData(), p2
= img
.GetData() + 3 * size
*2, ps
= smallimg
.GetData();
1504 smallimg
.SetMaskColour(mr
, mr
, mr
);
1506 for (y
= 0; y
< size
; y
++)
1508 for (x
= 0; x
< size
; x
++)
1510 sr
= sg
= sb
= smask
= 0;
1511 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1512 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1515 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
1516 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
1519 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1520 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1523 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
1524 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
1529 ps
[0] = ps
[1] = ps
[2] = mr
;
1532 ps
[0] = (unsigned char)(sr
>> 2);
1533 ps
[1] = (unsigned char)(sg
>> 2);
1534 ps
[2] = (unsigned char)(sb
>> 2);
1538 p1
+= size
*2 * 3, p2
+= size
*2 * 3;
1541 return wxBitmap(smallimg
);
1544 // This function is currently not unused anymore
1546 // finds empty borders and return non-empty area of image:
1547 static wxImage
CutEmptyBorders(const wxImage
& img
)
1549 unsigned char mr
= img
.GetMaskRed(),
1550 mg
= img
.GetMaskGreen(),
1551 mb
= img
.GetMaskBlue();
1552 unsigned char *dt
= img
.GetData(), *dttmp
;
1553 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
1555 unsigned top
, bottom
, left
, right
, i
;
1558 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1559 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1561 for (empt
= true, top
= 0; empt
&& top
< h
; top
++)
1564 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1567 for (empt
= true, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
1569 MK_DTTMP(0, bottom
);
1570 for (i
= 0; i
< w
; i
++, dttmp
+=3)
1573 for (empt
= true, left
= 0; empt
&& left
< w
; left
++)
1576 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1579 for (empt
= true, right
= w
-1; empt
&& right
> left
; right
--)
1582 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
1585 top
--, left
--, bottom
++, right
++;
1587 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
1591 #endif // wxUSE_MIMETYPE
1593 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
1595 if (!m_smallImageList
)
1599 if (!extension
.empty())
1601 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
->Get(extension
);
1602 if (entry
) return (entry
-> id
);
1605 wxFileType
*ft
= (mime
.empty()) ?
1606 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
1607 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
1609 wxIconLocation iconLoc
;
1614 if ( ft
&& ft
->GetIcon(&iconLoc
) )
1616 ic
= wxIcon( iconLoc
);
1625 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1630 bmp
.CopyFromIcon(ic
);
1635 m_HashTable
->Put(extension
, new wxFileIconEntry(newid
));
1639 const unsigned int size
= 16;
1641 int id
= m_smallImageList
->GetImageCount();
1642 if ((bmp
.GetWidth() == (int) size
) && (bmp
.GetHeight() == (int) size
))
1644 m_smallImageList
->Add(bmp
);
1646 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1649 wxImage img
= bmp
.ConvertToImage();
1651 if ((img
.GetWidth() != size
*2) || (img
.GetHeight() != size
*2))
1652 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1653 m_smallImageList
->Add(CreateAntialiasedBitmap(img
.Rescale(size
*2, size
*2)));
1655 m_smallImageList
->Add(CreateAntialiasedBitmap(img
));
1657 #endif // wxUSE_IMAGE
1659 m_HashTable
->Put(extension
, new wxFileIconEntry(id
));
1662 #else // !wxUSE_MIMETYPE
1665 if (extension
== wxT("exe"))
1669 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1672 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG