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"
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"
38 #include "wx/longlong.h"
39 #include "wx/msgdlg.h"
41 #include "wx/bmpbuttn.h"
42 #include "wx/tokenzr.h"
43 #include "wx/config.h"
44 #include "wx/imaglist.h"
46 #include "wx/artprov.h"
47 #include "wx/filefn.h"
48 #include "wx/file.h" // for wxS_IXXX constants only
49 #include "wx/filedlg.h" // wxOPEN, wxSAVE...
50 #include "wx/generic/filedlgg.h"
51 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
54 #include "wx/tooltip.h"
58 #include <sys/types.h>
71 #include "wx/msw/wrapwin.h"
72 #include "wx/msw/mslu.h"
83 #if defined(__UNIX__) || defined(__DOS__)
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
92 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long sortOrder
)
94 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
95 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
97 if (fd1
->GetFileName() == wxT(".."))
99 if (fd2
->GetFileName() == wxT(".."))
101 if (fd1
->IsDir() && !fd2
->IsDir())
103 if (fd2
->IsDir() && !fd1
->IsDir())
106 return sortOrder
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
110 int wxCALLBACK
wxFileDataSizeCompare(long data1
, long data2
, long sortOrder
)
112 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
113 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
115 if (fd1
->GetFileName() == wxT(".."))
117 if (fd2
->GetFileName() == wxT(".."))
119 if (fd1
->IsDir() && !fd2
->IsDir())
121 if (fd2
->IsDir() && !fd1
->IsDir())
123 if (fd1
->IsLink() && !fd2
->IsLink())
125 if (fd2
->IsLink() && !fd1
->IsLink())
128 return fd1
->GetSize() > fd2
->GetSize() ? sortOrder
: -sortOrder
;
132 int wxCALLBACK
wxFileDataTypeCompare(long data1
, long data2
, long sortOrder
)
134 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
135 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
137 if (fd1
->GetFileName() == wxT(".."))
139 if (fd2
->GetFileName() == wxT(".."))
141 if (fd1
->IsDir() && !fd2
->IsDir())
143 if (fd2
->IsDir() && !fd1
->IsDir())
145 if (fd1
->IsLink() && !fd2
->IsLink())
147 if (fd2
->IsLink() && !fd1
->IsLink())
150 return sortOrder
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
154 int wxCALLBACK
wxFileDataTimeCompare(long data1
, long data2
, long sortOrder
)
156 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
157 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
159 if (fd1
->GetFileName() == wxT(".."))
161 if (fd2
->GetFileName() == wxT(".."))
163 if (fd1
->IsDir() && !fd2
->IsDir())
165 if (fd2
->IsDir() && !fd1
->IsDir())
168 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? sortOrder
: -sortOrder
;
171 #if defined(__WXWINCE__)
172 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
173 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
174 #define IsTopMostDir(dir) (dir.empty())
176 #define IsTopMostDir(dir) (dir == wxT("/"))
179 // defined in src/generic/dirctrlg.cpp
180 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
182 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
186 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
189 m_fileName
= fileName
;
190 m_filePath
= filePath
;
197 void wxFileData::Init()
200 m_type
= wxFileData::is_file
;
201 m_image
= wxFileIconsTable::file
;
204 void wxFileData::Copy( const wxFileData
& fileData
)
206 m_fileName
= fileData
.GetFileName();
207 m_filePath
= fileData
.GetFilePath();
208 m_size
= fileData
.GetSize();
209 m_dateTime
= fileData
.GetDateTime();
210 m_permissions
= fileData
.GetPermissions();
211 m_type
= fileData
.GetType();
212 m_image
= fileData
.GetImageId();
215 void wxFileData::ReadData()
223 #if defined(__DOS__) || (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__OS2__)
224 // c:\.. is a drive don't stat it
225 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
231 #endif // __DOS__ || __WINDOWS__
237 DWORD fileAttribs
= GetFileAttributes(m_filePath
.fn_str());
238 m_type
|= (fileAttribs
& FILE_ATTRIBUTE_DIRECTORY
) != 0 ? is_dir
: 0;
241 wxSplitPath(m_filePath
, & p
, & f
, & ext
);
242 if (wxStricmp(ext
, wxT("exe")) == 0)
247 HANDLE fileHandle
= CreateFile(m_filePath
.fn_str(),
252 FILE_ATTRIBUTE_NORMAL
,
255 if (fileHandle
!= INVALID_HANDLE_VALUE
)
257 m_size
= GetFileSize(fileHandle
, 0);
258 CloseHandle(fileHandle
);
261 m_dateTime
= wxFileModificationTime(m_filePath
);
269 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
270 lstat( m_filePath
.fn_str(), &buff
);
271 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
273 // only translate to file charset if we don't go by our
274 // wxStat implementation
275 #ifndef wxNEED_WX_UNISTD_H
276 wxStat( m_filePath
.fn_str() , &buff
);
278 wxStat( m_filePath
, &buff
);
282 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
283 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
285 m_size
= buff
.st_size
;
287 m_dateTime
= buff
.st_mtime
;
291 #if defined(__UNIX__)
292 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
293 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
294 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
295 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
296 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
297 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
298 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
299 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
300 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
301 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
302 #elif defined(__WIN32__)
303 DWORD attribs
= ::GetFileAttributes(m_filePath
.c_str());
304 if (attribs
!= (DWORD
)-1)
306 m_permissions
.Printf(_T("%c%c%c%c"),
307 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
308 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
309 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
310 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
314 // try to get a better icon
315 if (m_image
== wxFileIconsTable::file
)
317 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
319 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
322 m_image
= wxFileIconsTable::executable
;
327 wxString
wxFileData::GetFileType() const
335 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
336 return m_fileName
.AfterLast(wxT('.'));
338 return wxEmptyString
;
341 wxString
wxFileData::GetModificationTime() const
343 // want time as 01:02 so they line up nicely, no %r in WIN32
344 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
347 wxString
wxFileData::GetHint() const
349 wxString s
= m_filePath
;
359 s
+= wxString::Format(_("%ld bytes"),
360 wxLongLong(m_size
).ToString().c_str());
366 s
<< GetModificationTime()
374 wxString
wxFileData::GetEntry( fileListFieldType num
) const
384 if (!IsDir() && !IsLink() && !IsDrive())
385 s
= wxLongLong(m_size
).ToString();
394 s
= GetModificationTime();
397 #if defined(__UNIX__) || defined(__WIN32__)
401 #endif // defined(__UNIX__) || defined(__WIN32__)
404 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
410 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
412 m_fileName
= fileName
;
413 m_filePath
= filePath
;
416 void wxFileData::MakeItem( wxListItem
&item
)
418 item
.m_text
= m_fileName
;
419 item
.ClearAttributes();
421 item
.SetTextColour(*wxRED
);
423 item
.SetTextColour(*wxBLUE
);
425 item
.m_image
= m_image
;
429 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
431 item
.SetTextColour(dg
);
433 item
.m_data
= wxPtrToUInt(this);
436 //-----------------------------------------------------------------------------
438 //-----------------------------------------------------------------------------
440 static bool ignoreChanges
= false;
442 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
444 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
445 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
446 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
447 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
448 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
452 wxFileCtrl::wxFileCtrl()
454 m_showHidden
= false;
456 m_sort_field
= wxFileData::FileList_Name
;
459 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
461 const wxString
& wild
,
466 const wxValidator
&validator
,
467 const wxString
&name
)
468 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
471 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
473 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
475 m_showHidden
= showHidden
;
478 m_sort_field
= wxFileData::FileList_Name
;
480 m_dirName
= wxT("*");
482 if (style
& wxLC_REPORT
)
483 ChangeToReportMode();
486 void wxFileCtrl::ChangeToListMode()
489 SetSingleStyle( wxLC_LIST
);
493 void wxFileCtrl::ChangeToReportMode()
496 SetSingleStyle( wxLC_REPORT
);
498 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
499 // don't hardcode since mm/dd is dd/mm elsewhere
501 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
502 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
503 GetTextExtent(txt
, &w
, &h
);
505 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
506 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
507 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
508 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
509 #if defined(__UNIX__)
510 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
511 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
512 #elif defined(__WIN32__)
513 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
514 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
520 void wxFileCtrl::ChangeToSmallIconMode()
523 SetSingleStyle( wxLC_SMALL_ICON
);
527 void wxFileCtrl::ShowHidden( bool show
)
533 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
536 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
537 fd
->MakeItem( item
);
538 long my_style
= GetWindowStyleFlag();
539 if (my_style
& wxLC_REPORT
)
541 ret
= InsertItem( item
);
542 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
543 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
545 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
547 ret
= InsertItem( item
);
552 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
554 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
555 wxCHECK_RET(fd
, wxT("invalid filedata"));
559 SetItemText(item
, fd
->GetFileName());
560 SetItemImage(item
, fd
->GetImageId());
562 if (GetWindowStyleFlag() & wxLC_REPORT
)
564 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
565 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
569 void wxFileCtrl::UpdateFiles()
571 // don't do anything before ShowModal() call which sets m_dirName
572 if ( m_dirName
== wxT("*") )
575 wxBusyCursor bcur
; // this may take a while...
583 #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__)
584 if ( IsTopMostDir(m_dirName
) )
586 wxArrayString names
, paths
;
588 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
590 for (n
=0; n
<count
; n
++)
592 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
593 if (Add(fd
, item
) != -1)
600 #endif // defined(__DOS__) || defined(__WINDOWS__)
603 if ( !IsTopMostDir(m_dirName
) && !m_dirName
.empty() )
605 wxString
p(wxPathOnly(m_dirName
));
606 #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__)
607 if (p
.empty()) p
= wxT("/");
609 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
610 if (Add(fd
, item
) != -1)
616 wxString
dirname(m_dirName
);
617 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
618 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
619 dirname
<< wxT('\\');
620 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
623 dirname
= wxFILE_SEP_PATH
;
628 if ( dir
.IsOpened() )
630 wxString
dirPrefix(dirname
);
631 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
632 dirPrefix
+= wxFILE_SEP_PATH
;
634 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
639 // Get the directories first (not matched against wildcards):
640 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
643 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
644 if (Add(fd
, item
) != -1)
649 cont
= dir
.GetNext(&f
);
652 // Tokenize the wildcard string, so we can handle more than 1
653 // search pattern in a wildcard.
654 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
655 while ( tokenWild
.HasMoreTokens() )
657 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
658 wxDIR_FILES
| hiddenFlag
);
661 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
662 if (Add(fd
, item
) != -1)
667 cont
= dir
.GetNext(&f
);
673 SortItems(m_sort_field
, m_sort_foward
);
676 void wxFileCtrl::SetWild( const wxString
&wild
)
678 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
685 void wxFileCtrl::MakeDir()
687 wxString
new_name( _("NewName") );
688 wxString
path( m_dirName
);
689 path
+= wxFILE_SEP_PATH
;
691 if (wxFileExists(path
))
693 // try NewName0, NewName1 etc.
696 new_name
= _("NewName");
698 num
.Printf( wxT("%d"), i
);
702 path
+= wxFILE_SEP_PATH
;
705 } while (wxFileExists(path
));
711 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
716 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
720 long id
= Add( fd
, item
);
724 SortItems(m_sort_field
, m_sort_foward
);
725 id
= FindItem( 0, wxPtrToUInt(fd
) );
733 void wxFileCtrl::GoToParentDir()
735 if (!IsTopMostDir(m_dirName
))
737 size_t len
= m_dirName
.Len();
738 if (wxEndsWithPathSeparator(m_dirName
))
739 m_dirName
.Remove( len
-1, 1 );
740 wxString
fname( wxFileNameFromPath(m_dirName
) );
741 m_dirName
= wxPathOnly( m_dirName
);
742 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
743 if (!m_dirName
.empty())
745 if (m_dirName
.Last() == wxT('.'))
746 m_dirName
= wxEmptyString
;
748 #elif defined(__UNIX__)
749 if (m_dirName
.empty())
750 m_dirName
= wxT("/");
753 long id
= FindItem( 0, fname
);
754 if (id
!= wxNOT_FOUND
)
756 ignoreChanges
= true;
757 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
759 ignoreChanges
= false;
764 void wxFileCtrl::GoToHomeDir()
766 wxString s
= wxGetUserHome( wxString() );
770 void wxFileCtrl::GoToDir( const wxString
&dir
)
772 if (!wxDirExists(dir
)) return;
777 ignoreChanges
= true;
778 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
779 ignoreChanges
= false;
784 void wxFileCtrl::FreeItemData(wxListItem
& item
)
788 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
795 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
797 FreeItemData(event
.m_item
);
800 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
805 void wxFileCtrl::FreeAllItemsData()
808 item
.m_mask
= wxLIST_MASK_DATA
;
810 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
811 while ( item
.m_itemId
!= -1 )
815 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
819 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
821 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
824 if ((event
.GetLabel().empty()) ||
825 (event
.GetLabel() == _(".")) ||
826 (event
.GetLabel() == _("..")) ||
827 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
829 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
835 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
836 new_name
+= wxFILE_SEP_PATH
;
837 new_name
+= event
.GetLabel();
841 if (wxFileExists(new_name
))
843 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
848 if (wxRenameFile(fd
->GetFilePath(),new_name
))
850 fd
->SetNewName( new_name
, event
.GetLabel() );
852 ignoreChanges
= true;
853 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
854 ignoreChanges
= false;
856 UpdateItem( event
.GetItem() );
857 EnsureVisible( event
.GetItem() );
861 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
867 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
869 int col
= event
.GetColumn();
873 case wxFileData::FileList_Name
:
874 case wxFileData::FileList_Size
:
875 case wxFileData::FileList_Type
:
876 case wxFileData::FileList_Time
: break;
880 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
881 m_sort_foward
= !m_sort_foward
;
883 m_sort_field
= (wxFileData::fileListFieldType
)col
;
885 SortItems(m_sort_field
, m_sort_foward
);
888 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool forward
)
890 m_sort_field
= field
;
891 m_sort_foward
= forward
;
892 const long sort_dir
= forward
? 1 : -1;
894 switch (m_sort_field
)
896 case wxFileData::FileList_Size
:
897 wxListCtrl::SortItems(wxFileDataSizeCompare
, sort_dir
);
900 case wxFileData::FileList_Type
:
901 wxListCtrl::SortItems(wxFileDataTypeCompare
, sort_dir
);
904 case wxFileData::FileList_Time
:
905 wxListCtrl::SortItems(wxFileDataTimeCompare
, sort_dir
);
908 case wxFileData::FileList_Name
:
910 wxListCtrl::SortItems(wxFileDataNameCompare
, sort_dir
);
915 wxFileCtrl::~wxFileCtrl()
917 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
918 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
919 // the destruction of the wxFileCtrl we need to free any data here:
923 //-----------------------------------------------------------------------------
924 // wxGenericFileDialog
925 //-----------------------------------------------------------------------------
927 #define ID_LIST_MODE (wxID_FILEDLGG )
928 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
929 #define ID_UP_DIR (wxID_FILEDLGG + 5)
930 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
931 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
932 #define ID_CHOICE (wxID_FILEDLGG + 8)
933 #define ID_TEXT (wxID_FILEDLGG + 9)
934 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
935 #define ID_CHECK (wxID_FILEDLGG + 12)
937 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
939 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
940 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
941 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
942 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
943 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
944 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
945 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
946 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
947 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
948 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
949 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
950 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
951 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
954 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
955 bool wxGenericFileDialog::ms_lastShowHidden
= false;
957 void wxGenericFileDialog::Init()
959 m_bypassGenericImpl
= false;
966 m_upDirButton
= NULL
;
967 m_newDirButton
= NULL
;
970 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
971 const wxString
& message
,
972 const wxString
& defaultDir
,
973 const wxString
& defaultFile
,
974 const wxString
& wildCard
,
977 bool bypassGenericImpl
) : wxFileDialogBase()
980 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, bypassGenericImpl
);
983 bool wxGenericFileDialog::Create( wxWindow
*parent
,
984 const wxString
& message
,
985 const wxString
& defaultDir
,
986 const wxString
& defaultFile
,
987 const wxString
& wildCard
,
990 bool bypassGenericImpl
)
992 m_bypassGenericImpl
= bypassGenericImpl
;
994 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
995 wildCard
, style
, pos
))
1000 if (m_bypassGenericImpl
)
1003 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
1004 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
1010 ignoreChanges
= true;
1012 if (wxConfig::Get(false))
1014 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1016 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1017 &ms_lastShowHidden
);
1020 if (m_dialogStyle
== 0)
1021 m_dialogStyle
= wxOPEN
;
1022 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
1023 m_dialogStyle
|= wxOPEN
;
1025 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
1029 m_dir
= wxFILE_SEP_PATH
;
1032 size_t len
= m_dir
.Len();
1033 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
1034 m_dir
.Remove( len
-1, 1 );
1037 m_path
+= wxFILE_SEP_PATH
;
1038 m_path
+= defaultFile
;
1039 m_filterExtension
= wxEmptyString
;
1043 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1045 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
1047 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1049 wxBitmapButton
*but
;
1051 but
= new wxBitmapButton(this, ID_LIST_MODE
,
1052 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
1054 but
->SetToolTip( _("View files as a list view") );
1056 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1058 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
1059 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
1061 but
->SetToolTip( _("View files as a detailed view") );
1063 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1065 buttonsizer
->Add( 30, 5, 1 );
1067 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
1068 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
1070 m_upDirButton
->SetToolTip( _("Go to parent directory") );
1072 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
1074 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1075 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
1076 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
1078 but
->SetToolTip( _("Go to home directory") );
1080 buttonsizer
->Add( but
, 0, wxALL
, 5);
1082 buttonsizer
->Add( 20, 20 );
1085 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
1086 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
1088 m_newDirButton
->SetToolTip( _("Create new directory") );
1090 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1093 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1095 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1097 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1099 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1100 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1101 staticsizer
->Add( m_static
, 1 );
1102 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1104 long style2
= ms_lastViewStyle
;
1105 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1106 style2
|= wxLC_SINGLE_SEL
;
1109 style2
|= wxSIMPLE_BORDER
;
1111 style2
|= wxSUNKEN_BORDER
;
1114 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1115 wxEmptyString
, ms_lastShowHidden
,
1116 wxDefaultPosition
, wxSize(540,200),
1121 // PDAs have a different screen layout
1122 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1124 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1125 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1126 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1127 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1130 m_choice
= new wxChoice( this, ID_CHOICE
);
1131 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1133 wxSizer
*bsizer
= CreateButtonSizer( wxOK
|wxCANCEL
, false, 5 );
1134 if(bsizer
->GetChildren().GetCount() > 0 )
1136 mainsizer
->Add( bsizer
, 0, wxEXPAND
| wxALL
, 5 );
1145 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1147 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1148 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxTE_PROCESS_ENTER
);
1149 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1150 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1151 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1153 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1154 m_choice
= new wxChoice( this, ID_CHOICE
);
1155 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1156 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1157 m_check
->SetValue( ms_lastShowHidden
);
1158 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1159 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1160 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1163 SetWildcard(wildCard
);
1165 SetAutoLayout( true );
1166 SetSizer( mainsizer
);
1170 mainsizer
->Fit( this );
1171 mainsizer
->SetSizeHints( this );
1178 ignoreChanges
= false;
1183 wxGenericFileDialog::~wxGenericFileDialog()
1185 ignoreChanges
= true;
1187 if (!m_bypassGenericImpl
)
1189 if (wxConfig::Get(false))
1191 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1193 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1197 const int count
= m_choice
->GetCount();
1198 for ( int i
= 0; i
< count
; i
++ )
1200 delete (wxString
*)m_choice
->GetClientData(i
);
1205 int wxGenericFileDialog::ShowModal()
1207 ignoreChanges
= true;
1209 m_list
->GoToDir(m_dir
);
1211 m_text
->SetValue(m_fileName
);
1213 ignoreChanges
= false;
1215 return wxDialog::ShowModal();
1218 bool wxGenericFileDialog::Show( bool show
)
1220 // Called by ShowModal, so don't repeate the update
1224 m_list
->GoToDir(m_dir
);
1226 m_text
->SetValue(m_fileName
);
1230 return wxDialog::Show( show
);
1233 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1235 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1236 m_list
->SetWild( *str
);
1237 m_filterIndex
= filterindex
;
1238 if ( str
->Left(2) == wxT("*.") )
1240 m_filterExtension
= str
->Mid(1);
1241 if (m_filterExtension
== _T(".*"))
1242 m_filterExtension
.clear();
1246 m_filterExtension
.clear();
1250 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1252 wxFileDialogBase::SetWildcard(wildCard
);
1254 wxArrayString wildDescriptions
, wildFilters
;
1255 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1258 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1260 const size_t countOld
= m_choice
->GetCount();
1262 for ( n
= 0; n
< countOld
; n
++ )
1264 delete (wxString
*)m_choice
->GetClientData(n
);
1267 for ( n
= 0; n
< count
; n
++ )
1269 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1272 SetFilterIndex( 0 );
1275 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1277 m_choice
->SetSelection( filterindex
);
1279 DoSetFilterIndex(filterindex
);
1282 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1284 DoSetFilterIndex((int)event
.GetInt());
1287 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1289 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1292 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1294 HandleAction( event
.m_item
.m_text
);
1297 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1299 HandleAction( m_text
->GetValue() );
1302 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1306 // Clear selections. Otherwise when the user types in a value they may
1307 // not get the file whose name they typed.
1308 if (m_list
->GetSelectedItemCount() > 0)
1310 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1311 wxLIST_STATE_SELECTED
);
1312 while ( item
!= -1 )
1314 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1315 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1321 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1323 static bool inSelected
= false;
1329 wxString
filename( event
.m_item
.m_text
);
1332 // No double-click on most WinCE devices, so do action immediately.
1333 HandleAction( filename
);
1335 if (filename
== wxT(".."))
1341 wxString dir
= m_list
->GetDir();
1342 if (!IsTopMostDir(dir
))
1343 dir
+= wxFILE_SEP_PATH
;
1345 if (wxDirExists(dir
))
1351 ignoreChanges
= true;
1352 m_text
->SetValue( filename
);
1353 ignoreChanges
= false;
1358 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1363 wxString
filename( fn
);
1364 if (filename
.empty())
1367 EndModal(wxID_CANCEL
);
1371 if (filename
== wxT(".")) return;
1373 wxString dir
= m_list
->GetDir();
1375 // "some/place/" means they want to chdir not try to load "place"
1376 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1378 filename
= filename
.RemoveLast();
1380 if (filename
== wxT(".."))
1382 ignoreChanges
= true;
1383 m_list
->GoToParentDir();
1386 ignoreChanges
= false;
1391 if (filename
== wxT("~"))
1393 ignoreChanges
= true;
1394 m_list
->GoToHomeDir();
1397 ignoreChanges
= false;
1401 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1403 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1407 if (!(m_dialogStyle
& wxSAVE
))
1409 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1410 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1412 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1414 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1417 m_list
->SetWild( filename
);
1422 if (!IsTopMostDir(dir
))
1423 dir
+= wxFILE_SEP_PATH
;
1424 if (!wxIsAbsolutePath(filename
))
1430 if (wxDirExists(filename
))
1432 ignoreChanges
= true;
1433 m_list
->GoToDir( filename
);
1435 ignoreChanges
= false;
1439 // they really wanted a dir, but it doesn't exist
1442 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1443 wxOK
| wxICON_ERROR
);
1447 // append the default extension to the filename if it doesn't have any
1449 // VZ: the logic of testing for !wxFileExists() only for the open file
1450 // dialog is not entirely clear to me, why don't we allow saving to a
1451 // file without extension as well?
1452 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1454 filename
= AppendExtension(filename
, m_filterExtension
);
1457 // check that the file [doesn't] exist if necessary
1458 if ( (m_dialogStyle
& wxSAVE
) &&
1459 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1460 wxFileExists( filename
) )
1463 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1465 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1468 else if ( (m_dialogStyle
& wxOPEN
) &&
1469 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1470 !wxFileExists(filename
) )
1472 wxMessageBox(_("Please choose an existing file."), _("Error"),
1473 wxOK
| wxICON_ERROR
);
1476 SetPath( filename
);
1478 // change to the directory where the user went if asked
1479 if ( m_dialogStyle
& wxCHANGE_DIR
)
1482 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1484 if ( cwd
!= wxGetCwd() )
1486 wxSetWorkingDirectory(cwd
);
1493 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1495 HandleAction( m_text
->GetValue() );
1498 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1500 ignoreChanges
= true;
1501 m_list
->ChangeToListMode();
1502 ms_lastViewStyle
= wxLC_LIST
;
1504 ignoreChanges
= false;
1507 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1509 ignoreChanges
= true;
1510 m_list
->ChangeToReportMode();
1511 ms_lastViewStyle
= wxLC_REPORT
;
1513 ignoreChanges
= false;
1516 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1518 ignoreChanges
= true;
1519 m_list
->GoToParentDir();
1522 ignoreChanges
= false;
1525 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1527 ignoreChanges
= true;
1528 m_list
->GoToHomeDir();
1531 ignoreChanges
= false;
1534 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1536 ignoreChanges
= true;
1540 ignoreChanges
= false;
1543 void wxGenericFileDialog::SetPath( const wxString
& path
)
1545 // not only set the full path but also update filename and dir
1550 m_path
= wxFILE_SEP_PATH
;
1553 if ( !path
.empty() )
1556 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1559 m_fileName
+= wxT(".");
1565 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1568 if (m_list
->GetSelectedItemCount() == 0)
1570 paths
.Add( GetPath() );
1574 paths
.Alloc( m_list
->GetSelectedItemCount() );
1576 wxString dir
= m_list
->GetDir();
1578 if (dir
!= wxT("/"))
1581 if (dir
!= wxT("/") && dir
!= wxT("\\"))
1583 dir
+= wxFILE_SEP_PATH
;
1586 item
.m_mask
= wxLIST_MASK_TEXT
;
1588 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1589 while ( item
.m_itemId
!= -1 )
1591 m_list
->GetItem( item
);
1592 paths
.Add( dir
+ item
.m_text
);
1593 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1594 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1598 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1601 if (m_list
->GetSelectedItemCount() == 0)
1603 files
.Add( GetFilename() );
1606 files
.Alloc( m_list
->GetSelectedItemCount() );
1609 item
.m_mask
= wxLIST_MASK_TEXT
;
1611 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1612 while ( item
.m_itemId
!= -1 )
1614 m_list
->GetItem( item
);
1615 files
.Add( item
.m_text
);
1616 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1617 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1621 void wxGenericFileDialog::UpdateControls()
1623 wxString dir
= m_list
->GetDir();
1624 m_static
->SetLabel(dir
);
1626 bool enable
= !IsTopMostDir(dir
);
1627 m_upDirButton
->Enable(enable
);
1629 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1630 m_newDirButton
->Enable(enable
);
1631 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1634 #ifdef USE_GENERIC_FILEDIALOG
1636 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
1638 #endif // USE_GENERIC_FILEDIALOG
1640 #endif // wxUSE_FILEDLG