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/checkbox.h"
27 #include "wx/textctrl.h"
28 #include "wx/choice.h"
29 #include "wx/checkbox.h"
30 #include "wx/stattext.h"
33 #include "wx/longlong.h"
35 #include "wx/msgdlg.h"
37 #include "wx/bmpbuttn.h"
38 #include "wx/tokenzr.h"
39 #include "wx/config.h"
40 #include "wx/imaglist.h"
42 #include "wx/artprov.h"
43 #include "wx/settings.h"
44 #include "wx/file.h" // for wxS_IXXX constants only
45 #include "wx/filedlg.h" // wxOPEN, wxSAVE...
46 #include "wx/generic/filedlgg.h"
47 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
50 #include "wx/tooltip.h"
53 #include <sys/types.h>
65 #include "wx/msw/wrapwin.h"
66 #include "wx/msw/mslu.h"
74 #if defined(__UNIX__) || defined(__DOS__)
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
83 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long sortOrder
)
85 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
86 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
88 if (fd1
->GetFileName() == wxT(".."))
90 if (fd2
->GetFileName() == wxT(".."))
92 if (fd1
->IsDir() && !fd2
->IsDir())
94 if (fd2
->IsDir() && !fd1
->IsDir())
97 return sortOrder
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
101 int wxCALLBACK
wxFileDataSizeCompare(long data1
, long data2
, long sortOrder
)
103 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
104 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
106 if (fd1
->GetFileName() == wxT(".."))
108 if (fd2
->GetFileName() == wxT(".."))
110 if (fd1
->IsDir() && !fd2
->IsDir())
112 if (fd2
->IsDir() && !fd1
->IsDir())
114 if (fd1
->IsLink() && !fd2
->IsLink())
116 if (fd2
->IsLink() && !fd1
->IsLink())
119 return fd1
->GetSize() > fd2
->GetSize() ? sortOrder
: -sortOrder
;
123 int wxCALLBACK
wxFileDataTypeCompare(long data1
, long data2
, long sortOrder
)
125 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
126 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
128 if (fd1
->GetFileName() == wxT(".."))
130 if (fd2
->GetFileName() == wxT(".."))
132 if (fd1
->IsDir() && !fd2
->IsDir())
134 if (fd2
->IsDir() && !fd1
->IsDir())
136 if (fd1
->IsLink() && !fd2
->IsLink())
138 if (fd2
->IsLink() && !fd1
->IsLink())
141 return sortOrder
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
145 int wxCALLBACK
wxFileDataTimeCompare(long data1
, long data2
, long sortOrder
)
147 wxFileData
*fd1
= (wxFileData
*)wxUIntToPtr(data1
);
148 wxFileData
*fd2
= (wxFileData
*)wxUIntToPtr(data2
);
150 if (fd1
->GetFileName() == wxT(".."))
152 if (fd2
->GetFileName() == wxT(".."))
154 if (fd1
->IsDir() && !fd2
->IsDir())
156 if (fd2
->IsDir() && !fd1
->IsDir())
159 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? sortOrder
: -sortOrder
;
162 #if defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)
163 #define IsTopMostDir(dir) (dir.empty())
165 #define IsTopMostDir(dir) (dir == wxT("/"))
168 // defined in src/generic/dirctrlg.cpp
169 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
171 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
175 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
178 m_fileName
= fileName
;
179 m_filePath
= filePath
;
186 void wxFileData::Init()
189 m_type
= wxFileData::is_file
;
190 m_image
= wxFileIconsTable::file
;
193 void wxFileData::Copy( const wxFileData
& fileData
)
195 m_fileName
= fileData
.GetFileName();
196 m_filePath
= fileData
.GetFilePath();
197 m_size
= fileData
.GetSize();
198 m_dateTime
= fileData
.GetDateTime();
199 m_permissions
= fileData
.GetPermissions();
200 m_type
= fileData
.GetType();
201 m_image
= fileData
.GetImageId();
204 void wxFileData::ReadData()
212 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
213 // c:\.. is a drive don't stat it
214 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
220 #endif // __DOS__ || __WINDOWS__
224 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
225 lstat( m_filePath
.fn_str(), &buff
);
226 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
228 // only translate to file charset if we don't go by our
229 // wxStat implementation
230 #ifndef wxNEED_WX_UNISTD_H
231 wxStat( m_filePath
.fn_str() , &buff
);
233 wxStat( m_filePath
, &buff
);
237 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
238 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
240 // try to get a better icon
241 if (m_image
== wxFileIconsTable::file
)
243 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
245 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
248 m_image
= wxFileIconsTable::executable
;
252 m_size
= buff
.st_size
;
254 m_dateTime
= buff
.st_mtime
;
256 #if defined(__UNIX__)
257 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
258 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
259 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
260 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
261 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
262 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
263 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
264 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
265 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
266 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
267 #elif defined(__WIN32__)
268 DWORD attribs
= GetFileAttributes(m_filePath
);
269 if (attribs
!= (DWORD
)-1)
271 m_permissions
.Printf(_T("%c%c%c%c"),
272 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
273 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
274 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
275 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
280 wxString
wxFileData::GetFileType() const
288 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
289 return m_fileName
.AfterLast(wxT('.'));
291 return wxEmptyString
;
294 wxString
wxFileData::GetModificationTime() const
296 // want time as 01:02 so they line up nicely, no %r in WIN32
297 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
300 wxString
wxFileData::GetHint() const
302 wxString s
= m_filePath
;
312 s
+= wxString::Format(_("%ld bytes"),
313 wxLongLong(m_size
).ToString().c_str());
319 s
<< GetModificationTime()
327 wxString
wxFileData::GetEntry( fileListFieldType num
) const
337 if (!IsDir() && !IsLink() && !IsDrive())
338 s
= wxLongLong(m_size
).ToString();
347 s
= GetModificationTime();
350 #if defined(__UNIX__) || defined(__WIN32__)
354 #endif // defined(__UNIX__) || defined(__WIN32__)
357 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
363 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
365 m_fileName
= fileName
;
366 m_filePath
= filePath
;
369 void wxFileData::MakeItem( wxListItem
&item
)
371 item
.m_text
= m_fileName
;
372 item
.ClearAttributes();
374 item
.SetTextColour(*wxRED
);
376 item
.SetTextColour(*wxBLUE
);
378 item
.m_image
= m_image
;
382 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
384 item
.SetTextColour(dg
);
386 item
.m_data
= wxPtrToUInt(this);
389 //-----------------------------------------------------------------------------
391 //-----------------------------------------------------------------------------
393 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
395 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
396 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
397 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
398 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
399 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
403 wxFileCtrl::wxFileCtrl()
405 m_showHidden
= false;
407 m_sort_field
= wxFileData::FileList_Name
;
410 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
412 const wxString
& wild
,
417 const wxValidator
&validator
,
418 const wxString
&name
)
419 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
422 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
424 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
426 m_showHidden
= showHidden
;
429 m_sort_field
= wxFileData::FileList_Name
;
431 m_dirName
= wxT("*");
433 if (style
& wxLC_REPORT
)
434 ChangeToReportMode();
437 void wxFileCtrl::ChangeToListMode()
440 SetSingleStyle( wxLC_LIST
);
444 void wxFileCtrl::ChangeToReportMode()
447 SetSingleStyle( wxLC_REPORT
);
449 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
450 // don't hardcode since mm/dd is dd/mm elsewhere
452 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
453 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
454 GetTextExtent(txt
, &w
, &h
);
456 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
457 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
458 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
459 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
460 #if defined(__UNIX__)
461 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
462 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
463 #elif defined(__WIN32__)
464 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
465 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
471 void wxFileCtrl::ChangeToSmallIconMode()
474 SetSingleStyle( wxLC_SMALL_ICON
);
478 void wxFileCtrl::ShowHidden( bool show
)
484 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
487 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
488 fd
->MakeItem( item
);
489 long my_style
= GetWindowStyleFlag();
490 if (my_style
& wxLC_REPORT
)
492 ret
= InsertItem( item
);
493 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
494 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
496 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
498 ret
= InsertItem( item
);
503 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
505 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
506 wxCHECK_RET(fd
, wxT("invalid filedata"));
510 SetItemText(item
, fd
->GetFileName());
511 SetItemImage(item
, fd
->GetImageId());
513 if (GetWindowStyleFlag() & wxLC_REPORT
)
515 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
516 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
520 void wxFileCtrl::UpdateFiles()
522 // don't do anything before ShowModal() call which sets m_dirName
523 if ( m_dirName
== wxT("*") )
526 wxBusyCursor bcur
; // this may take a while...
534 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
535 if ( IsTopMostDir(m_dirName
) )
537 wxArrayString names
, paths
;
539 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
541 for (n
=0; n
<count
; n
++)
543 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
544 if (Add(fd
, item
) != -1)
551 #endif // defined(__DOS__) || defined(__WINDOWS__)
554 if ( !IsTopMostDir(m_dirName
) )
556 wxString
p(wxPathOnly(m_dirName
));
557 #if defined(__UNIX__) && !defined(__OS2__)
558 if (p
.empty()) p
= wxT("/");
560 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
561 if (Add(fd
, item
) != -1)
567 wxString
dirname(m_dirName
);
568 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
569 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
570 dirname
<< wxT('\\');
571 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
574 if ( dir
.IsOpened() )
576 wxString
dirPrefix(dirname
);
577 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
578 dirPrefix
+= wxFILE_SEP_PATH
;
580 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
585 // Get the directories first (not matched against wildcards):
586 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
589 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
590 if (Add(fd
, item
) != -1)
595 cont
= dir
.GetNext(&f
);
598 // Tokenize the wildcard string, so we can handle more than 1
599 // search pattern in a wildcard.
600 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
601 while ( tokenWild
.HasMoreTokens() )
603 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
604 wxDIR_FILES
| hiddenFlag
);
607 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
608 if (Add(fd
, item
) != -1)
613 cont
= dir
.GetNext(&f
);
619 SortItems(m_sort_field
, m_sort_foward
);
622 void wxFileCtrl::SetWild( const wxString
&wild
)
624 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
631 void wxFileCtrl::MakeDir()
633 wxString
new_name( _("NewName") );
634 wxString
path( m_dirName
);
635 path
+= wxFILE_SEP_PATH
;
637 if (wxFileExists(path
))
639 // try NewName0, NewName1 etc.
642 new_name
= _("NewName");
644 num
.Printf( wxT("%d"), i
);
648 path
+= wxFILE_SEP_PATH
;
651 } while (wxFileExists(path
));
657 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
662 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
666 long id
= Add( fd
, item
);
670 SortItems(m_sort_field
, m_sort_foward
);
671 id
= FindItem( 0, wxPtrToUInt(fd
) );
679 void wxFileCtrl::GoToParentDir()
681 if (!IsTopMostDir(m_dirName
))
683 size_t len
= m_dirName
.Len();
684 if (wxEndsWithPathSeparator(m_dirName
))
685 m_dirName
.Remove( len
-1, 1 );
686 wxString
fname( wxFileNameFromPath(m_dirName
) );
687 m_dirName
= wxPathOnly( m_dirName
);
688 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
689 if (!m_dirName
.empty())
691 if (m_dirName
.Last() == wxT('.'))
692 m_dirName
= wxEmptyString
;
694 #elif defined(__UNIX__)
695 if (m_dirName
.empty())
696 m_dirName
= wxT("/");
699 long id
= FindItem( 0, fname
);
700 if (id
!= wxNOT_FOUND
)
702 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
708 void wxFileCtrl::GoToHomeDir()
710 wxString s
= wxGetUserHome( wxString() );
714 void wxFileCtrl::GoToDir( const wxString
&dir
)
716 if (!wxDirExists(dir
)) return;
720 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
724 void wxFileCtrl::FreeItemData(wxListItem
& item
)
728 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
735 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
737 FreeItemData(event
.m_item
);
740 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
745 void wxFileCtrl::FreeAllItemsData()
748 item
.m_mask
= wxLIST_MASK_DATA
;
750 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
751 while ( item
.m_itemId
!= -1 )
755 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
759 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
761 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
764 if ((event
.GetLabel().empty()) ||
765 (event
.GetLabel() == _(".")) ||
766 (event
.GetLabel() == _("..")) ||
767 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
769 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
775 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
776 new_name
+= wxFILE_SEP_PATH
;
777 new_name
+= event
.GetLabel();
781 if (wxFileExists(new_name
))
783 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
788 if (wxRenameFile(fd
->GetFilePath(),new_name
))
790 fd
->SetNewName( new_name
, event
.GetLabel() );
791 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
792 UpdateItem( event
.GetItem() );
793 EnsureVisible( event
.GetItem() );
797 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
803 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
805 int col
= event
.GetColumn();
809 case wxFileData::FileList_Name
:
810 case wxFileData::FileList_Size
:
811 case wxFileData::FileList_Type
:
812 case wxFileData::FileList_Time
: break;
816 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
817 m_sort_foward
= !m_sort_foward
;
819 m_sort_field
= (wxFileData::fileListFieldType
)col
;
821 SortItems(m_sort_field
, m_sort_foward
);
824 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool forward
)
826 m_sort_field
= field
;
827 m_sort_foward
= forward
;
828 const long sort_dir
= forward
? 1 : -1;
830 switch (m_sort_field
)
832 case wxFileData::FileList_Size
:
833 wxListCtrl::SortItems(wxFileDataSizeCompare
, sort_dir
);
836 case wxFileData::FileList_Type
:
837 wxListCtrl::SortItems(wxFileDataTypeCompare
, sort_dir
);
840 case wxFileData::FileList_Time
:
841 wxListCtrl::SortItems(wxFileDataTimeCompare
, sort_dir
);
844 case wxFileData::FileList_Name
:
846 wxListCtrl::SortItems(wxFileDataNameCompare
, sort_dir
);
851 wxFileCtrl::~wxFileCtrl()
853 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
854 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
855 // the destruction of the wxFileCtrl we need to free any data here:
859 //-----------------------------------------------------------------------------
860 // wxGenericFileDialog
861 //-----------------------------------------------------------------------------
863 #define ID_LIST_MODE (wxID_FILEDLGG )
864 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
865 #define ID_UP_DIR (wxID_FILEDLGG + 5)
866 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
867 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
868 #define ID_CHOICE (wxID_FILEDLGG + 8)
869 #define ID_TEXT (wxID_FILEDLGG + 9)
870 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
871 #define ID_CHECK (wxID_FILEDLGG + 12)
873 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
875 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
876 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
877 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
878 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
879 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
880 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
881 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
882 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
883 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
884 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
885 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
886 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
887 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
890 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
891 bool wxGenericFileDialog::ms_lastShowHidden
= false;
893 void wxGenericFileDialog::Init()
895 m_bypassGenericImpl
= false;
902 m_upDirButton
= NULL
;
903 m_newDirButton
= NULL
;
906 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
907 const wxString
& message
,
908 const wxString
& defaultDir
,
909 const wxString
& defaultFile
,
910 const wxString
& wildCard
,
913 bool bypassGenericImpl
) : wxFileDialogBase()
916 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, bypassGenericImpl
);
919 bool wxGenericFileDialog::Create( wxWindow
*parent
,
920 const wxString
& message
,
921 const wxString
& defaultDir
,
922 const wxString
& defaultFile
,
923 const wxString
& wildCard
,
926 bool bypassGenericImpl
)
928 m_bypassGenericImpl
= bypassGenericImpl
;
930 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
931 wildCard
, style
, pos
))
936 if (m_bypassGenericImpl
)
939 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
940 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
))
945 if (wxConfig::Get(false))
947 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
949 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
953 if (m_dialogStyle
== 0)
954 m_dialogStyle
= wxOPEN
;
955 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
956 m_dialogStyle
|= wxOPEN
;
958 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
963 size_t len
= m_dir
.Len();
964 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
965 m_dir
.Remove( len
-1, 1 );
968 m_path
+= wxFILE_SEP_PATH
;
969 m_path
+= defaultFile
;
970 m_filterExtension
= wxEmptyString
;
974 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
976 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
978 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
982 but
= new wxBitmapButton(this, ID_LIST_MODE
,
983 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
985 but
->SetToolTip( _("View files as a list view") );
987 buttonsizer
->Add( but
, 0, wxALL
, 5 );
989 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
990 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
992 but
->SetToolTip( _("View files as a detailed view") );
994 buttonsizer
->Add( but
, 0, wxALL
, 5 );
996 buttonsizer
->Add( 30, 5, 1 );
998 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
999 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
1001 m_upDirButton
->SetToolTip( _("Go to parent directory") );
1003 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
1005 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1006 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
1007 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
1009 but
->SetToolTip( _("Go to home directory") );
1011 buttonsizer
->Add( but
, 0, wxALL
, 5);
1013 buttonsizer
->Add( 20, 20 );
1016 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
1017 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
1019 m_newDirButton
->SetToolTip( _("Create new directory") );
1021 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1024 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1026 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1028 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1030 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1031 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1032 staticsizer
->Add( m_static
, 1 );
1033 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1035 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
1036 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1037 style2
|= wxLC_SINGLE_SEL
;
1039 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1040 wxEmptyString
, ms_lastShowHidden
,
1041 wxDefaultPosition
, wxSize(540,200),
1046 // PDAs have a different screen layout
1047 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1049 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1050 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1051 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1052 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1055 m_choice
= new wxChoice( this, ID_CHOICE
);
1056 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1058 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1059 buttonsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxALL
, 5 );
1060 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 5 );
1061 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1065 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1067 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1068 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1069 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1070 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1071 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1073 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1074 m_choice
= new wxChoice( this, ID_CHOICE
);
1075 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1076 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1077 m_check
->SetValue( ms_lastShowHidden
);
1078 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1079 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1080 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1083 SetWildcard(wildCard
);
1085 SetAutoLayout( true );
1086 SetSizer( mainsizer
);
1088 mainsizer
->Fit( this );
1089 mainsizer
->SetSizeHints( this );
1098 wxGenericFileDialog::~wxGenericFileDialog()
1100 if (!m_bypassGenericImpl
)
1102 if (wxConfig::Get(false))
1104 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1106 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1110 const int count
= m_choice
->GetCount();
1111 for ( int i
= 0; i
< count
; i
++ )
1113 delete (wxString
*)m_choice
->GetClientData(i
);
1118 int wxGenericFileDialog::ShowModal()
1120 m_list
->GoToDir(m_dir
);
1122 m_text
->SetValue(m_fileName
);
1124 return wxDialog::ShowModal();
1127 bool wxGenericFileDialog::Show( bool show
)
1131 m_list
->GoToDir(m_dir
);
1133 m_text
->SetValue(m_fileName
);
1136 return wxDialog::Show( show
);
1139 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1141 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1142 m_list
->SetWild( *str
);
1143 m_filterIndex
= filterindex
;
1144 if ( str
->Left(2) == wxT("*.") )
1146 m_filterExtension
= str
->Mid(1);
1147 if (m_filterExtension
== _T(".*"))
1148 m_filterExtension
.clear();
1152 m_filterExtension
.clear();
1156 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1158 wxFileDialogBase::SetWildcard(wildCard
);
1160 wxArrayString wildDescriptions
, wildFilters
;
1161 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1164 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1166 const size_t countOld
= m_choice
->GetCount();
1168 for ( n
= 0; n
< countOld
; n
++ )
1170 delete (wxString
*)m_choice
->GetClientData(n
);
1173 for ( n
= 0; n
< count
; n
++ )
1175 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1178 SetFilterIndex( 0 );
1181 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1183 m_choice
->SetSelection( filterindex
);
1185 DoSetFilterIndex(filterindex
);
1188 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1190 DoSetFilterIndex((int)event
.GetInt());
1193 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1195 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1198 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1200 HandleAction( event
.m_item
.m_text
);
1203 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1205 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1206 cevent
.SetEventObject( this );
1207 GetEventHandler()->ProcessEvent( cevent
);
1210 static bool ignoreChanges
= false;
1212 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1216 // Clear selections. Otherwise when the user types in a value they may
1217 // not get the file whose name they typed.
1218 if (m_list
->GetSelectedItemCount() > 0)
1220 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1221 wxLIST_STATE_SELECTED
);
1222 while ( item
!= -1 )
1224 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1225 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1231 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1233 wxString
filename( event
.m_item
.m_text
);
1234 if (filename
== wxT("..")) return;
1236 wxString dir
= m_list
->GetDir();
1237 if (!IsTopMostDir(dir
))
1238 dir
+= wxFILE_SEP_PATH
;
1240 if (wxDirExists(dir
)) return;
1242 ignoreChanges
= true;
1243 m_text
->SetValue( filename
);
1244 ignoreChanges
= false;
1247 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1249 wxString
filename( fn
);
1250 wxString dir
= m_list
->GetDir();
1251 if (filename
.empty()) return;
1252 if (filename
== wxT(".")) return;
1254 // "some/place/" means they want to chdir not try to load "place"
1255 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1257 filename
= filename
.RemoveLast();
1259 if (filename
== wxT(".."))
1261 m_list
->GoToParentDir();
1268 if (filename
== wxT("~"))
1270 m_list
->GoToHomeDir();
1276 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1278 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1282 if (!(m_dialogStyle
& wxSAVE
))
1284 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1285 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1287 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1289 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1292 m_list
->SetWild( filename
);
1297 if (!IsTopMostDir(dir
))
1298 dir
+= wxFILE_SEP_PATH
;
1299 if (!wxIsAbsolutePath(filename
))
1305 if (wxDirExists(filename
))
1307 m_list
->GoToDir( filename
);
1312 // they really wanted a dir, but it doesn't exist
1315 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1316 wxOK
| wxICON_ERROR
);
1320 // append the default extension to the filename if it doesn't have any
1322 // VZ: the logic of testing for !wxFileExists() only for the open file
1323 // dialog is not entirely clear to me, why don't we allow saving to a
1324 // file without extension as well?
1325 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1327 filename
= AppendExtension(filename
, m_filterExtension
);
1330 // check that the file [doesn't] exist if necessary
1331 if ( (m_dialogStyle
& wxSAVE
) &&
1332 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1333 wxFileExists( filename
) )
1336 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1338 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1341 else if ( (m_dialogStyle
& wxOPEN
) &&
1342 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1343 !wxFileExists(filename
) )
1345 wxMessageBox(_("Please choose an existing file."), _("Error"),
1346 wxOK
| wxICON_ERROR
);
1349 SetPath( filename
);
1351 // change to the directory where the user went if asked
1352 if ( m_dialogStyle
& wxCHANGE_DIR
)
1355 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1357 if ( cwd
!= wxGetCwd() )
1359 wxSetWorkingDirectory(cwd
);
1363 wxCommandEvent event
;
1364 wxDialog::OnOK(event
);
1367 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1369 HandleAction( m_text
->GetValue() );
1372 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1374 m_list
->ChangeToListMode();
1375 ms_lastViewStyle
= wxLC_LIST
;
1379 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1381 m_list
->ChangeToReportMode();
1382 ms_lastViewStyle
= wxLC_REPORT
;
1386 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1388 m_list
->GoToParentDir();
1393 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1395 m_list
->GoToHomeDir();
1400 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1405 void wxGenericFileDialog::SetPath( const wxString
& path
)
1407 // not only set the full path but also update filename and dir
1409 if ( !path
.empty() )
1412 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1415 m_fileName
+= wxT(".");
1421 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1424 if (m_list
->GetSelectedItemCount() == 0)
1426 paths
.Add( GetPath() );
1430 paths
.Alloc( m_list
->GetSelectedItemCount() );
1432 wxString dir
= m_list
->GetDir();
1434 if (dir
!= wxT("/"))
1436 dir
+= wxFILE_SEP_PATH
;
1439 item
.m_mask
= wxLIST_MASK_TEXT
;
1441 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1442 while ( item
.m_itemId
!= -1 )
1444 m_list
->GetItem( item
);
1445 paths
.Add( dir
+ item
.m_text
);
1446 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1447 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1451 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1454 if (m_list
->GetSelectedItemCount() == 0)
1456 files
.Add( GetFilename() );
1459 files
.Alloc( m_list
->GetSelectedItemCount() );
1462 item
.m_mask
= wxLIST_MASK_TEXT
;
1464 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1465 while ( item
.m_itemId
!= -1 )
1467 m_list
->GetItem( item
);
1468 files
.Add( item
.m_text
);
1469 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1470 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1474 void wxGenericFileDialog::UpdateControls()
1476 wxString dir
= m_list
->GetDir();
1477 m_static
->SetLabel(dir
);
1479 bool enable
= !IsTopMostDir(dir
);
1480 m_upDirButton
->Enable(enable
);
1482 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1483 m_newDirButton
->Enable(enable
);
1484 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1487 #ifdef USE_GENERIC_FILEDIALOG
1489 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
1491 #endif // USE_GENERIC_FILEDIALOG
1493 #endif // wxUSE_FILEDLG