1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
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__)
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/file.h" // for wxS_IXXX constants only
47 #include "wx/filedlg.h" // wxOPEN, wxSAVE...
48 #include "wx/generic/filedlgg.h"
49 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
52 #include "wx/tooltip.h"
55 #include <sys/types.h>
71 #if defined(__UNIX__) || defined(__DOS__)
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
80 int wxFileDataNameCompare( long data1
, long data2
, long data
)
82 wxFileData
*fd1
= (wxFileData
*)data1
;
83 wxFileData
*fd2
= (wxFileData
*)data2
;
84 if (fd1
->GetFileName() == wxT("..")) return -data
;
85 if (fd2
->GetFileName() == wxT("..")) return data
;
86 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
87 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
88 return data
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
92 int wxFileDataSizeCompare( long data1
, long data2
, long data
)
94 wxFileData
*fd1
= (wxFileData
*)data1
;
95 wxFileData
*fd2
= (wxFileData
*)data2
;
96 if (fd1
->GetFileName() == wxT("..")) return -data
;
97 if (fd2
->GetFileName() == wxT("..")) return data
;
98 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
99 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
100 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
101 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
102 return data
*(fd1
->GetSize() - fd2
->GetSize());
106 int wxFileDataTypeCompare( long data1
, long data2
, long data
)
108 wxFileData
*fd1
= (wxFileData
*)data1
;
109 wxFileData
*fd2
= (wxFileData
*)data2
;
110 if (fd1
->GetFileName() == wxT("..")) return -data
;
111 if (fd2
->GetFileName() == wxT("..")) return data
;
112 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
113 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
114 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
115 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
116 return data
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
120 int wxFileDataTimeCompare( long data1
, long data2
, long data
)
122 wxFileData
*fd1
= (wxFileData
*)data1
;
123 wxFileData
*fd2
= (wxFileData
*)data2
;
124 if (fd1
->GetFileName() == wxT("..")) return -data
;
125 if (fd2
->GetFileName() == wxT("..")) return data
;
126 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
127 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
129 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? int(data
) : -int(data
);
133 #define IsTopMostDir(dir) (dir == wxT("/"))
136 #if defined(__DOS__) || defined(__WINDOWS__)
137 #define IsTopMostDir(dir) (dir.IsEmpty())
140 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
141 // defined in src/generic/dirctrlg.cpp
142 extern bool wxIsDriveAvailable(const wxString
& dirName
);
145 // defined in src/generic/dirctrlg.cpp
146 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
148 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
152 wxFileData::wxFileData( const wxFileData
& fileData
)
154 m_fileName
= fileData
.GetFileName();
155 m_filePath
= fileData
.GetFilePath();
156 m_size
= fileData
.GetSize();
157 m_dateTime
= fileData
.GetDateTime();
158 m_permissions
= fileData
.GetPermissions();
159 m_type
= fileData
.GetType();
160 m_image
= GetImageId();
163 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
165 m_fileName
= fileName
;
166 m_filePath
= filePath
;
173 void wxFileData::ReadData()
181 #if defined(__DOS__) || defined(__WINDOWS__)
182 // c:\.. is a drive don't stat it
183 if ((fileName
== wxT("..")) && (filePath
.length() <= 5))
189 #endif // __DOS__ || __WINDOWS__
193 #if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
194 lstat( m_filePath
.fn_str(), &buff
);
195 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
197 wxStat( m_filePath
, &buff
);
200 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
201 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
203 // try to get a better icon
204 if (m_image
== wxFileIconsTable::file
)
207 m_image
= wxFileIconsTable::executable
;
208 else if (m_fileName
.Find(wxT('.'), TRUE
) != wxNOT_FOUND
)
209 m_image
= wxTheFileIconsTable
->GetIconID(m_fileName
.AfterLast(wxT('.')));
212 m_size
= buff
.st_size
;
214 m_dateTime
= buff
.st_mtime
;
216 #if defined(__UNIX__)
217 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
218 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
219 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
220 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
221 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
222 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
223 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
224 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
225 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
226 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
227 #elif defined(__WIN32__)
228 DWORD attribs
= GetFileAttributes(filePath
);
229 if (attribs
!= (DWORD
)-1)
231 m_permissions
.Printf(_T("%c%c%c%c"),
232 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
233 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
234 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
235 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
240 wxString
wxFileData::GetFileType() const
248 else if (m_fileName
.Find(wxT('.'), TRUE
) != wxNOT_FOUND
)
249 return m_fileName
.AfterLast(wxT('.'));
251 return wxEmptyString
;
254 wxString
wxFileData::GetModificationTime() const
256 // want time as 01:02 so they line up nicely, no %r in WIN32
257 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
260 wxString
wxFileData::GetHint() const
262 wxString s
= m_filePath
;
272 s
+= wxString::Format( _("%ld bytes"), m_size
);
278 s
<< GetModificationTime()
286 wxString
wxFileData::GetEntry( fileListFieldType num
) const
296 if (!IsDir() && !IsLink() && !IsDrive())
297 s
.Printf(_T("%ld"), m_size
);
306 s
= GetModificationTime();
309 #if defined(__UNIX__) || defined(__WIN32__)
313 #endif // defined(__UNIX__) || defined(__WIN32__)
316 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
322 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
324 m_fileName
= fileName
;
325 m_filePath
= filePath
;
328 void wxFileData::MakeItem( wxListItem
&item
)
330 item
.m_text
= m_fileName
;
331 item
.ClearAttributes();
333 item
.SetTextColour(*wxRED
);
335 item
.SetTextColour(*wxBLUE
);
337 item
.m_image
= m_image
;
341 wxColour
*dg
= wxTheColourDatabase
->FindColour( _T("MEDIUM GREY") );
342 item
.SetTextColour(*dg
);
344 item
.m_data
= (long)this;
347 //-----------------------------------------------------------------------------
349 //-----------------------------------------------------------------------------
351 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
353 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
354 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
355 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
356 EVT_LIST_COL_CLICK(-1, wxFileCtrl::OnListColClick
)
360 wxFileCtrl::wxFileCtrl()
362 m_showHidden
= FALSE
;
364 m_sort_field
= wxFileData::FileList_Name
;
367 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
369 const wxString
& wild
,
374 const wxValidator
&validator
,
375 const wxString
&name
)
376 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
379 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
381 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
383 m_showHidden
= showHidden
;
386 m_sort_field
= wxFileData::FileList_Name
;
388 m_dirName
= wxT("*");
390 if (style
& wxLC_REPORT
)
391 ChangeToReportMode();
394 void wxFileCtrl::ChangeToListMode()
397 SetSingleStyle( wxLC_LIST
);
401 void wxFileCtrl::ChangeToReportMode()
404 SetSingleStyle( wxLC_REPORT
);
406 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
407 // don't hardcode since mm/dd is dd/mm elsewhere
409 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
410 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
411 GetTextExtent(txt
, &w
, &h
);
413 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
414 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
415 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
416 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
417 #if defined(__UNIX__)
418 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
419 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
420 #elif defined(__WIN32__)
421 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
422 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
428 void wxFileCtrl::ChangeToSmallIconMode()
431 SetSingleStyle( wxLC_SMALL_ICON
);
435 void wxFileCtrl::ShowHidden( bool show
)
441 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
444 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
445 fd
->MakeItem( item
);
446 long my_style
= GetWindowStyleFlag();
447 if (my_style
& wxLC_REPORT
)
449 ret
= InsertItem( item
);
450 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
451 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
453 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
455 ret
= InsertItem( item
);
460 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
462 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
463 wxCHECK_RET(fd
, wxT("invalid filedata"));
467 SetItemText(item
, fd
->GetFileName());
468 SetItemImage(item
, fd
->GetImageId(), fd
->GetImageId());
470 if (GetWindowStyleFlag() & wxLC_REPORT
)
472 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
473 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
477 void wxFileCtrl::UpdateFiles()
479 // don't do anything before ShowModal() call which sets m_dirName
480 if ( m_dirName
== wxT("*") )
483 wxBusyCursor bcur
; // this may take a while...
488 wxFileData
*fd
= (wxFileData
*) NULL
;
493 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__WXPM__)
494 if ( IsTopMostDir(m_dirName
) )
496 wxArrayString names
, paths
;
498 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
500 for (n
=0; n
<count
; n
++)
502 fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
508 #endif // defined(__DOS__) || defined(__WINDOWS__)
511 if ( !IsTopMostDir(m_dirName
) )
513 wxString
p(wxPathOnly(m_dirName
));
515 if (p
.IsEmpty()) p
= wxT("/");
517 fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
522 wxString
dirname(m_dirName
);
523 #if defined(__DOS__) || defined(__WINDOWS__)
524 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
525 dirname
<< wxT('\\');
526 #endif // defined(__DOS__) || defined(__WINDOWS__)
529 if ( dir
.IsOpened() )
531 wxString
dirPrefix(dirname
);
532 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
533 dirPrefix
+= wxFILE_SEP_PATH
;
535 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
540 // Get the directories first (not matched against wildcards):
541 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
544 fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
547 cont
= dir
.GetNext(&f
);
550 // Tokenize the wildcard string, so we can handle more than 1
551 // search pattern in a wildcard.
552 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
553 while ( tokenWild
.HasMoreTokens() )
555 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
556 wxDIR_FILES
| hiddenFlag
);
559 fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
562 cont
= dir
.GetNext(&f
);
568 SortItems(m_sort_field
, m_sort_foward
);
571 void wxFileCtrl::SetWild( const wxString
&wild
)
573 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
580 void wxFileCtrl::MakeDir()
582 wxString
new_name( _("NewName") );
583 wxString
path( m_dirName
);
584 path
+= wxFILE_SEP_PATH
;
586 if (wxFileExists(path
))
588 // try NewName0, NewName1 etc.
591 new_name
= _("NewName");
593 num
.Printf( wxT("%d"), i
);
597 path
+= wxFILE_SEP_PATH
;
600 } while (wxFileExists(path
));
606 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
611 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
615 long id
= Add( fd
, item
);
619 SortItems(m_sort_field
, m_sort_foward
);
620 id
= FindItem( 0, (long)fd
);
626 void wxFileCtrl::GoToParentDir()
628 if (!IsTopMostDir(m_dirName
))
630 size_t len
= m_dirName
.Len();
631 if (wxEndsWithPathSeparator(m_dirName
))
632 m_dirName
.Remove( len
-1, 1 );
633 wxString
fname( wxFileNameFromPath(m_dirName
) );
634 m_dirName
= wxPathOnly( m_dirName
);
635 #if defined(__DOS__) || defined(__WINDOWS__)
636 if (!m_dirName
.IsEmpty())
638 if (m_dirName
.Last() == wxT('.'))
641 #elif defined(__UNIX__)
642 if (m_dirName
.IsEmpty())
643 m_dirName
= wxT("/");
646 long id
= FindItem( 0, fname
);
649 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
655 void wxFileCtrl::GoToHomeDir()
657 wxString s
= wxGetUserHome( wxString() );
661 void wxFileCtrl::GoToDir( const wxString
&dir
)
663 if (!wxDirExists(dir
)) return;
667 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
671 void wxFileCtrl::FreeItemData(const wxListItem
& item
)
673 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
677 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
679 FreeItemData(event
.m_item
);
682 void wxFileCtrl::FreeAllItemsData()
685 item
.m_mask
= wxLIST_MASK_DATA
;
687 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
688 while ( item
.m_itemId
!= -1 )
692 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
696 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
698 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
701 if ((event
.GetLabel().IsEmpty()) ||
702 (event
.GetLabel() == _(".")) ||
703 (event
.GetLabel() == _("..")) ||
704 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
706 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
712 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
713 new_name
+= wxFILE_SEP_PATH
;
714 new_name
+= event
.GetLabel();
718 if (wxFileExists(new_name
))
720 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
725 if (wxRenameFile(fd
->GetFilePath(),new_name
))
727 fd
->SetNewName( new_name
, event
.GetLabel() );
728 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
729 UpdateItem( event
.GetItem() );
730 EnsureVisible( event
.GetItem() );
734 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
740 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
742 int col
= event
.GetColumn();
746 case wxFileData::FileList_Name
:
747 case wxFileData::FileList_Size
:
748 case wxFileData::FileList_Type
:
749 case wxFileData::FileList_Time
: break;
753 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
754 m_sort_foward
= !m_sort_foward
;
756 m_sort_field
= (wxFileData::fileListFieldType
)col
;
758 SortItems(m_sort_field
, m_sort_foward
);
761 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
763 m_sort_field
= field
;
764 m_sort_foward
= foward
;
765 long sort_dir
= foward
? 1 : -1;
767 switch (m_sort_field
)
769 case wxFileData::FileList_Name
:
771 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
774 case wxFileData::FileList_Size
:
776 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
779 case wxFileData::FileList_Type
:
781 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
784 case wxFileData::FileList_Time
:
786 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
793 wxFileCtrl::~wxFileCtrl()
798 //-----------------------------------------------------------------------------
799 // wxGenericFileDialog
800 //-----------------------------------------------------------------------------
802 #define ID_LIST_MODE (wxID_FILEDLGG )
803 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
804 #define ID_UP_DIR (wxID_FILEDLGG + 5)
805 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
806 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
807 #define ID_CHOICE (wxID_FILEDLGG + 8)
808 #define ID_TEXT (wxID_FILEDLGG + 9)
809 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
810 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
811 #define ID_CHECK (wxID_FILEDLGG + 12)
813 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
,wxDialog
)
815 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
816 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
817 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
818 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
819 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
820 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
821 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
822 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
823 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
824 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
825 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
826 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
827 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
830 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
831 bool wxGenericFileDialog::ms_lastShowHidden
= FALSE
;
833 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
834 const wxString
& message
,
835 const wxString
& defaultDir
,
836 const wxString
& defaultFile
,
837 const wxString
& wildCard
,
840 : wxDialog( parent
, -1, message
, pos
, wxDefaultSize
,
841 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
843 if (wxConfig::Get(FALSE
))
845 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
847 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
852 m_dialogStyle
= style
;
854 if (m_dialogStyle
== 0)
855 m_dialogStyle
= wxOPEN
;
856 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
857 m_dialogStyle
|= wxOPEN
;
860 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
865 size_t len
= m_dir
.Len();
866 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
867 m_dir
.Remove( len
-1, 1 );
870 m_path
+= wxFILE_SEP_PATH
;
871 m_path
+= defaultFile
;
872 m_fileName
= defaultFile
;
873 m_wildCard
= wildCard
;
875 m_filterExtension
= wxEmptyString
;
877 // interpret wildcards
879 if (m_wildCard
.IsEmpty())
880 m_wildCard
= _("All files (*)|*");
882 wxArrayString wildDescriptions
, wildFilters
;
883 int wild_count
= wxParseFileFilter(m_wildCard
, wildDescriptions
, wildFilters
);
885 wxASSERT_MSG(wild_count
> 0, wxT("Wrong file type descripition") );
887 // if error parsing, add default back
888 if (wildFilters
.GetCount() < 1u)
891 m_wildCard
= _("All files (*)|*");
892 wildDescriptions
.Add(_("All files (*)"));
893 wildFilters
.Add(wxT("*"));
898 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
900 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
902 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
906 but
= new wxBitmapButton(this, ID_LIST_MODE
,
907 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_CMN_DIALOG
));
909 but
->SetToolTip( _("View files as a list view") );
911 buttonsizer
->Add( but
, 0, wxALL
, 5 );
913 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
914 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_CMN_DIALOG
));
916 but
->SetToolTip( _("View files as a detailed view") );
918 buttonsizer
->Add( but
, 0, wxALL
, 5 );
920 buttonsizer
->Add( 30, 5, 1 );
922 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
923 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_CMN_DIALOG
));
925 m_upDirButton
->SetToolTip( _("Go to parent directory") );
927 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
929 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
930 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
931 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_CMN_DIALOG
));
933 but
->SetToolTip( _("Go to home directory") );
935 buttonsizer
->Add( but
, 0, wxALL
, 5);
937 buttonsizer
->Add( 20, 20 );
940 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
941 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_CMN_DIALOG
));
943 m_newDirButton
->SetToolTip( _("Create new directory") );
945 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
948 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
950 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
952 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
954 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
955 m_static
= new wxStaticText( this, -1, m_dir
);
956 staticsizer
->Add( m_static
, 1 );
957 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
959 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
960 if ( !(m_dialogStyle
& wxMULTIPLE
) )
961 style2
|= wxLC_SINGLE_SEL
;
963 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
964 wildFilters
[0], ms_lastShowHidden
,
965 wxDefaultPosition
, wxSize(540,200),
970 // PDAs have a different screen layout
971 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
973 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
974 m_choice
= new wxChoice( this, ID_CHOICE
);
975 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
976 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
978 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
979 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
980 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
981 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
983 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
984 m_check
->SetValue( ms_lastShowHidden
);
985 textsizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 5 );
987 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
988 buttonsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxALL
, 5 );
989 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 5 );
990 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
994 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
996 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
997 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
998 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
999 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1000 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1002 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1003 m_choice
= new wxChoice( this, ID_CHOICE
);
1004 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1005 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1006 m_check
->SetValue( ms_lastShowHidden
);
1007 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1008 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
1009 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1012 for (size_t n
=0; n
<wildFilters
.GetCount(); n
++)
1014 m_choice
->Append( wildDescriptions
[n
], (void*) new wxString( wildFilters
[n
] ) );
1016 SetFilterIndex( 0 );
1018 SetAutoLayout( TRUE
);
1019 SetSizer( mainsizer
);
1021 mainsizer
->Fit( this );
1022 mainsizer
->SetSizeHints( this );
1029 wxGenericFileDialog::~wxGenericFileDialog()
1031 if (wxConfig::Get(FALSE
))
1033 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1035 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1039 const int count
= m_choice
->GetCount();
1040 for ( int i
= 0; i
< count
; i
++ )
1042 delete (wxString
*)m_choice
->GetClientData(i
);
1046 int wxGenericFileDialog::ShowModal()
1048 m_list
->GoToDir(m_dir
);
1050 m_text
->SetValue(m_fileName
);
1052 return wxDialog::ShowModal();
1055 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1057 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1058 m_list
->SetWild( *str
);
1059 m_filterIndex
= filterindex
;
1060 if ( str
->Left(2) == wxT("*.") )
1062 m_filterExtension
= str
->Mid(1);
1063 if (m_filterExtension
== _T(".*"))
1064 m_filterExtension
.clear();
1068 m_filterExtension
.clear();
1072 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1074 m_choice
->SetSelection( filterindex
);
1076 DoSetFilterIndex(filterindex
);
1079 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1081 DoSetFilterIndex((int)event
.GetInt());
1084 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1086 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1089 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1091 HandleAction( event
.m_item
.m_text
);
1094 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1096 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1097 cevent
.SetEventObject( this );
1098 GetEventHandler()->ProcessEvent( cevent
);
1101 static bool ignoreChanges
= FALSE
;
1103 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1107 // Clear selections. Otherwise when the user types in a value they may
1108 // not get the file whose name they typed.
1109 if (m_list
->GetSelectedItemCount() > 0)
1111 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1112 wxLIST_STATE_SELECTED
);
1113 while ( item
!= -1 )
1115 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1116 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1122 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1124 wxString
filename( event
.m_item
.m_text
);
1125 if (filename
== wxT("..")) return;
1127 wxString dir
= m_list
->GetDir();
1128 if (!IsTopMostDir(dir
))
1129 dir
+= wxFILE_SEP_PATH
;
1131 if (wxDirExists(dir
)) return;
1133 ignoreChanges
= TRUE
;
1134 m_text
->SetValue( filename
);
1135 ignoreChanges
= FALSE
;
1138 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1140 wxString
filename( fn
);
1141 wxString dir
= m_list
->GetDir();
1142 if (filename
.IsEmpty()) return;
1143 if (filename
== wxT(".")) return;
1145 // "some/place/" means they want to chdir not try to load "place"
1146 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1148 filename
= filename
.RemoveLast();
1150 if (filename
== wxT(".."))
1152 m_list
->GoToParentDir();
1159 if (filename
== wxT("~"))
1161 m_list
->GoToHomeDir();
1167 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1169 filename
= wxGetUserHome() + filename
.Remove(0, 1);
1173 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1174 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1176 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1178 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1181 m_list
->SetWild( filename
);
1185 if (!IsTopMostDir(dir
))
1186 dir
+= wxFILE_SEP_PATH
;
1187 if (!wxIsAbsolutePath(filename
))
1193 if (wxDirExists(filename
))
1195 m_list
->GoToDir( filename
);
1200 // they really wanted a dir, but it doesn't exist
1203 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1204 wxOK
| wxICON_ERROR
);
1208 // append the default extension to the filename if it doesn't have any
1210 // VZ: the logic of testing for !wxFileExists() only for the open file
1211 // dialog is not entirely clear to me, why don't we allow saving to a
1212 // file without extension as well?
1213 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1216 wxSplitPath(filename
, NULL
, NULL
, &ext
);
1219 // append the first extension of the filter string
1220 filename
+= m_filterExtension
.BeforeFirst(_T(';'));
1224 // check that the file [doesn't] exist if necessary
1225 if ( (m_dialogStyle
& wxSAVE
) &&
1226 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1227 wxFileExists( filename
) )
1230 msg
.Printf( _("File '%s' already exists, do you really want to "
1231 "overwrite it?"), filename
.c_str() );
1233 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1236 else if ( (m_dialogStyle
& wxOPEN
) &&
1237 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1238 !wxFileExists(filename
) )
1240 wxMessageBox(_("Please choose an existing file."), _("Error"),
1241 wxOK
| wxICON_ERROR
);
1244 SetPath( filename
);
1246 // change to the directory where the user went if asked
1247 if ( m_dialogStyle
& wxCHANGE_DIR
)
1250 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1252 if ( cwd
!= wxGetWorkingDirectory() )
1254 wxSetWorkingDirectory(cwd
);
1258 wxCommandEvent event
;
1259 wxDialog::OnOK(event
);
1262 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1264 HandleAction( m_text
->GetValue() );
1267 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1269 m_list
->ChangeToListMode();
1270 ms_lastViewStyle
= wxLC_LIST
;
1274 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1276 m_list
->ChangeToReportMode();
1277 ms_lastViewStyle
= wxLC_REPORT
;
1281 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1283 m_list
->GoToParentDir();
1288 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1290 m_list
->GoToHomeDir();
1295 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1300 void wxGenericFileDialog::SetPath( const wxString
& path
)
1302 // not only set the full path but also update filename and dir
1304 if ( !path
.empty() )
1307 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1310 m_fileName
+= wxT(".");
1316 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1319 if (m_list
->GetSelectedItemCount() == 0)
1321 paths
.Add( GetPath() );
1325 paths
.Alloc( m_list
->GetSelectedItemCount() );
1327 wxString dir
= m_list
->GetDir();
1329 if (dir
!= wxT("/"))
1331 dir
+= wxFILE_SEP_PATH
;
1334 item
.m_mask
= wxLIST_MASK_TEXT
;
1336 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1337 while ( item
.m_itemId
!= -1 )
1339 m_list
->GetItem( item
);
1340 paths
.Add( dir
+ item
.m_text
);
1341 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1342 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1346 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1349 if (m_list
->GetSelectedItemCount() == 0)
1351 files
.Add( GetFilename() );
1354 files
.Alloc( m_list
->GetSelectedItemCount() );
1357 item
.m_mask
= wxLIST_MASK_TEXT
;
1359 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1360 while ( item
.m_itemId
!= -1 )
1362 m_list
->GetItem( item
);
1363 files
.Add( item
.m_text
);
1364 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1365 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1369 void wxGenericFileDialog::UpdateControls()
1371 wxString dir
= m_list
->GetDir();
1372 m_static
->SetLabel(dir
);
1374 bool enable
= !IsTopMostDir(dir
);
1375 m_upDirButton
->Enable(enable
);
1377 #if defined(__DOS__) || defined(__WINDOWS__)
1378 m_newDirButton
->Enable(enable
);
1379 #endif // defined(__DOS__) || defined(__WINDOWS__)
1382 #ifdef USE_GENERIC_FILEDIALOG
1384 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1386 #endif // USE_GENERIC_FILEDIALOG
1388 #endif // wxUSE_FILEDLG