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(__UNIX__) && !defined(__OS2__) && !defined(__CYGWIN__)
139 #define IsTopMostDir(dir) (dir == wxT("/"))
142 #if defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)
143 #define IsTopMostDir(dir) (dir.empty())
146 // defined in src/generic/dirctrlg.cpp
147 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
149 //-----------------------------------------------------------------------------
151 //-----------------------------------------------------------------------------
153 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
156 m_fileName
= fileName
;
157 m_filePath
= filePath
;
164 void wxFileData::Init()
167 m_type
= wxFileData::is_file
;
168 m_image
= wxFileIconsTable::file
;
171 void wxFileData::Copy( const wxFileData
& fileData
)
173 m_fileName
= fileData
.GetFileName();
174 m_filePath
= fileData
.GetFilePath();
175 m_size
= fileData
.GetSize();
176 m_dateTime
= fileData
.GetDateTime();
177 m_permissions
= fileData
.GetPermissions();
178 m_type
= fileData
.GetType();
179 m_image
= fileData
.GetImageId();
182 void wxFileData::ReadData()
190 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
191 // c:\.. is a drive don't stat it
192 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
198 #endif // __DOS__ || __WINDOWS__
202 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
203 lstat( m_filePath
.fn_str(), &buff
);
204 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
206 // only translate to file charset if we don't go by our
207 // wxStat implementation
208 #ifndef wxNEED_WX_UNISTD_H
209 wxStat( m_filePath
.fn_str() , &buff
);
211 wxStat( m_filePath
, &buff
);
215 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
216 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
218 // try to get a better icon
219 if (m_image
== wxFileIconsTable::file
)
221 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
223 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
226 m_image
= wxFileIconsTable::executable
;
230 m_size
= (long)buff
.st_size
;
232 m_dateTime
= buff
.st_mtime
;
234 #if defined(__UNIX__)
235 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
236 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
237 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
238 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
239 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
240 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
241 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
242 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
243 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
244 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
245 #elif defined(__WIN32__)
246 DWORD attribs
= GetFileAttributes(m_filePath
);
247 if (attribs
!= (DWORD
)-1)
249 m_permissions
.Printf(_T("%c%c%c%c"),
250 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
251 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
252 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
253 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
258 wxString
wxFileData::GetFileType() const
266 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
267 return m_fileName
.AfterLast(wxT('.'));
269 return wxEmptyString
;
272 wxString
wxFileData::GetModificationTime() const
274 // want time as 01:02 so they line up nicely, no %r in WIN32
275 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
278 wxString
wxFileData::GetHint() const
280 wxString s
= m_filePath
;
290 s
+= wxString::Format( _("%ld bytes"), m_size
);
296 s
<< GetModificationTime()
304 wxString
wxFileData::GetEntry( fileListFieldType num
) const
314 if (!IsDir() && !IsLink() && !IsDrive())
315 s
.Printf(_T("%ld"), m_size
);
324 s
= GetModificationTime();
327 #if defined(__UNIX__) || defined(__WIN32__)
331 #endif // defined(__UNIX__) || defined(__WIN32__)
334 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
340 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
342 m_fileName
= fileName
;
343 m_filePath
= filePath
;
346 void wxFileData::MakeItem( wxListItem
&item
)
348 item
.m_text
= m_fileName
;
349 item
.ClearAttributes();
351 item
.SetTextColour(*wxRED
);
353 item
.SetTextColour(*wxBLUE
);
355 item
.m_image
= m_image
;
359 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
361 item
.SetTextColour(dg
);
363 item
.m_data
= (long)this;
366 //-----------------------------------------------------------------------------
368 //-----------------------------------------------------------------------------
370 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
372 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
373 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
374 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
375 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
376 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
380 wxFileCtrl::wxFileCtrl()
382 m_showHidden
= false;
384 m_sort_field
= wxFileData::FileList_Name
;
387 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
389 const wxString
& wild
,
394 const wxValidator
&validator
,
395 const wxString
&name
)
396 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
399 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
401 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
403 m_showHidden
= showHidden
;
406 m_sort_field
= wxFileData::FileList_Name
;
408 m_dirName
= wxT("*");
410 if (style
& wxLC_REPORT
)
411 ChangeToReportMode();
414 void wxFileCtrl::ChangeToListMode()
417 SetSingleStyle( wxLC_LIST
);
421 void wxFileCtrl::ChangeToReportMode()
424 SetSingleStyle( wxLC_REPORT
);
426 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
427 // don't hardcode since mm/dd is dd/mm elsewhere
429 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
430 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
431 GetTextExtent(txt
, &w
, &h
);
433 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
434 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
435 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
436 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
437 #if defined(__UNIX__)
438 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
439 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
440 #elif defined(__WIN32__)
441 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
442 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
448 void wxFileCtrl::ChangeToSmallIconMode()
451 SetSingleStyle( wxLC_SMALL_ICON
);
455 void wxFileCtrl::ShowHidden( bool show
)
461 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
464 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
465 fd
->MakeItem( item
);
466 long my_style
= GetWindowStyleFlag();
467 if (my_style
& wxLC_REPORT
)
469 ret
= InsertItem( item
);
470 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
471 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
473 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
475 ret
= InsertItem( item
);
480 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
482 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
483 wxCHECK_RET(fd
, wxT("invalid filedata"));
487 SetItemText(item
, fd
->GetFileName());
488 SetItemImage(item
, fd
->GetImageId());
490 if (GetWindowStyleFlag() & wxLC_REPORT
)
492 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
493 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
497 void wxFileCtrl::UpdateFiles()
499 // don't do anything before ShowModal() call which sets m_dirName
500 if ( m_dirName
== wxT("*") )
503 wxBusyCursor bcur
; // this may take a while...
511 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
512 if ( IsTopMostDir(m_dirName
) )
514 wxArrayString names
, paths
;
516 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
518 for (n
=0; n
<count
; n
++)
520 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
521 if (Add(fd
, item
) != -1)
528 #endif // defined(__DOS__) || defined(__WINDOWS__)
531 if ( !IsTopMostDir(m_dirName
) )
533 wxString
p(wxPathOnly(m_dirName
));
534 #if defined(__UNIX__) && !defined(__OS2__)
535 if (p
.empty()) p
= wxT("/");
537 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
538 if (Add(fd
, item
) != -1)
544 wxString
dirname(m_dirName
);
545 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
546 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
547 dirname
<< wxT('\\');
548 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
551 if ( dir
.IsOpened() )
553 wxString
dirPrefix(dirname
);
554 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
555 dirPrefix
+= wxFILE_SEP_PATH
;
557 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
562 // Get the directories first (not matched against wildcards):
563 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
566 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
567 if (Add(fd
, item
) != -1)
572 cont
= dir
.GetNext(&f
);
575 // Tokenize the wildcard string, so we can handle more than 1
576 // search pattern in a wildcard.
577 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
578 while ( tokenWild
.HasMoreTokens() )
580 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
581 wxDIR_FILES
| hiddenFlag
);
584 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
585 if (Add(fd
, item
) != -1)
590 cont
= dir
.GetNext(&f
);
596 SortItems(m_sort_field
, m_sort_foward
);
599 void wxFileCtrl::SetWild( const wxString
&wild
)
601 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
608 void wxFileCtrl::MakeDir()
610 wxString
new_name( _("NewName") );
611 wxString
path( m_dirName
);
612 path
+= wxFILE_SEP_PATH
;
614 if (wxFileExists(path
))
616 // try NewName0, NewName1 etc.
619 new_name
= _("NewName");
621 num
.Printf( wxT("%d"), i
);
625 path
+= wxFILE_SEP_PATH
;
628 } while (wxFileExists(path
));
634 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
639 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
643 long id
= Add( fd
, item
);
647 SortItems(m_sort_field
, m_sort_foward
);
648 id
= FindItem( 0, (long)fd
);
656 void wxFileCtrl::GoToParentDir()
658 if (!IsTopMostDir(m_dirName
))
660 size_t len
= m_dirName
.Len();
661 if (wxEndsWithPathSeparator(m_dirName
))
662 m_dirName
.Remove( len
-1, 1 );
663 wxString
fname( wxFileNameFromPath(m_dirName
) );
664 m_dirName
= wxPathOnly( m_dirName
);
665 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
666 if (!m_dirName
.empty())
668 if (m_dirName
.Last() == wxT('.'))
669 m_dirName
= wxEmptyString
;
671 #elif defined(__UNIX__)
672 if (m_dirName
.empty())
673 m_dirName
= wxT("/");
676 long id
= FindItem( 0, fname
);
677 if (id
!= wxNOT_FOUND
)
679 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
685 void wxFileCtrl::GoToHomeDir()
687 wxString s
= wxGetUserHome( wxString() );
691 void wxFileCtrl::GoToDir( const wxString
&dir
)
693 if (!wxDirExists(dir
)) return;
697 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
701 void wxFileCtrl::FreeItemData(wxListItem
& item
)
705 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
712 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
714 FreeItemData(event
.m_item
);
717 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
722 void wxFileCtrl::FreeAllItemsData()
725 item
.m_mask
= wxLIST_MASK_DATA
;
727 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
728 while ( item
.m_itemId
!= -1 )
732 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
736 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
738 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
741 if ((event
.GetLabel().empty()) ||
742 (event
.GetLabel() == _(".")) ||
743 (event
.GetLabel() == _("..")) ||
744 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
746 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
752 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
753 new_name
+= wxFILE_SEP_PATH
;
754 new_name
+= event
.GetLabel();
758 if (wxFileExists(new_name
))
760 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
765 if (wxRenameFile(fd
->GetFilePath(),new_name
))
767 fd
->SetNewName( new_name
, event
.GetLabel() );
768 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
769 UpdateItem( event
.GetItem() );
770 EnsureVisible( event
.GetItem() );
774 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
780 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
782 int col
= event
.GetColumn();
786 case wxFileData::FileList_Name
:
787 case wxFileData::FileList_Size
:
788 case wxFileData::FileList_Type
:
789 case wxFileData::FileList_Time
: break;
793 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
794 m_sort_foward
= !m_sort_foward
;
796 m_sort_field
= (wxFileData::fileListFieldType
)col
;
798 SortItems(m_sort_field
, m_sort_foward
);
801 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
803 m_sort_field
= field
;
804 m_sort_foward
= foward
;
805 long sort_dir
= foward
? 1 : -1;
807 switch (m_sort_field
)
809 case wxFileData::FileList_Name
:
811 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
814 case wxFileData::FileList_Size
:
816 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
819 case wxFileData::FileList_Type
:
821 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
824 case wxFileData::FileList_Time
:
826 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
833 wxFileCtrl::~wxFileCtrl()
835 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
836 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
837 // the destruction of the wxFileCtrl we need to free any data here:
841 //-----------------------------------------------------------------------------
842 // wxGenericFileDialog
843 //-----------------------------------------------------------------------------
845 #define ID_LIST_MODE (wxID_FILEDLGG )
846 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
847 #define ID_UP_DIR (wxID_FILEDLGG + 5)
848 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
849 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
850 #define ID_CHOICE (wxID_FILEDLGG + 8)
851 #define ID_TEXT (wxID_FILEDLGG + 9)
852 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
853 #define ID_CHECK (wxID_FILEDLGG + 12)
855 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
857 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
858 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
859 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
860 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
861 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
862 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
863 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
864 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
865 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
866 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
867 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
868 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
869 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
872 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
873 bool wxGenericFileDialog::ms_lastShowHidden
= false;
875 void wxGenericFileDialog::Init()
877 m_bypassGenericImpl
= false;
884 m_upDirButton
= NULL
;
885 m_newDirButton
= NULL
;
888 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
889 const wxString
& message
,
890 const wxString
& defaultDir
,
891 const wxString
& defaultFile
,
892 const wxString
& wildCard
,
895 bool bypassGenericImpl
) : wxFileDialogBase()
898 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, bypassGenericImpl
);
901 bool wxGenericFileDialog::Create( wxWindow
*parent
,
902 const wxString
& message
,
903 const wxString
& defaultDir
,
904 const wxString
& defaultFile
,
905 const wxString
& wildCard
,
908 bool bypassGenericImpl
)
910 m_bypassGenericImpl
= bypassGenericImpl
;
912 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
913 wildCard
, style
, pos
))
918 if (m_bypassGenericImpl
)
921 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
922 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
))
927 if (wxConfig::Get(false))
929 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
931 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
935 if (m_dialogStyle
== 0)
936 m_dialogStyle
= wxOPEN
;
937 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
938 m_dialogStyle
|= wxOPEN
;
940 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
945 size_t len
= m_dir
.Len();
946 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
947 m_dir
.Remove( len
-1, 1 );
950 m_path
+= wxFILE_SEP_PATH
;
951 m_path
+= defaultFile
;
952 m_filterExtension
= wxEmptyString
;
956 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
958 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
960 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
964 but
= new wxBitmapButton(this, ID_LIST_MODE
,
965 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
967 but
->SetToolTip( _("View files as a list view") );
969 buttonsizer
->Add( but
, 0, wxALL
, 5 );
971 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
972 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
974 but
->SetToolTip( _("View files as a detailed view") );
976 buttonsizer
->Add( but
, 0, wxALL
, 5 );
978 buttonsizer
->Add( 30, 5, 1 );
980 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
981 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
983 m_upDirButton
->SetToolTip( _("Go to parent directory") );
985 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
987 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
988 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
989 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
991 but
->SetToolTip( _("Go to home directory") );
993 buttonsizer
->Add( but
, 0, wxALL
, 5);
995 buttonsizer
->Add( 20, 20 );
998 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
999 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
1001 m_newDirButton
->SetToolTip( _("Create new directory") );
1003 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1006 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1008 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1010 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1012 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1013 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1014 staticsizer
->Add( m_static
, 1 );
1015 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1017 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
1018 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1019 style2
|= wxLC_SINGLE_SEL
;
1021 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1022 wxEmptyString
, ms_lastShowHidden
,
1023 wxDefaultPosition
, wxSize(540,200),
1028 // PDAs have a different screen layout
1029 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1031 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1032 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1033 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1034 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1037 m_choice
= new wxChoice( this, ID_CHOICE
);
1038 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1040 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1041 buttonsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxALL
, 5 );
1042 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 5 );
1043 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1047 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1049 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1050 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1051 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1052 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1053 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1055 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1056 m_choice
= new wxChoice( this, ID_CHOICE
);
1057 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1058 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1059 m_check
->SetValue( ms_lastShowHidden
);
1060 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1061 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1062 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1065 SetWildcard(wildCard
);
1067 SetAutoLayout( true );
1068 SetSizer( mainsizer
);
1070 mainsizer
->Fit( this );
1071 mainsizer
->SetSizeHints( this );
1080 wxGenericFileDialog::~wxGenericFileDialog()
1082 if (!m_bypassGenericImpl
)
1084 if (wxConfig::Get(false))
1086 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1088 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1092 const int count
= m_choice
->GetCount();
1093 for ( int i
= 0; i
< count
; i
++ )
1095 delete (wxString
*)m_choice
->GetClientData(i
);
1100 int wxGenericFileDialog::ShowModal()
1102 m_list
->GoToDir(m_dir
);
1104 m_text
->SetValue(m_fileName
);
1106 return wxDialog::ShowModal();
1109 bool wxGenericFileDialog::Show( bool show
)
1113 m_list
->GoToDir(m_dir
);
1115 m_text
->SetValue(m_fileName
);
1118 return wxDialog::Show( show
);
1121 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1123 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1124 m_list
->SetWild( *str
);
1125 m_filterIndex
= filterindex
;
1126 if ( str
->Left(2) == wxT("*.") )
1128 m_filterExtension
= str
->Mid(1);
1129 if (m_filterExtension
== _T(".*"))
1130 m_filterExtension
.clear();
1134 m_filterExtension
.clear();
1138 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1140 wxFileDialogBase::SetWildcard(wildCard
);
1142 wxArrayString wildDescriptions
, wildFilters
;
1143 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1146 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1148 const size_t countOld
= m_choice
->GetCount();
1150 for ( n
= 0; n
< countOld
; n
++ )
1152 delete (wxString
*)m_choice
->GetClientData(n
);
1155 for ( n
= 0; n
< count
; n
++ )
1157 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1160 SetFilterIndex( 0 );
1163 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1165 m_choice
->SetSelection( filterindex
);
1167 DoSetFilterIndex(filterindex
);
1170 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1172 DoSetFilterIndex((int)event
.GetInt());
1175 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1177 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1180 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1182 HandleAction( event
.m_item
.m_text
);
1185 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1187 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1188 cevent
.SetEventObject( this );
1189 GetEventHandler()->ProcessEvent( cevent
);
1192 static bool ignoreChanges
= false;
1194 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1198 // Clear selections. Otherwise when the user types in a value they may
1199 // not get the file whose name they typed.
1200 if (m_list
->GetSelectedItemCount() > 0)
1202 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1203 wxLIST_STATE_SELECTED
);
1204 while ( item
!= -1 )
1206 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1207 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1213 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1215 wxString
filename( event
.m_item
.m_text
);
1216 if (filename
== wxT("..")) return;
1218 wxString dir
= m_list
->GetDir();
1219 if (!IsTopMostDir(dir
))
1220 dir
+= wxFILE_SEP_PATH
;
1222 if (wxDirExists(dir
)) return;
1224 ignoreChanges
= true;
1225 m_text
->SetValue( filename
);
1226 ignoreChanges
= false;
1229 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1231 wxString
filename( fn
);
1232 wxString dir
= m_list
->GetDir();
1233 if (filename
.empty()) return;
1234 if (filename
== wxT(".")) return;
1236 // "some/place/" means they want to chdir not try to load "place"
1237 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1239 filename
= filename
.RemoveLast();
1241 if (filename
== wxT(".."))
1243 m_list
->GoToParentDir();
1250 if (filename
== wxT("~"))
1252 m_list
->GoToHomeDir();
1258 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1260 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1264 if (!(m_dialogStyle
& wxSAVE
))
1266 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1267 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1269 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1271 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1274 m_list
->SetWild( filename
);
1279 if (!IsTopMostDir(dir
))
1280 dir
+= wxFILE_SEP_PATH
;
1281 if (!wxIsAbsolutePath(filename
))
1287 if (wxDirExists(filename
))
1289 m_list
->GoToDir( filename
);
1294 // they really wanted a dir, but it doesn't exist
1297 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1298 wxOK
| wxICON_ERROR
);
1302 // append the default extension to the filename if it doesn't have any
1304 // VZ: the logic of testing for !wxFileExists() only for the open file
1305 // dialog is not entirely clear to me, why don't we allow saving to a
1306 // file without extension as well?
1307 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1309 filename
= AppendExtension(filename
, m_filterExtension
);
1312 // check that the file [doesn't] exist if necessary
1313 if ( (m_dialogStyle
& wxSAVE
) &&
1314 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1315 wxFileExists( filename
) )
1318 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1320 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1323 else if ( (m_dialogStyle
& wxOPEN
) &&
1324 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1325 !wxFileExists(filename
) )
1327 wxMessageBox(_("Please choose an existing file."), _("Error"),
1328 wxOK
| wxICON_ERROR
);
1331 SetPath( filename
);
1333 // change to the directory where the user went if asked
1334 if ( m_dialogStyle
& wxCHANGE_DIR
)
1337 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1339 if ( cwd
!= wxGetCwd() )
1341 wxSetWorkingDirectory(cwd
);
1345 wxCommandEvent event
;
1346 wxDialog::OnOK(event
);
1349 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1351 HandleAction( m_text
->GetValue() );
1354 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1356 m_list
->ChangeToListMode();
1357 ms_lastViewStyle
= wxLC_LIST
;
1361 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1363 m_list
->ChangeToReportMode();
1364 ms_lastViewStyle
= wxLC_REPORT
;
1368 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1370 m_list
->GoToParentDir();
1375 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1377 m_list
->GoToHomeDir();
1382 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1387 void wxGenericFileDialog::SetPath( const wxString
& path
)
1389 // not only set the full path but also update filename and dir
1391 if ( !path
.empty() )
1394 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1397 m_fileName
+= wxT(".");
1403 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1406 if (m_list
->GetSelectedItemCount() == 0)
1408 paths
.Add( GetPath() );
1412 paths
.Alloc( m_list
->GetSelectedItemCount() );
1414 wxString dir
= m_list
->GetDir();
1416 if (dir
!= wxT("/"))
1418 dir
+= wxFILE_SEP_PATH
;
1421 item
.m_mask
= wxLIST_MASK_TEXT
;
1423 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1424 while ( item
.m_itemId
!= -1 )
1426 m_list
->GetItem( item
);
1427 paths
.Add( dir
+ item
.m_text
);
1428 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1429 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1433 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1436 if (m_list
->GetSelectedItemCount() == 0)
1438 files
.Add( GetFilename() );
1441 files
.Alloc( m_list
->GetSelectedItemCount() );
1444 item
.m_mask
= wxLIST_MASK_TEXT
;
1446 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1447 while ( item
.m_itemId
!= -1 )
1449 m_list
->GetItem( item
);
1450 files
.Add( item
.m_text
);
1451 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1452 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1456 void wxGenericFileDialog::UpdateControls()
1458 wxString dir
= m_list
->GetDir();
1459 m_static
->SetLabel(dir
);
1461 bool enable
= !IsTopMostDir(dir
);
1462 m_upDirButton
->Enable(enable
);
1464 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1465 m_newDirButton
->Enable(enable
);
1466 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1469 #ifdef USE_GENERIC_FILEDIALOG
1471 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1473 #endif // USE_GENERIC_FILEDIALOG
1475 #endif // wxUSE_FILEDLG