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"
23 #if wxUSE_FILEDLG && !defined(__WXGTK24__)
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(__UNIX__) && !defined(__OS2__)
139 #define IsTopMostDir(dir) (dir == wxT("/"))
142 #if defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)
143 #define IsTopMostDir(dir) (dir.IsEmpty())
146 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
147 // defined in src/generic/dirctrlg.cpp
148 extern bool wxIsDriveAvailable(const wxString
& dirName
);
151 // defined in src/generic/dirctrlg.cpp
152 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
154 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
158 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
160 m_fileName
= fileName
;
161 m_filePath
= filePath
;
168 void wxFileData::Copy( const wxFileData
& fileData
)
170 m_fileName
= fileData
.GetFileName();
171 m_filePath
= fileData
.GetFilePath();
172 m_size
= fileData
.GetSize();
173 m_dateTime
= fileData
.GetDateTime();
174 m_permissions
= fileData
.GetPermissions();
175 m_type
= fileData
.GetType();
176 m_image
= GetImageId();
179 void wxFileData::ReadData()
187 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
188 // c:\.. is a drive don't stat it
189 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
195 #endif // __DOS__ || __WINDOWS__
199 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
200 lstat( m_filePath
.fn_str(), &buff
);
201 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
203 // only translate to file charset if we don't go by our
204 // wxStat implementation
205 #ifndef wxNEED_WX_UNISTD_H
206 wxStat( m_filePath
.fn_str() , &buff
);
208 wxStat( m_filePath
, &buff
);
212 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
213 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
215 // try to get a better icon
216 if (m_image
== wxFileIconsTable::file
)
218 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
220 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
223 m_image
= wxFileIconsTable::executable
;
227 m_size
= (long)buff
.st_size
;
229 m_dateTime
= buff
.st_mtime
;
231 #if defined(__UNIX__)
232 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
233 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
234 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
235 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
236 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
237 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
238 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
239 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
240 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
241 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
242 #elif defined(__WIN32__)
243 DWORD attribs
= GetFileAttributes(m_filePath
);
244 if (attribs
!= (DWORD
)-1)
246 m_permissions
.Printf(_T("%c%c%c%c"),
247 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
248 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
249 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
250 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
255 wxString
wxFileData::GetFileType() const
263 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
264 return m_fileName
.AfterLast(wxT('.'));
266 return wxEmptyString
;
269 wxString
wxFileData::GetModificationTime() const
271 // want time as 01:02 so they line up nicely, no %r in WIN32
272 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
275 wxString
wxFileData::GetHint() const
277 wxString s
= m_filePath
;
287 s
+= wxString::Format( _("%ld bytes"), m_size
);
293 s
<< GetModificationTime()
301 wxString
wxFileData::GetEntry( fileListFieldType num
) const
311 if (!IsDir() && !IsLink() && !IsDrive())
312 s
.Printf(_T("%ld"), m_size
);
321 s
= GetModificationTime();
324 #if defined(__UNIX__) || defined(__WIN32__)
328 #endif // defined(__UNIX__) || defined(__WIN32__)
331 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
337 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
339 m_fileName
= fileName
;
340 m_filePath
= filePath
;
343 void wxFileData::MakeItem( wxListItem
&item
)
345 item
.m_text
= m_fileName
;
346 item
.ClearAttributes();
348 item
.SetTextColour(*wxRED
);
350 item
.SetTextColour(*wxBLUE
);
352 item
.m_image
= m_image
;
356 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
358 item
.SetTextColour(dg
);
360 item
.m_data
= (long)this;
363 //-----------------------------------------------------------------------------
365 //-----------------------------------------------------------------------------
367 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
369 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
370 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
371 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
372 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
373 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
377 wxFileCtrl::wxFileCtrl()
379 m_showHidden
= false;
381 m_sort_field
= wxFileData::FileList_Name
;
384 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
386 const wxString
& wild
,
391 const wxValidator
&validator
,
392 const wxString
&name
)
393 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
396 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
398 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
400 m_showHidden
= showHidden
;
403 m_sort_field
= wxFileData::FileList_Name
;
405 m_dirName
= wxT("*");
407 if (style
& wxLC_REPORT
)
408 ChangeToReportMode();
411 void wxFileCtrl::ChangeToListMode()
414 SetSingleStyle( wxLC_LIST
);
418 void wxFileCtrl::ChangeToReportMode()
421 SetSingleStyle( wxLC_REPORT
);
423 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
424 // don't hardcode since mm/dd is dd/mm elsewhere
426 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
427 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
428 GetTextExtent(txt
, &w
, &h
);
430 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
431 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
432 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
433 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
434 #if defined(__UNIX__)
435 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
436 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
437 #elif defined(__WIN32__)
438 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
439 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
445 void wxFileCtrl::ChangeToSmallIconMode()
448 SetSingleStyle( wxLC_SMALL_ICON
);
452 void wxFileCtrl::ShowHidden( bool show
)
458 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
461 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
462 fd
->MakeItem( item
);
463 long my_style
= GetWindowStyleFlag();
464 if (my_style
& wxLC_REPORT
)
466 ret
= InsertItem( item
);
467 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
468 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
470 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
472 ret
= InsertItem( item
);
477 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
479 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
480 wxCHECK_RET(fd
, wxT("invalid filedata"));
484 SetItemText(item
, fd
->GetFileName());
485 SetItemImage(item
, fd
->GetImageId());
487 if (GetWindowStyleFlag() & wxLC_REPORT
)
489 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
490 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
494 void wxFileCtrl::UpdateFiles()
496 // don't do anything before ShowModal() call which sets m_dirName
497 if ( m_dirName
== wxT("*") )
500 wxBusyCursor bcur
; // this may take a while...
508 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
509 if ( IsTopMostDir(m_dirName
) )
511 wxArrayString names
, paths
;
513 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
515 for (n
=0; n
<count
; n
++)
517 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
518 if (Add(fd
, item
) != -1)
525 #endif // defined(__DOS__) || defined(__WINDOWS__)
528 if ( !IsTopMostDir(m_dirName
) )
530 wxString
p(wxPathOnly(m_dirName
));
531 #if defined(__UNIX__) && !defined(__OS2__)
532 if (p
.IsEmpty()) p
= wxT("/");
534 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
535 if (Add(fd
, item
) != -1)
541 wxString
dirname(m_dirName
);
542 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
543 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
544 dirname
<< wxT('\\');
545 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
548 if ( dir
.IsOpened() )
550 wxString
dirPrefix(dirname
);
551 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
552 dirPrefix
+= wxFILE_SEP_PATH
;
554 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
559 // Get the directories first (not matched against wildcards):
560 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
563 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
564 if (Add(fd
, item
) != -1)
569 cont
= dir
.GetNext(&f
);
572 // Tokenize the wildcard string, so we can handle more than 1
573 // search pattern in a wildcard.
574 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
575 while ( tokenWild
.HasMoreTokens() )
577 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
578 wxDIR_FILES
| hiddenFlag
);
581 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
582 if (Add(fd
, item
) != -1)
587 cont
= dir
.GetNext(&f
);
593 SortItems(m_sort_field
, m_sort_foward
);
596 void wxFileCtrl::SetWild( const wxString
&wild
)
598 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
605 void wxFileCtrl::MakeDir()
607 wxString
new_name( _("NewName") );
608 wxString
path( m_dirName
);
609 path
+= wxFILE_SEP_PATH
;
611 if (wxFileExists(path
))
613 // try NewName0, NewName1 etc.
616 new_name
= _("NewName");
618 num
.Printf( wxT("%d"), i
);
622 path
+= wxFILE_SEP_PATH
;
625 } while (wxFileExists(path
));
631 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
636 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
640 long id
= Add( fd
, item
);
644 SortItems(m_sort_field
, m_sort_foward
);
645 id
= FindItem( 0, (long)fd
);
653 void wxFileCtrl::GoToParentDir()
655 if (!IsTopMostDir(m_dirName
))
657 size_t len
= m_dirName
.Len();
658 if (wxEndsWithPathSeparator(m_dirName
))
659 m_dirName
.Remove( len
-1, 1 );
660 wxString
fname( wxFileNameFromPath(m_dirName
) );
661 m_dirName
= wxPathOnly( m_dirName
);
662 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
663 if (!m_dirName
.IsEmpty())
665 if (m_dirName
.Last() == wxT('.'))
666 m_dirName
= wxEmptyString
;
668 #elif defined(__UNIX__)
669 if (m_dirName
.IsEmpty())
670 m_dirName
= wxT("/");
673 long id
= FindItem( 0, fname
);
676 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
682 void wxFileCtrl::GoToHomeDir()
684 wxString s
= wxGetUserHome( wxString() );
688 void wxFileCtrl::GoToDir( const wxString
&dir
)
690 if (!wxDirExists(dir
)) return;
694 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
698 void wxFileCtrl::FreeItemData(wxListItem
& item
)
702 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
709 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
711 FreeItemData(event
.m_item
);
714 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
719 void wxFileCtrl::FreeAllItemsData()
722 item
.m_mask
= wxLIST_MASK_DATA
;
724 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
725 while ( item
.m_itemId
!= -1 )
729 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
733 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
735 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
738 if ((event
.GetLabel().IsEmpty()) ||
739 (event
.GetLabel() == _(".")) ||
740 (event
.GetLabel() == _("..")) ||
741 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
743 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
749 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
750 new_name
+= wxFILE_SEP_PATH
;
751 new_name
+= event
.GetLabel();
755 if (wxFileExists(new_name
))
757 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
762 if (wxRenameFile(fd
->GetFilePath(),new_name
))
764 fd
->SetNewName( new_name
, event
.GetLabel() );
765 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
766 UpdateItem( event
.GetItem() );
767 EnsureVisible( event
.GetItem() );
771 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
777 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
779 int col
= event
.GetColumn();
783 case wxFileData::FileList_Name
:
784 case wxFileData::FileList_Size
:
785 case wxFileData::FileList_Type
:
786 case wxFileData::FileList_Time
: break;
790 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
791 m_sort_foward
= !m_sort_foward
;
793 m_sort_field
= (wxFileData::fileListFieldType
)col
;
795 SortItems(m_sort_field
, m_sort_foward
);
798 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
800 m_sort_field
= field
;
801 m_sort_foward
= foward
;
802 long sort_dir
= foward
? 1 : -1;
804 switch (m_sort_field
)
806 case wxFileData::FileList_Name
:
808 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
811 case wxFileData::FileList_Size
:
813 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
816 case wxFileData::FileList_Type
:
818 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
821 case wxFileData::FileList_Time
:
823 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
830 wxFileCtrl::~wxFileCtrl()
832 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
833 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
834 // the destruction of the wxFileCtrl we need to free any data here:
838 //-----------------------------------------------------------------------------
839 // wxGenericFileDialog
840 //-----------------------------------------------------------------------------
842 #define ID_LIST_MODE (wxID_FILEDLGG )
843 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
844 #define ID_UP_DIR (wxID_FILEDLGG + 5)
845 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
846 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
847 #define ID_CHOICE (wxID_FILEDLGG + 8)
848 #define ID_TEXT (wxID_FILEDLGG + 9)
849 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
850 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
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 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
874 const wxString
& message
,
875 const wxString
& defaultDir
,
876 const wxString
& defaultFile
,
877 const wxString
& wildCard
,
880 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
)
882 wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
883 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
885 if (wxConfig::Get(false))
887 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
889 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
893 if (m_dialogStyle
== 0)
894 m_dialogStyle
= wxOPEN
;
895 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
896 m_dialogStyle
|= wxOPEN
;
898 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
903 size_t len
= m_dir
.Len();
904 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
905 m_dir
.Remove( len
-1, 1 );
908 m_path
+= wxFILE_SEP_PATH
;
909 m_path
+= defaultFile
;
910 m_filterExtension
= wxEmptyString
;
914 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
916 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
918 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
922 but
= new wxBitmapButton(this, ID_LIST_MODE
,
923 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
925 but
->SetToolTip( _("View files as a list view") );
927 buttonsizer
->Add( but
, 0, wxALL
, 5 );
929 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
930 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
932 but
->SetToolTip( _("View files as a detailed view") );
934 buttonsizer
->Add( but
, 0, wxALL
, 5 );
936 buttonsizer
->Add( 30, 5, 1 );
938 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
939 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
941 m_upDirButton
->SetToolTip( _("Go to parent directory") );
943 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
945 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
946 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
947 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
949 but
->SetToolTip( _("Go to home directory") );
951 buttonsizer
->Add( but
, 0, wxALL
, 5);
953 buttonsizer
->Add( 20, 20 );
956 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
957 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
959 m_newDirButton
->SetToolTip( _("Create new directory") );
961 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
964 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
966 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
968 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
970 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
971 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
972 staticsizer
->Add( m_static
, 1 );
973 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
975 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
976 if ( !(m_dialogStyle
& wxMULTIPLE
) )
977 style2
|= wxLC_SINGLE_SEL
;
979 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
980 _T(""), ms_lastShowHidden
,
981 wxDefaultPosition
, wxSize(540,200),
986 // PDAs have a different screen layout
987 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
989 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
990 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
991 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
992 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
995 m_choice
= new wxChoice( this, ID_CHOICE
);
996 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
998 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
999 buttonsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxALL
, 5 );
1000 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 5 );
1001 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1005 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1007 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1008 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1009 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1010 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1011 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1013 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1014 m_choice
= new wxChoice( this, ID_CHOICE
);
1015 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1016 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1017 m_check
->SetValue( ms_lastShowHidden
);
1018 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1019 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1020 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1023 SetWildcard(wildCard
);
1025 SetAutoLayout( true );
1026 SetSizer( mainsizer
);
1028 mainsizer
->Fit( this );
1029 mainsizer
->SetSizeHints( this );
1036 wxGenericFileDialog::~wxGenericFileDialog()
1038 if (wxConfig::Get(false))
1040 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1042 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1046 const int count
= m_choice
->GetCount();
1047 for ( int i
= 0; i
< count
; i
++ )
1049 delete (wxString
*)m_choice
->GetClientData(i
);
1053 int wxGenericFileDialog::ShowModal()
1055 m_list
->GoToDir(m_dir
);
1057 m_text
->SetValue(m_fileName
);
1059 return wxDialog::ShowModal();
1062 bool wxGenericFileDialog::Show( bool show
)
1066 m_list
->GoToDir(m_dir
);
1068 m_text
->SetValue(m_fileName
);
1071 return wxDialog::Show( show
);
1074 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1076 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1077 m_list
->SetWild( *str
);
1078 m_filterIndex
= filterindex
;
1079 if ( str
->Left(2) == wxT("*.") )
1081 m_filterExtension
= str
->Mid(1);
1082 if (m_filterExtension
== _T(".*"))
1083 m_filterExtension
.clear();
1087 m_filterExtension
.clear();
1091 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1093 wxFileDialogBase::SetWildcard(wildCard
);
1095 wxArrayString wildDescriptions
, wildFilters
;
1096 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1099 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1102 for ( size_t n
= 0; n
< count
; n
++ )
1104 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1107 SetFilterIndex( 0 );
1110 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1112 m_choice
->SetSelection( filterindex
);
1114 DoSetFilterIndex(filterindex
);
1117 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1119 DoSetFilterIndex((int)event
.GetInt());
1122 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1124 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1127 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1129 HandleAction( event
.m_item
.m_text
);
1132 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1134 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1135 cevent
.SetEventObject( this );
1136 GetEventHandler()->ProcessEvent( cevent
);
1139 static bool ignoreChanges
= false;
1141 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1145 // Clear selections. Otherwise when the user types in a value they may
1146 // not get the file whose name they typed.
1147 if (m_list
->GetSelectedItemCount() > 0)
1149 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1150 wxLIST_STATE_SELECTED
);
1151 while ( item
!= -1 )
1153 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1154 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1160 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1162 wxString
filename( event
.m_item
.m_text
);
1163 if (filename
== wxT("..")) return;
1165 wxString dir
= m_list
->GetDir();
1166 if (!IsTopMostDir(dir
))
1167 dir
+= wxFILE_SEP_PATH
;
1169 if (wxDirExists(dir
)) return;
1171 ignoreChanges
= true;
1172 m_text
->SetValue( filename
);
1173 ignoreChanges
= false;
1176 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1178 wxString
filename( fn
);
1179 wxString dir
= m_list
->GetDir();
1180 if (filename
.IsEmpty()) return;
1181 if (filename
== wxT(".")) return;
1183 // "some/place/" means they want to chdir not try to load "place"
1184 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1186 filename
= filename
.RemoveLast();
1188 if (filename
== wxT(".."))
1190 m_list
->GoToParentDir();
1197 if (filename
== wxT("~"))
1199 m_list
->GoToHomeDir();
1205 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1207 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1211 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1212 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1214 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1216 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1219 m_list
->SetWild( filename
);
1223 if (!IsTopMostDir(dir
))
1224 dir
+= wxFILE_SEP_PATH
;
1225 if (!wxIsAbsolutePath(filename
))
1231 if (wxDirExists(filename
))
1233 m_list
->GoToDir( filename
);
1238 // they really wanted a dir, but it doesn't exist
1241 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1242 wxOK
| wxICON_ERROR
);
1246 // append the default extension to the filename if it doesn't have any
1248 // VZ: the logic of testing for !wxFileExists() only for the open file
1249 // dialog is not entirely clear to me, why don't we allow saving to a
1250 // file without extension as well?
1251 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1253 filename
= AppendExtension(filename
, m_filterExtension
);
1256 // check that the file [doesn't] exist if necessary
1257 if ( (m_dialogStyle
& wxSAVE
) &&
1258 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1259 wxFileExists( filename
) )
1262 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1264 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1267 else if ( (m_dialogStyle
& wxOPEN
) &&
1268 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1269 !wxFileExists(filename
) )
1271 wxMessageBox(_("Please choose an existing file."), _("Error"),
1272 wxOK
| wxICON_ERROR
);
1275 SetPath( filename
);
1277 // change to the directory where the user went if asked
1278 if ( m_dialogStyle
& wxCHANGE_DIR
)
1281 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1283 if ( cwd
!= wxGetCwd() )
1285 wxSetWorkingDirectory(cwd
);
1289 wxCommandEvent event
;
1290 wxDialog::OnOK(event
);
1293 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1295 HandleAction( m_text
->GetValue() );
1298 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1300 m_list
->ChangeToListMode();
1301 ms_lastViewStyle
= wxLC_LIST
;
1305 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1307 m_list
->ChangeToReportMode();
1308 ms_lastViewStyle
= wxLC_REPORT
;
1312 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1314 m_list
->GoToParentDir();
1319 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1321 m_list
->GoToHomeDir();
1326 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1331 void wxGenericFileDialog::SetPath( const wxString
& path
)
1333 // not only set the full path but also update filename and dir
1335 if ( !path
.empty() )
1338 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1341 m_fileName
+= wxT(".");
1347 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1350 if (m_list
->GetSelectedItemCount() == 0)
1352 paths
.Add( GetPath() );
1356 paths
.Alloc( m_list
->GetSelectedItemCount() );
1358 wxString dir
= m_list
->GetDir();
1360 if (dir
!= wxT("/"))
1362 dir
+= wxFILE_SEP_PATH
;
1365 item
.m_mask
= wxLIST_MASK_TEXT
;
1367 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1368 while ( item
.m_itemId
!= -1 )
1370 m_list
->GetItem( item
);
1371 paths
.Add( dir
+ item
.m_text
);
1372 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1373 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1377 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1380 if (m_list
->GetSelectedItemCount() == 0)
1382 files
.Add( GetFilename() );
1385 files
.Alloc( m_list
->GetSelectedItemCount() );
1388 item
.m_mask
= wxLIST_MASK_TEXT
;
1390 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1391 while ( item
.m_itemId
!= -1 )
1393 m_list
->GetItem( item
);
1394 files
.Add( item
.m_text
);
1395 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1396 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1400 void wxGenericFileDialog::UpdateControls()
1402 wxString dir
= m_list
->GetDir();
1403 m_static
->SetLabel(dir
);
1405 bool enable
= !IsTopMostDir(dir
);
1406 m_upDirButton
->Enable(enable
);
1408 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1409 m_newDirButton
->Enable(enable
);
1410 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1413 #ifdef USE_GENERIC_FILEDIALOG
1415 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1417 #endif // USE_GENERIC_FILEDIALOG
1419 #endif // wxUSE_FILEDLG && !defined(__WXGTK24__)