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__) || defined(__OS2__)
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
;
264 s
+= wxString::Format( _("%ld bytes"), m_size
);
270 s
<< GetModificationTime()
278 wxString
wxFileData::GetEntry( fileListFieldType num
) const
288 if (!IsDir() && !IsLink() && !IsDrive())
289 s
.Printf(_T("%ld"), m_size
);
298 s
= GetModificationTime();
301 #if defined(__UNIX__) || defined(__WIN32__)
305 #endif // defined(__UNIX__) || defined(__WIN32__)
308 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
314 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
316 m_fileName
= fileName
;
317 m_filePath
= filePath
;
320 void wxFileData::MakeItem( wxListItem
&item
)
322 item
.m_text
= m_fileName
;
323 item
.ClearAttributes();
325 item
.SetTextColour(*wxRED
);
327 item
.SetTextColour(*wxBLUE
);
329 item
.m_image
= m_image
;
333 wxColour
*dg
= wxTheColourDatabase
->FindColour( _T("MEDIUM GREY") );
334 item
.SetTextColour(*dg
);
336 item
.m_data
= (long)this;
339 //-----------------------------------------------------------------------------
341 //-----------------------------------------------------------------------------
343 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
345 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
346 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
347 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
348 EVT_LIST_COL_CLICK(-1, wxFileCtrl::OnListColClick
)
352 wxFileCtrl::wxFileCtrl()
354 m_showHidden
= FALSE
;
356 m_sort_field
= wxFileData::FileList_Name
;
359 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
361 const wxString
& wild
,
366 const wxValidator
&validator
,
367 const wxString
&name
)
368 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
371 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
373 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
375 m_showHidden
= showHidden
;
378 m_sort_field
= wxFileData::FileList_Name
;
380 m_dirName
= wxT("*");
382 if (style
& wxLC_REPORT
)
383 ChangeToReportMode();
386 void wxFileCtrl::ChangeToListMode()
389 SetSingleStyle( wxLC_LIST
);
393 void wxFileCtrl::ChangeToReportMode()
396 SetSingleStyle( wxLC_REPORT
);
398 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
399 // don't hardcode since mm/dd is dd/mm elsewhere
401 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
402 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
403 GetTextExtent(txt
, &w
, &h
);
405 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
406 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
407 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
408 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
409 #if defined(__UNIX__)
410 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
411 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
412 #elif defined(__WIN32__)
413 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
414 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
420 void wxFileCtrl::ChangeToSmallIconMode()
423 SetSingleStyle( wxLC_SMALL_ICON
);
427 void wxFileCtrl::ShowHidden( bool show
)
433 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
436 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
437 fd
->MakeItem( item
);
438 long my_style
= GetWindowStyleFlag();
439 if (my_style
& wxLC_REPORT
)
441 ret
= InsertItem( item
);
442 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
443 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
445 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
447 ret
= InsertItem( item
);
452 void wxFileCtrl::UpdateFiles()
454 // don't do anything before ShowModal() call which sets m_dirName
455 if ( m_dirName
== wxT("*") )
458 wxBusyCursor bcur
; // this may take a while...
463 wxFileData
*fd
= (wxFileData
*) NULL
;
468 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__WXPM__)
469 if ( IsTopMostDir(m_dirName
) )
471 wxArrayString names
, paths
;
473 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
475 for (n
=0; n
<count
; n
++)
477 fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
483 #endif // defined(__DOS__) || defined(__WINDOWS__)
486 if ( !IsTopMostDir(m_dirName
) )
488 wxString
p(wxPathOnly(m_dirName
));
490 if (p
.IsEmpty()) p
= wxT("/");
492 fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
497 wxString
dirname(m_dirName
);
498 #if defined(__DOS__) || defined(__WINDOWS__)
499 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
500 dirname
<< wxT('\\');
501 #endif // defined(__DOS__) || defined(__WINDOWS__)
504 if ( dir
.IsOpened() )
506 wxString
dirPrefix(dirname
);
507 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
508 dirPrefix
+= wxFILE_SEP_PATH
;
510 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
515 // Get the directories first (not matched against wildcards):
516 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
519 fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
522 cont
= dir
.GetNext(&f
);
525 // Tokenize the wildcard string, so we can handle more than 1
526 // search pattern in a wildcard.
527 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
528 while ( tokenWild
.HasMoreTokens() )
530 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
531 wxDIR_FILES
| hiddenFlag
);
534 fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
537 cont
= dir
.GetNext(&f
);
543 SortItems(m_sort_field
, m_sort_foward
);
546 void wxFileCtrl::SetWild( const wxString
&wild
)
548 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
555 void wxFileCtrl::MakeDir()
557 wxString
new_name( _("NewName") );
558 wxString
path( m_dirName
);
559 path
+= wxFILE_SEP_PATH
;
561 if (wxFileExists(path
))
563 // try NewName0, NewName1 etc.
566 new_name
= _("NewName");
568 num
.Printf( wxT("%d"), i
);
572 path
+= wxFILE_SEP_PATH
;
575 } while (wxFileExists(path
));
581 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
586 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
590 long id
= Add( fd
, item
);
594 SortItems(m_sort_field
, m_sort_foward
);
595 id
= FindItem( 0, (long)fd
);
601 void wxFileCtrl::GoToParentDir()
603 if (!IsTopMostDir(m_dirName
))
605 size_t len
= m_dirName
.Len();
606 if (wxEndsWithPathSeparator(m_dirName
))
607 m_dirName
.Remove( len
-1, 1 );
608 wxString
fname( wxFileNameFromPath(m_dirName
) );
609 m_dirName
= wxPathOnly( m_dirName
);
610 #if defined(__DOS__) || defined(__WINDOWS__)
611 if (!m_dirName
.IsEmpty())
613 if (m_dirName
.Last() == wxT('.'))
616 #elif defined(__UNIX__)
617 if (m_dirName
.IsEmpty())
618 m_dirName
= wxT("/");
621 long id
= FindItem( 0, fname
);
624 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
630 void wxFileCtrl::GoToHomeDir()
632 wxString s
= wxGetUserHome( wxString() );
636 void wxFileCtrl::GoToDir( const wxString
&dir
)
638 if (!wxDirExists(dir
)) return;
642 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
646 void wxFileCtrl::FreeItemData(const wxListItem
& item
)
648 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
652 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
654 FreeItemData(event
.m_item
);
657 void wxFileCtrl::FreeAllItemsData()
660 item
.m_mask
= wxLIST_MASK_DATA
;
662 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
663 while ( item
.m_itemId
!= -1 )
667 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
671 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
673 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
676 if ((event
.GetLabel().IsEmpty()) ||
677 (event
.GetLabel() == _(".")) ||
678 (event
.GetLabel() == _("..")) ||
679 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
681 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
687 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
688 new_name
+= wxFILE_SEP_PATH
;
689 new_name
+= event
.GetLabel();
693 if (wxFileExists(new_name
))
695 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
700 if (wxRenameFile(fd
->GetFilePath(),new_name
))
702 fd
->SetNewName( new_name
, event
.GetLabel() );
703 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
704 EnsureVisible( event
.GetItem() );
708 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
714 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
716 int col
= event
.GetColumn();
720 case wxFileData::FileList_Name
:
721 case wxFileData::FileList_Size
:
722 case wxFileData::FileList_Type
:
723 case wxFileData::FileList_Time
: break;
727 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
728 m_sort_foward
= !m_sort_foward
;
730 m_sort_field
= (wxFileData::fileListFieldType
)col
;
732 SortItems(m_sort_field
, m_sort_foward
);
735 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
737 m_sort_field
= field
;
738 m_sort_foward
= foward
;
739 long sort_dir
= foward
? 1 : -1;
741 switch (m_sort_field
)
743 case wxFileData::FileList_Name
:
745 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
748 case wxFileData::FileList_Size
:
750 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
753 case wxFileData::FileList_Type
:
755 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
758 case wxFileData::FileList_Time
:
760 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
767 wxFileCtrl::~wxFileCtrl()
772 //-----------------------------------------------------------------------------
773 // wxGenericFileDialog
774 //-----------------------------------------------------------------------------
776 #define ID_LIST_MODE (wxID_FILEDLGG )
777 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
778 #define ID_UP_DIR (wxID_FILEDLGG + 5)
779 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
780 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
781 #define ID_CHOICE (wxID_FILEDLGG + 8)
782 #define ID_TEXT (wxID_FILEDLGG + 9)
783 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
784 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
785 #define ID_CHECK (wxID_FILEDLGG + 12)
787 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
,wxDialog
)
789 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
790 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
791 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
792 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
793 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
794 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
795 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
796 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
797 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
798 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
799 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
800 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
801 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
804 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
805 bool wxGenericFileDialog::ms_lastShowHidden
= FALSE
;
807 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
808 const wxString
& message
,
809 const wxString
& defaultDir
,
810 const wxString
& defaultFile
,
811 const wxString
& wildCard
,
814 : wxDialog( parent
, -1, message
, pos
, wxDefaultSize
,
815 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
817 if (wxConfig::Get(FALSE
))
819 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
821 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
826 m_dialogStyle
= style
;
828 if (m_dialogStyle
== 0)
829 m_dialogStyle
= wxOPEN
;
830 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
831 m_dialogStyle
|= wxOPEN
;
834 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
839 size_t len
= m_dir
.Len();
840 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
841 m_dir
.Remove( len
-1, 1 );
844 m_path
+= wxFILE_SEP_PATH
;
845 m_path
+= defaultFile
;
846 m_fileName
= defaultFile
;
847 m_wildCard
= wildCard
;
849 m_filterExtension
= wxEmptyString
;
851 // interpret wildcards
853 if (m_wildCard
.IsEmpty())
854 m_wildCard
= _("All files (*)|*");
856 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
858 wxString firstWildText
;
859 if (tokens
.CountTokens() == 1)
861 firstWildText
= tokens
.GetNextToken();
862 firstWild
= firstWildText
;
866 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
867 firstWildText
= tokens
.GetNextToken();
868 firstWild
= tokens
.GetNextToken();
870 if ( firstWild
.Left( 2 ) == wxT("*.") )
871 m_filterExtension
= firstWild
.Mid( 1 );
872 if ( m_filterExtension
== wxT(".*") )
873 m_filterExtension
= wxEmptyString
;
877 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
879 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
881 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
885 but
= new wxBitmapButton(this, ID_LIST_MODE
,
886 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_CMN_DIALOG
));
888 but
->SetToolTip( _("View files as a list view") );
890 buttonsizer
->Add( but
, 0, wxALL
, 5 );
892 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
893 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_CMN_DIALOG
));
895 but
->SetToolTip( _("View files as a detailed view") );
897 buttonsizer
->Add( but
, 0, wxALL
, 5 );
899 buttonsizer
->Add( 30, 5, 1 );
901 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
902 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_CMN_DIALOG
));
904 m_upDirButton
->SetToolTip( _("Go to parent directory") );
906 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
908 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
909 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
910 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_CMN_DIALOG
));
912 but
->SetToolTip( _("Go to home directory") );
914 buttonsizer
->Add( but
, 0, wxALL
, 5);
916 buttonsizer
->Add( 20, 20 );
919 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
920 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_CMN_DIALOG
));
922 m_newDirButton
->SetToolTip( _("Create new directory") );
924 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
927 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
929 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
931 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
933 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
934 m_static
= new wxStaticText( this, -1, m_dir
);
935 staticsizer
->Add( m_static
, 1 );
936 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
938 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
939 if ( !(m_dialogStyle
& wxMULTIPLE
) )
940 style2
|= wxLC_SINGLE_SEL
;
942 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
943 firstWild
, ms_lastShowHidden
,
944 wxDefaultPosition
, wxSize(540,200),
949 // PDAs have a different screen layout
950 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
952 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
953 m_choice
= new wxChoice( this, ID_CHOICE
);
954 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
955 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
957 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
958 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
959 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
960 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
962 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
963 m_check
->SetValue( ms_lastShowHidden
);
964 textsizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 5 );
966 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
967 buttonsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxALL
, 5 );
968 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 5 );
969 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
973 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
975 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
976 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
977 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
978 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
979 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
981 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
982 m_choice
= new wxChoice( this, ID_CHOICE
);
983 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
984 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
985 m_check
->SetValue( ms_lastShowHidden
);
986 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
987 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
988 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
991 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
992 while (tokens
.HasMoreTokens())
994 firstWildText
= tokens
.GetNextToken();
995 firstWild
= tokens
.GetNextToken();
996 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
998 m_choice
->SetSelection( 0 );
1000 SetAutoLayout( TRUE
);
1001 SetSizer( mainsizer
);
1003 mainsizer
->Fit( this );
1004 mainsizer
->SetSizeHints( this );
1011 wxGenericFileDialog::~wxGenericFileDialog()
1013 if (wxConfig::Get(FALSE
))
1015 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1017 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1021 const int count
= m_choice
->GetCount();
1022 for ( int i
= 0; i
< count
; i
++ )
1024 delete (wxString
*)m_choice
->GetClientData(i
);
1028 int wxGenericFileDialog::ShowModal()
1030 m_list
->GoToDir(m_dir
);
1032 m_text
->SetValue(m_fileName
);
1034 return wxDialog::ShowModal();
1037 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1039 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1040 m_list
->SetWild( *str
);
1041 m_filterIndex
= filterindex
;
1042 if ( str
->Left(2) == wxT("*.") )
1044 m_filterExtension
= str
->Mid(1);
1045 if (m_filterExtension
== _T(".*"))
1046 m_filterExtension
.clear();
1050 m_filterExtension
.clear();
1054 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1056 m_choice
->SetSelection( filterindex
);
1058 DoSetFilterIndex(filterindex
);
1061 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1063 DoSetFilterIndex((int)event
.GetInt());
1066 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1068 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1071 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1073 HandleAction( event
.m_item
.m_text
);
1076 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1078 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1079 cevent
.SetEventObject( this );
1080 GetEventHandler()->ProcessEvent( cevent
);
1083 static bool ignoreChanges
= FALSE
;
1085 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1089 // Clear selections. Otherwise when the user types in a value they may
1090 // not get the file whose name they typed.
1091 if (m_list
->GetSelectedItemCount() > 0)
1093 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1094 wxLIST_STATE_SELECTED
);
1095 while ( item
!= -1 )
1097 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1098 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1104 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1106 wxString
filename( event
.m_item
.m_text
);
1107 if (filename
== wxT("..")) return;
1109 wxString dir
= m_list
->GetDir();
1110 if (!IsTopMostDir(dir
))
1111 dir
+= wxFILE_SEP_PATH
;
1113 if (wxDirExists(dir
)) return;
1115 ignoreChanges
= TRUE
;
1116 m_text
->SetValue( filename
);
1117 ignoreChanges
= FALSE
;
1120 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1122 wxString
filename( fn
);
1123 wxString dir
= m_list
->GetDir();
1124 if (filename
.IsEmpty()) return;
1125 if (filename
== wxT(".")) return;
1127 if (filename
== wxT(".."))
1129 m_list
->GoToParentDir();
1136 if (filename
== wxT("~"))
1138 m_list
->GoToHomeDir();
1144 if (filename
[0u] == wxT('~'))
1146 filename
.Remove( 0, 1 );
1147 wxString
tmp( wxGetUserHome() );
1154 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1155 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1157 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1159 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1162 m_list
->SetWild( filename
);
1166 if (!IsTopMostDir(dir
))
1167 dir
+= wxFILE_SEP_PATH
;
1168 if (!wxIsAbsolutePath(filename
))
1174 if (wxDirExists(filename
))
1176 m_list
->GoToDir( filename
);
1181 // append the default extension to the filename if it doesn't have any
1183 // VZ: the logic of testing for !wxFileExists() only for the open file
1184 // dialog is not entirely clear to me, why don't we allow saving to a
1185 // file without extension as well?
1186 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1189 wxSplitPath(filename
, NULL
, NULL
, &ext
);
1192 // append the first extension of the filter string
1193 filename
+= m_filterExtension
.BeforeFirst(_T(';'));
1197 // check that the file [doesn't] exist if necessary
1198 if ( (m_dialogStyle
& wxSAVE
) &&
1199 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1200 wxFileExists( filename
) )
1203 msg
.Printf( _("File '%s' already exists, do you really want to "
1204 "overwrite it?"), filename
.c_str() );
1206 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1209 else if ( (m_dialogStyle
& wxOPEN
) &&
1210 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1211 !wxFileExists(filename
) )
1213 wxMessageBox(_("Please choose an existing file."), _("Error"),
1214 wxOK
| wxICON_ERROR
);
1217 SetPath( filename
);
1219 // change to the directory where the user went if asked
1220 if ( m_dialogStyle
& wxCHANGE_DIR
)
1223 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1225 if ( cwd
!= wxGetWorkingDirectory() )
1227 wxSetWorkingDirectory(cwd
);
1231 wxCommandEvent event
;
1232 wxDialog::OnOK(event
);
1235 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1237 HandleAction( m_text
->GetValue() );
1240 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1242 m_list
->ChangeToListMode();
1243 ms_lastViewStyle
= wxLC_LIST
;
1247 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1249 m_list
->ChangeToReportMode();
1250 ms_lastViewStyle
= wxLC_REPORT
;
1254 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1256 m_list
->GoToParentDir();
1261 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1263 m_list
->GoToHomeDir();
1268 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1273 void wxGenericFileDialog::SetPath( const wxString
& path
)
1275 // not only set the full path but also update filename and dir
1277 if ( !path
.empty() )
1280 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1283 m_fileName
+= wxT(".");
1289 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1292 if (m_list
->GetSelectedItemCount() == 0)
1294 paths
.Add( GetPath() );
1298 paths
.Alloc( m_list
->GetSelectedItemCount() );
1300 wxString dir
= m_list
->GetDir();
1302 if (dir
!= wxT("/"))
1304 dir
+= wxFILE_SEP_PATH
;
1307 item
.m_mask
= wxLIST_MASK_TEXT
;
1309 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1310 while ( item
.m_itemId
!= -1 )
1312 m_list
->GetItem( item
);
1313 paths
.Add( dir
+ item
.m_text
);
1314 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1315 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1319 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1322 if (m_list
->GetSelectedItemCount() == 0)
1324 files
.Add( GetFilename() );
1327 files
.Alloc( m_list
->GetSelectedItemCount() );
1330 item
.m_mask
= wxLIST_MASK_TEXT
;
1332 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1333 while ( item
.m_itemId
!= -1 )
1335 m_list
->GetItem( item
);
1336 files
.Add( item
.m_text
);
1337 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1338 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1342 void wxGenericFileDialog::UpdateControls()
1344 wxString dir
= m_list
->GetDir();
1345 m_static
->SetLabel(dir
);
1347 bool enable
= !IsTopMostDir(dir
);
1348 m_upDirButton
->Enable(enable
);
1350 #if defined(__DOS__) || defined(__WINDOWS__)
1351 m_newDirButton
->Enable(enable
);
1352 #endif // defined(__DOS__) || defined(__WINDOWS__)
1355 #ifdef USE_GENERIC_FILEDIALOG
1357 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1359 // ----------------------------------------------------------------------------
1361 // ----------------------------------------------------------------------------
1363 // common part of both wxFileSelectorEx() and wxFileSelector()
1365 DoSelectFile(const wxChar
*title
,
1366 const wxChar
*defaultDir
,
1367 const wxChar
*defaultFileName
,
1368 const wxChar
*defaultExtension
,
1369 int *indexDefaultExtension
,
1370 const wxChar
*filter
,
1376 // the filter may be either given explicitly or created automatically from
1377 // the default extension
1378 wxString filterReal
;
1381 // the user has specified the filter explicitly, use it
1382 filterReal
= filter
;
1384 else if ( !wxIsEmpty(defaultExtension
) )
1386 // create the filter to match the given extension
1387 filterReal
<< wxT("*.") << defaultExtension
;
1390 wxFileDialog
fileDialog(parent
,
1399 if ( fileDialog
.ShowModal() == wxID_OK
)
1401 path
= fileDialog
.GetPath();
1402 if ( indexDefaultExtension
)
1404 *indexDefaultExtension
= fileDialog
.GetFilterIndex();
1412 wxFileSelectorEx(const wxChar
*title
,
1413 const wxChar
*defaultDir
,
1414 const wxChar
*defaultFileName
,
1415 int *indexDefaultExtension
,
1416 const wxChar
*filter
,
1422 return DoSelectFile(title
,
1425 wxT(""), // def ext determined by index
1426 indexDefaultExtension
,
1435 wxFileSelector(const wxChar
*title
,
1436 const wxChar
*defaultDir
,
1437 const wxChar
*defaultFileName
,
1438 const wxChar
*defaultExtension
,
1439 const wxChar
*filter
,
1445 return DoSelectFile(title
,
1449 NULL
, // not interested in filter index
1457 static wxString
GetWildcardString(const wxChar
*ext
)
1462 if ( *ext
== wxT('.') )
1465 wild
<< _T("*.") << ext
;
1467 else // no extension specified
1469 wild
= wxFileSelectorDefaultWildcardStr
;
1475 wxString
wxLoadFileSelector(const wxChar
*what
,
1477 const wxChar
*nameDef
,
1481 if ( what
&& *what
)
1482 prompt
= wxString::Format(_("Load %s file"), what
);
1484 prompt
= _("Load file");
1486 return wxFileSelector(prompt
, NULL
, nameDef
, ext
,
1487 GetWildcardString(ext
), 0, parent
);
1490 wxString
wxSaveFileSelector(const wxChar
*what
,
1492 const wxChar
*nameDef
,
1496 if ( what
&& *what
)
1497 prompt
= wxString::Format(_("Save %s file"), what
);
1499 prompt
= _("Save file");
1501 return wxFileSelector(prompt
, NULL
, nameDef
, ext
,
1502 GetWildcardString(ext
), 0, parent
);
1505 #endif // USE_GENERIC_FILEDIALOG
1507 #endif // wxUSE_FILEDLG