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__) && !defined(__CYGWIN__)
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 void wxGenericFileDialog::Init()
883 m_bypassGenericImpl
= false;
890 m_upDirButton
= NULL
;
891 m_newDirButton
= NULL
;
894 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
895 const wxString
& message
,
896 const wxString
& defaultDir
,
897 const wxString
& defaultFile
,
898 const wxString
& wildCard
,
901 bool bypassGenericImpl
) : wxFileDialogBase()
904 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, bypassGenericImpl
);
907 bool wxGenericFileDialog::Create( wxWindow
*parent
,
908 const wxString
& message
,
909 const wxString
& defaultDir
,
910 const wxString
& defaultFile
,
911 const wxString
& wildCard
,
914 bool bypassGenericImpl
)
916 m_bypassGenericImpl
= bypassGenericImpl
;
918 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
919 wildCard
, style
, pos
))
924 if (m_bypassGenericImpl
)
927 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, wxDefaultSize
,
928 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
))
933 if (wxConfig::Get(false))
935 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
937 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
941 if (m_dialogStyle
== 0)
942 m_dialogStyle
= wxOPEN
;
943 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
944 m_dialogStyle
|= wxOPEN
;
946 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
951 size_t len
= m_dir
.Len();
952 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
953 m_dir
.Remove( len
-1, 1 );
956 m_path
+= wxFILE_SEP_PATH
;
957 m_path
+= defaultFile
;
958 m_filterExtension
= wxEmptyString
;
962 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
964 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
966 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
970 but
= new wxBitmapButton(this, ID_LIST_MODE
,
971 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
973 but
->SetToolTip( _("View files as a list view") );
975 buttonsizer
->Add( but
, 0, wxALL
, 5 );
977 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
978 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
980 but
->SetToolTip( _("View files as a detailed view") );
982 buttonsizer
->Add( but
, 0, wxALL
, 5 );
984 buttonsizer
->Add( 30, 5, 1 );
986 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
987 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
989 m_upDirButton
->SetToolTip( _("Go to parent directory") );
991 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
993 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
994 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
995 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
997 but
->SetToolTip( _("Go to home directory") );
999 buttonsizer
->Add( but
, 0, wxALL
, 5);
1001 buttonsizer
->Add( 20, 20 );
1004 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
1005 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
1007 m_newDirButton
->SetToolTip( _("Create new directory") );
1009 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
1012 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1014 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1016 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1018 staticsizer
->Add( new wxStaticText( this, wxID_ANY
, _("Current directory:") ), 0, wxRIGHT
, 10 );
1019 m_static
= new wxStaticText( this, wxID_ANY
, m_dir
);
1020 staticsizer
->Add( m_static
, 1 );
1021 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1023 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
1024 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1025 style2
|= wxLC_SINGLE_SEL
;
1027 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
1028 wxEmptyString
, ms_lastShowHidden
,
1029 wxDefaultPosition
, wxSize(540,200),
1034 // PDAs have a different screen layout
1035 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1037 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1038 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1039 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1040 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1043 m_choice
= new wxChoice( this, ID_CHOICE
);
1044 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1046 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1047 buttonsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxALL
, 5 );
1048 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 5 );
1049 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1053 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1055 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1056 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1057 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1058 textsizer
->Add( new wxButton( this, wxID_OK
), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1059 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1061 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1062 m_choice
= new wxChoice( this, ID_CHOICE
);
1063 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1064 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1065 m_check
->SetValue( ms_lastShowHidden
);
1066 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1067 choicesizer
->Add( new wxButton( this, wxID_CANCEL
), 0, wxCENTER
| wxALL
, 10 );
1068 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1071 SetWildcard(wildCard
);
1073 SetAutoLayout( true );
1074 SetSizer( mainsizer
);
1076 mainsizer
->Fit( this );
1077 mainsizer
->SetSizeHints( this );
1086 wxGenericFileDialog::~wxGenericFileDialog()
1088 if (!m_bypassGenericImpl
)
1090 if (wxConfig::Get(false))
1092 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1094 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1098 const int count
= m_choice
->GetCount();
1099 for ( int i
= 0; i
< count
; i
++ )
1101 delete (wxString
*)m_choice
->GetClientData(i
);
1106 int wxGenericFileDialog::ShowModal()
1108 m_list
->GoToDir(m_dir
);
1110 m_text
->SetValue(m_fileName
);
1112 return wxDialog::ShowModal();
1115 bool wxGenericFileDialog::Show( bool show
)
1119 m_list
->GoToDir(m_dir
);
1121 m_text
->SetValue(m_fileName
);
1124 return wxDialog::Show( show
);
1127 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1129 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1130 m_list
->SetWild( *str
);
1131 m_filterIndex
= filterindex
;
1132 if ( str
->Left(2) == wxT("*.") )
1134 m_filterExtension
= str
->Mid(1);
1135 if (m_filterExtension
== _T(".*"))
1136 m_filterExtension
.clear();
1140 m_filterExtension
.clear();
1144 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
1146 wxFileDialogBase::SetWildcard(wildCard
);
1148 wxArrayString wildDescriptions
, wildFilters
;
1149 const size_t count
= wxParseCommonDialogsFilter(m_wildCard
,
1152 wxCHECK_RET( count
, wxT("wxFileDialog: bad wildcard string") );
1154 const size_t countOld
= m_choice
->GetCount();
1156 for ( n
= 0; n
< countOld
; n
++ )
1158 delete (wxString
*)m_choice
->GetClientData(n
);
1161 for ( n
= 0; n
< count
; n
++ )
1163 m_choice
->Append( wildDescriptions
[n
], new wxString( wildFilters
[n
] ) );
1166 SetFilterIndex( 0 );
1169 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1171 m_choice
->SetSelection( filterindex
);
1173 DoSetFilterIndex(filterindex
);
1176 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1178 DoSetFilterIndex((int)event
.GetInt());
1181 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1183 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1186 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1188 HandleAction( event
.m_item
.m_text
);
1191 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1193 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1194 cevent
.SetEventObject( this );
1195 GetEventHandler()->ProcessEvent( cevent
);
1198 static bool ignoreChanges
= false;
1200 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1204 // Clear selections. Otherwise when the user types in a value they may
1205 // not get the file whose name they typed.
1206 if (m_list
->GetSelectedItemCount() > 0)
1208 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1209 wxLIST_STATE_SELECTED
);
1210 while ( item
!= -1 )
1212 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1213 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1219 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1221 wxString
filename( event
.m_item
.m_text
);
1222 if (filename
== wxT("..")) return;
1224 wxString dir
= m_list
->GetDir();
1225 if (!IsTopMostDir(dir
))
1226 dir
+= wxFILE_SEP_PATH
;
1228 if (wxDirExists(dir
)) return;
1230 ignoreChanges
= true;
1231 m_text
->SetValue( filename
);
1232 ignoreChanges
= false;
1235 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1237 wxString
filename( fn
);
1238 wxString dir
= m_list
->GetDir();
1239 if (filename
.empty()) return;
1240 if (filename
== wxT(".")) return;
1242 // "some/place/" means they want to chdir not try to load "place"
1243 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1245 filename
= filename
.RemoveLast();
1247 if (filename
== wxT(".."))
1249 m_list
->GoToParentDir();
1256 if (filename
== wxT("~"))
1258 m_list
->GoToHomeDir();
1264 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1266 filename
= wxString(wxGetUserHome()) + filename
.Remove(0, 1);
1270 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1271 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1273 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1275 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1278 m_list
->SetWild( filename
);
1282 if (!IsTopMostDir(dir
))
1283 dir
+= wxFILE_SEP_PATH
;
1284 if (!wxIsAbsolutePath(filename
))
1290 if (wxDirExists(filename
))
1292 m_list
->GoToDir( filename
);
1297 // they really wanted a dir, but it doesn't exist
1300 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1301 wxOK
| wxICON_ERROR
);
1305 // append the default extension to the filename if it doesn't have any
1307 // VZ: the logic of testing for !wxFileExists() only for the open file
1308 // dialog is not entirely clear to me, why don't we allow saving to a
1309 // file without extension as well?
1310 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1312 filename
= AppendExtension(filename
, m_filterExtension
);
1315 // check that the file [doesn't] exist if necessary
1316 if ( (m_dialogStyle
& wxSAVE
) &&
1317 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1318 wxFileExists( filename
) )
1321 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1323 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1326 else if ( (m_dialogStyle
& wxOPEN
) &&
1327 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1328 !wxFileExists(filename
) )
1330 wxMessageBox(_("Please choose an existing file."), _("Error"),
1331 wxOK
| wxICON_ERROR
);
1334 SetPath( filename
);
1336 // change to the directory where the user went if asked
1337 if ( m_dialogStyle
& wxCHANGE_DIR
)
1340 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1342 if ( cwd
!= wxGetCwd() )
1344 wxSetWorkingDirectory(cwd
);
1348 wxCommandEvent event
;
1349 wxDialog::OnOK(event
);
1352 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1354 HandleAction( m_text
->GetValue() );
1357 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1359 m_list
->ChangeToListMode();
1360 ms_lastViewStyle
= wxLC_LIST
;
1364 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1366 m_list
->ChangeToReportMode();
1367 ms_lastViewStyle
= wxLC_REPORT
;
1371 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1373 m_list
->GoToParentDir();
1378 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1380 m_list
->GoToHomeDir();
1385 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1390 void wxGenericFileDialog::SetPath( const wxString
& path
)
1392 // not only set the full path but also update filename and dir
1394 if ( !path
.empty() )
1397 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1400 m_fileName
+= wxT(".");
1406 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1409 if (m_list
->GetSelectedItemCount() == 0)
1411 paths
.Add( GetPath() );
1415 paths
.Alloc( m_list
->GetSelectedItemCount() );
1417 wxString dir
= m_list
->GetDir();
1419 if (dir
!= wxT("/"))
1421 dir
+= wxFILE_SEP_PATH
;
1424 item
.m_mask
= wxLIST_MASK_TEXT
;
1426 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1427 while ( item
.m_itemId
!= -1 )
1429 m_list
->GetItem( item
);
1430 paths
.Add( dir
+ item
.m_text
);
1431 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1432 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1436 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1439 if (m_list
->GetSelectedItemCount() == 0)
1441 files
.Add( GetFilename() );
1444 files
.Alloc( m_list
->GetSelectedItemCount() );
1447 item
.m_mask
= wxLIST_MASK_TEXT
;
1449 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1450 while ( item
.m_itemId
!= -1 )
1452 m_list
->GetItem( item
);
1453 files
.Add( item
.m_text
);
1454 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1455 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1459 void wxGenericFileDialog::UpdateControls()
1461 wxString dir
= m_list
->GetDir();
1462 m_static
->SetLabel(dir
);
1464 bool enable
= !IsTopMostDir(dir
);
1465 m_upDirButton
->Enable(enable
);
1467 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1468 m_newDirButton
->Enable(enable
);
1469 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1472 #ifdef USE_GENERIC_FILEDIALOG
1474 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1476 #endif // USE_GENERIC_FILEDIALOG
1478 #endif // wxUSE_FILEDLG