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
)
211 if (m_fileName
.Find(wxT('.'), TRUE
) != wxNOT_FOUND
)
213 m_image
= wxTheFileIconsTable
->GetIconID( m_fileName
.AfterLast(wxT('.')));
216 m_image
= wxFileIconsTable::executable
;
220 m_size
= buff
.st_size
;
222 m_dateTime
= buff
.st_mtime
;
224 #if defined(__UNIX__)
225 m_permissions
.Printf(_T("%c%c%c%c%c%c%c%c%c"),
226 buff
.st_mode
& wxS_IRUSR
? _T('r') : _T('-'),
227 buff
.st_mode
& wxS_IWUSR
? _T('w') : _T('-'),
228 buff
.st_mode
& wxS_IXUSR
? _T('x') : _T('-'),
229 buff
.st_mode
& wxS_IRGRP
? _T('r') : _T('-'),
230 buff
.st_mode
& wxS_IWGRP
? _T('w') : _T('-'),
231 buff
.st_mode
& wxS_IXGRP
? _T('x') : _T('-'),
232 buff
.st_mode
& wxS_IROTH
? _T('r') : _T('-'),
233 buff
.st_mode
& wxS_IWOTH
? _T('w') : _T('-'),
234 buff
.st_mode
& wxS_IXOTH
? _T('x') : _T('-'));
235 #elif defined(__WIN32__)
236 DWORD attribs
= GetFileAttributes(m_filePath
);
237 if (attribs
!= (DWORD
)-1)
239 m_permissions
.Printf(_T("%c%c%c%c"),
240 attribs
& FILE_ATTRIBUTE_ARCHIVE
? _T('A') : _T(' '),
241 attribs
& FILE_ATTRIBUTE_READONLY
? _T('R') : _T(' '),
242 attribs
& FILE_ATTRIBUTE_HIDDEN
? _T('H') : _T(' '),
243 attribs
& FILE_ATTRIBUTE_SYSTEM
? _T('S') : _T(' '));
248 wxString
wxFileData::GetFileType() const
256 else if (m_fileName
.Find(wxT('.'), TRUE
) != wxNOT_FOUND
)
257 return m_fileName
.AfterLast(wxT('.'));
259 return wxEmptyString
;
262 wxString
wxFileData::GetModificationTime() const
264 // want time as 01:02 so they line up nicely, no %r in WIN32
265 return m_dateTime
.FormatDate() + wxT(" ") + m_dateTime
.Format(wxT("%I:%M:%S %p"));
268 wxString
wxFileData::GetHint() const
270 wxString s
= m_filePath
;
280 s
+= wxString::Format( _("%ld bytes"), m_size
);
286 s
<< GetModificationTime()
294 wxString
wxFileData::GetEntry( fileListFieldType num
) const
304 if (!IsDir() && !IsLink() && !IsDrive())
305 s
.Printf(_T("%ld"), m_size
);
314 s
= GetModificationTime();
317 #if defined(__UNIX__) || defined(__WIN32__)
321 #endif // defined(__UNIX__) || defined(__WIN32__)
324 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
330 void wxFileData::SetNewName( const wxString
&filePath
, const wxString
&fileName
)
332 m_fileName
= fileName
;
333 m_filePath
= filePath
;
336 void wxFileData::MakeItem( wxListItem
&item
)
338 item
.m_text
= m_fileName
;
339 item
.ClearAttributes();
341 item
.SetTextColour(*wxRED
);
343 item
.SetTextColour(*wxBLUE
);
345 item
.m_image
= m_image
;
349 wxColour dg
= wxTheColourDatabase
->Find( _T("MEDIUM GREY") );
351 item
.SetTextColour(dg
);
353 item
.m_data
= (long)this;
356 //-----------------------------------------------------------------------------
358 //-----------------------------------------------------------------------------
360 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
362 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
363 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
364 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
365 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
366 EVT_LIST_COL_CLICK(-1, wxFileCtrl::OnListColClick
)
370 wxFileCtrl::wxFileCtrl()
372 m_showHidden
= FALSE
;
374 m_sort_field
= wxFileData::FileList_Name
;
377 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
379 const wxString
& wild
,
384 const wxValidator
&validator
,
385 const wxString
&name
)
386 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
389 wxImageList
*imageList
= wxTheFileIconsTable
->GetSmallImageList();
391 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
393 m_showHidden
= showHidden
;
396 m_sort_field
= wxFileData::FileList_Name
;
398 m_dirName
= wxT("*");
400 if (style
& wxLC_REPORT
)
401 ChangeToReportMode();
404 void wxFileCtrl::ChangeToListMode()
407 SetSingleStyle( wxLC_LIST
);
411 void wxFileCtrl::ChangeToReportMode()
414 SetSingleStyle( wxLC_REPORT
);
416 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
417 // don't hardcode since mm/dd is dd/mm elsewhere
419 wxDateTime
dt(22, wxDateTime::Dec
, 2002, 22, 22, 22);
420 wxString txt
= dt
.FormatDate() + wxT("22") + dt
.Format(wxT("%I:%M:%S %p"));
421 GetTextExtent(txt
, &w
, &h
);
423 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, w
);
424 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, w
/2 );
425 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT
, w
/2 );
426 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT
, w
);
427 #if defined(__UNIX__)
428 GetTextExtent(wxT("Permissions 2"), &w
, &h
);
429 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, w
);
430 #elif defined(__WIN32__)
431 GetTextExtent(wxT("Attributes 2"), &w
, &h
);
432 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT
, w
);
438 void wxFileCtrl::ChangeToSmallIconMode()
441 SetSingleStyle( wxLC_SMALL_ICON
);
445 void wxFileCtrl::ShowHidden( bool show
)
451 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
454 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
455 fd
->MakeItem( item
);
456 long my_style
= GetWindowStyleFlag();
457 if (my_style
& wxLC_REPORT
)
459 ret
= InsertItem( item
);
460 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
461 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
463 else if ((my_style
& wxLC_LIST
) || (my_style
& wxLC_SMALL_ICON
))
465 ret
= InsertItem( item
);
470 void wxFileCtrl::UpdateItem(const wxListItem
&item
)
472 wxFileData
*fd
= (wxFileData
*)GetItemData(item
);
473 wxCHECK_RET(fd
, wxT("invalid filedata"));
477 SetItemText(item
, fd
->GetFileName());
478 SetItemImage(item
, fd
->GetImageId(), fd
->GetImageId());
480 if (GetWindowStyleFlag() & wxLC_REPORT
)
482 for (int i
= 1; i
< wxFileData::FileList_Max
; i
++)
483 SetItem( item
.m_itemId
, i
, fd
->GetEntry((wxFileData::fileListFieldType
)i
) );
487 void wxFileCtrl::UpdateFiles()
489 // don't do anything before ShowModal() call which sets m_dirName
490 if ( m_dirName
== wxT("*") )
493 wxBusyCursor bcur
; // this may take a while...
501 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
502 if ( IsTopMostDir(m_dirName
) )
504 wxArrayString names
, paths
;
506 size_t n
, count
= wxGetAvailableDrives(paths
, names
, icons
);
508 for (n
=0; n
<count
; n
++)
510 wxFileData
*fd
= new wxFileData(paths
[n
], names
[n
], wxFileData::is_drive
, icons
[n
]);
511 if (Add(fd
, item
) != -1)
518 #endif // defined(__DOS__) || defined(__WINDOWS__)
521 if ( !IsTopMostDir(m_dirName
) )
523 wxString
p(wxPathOnly(m_dirName
));
524 #if defined(__UNIX__) && !defined(__OS2__)
525 if (p
.IsEmpty()) p
= wxT("/");
527 wxFileData
*fd
= new wxFileData(p
, wxT(".."), wxFileData::is_dir
, wxFileIconsTable::folder
);
528 if (Add(fd
, item
) != -1)
534 wxString
dirname(m_dirName
);
535 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
536 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
537 dirname
<< wxT('\\');
538 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
541 if ( dir
.IsOpened() )
543 wxString
dirPrefix(dirname
);
544 if (dirPrefix
.Last() != wxFILE_SEP_PATH
)
545 dirPrefix
+= wxFILE_SEP_PATH
;
547 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
552 // Get the directories first (not matched against wildcards):
553 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
556 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_dir
, wxFileIconsTable::folder
);
557 if (Add(fd
, item
) != -1)
562 cont
= dir
.GetNext(&f
);
565 // Tokenize the wildcard string, so we can handle more than 1
566 // search pattern in a wildcard.
567 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
568 while ( tokenWild
.HasMoreTokens() )
570 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
571 wxDIR_FILES
| hiddenFlag
);
574 wxFileData
*fd
= new wxFileData(dirPrefix
+ f
, f
, wxFileData::is_file
, wxFileIconsTable::file
);
575 if (Add(fd
, item
) != -1)
580 cont
= dir
.GetNext(&f
);
586 SortItems(m_sort_field
, m_sort_foward
);
589 void wxFileCtrl::SetWild( const wxString
&wild
)
591 if (wild
.Find(wxT('|')) != wxNOT_FOUND
)
598 void wxFileCtrl::MakeDir()
600 wxString
new_name( _("NewName") );
601 wxString
path( m_dirName
);
602 path
+= wxFILE_SEP_PATH
;
604 if (wxFileExists(path
))
606 // try NewName0, NewName1 etc.
609 new_name
= _("NewName");
611 num
.Printf( wxT("%d"), i
);
615 path
+= wxFILE_SEP_PATH
;
618 } while (wxFileExists(path
));
624 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
629 wxFileData
*fd
= new wxFileData( path
, new_name
, wxFileData::is_dir
, wxFileIconsTable::folder
);
633 long id
= Add( fd
, item
);
637 SortItems(m_sort_field
, m_sort_foward
);
638 id
= FindItem( 0, (long)fd
);
646 void wxFileCtrl::GoToParentDir()
648 if (!IsTopMostDir(m_dirName
))
650 size_t len
= m_dirName
.Len();
651 if (wxEndsWithPathSeparator(m_dirName
))
652 m_dirName
.Remove( len
-1, 1 );
653 wxString
fname( wxFileNameFromPath(m_dirName
) );
654 m_dirName
= wxPathOnly( m_dirName
);
655 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
656 if (!m_dirName
.IsEmpty())
658 if (m_dirName
.Last() == wxT('.'))
661 #elif defined(__UNIX__)
662 if (m_dirName
.IsEmpty())
663 m_dirName
= wxT("/");
666 long id
= FindItem( 0, fname
);
669 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
675 void wxFileCtrl::GoToHomeDir()
677 wxString s
= wxGetUserHome( wxString() );
681 void wxFileCtrl::GoToDir( const wxString
&dir
)
683 if (!wxDirExists(dir
)) return;
687 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
691 void wxFileCtrl::FreeItemData(wxListItem
& item
)
695 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
702 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
704 FreeItemData(event
.m_item
);
707 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&event
)
712 void wxFileCtrl::FreeAllItemsData()
715 item
.m_mask
= wxLIST_MASK_DATA
;
717 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
718 while ( item
.m_itemId
!= -1 )
722 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
726 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
728 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
731 if ((event
.GetLabel().IsEmpty()) ||
732 (event
.GetLabel() == _(".")) ||
733 (event
.GetLabel() == _("..")) ||
734 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
736 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
742 wxString
new_name( wxPathOnly( fd
->GetFilePath() ) );
743 new_name
+= wxFILE_SEP_PATH
;
744 new_name
+= event
.GetLabel();
748 if (wxFileExists(new_name
))
750 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
755 if (wxRenameFile(fd
->GetFilePath(),new_name
))
757 fd
->SetNewName( new_name
, event
.GetLabel() );
758 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
759 UpdateItem( event
.GetItem() );
760 EnsureVisible( event
.GetItem() );
764 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
770 void wxFileCtrl::OnListColClick( wxListEvent
&event
)
772 int col
= event
.GetColumn();
776 case wxFileData::FileList_Name
:
777 case wxFileData::FileList_Size
:
778 case wxFileData::FileList_Type
:
779 case wxFileData::FileList_Time
: break;
783 if ((wxFileData::fileListFieldType
)col
== m_sort_field
)
784 m_sort_foward
= !m_sort_foward
;
786 m_sort_field
= (wxFileData::fileListFieldType
)col
;
788 SortItems(m_sort_field
, m_sort_foward
);
791 void wxFileCtrl::SortItems(wxFileData::fileListFieldType field
, bool foward
)
793 m_sort_field
= field
;
794 m_sort_foward
= foward
;
795 long sort_dir
= foward
? 1 : -1;
797 switch (m_sort_field
)
799 case wxFileData::FileList_Name
:
801 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataNameCompare
, sort_dir
);
804 case wxFileData::FileList_Size
:
806 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataSizeCompare
, sort_dir
);
809 case wxFileData::FileList_Type
:
811 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTypeCompare
, sort_dir
);
814 case wxFileData::FileList_Time
:
816 wxListCtrl::SortItems((wxListCtrlCompare
)wxFileDataTimeCompare
, sort_dir
);
823 wxFileCtrl::~wxFileCtrl()
827 //-----------------------------------------------------------------------------
828 // wxGenericFileDialog
829 //-----------------------------------------------------------------------------
831 #define ID_LIST_MODE (wxID_FILEDLGG )
832 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
833 #define ID_UP_DIR (wxID_FILEDLGG + 5)
834 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
835 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
836 #define ID_CHOICE (wxID_FILEDLGG + 8)
837 #define ID_TEXT (wxID_FILEDLGG + 9)
838 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
839 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
840 #define ID_CHECK (wxID_FILEDLGG + 12)
842 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
844 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
845 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
846 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
847 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
848 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
849 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
850 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnListOk
)
851 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxGenericFileDialog::OnSelected
)
852 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxGenericFileDialog::OnActivated
)
853 EVT_CHOICE(ID_CHOICE
,wxGenericFileDialog::OnChoiceFilter
)
854 EVT_TEXT_ENTER(ID_TEXT
,wxGenericFileDialog::OnTextEnter
)
855 EVT_TEXT(ID_TEXT
,wxGenericFileDialog::OnTextChange
)
856 EVT_CHECKBOX(ID_CHECK
,wxGenericFileDialog::OnCheck
)
859 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
860 bool wxGenericFileDialog::ms_lastShowHidden
= FALSE
;
862 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
863 const wxString
& message
,
864 const wxString
& defaultDir
,
865 const wxString
& defaultFile
,
866 const wxString
& wildCard
,
869 :wxFileDialogBase(parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
)
871 wxDialog::Create( parent
, -1, message
, pos
, wxDefaultSize
,
872 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
874 if (wxConfig::Get(FALSE
))
876 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
878 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
882 if (m_dialogStyle
== 0)
883 m_dialogStyle
= wxOPEN
;
884 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
885 m_dialogStyle
|= wxOPEN
;
887 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
892 size_t len
= m_dir
.Len();
893 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
894 m_dir
.Remove( len
-1, 1 );
897 m_path
+= wxFILE_SEP_PATH
;
898 m_path
+= defaultFile
;
899 m_filterExtension
= wxEmptyString
;
901 // interpret wildcards
902 wxArrayString wildDescriptions
, wildFilters
;
903 if ( !ParseWildcard(m_wildCard
, wildDescriptions
, wildFilters
) )
905 wxFAIL_MSG( wxT("Wrong file type description") );
910 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
912 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
914 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
918 but
= new wxBitmapButton(this, ID_LIST_MODE
,
919 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_CMN_DIALOG
));
921 but
->SetToolTip( _("View files as a list view") );
923 buttonsizer
->Add( but
, 0, wxALL
, 5 );
925 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
926 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_CMN_DIALOG
));
928 but
->SetToolTip( _("View files as a detailed view") );
930 buttonsizer
->Add( but
, 0, wxALL
, 5 );
932 buttonsizer
->Add( 30, 5, 1 );
934 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
935 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_CMN_DIALOG
));
937 m_upDirButton
->SetToolTip( _("Go to parent directory") );
939 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
941 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
942 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
943 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_CMN_DIALOG
));
945 but
->SetToolTip( _("Go to home directory") );
947 buttonsizer
->Add( but
, 0, wxALL
, 5);
949 buttonsizer
->Add( 20, 20 );
952 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
953 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_CMN_DIALOG
));
955 m_newDirButton
->SetToolTip( _("Create new directory") );
957 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
960 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
962 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
964 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
966 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
967 m_static
= new wxStaticText( this, -1, m_dir
);
968 staticsizer
->Add( m_static
, 1 );
969 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
971 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
972 if ( !(m_dialogStyle
& wxMULTIPLE
) )
973 style2
|= wxLC_SINGLE_SEL
;
975 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
,
976 wildFilters
[0], ms_lastShowHidden
,
977 wxDefaultPosition
, wxSize(540,200),
982 // PDAs have a different screen layout
983 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
985 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
986 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
987 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
988 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
991 m_choice
= new wxChoice( this, ID_CHOICE
);
992 textsizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
994 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
995 buttonsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxALL
, 5 );
996 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 5 );
997 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1001 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1003 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1004 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1005 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1006 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1007 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1009 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1010 m_choice
= new wxChoice( this, ID_CHOICE
);
1011 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1012 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1013 m_check
->SetValue( ms_lastShowHidden
);
1014 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1015 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
1016 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1019 for (size_t n
=0; n
<wildFilters
.GetCount(); n
++)
1021 m_choice
->Append( wildDescriptions
[n
], (void*) new wxString( wildFilters
[n
] ) );
1023 SetFilterIndex( 0 );
1025 SetAutoLayout( TRUE
);
1026 SetSizer( mainsizer
);
1028 mainsizer
->Fit( this );
1029 mainsizer
->SetSizeHints( this );
1036 wxGenericFileDialog::~wxGenericFileDialog()
1038 if (wxConfig::Get(FALSE
))
1040 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1042 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1046 const int count
= m_choice
->GetCount();
1047 for ( int i
= 0; i
< count
; i
++ )
1049 delete (wxString
*)m_choice
->GetClientData(i
);
1053 int wxGenericFileDialog::ShowModal()
1055 m_list
->GoToDir(m_dir
);
1057 m_text
->SetValue(m_fileName
);
1059 return wxDialog::ShowModal();
1062 bool wxGenericFileDialog::Show( bool show
)
1066 m_list
->GoToDir(m_dir
);
1068 m_text
->SetValue(m_fileName
);
1071 return wxDialog::Show( show
);
1074 void wxGenericFileDialog::DoSetFilterIndex(int filterindex
)
1076 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1077 m_list
->SetWild( *str
);
1078 m_filterIndex
= filterindex
;
1079 if ( str
->Left(2) == wxT("*.") )
1081 m_filterExtension
= str
->Mid(1);
1082 if (m_filterExtension
== _T(".*"))
1083 m_filterExtension
.clear();
1087 m_filterExtension
.clear();
1091 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
1093 m_choice
->SetSelection( filterindex
);
1095 DoSetFilterIndex(filterindex
);
1098 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1100 DoSetFilterIndex((int)event
.GetInt());
1103 void wxGenericFileDialog::OnCheck( wxCommandEvent
&event
)
1105 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1108 void wxGenericFileDialog::OnActivated( wxListEvent
&event
)
1110 HandleAction( event
.m_item
.m_text
);
1113 void wxGenericFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1115 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1116 cevent
.SetEventObject( this );
1117 GetEventHandler()->ProcessEvent( cevent
);
1120 static bool ignoreChanges
= FALSE
;
1122 void wxGenericFileDialog::OnTextChange( wxCommandEvent
&WXUNUSED(event
) )
1126 // Clear selections. Otherwise when the user types in a value they may
1127 // not get the file whose name they typed.
1128 if (m_list
->GetSelectedItemCount() > 0)
1130 long item
= m_list
->GetNextItem(-1, wxLIST_NEXT_ALL
,
1131 wxLIST_STATE_SELECTED
);
1132 while ( item
!= -1 )
1134 m_list
->SetItemState(item
,0, wxLIST_STATE_SELECTED
);
1135 item
= m_list
->GetNextItem(item
, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1141 void wxGenericFileDialog::OnSelected( wxListEvent
&event
)
1143 wxString
filename( event
.m_item
.m_text
);
1144 if (filename
== wxT("..")) return;
1146 wxString dir
= m_list
->GetDir();
1147 if (!IsTopMostDir(dir
))
1148 dir
+= wxFILE_SEP_PATH
;
1150 if (wxDirExists(dir
)) return;
1152 ignoreChanges
= TRUE
;
1153 m_text
->SetValue( filename
);
1154 ignoreChanges
= FALSE
;
1157 void wxGenericFileDialog::HandleAction( const wxString
&fn
)
1159 wxString
filename( fn
);
1160 wxString dir
= m_list
->GetDir();
1161 if (filename
.IsEmpty()) return;
1162 if (filename
== wxT(".")) return;
1164 // "some/place/" means they want to chdir not try to load "place"
1165 bool want_dir
= filename
.Last() == wxFILE_SEP_PATH
;
1167 filename
= filename
.RemoveLast();
1169 if (filename
== wxT(".."))
1171 m_list
->GoToParentDir();
1178 if (filename
== wxT("~"))
1180 m_list
->GoToHomeDir();
1186 if (filename
.BeforeFirst(wxT('/')) == wxT("~"))
1188 filename
= wxGetUserHome() + filename
.Remove(0, 1);
1192 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1193 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1195 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1197 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1200 m_list
->SetWild( filename
);
1204 if (!IsTopMostDir(dir
))
1205 dir
+= wxFILE_SEP_PATH
;
1206 if (!wxIsAbsolutePath(filename
))
1212 if (wxDirExists(filename
))
1214 m_list
->GoToDir( filename
);
1219 // they really wanted a dir, but it doesn't exist
1222 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1223 wxOK
| wxICON_ERROR
);
1227 // append the default extension to the filename if it doesn't have any
1229 // VZ: the logic of testing for !wxFileExists() only for the open file
1230 // dialog is not entirely clear to me, why don't we allow saving to a
1231 // file without extension as well?
1232 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1234 filename
= AppendExtension(filename
, m_filterExtension
);
1237 // check that the file [doesn't] exist if necessary
1238 if ( (m_dialogStyle
& wxSAVE
) &&
1239 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1240 wxFileExists( filename
) )
1243 msg
.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename
.c_str() );
1245 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1248 else if ( (m_dialogStyle
& wxOPEN
) &&
1249 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1250 !wxFileExists(filename
) )
1252 wxMessageBox(_("Please choose an existing file."), _("Error"),
1253 wxOK
| wxICON_ERROR
);
1256 SetPath( filename
);
1258 // change to the directory where the user went if asked
1259 if ( m_dialogStyle
& wxCHANGE_DIR
)
1262 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1264 if ( cwd
!= wxGetCwd() )
1266 wxSetWorkingDirectory(cwd
);
1270 wxCommandEvent event
;
1271 wxDialog::OnOK(event
);
1274 void wxGenericFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1276 HandleAction( m_text
->GetValue() );
1279 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1281 m_list
->ChangeToListMode();
1282 ms_lastViewStyle
= wxLC_LIST
;
1286 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1288 m_list
->ChangeToReportMode();
1289 ms_lastViewStyle
= wxLC_REPORT
;
1293 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1295 m_list
->GoToParentDir();
1300 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1302 m_list
->GoToHomeDir();
1307 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1312 void wxGenericFileDialog::SetPath( const wxString
& path
)
1314 // not only set the full path but also update filename and dir
1316 if ( !path
.empty() )
1319 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1322 m_fileName
+= wxT(".");
1328 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
1331 if (m_list
->GetSelectedItemCount() == 0)
1333 paths
.Add( GetPath() );
1337 paths
.Alloc( m_list
->GetSelectedItemCount() );
1339 wxString dir
= m_list
->GetDir();
1341 if (dir
!= wxT("/"))
1343 dir
+= wxFILE_SEP_PATH
;
1346 item
.m_mask
= wxLIST_MASK_TEXT
;
1348 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1349 while ( item
.m_itemId
!= -1 )
1351 m_list
->GetItem( item
);
1352 paths
.Add( dir
+ item
.m_text
);
1353 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1354 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1358 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
1361 if (m_list
->GetSelectedItemCount() == 0)
1363 files
.Add( GetFilename() );
1366 files
.Alloc( m_list
->GetSelectedItemCount() );
1369 item
.m_mask
= wxLIST_MASK_TEXT
;
1371 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1372 while ( item
.m_itemId
!= -1 )
1374 m_list
->GetItem( item
);
1375 files
.Add( item
.m_text
);
1376 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1377 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1381 void wxGenericFileDialog::UpdateControls()
1383 wxString dir
= m_list
->GetDir();
1384 m_static
->SetLabel(dir
);
1386 bool enable
= !IsTopMostDir(dir
);
1387 m_upDirButton
->Enable(enable
);
1389 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1390 m_newDirButton
->Enable(enable
);
1391 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1394 #ifdef USE_GENERIC_FILEDIALOG
1396 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
);
1398 #endif // USE_GENERIC_FILEDIALOG
1400 #endif // wxUSE_FILEDLG