1 //////////////////////////////////////////////////////////////////////////////
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"
34 #include "wx/msgdlg.h"
36 #include "wx/bmpbuttn.h"
37 #include "wx/tokenzr.h"
38 #include "wx/config.h"
39 #include "wx/imaglist.h"
41 #include "wx/artprov.h"
42 #include "wx/settings.h"
43 #include "wx/file.h" // for wxS_IXXX constants only
44 #include "wx/filedlg.h" // wxOPEN, wxSAVE...
45 #include "wx/generic/filedlgg.h"
46 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
49 #include "wx/tooltip.h"
52 #include <sys/types.h>
64 #include "wx/msw/wrapwin.h"
65 #include "wx/msw/mslu.h"
73 #if defined(__UNIX__) || defined(__DOS__)
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
82 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long data
)
84 wxFileData
*fd1
= (wxFileData
*)data1
;
85 wxFileData
*fd2
= (wxFileData
*)data2
;
86 if (fd1
->GetFileName() == wxT("..")) return -data
;
87 if (fd2
->GetFileName() == wxT("..")) return data
;
88 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
89 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
90 return data
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
94 int wxCALLBACK
wxFileDataSizeCompare( long data1
, long data2
, long data
)
96 wxFileData
*fd1
= (wxFileData
*)data1
;
97 wxFileData
*fd2
= (wxFileData
*)data2
;
98 if (fd1
->GetFileName() == wxT("..")) return -data
;
99 if (fd2
->GetFileName() == wxT("..")) return data
;
100 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
101 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
102 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
103 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
104 return data
*(fd1
->GetSize() - fd2
->GetSize());
108 int wxCALLBACK
wxFileDataTypeCompare( long data1
, long data2
, long data
)
110 wxFileData
*fd1
= (wxFileData
*)data1
;
111 wxFileData
*fd2
= (wxFileData
*)data2
;
112 if (fd1
->GetFileName() == wxT("..")) return -data
;
113 if (fd2
->GetFileName() == wxT("..")) return data
;
114 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
115 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
116 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
117 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
118 return data
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
122 int wxCALLBACK
wxFileDataTimeCompare( long data1
, long data2
, long data
)
124 wxFileData
*fd1
= (wxFileData
*)data1
;
125 wxFileData
*fd2
= (wxFileData
*)data2
;
126 if (fd1
->GetFileName() == wxT("..")) return -data
;
127 if (fd2
->GetFileName() == wxT("..")) return data
;
128 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
129 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
131 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? int(data
) : -int(data
);
134 #if defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)
135 #define IsTopMostDir(dir) (dir.empty())
137 #define IsTopMostDir(dir) (dir == wxT("/"))
140 // defined in src/generic/dirctrlg.cpp
141 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
143 //-----------------------------------------------------------------------------
145 //-----------------------------------------------------------------------------
147 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
150 m_fileName
= fileName
;
151 m_filePath
= filePath
;
158 void wxFileData::Init()
161 m_type
= wxFileData::is_file
;
162 m_image
= wxFileIconsTable::file
;
165 void wxFileData::Copy( const wxFileData
& fileData
)
167 m_fileName
= fileData
.GetFileName();
168 m_filePath
= fileData
.GetFilePath();
169 m_size
= fileData
.GetSize();
170 m_dateTime
= fileData
.GetDateTime();
171 m_permissions
= fileData
.GetPermissions();
172 m_type
= fileData
.GetType();
173 m_image
= fileData
.GetImageId();
176 void wxFileData::ReadData()
184 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
185 // c:\.. is a drive don't stat it
186 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
192 #endif // __DOS__ || __WINDOWS__
196 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
197 lstat( m_filePath
.fn_str(), &buff
);
198 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
200 // only translate to file charset if we don't go by our
201 // wxStat implementation
202 #ifndef wxNEED_WX_UNISTD_H
203 wxStat( m_filePath
.fn_str() , &buff
);
205 wxStat( m_filePath
, &buff
);
209 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
210 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
212 // try to get a better icon
213 if (m_image
== wxFileIconsTable::file
)
215 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
217 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
220 m_image
= wxFileIconsTable::executable
;
224 m_size
= (long)buff
.st_size
;
226 m_dateTime
= buff
.st_mtime
;
228 #if defined(__UNIX__)
229 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
230 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
231 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
232 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
233 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
234 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
235 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
236 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
237 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
238 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
239 #elif defined(__WIN32__)
240 DWORD attribs
= GetFileAttributes(m_filePath
);
241 if (attribs
!= (DWORD
)-1)
243 m_permissions
.Printf(_T("%c%c%c%c"),
244 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
245 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
246 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
247 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
252 wxString
wxFileData::GetFileType() const
260 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
261 return m_fileName
.AfterLast(wxT('.'));
263 return wxEmptyString
;
266 wxString
wxFileData::GetModificationTime() const
268 // want time as 01:02 so they line up nicely, no %r in WIN32
269 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
272 wxString
wxFileData::GetHint() const
274 wxString s
= m_filePath
;
284 s
+= wxString::Format( _("%ld bytes"), m_size
);
290 s
<< GetModificationTime()
298 wxString
wxFileData::GetEntry( fileListFieldType num
) const
308 if (!IsDir() && !IsLink() && !IsDrive())
309 s
.Printf(_T("%ld"), m_size
);
318 s
= GetModificationTime();
321 #if defined(__UNIX__) || defined(__WIN32__)
325 #endif // defined(__UNIX__) || defined(__WIN32__)
328 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
334 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
336 m_fileName
= fileName
;
337 m_filePath
= filePath
;
340 void wxFileData::MakeItem( wxListItem
&item
)
342 item
.m_text
= m_fileName
;
343 item
.ClearAttributes();
345 item
.SetTextColour(*wxRED
);
347 item
.SetTextColour(*wxBLUE
);
349 item
.m_image
= m_image
;
353 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
355 item
.SetTextColour(dg
);
357 item
.m_data
= (long)this;
360 //-----------------------------------------------------------------------------
362 //-----------------------------------------------------------------------------
364 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
366 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
367 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
368 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
369 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
370 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
374 wxFileCtrl::wxFileCtrl()
376 m_showHidden
= false;
378 m_sort_field
= wxFileData::FileList_Name
;
381 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
383 const wxString
& wild
,
388 const wxValidator
&validator
,
389 const wxString
&name
)
390 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
393 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
395 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
397 m_showHidden
= showHidden
;
400 m_sort_field
= wxFileData::FileList_Name
;
402 m_dirName
= wxT("*");
404 if (style
& wxLC_REPORT
)
405 ChangeToReportMode();
408 void wxFileCtrl::ChangeToListMode()
411 SetSingleStyle( wxLC_LIST
);
415 void wxFileCtrl::ChangeToReportMode()
418 SetSingleStyle( wxLC_REPORT
);
420 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
421 // don't hardcode since mm/dd is dd/mm elsewhere
423 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
424 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
425 GetTextExtent(txt
, &w
, &h
);
427 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
428 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
429 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
430 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
431 #if defined(__UNIX__)
432 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
433 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
434 #elif defined(__WIN32__)
435 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
436 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
442 void wxFileCtrl::ChangeToSmallIconMode()
445 SetSingleStyle( wxLC_SMALL_ICON
);
449 void wxFileCtrl::ShowHidden( bool show
)
455 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
458 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
459 fd
->MakeItem( item
);
460 long my_style
= GetWindowStyleFlag();
461 if (my_style
& wxLC_REPORT
)
463 ret
= InsertItem( item
);
464 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
465 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
467 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
469 ret
= InsertItem( item
);
474 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
476 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
477 wxCHECK_RET(fd
, wxT("invalid filedata"));
481 SetItemText(item
, fd
->GetFileName());
482 SetItemImage(item
, fd
->GetImageId());
484 if (GetWindowStyleFlag() & wxLC_REPORT
)
486 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
487 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
491 void wxFileCtrl::UpdateFiles()
493 // don't do anything before ShowModal() call which sets m_dirName
494 if ( m_dirName
== wxT("*") )
497 wxBusyCursor bcur
; // this may take a while...
505 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
506 if ( IsTopMostDir(m_dirName
) )
508 wxArrayString names
, paths
;
510 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
512 for (n
=0; n
<count
; n
++)
514 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
515 if (Add(fd
, item
) != -1)
522 #endif // defined(__DOS__) || defined(__WINDOWS__)
525 if ( !IsTopMostDir(m_dirName
) )
527 wxString
p(wxPathOnly(m_dirName
));
528 #if defined(__UNIX__) && !defined(__OS2__)
529 if (p
.empty()) p
= wxT("/");
531 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
532 if (Add(fd
, item
) != -1)
538 wxString
dirname(m_dirName
);
539 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
540 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
541 dirname
<< wxT('\\');
542 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
545 if ( dir
.IsOpened() )
547 wxString
dirPrefix(dirname
);
548 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
549 dirPrefix
+= wxFILE_SEP_PATH
;
551 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
556 // Get the directories first (not matched against wildcards):
557 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
560 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
561 if (Add(fd
, item
) != -1)
566 cont
= dir
.GetNext(&f
);
569 // Tokenize the wildcard string, so we can handle more than 1
570 // search pattern in a wildcard.
571 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
572 while ( tokenWild
.HasMoreTokens() )
574 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
575 wxDIR_FILES
| hiddenFlag
);
578 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
579 if (Add(fd
, item
) != -1)
584 cont
= dir
.GetNext(&f
);
590 SortItems(m_sort_field
, m_sort_foward
);
593 void wxFileCtrl::SetWild( const wxString
&wild
)
595 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
602 void wxFileCtrl::MakeDir()
604 wxString
new_name( _("NewName") );
605 wxString
path( m_dirName
);
606 path
+= wxFILE_SEP_PATH
;
608 if (wxFileExists(path
))
610 // try NewName0, NewName1 etc.
613 new_name
= _("NewName");
615 num
.Printf( wxT("%d"), i
);
619 path
+= wxFILE_SEP_PATH
;
622 } while (wxFileExists(path
));
628 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
633 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
637 long id
= Add( fd
, item
);
641 SortItems(m_sort_field
, m_sort_foward
);
642 id
= FindItem( 0, (long)fd
);
650 void wxFileCtrl::GoToParentDir()
652 if (!IsTopMostDir(m_dirName
))
654 size_t len
= m_dirName
.Len();
655 if (wxEndsWithPathSeparator(m_dirName
))
656 m_dirName
.Remove( len
-1, 1 );
657 wxString
fname( wxFileNameFromPath(m_dirName
) );
658 m_dirName
= wxPathOnly( m_dirName
);
659 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
660 if (!m_dirName
.empty())
662 if (m_dirName
.Last() == wxT('.'))
663 m_dirName
= wxEmptyString
;
665 #elif defined(__UNIX__)
666 if (m_dirName
.empty())
667 m_dirName
= wxT("/");
670 long id
= FindItem( 0, fname
);
671 if (id
!= wxNOT_FOUND
)
673 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
679 void wxFileCtrl::GoToHomeDir()
681 wxString s
= wxGetUserHome( wxString() );
685 void wxFileCtrl::GoToDir( const wxString
&dir
)
687 if (!wxDirExists(dir
)) return;
691 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
695 void wxFileCtrl::FreeItemData(wxListItem
& item
)
699 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
706 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
708 FreeItemData(event
.m_item
);
711 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
716 void wxFileCtrl::FreeAllItemsData()
719 item
.m_mask
= wxLIST_MASK_DATA
;
721 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
722 while ( item
.m_itemId
!= -1 )
726 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
730 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
732 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
735 if ((event
.GetLabel().empty()) ||
736 (event
.GetLabel() == _(".")) ||
737 (event
.GetLabel() == _("..")) ||
738 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
740 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
746 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
747 new_name
+= wxFILE_SEP_PATH
;
748 new_name
+= event
.GetLabel();
752 if (wxFileExists(new_name
))
754 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
759 if (wxRenameFile(fd
->GetFilePath(),new_name
))
761 fd
->SetNewName( new_name
, event
.GetLabel() );
762 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
763 UpdateItem( event
.GetItem() );
764 EnsureVisible( event
.GetItem() );
768 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
774 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
776 int col
= event
.GetColumn();
780 case wxFileData::FileList_Name
:
781 case wxFileData::FileList_Size
:
782 case wxFileData::FileList_Type
:
783 case wxFileData::FileList_Time
: break;
787 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
788 m_sort_foward
= !m_sort_foward
;
790 m_sort_field
= (wxFileData::fileListFieldType
)col
;
792 SortItems(m_sort_field
, m_sort_foward
);
795 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
797 m_sort_field
= field
;
798 m_sort_foward
= foward
;
799 long sort_dir
= foward
? 1 : -1;
801 switch (m_sort_field
)
803 case wxFileData::FileList_Name
:
805 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
808 case wxFileData::FileList_Size
:
810 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
813 case wxFileData::FileList_Type
:
815 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
818 case wxFileData::FileList_Time
:
820 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
827 wxFileCtrl::~wxFileCtrl()
829 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
830 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
831 // the destruction of the wxFileCtrl we need to free any data here:
835 //-----------------------------------------------------------------------------
836 // wxGenericFileDialog
837 //-----------------------------------------------------------------------------
839 #define ID_LIST_MODE (wxID_FILEDLGG )
840 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
841 #define ID_UP_DIR (wxID_FILEDLGG + 5)
842 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
843 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
844 #define ID_CHOICE (wxID_FILEDLGG + 8)
845 #define ID_TEXT (wxID_FILEDLGG + 9)
846 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
847 #define ID_CHECK (wxID_FILEDLGG + 12)
849 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
851 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
852 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
853 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
854 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
855 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
856 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
857 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
858 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
859 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
860 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
861 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
862 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
863 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
866 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
867 bool wxGenericFileDialog::ms_lastShowHidden
= false;
869 void wxGenericFileDialog::Init()
871 m_bypassGenericImpl
= false;
878 m_upDirButton
= NULL
;
879 m_newDirButton
= NULL
;
882 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
883 const wxString
& message
,
884 const wxString
& defaultDir
,
885 const wxString
& defaultFile
,
886 const wxString
& wildCard
,
889 bool bypassGenericImpl
) : wxFileDialogBase()
892 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, bypassGenericImpl
);
895 bool wxGenericFileDialog::Create( wxWindow
*parent
,
896 const wxString
& message
,
897 const wxString
& defaultDir
,
898 const wxString
& defaultFile
,
899 const wxString
& wildCard
,
902 bool bypassGenericImpl
)
904 m_bypassGenericImpl
= bypassGenericImpl
;
906 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
907 wildCard
, style
, pos
))
912 if (m_bypassGenericImpl
)
915 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
916 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
))
921 if (wxConfig::Get(false))
923 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
925 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
929 if (m_dialogStyle
== 0)
930 m_dialogStyle
= wxOPEN
;
931 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
932 m_dialogStyle
|= wxOPEN
;
934 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
939 size_t len
= m_dir
.Len();
940 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
941 m_dir
.Remove( len
-1, 1 );
944 m_path
+= wxFILE_SEP_PATH
;
945 m_path
+= defaultFile
;
946 m_filterExtension
= wxEmptyString
;
950 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
952 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
954 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
958 but
= new wxBitmapButton(this, ID_LIST_MODE
,
959 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
961 but
->SetToolTip( _("View files as a list view") );
963 buttonsizer
->Add( but
, 0, wxALL
, 5 );
965 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
966 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
968 but
->SetToolTip( _("View files as a detailed view") );
970 buttonsizer
->Add( but
, 0, wxALL
, 5 );
972 buttonsizer
->Add( 30, 5, 1 );
974 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
975 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
977 m_upDirButton
->SetToolTip( _("Go to parent directory") );
979 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
981 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
982 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
983 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
985 but
->SetToolTip( _("Go to home directory") );
987 buttonsizer
->Add( but
, 0, wxALL
, 5);
989 buttonsizer
->Add( 20, 20 );
992 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
993 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
995 m_newDirButton
->SetToolTip( _("Create new directory") );
997 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1000 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1002 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1004 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1006 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1007 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1008 staticsizer
->Add( m_static
, 1 );
1009 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1011 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
1012 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1013 style2
|= wxLC_SINGLE_SEL
;
1015 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1016 wxEmptyString
, ms_lastShowHidden
,
1017 wxDefaultPosition
, wxSize(540,200),
1022 // PDAs have a different screen layout
1023 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1025 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1026 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1027 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1028 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1031 m_choice
= new wxChoice( this, ID_CHOICE
);
1032 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1034 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1035 buttonsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxALL
, 5 );
1036 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 5 );
1037 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1041 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1043 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1044 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1045 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1046 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1047 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1049 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1050 m_choice
= new wxChoice( this, ID_CHOICE
);
1051 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1052 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1053 m_check
->SetValue( ms_lastShowHidden
);
1054 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1055 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1056 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1059 SetWildcard(wildCard
);
1061 SetAutoLayout( true );
1062 SetSizer( mainsizer
);
1064 mainsizer
->Fit( this );
1065 mainsizer
->SetSizeHints( this );
1074 wxGenericFileDialog::~wxGenericFileDialog()
1076 if (!m_bypassGenericImpl
)
1078 if (wxConfig::Get(false))
1080 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1082 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1086 const int count
= m_choice
->GetCount();
1087 for ( int i
= 0; i
< count
; i
++ )
1089 delete (wxString
*)m_choice
->GetClientData(i
);
1094 int wxGenericFileDialog::ShowModal()
1096 m_list
->GoToDir(m_dir
);
1098 m_text
->SetValue(m_fileName
);
1100 return wxDialog::ShowModal();
1103 bool wxGenericFileDialog::Show( bool show
)
1107 m_list
->GoToDir(m_dir
);
1109 m_text
->SetValue(m_fileName
);
1112 return wxDialog::Show( show
);
1115 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1117 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1118 m_list
->SetWild( *str
);
1119 m_filterIndex
= filterindex
;
1120 if ( str
->Left(2) == wxT("*.") )
1122 m_filterExtension
= str
->Mid(1);
1123 if (m_filterExtension
== _T(".*"))
1124 m_filterExtension
.clear();
1128 m_filterExtension
.clear();
1132 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1134 wxFileDialogBase::SetWildcard(wildCard
);
1136 wxArrayString wildDescriptions
, wildFilters
;
1137 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1140 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1142 const size_t countOld
= m_choice
->GetCount();
1144 for ( n
= 0; n
< countOld
; n
++ )
1146 delete (wxString
*)m_choice
->GetClientData(n
);
1149 for ( n
= 0; n
< count
; n
++ )
1151 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1154 SetFilterIndex( 0 );
1157 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1159 m_choice
->SetSelection( filterindex
);
1161 DoSetFilterIndex(filterindex
);
1164 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1166 DoSetFilterIndex((int)event
.GetInt());
1169 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1171 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1174 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1176 HandleAction( event
.m_item
.m_text
);
1179 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1181 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1182 cevent
.SetEventObject( this );
1183 GetEventHandler()->ProcessEvent( cevent
);
1186 static bool ignoreChanges
= false;
1188 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1192 // Clear selections. Otherwise when the user types in a value they may
1193 // not get the file whose name they typed.
1194 if (m_list
->GetSelectedItemCount() > 0)
1196 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1197 wxLIST_STATE_SELECTED
);
1198 while ( item
!= -1 )
1200 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1201 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1207 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1209 wxString
filename( event
.m_item
.m_text
);
1210 if (filename
== wxT("..")) return;
1212 wxString dir
= m_list
->GetDir();
1213 if (!IsTopMostDir(dir
))
1214 dir
+= wxFILE_SEP_PATH
;
1216 if (wxDirExists(dir
)) return;
1218 ignoreChanges
= true;
1219 m_text
->SetValue( filename
);
1220 ignoreChanges
= false;
1223 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1225 wxString
filename( fn
);
1226 wxString dir
= m_list
->GetDir();
1227 if (filename
.empty()) return;
1228 if (filename
== wxT(".")) return;
1230 // "some/place/" means they want to chdir not try to load "place"
1231 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1233 filename
= filename
.RemoveLast();
1235 if (filename
== wxT(".."))
1237 m_list
->GoToParentDir();
1244 if (filename
== wxT("~"))
1246 m_list
->GoToHomeDir();
1252 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1254 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1258 if (!(m_dialogStyle
& wxSAVE
))
1260 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1261 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1263 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1265 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1268 m_list
->SetWild( filename
);
1273 if (!IsTopMostDir(dir
))
1274 dir
+= wxFILE_SEP_PATH
;
1275 if (!wxIsAbsolutePath(filename
))
1281 if (wxDirExists(filename
))
1283 m_list
->GoToDir( filename
);
1288 // they really wanted a dir, but it doesn't exist
1291 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1292 wxOK
| wxICON_ERROR
);
1296 // append the default extension to the filename if it doesn't have any
1298 // VZ: the logic of testing for !wxFileExists() only for the open file
1299 // dialog is not entirely clear to me, why don't we allow saving to a
1300 // file without extension as well?
1301 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1303 filename
= AppendExtension(filename
, m_filterExtension
);
1306 // check that the file [doesn't] exist if necessary
1307 if ( (m_dialogStyle
& wxSAVE
) &&
1308 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1309 wxFileExists( filename
) )
1312 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1314 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1317 else if ( (m_dialogStyle
& wxOPEN
) &&
1318 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1319 !wxFileExists(filename
) )
1321 wxMessageBox(_("Please choose an existing file."), _("Error"),
1322 wxOK
| wxICON_ERROR
);
1325 SetPath( filename
);
1327 // change to the directory where the user went if asked
1328 if ( m_dialogStyle
& wxCHANGE_DIR
)
1331 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1333 if ( cwd
!= wxGetCwd() )
1335 wxSetWorkingDirectory(cwd
);
1339 wxCommandEvent event
;
1340 wxDialog::OnOK(event
);
1343 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1345 HandleAction( m_text
->GetValue() );
1348 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1350 m_list
->ChangeToListMode();
1351 ms_lastViewStyle
= wxLC_LIST
;
1355 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1357 m_list
->ChangeToReportMode();
1358 ms_lastViewStyle
= wxLC_REPORT
;
1362 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1364 m_list
->GoToParentDir();
1369 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1371 m_list
->GoToHomeDir();
1376 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1381 void wxGenericFileDialog::SetPath( const wxString
& path
)
1383 // not only set the full path but also update filename and dir
1385 if ( !path
.empty() )
1388 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1391 m_fileName
+= wxT(".");
1397 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1400 if (m_list
->GetSelectedItemCount() == 0)
1402 paths
.Add( GetPath() );
1406 paths
.Alloc( m_list
->GetSelectedItemCount() );
1408 wxString dir
= m_list
->GetDir();
1410 if (dir
!= wxT("/"))
1412 dir
+= wxFILE_SEP_PATH
;
1415 item
.m_mask
= wxLIST_MASK_TEXT
;
1417 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1418 while ( item
.m_itemId
!= -1 )
1420 m_list
->GetItem( item
);
1421 paths
.Add( dir
+ item
.m_text
);
1422 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1423 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1427 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1430 if (m_list
->GetSelectedItemCount() == 0)
1432 files
.Add( GetFilename() );
1435 files
.Alloc( m_list
->GetSelectedItemCount() );
1438 item
.m_mask
= wxLIST_MASK_TEXT
;
1440 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1441 while ( item
.m_itemId
!= -1 )
1443 m_list
->GetItem( item
);
1444 files
.Add( item
.m_text
);
1445 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1446 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1450 void wxGenericFileDialog::UpdateControls()
1452 wxString dir
= m_list
->GetDir();
1453 m_static
->SetLabel(dir
);
1455 bool enable
= !IsTopMostDir(dir
);
1456 m_upDirButton
->Enable(enable
);
1458 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1459 m_newDirButton
->Enable(enable
);
1460 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1463 #ifdef USE_GENERIC_FILEDIALOG
1465 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1467 #endif // USE_GENERIC_FILEDIALOG
1469 #endif // wxUSE_FILEDLG