1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "filedlgg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 // NOTE : it probably also supports MAC, untested
34 #if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__)
35 #error wxGenericFileDialog currently only supports Unix, win32 and DOS
38 #include "wx/checkbox.h"
39 #include "wx/textctrl.h"
40 #include "wx/choice.h"
41 #include "wx/checkbox.h"
42 #include "wx/stattext.h"
46 #include "wx/msgdlg.h"
48 #include "wx/bmpbuttn.h"
49 #include "wx/tokenzr.h"
50 #include "wx/config.h"
51 #include "wx/imaglist.h"
53 #include "wx/artprov.h"
54 #include "wx/file.h" // for wxS_IXXX constants only
55 #include "wx/filedlg.h" // wxOPEN, wxSAVE...
56 #include "wx/generic/filedlgg.h"
57 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
60 #include "wx/tooltip.h"
63 #include <sys/types.h>
79 #if defined(__UNIX__) || defined(__DOS__)
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
88 int wxFileDataNameCompare( long data1
, long data2
, long data
)
90 wxFileData
*fd1
= (wxFileData
*)data1
;
91 wxFileData
*fd2
= (wxFileData
*)data2
;
92 if (fd1
->GetFileName() == wxT("..")) return -data
;
93 if (fd2
->GetFileName() == wxT("..")) return data
;
94 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
95 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
96 return data
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
100 int wxFileDataSizeCompare( long data1
, long data2
, long data
)
102 wxFileData
*fd1
= (wxFileData
*)data1
;
103 wxFileData
*fd2
= (wxFileData
*)data2
;
104 if (fd1
->GetFileName() == wxT("..")) return -data
;
105 if (fd2
->GetFileName() == wxT("..")) return data
;
106 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
107 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
108 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
109 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
110 return data
*(fd1
->GetSize() - fd2
->GetSize());
114 int wxFileDataTypeCompare( long data1
, long data2
, long data
)
116 wxFileData
*fd1
= (wxFileData
*)data1
;
117 wxFileData
*fd2
= (wxFileData
*)data2
;
118 if (fd1
->GetFileName() == wxT("..")) return -data
;
119 if (fd2
->GetFileName() == wxT("..")) return data
;
120 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
121 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
122 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
123 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
124 return data
*wxStrcmp( fd1
->GetType(), fd2
->GetType() );
128 int wxFileDataTimeCompare( long data1
, long data2
, long data
)
130 wxFileData
*fd1
= (wxFileData
*)data1
;
131 wxFileData
*fd2
= (wxFileData
*)data2
;
132 if (fd1
->GetFileName() == wxT("..")) return -data
;
133 if (fd2
->GetFileName() == wxT("..")) return data
;
134 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
135 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
137 return fd1
->GetTime().IsLaterThan(fd2
->GetTime()) ? int(data
) : -int(data
);
141 #define IsTopMostDir(dir) (dir == wxT("/"))
144 #if defined(__DOS__) || defined(__WINDOWS__)
145 #define IsTopMostDir(dir) (dir.IsEmpty())
148 #if defined(__DOS__) || defined(__WINDOWS__)
149 // defined in src/generic/dirctrlg.cpp
150 extern bool wxIsDriveAvailable(const wxString
& dirName
);
153 // defined in src/generic/dirctrlg.cpp
154 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
156 //-----------------------------------------------------------------------------
158 //-----------------------------------------------------------------------------
160 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
162 m_fileName
= fileName
;
163 m_filePath
= filePath
;
173 #if defined(__DOS__) || defined(__WINDOWS__)
174 // c:\.. is a drive don't stat it
175 if ((fileName
== wxT("..")) && (filePath
.length() <= 5))
181 #endif // __DOS__ || __WINDOWS__
185 #if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
186 lstat( m_filePath
.fn_str(), &buff
);
187 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
189 wxStat( m_filePath
, &buff
);
192 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
193 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
195 // try to get a better icon
196 if (m_image
== wxFileIconsTable::file
)
199 m_image
= wxFileIconsTable::executable
;
200 else if (m_fileName
.Find(wxT('.'), TRUE
) != wxNOT_FOUND
)
201 m_image
= wxTheFileIconsTable
->GetIconID(m_fileName
.AfterLast(wxT('.')));
204 m_size
= buff
.st_size
;
206 m_dateTime
= buff
.st_mtime
;
208 #if defined(__UNIX__)
209 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
210 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
211 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
212 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
213 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
214 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
215 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
216 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
217 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
218 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
219 #elif defined(__WIN32__)
220 DWORD attribs
= GetFileAttributes(filePath
);
221 if (attribs
!= (DWORD
)-1)
223 m_permissions
.Printf(_T("%c%c%c%c"),
224 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
225 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
226 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
227 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
232 wxString
wxFileData::GetType() const
240 else if (m_fileName
.Find(wxT('.'), TRUE
) != wxNOT_FOUND
)
241 return m_fileName
.AfterLast(wxT('.'));
243 return wxEmptyString
;
246 wxString
wxFileData::GetModificationTime() const
248 // want time as 01:02 so they line up nicely, no %r in WIN32
249 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
252 wxString
wxFileData::GetHint() const
254 wxString s
= m_filePath
;
262 s
+= wxT("<DRIVE> ");
267 s
+= LongToString( m_size
);
271 s
+= GetModificationTime();
277 wxString
wxFileData::GetEntry( fileListFieldType num
) const
287 if (!IsDir() && !IsLink() && !IsDrive())
288 s
.Printf(_T("%ld"), m_size
);
297 s
= GetModificationTime();
300 #if defined(__UNIX__) || defined(__WIN32__)
304 #endif // defined(__UNIX__) || defined(__WIN32__)
307 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
313 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
315 m_fileName
= fileName
;
316 m_filePath
= filePath
;
319 void wxFileData::MakeItem( wxListItem
&item
)
321 item
.m_text
= m_fileName
;
322 item
.ClearAttributes();
324 item
.SetTextColour(*wxRED
);
326 item
.SetTextColour(*wxBLUE
);
328 item
.m_image
= m_image
;
332 wxColour
*dg
= wxTheColourDatabase
->FindColour( _T("MEDIUM GREY") );
333 item
.SetTextColour(*dg
);
335 item
.m_data
= (long)this;
338 //-----------------------------------------------------------------------------
340 //-----------------------------------------------------------------------------
342 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
344 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
345 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
346 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
347 EVT_LIST_COL_CLICK(-1, wxFileCtrl::OnListColClick
)
351 wxFileCtrl::wxFileCtrl()
353 m_showHidden
= FALSE
;
355 m_sort_field
= wxFileData::FileList_Name
;
358 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
360 const wxString
& wild
,
365 const wxValidator
&validator
,
366 const wxString
&name
)
367 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
370 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
372 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
374 m_showHidden
= showHidden
;
377 m_sort_field
= wxFileData::FileList_Name
;
379 m_dirName
= wxT("*");
381 if (style
& wxLC_REPORT
)
382 ChangeToReportMode();
385 void wxFileCtrl::ChangeToListMode()
388 SetSingleStyle( wxLC_LIST
);
392 void wxFileCtrl::ChangeToReportMode()
395 SetSingleStyle( wxLC_REPORT
);
397 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
398 // don't hardcode since mm/dd is dd/mm elsewhere
400 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
401 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
402 GetTextExtent(txt
, &w
, &h
);
404 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
405 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
406 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
407 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
408 #if defined(__UNIX__)
409 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
410 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
411 #elif defined(__WIN32__)
412 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
413 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
419 void wxFileCtrl::ChangeToSmallIconMode()
422 SetSingleStyle( wxLC_SMALL_ICON
);
426 void wxFileCtrl::ShowHidden( bool show
)
432 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
435 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
436 fd
->MakeItem( item
);
437 long my_style
= GetWindowStyleFlag();
438 if (my_style
& wxLC_REPORT
)
440 ret
= InsertItem( item
);
441 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
442 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
444 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
446 ret
= InsertItem( item
);
451 void wxFileCtrl::UpdateFiles()
453 // don't do anything before ShowModal() call which sets m_dirName
454 if ( m_dirName
== wxT("*") )
457 wxBusyCursor bcur
; // this may take a while...
462 wxFileData
*fd
= (wxFileData
*) NULL
;
467 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__WXPM__)
468 if ( IsTopMostDir(m_dirName
) )
470 wxArrayString names
, paths
;
472 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
474 for (n
=0; n
<count
; n
++)
476 fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
482 #endif // defined(__DOS__) || defined(__WINDOWS__)
485 if ( !IsTopMostDir(m_dirName
) )
487 wxString
p(wxPathOnly(m_dirName
));
489 if (p
.IsEmpty()) p
= wxT("/");
491 fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
496 wxString
dirname(m_dirName
);
497 #if defined(__DOS__) || defined(__WINDOWS__)
498 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
499 dirname
<< wxT('\\');
500 #endif // defined(__DOS__) || defined(__WINDOWS__)
503 if ( dir
.IsOpened() )
505 wxString
dirPrefix(dirname
);
506 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
507 dirPrefix
+= wxFILE_SEP_PATH
;
509 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
514 // Get the directories first (not matched against wildcards):
515 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
518 fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
521 cont
= dir
.GetNext(&f
);
524 // Tokenize the wildcard string, so we can handle more than 1
525 // search pattern in a wildcard.
526 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
527 while ( tokenWild
.HasMoreTokens() )
529 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
530 wxDIR_FILES
| hiddenFlag
);
533 fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
536 cont
= dir
.GetNext(&f
);
542 SortItems(m_sort_field
, m_sort_foward
);
545 void wxFileCtrl::SetWild( const wxString
&wild
)
547 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
554 void wxFileCtrl::MakeDir()
556 wxString
new_name( _("NewName") );
557 wxString
path( m_dirName
);
558 path
+= wxFILE_SEP_PATH
;
560 if (wxFileExists(path
))
562 // try NewName0, NewName1 etc.
565 new_name
= _("NewName");
567 num
.Printf( wxT("%d"), i
);
571 path
+= wxFILE_SEP_PATH
;
574 } while (wxFileExists(path
));
580 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
585 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
589 long id
= Add( fd
, item
);
593 SortItems(m_sort_field
, m_sort_foward
);
594 id
= FindItem( 0, (long)fd
);
600 void wxFileCtrl::GoToParentDir()
602 if (!IsTopMostDir(m_dirName
))
604 size_t len
= m_dirName
.Len();
605 if (wxEndsWithPathSeparator(m_dirName
))
606 m_dirName
.Remove( len
-1, 1 );
607 wxString
fname( wxFileNameFromPath(m_dirName
) );
608 m_dirName
= wxPathOnly( m_dirName
);
609 #if defined(__DOS__) || defined(__WINDOWS__)
610 if (!m_dirName
.IsEmpty())
612 if (m_dirName
.Last() == wxT('.'))
615 #elif defined(__UNIX__)
616 if (m_dirName
.IsEmpty())
617 m_dirName
= wxT("/");
620 long id
= FindItem( 0, fname
);
623 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
629 void wxFileCtrl::GoToHomeDir()
631 wxString s
= wxGetUserHome( wxString() );
635 void wxFileCtrl::GoToDir( const wxString
&dir
)
637 if (!wxDirExists(dir
)) return;
641 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
645 void wxFileCtrl::FreeItemData(const wxListItem
& item
)
647 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
651 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
653 FreeItemData(event
.m_item
);
656 void wxFileCtrl::FreeAllItemsData()
659 item
.m_mask
= wxLIST_MASK_DATA
;
661 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
662 while ( item
.m_itemId
!= -1 )
666 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
670 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
672 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
675 if ((event
.GetLabel().IsEmpty()) ||
676 (event
.GetLabel() == _(".")) ||
677 (event
.GetLabel() == _("..")) ||
678 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
680 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
686 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
687 new_name
+= wxFILE_SEP_PATH
;
688 new_name
+= event
.GetLabel();
692 if (wxFileExists(new_name
))
694 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
699 if (wxRenameFile(fd
->GetFilePath(),new_name
))
701 fd
->SetNewName( new_name
, event
.GetLabel() );
702 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
703 EnsureVisible( event
.GetItem() );
707 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
713 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
715 int col
= event
.GetColumn();
719 case wxFileData::FileList_Name
:
720 case wxFileData::FileList_Size
:
721 case wxFileData::FileList_Type
:
722 case wxFileData::FileList_Time
: break;
726 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
727 m_sort_foward
= !m_sort_foward
;
729 m_sort_field
= (wxFileData::fileListFieldType
)col
;
731 SortItems(m_sort_field
, m_sort_foward
);
734 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
736 m_sort_field
= field
;
737 m_sort_foward
= foward
;
738 long sort_dir
= foward
? 1 : -1;
740 switch (m_sort_field
)
742 case wxFileData::FileList_Name
:
744 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
747 case wxFileData::FileList_Size
:
749 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
752 case wxFileData::FileList_Type
:
754 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
757 case wxFileData::FileList_Time
:
759 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
766 wxFileCtrl::~wxFileCtrl()
771 //-----------------------------------------------------------------------------
772 // wxGenericFileDialog
773 //-----------------------------------------------------------------------------
775 #define ID_LIST_MODE (wxID_FILEDLGG )
776 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
777 #define ID_UP_DIR (wxID_FILEDLGG + 5)
778 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
779 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
780 #define ID_CHOICE (wxID_FILEDLGG + 8)
781 #define ID_TEXT (wxID_FILEDLGG + 9)
782 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
783 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
784 #define ID_CHECK (wxID_FILEDLGG + 12)
786 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
,wxDialog
)
788 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
789 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
790 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
791 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
792 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
793 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
794 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
795 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
796 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
797 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
798 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
799 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
800 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
803 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
804 bool wxGenericFileDialog::ms_lastShowHidden
= FALSE
;
806 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
807 const wxString
& message
,
808 const wxString
& defaultDir
,
809 const wxString
& defaultFile
,
810 const wxString
& wildCard
,
813 : wxDialog( parent
, -1, message
, pos
, wxDefaultSize
,
814 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
816 if (wxConfig::Get(FALSE
))
818 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
820 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
825 m_dialogStyle
= style
;
827 if (m_dialogStyle
== 0)
828 m_dialogStyle
= wxOPEN
;
829 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
830 m_dialogStyle
|= wxOPEN
;
833 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
838 size_t len
= m_dir
.Len();
839 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
840 m_dir
.Remove( len
-1, 1 );
843 m_path
+= wxFILE_SEP_PATH
;
844 m_path
+= defaultFile
;
845 m_fileName
= defaultFile
;
846 m_wildCard
= wildCard
;
848 m_filterExtension
= wxEmptyString
;
850 // interpret wildcards
852 if (m_wildCard
.IsEmpty())
853 m_wildCard
= _("All files (*)|*");
855 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
857 wxString firstWildText
;
858 if (tokens
.CountTokens() == 1)
860 firstWildText
= tokens
.GetNextToken();
861 firstWild
= firstWildText
;
865 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
866 firstWildText
= tokens
.GetNextToken();
867 firstWild
= tokens
.GetNextToken();
869 if ( firstWild
.Left( 2 ) == wxT("*.") )
870 m_filterExtension
= firstWild
.Mid( 1 );
871 if ( m_filterExtension
== wxT(".*") )
872 m_filterExtension
= wxEmptyString
;
876 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
878 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
880 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
884 but
= new wxBitmapButton(this, ID_LIST_MODE
,
885 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_CMN_DIALOG
));
887 but
->SetToolTip( _("View files as a list view") );
889 buttonsizer
->Add( but
, 0, wxALL
, 5 );
891 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
892 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_CMN_DIALOG
));
894 but
->SetToolTip( _("View files as a detailed view") );
896 buttonsizer
->Add( but
, 0, wxALL
, 5 );
898 buttonsizer
->Add( 30, 5, 1 );
900 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
901 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_CMN_DIALOG
));
903 m_upDirButton
->SetToolTip( _("Go to parent directory") );
905 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
907 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
908 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
909 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_CMN_DIALOG
));
911 but
->SetToolTip( _("Go to home directory") );
913 buttonsizer
->Add( but
, 0, wxALL
, 5);
915 buttonsizer
->Add( 20, 20 );
918 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
919 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_CMN_DIALOG
));
921 m_newDirButton
->SetToolTip( _("Create new directory") );
923 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
926 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
928 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
930 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
932 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
933 m_static
= new wxStaticText( this, -1, m_dir
);
934 staticsizer
->Add( m_static
, 1 );
935 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
937 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
938 if ( !(m_dialogStyle
& wxMULTIPLE
) )
939 style2
|= wxLC_SINGLE_SEL
;
941 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
942 firstWild
, ms_lastShowHidden
,
943 wxDefaultPosition
, wxSize(540,200),
948 // PDAs have a different screen layout
949 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
951 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
952 m_choice
= new wxChoice( this, ID_CHOICE
);
953 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
954 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
956 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
957 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
958 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
959 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
961 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
962 m_check
->SetValue( ms_lastShowHidden
);
963 textsizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 5 );
965 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
966 buttonsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxALL
, 5 );
967 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 5 );
968 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
972 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
974 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
975 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
976 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
977 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
978 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
980 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
981 m_choice
= new wxChoice( this, ID_CHOICE
);
982 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
983 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
984 m_check
->SetValue( ms_lastShowHidden
);
985 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
986 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
987 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
990 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
991 while (tokens
.HasMoreTokens())
993 firstWildText
= tokens
.GetNextToken();
994 firstWild
= tokens
.GetNextToken();
995 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
997 m_choice
->SetSelection( 0 );
999 SetAutoLayout( TRUE
);
1000 SetSizer( mainsizer
);
1002 mainsizer
->Fit( this );
1003 mainsizer
->SetSizeHints( this );
1010 wxGenericFileDialog::~wxGenericFileDialog()
1012 if (wxConfig::Get(FALSE
))
1014 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1016 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1020 const int count
= m_choice
->GetCount();
1021 for ( int i
= 0; i
< count
; i
++ )
1023 delete (wxString
*)m_choice
->GetClientData(i
);
1027 int wxGenericFileDialog::ShowModal()
1029 m_list
->GoToDir(m_dir
);
1031 m_text
->SetValue(m_fileName
);
1033 return wxDialog::ShowModal();
1036 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1038 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1039 m_list
->SetWild( *str
);
1040 m_filterIndex
= filterindex
;
1041 if ( str
->Left(2) == wxT("*.") )
1043 m_filterExtension
= str
->Mid(1);
1044 if (m_filterExtension
== _T(".*"))
1045 m_filterExtension
.clear();
1049 m_filterExtension
.clear();
1053 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1055 m_choice
->SetSelection( filterindex
);
1057 DoSetFilterIndex(filterindex
);
1060 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1062 DoSetFilterIndex((int)event
.GetInt());
1065 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1067 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1070 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1072 HandleAction( event
.m_item
.m_text
);
1075 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1077 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1078 cevent
.SetEventObject( this );
1079 GetEventHandler()->ProcessEvent( cevent
);
1082 static bool ignoreChanges
= FALSE
;
1084 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1088 // Clear selections. Otherwise when the user types in a value they may
1089 // not get the file whose name they typed.
1090 if (m_list
->GetSelectedItemCount() > 0)
1092 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1093 wxLIST_STATE_SELECTED
);
1094 while ( item
!= -1 )
1096 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1097 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1103 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1105 wxString
filename( event
.m_item
.m_text
);
1106 if (filename
== wxT("..")) return;
1108 wxString dir
= m_list
->GetDir();
1109 if (!IsTopMostDir(dir
))
1110 dir
+= wxFILE_SEP_PATH
;
1112 if (wxDirExists(dir
)) return;
1114 ignoreChanges
= TRUE
;
1115 m_text
->SetValue( filename
);
1116 ignoreChanges
= FALSE
;
1119 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1121 wxString
filename( fn
);
1122 wxString dir
= m_list
->GetDir();
1123 if (filename
.IsEmpty()) return;
1124 if (filename
== wxT(".")) return;
1126 if (filename
== wxT(".."))
1128 m_list
->GoToParentDir();
1135 if (filename
== wxT("~"))
1137 m_list
->GoToHomeDir();
1143 if (filename
[0u] == wxT('~'))
1145 filename
.Remove( 0, 1 );
1146 wxString
tmp( wxGetUserHome() );
1153 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1154 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1156 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1158 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1161 m_list
->SetWild( filename
);
1165 if (!IsTopMostDir(dir
))
1166 dir
+= wxFILE_SEP_PATH
;
1167 if (!wxIsAbsolutePath(filename
))
1173 if (wxDirExists(filename
))
1175 m_list
->GoToDir( filename
);
1180 // append the default extension to the filename if it doesn't have any
1182 // VZ: the logic of testing for !wxFileExists() only for the open file
1183 // dialog is not entirely clear to me, why don't we allow saving to a
1184 // file without extension as well?
1185 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1188 wxSplitPath(filename
, NULL
, NULL
, &ext
);
1191 // append the first extension of the filter string
1192 filename
+= m_filterExtension
.BeforeFirst(_T(';'));
1196 // check that the file [doesn't] exist if necessary
1197 if ( (m_dialogStyle
& wxSAVE
) &&
1198 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1199 wxFileExists( filename
) )
1202 msg
.Printf( _("File '%s' already exists, do you really want to "
1203 "overwrite it?"), filename
.c_str() );
1205 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1208 else if ( (m_dialogStyle
& wxOPEN
) &&
1209 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1210 !wxFileExists(filename
) )
1212 wxMessageBox(_("Please choose an existing file."), _("Error"),
1213 wxOK
| wxICON_ERROR
);
1216 SetPath( filename
);
1218 // change to the directory where the user went if asked
1219 if ( m_dialogStyle
& wxCHANGE_DIR
)
1222 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1224 if ( cwd
!= wxGetWorkingDirectory() )
1226 wxSetWorkingDirectory(cwd
);
1230 wxCommandEvent event
;
1231 wxDialog::OnOK(event
);
1234 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1236 HandleAction( m_text
->GetValue() );
1239 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1241 m_list
->ChangeToListMode();
1242 ms_lastViewStyle
= wxLC_LIST
;
1246 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1248 m_list
->ChangeToReportMode();
1249 ms_lastViewStyle
= wxLC_REPORT
;
1253 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1255 m_list
->GoToParentDir();
1260 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1262 m_list
->GoToHomeDir();
1267 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1272 void wxGenericFileDialog::SetPath( const wxString
& path
)
1274 // not only set the full path but also update filename and dir
1276 if ( !path
.empty() )
1279 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1282 m_fileName
+= wxT(".");
1288 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1291 if (m_list
->GetSelectedItemCount() == 0)
1293 paths
.Add( GetPath() );
1297 paths
.Alloc( m_list
->GetSelectedItemCount() );
1299 wxString dir
= m_list
->GetDir();
1301 if (dir
!= wxT("/"))
1303 dir
+= wxFILE_SEP_PATH
;
1306 item
.m_mask
= wxLIST_MASK_TEXT
;
1308 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1309 while ( item
.m_itemId
!= -1 )
1311 m_list
->GetItem( item
);
1312 paths
.Add( dir
+ item
.m_text
);
1313 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1314 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1318 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1321 if (m_list
->GetSelectedItemCount() == 0)
1323 files
.Add( GetFilename() );
1326 files
.Alloc( m_list
->GetSelectedItemCount() );
1329 item
.m_mask
= wxLIST_MASK_TEXT
;
1331 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1332 while ( item
.m_itemId
!= -1 )
1334 m_list
->GetItem( item
);
1335 files
.Add( item
.m_text
);
1336 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1337 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1341 void wxGenericFileDialog::UpdateControls()
1343 wxString dir
= m_list
->GetDir();
1344 m_static
->SetLabel(dir
);
1346 bool enable
= !IsTopMostDir(dir
);
1347 m_upDirButton
->Enable(enable
);
1349 #if defined(__DOS__) || defined(__WINDOWS__)
1350 m_newDirButton
->Enable(enable
);
1351 #endif // defined(__DOS__) || defined(__WINDOWS__)
1354 #ifdef USE_GENERIC_FILEDIALOG
1356 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1358 // ----------------------------------------------------------------------------
1360 // ----------------------------------------------------------------------------
1362 // common part of both wxFileSelectorEx() and wxFileSelector()
1364 DoSelectFile(const wxChar
*title
,
1365 const wxChar
*defaultDir
,
1366 const wxChar
*defaultFileName
,
1367 const wxChar
*defaultExtension
,
1368 int *indexDefaultExtension
,
1369 const wxChar
*filter
,
1375 // the filter may be either given explicitly or created automatically from
1376 // the default extension
1377 wxString filterReal
;
1380 // the user has specified the filter explicitly, use it
1381 filterReal
= filter
;
1383 else if ( !wxIsEmpty(defaultExtension
) )
1385 // create the filter to match the given extension
1386 filterReal
<< wxT("*.") << defaultExtension
;
1389 wxFileDialog
fileDialog(parent
,
1398 if ( fileDialog
.ShowModal() == wxID_OK
)
1400 path
= fileDialog
.GetPath();
1401 if ( indexDefaultExtension
)
1403 *indexDefaultExtension
= fileDialog
.GetFilterIndex();
1411 wxFileSelectorEx(const wxChar
*title
,
1412 const wxChar
*defaultDir
,
1413 const wxChar
*defaultFileName
,
1414 int *indexDefaultExtension
,
1415 const wxChar
*filter
,
1421 return DoSelectFile(title
,
1424 wxT(""), // def ext determined by index
1425 indexDefaultExtension
,
1434 wxFileSelector(const wxChar
*title
,
1435 const wxChar
*defaultDir
,
1436 const wxChar
*defaultFileName
,
1437 const wxChar
*defaultExtension
,
1438 const wxChar
*filter
,
1444 return DoSelectFile(title
,
1448 NULL
, // not interested in filter index
1456 static wxString
GetWildcardString(const wxChar
*ext
)
1461 if ( *ext
== wxT('.') )
1464 wild
<< _T("*.") << ext
;
1466 else // no extension specified
1468 wild
= wxFileSelectorDefaultWildcardStr
;
1474 wxString
wxLoadFileSelector(const wxChar
*what
,
1476 const wxChar
*nameDef
,
1480 if ( what
&& *what
)
1481 prompt
= wxString::Format(_("Load %s file"), what
);
1483 prompt
= _("Load file");
1485 return wxFileSelector(prompt
, NULL
, nameDef
, ext
,
1486 GetWildcardString(ext
), 0, parent
);
1489 wxString
wxSaveFileSelector(const wxChar
*what
,
1491 const wxChar
*nameDef
,
1495 if ( what
&& *what
)
1496 prompt
= wxString::Format(_("Save %s file"), what
);
1498 prompt
= _("Save file");
1500 return wxFileSelector(prompt
, NULL
, nameDef
, ext
,
1501 GetWildcardString(ext
), 0, parent
);
1504 #endif // USE_GENERIC_FILEDIALOG
1506 #endif // wxUSE_FILEDLG