1 //////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "filedlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 // NOTE : it probably also supports MAC, untested
26 #if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__)
27 #error wxGenericFileDialog currently only supports Unix, win32 and DOS
30 #include "wx/checkbox.h"
31 #include "wx/textctrl.h"
32 #include "wx/choice.h"
33 #include "wx/checkbox.h"
34 #include "wx/stattext.h"
38 #include "wx/msgdlg.h"
40 #include "wx/bmpbuttn.h"
41 #include "wx/tokenzr.h"
42 #include "wx/config.h"
43 #include "wx/imaglist.h"
45 #include "wx/artprov.h"
46 #include "wx/settings.h"
47 #include "wx/file.h" // for wxS_IXXX constants only
48 #include "wx/filedlg.h" // wxOPEN, wxSAVE...
49 #include "wx/generic/filedlgg.h"
50 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
53 #include "wx/tooltip.h"
56 #include <sys/types.h>
68 #include "wx/msw/wrapwin.h"
69 #include "wx/msw/mslu.h"
77 #if defined(__UNIX__) || defined(__DOS__)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
86 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long data
)
88 wxFileData
*fd1
= (wxFileData
*)data1
;
89 wxFileData
*fd2
= (wxFileData
*)data2
;
90 if (fd1
->GetFileName() == wxT("..")) return -data
;
91 if (fd2
->GetFileName() == wxT("..")) return data
;
92 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
93 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
94 return data
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
98 int wxCALLBACK
wxFileDataSizeCompare( long data1
, long data2
, long data
)
100 wxFileData
*fd1
= (wxFileData
*)data1
;
101 wxFileData
*fd2
= (wxFileData
*)data2
;
102 if (fd1
->GetFileName() == wxT("..")) return -data
;
103 if (fd2
->GetFileName() == wxT("..")) return data
;
104 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
105 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
106 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
107 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
108 return data
*(fd1
->GetSize() - fd2
->GetSize());
112 int wxCALLBACK
wxFileDataTypeCompare( long data1
, long data2
, long data
)
114 wxFileData
*fd1
= (wxFileData
*)data1
;
115 wxFileData
*fd2
= (wxFileData
*)data2
;
116 if (fd1
->GetFileName() == wxT("..")) return -data
;
117 if (fd2
->GetFileName() == wxT("..")) return data
;
118 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
119 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
120 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
121 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
122 return data
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
126 int wxCALLBACK
wxFileDataTimeCompare( long data1
, long data2
, long data
)
128 wxFileData
*fd1
= (wxFileData
*)data1
;
129 wxFileData
*fd2
= (wxFileData
*)data2
;
130 if (fd1
->GetFileName() == wxT("..")) return -data
;
131 if (fd2
->GetFileName() == wxT("..")) return data
;
132 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
133 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
135 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? int(data
) : -int(data
);
138 #if defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)
139 #define IsTopMostDir(dir) (dir.empty())
141 #define IsTopMostDir(dir) (dir == wxT("/"))
144 // defined in src/generic/dirctrlg.cpp
145 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
147 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
151 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
154 m_fileName
= fileName
;
155 m_filePath
= filePath
;
162 void wxFileData::Init()
165 m_type
= wxFileData::is_file
;
166 m_image
= wxFileIconsTable::file
;
169 void wxFileData::Copy( const wxFileData
& fileData
)
171 m_fileName
= fileData
.GetFileName();
172 m_filePath
= fileData
.GetFilePath();
173 m_size
= fileData
.GetSize();
174 m_dateTime
= fileData
.GetDateTime();
175 m_permissions
= fileData
.GetPermissions();
176 m_type
= fileData
.GetType();
177 m_image
= fileData
.GetImageId();
180 void wxFileData::ReadData()
188 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
189 // c:\.. is a drive don't stat it
190 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
196 #endif // __DOS__ || __WINDOWS__
200 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
201 lstat( m_filePath
.fn_str(), &buff
);
202 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
204 // only translate to file charset if we don't go by our
205 // wxStat implementation
206 #ifndef wxNEED_WX_UNISTD_H
207 wxStat( m_filePath
.fn_str() , &buff
);
209 wxStat( m_filePath
, &buff
);
213 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
214 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
216 // try to get a better icon
217 if (m_image
== wxFileIconsTable::file
)
219 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
221 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
224 m_image
= wxFileIconsTable::executable
;
228 m_size
= (long)buff
.st_size
;
230 m_dateTime
= buff
.st_mtime
;
232 #if defined(__UNIX__)
233 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
234 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
235 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
236 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
237 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
238 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
239 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
240 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
241 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
242 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
243 #elif defined(__WIN32__)
244 DWORD attribs
= GetFileAttributes(m_filePath
);
245 if (attribs
!= (DWORD
)-1)
247 m_permissions
.Printf(_T("%c%c%c%c"),
248 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
249 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
250 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
251 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
256 wxString
wxFileData::GetFileType() const
264 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
265 return m_fileName
.AfterLast(wxT('.'));
267 return wxEmptyString
;
270 wxString
wxFileData::GetModificationTime() const
272 // want time as 01:02 so they line up nicely, no %r in WIN32
273 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
276 wxString
wxFileData::GetHint() const
278 wxString s
= m_filePath
;
288 s
+= wxString::Format( _("%ld bytes"), m_size
);
294 s
<< GetModificationTime()
302 wxString
wxFileData::GetEntry( fileListFieldType num
) const
312 if (!IsDir() && !IsLink() && !IsDrive())
313 s
.Printf(_T("%ld"), m_size
);
322 s
= GetModificationTime();
325 #if defined(__UNIX__) || defined(__WIN32__)
329 #endif // defined(__UNIX__) || defined(__WIN32__)
332 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
338 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
340 m_fileName
= fileName
;
341 m_filePath
= filePath
;
344 void wxFileData::MakeItem( wxListItem
&item
)
346 item
.m_text
= m_fileName
;
347 item
.ClearAttributes();
349 item
.SetTextColour(*wxRED
);
351 item
.SetTextColour(*wxBLUE
);
353 item
.m_image
= m_image
;
357 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
359 item
.SetTextColour(dg
);
361 item
.m_data
= (long)this;
364 //-----------------------------------------------------------------------------
366 //-----------------------------------------------------------------------------
368 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
370 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
371 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
372 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
373 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
374 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
378 wxFileCtrl::wxFileCtrl()
380 m_showHidden
= false;
382 m_sort_field
= wxFileData::FileList_Name
;
385 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
387 const wxString
& wild
,
392 const wxValidator
&validator
,
393 const wxString
&name
)
394 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
397 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
399 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
401 m_showHidden
= showHidden
;
404 m_sort_field
= wxFileData::FileList_Name
;
406 m_dirName
= wxT("*");
408 if (style
& wxLC_REPORT
)
409 ChangeToReportMode();
412 void wxFileCtrl::ChangeToListMode()
415 SetSingleStyle( wxLC_LIST
);
419 void wxFileCtrl::ChangeToReportMode()
422 SetSingleStyle( wxLC_REPORT
);
424 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
425 // don't hardcode since mm/dd is dd/mm elsewhere
427 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
428 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
429 GetTextExtent(txt
, &w
, &h
);
431 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
432 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
433 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
434 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
435 #if defined(__UNIX__)
436 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
437 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
438 #elif defined(__WIN32__)
439 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
440 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
446 void wxFileCtrl::ChangeToSmallIconMode()
449 SetSingleStyle( wxLC_SMALL_ICON
);
453 void wxFileCtrl::ShowHidden( bool show
)
459 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
462 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
463 fd
->MakeItem( item
);
464 long my_style
= GetWindowStyleFlag();
465 if (my_style
& wxLC_REPORT
)
467 ret
= InsertItem( item
);
468 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
469 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
471 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
473 ret
= InsertItem( item
);
478 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
480 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
481 wxCHECK_RET(fd
, wxT("invalid filedata"));
485 SetItemText(item
, fd
->GetFileName());
486 SetItemImage(item
, fd
->GetImageId());
488 if (GetWindowStyleFlag() & wxLC_REPORT
)
490 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
491 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
495 void wxFileCtrl::UpdateFiles()
497 // don't do anything before ShowModal() call which sets m_dirName
498 if ( m_dirName
== wxT("*") )
501 wxBusyCursor bcur
; // this may take a while...
509 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
510 if ( IsTopMostDir(m_dirName
) )
512 wxArrayString names
, paths
;
514 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
516 for (n
=0; n
<count
; n
++)
518 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
519 if (Add(fd
, item
) != -1)
526 #endif // defined(__DOS__) || defined(__WINDOWS__)
529 if ( !IsTopMostDir(m_dirName
) )
531 wxString
p(wxPathOnly(m_dirName
));
532 #if defined(__UNIX__) && !defined(__OS2__)
533 if (p
.empty()) p
= wxT("/");
535 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
536 if (Add(fd
, item
) != -1)
542 wxString
dirname(m_dirName
);
543 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
544 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
545 dirname
<< wxT('\\');
546 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
549 if ( dir
.IsOpened() )
551 wxString
dirPrefix(dirname
);
552 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
553 dirPrefix
+= wxFILE_SEP_PATH
;
555 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
560 // Get the directories first (not matched against wildcards):
561 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
564 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
565 if (Add(fd
, item
) != -1)
570 cont
= dir
.GetNext(&f
);
573 // Tokenize the wildcard string, so we can handle more than 1
574 // search pattern in a wildcard.
575 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
576 while ( tokenWild
.HasMoreTokens() )
578 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
579 wxDIR_FILES
| hiddenFlag
);
582 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
583 if (Add(fd
, item
) != -1)
588 cont
= dir
.GetNext(&f
);
594 SortItems(m_sort_field
, m_sort_foward
);
597 void wxFileCtrl::SetWild( const wxString
&wild
)
599 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
606 void wxFileCtrl::MakeDir()
608 wxString
new_name( _("NewName") );
609 wxString
path( m_dirName
);
610 path
+= wxFILE_SEP_PATH
;
612 if (wxFileExists(path
))
614 // try NewName0, NewName1 etc.
617 new_name
= _("NewName");
619 num
.Printf( wxT("%d"), i
);
623 path
+= wxFILE_SEP_PATH
;
626 } while (wxFileExists(path
));
632 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
637 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
641 long id
= Add( fd
, item
);
645 SortItems(m_sort_field
, m_sort_foward
);
646 id
= FindItem( 0, (long)fd
);
654 void wxFileCtrl::GoToParentDir()
656 if (!IsTopMostDir(m_dirName
))
658 size_t len
= m_dirName
.Len();
659 if (wxEndsWithPathSeparator(m_dirName
))
660 m_dirName
.Remove( len
-1, 1 );
661 wxString
fname( wxFileNameFromPath(m_dirName
) );
662 m_dirName
= wxPathOnly( m_dirName
);
663 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
664 if (!m_dirName
.empty())
666 if (m_dirName
.Last() == wxT('.'))
667 m_dirName
= wxEmptyString
;
669 #elif defined(__UNIX__)
670 if (m_dirName
.empty())
671 m_dirName
= wxT("/");
674 long id
= FindItem( 0, fname
);
675 if (id
!= wxNOT_FOUND
)
677 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
683 void wxFileCtrl::GoToHomeDir()
685 wxString s
= wxGetUserHome( wxString() );
689 void wxFileCtrl::GoToDir( const wxString
&dir
)
691 if (!wxDirExists(dir
)) return;
695 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
699 void wxFileCtrl::FreeItemData(wxListItem
& item
)
703 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
710 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
712 FreeItemData(event
.m_item
);
715 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
720 void wxFileCtrl::FreeAllItemsData()
723 item
.m_mask
= wxLIST_MASK_DATA
;
725 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
726 while ( item
.m_itemId
!= -1 )
730 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
734 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
736 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
739 if ((event
.GetLabel().empty()) ||
740 (event
.GetLabel() == _(".")) ||
741 (event
.GetLabel() == _("..")) ||
742 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
744 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
750 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
751 new_name
+= wxFILE_SEP_PATH
;
752 new_name
+= event
.GetLabel();
756 if (wxFileExists(new_name
))
758 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
763 if (wxRenameFile(fd
->GetFilePath(),new_name
))
765 fd
->SetNewName( new_name
, event
.GetLabel() );
766 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
767 UpdateItem( event
.GetItem() );
768 EnsureVisible( event
.GetItem() );
772 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
778 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
780 int col
= event
.GetColumn();
784 case wxFileData::FileList_Name
:
785 case wxFileData::FileList_Size
:
786 case wxFileData::FileList_Type
:
787 case wxFileData::FileList_Time
: break;
791 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
792 m_sort_foward
= !m_sort_foward
;
794 m_sort_field
= (wxFileData::fileListFieldType
)col
;
796 SortItems(m_sort_field
, m_sort_foward
);
799 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
801 m_sort_field
= field
;
802 m_sort_foward
= foward
;
803 long sort_dir
= foward
? 1 : -1;
805 switch (m_sort_field
)
807 case wxFileData::FileList_Name
:
809 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
812 case wxFileData::FileList_Size
:
814 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
817 case wxFileData::FileList_Type
:
819 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
822 case wxFileData::FileList_Time
:
824 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
831 wxFileCtrl::~wxFileCtrl()
833 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
834 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
835 // the destruction of the wxFileCtrl we need to free any data here:
839 //-----------------------------------------------------------------------------
840 // wxGenericFileDialog
841 //-----------------------------------------------------------------------------
843 #define ID_LIST_MODE (wxID_FILEDLGG )
844 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
845 #define ID_UP_DIR (wxID_FILEDLGG + 5)
846 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
847 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
848 #define ID_CHOICE (wxID_FILEDLGG + 8)
849 #define ID_TEXT (wxID_FILEDLGG + 9)
850 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
851 #define ID_CHECK (wxID_FILEDLGG + 12)
853 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
855 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
856 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
857 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
858 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
859 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
860 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
861 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
862 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
863 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
864 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
865 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
866 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
867 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
870 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
871 bool wxGenericFileDialog::ms_lastShowHidden
= false;
873 void wxGenericFileDialog::Init()
875 m_bypassGenericImpl
= false;
882 m_upDirButton
= NULL
;
883 m_newDirButton
= NULL
;
886 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
887 const wxString
& message
,
888 const wxString
& defaultDir
,
889 const wxString
& defaultFile
,
890 const wxString
& wildCard
,
893 bool bypassGenericImpl
) : wxFileDialogBase()
896 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, bypassGenericImpl
);
899 bool wxGenericFileDialog::Create( wxWindow
*parent
,
900 const wxString
& message
,
901 const wxString
& defaultDir
,
902 const wxString
& defaultFile
,
903 const wxString
& wildCard
,
906 bool bypassGenericImpl
)
908 m_bypassGenericImpl
= bypassGenericImpl
;
910 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
911 wildCard
, style
, pos
))
916 if (m_bypassGenericImpl
)
919 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
920 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
))
925 if (wxConfig::Get(false))
927 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
929 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
933 if (m_dialogStyle
== 0)
934 m_dialogStyle
= wxOPEN
;
935 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
936 m_dialogStyle
|= wxOPEN
;
938 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
943 size_t len
= m_dir
.Len();
944 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
945 m_dir
.Remove( len
-1, 1 );
948 m_path
+= wxFILE_SEP_PATH
;
949 m_path
+= defaultFile
;
950 m_filterExtension
= wxEmptyString
;
954 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
956 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
958 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
962 but
= new wxBitmapButton(this, ID_LIST_MODE
,
963 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
965 but
->SetToolTip( _("View files as a list view") );
967 buttonsizer
->Add( but
, 0, wxALL
, 5 );
969 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
970 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
972 but
->SetToolTip( _("View files as a detailed view") );
974 buttonsizer
->Add( but
, 0, wxALL
, 5 );
976 buttonsizer
->Add( 30, 5, 1 );
978 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
979 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
981 m_upDirButton
->SetToolTip( _("Go to parent directory") );
983 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
985 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
986 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
987 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
989 but
->SetToolTip( _("Go to home directory") );
991 buttonsizer
->Add( but
, 0, wxALL
, 5);
993 buttonsizer
->Add( 20, 20 );
996 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
997 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
999 m_newDirButton
->SetToolTip( _("Create new directory") );
1001 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1004 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1006 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1008 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1010 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1011 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1012 staticsizer
->Add( m_static
, 1 );
1013 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1015 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
1016 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1017 style2
|= wxLC_SINGLE_SEL
;
1019 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1020 wxEmptyString
, ms_lastShowHidden
,
1021 wxDefaultPosition
, wxSize(540,200),
1026 // PDAs have a different screen layout
1027 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1029 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1030 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1031 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1032 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1035 m_choice
= new wxChoice( this, ID_CHOICE
);
1036 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1038 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1039 buttonsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxALL
, 5 );
1040 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 5 );
1041 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1045 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1047 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1048 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1049 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1050 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1051 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1053 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1054 m_choice
= new wxChoice( this, ID_CHOICE
);
1055 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1056 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1057 m_check
->SetValue( ms_lastShowHidden
);
1058 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1059 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1060 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1063 SetWildcard(wildCard
);
1065 SetAutoLayout( true );
1066 SetSizer( mainsizer
);
1068 mainsizer
->Fit( this );
1069 mainsizer
->SetSizeHints( this );
1078 wxGenericFileDialog::~wxGenericFileDialog()
1080 if (!m_bypassGenericImpl
)
1082 if (wxConfig::Get(false))
1084 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1086 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1090 const int count
= m_choice
->GetCount();
1091 for ( int i
= 0; i
< count
; i
++ )
1093 delete (wxString
*)m_choice
->GetClientData(i
);
1098 int wxGenericFileDialog::ShowModal()
1100 m_list
->GoToDir(m_dir
);
1102 m_text
->SetValue(m_fileName
);
1104 return wxDialog::ShowModal();
1107 bool wxGenericFileDialog::Show( bool show
)
1111 m_list
->GoToDir(m_dir
);
1113 m_text
->SetValue(m_fileName
);
1116 return wxDialog::Show( show
);
1119 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1121 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1122 m_list
->SetWild( *str
);
1123 m_filterIndex
= filterindex
;
1124 if ( str
->Left(2) == wxT("*.") )
1126 m_filterExtension
= str
->Mid(1);
1127 if (m_filterExtension
== _T(".*"))
1128 m_filterExtension
.clear();
1132 m_filterExtension
.clear();
1136 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1138 wxFileDialogBase::SetWildcard(wildCard
);
1140 wxArrayString wildDescriptions
, wildFilters
;
1141 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1144 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1146 const size_t countOld
= m_choice
->GetCount();
1148 for ( n
= 0; n
< countOld
; n
++ )
1150 delete (wxString
*)m_choice
->GetClientData(n
);
1153 for ( n
= 0; n
< count
; n
++ )
1155 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1158 SetFilterIndex( 0 );
1161 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1163 m_choice
->SetSelection( filterindex
);
1165 DoSetFilterIndex(filterindex
);
1168 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1170 DoSetFilterIndex((int)event
.GetInt());
1173 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1175 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1178 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1180 HandleAction( event
.m_item
.m_text
);
1183 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1185 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1186 cevent
.SetEventObject( this );
1187 GetEventHandler()->ProcessEvent( cevent
);
1190 static bool ignoreChanges
= false;
1192 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1196 // Clear selections. Otherwise when the user types in a value they may
1197 // not get the file whose name they typed.
1198 if (m_list
->GetSelectedItemCount() > 0)
1200 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1201 wxLIST_STATE_SELECTED
);
1202 while ( item
!= -1 )
1204 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1205 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1211 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1213 wxString
filename( event
.m_item
.m_text
);
1214 if (filename
== wxT("..")) return;
1216 wxString dir
= m_list
->GetDir();
1217 if (!IsTopMostDir(dir
))
1218 dir
+= wxFILE_SEP_PATH
;
1220 if (wxDirExists(dir
)) return;
1222 ignoreChanges
= true;
1223 m_text
->SetValue( filename
);
1224 ignoreChanges
= false;
1227 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1229 wxString
filename( fn
);
1230 wxString dir
= m_list
->GetDir();
1231 if (filename
.empty()) return;
1232 if (filename
== wxT(".")) return;
1234 // "some/place/" means they want to chdir not try to load "place"
1235 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1237 filename
= filename
.RemoveLast();
1239 if (filename
== wxT(".."))
1241 m_list
->GoToParentDir();
1248 if (filename
== wxT("~"))
1250 m_list
->GoToHomeDir();
1256 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1258 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1262 if (!(m_dialogStyle
& wxSAVE
))
1264 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1265 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1267 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1269 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1272 m_list
->SetWild( filename
);
1277 if (!IsTopMostDir(dir
))
1278 dir
+= wxFILE_SEP_PATH
;
1279 if (!wxIsAbsolutePath(filename
))
1285 if (wxDirExists(filename
))
1287 m_list
->GoToDir( filename
);
1292 // they really wanted a dir, but it doesn't exist
1295 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1296 wxOK
| wxICON_ERROR
);
1300 // append the default extension to the filename if it doesn't have any
1302 // VZ: the logic of testing for !wxFileExists() only for the open file
1303 // dialog is not entirely clear to me, why don't we allow saving to a
1304 // file without extension as well?
1305 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1307 filename
= AppendExtension(filename
, m_filterExtension
);
1310 // check that the file [doesn't] exist if necessary
1311 if ( (m_dialogStyle
& wxSAVE
) &&
1312 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1313 wxFileExists( filename
) )
1316 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1318 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1321 else if ( (m_dialogStyle
& wxOPEN
) &&
1322 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1323 !wxFileExists(filename
) )
1325 wxMessageBox(_("Please choose an existing file."), _("Error"),
1326 wxOK
| wxICON_ERROR
);
1329 SetPath( filename
);
1331 // change to the directory where the user went if asked
1332 if ( m_dialogStyle
& wxCHANGE_DIR
)
1335 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1337 if ( cwd
!= wxGetCwd() )
1339 wxSetWorkingDirectory(cwd
);
1343 wxCommandEvent event
;
1344 wxDialog::OnOK(event
);
1347 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1349 HandleAction( m_text
->GetValue() );
1352 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1354 m_list
->ChangeToListMode();
1355 ms_lastViewStyle
= wxLC_LIST
;
1359 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1361 m_list
->ChangeToReportMode();
1362 ms_lastViewStyle
= wxLC_REPORT
;
1366 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1368 m_list
->GoToParentDir();
1373 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1375 m_list
->GoToHomeDir();
1380 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1385 void wxGenericFileDialog::SetPath( const wxString
& path
)
1387 // not only set the full path but also update filename and dir
1389 if ( !path
.empty() )
1392 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1395 m_fileName
+= wxT(".");
1401 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1404 if (m_list
->GetSelectedItemCount() == 0)
1406 paths
.Add( GetPath() );
1410 paths
.Alloc( m_list
->GetSelectedItemCount() );
1412 wxString dir
= m_list
->GetDir();
1414 if (dir
!= wxT("/"))
1416 dir
+= wxFILE_SEP_PATH
;
1419 item
.m_mask
= wxLIST_MASK_TEXT
;
1421 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1422 while ( item
.m_itemId
!= -1 )
1424 m_list
->GetItem( item
);
1425 paths
.Add( dir
+ item
.m_text
);
1426 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1427 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1431 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1434 if (m_list
->GetSelectedItemCount() == 0)
1436 files
.Add( GetFilename() );
1439 files
.Alloc( m_list
->GetSelectedItemCount() );
1442 item
.m_mask
= wxLIST_MASK_TEXT
;
1444 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1445 while ( item
.m_itemId
!= -1 )
1447 m_list
->GetItem( item
);
1448 files
.Add( item
.m_text
);
1449 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1450 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1454 void wxGenericFileDialog::UpdateControls()
1456 wxString dir
= m_list
->GetDir();
1457 m_static
->SetLabel(dir
);
1459 bool enable
= !IsTopMostDir(dir
);
1460 m_upDirButton
->Enable(enable
);
1462 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1463 m_newDirButton
->Enable(enable
);
1464 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1467 #ifdef USE_GENERIC_FILEDIALOG
1469 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1471 #endif // USE_GENERIC_FILEDIALOG
1473 #endif // wxUSE_FILEDLG