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
28 #include "wx/msw/wrapwin.h"
32 #include "wx/settings.h"
34 #include "wx/msgdlg.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/checkbox.h"
37 #include "wx/choice.h"
38 #include "wx/stattext.h"
39 #include "wx/textctrl.h"
41 #include "wx/filedlg.h" // wxFD_OPEN, wxFD_SAVE...
44 #include "wx/longlong.h"
45 #include "wx/tokenzr.h"
46 #include "wx/config.h"
47 #include "wx/imaglist.h"
49 #include "wx/artprov.h"
50 #include "wx/filefn.h"
51 #include "wx/file.h" // for wxS_IXXX constants only
52 #include "wx/generic/filedlgg.h"
53 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
56 #include "wx/tooltip.h"
60 #include <sys/types.h>
73 #include "wx/msw/mslu.h"
84 #if defined(__UNIX__) || defined(__DOS__)
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
93 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long sortOrder
)
95 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
96 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
98 if (fd1
->GetFileName() == wxT(".."))
100 if (fd2
->GetFileName() == wxT(".."))
102 if (fd1
->IsDir() && !fd2
->IsDir())
104 if (fd2
->IsDir() && !fd1
->IsDir())
107 return sortOrder
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
111 int wxCALLBACK
wxFileDataSizeCompare(long data1
, long data2
, long sortOrder
)
113 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
114 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
116 if (fd1
->GetFileName() == wxT(".."))
118 if (fd2
->GetFileName() == wxT(".."))
120 if (fd1
->IsDir() && !fd2
->IsDir())
122 if (fd2
->IsDir() && !fd1
->IsDir())
124 if (fd1
->IsLink() && !fd2
->IsLink())
126 if (fd2
->IsLink() && !fd1
->IsLink())
129 return fd1
->GetSize() > fd2
->GetSize() ? sortOrder
: -sortOrder
;
133 int wxCALLBACK
wxFileDataTypeCompare(long data1
, long data2
, long sortOrder
)
135 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
136 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
138 if (fd1
->GetFileName() == wxT(".."))
140 if (fd2
->GetFileName() == wxT(".."))
142 if (fd1
->IsDir() && !fd2
->IsDir())
144 if (fd2
->IsDir() && !fd1
->IsDir())
146 if (fd1
->IsLink() && !fd2
->IsLink())
148 if (fd2
->IsLink() && !fd1
->IsLink())
151 return sortOrder
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
155 int wxCALLBACK
wxFileDataTimeCompare(long data1
, long data2
, long sortOrder
)
157 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
158 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
160 if (fd1
->GetFileName() == wxT(".."))
162 if (fd2
->GetFileName() == wxT(".."))
164 if (fd1
->IsDir() && !fd2
->IsDir())
166 if (fd2
->IsDir() && !fd1
->IsDir())
169 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? sortOrder
: -sortOrder
;
172 #if defined(__WXWINCE__)
173 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
174 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
175 #define IsTopMostDir(dir) (dir.empty())
177 #define IsTopMostDir(dir) (dir == wxT("/"))
180 // defined in src/generic/dirctrlg.cpp
181 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
183 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
187 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
190 m_fileName
= fileName
;
191 m_filePath
= filePath
;
198 void wxFileData::Init()
201 m_type
= wxFileData::is_file
;
202 m_image
= wxFileIconsTable::file
;
205 void wxFileData::Copy( const wxFileData
& fileData
)
207 m_fileName
= fileData
.GetFileName();
208 m_filePath
= fileData
.GetFilePath();
209 m_size
= fileData
.GetSize();
210 m_dateTime
= fileData
.GetDateTime();
211 m_permissions
= fileData
.GetPermissions();
212 m_type
= fileData
.GetType();
213 m_image
= fileData
.GetImageId();
216 void wxFileData::ReadData()
224 #if defined(__DOS__) || (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__OS2__)
225 // c:\.. is a drive don't stat it
226 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
232 #endif // __DOS__ || __WINDOWS__
238 DWORD fileAttribs
= GetFileAttributes(m_filePath
.fn_str());
239 m_type
|= (fileAttribs
& FILE_ATTRIBUTE_DIRECTORY
) != 0 ? is_dir
: 0;
242 wxSplitPath(m_filePath
, & p
, & f
, & ext
);
243 if (wxStricmp(ext
, wxT("exe")) == 0)
248 HANDLE fileHandle
= CreateFile(m_filePath
.fn_str(),
253 FILE_ATTRIBUTE_NORMAL
,
256 if (fileHandle
!= INVALID_HANDLE_VALUE
)
258 m_size
= GetFileSize(fileHandle
, 0);
259 CloseHandle(fileHandle
);
262 m_dateTime
= wxFileModificationTime(m_filePath
);
270 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
271 lstat( m_filePath
.fn_str(), &buff
);
272 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
274 // only translate to file charset if we don't go by our
275 // wxStat implementation
276 #ifndef wxNEED_WX_UNISTD_H
277 wxStat( m_filePath
.fn_str() , &buff
);
279 wxStat( m_filePath
, &buff
);
283 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
284 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
286 m_size
= buff
.st_size
;
288 m_dateTime
= buff
.st_mtime
;
292 #if defined(__UNIX__)
293 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
294 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
295 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
296 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
297 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
298 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
299 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
300 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
301 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
302 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
303 #elif defined(__WIN32__)
304 DWORD attribs
= ::GetFileAttributes(m_filePath
.c_str());
305 if (attribs
!= (DWORD
)-1)
307 m_permissions
.Printf(_T("%c%c%c%c"),
308 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
309 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
310 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
311 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
315 // try to get a better icon
316 if (m_image
== wxFileIconsTable::file
)
318 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
320 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
323 m_image
= wxFileIconsTable::executable
;
328 wxString
wxFileData::GetFileType() const
336 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
337 return m_fileName
.AfterLast(wxT('.'));
339 return wxEmptyString
;
342 wxString
wxFileData::GetModificationTime() const
344 // want time as 01:02 so they line up nicely, no %r in WIN32
345 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
348 wxString
wxFileData::GetHint() const
350 wxString s
= m_filePath
;
360 s
+= wxString::Format(wxPLURAL("%ld byte", "%ld bytes", m_size
),
361 wxLongLong(m_size
).ToString().c_str());
367 s
<< GetModificationTime()
375 wxString
wxFileData::GetEntry( fileListFieldType num
) const
385 if (!IsDir() && !IsLink() && !IsDrive())
386 s
= wxLongLong(m_size
).ToString();
395 s
= GetModificationTime();
398 #if defined(__UNIX__) || defined(__WIN32__)
402 #endif // defined(__UNIX__) || defined(__WIN32__)
405 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
411 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
413 m_fileName
= fileName
;
414 m_filePath
= filePath
;
417 void wxFileData::MakeItem( wxListItem
&item
)
419 item
.m_text
= m_fileName
;
420 item
.ClearAttributes();
422 item
.SetTextColour(*wxRED
);
424 item
.SetTextColour(*wxBLUE
);
426 item
.m_image
= m_image
;
430 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
432 item
.SetTextColour(dg
);
434 item
.m_data
= wxPtrToUInt(this);
437 //-----------------------------------------------------------------------------
439 //-----------------------------------------------------------------------------
441 static bool ignoreChanges
= false;
443 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
445 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
446 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
447 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
448 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
449 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
453 wxFileCtrl::wxFileCtrl()
455 m_showHidden
= false;
457 m_sort_field
= wxFileData::FileList_Name
;
460 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
462 const wxString
& wild
,
467 const wxValidator
&validator
,
468 const wxString
&name
)
469 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
472 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
474 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
476 m_showHidden
= showHidden
;
479 m_sort_field
= wxFileData::FileList_Name
;
481 m_dirName
= wxT("*");
483 if (style
& wxLC_REPORT
)
484 ChangeToReportMode();
487 void wxFileCtrl::ChangeToListMode()
490 SetSingleStyle( wxLC_LIST
);
494 void wxFileCtrl::ChangeToReportMode()
497 SetSingleStyle( wxLC_REPORT
);
499 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
500 // don't hardcode since mm/dd is dd/mm elsewhere
502 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
503 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
504 GetTextExtent(txt
, &w
, &h
);
506 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
507 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
508 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
509 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
510 #if defined(__UNIX__)
511 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
512 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
513 #elif defined(__WIN32__)
514 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
515 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
521 void wxFileCtrl::ChangeToSmallIconMode()
524 SetSingleStyle( wxLC_SMALL_ICON
);
528 void wxFileCtrl::ShowHidden( bool show
)
534 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
537 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
538 fd
->MakeItem( item
);
539 long my_style
= GetWindowStyleFlag();
540 if (my_style
& wxLC_REPORT
)
542 ret
= InsertItem( item
);
543 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
544 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
546 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
548 ret
= InsertItem( item
);
553 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
555 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
556 wxCHECK_RET(fd
, wxT("invalid filedata"));
560 SetItemText(item
, fd
->GetFileName());
561 SetItemImage(item
, fd
->GetImageId());
563 if (GetWindowStyleFlag() & wxLC_REPORT
)
565 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
566 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
570 void wxFileCtrl::UpdateFiles()
572 // don't do anything before ShowModal() call which sets m_dirName
573 if ( m_dirName
== wxT("*") )
576 wxBusyCursor bcur
; // this may take a while...
584 #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__)
585 if ( IsTopMostDir(m_dirName
) )
587 wxArrayString names
, paths
;
589 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
591 for (n
=0; n
<count
; n
++)
593 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
594 if (Add(fd
, item
) != -1)
601 #endif // defined(__DOS__) || defined(__WINDOWS__)
604 if ( !IsTopMostDir(m_dirName
) && !m_dirName
.empty() )
606 wxString
p(wxPathOnly(m_dirName
));
607 #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__)
608 if (p
.empty()) p
= wxT("/");
610 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
611 if (Add(fd
, item
) != -1)
617 wxString
dirname(m_dirName
);
618 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
619 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
620 dirname
<< wxT('\\');
621 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
624 dirname
= wxFILE_SEP_PATH
;
629 if ( dir
.IsOpened() )
631 wxString
dirPrefix(dirname
);
632 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
633 dirPrefix
+= wxFILE_SEP_PATH
;
635 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
640 // Get the directories first (not matched against wildcards):
641 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
644 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
645 if (Add(fd
, item
) != -1)
650 cont
= dir
.GetNext(&f
);
653 // Tokenize the wildcard string, so we can handle more than 1
654 // search pattern in a wildcard.
655 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
656 while ( tokenWild
.HasMoreTokens() )
658 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
659 wxDIR_FILES
| hiddenFlag
);
662 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
663 if (Add(fd
, item
) != -1)
668 cont
= dir
.GetNext(&f
);
674 SortItems(m_sort_field
, m_sort_foward
);
677 void wxFileCtrl::SetWild( const wxString
&wild
)
679 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
686 void wxFileCtrl::MakeDir()
688 wxString
new_name( _("NewName") );
689 wxString
path( m_dirName
);
690 path
+= wxFILE_SEP_PATH
;
692 if (wxFileExists(path
))
694 // try NewName0, NewName1 etc.
697 new_name
= _("NewName");
699 num
.Printf( wxT("%d"), i
);
703 path
+= wxFILE_SEP_PATH
;
706 } while (wxFileExists(path
));
712 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
717 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
721 long id
= Add( fd
, item
);
725 SortItems(m_sort_field
, m_sort_foward
);
726 id
= FindItem( 0, wxPtrToUInt(fd
) );
734 void wxFileCtrl::GoToParentDir()
736 if (!IsTopMostDir(m_dirName
))
738 size_t len
= m_dirName
.length();
739 if (wxEndsWithPathSeparator(m_dirName
))
740 m_dirName
.Remove( len
-1, 1 );
741 wxString
fname( wxFileNameFromPath(m_dirName
) );
742 m_dirName
= wxPathOnly( m_dirName
);
743 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
744 if (!m_dirName
.empty())
746 if (m_dirName
.Last() == wxT('.'))
747 m_dirName
= wxEmptyString
;
749 #elif defined(__UNIX__)
750 if (m_dirName
.empty())
751 m_dirName
= wxT("/");
754 long id
= FindItem( 0, fname
);
755 if (id
!= wxNOT_FOUND
)
757 ignoreChanges
= true;
758 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
760 ignoreChanges
= false;
765 void wxFileCtrl::GoToHomeDir()
767 wxString s
= wxGetUserHome( wxString() );
771 void wxFileCtrl::GoToDir( const wxString
&dir
)
773 if (!wxDirExists(dir
)) return;
778 ignoreChanges
= true;
779 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
780 ignoreChanges
= false;
785 void wxFileCtrl::FreeItemData(wxListItem
& item
)
789 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
796 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
798 FreeItemData(event
.m_item
);
801 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
806 void wxFileCtrl::FreeAllItemsData()
809 item
.m_mask
= wxLIST_MASK_DATA
;
811 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
812 while ( item
.m_itemId
!= -1 )
816 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
820 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
822 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
825 if ((event
.GetLabel().empty()) ||
826 (event
.GetLabel() == wxT(".")) ||
827 (event
.GetLabel() == wxT("..")) ||
828 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
830 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
836 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
837 new_name
+= wxFILE_SEP_PATH
;
838 new_name
+= event
.GetLabel();
842 if (wxFileExists(new_name
))
844 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
849 if (wxRenameFile(fd
->GetFilePath(),new_name
))
851 fd
->SetNewName( new_name
, event
.GetLabel() );
853 ignoreChanges
= true;
854 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
855 ignoreChanges
= false;
857 UpdateItem( event
.GetItem() );
858 EnsureVisible( event
.GetItem() );
862 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
868 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
870 int col
= event
.GetColumn();
874 case wxFileData::FileList_Name
:
875 case wxFileData::FileList_Size
:
876 case wxFileData::FileList_Type
:
877 case wxFileData::FileList_Time
: break;
881 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
882 m_sort_foward
= !m_sort_foward
;
884 m_sort_field
= (wxFileData::fileListFieldType
)col
;
886 SortItems(m_sort_field
, m_sort_foward
);
889 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool forward
)
891 m_sort_field
= field
;
892 m_sort_foward
= forward
;
893 const long sort_dir
= forward
? 1 : -1;
895 switch (m_sort_field
)
897 case wxFileData::FileList_Size
:
898 wxListCtrl::SortItems(wxFileDataSizeCompare
, sort_dir
);
901 case wxFileData::FileList_Type
:
902 wxListCtrl::SortItems(wxFileDataTypeCompare
, sort_dir
);
905 case wxFileData::FileList_Time
:
906 wxListCtrl::SortItems(wxFileDataTimeCompare
, sort_dir
);
909 case wxFileData::FileList_Name
:
911 wxListCtrl::SortItems(wxFileDataNameCompare
, sort_dir
);
916 wxFileCtrl::~wxFileCtrl()
918 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
919 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
920 // the destruction of the wxFileCtrl we need to free any data here:
924 //-----------------------------------------------------------------------------
925 // wxGenericFileDialog
926 //-----------------------------------------------------------------------------
928 #define ID_LIST_MODE (wxID_FILEDLGG )
929 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
930 #define ID_UP_DIR (wxID_FILEDLGG + 5)
931 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
932 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
933 #define ID_CHOICE (wxID_FILEDLGG + 8)
934 #define ID_TEXT (wxID_FILEDLGG + 9)
935 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
936 #define ID_CHECK (wxID_FILEDLGG + 12)
938 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
940 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
941 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
942 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
943 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
944 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
945 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
946 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
947 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
948 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
949 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
950 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
951 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
952 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
955 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
956 bool wxGenericFileDialog::ms_lastShowHidden
= false;
958 void wxGenericFileDialog::Init()
960 m_bypassGenericImpl
= false;
967 m_upDirButton
= NULL
;
968 m_newDirButton
= NULL
;
971 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
972 const wxString
& message
,
973 const wxString
& defaultDir
,
974 const wxString
& defaultFile
,
975 const wxString
& wildCard
,
979 const wxString
& name
,
980 bool bypassGenericImpl
) : wxFileDialogBase()
983 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, sz
, name
, bypassGenericImpl
);
986 bool wxGenericFileDialog::Create( wxWindow
*parent
,
987 const wxString
& message
,
988 const wxString
& defaultDir
,
989 const wxString
& defaultFile
,
990 const wxString
& wildCard
,
994 const wxString
& name
,
995 bool bypassGenericImpl
)
997 m_bypassGenericImpl
= bypassGenericImpl
;
999 parent
= GetParentForModalDialog(parent
);
1001 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
1002 wildCard
, style
, pos
, sz
, name
))
1007 if (m_bypassGenericImpl
)
1010 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, sz
,
1011 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
| style
, name
1017 ignoreChanges
= true;
1020 if (wxConfig::Get(false))
1022 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1024 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1025 &ms_lastShowHidden
);
1029 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
1033 m_dir
= wxFILE_SEP_PATH
;
1036 size_t len
= m_dir
.length();
1037 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
1038 m_dir
.Remove( len
-1, 1 );
1041 m_path
+= wxFILE_SEP_PATH
;
1042 m_path
+= defaultFile
;
1043 m_filterExtension
= wxEmptyString
;
1047 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1049 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
1051 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1053 wxBitmapButton
*but
;
1055 but
= new wxBitmapButton(this, ID_LIST_MODE
,
1056 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
1058 but
->SetToolTip( _("View files as a list view") );
1060 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1062 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
1063 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
1065 but
->SetToolTip( _("View files as a detailed view") );
1067 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1069 buttonsizer
->Add( 30, 5, 1 );
1071 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
1072 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
1074 m_upDirButton
->SetToolTip( _("Go to parent directory") );
1076 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
1078 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1079 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
1080 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
1082 but
->SetToolTip( _("Go to home directory") );
1084 buttonsizer
->Add( but
, 0, wxALL
, 5);
1086 buttonsizer
->Add( 20, 20 );
1089 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
1090 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
1092 m_newDirButton
->SetToolTip( _("Create new directory") );
1094 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1097 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1099 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1101 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1103 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1104 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1105 staticsizer
->Add( m_static
, 1 );
1106 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1108 long style2
= ms_lastViewStyle
;
1109 if ( !HasFdFlag(wxFD_MULTIPLE
) )
1110 style2
|= wxLC_SINGLE_SEL
;
1113 style2
|= wxSIMPLE_BORDER
;
1115 style2
|= wxSUNKEN_BORDER
;
1118 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1119 wxEmptyString
, ms_lastShowHidden
,
1120 wxDefaultPosition
, wxSize(540,200),
1123 m_text
= new wxTextCtrl(this, ID_TEXT
, m_fileName
,
1124 wxDefaultPosition
, wxDefaultSize
,
1125 wxTE_PROCESS_ENTER
);
1126 m_choice
= new wxChoice(this, ID_CHOICE
);
1130 // PDAs have a different screen layout
1131 mainsizer
->Add(m_list
, wxSizerFlags(1).Expand().HorzBorder());
1133 wxBoxSizer
*textsizer
= new wxBoxSizer(wxHORIZONTAL
);
1134 textsizer
->Add(m_text
, wxSizerFlags(1).Centre().Border());
1135 mainsizer
->Add(textsizer
, wxSizerFlags().Expand());
1138 textsizer
->Add(m_choice
, wxSizerFlags(1).Centre().Border());
1140 wxSizer
*bsizer
= CreateButtonSizer(wxOK
| wxCANCEL
);
1142 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().Border());
1146 mainsizer
->Add(m_list
, wxSizerFlags(1).Expand().DoubleHorzBorder());
1148 wxBoxSizer
*textsizer
= new wxBoxSizer(wxHORIZONTAL
);
1149 textsizer
->Add(m_text
, wxSizerFlags(1).Centre().
1150 DoubleBorder(wxLEFT
| wxRIGHT
| wxTOP
));
1151 textsizer
->Add(new wxButton(this, wxID_OK
), wxSizerFlags().Centre().
1152 DoubleBorder(wxLEFT
| wxRIGHT
| wxTOP
));
1153 mainsizer
->Add(textsizer
, wxSizerFlags().Expand());
1155 wxSizerFlags flagsCentre
;
1156 flagsCentre
.Centre().DoubleBorder();
1158 wxBoxSizer
*choicesizer
= new wxBoxSizer(wxHORIZONTAL
);
1159 choicesizer
->Add(m_choice
, wxSizerFlags(flagsCentre
).Proportion(1));
1161 m_check
= new wxCheckBox(this, ID_CHECK
, _("Show &hidden files"));
1162 m_check
->SetValue(ms_lastShowHidden
);
1164 choicesizer
->Add(m_check
, flagsCentre
);
1165 choicesizer
->Add(new wxButton(this, wxID_CANCEL
), flagsCentre
);
1166 mainsizer
->Add(choicesizer
, wxSizerFlags().Expand());
1169 SetWildcard(wildCard
);
1171 SetAutoLayout( true );
1172 SetSizer( mainsizer
);
1176 mainsizer
->Fit( this );
1177 mainsizer
->SetSizeHints( this );
1184 ignoreChanges
= false;
1189 wxGenericFileDialog::~wxGenericFileDialog()
1191 ignoreChanges
= true;
1193 if (!m_bypassGenericImpl
)
1196 if (wxConfig::Get(false))
1198 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1200 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1205 const int count
= m_choice
->GetCount();
1206 for ( int i
= 0; i
< count
; i
++ )
1208 delete (wxString
*)m_choice
->GetClientData(i
);
1213 int wxGenericFileDialog::ShowModal()
1215 ignoreChanges
= true;
1217 m_list
->GoToDir(m_dir
);
1219 m_text
->SetValue(m_fileName
);
1221 ignoreChanges
= false;
1223 return wxDialog::ShowModal();
1226 bool wxGenericFileDialog::Show( bool show
)
1228 // Called by ShowModal, so don't repeate the update
1232 m_list
->GoToDir(m_dir
);
1234 m_text
->SetValue(m_fileName
);
1238 return wxDialog::Show( show
);
1241 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1243 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1244 m_list
->SetWild( *str
);
1245 m_filterIndex
= filterindex
;
1246 if ( str
->Left(2) == wxT("*.") )
1248 m_filterExtension
= str
->Mid(1);
1249 if (m_filterExtension
== _T(".*"))
1250 m_filterExtension
.clear();
1254 m_filterExtension
.clear();
1258 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1260 wxFileDialogBase::SetWildcard(wildCard
);
1262 wxArrayString wildDescriptions
, wildFilters
;
1263 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1266 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1268 const size_t countOld
= m_choice
->GetCount();
1270 for ( n
= 0; n
< countOld
; n
++ )
1272 delete (wxString
*)m_choice
->GetClientData(n
);
1275 for ( n
= 0; n
< count
; n
++ )
1277 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1280 SetFilterIndex( 0 );
1283 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1285 m_choice
->SetSelection( filterindex
);
1287 DoSetFilterIndex(filterindex
);
1290 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1292 DoSetFilterIndex((int)event
.GetInt());
1295 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1297 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1300 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1302 HandleAction( event
.m_item
.m_text
);
1305 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1307 HandleAction( m_text
->GetValue() );
1310 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1314 // Clear selections. Otherwise when the user types in a value they may
1315 // not get the file whose name they typed.
1316 if (m_list
->GetSelectedItemCount() > 0)
1318 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1319 wxLIST_STATE_SELECTED
);
1320 while ( item
!= -1 )
1322 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1323 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1329 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1331 static bool inSelected
= false;
1337 wxString
filename( event
.m_item
.m_text
);
1340 // No double-click on most WinCE devices, so do action immediately.
1341 HandleAction( filename
);
1343 if (filename
== wxT(".."))
1349 wxString dir
= m_list
->GetDir();
1350 if (!IsTopMostDir(dir
))
1351 dir
+= wxFILE_SEP_PATH
;
1353 if (wxDirExists(dir
))
1359 ignoreChanges
= true;
1360 m_text
->SetValue( filename
);
1361 ignoreChanges
= false;
1366 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1371 wxString
filename( fn
);
1372 if (filename
.empty())
1375 EndModal(wxID_CANCEL
);
1379 if (filename
== wxT(".")) return;
1381 wxString dir
= m_list
->GetDir();
1383 // "some/place/" means they want to chdir not try to load "place"
1384 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1386 filename
= filename
.RemoveLast();
1388 if (filename
== wxT(".."))
1390 ignoreChanges
= true;
1391 m_list
->GoToParentDir();
1394 ignoreChanges
= false;
1399 if (filename
== wxT("~"))
1401 ignoreChanges
= true;
1402 m_list
->GoToHomeDir();
1405 ignoreChanges
= false;
1409 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1411 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1415 if (!HasFdFlag(wxFD_SAVE
))
1417 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1418 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1420 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1422 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1425 m_list
->SetWild( filename
);
1430 if (!IsTopMostDir(dir
))
1431 dir
+= wxFILE_SEP_PATH
;
1432 if (!wxIsAbsolutePath(filename
))
1438 if (wxDirExists(filename
))
1440 ignoreChanges
= true;
1441 m_list
->GoToDir( filename
);
1443 ignoreChanges
= false;
1447 // they really wanted a dir, but it doesn't exist
1450 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1451 wxOK
| wxICON_ERROR
);
1455 // append the default extension to the filename if it doesn't have any
1457 // VZ: the logic of testing for !wxFileExists() only for the open file
1458 // dialog is not entirely clear to me, why don't we allow saving to a
1459 // file without extension as well?
1460 if ( !HasFdFlag(wxFD_OPEN
) || !wxFileExists(filename
) )
1462 filename
= AppendExtension(filename
, m_filterExtension
);
1465 // check that the file [doesn't] exist if necessary
1466 if ( HasFdFlag(wxFD_SAVE
) && HasFdFlag(wxFD_OVERWRITE_PROMPT
) &&
1467 wxFileExists( filename
) )
1470 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1472 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1475 else if ( HasFdFlag(wxFD_OPEN
) && HasFdFlag(wxFD_FILE_MUST_EXIST
) &&
1476 !wxFileExists(filename
) )
1478 wxMessageBox(_("Please choose an existing file."), _("Error"),
1479 wxOK
| wxICON_ERROR
);
1484 SetPath( filename
);
1486 // change to the directory where the user went if asked
1487 if ( HasFdFlag(wxFD_CHANGE_DIR
) )
1490 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1492 if ( cwd
!= wxGetCwd() )
1494 wxSetWorkingDirectory(cwd
);
1501 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1503 HandleAction( m_text
->GetValue() );
1506 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1508 ignoreChanges
= true;
1509 m_list
->ChangeToListMode();
1510 ms_lastViewStyle
= wxLC_LIST
;
1512 ignoreChanges
= false;
1515 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1517 ignoreChanges
= true;
1518 m_list
->ChangeToReportMode();
1519 ms_lastViewStyle
= wxLC_REPORT
;
1521 ignoreChanges
= false;
1524 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1526 ignoreChanges
= true;
1527 m_list
->GoToParentDir();
1530 ignoreChanges
= false;
1533 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1535 ignoreChanges
= true;
1536 m_list
->GoToHomeDir();
1539 ignoreChanges
= false;
1542 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1544 ignoreChanges
= true;
1548 ignoreChanges
= false;
1551 void wxGenericFileDialog::SetPath( const wxString
& path
)
1553 // not only set the full path but also update filename and dir
1558 m_path
= wxFILE_SEP_PATH
;
1561 if ( !path
.empty() )
1564 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1567 m_fileName
+= wxT(".");
1573 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1576 if (m_list
->GetSelectedItemCount() == 0)
1578 paths
.Add( GetPath() );
1582 paths
.Alloc( m_list
->GetSelectedItemCount() );
1584 wxString dir
= m_list
->GetDir();
1586 if (dir
!= wxT("/"))
1589 if (dir
!= wxT("/") && dir
!= wxT("\\"))
1591 dir
+= wxFILE_SEP_PATH
;
1594 item
.m_mask
= wxLIST_MASK_TEXT
;
1596 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1597 while ( item
.m_itemId
!= -1 )
1599 m_list
->GetItem( item
);
1600 paths
.Add( dir
+ item
.m_text
);
1601 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1602 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1606 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1609 if (m_list
->GetSelectedItemCount() == 0)
1611 files
.Add( GetFilename() );
1614 files
.Alloc( m_list
->GetSelectedItemCount() );
1617 item
.m_mask
= wxLIST_MASK_TEXT
;
1619 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1620 while ( item
.m_itemId
!= -1 )
1622 m_list
->GetItem( item
);
1623 files
.Add( item
.m_text
);
1624 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1625 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1629 void wxGenericFileDialog::UpdateControls()
1631 wxString dir
= m_list
->GetDir();
1632 m_static
->SetLabel(dir
);
1634 bool enable
= !IsTopMostDir(dir
);
1635 m_upDirButton
->Enable(enable
);
1637 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1638 m_newDirButton
->Enable(enable
);
1639 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1642 #ifdef wxHAS_GENERIC_FILEDIALOG
1644 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
1646 #endif // wxHAS_GENERIC_FILEDIALOG
1648 #endif // wxUSE_FILEDLG