1 //////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/filedlgg.cpp
3 // Purpose: wxGenericFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 // NOTE : it probably also supports MAC, untested
22 #if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__)
23 #error wxGenericFileDialog currently only supports Unix, win32 and DOS
29 #include "wx/settings.h"
31 #include "wx/msgdlg.h"
32 #include "wx/bmpbuttn.h"
33 #include "wx/checkbox.h"
36 #include "wx/textctrl.h"
37 #include "wx/choice.h"
38 #include "wx/stattext.h"
39 #include "wx/longlong.h"
41 #include "wx/tokenzr.h"
42 #include "wx/config.h"
43 #include "wx/imaglist.h"
45 #include "wx/artprov.h"
46 #include "wx/filefn.h"
47 #include "wx/file.h" // for wxS_IXXX constants only
48 #include "wx/filedlg.h" // wxFD_OPEN, wxFD_SAVE...
49 #include "wx/generic/filedlgg.h"
50 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
53 #include "wx/tooltip.h"
57 #include <sys/types.h>
70 #include "wx/msw/wrapwin.h"
71 #include "wx/msw/mslu.h"
82 #if defined(__UNIX__) || defined(__DOS__)
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
91 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long sortOrder
)
93 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
94 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
96 if (fd1
->GetFileName() == wxT(".."))
98 if (fd2
->GetFileName() == wxT(".."))
100 if (fd1
->IsDir() && !fd2
->IsDir())
102 if (fd2
->IsDir() && !fd1
->IsDir())
105 return sortOrder
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
109 int wxCALLBACK
wxFileDataSizeCompare(long data1
, long data2
, long sortOrder
)
111 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
112 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
114 if (fd1
->GetFileName() == wxT(".."))
116 if (fd2
->GetFileName() == wxT(".."))
118 if (fd1
->IsDir() && !fd2
->IsDir())
120 if (fd2
->IsDir() && !fd1
->IsDir())
122 if (fd1
->IsLink() && !fd2
->IsLink())
124 if (fd2
->IsLink() && !fd1
->IsLink())
127 return fd1
->GetSize() > fd2
->GetSize() ? sortOrder
: -sortOrder
;
131 int wxCALLBACK
wxFileDataTypeCompare(long data1
, long data2
, long sortOrder
)
133 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
134 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
136 if (fd1
->GetFileName() == wxT(".."))
138 if (fd2
->GetFileName() == wxT(".."))
140 if (fd1
->IsDir() && !fd2
->IsDir())
142 if (fd2
->IsDir() && !fd1
->IsDir())
144 if (fd1
->IsLink() && !fd2
->IsLink())
146 if (fd2
->IsLink() && !fd1
->IsLink())
149 return sortOrder
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
153 int wxCALLBACK
wxFileDataTimeCompare(long data1
, long data2
, long sortOrder
)
155 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
156 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
158 if (fd1
->GetFileName() == wxT(".."))
160 if (fd2
->GetFileName() == wxT(".."))
162 if (fd1
->IsDir() && !fd2
->IsDir())
164 if (fd2
->IsDir() && !fd1
->IsDir())
167 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? sortOrder
: -sortOrder
;
170 #if defined(__WXWINCE__)
171 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
172 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
173 #define IsTopMostDir(dir) (dir.empty())
175 #define IsTopMostDir(dir) (dir == wxT("/"))
178 // defined in src/generic/dirctrlg.cpp
179 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
185 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
188 m_fileName
= fileName
;
189 m_filePath
= filePath
;
196 void wxFileData::Init()
199 m_type
= wxFileData::is_file
;
200 m_image
= wxFileIconsTable::file
;
203 void wxFileData::Copy( const wxFileData
& fileData
)
205 m_fileName
= fileData
.GetFileName();
206 m_filePath
= fileData
.GetFilePath();
207 m_size
= fileData
.GetSize();
208 m_dateTime
= fileData
.GetDateTime();
209 m_permissions
= fileData
.GetPermissions();
210 m_type
= fileData
.GetType();
211 m_image
= fileData
.GetImageId();
214 void wxFileData::ReadData()
222 #if defined(__DOS__) || (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__OS2__)
223 // c:\.. is a drive don't stat it
224 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
230 #endif // __DOS__ || __WINDOWS__
236 DWORD fileAttribs
= GetFileAttributes(m_filePath
.fn_str());
237 m_type
|= (fileAttribs
& FILE_ATTRIBUTE_DIRECTORY
) != 0 ? is_dir
: 0;
240 wxSplitPath(m_filePath
, & p
, & f
, & ext
);
241 if (wxStricmp(ext
, wxT("exe")) == 0)
246 HANDLE fileHandle
= CreateFile(m_filePath
.fn_str(),
251 FILE_ATTRIBUTE_NORMAL
,
254 if (fileHandle
!= INVALID_HANDLE_VALUE
)
256 m_size
= GetFileSize(fileHandle
, 0);
257 CloseHandle(fileHandle
);
260 m_dateTime
= wxFileModificationTime(m_filePath
);
268 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
269 lstat( m_filePath
.fn_str(), &buff
);
270 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
272 // only translate to file charset if we don't go by our
273 // wxStat implementation
274 #ifndef wxNEED_WX_UNISTD_H
275 wxStat( m_filePath
.fn_str() , &buff
);
277 wxStat( m_filePath
, &buff
);
281 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
282 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
284 m_size
= buff
.st_size
;
286 m_dateTime
= buff
.st_mtime
;
290 #if defined(__UNIX__)
291 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
292 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
293 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
294 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
295 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
296 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
297 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
298 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
299 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
300 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
301 #elif defined(__WIN32__)
302 DWORD attribs
= ::GetFileAttributes(m_filePath
.c_str());
303 if (attribs
!= (DWORD
)-1)
305 m_permissions
.Printf(_T("%c%c%c%c"),
306 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
307 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
308 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
309 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
313 // try to get a better icon
314 if (m_image
== wxFileIconsTable::file
)
316 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
318 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
321 m_image
= wxFileIconsTable::executable
;
326 wxString
wxFileData::GetFileType() const
334 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
335 return m_fileName
.AfterLast(wxT('.'));
337 return wxEmptyString
;
340 wxString
wxFileData::GetModificationTime() const
342 // want time as 01:02 so they line up nicely, no %r in WIN32
343 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
346 wxString
wxFileData::GetHint() const
348 wxString s
= m_filePath
;
358 s
+= wxString::Format(_("%ld bytes"),
359 wxLongLong(m_size
).ToString().c_str());
365 s
<< GetModificationTime()
373 wxString
wxFileData::GetEntry( fileListFieldType num
) const
383 if (!IsDir() && !IsLink() && !IsDrive())
384 s
= wxLongLong(m_size
).ToString();
393 s
= GetModificationTime();
396 #if defined(__UNIX__) || defined(__WIN32__)
400 #endif // defined(__UNIX__) || defined(__WIN32__)
403 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
409 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
411 m_fileName
= fileName
;
412 m_filePath
= filePath
;
415 void wxFileData::MakeItem( wxListItem
&item
)
417 item
.m_text
= m_fileName
;
418 item
.ClearAttributes();
420 item
.SetTextColour(*wxRED
);
422 item
.SetTextColour(*wxBLUE
);
424 item
.m_image
= m_image
;
428 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
430 item
.SetTextColour(dg
);
432 item
.m_data
= wxPtrToUInt(this);
435 //-----------------------------------------------------------------------------
437 //-----------------------------------------------------------------------------
439 static bool ignoreChanges
= false;
441 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
443 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
444 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
445 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
446 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
447 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
451 wxFileCtrl::wxFileCtrl()
453 m_showHidden
= false;
455 m_sort_field
= wxFileData::FileList_Name
;
458 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
460 const wxString
& wild
,
465 const wxValidator
&validator
,
466 const wxString
&name
)
467 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
470 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
472 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
474 m_showHidden
= showHidden
;
477 m_sort_field
= wxFileData::FileList_Name
;
479 m_dirName
= wxT("*");
481 if (style
& wxLC_REPORT
)
482 ChangeToReportMode();
485 void wxFileCtrl::ChangeToListMode()
488 SetSingleStyle( wxLC_LIST
);
492 void wxFileCtrl::ChangeToReportMode()
495 SetSingleStyle( wxLC_REPORT
);
497 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
498 // don't hardcode since mm/dd is dd/mm elsewhere
500 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
501 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
502 GetTextExtent(txt
, &w
, &h
);
504 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
505 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
506 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
507 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
508 #if defined(__UNIX__)
509 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
510 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
511 #elif defined(__WIN32__)
512 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
513 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
519 void wxFileCtrl::ChangeToSmallIconMode()
522 SetSingleStyle( wxLC_SMALL_ICON
);
526 void wxFileCtrl::ShowHidden( bool show
)
532 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
535 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
536 fd
->MakeItem( item
);
537 long my_style
= GetWindowStyleFlag();
538 if (my_style
& wxLC_REPORT
)
540 ret
= InsertItem( item
);
541 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
542 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
544 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
546 ret
= InsertItem( item
);
551 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
553 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
554 wxCHECK_RET(fd
, wxT("invalid filedata"));
558 SetItemText(item
, fd
->GetFileName());
559 SetItemImage(item
, fd
->GetImageId());
561 if (GetWindowStyleFlag() & wxLC_REPORT
)
563 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
564 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
568 void wxFileCtrl::UpdateFiles()
570 // don't do anything before ShowModal() call which sets m_dirName
571 if ( m_dirName
== wxT("*") )
574 wxBusyCursor bcur
; // this may take a while...
582 #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__)
583 if ( IsTopMostDir(m_dirName
) )
585 wxArrayString names
, paths
;
587 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
589 for (n
=0; n
<count
; n
++)
591 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
592 if (Add(fd
, item
) != -1)
599 #endif // defined(__DOS__) || defined(__WINDOWS__)
602 if ( !IsTopMostDir(m_dirName
) && !m_dirName
.empty() )
604 wxString
p(wxPathOnly(m_dirName
));
605 #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__)
606 if (p
.empty()) p
= wxT("/");
608 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
609 if (Add(fd
, item
) != -1)
615 wxString
dirname(m_dirName
);
616 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
617 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
618 dirname
<< wxT('\\');
619 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
622 dirname
= wxFILE_SEP_PATH
;
627 if ( dir
.IsOpened() )
629 wxString
dirPrefix(dirname
);
630 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
631 dirPrefix
+= wxFILE_SEP_PATH
;
633 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
638 // Get the directories first (not matched against wildcards):
639 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
642 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
643 if (Add(fd
, item
) != -1)
648 cont
= dir
.GetNext(&f
);
651 // Tokenize the wildcard string, so we can handle more than 1
652 // search pattern in a wildcard.
653 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
654 while ( tokenWild
.HasMoreTokens() )
656 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
657 wxDIR_FILES
| hiddenFlag
);
660 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
661 if (Add(fd
, item
) != -1)
666 cont
= dir
.GetNext(&f
);
672 SortItems(m_sort_field
, m_sort_foward
);
675 void wxFileCtrl::SetWild( const wxString
&wild
)
677 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
684 void wxFileCtrl::MakeDir()
686 wxString
new_name( _("NewName") );
687 wxString
path( m_dirName
);
688 path
+= wxFILE_SEP_PATH
;
690 if (wxFileExists(path
))
692 // try NewName0, NewName1 etc.
695 new_name
= _("NewName");
697 num
.Printf( wxT("%d"), i
);
701 path
+= wxFILE_SEP_PATH
;
704 } while (wxFileExists(path
));
710 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
715 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
719 long id
= Add( fd
, item
);
723 SortItems(m_sort_field
, m_sort_foward
);
724 id
= FindItem( 0, wxPtrToUInt(fd
) );
732 void wxFileCtrl::GoToParentDir()
734 if (!IsTopMostDir(m_dirName
))
736 size_t len
= m_dirName
.Len();
737 if (wxEndsWithPathSeparator(m_dirName
))
738 m_dirName
.Remove( len
-1, 1 );
739 wxString
fname( wxFileNameFromPath(m_dirName
) );
740 m_dirName
= wxPathOnly( m_dirName
);
741 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
742 if (!m_dirName
.empty())
744 if (m_dirName
.Last() == wxT('.'))
745 m_dirName
= wxEmptyString
;
747 #elif defined(__UNIX__)
748 if (m_dirName
.empty())
749 m_dirName
= wxT("/");
752 long id
= FindItem( 0, fname
);
753 if (id
!= wxNOT_FOUND
)
755 ignoreChanges
= true;
756 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
758 ignoreChanges
= false;
763 void wxFileCtrl::GoToHomeDir()
765 wxString s
= wxGetUserHome( wxString() );
769 void wxFileCtrl::GoToDir( const wxString
&dir
)
771 if (!wxDirExists(dir
)) return;
776 ignoreChanges
= true;
777 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
778 ignoreChanges
= false;
783 void wxFileCtrl::FreeItemData(wxListItem
& item
)
787 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
794 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
796 FreeItemData(event
.m_item
);
799 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
804 void wxFileCtrl::FreeAllItemsData()
807 item
.m_mask
= wxLIST_MASK_DATA
;
809 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
810 while ( item
.m_itemId
!= -1 )
814 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
818 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
820 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
823 if ((event
.GetLabel().empty()) ||
824 (event
.GetLabel() == _(".")) ||
825 (event
.GetLabel() == _("..")) ||
826 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
828 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
834 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
835 new_name
+= wxFILE_SEP_PATH
;
836 new_name
+= event
.GetLabel();
840 if (wxFileExists(new_name
))
842 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
847 if (wxRenameFile(fd
->GetFilePath(),new_name
))
849 fd
->SetNewName( new_name
, event
.GetLabel() );
851 ignoreChanges
= true;
852 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
853 ignoreChanges
= false;
855 UpdateItem( event
.GetItem() );
856 EnsureVisible( event
.GetItem() );
860 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
866 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
868 int col
= event
.GetColumn();
872 case wxFileData::FileList_Name
:
873 case wxFileData::FileList_Size
:
874 case wxFileData::FileList_Type
:
875 case wxFileData::FileList_Time
: break;
879 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
880 m_sort_foward
= !m_sort_foward
;
882 m_sort_field
= (wxFileData::fileListFieldType
)col
;
884 SortItems(m_sort_field
, m_sort_foward
);
887 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool forward
)
889 m_sort_field
= field
;
890 m_sort_foward
= forward
;
891 const long sort_dir
= forward
? 1 : -1;
893 switch (m_sort_field
)
895 case wxFileData::FileList_Size
:
896 wxListCtrl::SortItems(wxFileDataSizeCompare
, sort_dir
);
899 case wxFileData::FileList_Type
:
900 wxListCtrl::SortItems(wxFileDataTypeCompare
, sort_dir
);
903 case wxFileData::FileList_Time
:
904 wxListCtrl::SortItems(wxFileDataTimeCompare
, sort_dir
);
907 case wxFileData::FileList_Name
:
909 wxListCtrl::SortItems(wxFileDataNameCompare
, sort_dir
);
914 wxFileCtrl::~wxFileCtrl()
916 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
917 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
918 // the destruction of the wxFileCtrl we need to free any data here:
922 //-----------------------------------------------------------------------------
923 // wxGenericFileDialog
924 //-----------------------------------------------------------------------------
926 #define ID_LIST_MODE (wxID_FILEDLGG )
927 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
928 #define ID_UP_DIR (wxID_FILEDLGG + 5)
929 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
930 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
931 #define ID_CHOICE (wxID_FILEDLGG + 8)
932 #define ID_TEXT (wxID_FILEDLGG + 9)
933 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
934 #define ID_CHECK (wxID_FILEDLGG + 12)
936 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
938 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
939 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
940 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
941 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
942 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
943 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
944 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
945 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
946 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
947 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
948 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
949 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
950 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
953 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
954 bool wxGenericFileDialog::ms_lastShowHidden
= false;
956 void wxGenericFileDialog::Init()
958 m_bypassGenericImpl
= false;
965 m_upDirButton
= NULL
;
966 m_newDirButton
= NULL
;
969 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
970 const wxString
& message
,
971 const wxString
& defaultDir
,
972 const wxString
& defaultFile
,
973 const wxString
& wildCard
,
977 const wxString
& name
,
978 bool bypassGenericImpl
) : wxFileDialogBase()
981 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, sz
, name
, bypassGenericImpl
);
984 bool wxGenericFileDialog::Create( wxWindow
*parent
,
985 const wxString
& message
,
986 const wxString
& defaultDir
,
987 const wxString
& defaultFile
,
988 const wxString
& wildCard
,
992 const wxString
& name
,
993 bool bypassGenericImpl
)
995 m_bypassGenericImpl
= bypassGenericImpl
;
997 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
998 wildCard
, style
, pos
, sz
, name
))
1003 if (m_bypassGenericImpl
)
1006 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, sz
,
1007 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
, name
1013 ignoreChanges
= true;
1015 if (wxConfig::Get(false))
1017 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1019 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1020 &ms_lastShowHidden
);
1023 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
1027 m_dir
= wxFILE_SEP_PATH
;
1030 size_t len
= m_dir
.Len();
1031 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
1032 m_dir
.Remove( len
-1, 1 );
1035 m_path
+= wxFILE_SEP_PATH
;
1036 m_path
+= defaultFile
;
1037 m_filterExtension
= wxEmptyString
;
1041 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1043 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
1045 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1047 wxBitmapButton
*but
;
1049 but
= new wxBitmapButton(this, ID_LIST_MODE
,
1050 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
1052 but
->SetToolTip( _("View files as a list view") );
1054 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1056 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
1057 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
1059 but
->SetToolTip( _("View files as a detailed view") );
1061 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1063 buttonsizer
->Add( 30, 5, 1 );
1065 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
1066 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
1068 m_upDirButton
->SetToolTip( _("Go to parent directory") );
1070 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
1072 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1073 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
1074 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
1076 but
->SetToolTip( _("Go to home directory") );
1078 buttonsizer
->Add( but
, 0, wxALL
, 5);
1080 buttonsizer
->Add( 20, 20 );
1083 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
1084 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
1086 m_newDirButton
->SetToolTip( _("Create new directory") );
1088 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1091 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1093 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1095 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1097 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1098 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1099 staticsizer
->Add( m_static
, 1 );
1100 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1102 long style2
= ms_lastViewStyle
;
1103 if ( !HasFlag(wxFD_MULTIPLE
) )
1104 style2
|= wxLC_SINGLE_SEL
;
1107 style2
|= wxSIMPLE_BORDER
;
1109 style2
|= wxSUNKEN_BORDER
;
1112 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1113 wxEmptyString
, ms_lastShowHidden
,
1114 wxDefaultPosition
, wxSize(540,200),
1119 // PDAs have a different screen layout
1120 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1122 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1123 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1124 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1125 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1128 m_choice
= new wxChoice( this, ID_CHOICE
);
1129 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1131 wxSizer
*bsizer
= CreateButtonSizer( wxOK
|wxCANCEL
, false, 5 );
1132 if(bsizer
->GetChildren().GetCount() > 0 )
1134 mainsizer
->Add( bsizer
, 0, wxEXPAND
| wxALL
, 5 );
1143 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1145 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1146 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1147 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1148 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1149 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1151 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1152 m_choice
= new wxChoice( this, ID_CHOICE
);
1153 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1154 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1155 m_check
->SetValue( ms_lastShowHidden
);
1156 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1157 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1158 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1161 SetWildcard(wildCard
);
1163 SetAutoLayout( true );
1164 SetSizer( mainsizer
);
1168 mainsizer
->Fit( this );
1169 mainsizer
->SetSizeHints( this );
1176 ignoreChanges
= false;
1181 wxGenericFileDialog::~wxGenericFileDialog()
1183 ignoreChanges
= true;
1185 if (!m_bypassGenericImpl
)
1187 if (wxConfig::Get(false))
1189 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1191 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1195 const int count
= m_choice
->GetCount();
1196 for ( int i
= 0; i
< count
; i
++ )
1198 delete (wxString
*)m_choice
->GetClientData(i
);
1203 int wxGenericFileDialog::ShowModal()
1205 ignoreChanges
= true;
1207 m_list
->GoToDir(m_dir
);
1209 m_text
->SetValue(m_fileName
);
1211 ignoreChanges
= false;
1213 return wxDialog::ShowModal();
1216 bool wxGenericFileDialog::Show( bool show
)
1218 // Called by ShowModal, so don't repeate the update
1222 m_list
->GoToDir(m_dir
);
1224 m_text
->SetValue(m_fileName
);
1228 return wxDialog::Show( show
);
1231 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1233 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1234 m_list
->SetWild( *str
);
1235 m_filterIndex
= filterindex
;
1236 if ( str
->Left(2) == wxT("*.") )
1238 m_filterExtension
= str
->Mid(1);
1239 if (m_filterExtension
== _T(".*"))
1240 m_filterExtension
.clear();
1244 m_filterExtension
.clear();
1248 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1250 wxFileDialogBase::SetWildcard(wildCard
);
1252 wxArrayString wildDescriptions
, wildFilters
;
1253 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1256 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1258 const size_t countOld
= m_choice
->GetCount();
1260 for ( n
= 0; n
< countOld
; n
++ )
1262 delete (wxString
*)m_choice
->GetClientData(n
);
1265 for ( n
= 0; n
< count
; n
++ )
1267 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1270 SetFilterIndex( 0 );
1273 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1275 m_choice
->SetSelection( filterindex
);
1277 DoSetFilterIndex(filterindex
);
1280 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1282 DoSetFilterIndex((int)event
.GetInt());
1285 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1287 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1290 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1292 HandleAction( event
.m_item
.m_text
);
1295 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1297 HandleAction( m_text
->GetValue() );
1300 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1304 // Clear selections. Otherwise when the user types in a value they may
1305 // not get the file whose name they typed.
1306 if (m_list
->GetSelectedItemCount() > 0)
1308 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1309 wxLIST_STATE_SELECTED
);
1310 while ( item
!= -1 )
1312 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1313 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1319 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1321 static bool inSelected
= false;
1327 wxString
filename( event
.m_item
.m_text
);
1330 // No double-click on most WinCE devices, so do action immediately.
1331 HandleAction( filename
);
1333 if (filename
== wxT(".."))
1339 wxString dir
= m_list
->GetDir();
1340 if (!IsTopMostDir(dir
))
1341 dir
+= wxFILE_SEP_PATH
;
1343 if (wxDirExists(dir
))
1349 ignoreChanges
= true;
1350 m_text
->SetValue( filename
);
1351 ignoreChanges
= false;
1356 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1361 wxString
filename( fn
);
1362 if (filename
.empty())
1365 EndModal(wxID_CANCEL
);
1369 if (filename
== wxT(".")) return;
1371 wxString dir
= m_list
->GetDir();
1373 // "some/place/" means they want to chdir not try to load "place"
1374 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1376 filename
= filename
.RemoveLast();
1378 if (filename
== wxT(".."))
1380 ignoreChanges
= true;
1381 m_list
->GoToParentDir();
1384 ignoreChanges
= false;
1389 if (filename
== wxT("~"))
1391 ignoreChanges
= true;
1392 m_list
->GoToHomeDir();
1395 ignoreChanges
= false;
1399 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1401 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1405 if (!HasFlag(wxFD_SAVE
))
1407 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1408 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1410 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1412 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1415 m_list
->SetWild( filename
);
1420 if (!IsTopMostDir(dir
))
1421 dir
+= wxFILE_SEP_PATH
;
1422 if (!wxIsAbsolutePath(filename
))
1428 if (wxDirExists(filename
))
1430 ignoreChanges
= true;
1431 m_list
->GoToDir( filename
);
1433 ignoreChanges
= false;
1437 // they really wanted a dir, but it doesn't exist
1440 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1441 wxOK
| wxICON_ERROR
);
1445 // append the default extension to the filename if it doesn't have any
1447 // VZ: the logic of testing for !wxFileExists() only for the open file
1448 // dialog is not entirely clear to me, why don't we allow saving to a
1449 // file without extension as well?
1450 if ( !HasFlag(wxFD_OPEN
) || !wxFileExists(filename
) )
1452 filename
= AppendExtension(filename
, m_filterExtension
);
1455 // check that the file [doesn't] exist if necessary
1456 if ( HasFlag(wxFD_SAVE
) && HasFlag(wxFD_OVERWRITE_PROMPT
) &&
1457 wxFileExists( filename
) )
1460 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1462 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1465 else if ( HasFlag(wxFD_OPEN
) && HasFlag(wxFD_FILE_MUST_EXIST
) &&
1466 !wxFileExists(filename
) )
1468 wxMessageBox(_("Please choose an existing file."), _("Error"),
1469 wxOK
| wxICON_ERROR
);
1472 SetPath( filename
);
1474 // change to the directory where the user went if asked
1475 if ( HasFlag(wxFD_CHANGE_DIR
) )
1478 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1480 if ( cwd
!= wxGetCwd() )
1482 wxSetWorkingDirectory(cwd
);
1489 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1491 HandleAction( m_text
->GetValue() );
1494 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1496 ignoreChanges
= true;
1497 m_list
->ChangeToListMode();
1498 ms_lastViewStyle
= wxLC_LIST
;
1500 ignoreChanges
= false;
1503 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1505 ignoreChanges
= true;
1506 m_list
->ChangeToReportMode();
1507 ms_lastViewStyle
= wxLC_REPORT
;
1509 ignoreChanges
= false;
1512 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1514 ignoreChanges
= true;
1515 m_list
->GoToParentDir();
1518 ignoreChanges
= false;
1521 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1523 ignoreChanges
= true;
1524 m_list
->GoToHomeDir();
1527 ignoreChanges
= false;
1530 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1532 ignoreChanges
= true;
1536 ignoreChanges
= false;
1539 void wxGenericFileDialog::SetPath( const wxString
& path
)
1541 // not only set the full path but also update filename and dir
1546 m_path
= wxFILE_SEP_PATH
;
1549 if ( !path
.empty() )
1552 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1555 m_fileName
+= wxT(".");
1561 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1564 if (m_list
->GetSelectedItemCount() == 0)
1566 paths
.Add( GetPath() );
1570 paths
.Alloc( m_list
->GetSelectedItemCount() );
1572 wxString dir
= m_list
->GetDir();
1574 if (dir
!= wxT("/"))
1577 if (dir
!= wxT("/") && dir
!= wxT("\\"))
1579 dir
+= wxFILE_SEP_PATH
;
1582 item
.m_mask
= wxLIST_MASK_TEXT
;
1584 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1585 while ( item
.m_itemId
!= -1 )
1587 m_list
->GetItem( item
);
1588 paths
.Add( dir
+ item
.m_text
);
1589 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1590 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1594 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1597 if (m_list
->GetSelectedItemCount() == 0)
1599 files
.Add( GetFilename() );
1602 files
.Alloc( m_list
->GetSelectedItemCount() );
1605 item
.m_mask
= wxLIST_MASK_TEXT
;
1607 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1608 while ( item
.m_itemId
!= -1 )
1610 m_list
->GetItem( item
);
1611 files
.Add( item
.m_text
);
1612 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1613 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1617 void wxGenericFileDialog::UpdateControls()
1619 wxString dir
= m_list
->GetDir();
1620 m_static
->SetLabel(dir
);
1622 bool enable
= !IsTopMostDir(dir
);
1623 m_upDirButton
->Enable(enable
);
1625 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1626 m_newDirButton
->Enable(enable
);
1627 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1630 #ifdef USE_GENERIC_FILEDIALOG
1632 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
1634 #endif // USE_GENERIC_FILEDIALOG
1636 #endif // wxUSE_FILEDLG