1 //////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "filedlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 // NOTE : it probably also supports MAC, untested
26 #if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__)
27 #error wxGenericFileDialog currently only supports Unix, win32 and DOS
30 #include "wx/checkbox.h"
31 #include "wx/textctrl.h"
32 #include "wx/choice.h"
33 #include "wx/checkbox.h"
34 #include "wx/stattext.h"
38 #include "wx/msgdlg.h"
40 #include "wx/bmpbuttn.h"
41 #include "wx/tokenzr.h"
42 #include "wx/config.h"
43 #include "wx/imaglist.h"
45 #include "wx/artprov.h"
46 #include "wx/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>
67 #include "wx/msw/wrapwin.h"
68 #include "wx/msw/mslu.h"
76 #if defined(__UNIX__) || defined(__DOS__)
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
85 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long data
)
87 wxFileData
*fd1
= (wxFileData
*)data1
;
88 wxFileData
*fd2
= (wxFileData
*)data2
;
89 if (fd1
->GetFileName() == wxT("..")) return -data
;
90 if (fd2
->GetFileName() == wxT("..")) return data
;
91 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
92 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
93 return data
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
97 int wxCALLBACK
wxFileDataSizeCompare( long data1
, long data2
, long data
)
99 wxFileData
*fd1
= (wxFileData
*)data1
;
100 wxFileData
*fd2
= (wxFileData
*)data2
;
101 if (fd1
->GetFileName() == wxT("..")) return -data
;
102 if (fd2
->GetFileName() == wxT("..")) return data
;
103 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
104 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
105 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
106 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
107 return data
*(fd1
->GetSize() - fd2
->GetSize());
111 int wxCALLBACK
wxFileDataTypeCompare( long data1
, long data2
, long data
)
113 wxFileData
*fd1
= (wxFileData
*)data1
;
114 wxFileData
*fd2
= (wxFileData
*)data2
;
115 if (fd1
->GetFileName() == wxT("..")) return -data
;
116 if (fd2
->GetFileName() == wxT("..")) return data
;
117 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
118 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
119 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
120 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
121 return data
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
125 int wxCALLBACK
wxFileDataTimeCompare( long data1
, long data2
, long data
)
127 wxFileData
*fd1
= (wxFileData
*)data1
;
128 wxFileData
*fd2
= (wxFileData
*)data2
;
129 if (fd1
->GetFileName() == wxT("..")) return -data
;
130 if (fd2
->GetFileName() == wxT("..")) return data
;
131 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
132 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
134 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? int(data
) : -int(data
);
137 #if defined(__UNIX__) && !defined(__OS2__)
138 #define IsTopMostDir(dir) (dir == wxT("/"))
141 #if defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)
142 #define IsTopMostDir(dir) (dir.IsEmpty())
145 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
146 // defined in src/generic/dirctrlg.cpp
147 extern bool wxIsDriveAvailable(const wxString
& dirName
);
150 // defined in src/generic/dirctrlg.cpp
151 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
153 //-----------------------------------------------------------------------------
155 //-----------------------------------------------------------------------------
157 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
159 m_fileName
= fileName
;
160 m_filePath
= filePath
;
167 void wxFileData::Copy( const wxFileData
& fileData
)
169 m_fileName
= fileData
.GetFileName();
170 m_filePath
= fileData
.GetFilePath();
171 m_size
= fileData
.GetSize();
172 m_dateTime
= fileData
.GetDateTime();
173 m_permissions
= fileData
.GetPermissions();
174 m_type
= fileData
.GetType();
175 m_image
= GetImageId();
178 void wxFileData::ReadData()
186 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
187 // c:\.. is a drive don't stat it
188 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
194 #endif // __DOS__ || __WINDOWS__
198 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
199 lstat( m_filePath
.fn_str(), &buff
);
200 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
202 wxStat( m_filePath
, &buff
);
205 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
206 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
208 // try to get a better icon
209 if (m_image
== wxFileIconsTable::file
)
212 m_image
= wxFileIconsTable::executable
;
213 else if (m_fileName
.Find(wxT('.'), TRUE
) != wxNOT_FOUND
)
214 m_image
= wxTheFileIconsTable
->GetIconID(m_fileName
.AfterLast(wxT('.')));
217 m_size
= buff
.st_size
;
219 m_dateTime
= buff
.st_mtime
;
221 #if defined(__UNIX__)
222 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
223 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
224 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
225 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
226 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
227 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
228 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
229 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
230 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
231 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
232 #elif defined(__WIN32__)
233 DWORD attribs
= GetFileAttributes(m_filePath
);
234 if (attribs
!= (DWORD
)-1)
236 m_permissions
.Printf(_T("%c%c%c%c"),
237 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
238 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
239 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
240 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
245 wxString
wxFileData::GetFileType() const
253 else if (m_fileName
.Find(wxT('.'), TRUE
) != wxNOT_FOUND
)
254 return m_fileName
.AfterLast(wxT('.'));
256 return wxEmptyString
;
259 wxString
wxFileData::GetModificationTime() const
261 // want time as 01:02 so they line up nicely, no %r in WIN32
262 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
265 wxString
wxFileData::GetHint() const
267 wxString s
= m_filePath
;
277 s
+= wxString::Format( _("%ld bytes"), m_size
);
283 s
<< GetModificationTime()
291 wxString
wxFileData::GetEntry( fileListFieldType num
) const
301 if (!IsDir() && !IsLink() && !IsDrive())
302 s
.Printf(_T("%ld"), m_size
);
311 s
= GetModificationTime();
314 #if defined(__UNIX__) || defined(__WIN32__)
318 #endif // defined(__UNIX__) || defined(__WIN32__)
321 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
327 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
329 m_fileName
= fileName
;
330 m_filePath
= filePath
;
333 void wxFileData::MakeItem( wxListItem
&item
)
335 item
.m_text
= m_fileName
;
336 item
.ClearAttributes();
338 item
.SetTextColour(*wxRED
);
340 item
.SetTextColour(*wxBLUE
);
342 item
.m_image
= m_image
;
346 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
348 item
.SetTextColour(dg
);
350 item
.m_data
= (long)this;
353 //-----------------------------------------------------------------------------
355 //-----------------------------------------------------------------------------
357 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
359 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
360 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
361 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
362 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
363 EVT_LIST_COL_CLICK(-1, wxFileCtrl::OnListColClick
)
367 wxFileCtrl::wxFileCtrl()
369 m_showHidden
= FALSE
;
371 m_sort_field
= wxFileData::FileList_Name
;
374 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
376 const wxString
& wild
,
381 const wxValidator
&validator
,
382 const wxString
&name
)
383 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
386 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
388 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
390 m_showHidden
= showHidden
;
393 m_sort_field
= wxFileData::FileList_Name
;
395 m_dirName
= wxT("*");
397 if (style
& wxLC_REPORT
)
398 ChangeToReportMode();
401 void wxFileCtrl::ChangeToListMode()
404 SetSingleStyle( wxLC_LIST
);
408 void wxFileCtrl::ChangeToReportMode()
411 SetSingleStyle( wxLC_REPORT
);
413 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
414 // don't hardcode since mm/dd is dd/mm elsewhere
416 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
417 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
418 GetTextExtent(txt
, &w
, &h
);
420 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
421 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
422 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
423 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
424 #if defined(__UNIX__)
425 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
426 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
427 #elif defined(__WIN32__)
428 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
429 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
435 void wxFileCtrl::ChangeToSmallIconMode()
438 SetSingleStyle( wxLC_SMALL_ICON
);
442 void wxFileCtrl::ShowHidden( bool show
)
448 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
451 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
452 fd
->MakeItem( item
);
453 long my_style
= GetWindowStyleFlag();
454 if (my_style
& wxLC_REPORT
)
456 ret
= InsertItem( item
);
457 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
458 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
460 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
462 ret
= InsertItem( item
);
467 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
469 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
470 wxCHECK_RET(fd
, wxT("invalid filedata"));
474 SetItemText(item
, fd
->GetFileName());
475 SetItemImage(item
, fd
->GetImageId(), fd
->GetImageId());
477 if (GetWindowStyleFlag() & wxLC_REPORT
)
479 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
480 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
484 void wxFileCtrl::UpdateFiles()
486 // don't do anything before ShowModal() call which sets m_dirName
487 if ( m_dirName
== wxT("*") )
490 wxBusyCursor bcur
; // this may take a while...
498 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
499 if ( IsTopMostDir(m_dirName
) )
501 wxArrayString names
, paths
;
503 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
505 for (n
=0; n
<count
; n
++)
507 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
508 if (Add(fd
, item
) != -1)
515 #endif // defined(__DOS__) || defined(__WINDOWS__)
518 if ( !IsTopMostDir(m_dirName
) )
520 wxString
p(wxPathOnly(m_dirName
));
521 #if defined(__UNIX__) && !defined(__OS2__)
522 if (p
.IsEmpty()) p
= wxT("/");
524 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
525 if (Add(fd
, item
) != -1)
531 wxString
dirname(m_dirName
);
532 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
533 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
534 dirname
<< wxT('\\');
535 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
538 if ( dir
.IsOpened() )
540 wxString
dirPrefix(dirname
);
541 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
542 dirPrefix
+= wxFILE_SEP_PATH
;
544 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
549 // Get the directories first (not matched against wildcards):
550 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
553 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
554 if (Add(fd
, item
) != -1)
559 cont
= dir
.GetNext(&f
);
562 // Tokenize the wildcard string, so we can handle more than 1
563 // search pattern in a wildcard.
564 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
565 while ( tokenWild
.HasMoreTokens() )
567 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
568 wxDIR_FILES
| hiddenFlag
);
571 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
572 if (Add(fd
, item
) != -1)
577 cont
= dir
.GetNext(&f
);
583 SortItems(m_sort_field
, m_sort_foward
);
586 void wxFileCtrl::SetWild( const wxString
&wild
)
588 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
595 void wxFileCtrl::MakeDir()
597 wxString
new_name( _("NewName") );
598 wxString
path( m_dirName
);
599 path
+= wxFILE_SEP_PATH
;
601 if (wxFileExists(path
))
603 // try NewName0, NewName1 etc.
606 new_name
= _("NewName");
608 num
.Printf( wxT("%d"), i
);
612 path
+= wxFILE_SEP_PATH
;
615 } while (wxFileExists(path
));
621 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
626 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
630 long id
= Add( fd
, item
);
634 SortItems(m_sort_field
, m_sort_foward
);
635 id
= FindItem( 0, (long)fd
);
643 void wxFileCtrl::GoToParentDir()
645 if (!IsTopMostDir(m_dirName
))
647 size_t len
= m_dirName
.Len();
648 if (wxEndsWithPathSeparator(m_dirName
))
649 m_dirName
.Remove( len
-1, 1 );
650 wxString
fname( wxFileNameFromPath(m_dirName
) );
651 m_dirName
= wxPathOnly( m_dirName
);
652 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
653 if (!m_dirName
.IsEmpty())
655 if (m_dirName
.Last() == wxT('.'))
658 #elif defined(__UNIX__)
659 if (m_dirName
.IsEmpty())
660 m_dirName
= wxT("/");
663 long id
= FindItem( 0, fname
);
666 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
672 void wxFileCtrl::GoToHomeDir()
674 wxString s
= wxGetUserHome( wxString() );
678 void wxFileCtrl::GoToDir( const wxString
&dir
)
680 if (!wxDirExists(dir
)) return;
684 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
688 void wxFileCtrl::FreeItemData(wxListItem
& item
)
692 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
699 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
701 FreeItemData(event
.m_item
);
704 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&event
)
709 void wxFileCtrl::FreeAllItemsData()
712 item
.m_mask
= wxLIST_MASK_DATA
;
714 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
715 while ( item
.m_itemId
!= -1 )
719 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
723 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
725 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
728 if ((event
.GetLabel().IsEmpty()) ||
729 (event
.GetLabel() == _(".")) ||
730 (event
.GetLabel() == _("..")) ||
731 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
733 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
739 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
740 new_name
+= wxFILE_SEP_PATH
;
741 new_name
+= event
.GetLabel();
745 if (wxFileExists(new_name
))
747 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
752 if (wxRenameFile(fd
->GetFilePath(),new_name
))
754 fd
->SetNewName( new_name
, event
.GetLabel() );
755 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
756 UpdateItem( event
.GetItem() );
757 EnsureVisible( event
.GetItem() );
761 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
767 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
769 int col
= event
.GetColumn();
773 case wxFileData::FileList_Name
:
774 case wxFileData::FileList_Size
:
775 case wxFileData::FileList_Type
:
776 case wxFileData::FileList_Time
: break;
780 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
781 m_sort_foward
= !m_sort_foward
;
783 m_sort_field
= (wxFileData::fileListFieldType
)col
;
785 SortItems(m_sort_field
, m_sort_foward
);
788 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
790 m_sort_field
= field
;
791 m_sort_foward
= foward
;
792 long sort_dir
= foward
? 1 : -1;
794 switch (m_sort_field
)
796 case wxFileData::FileList_Name
:
798 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
801 case wxFileData::FileList_Size
:
803 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
806 case wxFileData::FileList_Type
:
808 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
811 case wxFileData::FileList_Time
:
813 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
820 wxFileCtrl::~wxFileCtrl()
824 //-----------------------------------------------------------------------------
825 // wxGenericFileDialog
826 //-----------------------------------------------------------------------------
828 #define ID_LIST_MODE (wxID_FILEDLGG )
829 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
830 #define ID_UP_DIR (wxID_FILEDLGG + 5)
831 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
832 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
833 #define ID_CHOICE (wxID_FILEDLGG + 8)
834 #define ID_TEXT (wxID_FILEDLGG + 9)
835 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
836 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
837 #define ID_CHECK (wxID_FILEDLGG + 12)
839 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
841 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
842 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
843 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
844 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
845 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
846 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
847 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
848 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
849 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
850 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
851 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
852 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
853 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
856 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
857 bool wxGenericFileDialog::ms_lastShowHidden
= FALSE
;
859 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
860 const wxString
& message
,
861 const wxString
& defaultDir
,
862 const wxString
& defaultFile
,
863 const wxString
& wildCard
,
866 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
)
868 wxDialog::Create( parent
, -1, message
, pos
, wxDefaultSize
,
869 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
871 if (wxConfig::Get(FALSE
))
873 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
875 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
879 if (m_dialogStyle
== 0)
880 m_dialogStyle
= wxOPEN
;
881 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
882 m_dialogStyle
|= wxOPEN
;
884 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
889 size_t len
= m_dir
.Len();
890 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
891 m_dir
.Remove( len
-1, 1 );
894 m_path
+= wxFILE_SEP_PATH
;
895 m_path
+= defaultFile
;
896 m_filterExtension
= wxEmptyString
;
898 // interpret wildcards
899 wxArrayString wildDescriptions
, wildFilters
;
900 if ( !ParseWildcard(m_wildCard
, wildDescriptions
, wildFilters
) )
902 wxFAIL_MSG( wxT("Wrong file type description") );
907 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
909 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
911 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
915 but
= new wxBitmapButton(this, ID_LIST_MODE
,
916 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_CMN_DIALOG
));
918 but
->SetToolTip( _("View files as a list view") );
920 buttonsizer
->Add( but
, 0, wxALL
, 5 );
922 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
923 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_CMN_DIALOG
));
925 but
->SetToolTip( _("View files as a detailed view") );
927 buttonsizer
->Add( but
, 0, wxALL
, 5 );
929 buttonsizer
->Add( 30, 5, 1 );
931 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
932 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_CMN_DIALOG
));
934 m_upDirButton
->SetToolTip( _("Go to parent directory") );
936 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
938 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
939 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
940 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_CMN_DIALOG
));
942 but
->SetToolTip( _("Go to home directory") );
944 buttonsizer
->Add( but
, 0, wxALL
, 5);
946 buttonsizer
->Add( 20, 20 );
949 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
950 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_CMN_DIALOG
));
952 m_newDirButton
->SetToolTip( _("Create new directory") );
954 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
957 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
959 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
961 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
963 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
964 m_static
= new wxStaticText( this, -1, m_dir
);
965 staticsizer
->Add( m_static
, 1 );
966 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
968 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
969 if ( !(m_dialogStyle
& wxMULTIPLE
) )
970 style2
|= wxLC_SINGLE_SEL
;
972 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
973 wildFilters
[0], ms_lastShowHidden
,
974 wxDefaultPosition
, wxSize(540,200),
979 // PDAs have a different screen layout
980 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
982 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
983 m_choice
= new wxChoice( this, ID_CHOICE
);
984 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
985 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
987 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
988 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
989 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
990 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
992 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
993 m_check
->SetValue( ms_lastShowHidden
);
994 textsizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 5 );
996 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
997 buttonsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxALL
, 5 );
998 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 5 );
999 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1003 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1005 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1006 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1007 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1008 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1009 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1011 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1012 m_choice
= new wxChoice( this, ID_CHOICE
);
1013 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1014 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1015 m_check
->SetValue( ms_lastShowHidden
);
1016 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1017 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
1018 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1021 for (size_t n
=0; n
<wildFilters
.GetCount(); n
++)
1023 m_choice
->Append( wildDescriptions
[n
], (void*) new wxString( wildFilters
[n
] ) );
1025 SetFilterIndex( 0 );
1027 SetAutoLayout( TRUE
);
1028 SetSizer( mainsizer
);
1030 mainsizer
->Fit( this );
1031 mainsizer
->SetSizeHints( this );
1038 wxGenericFileDialog::~wxGenericFileDialog()
1040 if (wxConfig::Get(FALSE
))
1042 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1044 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1048 const int count
= m_choice
->GetCount();
1049 for ( int i
= 0; i
< count
; i
++ )
1051 delete (wxString
*)m_choice
->GetClientData(i
);
1055 int wxGenericFileDialog::ShowModal()
1057 m_list
->GoToDir(m_dir
);
1059 m_text
->SetValue(m_fileName
);
1061 return wxDialog::ShowModal();
1064 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1066 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1067 m_list
->SetWild( *str
);
1068 m_filterIndex
= filterindex
;
1069 if ( str
->Left(2) == wxT("*.") )
1071 m_filterExtension
= str
->Mid(1);
1072 if (m_filterExtension
== _T(".*"))
1073 m_filterExtension
.clear();
1077 m_filterExtension
.clear();
1081 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1083 m_choice
->SetSelection( filterindex
);
1085 DoSetFilterIndex(filterindex
);
1088 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1090 DoSetFilterIndex((int)event
.GetInt());
1093 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1095 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1098 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1100 HandleAction( event
.m_item
.m_text
);
1103 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1105 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1106 cevent
.SetEventObject( this );
1107 GetEventHandler()->ProcessEvent( cevent
);
1110 static bool ignoreChanges
= FALSE
;
1112 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1116 // Clear selections. Otherwise when the user types in a value they may
1117 // not get the file whose name they typed.
1118 if (m_list
->GetSelectedItemCount() > 0)
1120 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1121 wxLIST_STATE_SELECTED
);
1122 while ( item
!= -1 )
1124 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1125 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1131 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1133 wxString
filename( event
.m_item
.m_text
);
1134 if (filename
== wxT("..")) return;
1136 wxString dir
= m_list
->GetDir();
1137 if (!IsTopMostDir(dir
))
1138 dir
+= wxFILE_SEP_PATH
;
1140 if (wxDirExists(dir
)) return;
1142 ignoreChanges
= TRUE
;
1143 m_text
->SetValue( filename
);
1144 ignoreChanges
= FALSE
;
1147 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1149 wxString
filename( fn
);
1150 wxString dir
= m_list
->GetDir();
1151 if (filename
.IsEmpty()) return;
1152 if (filename
== wxT(".")) return;
1154 // "some/place/" means they want to chdir not try to load "place"
1155 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1157 filename
= filename
.RemoveLast();
1159 if (filename
== wxT(".."))
1161 m_list
->GoToParentDir();
1168 if (filename
== wxT("~"))
1170 m_list
->GoToHomeDir();
1176 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1178 filename
= wxGetUserHome() + filename
.Remove(0, 1);
1182 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1183 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1185 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1187 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1190 m_list
->SetWild( filename
);
1194 if (!IsTopMostDir(dir
))
1195 dir
+= wxFILE_SEP_PATH
;
1196 if (!wxIsAbsolutePath(filename
))
1202 if (wxDirExists(filename
))
1204 m_list
->GoToDir( filename
);
1209 // they really wanted a dir, but it doesn't exist
1212 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1213 wxOK
| wxICON_ERROR
);
1217 // append the default extension to the filename if it doesn't have any
1219 // VZ: the logic of testing for !wxFileExists() only for the open file
1220 // dialog is not entirely clear to me, why don't we allow saving to a
1221 // file without extension as well?
1222 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1224 filename
= AppendExtension(filename
, m_filterExtension
);
1227 // check that the file [doesn't] exist if necessary
1228 if ( (m_dialogStyle
& wxSAVE
) &&
1229 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1230 wxFileExists( filename
) )
1233 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1235 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1238 else if ( (m_dialogStyle
& wxOPEN
) &&
1239 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1240 !wxFileExists(filename
) )
1242 wxMessageBox(_("Please choose an existing file."), _("Error"),
1243 wxOK
| wxICON_ERROR
);
1246 SetPath( filename
);
1248 // change to the directory where the user went if asked
1249 if ( m_dialogStyle
& wxCHANGE_DIR
)
1252 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1254 if ( cwd
!= wxGetCwd() )
1256 wxSetWorkingDirectory(cwd
);
1260 wxCommandEvent event
;
1261 wxDialog::OnOK(event
);
1264 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1266 HandleAction( m_text
->GetValue() );
1269 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1271 m_list
->ChangeToListMode();
1272 ms_lastViewStyle
= wxLC_LIST
;
1276 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1278 m_list
->ChangeToReportMode();
1279 ms_lastViewStyle
= wxLC_REPORT
;
1283 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1285 m_list
->GoToParentDir();
1290 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1292 m_list
->GoToHomeDir();
1297 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1302 void wxGenericFileDialog::SetPath( const wxString
& path
)
1304 // not only set the full path but also update filename and dir
1306 if ( !path
.empty() )
1309 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1312 m_fileName
+= wxT(".");
1318 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1321 if (m_list
->GetSelectedItemCount() == 0)
1323 paths
.Add( GetPath() );
1327 paths
.Alloc( m_list
->GetSelectedItemCount() );
1329 wxString dir
= m_list
->GetDir();
1331 if (dir
!= wxT("/"))
1333 dir
+= wxFILE_SEP_PATH
;
1336 item
.m_mask
= wxLIST_MASK_TEXT
;
1338 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1339 while ( item
.m_itemId
!= -1 )
1341 m_list
->GetItem( item
);
1342 paths
.Add( dir
+ item
.m_text
);
1343 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1344 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1348 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1351 if (m_list
->GetSelectedItemCount() == 0)
1353 files
.Add( GetFilename() );
1356 files
.Alloc( m_list
->GetSelectedItemCount() );
1359 item
.m_mask
= wxLIST_MASK_TEXT
;
1361 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1362 while ( item
.m_itemId
!= -1 )
1364 m_list
->GetItem( item
);
1365 files
.Add( item
.m_text
);
1366 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1367 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1371 void wxGenericFileDialog::UpdateControls()
1373 wxString dir
= m_list
->GetDir();
1374 m_static
->SetLabel(dir
);
1376 bool enable
= !IsTopMostDir(dir
);
1377 m_upDirButton
->Enable(enable
);
1379 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1380 m_newDirButton
->Enable(enable
);
1381 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1384 #ifdef USE_GENERIC_FILEDIALOG
1386 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1388 #endif // USE_GENERIC_FILEDIALOG
1390 #endif // wxUSE_FILEDLG