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
26 #include "wx/generic/filedlgg.h"
33 #include "wx/checkbox.h"
34 #include "wx/textctrl.h"
35 #include "wx/choice.h"
36 #include "wx/checkbox.h"
37 #include "wx/stattext.h"
40 #include "wx/longlong.h"
41 #include "wx/msgdlg.h"
43 #include "wx/bmpbuttn.h"
44 #include "wx/tokenzr.h"
45 #include "wx/config.h"
46 #include "wx/imaglist.h"
48 #include "wx/artprov.h"
49 #include "wx/settings.h"
50 #include "wx/filefn.h"
51 #include "wx/file.h" // for wxS_IXXX constants only
52 #include "wx/filedlg.h" // wxOPEN, wxSAVE...
53 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
56 #include "wx/tooltip.h"
60 #include <sys/types.h>
73 #include "wx/msw/wrapwin.h"
74 #include "wx/msw/mslu.h"
85 #if defined(__UNIX__) || defined(__DOS__)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
94 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long sortOrder
)
96 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
97 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
99 if (fd1
->GetFileName() == wxT(".."))
101 if (fd2
->GetFileName() == wxT(".."))
103 if (fd1
->IsDir() && !fd2
->IsDir())
105 if (fd2
->IsDir() && !fd1
->IsDir())
108 return sortOrder
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
112 int wxCALLBACK
wxFileDataSizeCompare(long data1
, long data2
, long sortOrder
)
114 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
115 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
117 if (fd1
->GetFileName() == wxT(".."))
119 if (fd2
->GetFileName() == wxT(".."))
121 if (fd1
->IsDir() && !fd2
->IsDir())
123 if (fd2
->IsDir() && !fd1
->IsDir())
125 if (fd1
->IsLink() && !fd2
->IsLink())
127 if (fd2
->IsLink() && !fd1
->IsLink())
130 return fd1
->GetSize() > fd2
->GetSize() ? sortOrder
: -sortOrder
;
134 int wxCALLBACK
wxFileDataTypeCompare(long data1
, long data2
, long sortOrder
)
136 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
137 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
139 if (fd1
->GetFileName() == wxT(".."))
141 if (fd2
->GetFileName() == wxT(".."))
143 if (fd1
->IsDir() && !fd2
->IsDir())
145 if (fd2
->IsDir() && !fd1
->IsDir())
147 if (fd1
->IsLink() && !fd2
->IsLink())
149 if (fd2
->IsLink() && !fd1
->IsLink())
152 return sortOrder
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
156 int wxCALLBACK
wxFileDataTimeCompare(long data1
, long data2
, long sortOrder
)
158 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
159 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
161 if (fd1
->GetFileName() == wxT(".."))
163 if (fd2
->GetFileName() == wxT(".."))
165 if (fd1
->IsDir() && !fd2
->IsDir())
167 if (fd2
->IsDir() && !fd1
->IsDir())
170 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? sortOrder
: -sortOrder
;
173 #if defined(__WXWINCE__)
174 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
175 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
176 #define IsTopMostDir(dir) (dir.empty())
178 #define IsTopMostDir(dir) (dir == wxT("/"))
181 // defined in src/generic/dirctrlg.cpp
182 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
184 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
188 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
191 m_fileName
= fileName
;
192 m_filePath
= filePath
;
199 void wxFileData::Init()
202 m_type
= wxFileData::is_file
;
203 m_image
= wxFileIconsTable::file
;
206 void wxFileData::Copy( const wxFileData
& fileData
)
208 m_fileName
= fileData
.GetFileName();
209 m_filePath
= fileData
.GetFilePath();
210 m_size
= fileData
.GetSize();
211 m_dateTime
= fileData
.GetDateTime();
212 m_permissions
= fileData
.GetPermissions();
213 m_type
= fileData
.GetType();
214 m_image
= fileData
.GetImageId();
217 void wxFileData::ReadData()
225 #if defined(__DOS__) || (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__OS2__)
226 // c:\.. is a drive don't stat it
227 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
233 #endif // __DOS__ || __WINDOWS__
239 DWORD fileAttribs
= GetFileAttributes(m_filePath
.fn_str());
240 m_type
|= (fileAttribs
& FILE_ATTRIBUTE_DIRECTORY
) != 0 ? is_dir
: 0;
243 wxSplitPath(m_filePath
, & p
, & f
, & ext
);
244 if (wxStricmp(ext
, wxT("exe")) == 0)
249 HANDLE fileHandle
= CreateFile(m_filePath
.fn_str(),
254 FILE_ATTRIBUTE_NORMAL
,
257 if (fileHandle
!= INVALID_HANDLE_VALUE
)
259 m_size
= GetFileSize(fileHandle
, 0);
260 CloseHandle(fileHandle
);
263 m_dateTime
= wxFileModificationTime(m_filePath
);
271 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
272 lstat( m_filePath
.fn_str(), &buff
);
273 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
275 // only translate to file charset if we don't go by our
276 // wxStat implementation
277 #ifndef wxNEED_WX_UNISTD_H
278 wxStat( m_filePath
.fn_str() , &buff
);
280 wxStat( m_filePath
, &buff
);
284 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
285 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
287 m_size
= buff
.st_size
;
289 m_dateTime
= buff
.st_mtime
;
293 #if defined(__UNIX__)
294 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
295 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
296 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
297 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
298 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
299 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
300 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
301 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
302 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
303 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
304 #elif defined(__WIN32__)
305 DWORD attribs
= ::GetFileAttributes(m_filePath
.c_str());
306 if (attribs
!= (DWORD
)-1)
308 m_permissions
.Printf(_T("%c%c%c%c"),
309 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
310 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
311 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
312 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
316 // try to get a better icon
317 if (m_image
== wxFileIconsTable::file
)
319 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
321 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
324 m_image
= wxFileIconsTable::executable
;
329 wxString
wxFileData::GetFileType() const
337 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
338 return m_fileName
.AfterLast(wxT('.'));
340 return wxEmptyString
;
343 wxString
wxFileData::GetModificationTime() const
345 // want time as 01:02 so they line up nicely, no %r in WIN32
346 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
349 wxString
wxFileData::GetHint() const
351 wxString s
= m_filePath
;
361 s
+= wxString::Format(_("%ld bytes"),
362 wxLongLong(m_size
).ToString().c_str());
368 s
<< GetModificationTime()
376 wxString
wxFileData::GetEntry( fileListFieldType num
) const
386 if (!IsDir() && !IsLink() && !IsDrive())
387 s
= wxLongLong(m_size
).ToString();
396 s
= GetModificationTime();
399 #if defined(__UNIX__) || defined(__WIN32__)
403 #endif // defined(__UNIX__) || defined(__WIN32__)
406 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
412 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
414 m_fileName
= fileName
;
415 m_filePath
= filePath
;
418 void wxFileData::MakeItem( wxListItem
&item
)
420 item
.m_text
= m_fileName
;
421 item
.ClearAttributes();
423 item
.SetTextColour(*wxRED
);
425 item
.SetTextColour(*wxBLUE
);
427 item
.m_image
= m_image
;
431 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
433 item
.SetTextColour(dg
);
435 item
.m_data
= wxPtrToUInt(this);
438 //-----------------------------------------------------------------------------
440 //-----------------------------------------------------------------------------
442 static bool ignoreChanges
= false;
444 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
446 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
447 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
448 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
449 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
450 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
454 wxFileCtrl::wxFileCtrl()
456 m_showHidden
= false;
458 m_sort_field
= wxFileData::FileList_Name
;
461 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
463 const wxString
& wild
,
468 const wxValidator
&validator
,
469 const wxString
&name
)
470 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
473 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
475 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
477 m_showHidden
= showHidden
;
480 m_sort_field
= wxFileData::FileList_Name
;
482 m_dirName
= wxT("*");
484 if (style
& wxLC_REPORT
)
485 ChangeToReportMode();
488 void wxFileCtrl::ChangeToListMode()
491 SetSingleStyle( wxLC_LIST
);
495 void wxFileCtrl::ChangeToReportMode()
498 SetSingleStyle( wxLC_REPORT
);
500 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
501 // don't hardcode since mm/dd is dd/mm elsewhere
503 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
504 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
505 GetTextExtent(txt
, &w
, &h
);
507 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
508 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
509 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
510 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
511 #if defined(__UNIX__)
512 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
513 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
514 #elif defined(__WIN32__)
515 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
516 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
522 void wxFileCtrl::ChangeToSmallIconMode()
525 SetSingleStyle( wxLC_SMALL_ICON
);
529 void wxFileCtrl::ShowHidden( bool show
)
535 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
538 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
539 fd
->MakeItem( item
);
540 long my_style
= GetWindowStyleFlag();
541 if (my_style
& wxLC_REPORT
)
543 ret
= InsertItem( item
);
544 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
545 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
547 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
549 ret
= InsertItem( item
);
554 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
556 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
557 wxCHECK_RET(fd
, wxT("invalid filedata"));
561 SetItemText(item
, fd
->GetFileName());
562 SetItemImage(item
, fd
->GetImageId());
564 if (GetWindowStyleFlag() & wxLC_REPORT
)
566 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
567 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
571 void wxFileCtrl::UpdateFiles()
573 // don't do anything before ShowModal() call which sets m_dirName
574 if ( m_dirName
== wxT("*") )
577 wxBusyCursor bcur
; // this may take a while...
585 #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__)
586 if ( IsTopMostDir(m_dirName
) )
588 wxArrayString names
, paths
;
590 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
592 for (n
=0; n
<count
; n
++)
594 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
595 if (Add(fd
, item
) != -1)
602 #endif // defined(__DOS__) || defined(__WINDOWS__)
605 if ( !IsTopMostDir(m_dirName
) && !m_dirName
.empty() )
607 wxString
p(wxPathOnly(m_dirName
));
608 #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__)
609 if (p
.empty()) p
= wxT("/");
611 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
612 if (Add(fd
, item
) != -1)
618 wxString
dirname(m_dirName
);
619 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
620 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
621 dirname
<< wxT('\\');
622 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
625 dirname
= wxFILE_SEP_PATH
;
630 if ( dir
.IsOpened() )
632 wxString
dirPrefix(dirname
);
633 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
634 dirPrefix
+= wxFILE_SEP_PATH
;
636 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
641 // Get the directories first (not matched against wildcards):
642 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
645 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
646 if (Add(fd
, item
) != -1)
651 cont
= dir
.GetNext(&f
);
654 // Tokenize the wildcard string, so we can handle more than 1
655 // search pattern in a wildcard.
656 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
657 while ( tokenWild
.HasMoreTokens() )
659 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
660 wxDIR_FILES
| hiddenFlag
);
663 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
664 if (Add(fd
, item
) != -1)
669 cont
= dir
.GetNext(&f
);
675 SortItems(m_sort_field
, m_sort_foward
);
678 void wxFileCtrl::SetWild( const wxString
&wild
)
680 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
687 void wxFileCtrl::MakeDir()
689 wxString
new_name( _("NewName") );
690 wxString
path( m_dirName
);
691 path
+= wxFILE_SEP_PATH
;
693 if (wxFileExists(path
))
695 // try NewName0, NewName1 etc.
698 new_name
= _("NewName");
700 num
.Printf( wxT("%d"), i
);
704 path
+= wxFILE_SEP_PATH
;
707 } while (wxFileExists(path
));
713 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
718 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
722 long id
= Add( fd
, item
);
726 SortItems(m_sort_field
, m_sort_foward
);
727 id
= FindItem( 0, wxPtrToUInt(fd
) );
735 void wxFileCtrl::GoToParentDir()
737 if (!IsTopMostDir(m_dirName
))
739 size_t len
= m_dirName
.Len();
740 if (wxEndsWithPathSeparator(m_dirName
))
741 m_dirName
.Remove( len
-1, 1 );
742 wxString
fname( wxFileNameFromPath(m_dirName
) );
743 m_dirName
= wxPathOnly( m_dirName
);
744 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
745 if (!m_dirName
.empty())
747 if (m_dirName
.Last() == wxT('.'))
748 m_dirName
= wxEmptyString
;
750 #elif defined(__UNIX__)
751 if (m_dirName
.empty())
752 m_dirName
= wxT("/");
755 long id
= FindItem( 0, fname
);
756 if (id
!= wxNOT_FOUND
)
758 ignoreChanges
= true;
759 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
761 ignoreChanges
= false;
766 void wxFileCtrl::GoToHomeDir()
768 wxString s
= wxGetUserHome( wxString() );
772 void wxFileCtrl::GoToDir( const wxString
&dir
)
774 if (!wxDirExists(dir
)) return;
779 ignoreChanges
= true;
780 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
781 ignoreChanges
= false;
786 void wxFileCtrl::FreeItemData(wxListItem
& item
)
790 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
797 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
799 FreeItemData(event
.m_item
);
802 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
807 void wxFileCtrl::FreeAllItemsData()
810 item
.m_mask
= wxLIST_MASK_DATA
;
812 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
813 while ( item
.m_itemId
!= -1 )
817 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
821 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
823 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
826 if ((event
.GetLabel().empty()) ||
827 (event
.GetLabel() == _(".")) ||
828 (event
.GetLabel() == _("..")) ||
829 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
831 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
837 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
838 new_name
+= wxFILE_SEP_PATH
;
839 new_name
+= event
.GetLabel();
843 if (wxFileExists(new_name
))
845 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
850 if (wxRenameFile(fd
->GetFilePath(),new_name
))
852 fd
->SetNewName( new_name
, event
.GetLabel() );
854 ignoreChanges
= true;
855 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
856 ignoreChanges
= false;
858 UpdateItem( event
.GetItem() );
859 EnsureVisible( event
.GetItem() );
863 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
869 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
871 int col
= event
.GetColumn();
875 case wxFileData::FileList_Name
:
876 case wxFileData::FileList_Size
:
877 case wxFileData::FileList_Type
:
878 case wxFileData::FileList_Time
: break;
882 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
883 m_sort_foward
= !m_sort_foward
;
885 m_sort_field
= (wxFileData::fileListFieldType
)col
;
887 SortItems(m_sort_field
, m_sort_foward
);
890 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool forward
)
892 m_sort_field
= field
;
893 m_sort_foward
= forward
;
894 const long sort_dir
= forward
? 1 : -1;
896 switch (m_sort_field
)
898 case wxFileData::FileList_Size
:
899 wxListCtrl::SortItems(wxFileDataSizeCompare
, sort_dir
);
902 case wxFileData::FileList_Type
:
903 wxListCtrl::SortItems(wxFileDataTypeCompare
, sort_dir
);
906 case wxFileData::FileList_Time
:
907 wxListCtrl::SortItems(wxFileDataTimeCompare
, sort_dir
);
910 case wxFileData::FileList_Name
:
912 wxListCtrl::SortItems(wxFileDataNameCompare
, sort_dir
);
917 wxFileCtrl::~wxFileCtrl()
919 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
920 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
921 // the destruction of the wxFileCtrl we need to free any data here:
925 //-----------------------------------------------------------------------------
926 // wxGenericFileDialog
927 //-----------------------------------------------------------------------------
929 #define ID_LIST_MODE (wxID_FILEDLGG )
930 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
931 #define ID_UP_DIR (wxID_FILEDLGG + 5)
932 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
933 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
934 #define ID_CHOICE (wxID_FILEDLGG + 8)
935 #define ID_TEXT (wxID_FILEDLGG + 9)
936 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
937 #define ID_CHECK (wxID_FILEDLGG + 12)
939 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
941 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
942 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
943 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
944 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
945 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
946 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
947 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
948 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
949 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
950 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
951 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
952 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
953 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
956 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
957 bool wxGenericFileDialog::ms_lastShowHidden
= false;
959 void wxGenericFileDialog::Init()
961 m_bypassGenericImpl
= false;
968 m_upDirButton
= NULL
;
969 m_newDirButton
= NULL
;
972 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
973 const wxString
& message
,
974 const wxString
& defaultDir
,
975 const wxString
& defaultFile
,
976 const wxString
& wildCard
,
979 bool bypassGenericImpl
) : wxFileDialogBase()
982 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, bypassGenericImpl
);
985 bool wxGenericFileDialog::Create( wxWindow
*parent
,
986 const wxString
& message
,
987 const wxString
& defaultDir
,
988 const wxString
& defaultFile
,
989 const wxString
& wildCard
,
992 bool bypassGenericImpl
)
994 m_bypassGenericImpl
= bypassGenericImpl
;
996 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
997 wildCard
, style
, pos
))
1002 if (m_bypassGenericImpl
)
1005 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
1006 wxDEFAULT_DIALOG_STYLE
1007 #if !(defined(__PDA__) || defined(__SMARTPHONE__))
1015 ignoreChanges
= true;
1017 if (wxConfig::Get(false))
1019 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1021 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1022 &ms_lastShowHidden
);
1025 if (m_dialogStyle
== 0)
1026 m_dialogStyle
= wxOPEN
;
1027 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
1028 m_dialogStyle
|= wxOPEN
;
1030 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
1034 m_dir
= wxFILE_SEP_PATH
;
1037 size_t len
= m_dir
.Len();
1038 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
1039 m_dir
.Remove( len
-1, 1 );
1042 m_path
+= wxFILE_SEP_PATH
;
1043 m_path
+= defaultFile
;
1044 m_filterExtension
= wxEmptyString
;
1048 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1050 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
1052 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1054 wxBitmapButton
*but
;
1056 but
= new wxBitmapButton(this, ID_LIST_MODE
,
1057 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
1059 but
->SetToolTip( _("View files as a list view") );
1061 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1063 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
1064 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
1066 but
->SetToolTip( _("View files as a detailed view") );
1068 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1070 buttonsizer
->Add( 30, 5, 1 );
1072 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
1073 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
1075 m_upDirButton
->SetToolTip( _("Go to parent directory") );
1077 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
1079 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1080 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
1081 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
1083 but
->SetToolTip( _("Go to home directory") );
1085 buttonsizer
->Add( but
, 0, wxALL
, 5);
1087 buttonsizer
->Add( 20, 20 );
1090 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
1091 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
1093 m_newDirButton
->SetToolTip( _("Create new directory") );
1095 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1098 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1100 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1102 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1104 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1105 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1106 staticsizer
->Add( m_static
, 1 );
1107 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1109 long style2
= ms_lastViewStyle
;
1110 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1111 style2
|= wxLC_SINGLE_SEL
;
1114 style2
|= wxSIMPLE_BORDER
;
1116 style2
|= wxSUNKEN_BORDER
;
1119 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1120 wxEmptyString
, ms_lastShowHidden
,
1121 wxDefaultPosition
, wxSize(540,200),
1126 // PDAs have a different screen layout
1127 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1129 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1130 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1131 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1132 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1135 m_choice
= new wxChoice( this, ID_CHOICE
);
1136 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1138 wxSizer
*bsizer
= CreateButtonSizer( wxOK
|wxCANCEL
, false, 5 );
1139 if(bsizer
->GetChildren().GetCount() > 0 )
1141 mainsizer
->Add( bsizer
, 0, wxEXPAND
| wxALL
, 5 );
1150 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1152 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1153 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1154 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1155 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1156 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1158 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1159 m_choice
= new wxChoice( this, ID_CHOICE
);
1160 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1161 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1162 m_check
->SetValue( ms_lastShowHidden
);
1163 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1164 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1165 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1168 SetWildcard(wildCard
);
1170 SetAutoLayout( true );
1171 SetSizer( mainsizer
);
1175 mainsizer
->Fit( this );
1176 mainsizer
->SetSizeHints( this );
1183 ignoreChanges
= false;
1188 wxGenericFileDialog::~wxGenericFileDialog()
1190 ignoreChanges
= true;
1192 if (!m_bypassGenericImpl
)
1194 if (wxConfig::Get(false))
1196 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1198 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1202 const int count
= m_choice
->GetCount();
1203 for ( int i
= 0; i
< count
; i
++ )
1205 delete (wxString
*)m_choice
->GetClientData(i
);
1210 int wxGenericFileDialog::ShowModal()
1212 ignoreChanges
= true;
1214 m_list
->GoToDir(m_dir
);
1216 m_text
->SetValue(m_fileName
);
1218 ignoreChanges
= false;
1220 return wxDialog::ShowModal();
1223 bool wxGenericFileDialog::Show( bool show
)
1225 // Called by ShowModal, so don't repeate the update
1229 m_list
->GoToDir(m_dir
);
1231 m_text
->SetValue(m_fileName
);
1235 return wxDialog::Show( show
);
1238 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1240 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1241 m_list
->SetWild( *str
);
1242 m_filterIndex
= filterindex
;
1243 if ( str
->Left(2) == wxT("*.") )
1245 m_filterExtension
= str
->Mid(1);
1246 if (m_filterExtension
== _T(".*"))
1247 m_filterExtension
.clear();
1251 m_filterExtension
.clear();
1255 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1257 wxFileDialogBase::SetWildcard(wildCard
);
1259 wxArrayString wildDescriptions
, wildFilters
;
1260 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1263 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1265 const size_t countOld
= m_choice
->GetCount();
1267 for ( n
= 0; n
< countOld
; n
++ )
1269 delete (wxString
*)m_choice
->GetClientData(n
);
1272 for ( n
= 0; n
< count
; n
++ )
1274 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1277 SetFilterIndex( 0 );
1280 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1282 m_choice
->SetSelection( filterindex
);
1284 DoSetFilterIndex(filterindex
);
1287 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1289 DoSetFilterIndex((int)event
.GetInt());
1292 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1294 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1297 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1299 HandleAction( event
.m_item
.m_text
);
1302 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1304 HandleAction( m_text
->GetValue() );
1307 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1311 // Clear selections. Otherwise when the user types in a value they may
1312 // not get the file whose name they typed.
1313 if (m_list
->GetSelectedItemCount() > 0)
1315 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1316 wxLIST_STATE_SELECTED
);
1317 while ( item
!= -1 )
1319 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1320 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1326 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1328 static bool inSelected
= false;
1334 wxString
filename( event
.m_item
.m_text
);
1337 // No double-click on most WinCE devices, so do action immediately.
1338 HandleAction( filename
);
1340 if (filename
== wxT("..")) return;
1342 wxString dir
= m_list
->GetDir();
1343 if (!IsTopMostDir(dir
))
1344 dir
+= wxFILE_SEP_PATH
;
1346 if (wxDirExists(dir
)) return;
1348 ignoreChanges
= true;
1349 m_text
->SetValue( filename
);
1350 ignoreChanges
= false;
1355 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1360 wxString
filename( fn
);
1361 if (filename
.empty())
1364 EndModal(wxID_CANCEL
);
1368 if (filename
== wxT(".")) return;
1370 wxString dir
= m_list
->GetDir();
1372 // "some/place/" means they want to chdir not try to load "place"
1373 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1375 filename
= filename
.RemoveLast();
1377 if (filename
== wxT(".."))
1379 ignoreChanges
= true;
1380 m_list
->GoToParentDir();
1383 ignoreChanges
= false;
1388 if (filename
== wxT("~"))
1390 ignoreChanges
= true;
1391 m_list
->GoToHomeDir();
1394 ignoreChanges
= false;
1398 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1400 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1404 if (!(m_dialogStyle
& wxSAVE
))
1406 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1407 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1409 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1411 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1414 m_list
->SetWild( filename
);
1419 if (!IsTopMostDir(dir
))
1420 dir
+= wxFILE_SEP_PATH
;
1421 if (!wxIsAbsolutePath(filename
))
1427 if (wxDirExists(filename
))
1429 ignoreChanges
= true;
1430 m_list
->GoToDir( filename
);
1432 ignoreChanges
= false;
1436 // they really wanted a dir, but it doesn't exist
1439 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1440 wxOK
| wxICON_ERROR
);
1444 // append the default extension to the filename if it doesn't have any
1446 // VZ: the logic of testing for !wxFileExists() only for the open file
1447 // dialog is not entirely clear to me, why don't we allow saving to a
1448 // file without extension as well?
1449 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1451 filename
= AppendExtension(filename
, m_filterExtension
);
1454 // check that the file [doesn't] exist if necessary
1455 if ( (m_dialogStyle
& wxSAVE
) &&
1456 (m_dialogStyle
& wxOVERWRITE_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 ( (m_dialogStyle
& wxOPEN
) &&
1466 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1467 !wxFileExists(filename
) )
1469 wxMessageBox(_("Please choose an existing file."), _("Error"),
1470 wxOK
| wxICON_ERROR
);
1473 SetPath( filename
);
1475 // change to the directory where the user went if asked
1476 if ( m_dialogStyle
& wxCHANGE_DIR
)
1479 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1481 if ( cwd
!= wxGetCwd() )
1483 wxSetWorkingDirectory(cwd
);
1490 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1492 HandleAction( m_text
->GetValue() );
1495 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1497 ignoreChanges
= true;
1498 m_list
->ChangeToListMode();
1499 ms_lastViewStyle
= wxLC_LIST
;
1501 ignoreChanges
= false;
1504 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1506 ignoreChanges
= true;
1507 m_list
->ChangeToReportMode();
1508 ms_lastViewStyle
= wxLC_REPORT
;
1510 ignoreChanges
= false;
1513 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1515 ignoreChanges
= true;
1516 m_list
->GoToParentDir();
1519 ignoreChanges
= false;
1522 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1524 ignoreChanges
= true;
1525 m_list
->GoToHomeDir();
1528 ignoreChanges
= false;
1531 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1533 ignoreChanges
= true;
1537 ignoreChanges
= false;
1540 void wxGenericFileDialog::SetPath( const wxString
& path
)
1542 // not only set the full path but also update filename and dir
1547 m_path
= wxFILE_SEP_PATH
;
1550 if ( !path
.empty() )
1553 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1556 m_fileName
+= wxT(".");
1562 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1565 if (m_list
->GetSelectedItemCount() == 0)
1567 paths
.Add( GetPath() );
1571 paths
.Alloc( m_list
->GetSelectedItemCount() );
1573 wxString dir
= m_list
->GetDir();
1575 if (dir
!= wxT("/"))
1578 if (dir
!= wxT("/") && dir
!= wxT("\\"))
1580 dir
+= wxFILE_SEP_PATH
;
1583 item
.m_mask
= wxLIST_MASK_TEXT
;
1585 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1586 while ( item
.m_itemId
!= -1 )
1588 m_list
->GetItem( item
);
1589 paths
.Add( dir
+ item
.m_text
);
1590 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1591 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1595 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1598 if (m_list
->GetSelectedItemCount() == 0)
1600 files
.Add( GetFilename() );
1603 files
.Alloc( m_list
->GetSelectedItemCount() );
1606 item
.m_mask
= wxLIST_MASK_TEXT
;
1608 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1609 while ( item
.m_itemId
!= -1 )
1611 m_list
->GetItem( item
);
1612 files
.Add( item
.m_text
);
1613 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1614 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1618 void wxGenericFileDialog::UpdateControls()
1620 wxString dir
= m_list
->GetDir();
1621 m_static
->SetLabel(dir
);
1623 bool enable
= !IsTopMostDir(dir
);
1624 m_upDirButton
->Enable(enable
);
1626 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1627 m_newDirButton
->Enable(enable
);
1628 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1631 #ifdef USE_GENERIC_FILEDIALOG
1633 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
1635 #endif // USE_GENERIC_FILEDIALOG
1637 #endif // wxUSE_FILEDLG