1 //////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxGenericFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "filedlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 // NOTE : it probably also supports MAC, untested
26 #if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__)
27 #error wxGenericFileDialog currently only supports Unix, win32 and DOS
30 #include "wx/checkbox.h"
31 #include "wx/textctrl.h"
32 #include "wx/choice.h"
33 #include "wx/checkbox.h"
34 #include "wx/stattext.h"
38 #include "wx/msgdlg.h"
40 #include "wx/bmpbuttn.h"
41 #include "wx/tokenzr.h"
42 #include "wx/config.h"
43 #include "wx/imaglist.h"
45 #include "wx/artprov.h"
46 #include "wx/settings.h"
47 #include "wx/file.h" // for wxS_IXXX constants only
48 #include "wx/filedlg.h" // wxOPEN, wxSAVE...
49 #include "wx/generic/filedlgg.h"
50 #include "wx/generic/dirctrlg.h" // for wxFileIconsTable
53 #include "wx/tooltip.h"
56 #include <sys/types.h>
68 #include "wx/msw/wrapwin.h"
69 #include "wx/msw/mslu.h"
77 #if defined(__UNIX__) || defined(__DOS__)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
86 int wxCALLBACK
wxFileDataNameCompare( long data1
, long data2
, long data
)
88 wxFileData
*fd1
= (wxFileData
*)data1
;
89 wxFileData
*fd2
= (wxFileData
*)data2
;
90 if (fd1
->GetFileName() == wxT("..")) return -data
;
91 if (fd2
->GetFileName() == wxT("..")) return data
;
92 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
93 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
94 return data
*wxStrcmp( fd1
->GetFileName(), fd2
->GetFileName() );
98 int wxCALLBACK
wxFileDataSizeCompare( long data1
, long data2
, long data
)
100 wxFileData
*fd1
= (wxFileData
*)data1
;
101 wxFileData
*fd2
= (wxFileData
*)data2
;
102 if (fd1
->GetFileName() == wxT("..")) return -data
;
103 if (fd2
->GetFileName() == wxT("..")) return data
;
104 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
105 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
106 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
107 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
108 return data
*(fd1
->GetSize() - fd2
->GetSize());
112 int wxCALLBACK
wxFileDataTypeCompare( long data1
, long data2
, long data
)
114 wxFileData
*fd1
= (wxFileData
*)data1
;
115 wxFileData
*fd2
= (wxFileData
*)data2
;
116 if (fd1
->GetFileName() == wxT("..")) return -data
;
117 if (fd2
->GetFileName() == wxT("..")) return data
;
118 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
119 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
120 if (fd1
->IsLink() && !fd2
->IsLink()) return -data
;
121 if (fd2
->IsLink() && !fd1
->IsLink()) return data
;
122 return data
*wxStrcmp( fd1
->GetFileType(), fd2
->GetFileType() );
126 int wxCALLBACK
wxFileDataTimeCompare( long data1
, long data2
, long data
)
128 wxFileData
*fd1
= (wxFileData
*)data1
;
129 wxFileData
*fd2
= (wxFileData
*)data2
;
130 if (fd1
->GetFileName() == wxT("..")) return -data
;
131 if (fd2
->GetFileName() == wxT("..")) return data
;
132 if (fd1
->IsDir() && !fd2
->IsDir()) return -data
;
133 if (fd2
->IsDir() && !fd1
->IsDir()) return data
;
135 return fd1
->GetDateTime().IsLaterThan(fd2
->GetDateTime()) ? int(data
) : -int(data
);
138 #if defined(__UNIX__) && !defined(__OS2__)
139 #define IsTopMostDir(dir) (dir == wxT("/"))
142 #if defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)
143 #define IsTopMostDir(dir) (dir.empty())
146 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
147 // defined in src/generic/dirctrlg.cpp
148 extern bool wxIsDriveAvailable(const wxString
& dirName
);
151 // defined in src/generic/dirctrlg.cpp
152 extern size_t wxGetAvailableDrives(wxArrayString
&paths
, wxArrayString
&names
, wxArrayInt
&icon_ids
);
154 //-----------------------------------------------------------------------------
156 //-----------------------------------------------------------------------------
158 wxFileData::wxFileData( const wxString
&filePath
, const wxString
&fileName
, fileType type
, int image_id
)
161 m_fileName
= fileName
;
162 m_filePath
= filePath
;
169 void wxFileData::Init()
172 m_type
= wxFileData::is_file
;
173 m_image
= wxFileIconsTable::file
;
176 void wxFileData::Copy( const wxFileData
& fileData
)
178 m_fileName
= fileData
.GetFileName();
179 m_filePath
= fileData
.GetFilePath();
180 m_size
= fileData
.GetSize();
181 m_dateTime
= fileData
.GetDateTime();
182 m_permissions
= fileData
.GetPermissions();
183 m_type
= fileData
.GetType();
184 m_image
= fileData
.GetImageId();
187 void wxFileData::ReadData()
195 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
196 // c:\.. is a drive don't stat it
197 if ((m_fileName
== wxT("..")) && (m_filePath
.length() <= 5))
203 #endif // __DOS__ || __WINDOWS__
207 #if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
208 lstat( m_filePath
.fn_str(), &buff
);
209 m_type
|= S_ISLNK( buff
.st_mode
) != 0 ? is_link
: 0;
211 // only translate to file charset if we don't go by our
212 // wxStat implementation
213 #ifndef wxNEED_WX_UNISTD_H
214 wxStat( m_filePath
.fn_str() , &buff
);
216 wxStat( m_filePath
, &buff
);
220 m_type
|= (buff
.st_mode
& S_IFDIR
) != 0 ? is_dir
: 0;
221 m_type
|= (buff
.st_mode
& wxS_IXUSR
) != 0 ? is_exe
: 0;
223 // try to get a better icon
224 if (m_image
== wxFileIconsTable::file
)
226 if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
228 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
231 m_image
= wxFileIconsTable::executable
;
235 m_size
= (long)buff
.st_size
;
237 m_dateTime
= buff
.st_mtime
;
239 #if defined(__UNIX__)
240 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
241 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
242 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
243 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
244 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
245 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
246 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
247 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
248 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
249 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
250 #elif defined(__WIN32__)
251 DWORD attribs
= GetFileAttributes(m_filePath
);
252 if (attribs
!= (DWORD
)-1)
254 m_permissions
.Printf(_T("%c%c%c%c"),
255 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
256 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
257 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
258 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
263 wxString
wxFileData::GetFileType() const
271 else if (m_fileName
.Find(wxT('.'), true) != wxNOT_FOUND
)
272 return m_fileName
.AfterLast(wxT('.'));
274 return wxEmptyString
;
277 wxString
wxFileData::GetModificationTime() const
279 // want time as 01:02 so they line up nicely, no %r in WIN32
280 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
283 wxString
wxFileData::GetHint() const
285 wxString s
= m_filePath
;
295 s
+= wxString::Format( _("%ld bytes"), m_size
);
301 s
<< GetModificationTime()
309 wxString
wxFileData::GetEntry( fileListFieldType num
) const
319 if (!IsDir() && !IsLink() && !IsDrive())
320 s
.Printf(_T("%ld"), m_size
);
329 s
= GetModificationTime();
332 #if defined(__UNIX__) || defined(__WIN32__)
336 #endif // defined(__UNIX__) || defined(__WIN32__)
339 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
345 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
347 m_fileName
= fileName
;
348 m_filePath
= filePath
;
351 void wxFileData::MakeItem( wxListItem
&item
)
353 item
.m_text
= m_fileName
;
354 item
.ClearAttributes();
356 item
.SetTextColour(*wxRED
);
358 item
.SetTextColour(*wxBLUE
);
360 item
.m_image
= m_image
;
364 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
366 item
.SetTextColour(dg
);
368 item
.m_data
= (long)this;
371 //-----------------------------------------------------------------------------
373 //-----------------------------------------------------------------------------
375 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
377 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
378 EVT_LIST_DELETE_ITEM(wxID_ANY
, wxFileCtrl::OnListDeleteItem
)
379 EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY
, wxFileCtrl::OnListDeleteAllItems
)
380 EVT_LIST_END_LABEL_EDIT(wxID_ANY
, wxFileCtrl::OnListEndLabelEdit
)
381 EVT_LIST_COL_CLICK(wxID_ANY
, wxFileCtrl::OnListColClick
)
385 wxFileCtrl::wxFileCtrl()
387 m_showHidden
= false;
389 m_sort_field
= wxFileData::FileList_Name
;
392 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
394 const wxString
& wild
,
399 const wxValidator
&validator
,
400 const wxString
&name
)
401 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
404 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
406 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
408 m_showHidden
= showHidden
;
411 m_sort_field
= wxFileData::FileList_Name
;
413 m_dirName
= wxT("*");
415 if (style
& wxLC_REPORT
)
416 ChangeToReportMode();
419 void wxFileCtrl::ChangeToListMode()
422 SetSingleStyle( wxLC_LIST
);
426 void wxFileCtrl::ChangeToReportMode()
429 SetSingleStyle( wxLC_REPORT
);
431 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
432 // don't hardcode since mm/dd is dd/mm elsewhere
434 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
435 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
436 GetTextExtent(txt
, &w
, &h
);
438 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
439 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
440 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
441 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
442 #if defined(__UNIX__)
443 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
444 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
445 #elif defined(__WIN32__)
446 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
447 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
453 void wxFileCtrl::ChangeToSmallIconMode()
456 SetSingleStyle( wxLC_SMALL_ICON
);
460 void wxFileCtrl::ShowHidden( bool show
)
466 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
469 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
470 fd
->MakeItem( item
);
471 long my_style
= GetWindowStyleFlag();
472 if (my_style
& wxLC_REPORT
)
474 ret
= InsertItem( item
);
475 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
476 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
478 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
480 ret
= InsertItem( item
);
485 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
487 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
488 wxCHECK_RET(fd
, wxT("invalid filedata"));
492 SetItemText(item
, fd
->GetFileName());
493 SetItemImage(item
, fd
->GetImageId());
495 if (GetWindowStyleFlag() & wxLC_REPORT
)
497 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
498 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
502 void wxFileCtrl::UpdateFiles()
504 // don't do anything before ShowModal() call which sets m_dirName
505 if ( m_dirName
== wxT("*") )
508 wxBusyCursor bcur
; // this may take a while...
516 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
517 if ( IsTopMostDir(m_dirName
) )
519 wxArrayString names
, paths
;
521 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
523 for (n
=0; n
<count
; n
++)
525 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
526 if (Add(fd
, item
) != -1)
533 #endif // defined(__DOS__) || defined(__WINDOWS__)
536 if ( !IsTopMostDir(m_dirName
) )
538 wxString
p(wxPathOnly(m_dirName
));
539 #if defined(__UNIX__) && !defined(__OS2__)
540 if (p
.empty()) p
= wxT("/");
542 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
543 if (Add(fd
, item
) != -1)
549 wxString
dirname(m_dirName
);
550 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
551 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
552 dirname
<< wxT('\\');
553 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
556 if ( dir
.IsOpened() )
558 wxString
dirPrefix(dirname
);
559 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
560 dirPrefix
+= wxFILE_SEP_PATH
;
562 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
567 // Get the directories first (not matched against wildcards):
568 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
571 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
572 if (Add(fd
, item
) != -1)
577 cont
= dir
.GetNext(&f
);
580 // Tokenize the wildcard string, so we can handle more than 1
581 // search pattern in a wildcard.
582 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
583 while ( tokenWild
.HasMoreTokens() )
585 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
586 wxDIR_FILES
| hiddenFlag
);
589 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
590 if (Add(fd
, item
) != -1)
595 cont
= dir
.GetNext(&f
);
601 SortItems(m_sort_field
, m_sort_foward
);
604 void wxFileCtrl::SetWild( const wxString
&wild
)
606 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
613 void wxFileCtrl::MakeDir()
615 wxString
new_name( _("NewName") );
616 wxString
path( m_dirName
);
617 path
+= wxFILE_SEP_PATH
;
619 if (wxFileExists(path
))
621 // try NewName0, NewName1 etc.
624 new_name
= _("NewName");
626 num
.Printf( wxT("%d"), i
);
630 path
+= wxFILE_SEP_PATH
;
633 } while (wxFileExists(path
));
639 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
644 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
648 long id
= Add( fd
, item
);
652 SortItems(m_sort_field
, m_sort_foward
);
653 id
= FindItem( 0, (long)fd
);
661 void wxFileCtrl::GoToParentDir()
663 if (!IsTopMostDir(m_dirName
))
665 size_t len
= m_dirName
.Len();
666 if (wxEndsWithPathSeparator(m_dirName
))
667 m_dirName
.Remove( len
-1, 1 );
668 wxString
fname( wxFileNameFromPath(m_dirName
) );
669 m_dirName
= wxPathOnly( m_dirName
);
670 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
671 if (!m_dirName
.empty())
673 if (m_dirName
.Last() == wxT('.'))
674 m_dirName
= wxEmptyString
;
676 #elif defined(__UNIX__)
677 if (m_dirName
.empty())
678 m_dirName
= wxT("/");
681 long id
= FindItem( 0, fname
);
682 if (id
!= wxNOT_FOUND
)
684 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
690 void wxFileCtrl::GoToHomeDir()
692 wxString s
= wxGetUserHome( wxString() );
696 void wxFileCtrl::GoToDir( const wxString
&dir
)
698 if (!wxDirExists(dir
)) return;
702 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
706 void wxFileCtrl::FreeItemData(wxListItem
& item
)
710 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
717 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
719 FreeItemData(event
.m_item
);
722 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
& WXUNUSED(event
) )
727 void wxFileCtrl::FreeAllItemsData()
730 item
.m_mask
= wxLIST_MASK_DATA
;
732 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
733 while ( item
.m_itemId
!= -1 )
737 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
741 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
743 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
746 if ((event
.GetLabel().empty()) ||
747 (event
.GetLabel() == _(".")) ||
748 (event
.GetLabel() == _("..")) ||
749 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
751 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
757 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
758 new_name
+= wxFILE_SEP_PATH
;
759 new_name
+= event
.GetLabel();
763 if (wxFileExists(new_name
))
765 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
770 if (wxRenameFile(fd
->GetFilePath(),new_name
))
772 fd
->SetNewName( new_name
, event
.GetLabel() );
773 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
774 UpdateItem( event
.GetItem() );
775 EnsureVisible( event
.GetItem() );
779 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
785 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
787 int col
= event
.GetColumn();
791 case wxFileData::FileList_Name
:
792 case wxFileData::FileList_Size
:
793 case wxFileData::FileList_Type
:
794 case wxFileData::FileList_Time
: break;
798 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
799 m_sort_foward
= !m_sort_foward
;
801 m_sort_field
= (wxFileData::fileListFieldType
)col
;
803 SortItems(m_sort_field
, m_sort_foward
);
806 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
808 m_sort_field
= field
;
809 m_sort_foward
= foward
;
810 long sort_dir
= foward
? 1 : -1;
812 switch (m_sort_field
)
814 case wxFileData::FileList_Name
:
816 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
819 case wxFileData::FileList_Size
:
821 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
824 case wxFileData::FileList_Type
:
826 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
829 case wxFileData::FileList_Time
:
831 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
838 wxFileCtrl::~wxFileCtrl()
840 // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and
841 // wxFileCtrl::OnListDeleteAllItems. But if the event is generated after
842 // the destruction of the wxFileCtrl we need to free any data here:
846 //-----------------------------------------------------------------------------
847 // wxGenericFileDialog
848 //-----------------------------------------------------------------------------
850 #define ID_LIST_MODE (wxID_FILEDLGG )
851 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
852 #define ID_UP_DIR (wxID_FILEDLGG + 5)
853 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
854 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
855 #define ID_CHOICE (wxID_FILEDLGG + 8)
856 #define ID_TEXT (wxID_FILEDLGG + 9)
857 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
858 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
859 #define ID_CHECK (wxID_FILEDLGG + 12)
861 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
863 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
864 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
865 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
866 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
867 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
868 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
869 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
870 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
871 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
872 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
873 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
874 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
875 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
878 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
879 bool wxGenericFileDialog::ms_lastShowHidden
= false;
881 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
882 const wxString
& message
,
883 const wxString
& defaultDir
,
884 const wxString
& defaultFile
,
885 const wxString
& wildCard
,
888 bool bypassGenericImpl
)
889 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
)
891 m_bypassGenericImpl
= bypassGenericImpl
;
893 if (!m_bypassGenericImpl
)
894 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
);
897 bool wxGenericFileDialog::Create( wxWindow
*parent
,
898 const wxString
& message
,
899 const wxString
& WXUNUSED(defaultDir
),
900 const wxString
& defaultFile
,
901 const wxString
& wildCard
,
902 long WXUNUSED(style
),
905 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
906 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
))
911 if (wxConfig::Get(false))
913 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
915 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
919 if (m_dialogStyle
== 0)
920 m_dialogStyle
= wxOPEN
;
921 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
922 m_dialogStyle
|= wxOPEN
;
924 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
929 size_t len
= m_dir
.Len();
930 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
931 m_dir
.Remove( len
-1, 1 );
934 m_path
+= wxFILE_SEP_PATH
;
935 m_path
+= defaultFile
;
936 m_filterExtension
= wxEmptyString
;
940 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
942 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
944 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
948 but
= new wxBitmapButton(this, ID_LIST_MODE
,
949 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
951 but
->SetToolTip( _("View files as a list view") );
953 buttonsizer
->Add( but
, 0, wxALL
, 5 );
955 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
956 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
958 but
->SetToolTip( _("View files as a detailed view") );
960 buttonsizer
->Add( but
, 0, wxALL
, 5 );
962 buttonsizer
->Add( 30, 5, 1 );
964 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
965 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
967 m_upDirButton
->SetToolTip( _("Go to parent directory") );
969 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
971 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
972 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
973 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
975 but
->SetToolTip( _("Go to home directory") );
977 buttonsizer
->Add( but
, 0, wxALL
, 5);
979 buttonsizer
->Add( 20, 20 );
982 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
983 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
985 m_newDirButton
->SetToolTip( _("Create new directory") );
987 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
990 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
992 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
994 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
996 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
997 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
998 staticsizer
->Add( m_static
, 1 );
999 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1001 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
1002 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1003 style2
|= wxLC_SINGLE_SEL
;
1005 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1006 wxEmptyString
, ms_lastShowHidden
,
1007 wxDefaultPosition
, wxSize(540,200),
1012 // PDAs have a different screen layout
1013 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1015 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1016 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1017 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1018 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1021 m_choice
= new wxChoice( this, ID_CHOICE
);
1022 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1024 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1025 buttonsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxALL
, 5 );
1026 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 5 );
1027 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1031 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1033 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1034 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1035 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1036 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1037 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1039 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1040 m_choice
= new wxChoice( this, ID_CHOICE
);
1041 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1042 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1043 m_check
->SetValue( ms_lastShowHidden
);
1044 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1045 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1046 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1049 SetWildcard(wildCard
);
1051 SetAutoLayout( true );
1052 SetSizer( mainsizer
);
1054 mainsizer
->Fit( this );
1055 mainsizer
->SetSizeHints( this );
1064 wxGenericFileDialog::~wxGenericFileDialog()
1066 if (!m_bypassGenericImpl
)
1068 if (wxConfig::Get(false))
1070 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1072 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1076 const int count
= m_choice
->GetCount();
1077 for ( int i
= 0; i
< count
; i
++ )
1079 delete (wxString
*)m_choice
->GetClientData(i
);
1084 int wxGenericFileDialog::ShowModal()
1086 m_list
->GoToDir(m_dir
);
1088 m_text
->SetValue(m_fileName
);
1090 return wxDialog::ShowModal();
1093 bool wxGenericFileDialog::Show( bool show
)
1097 m_list
->GoToDir(m_dir
);
1099 m_text
->SetValue(m_fileName
);
1102 return wxDialog::Show( show
);
1105 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1107 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1108 m_list
->SetWild( *str
);
1109 m_filterIndex
= filterindex
;
1110 if ( str
->Left(2) == wxT("*.") )
1112 m_filterExtension
= str
->Mid(1);
1113 if (m_filterExtension
== _T(".*"))
1114 m_filterExtension
.clear();
1118 m_filterExtension
.clear();
1122 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1124 wxFileDialogBase::SetWildcard(wildCard
);
1126 wxArrayString wildDescriptions
, wildFilters
;
1127 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1130 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1133 for ( size_t n
= 0; n
< count
; n
++ )
1135 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1138 SetFilterIndex( 0 );
1141 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1143 m_choice
->SetSelection( filterindex
);
1145 DoSetFilterIndex(filterindex
);
1148 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1150 DoSetFilterIndex((int)event
.GetInt());
1153 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1155 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1158 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1160 HandleAction( event
.m_item
.m_text
);
1163 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1165 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1166 cevent
.SetEventObject( this );
1167 GetEventHandler()->ProcessEvent( cevent
);
1170 static bool ignoreChanges
= false;
1172 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1176 // Clear selections. Otherwise when the user types in a value they may
1177 // not get the file whose name they typed.
1178 if (m_list
->GetSelectedItemCount() > 0)
1180 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1181 wxLIST_STATE_SELECTED
);
1182 while ( item
!= -1 )
1184 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1185 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1191 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1193 wxString
filename( event
.m_item
.m_text
);
1194 if (filename
== wxT("..")) return;
1196 wxString dir
= m_list
->GetDir();
1197 if (!IsTopMostDir(dir
))
1198 dir
+= wxFILE_SEP_PATH
;
1200 if (wxDirExists(dir
)) return;
1202 ignoreChanges
= true;
1203 m_text
->SetValue( filename
);
1204 ignoreChanges
= false;
1207 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1209 wxString
filename( fn
);
1210 wxString dir
= m_list
->GetDir();
1211 if (filename
.empty()) return;
1212 if (filename
== wxT(".")) return;
1214 // "some/place/" means they want to chdir not try to load "place"
1215 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1217 filename
= filename
.RemoveLast();
1219 if (filename
== wxT(".."))
1221 m_list
->GoToParentDir();
1228 if (filename
== wxT("~"))
1230 m_list
->GoToHomeDir();
1236 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1238 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1242 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1243 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1245 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1247 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1250 m_list
->SetWild( filename
);
1254 if (!IsTopMostDir(dir
))
1255 dir
+= wxFILE_SEP_PATH
;
1256 if (!wxIsAbsolutePath(filename
))
1262 if (wxDirExists(filename
))
1264 m_list
->GoToDir( filename
);
1269 // they really wanted a dir, but it doesn't exist
1272 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1273 wxOK
| wxICON_ERROR
);
1277 // append the default extension to the filename if it doesn't have any
1279 // VZ: the logic of testing for !wxFileExists() only for the open file
1280 // dialog is not entirely clear to me, why don't we allow saving to a
1281 // file without extension as well?
1282 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1284 filename
= AppendExtension(filename
, m_filterExtension
);
1287 // check that the file [doesn't] exist if necessary
1288 if ( (m_dialogStyle
& wxSAVE
) &&
1289 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1290 wxFileExists( filename
) )
1293 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1295 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1298 else if ( (m_dialogStyle
& wxOPEN
) &&
1299 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1300 !wxFileExists(filename
) )
1302 wxMessageBox(_("Please choose an existing file."), _("Error"),
1303 wxOK
| wxICON_ERROR
);
1306 SetPath( filename
);
1308 // change to the directory where the user went if asked
1309 if ( m_dialogStyle
& wxCHANGE_DIR
)
1312 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1314 if ( cwd
!= wxGetCwd() )
1316 wxSetWorkingDirectory(cwd
);
1320 wxCommandEvent event
;
1321 wxDialog::OnOK(event
);
1324 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1326 HandleAction( m_text
->GetValue() );
1329 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1331 m_list
->ChangeToListMode();
1332 ms_lastViewStyle
= wxLC_LIST
;
1336 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1338 m_list
->ChangeToReportMode();
1339 ms_lastViewStyle
= wxLC_REPORT
;
1343 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1345 m_list
->GoToParentDir();
1350 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1352 m_list
->GoToHomeDir();
1357 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1362 void wxGenericFileDialog::SetPath( const wxString
& path
)
1364 // not only set the full path but also update filename and dir
1366 if ( !path
.empty() )
1369 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1372 m_fileName
+= wxT(".");
1378 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1381 if (m_list
->GetSelectedItemCount() == 0)
1383 paths
.Add( GetPath() );
1387 paths
.Alloc( m_list
->GetSelectedItemCount() );
1389 wxString dir
= m_list
->GetDir();
1391 if (dir
!= wxT("/"))
1393 dir
+= wxFILE_SEP_PATH
;
1396 item
.m_mask
= wxLIST_MASK_TEXT
;
1398 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1399 while ( item
.m_itemId
!= -1 )
1401 m_list
->GetItem( item
);
1402 paths
.Add( dir
+ item
.m_text
);
1403 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1404 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1408 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1411 if (m_list
->GetSelectedItemCount() == 0)
1413 files
.Add( GetFilename() );
1416 files
.Alloc( m_list
->GetSelectedItemCount() );
1419 item
.m_mask
= wxLIST_MASK_TEXT
;
1421 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1422 while ( item
.m_itemId
!= -1 )
1424 m_list
->GetItem( item
);
1425 files
.Add( item
.m_text
);
1426 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1427 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1431 void wxGenericFileDialog::UpdateControls()
1433 wxString dir
= m_list
->GetDir();
1434 m_static
->SetLabel(dir
);
1436 bool enable
= !IsTopMostDir(dir
);
1437 m_upDirButton
->Enable(enable
);
1439 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1440 m_newDirButton
->Enable(enable
);
1441 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1444 #ifdef USE_GENERIC_FILEDIALOG
1446 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1448 #endif // USE_GENERIC_FILEDIALOG
1450 #endif // wxUSE_FILEDLG