1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #if !defined(__UNIX__) && !defined(__DOS__)
26 #error wxFileDialog currently only supports Unix and DOS
29 #include "wx/checkbox.h"
30 #include "wx/textctrl.h"
31 #include "wx/choice.h"
32 #include "wx/checkbox.h"
33 #include "wx/stattext.h"
34 #include "wx/filedlg.h"
38 #include "wx/listctrl.h"
39 #include "wx/msgdlg.h"
41 #include "wx/bmpbuttn.h"
42 #include "wx/tokenzr.h"
43 #include "wx/mimetype.h"
45 #include "wx/module.h"
46 #include "wx/config.h"
47 #include "wx/imaglist.h"
50 #include "wx/tooltip.h"
53 #include <sys/types.h>
72 #include "wx/generic/home.xpm"
74 #include "wx/generic/listview.xpm"
75 #include "wx/generic/repview.xpm"
76 #include "wx/generic/new_dir.xpm"
77 #include "wx/generic/dir_up.xpm"
78 #include "wx/generic/folder.xpm"
79 #include "wx/generic/deffile.xpm"
80 #include "wx/generic/exefile.xpm"
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 class wxFileData
: public wxObject
97 wxString m_permissions
;
104 wxFileData( const wxString
&name
, const wxString
&fname
);
105 wxString
GetName() const;
106 wxString
GetFullName() const;
107 wxString
GetHint() const;
108 wxString
GetEntry( int num
);
113 void MakeItem( wxListItem
&item
);
114 void SetNewName( const wxString
&name
, const wxString
&fname
);
117 DECLARE_DYNAMIC_CLASS(wxFileData
);
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
124 class wxFileCtrl
: public wxListCtrl
133 wxFileCtrl( wxWindow
*win
,
135 const wxString
&dirName
,
136 const wxString
&wild
,
137 const wxPoint
&pos
= wxDefaultPosition
,
138 const wxSize
&size
= wxDefaultSize
,
139 long style
= wxLC_LIST
,
140 const wxValidator
&validator
= wxDefaultValidator
,
141 const wxString
&name
= wxT("filelist") );
142 void ChangeToListMode();
143 void ChangeToReportMode();
144 void ChangeToIconMode();
145 void ShowHidden( bool show
= TRUE
);
146 long Add( wxFileData
*fd
, wxListItem
&item
);
148 virtual void StatusbarText( wxChar
*WXUNUSED(text
) ) {};
150 void GoToParentDir();
152 void GoToDir( const wxString
&dir
);
153 void SetWild( const wxString
&wild
);
154 void GetDir( wxString
&dir
);
155 void OnListDeleteItem( wxListEvent
&event
);
156 void OnListDeleteAllItems( wxListEvent
&event
);
157 void OnListEndLabelEdit( wxListEvent
&event
);
160 DECLARE_DYNAMIC_CLASS(wxFileCtrl
);
161 DECLARE_EVENT_TABLE()
164 // ----------------------------------------------------------------------------
165 // private classes - icons list management
166 // ----------------------------------------------------------------------------
168 class wxFileIconEntry
: public wxObject
171 wxFileIconEntry(int i
) { id
= i
; }
177 class wxFileIconsTable
182 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
183 wxImageList
*GetImageList() { return &m_ImageList
; }
186 wxImageList m_ImageList
;
187 wxHashTable m_HashTable
;
190 static wxFileIconsTable
*g_IconsTable
= NULL
;
194 #define FI_EXECUTABLE 2
196 wxFileIconsTable::wxFileIconsTable() :
198 m_HashTable(wxKEY_STRING
)
200 m_HashTable
.DeleteContents(TRUE
);
201 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
202 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
203 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
205 m_ImageList
.Add(wxBitmap(exefile_xpm
));
206 m_HashTable
.Delete(_T("exe"));
207 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
209 /* else put into list by GetIconID
210 (KDE defines application/x-executable for *.exe and has nice icon)
217 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
218 // one icon and we won't resize it
220 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
222 wxImage
small(16, 16);
223 unsigned char *p1
, *p2
, *ps
;
224 unsigned char mr
= img
.GetMaskRed(),
225 mg
= img
.GetMaskGreen(),
226 mb
= img
.GetMaskBlue();
229 unsigned sr
, sg
, sb
, smask
;
231 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
232 small
.SetMaskColour(mr
, mr
, mr
);
234 for (y
= 0; y
< 16; y
++)
236 for (x
= 0; x
< 16; x
++)
238 sr
= sg
= sb
= smask
= 0;
239 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
240 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
243 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
244 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
247 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
248 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
251 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
252 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
257 ps
[0] = ps
[1] = ps
[2] = mr
;
259 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
262 p1
+= 32 * 3, p2
+= 32 * 3;
265 return small
.ConvertToBitmap();
268 // finds empty borders and return non-empty area of image:
269 static wxImage
CutEmptyBorders(const wxImage
& img
)
271 unsigned char mr
= img
.GetMaskRed(),
272 mg
= img
.GetMaskGreen(),
273 mb
= img
.GetMaskBlue();
274 unsigned char *dt
= img
.GetData(), *dttmp
;
275 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
277 unsigned top
, bottom
, left
, right
, i
;
280 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
281 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
283 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
286 for (i
= 0; i
< w
; i
++, dttmp
+=3)
289 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
292 for (i
= 0; i
< w
; i
++, dttmp
+=3)
295 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
298 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
301 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
304 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
307 top
--, left
--, bottom
++, right
++;
309 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
311 #endif // wxUSE_MIMETYPE
315 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
318 if (!extension
.IsEmpty())
320 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
321 if (entry
) return (entry
-> id
);
324 wxFileType
*ft
= (mime
.IsEmpty()) ?
325 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
326 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
328 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
330 int newid
= FI_UNKNOWN
;
331 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
337 int id
= m_ImageList
.GetImageCount();
338 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
339 m_ImageList
.Add(img
.ConvertToBitmap());
342 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
343 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
345 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
347 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
350 #else // !wxUSE_MIMETYPE
352 if (extension
== wxT("exe"))
353 return FI_EXECUTABLE
;
356 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
361 // ----------------------------------------------------------------------------
363 // ----------------------------------------------------------------------------
366 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
368 wxFileData
*fd1
= (wxFileData
*)data1
;
369 wxFileData
*fd2
= (wxFileData
*)data2
;
370 if (fd1
->GetName() == wxT("..")) return -1;
371 if (fd2
->GetName() == wxT("..")) return 1;
372 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
373 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
374 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
377 //-----------------------------------------------------------------------------
379 //-----------------------------------------------------------------------------
381 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
383 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
389 stat( m_fileName
.fn_str(), &buff
);
391 #if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
393 lstat( m_fileName
.fn_str(), &lbuff
);
394 m_isLink
= S_ISLNK( lbuff
.st_mode
);
395 struct tm
*t
= localtime( &lbuff
.st_mtime
);
398 struct tm
*t
= localtime( &buff
.st_mtime
);
401 // struct passwd *user = getpwuid( buff.st_uid );
402 // struct group *grp = getgrgid( buff.st_gid );
404 m_isDir
= S_ISDIR( buff
.st_mode
);
405 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
407 m_size
= buff
.st_size
;
410 m_minute
= t
->tm_min
;
411 m_month
= t
->tm_mon
+1;
416 m_permissions
.sprintf( wxT("%c%c%c"),
417 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
418 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
419 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
422 wxString
wxFileData::GetName() const
427 wxString
wxFileData::GetFullName() const
432 wxString
wxFileData::GetHint() const
434 wxString s
= m_fileName
;
436 if (m_isDir
) s
+= _("<DIR> ");
437 else if (m_isLink
) s
+= _("<LINK> ");
440 s
+= LongToString( m_size
);
443 s
+= IntToString( m_day
);
445 s
+= IntToString( m_month
);
447 s
+= IntToString( m_year
);
449 s
+= IntToString( m_hour
);
451 s
+= IntToString( m_minute
);
457 wxString
wxFileData::GetEntry( int num
)
469 if (m_isDir
) s
= _("<DIR>");
470 else if (m_isLink
) s
= _("<LINK>");
471 else s
= LongToString( m_size
);
476 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
477 s
+= IntToString( m_day
);
479 if (m_month
< 10) s
+= wxT("0");
480 s
+= IntToString( m_month
);
482 s
+= IntToString( m_year
);
487 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
488 s
+= IntToString( m_hour
);
490 if (m_minute
< 10) s
+= wxT("0");
491 s
+= IntToString( m_minute
);
504 bool wxFileData::IsDir()
509 bool wxFileData::IsExe()
514 bool wxFileData::IsLink()
519 long wxFileData::GetSize()
524 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
530 void wxFileData::MakeItem( wxListItem
&item
)
532 item
.m_text
= m_name
;
533 item
.ClearAttributes();
534 if (IsExe()) item
.SetTextColour(*wxRED
);
535 if (IsDir()) item
.SetTextColour(*wxBLUE
);
538 item
.m_image
= FI_FOLDER
;
540 item
.m_image
= FI_EXECUTABLE
;
541 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
542 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
544 item
.m_image
= FI_UNKNOWN
;
548 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
549 item
.SetTextColour(*dg
);
551 item
.m_data
= (long)this;
554 //-----------------------------------------------------------------------------
556 //-----------------------------------------------------------------------------
558 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
560 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
561 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
562 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
563 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
567 wxFileCtrl::wxFileCtrl()
569 #if defined(__UNIX__)
570 m_dirName
= wxT("/");
571 #elif defined(__DOS__)
572 m_dirName
= wxT("C:\\");
574 m_showHidden
= FALSE
;
577 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
578 const wxString
&dirName
, const wxString
&wild
,
579 const wxPoint
&pos
, const wxSize
&size
,
580 long style
, const wxValidator
&validator
, const wxString
&name
) :
581 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
583 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
584 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
586 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
590 m_showHidden
= FALSE
;
594 void wxFileCtrl::ChangeToListMode()
596 SetSingleStyle( wxLC_LIST
);
600 void wxFileCtrl::ChangeToReportMode()
602 SetSingleStyle( wxLC_REPORT
);
606 void wxFileCtrl::ChangeToIconMode()
608 SetSingleStyle( wxLC_ICON
);
612 void wxFileCtrl::ShowHidden( bool show
)
618 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
621 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
622 fd
->MakeItem( item
);
623 long my_style
= GetWindowStyleFlag();
624 if (my_style
& wxLC_REPORT
)
627 const int noEntries
= 5;
629 const int noEntries
= 4;
631 ret
= InsertItem( item
);
632 for (int i
= 1; i
< noEntries
; i
++)
633 SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
635 else if (my_style
& wxLC_LIST
)
637 ret
= InsertItem( item
);
642 void wxFileCtrl::Update()
644 long my_style
= GetWindowStyleFlag();
645 int name_col_width
= 0;
646 if (my_style
& wxLC_REPORT
)
648 if (GetColumnCount() > 0)
649 name_col_width
= GetColumnWidth( 0 );
653 if (my_style
& wxLC_REPORT
)
655 if (name_col_width
< 140) name_col_width
= 140;
656 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
657 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
658 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
659 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
661 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
664 wxFileData
*fd
= (wxFileData
*) NULL
;
669 if (m_dirName
!= wxT("/"))
671 wxString
p( wxPathOnly(m_dirName
) );
672 if (p
.IsEmpty()) p
= wxT("/");
673 fd
= new wxFileData( wxT(".."), p
);
678 #if defined(__UNIX__)
679 wxString res
= m_dirName
+ wxT("/*");
680 #elif defined(__DOS__)
681 wxString res
= m_dirName
+ wxT("\\*.*");
683 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
686 res
= wxFileNameFromPath( f
);
687 fd
= new wxFileData( res
, f
);
688 wxString s
= fd
->GetName();
689 if (m_showHidden
|| (s
[0u] != wxT('.')))
694 f
= wxFindNextFile();
697 // Tokenize the wildcard string, so we can handle more than 1
698 // search pattern in a wildcard.
699 wxStringTokenizer
tokenWild( m_wild
, ";" );
700 while ( tokenWild
.HasMoreTokens() )
702 res
= m_dirName
+ wxFILE_SEP_PATH
+ tokenWild
.GetNextToken();
703 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
706 res
= wxFileNameFromPath( f
);
707 fd
= new wxFileData( res
, f
);
708 wxString s
= fd
->GetName();
709 if (m_showHidden
|| (s
[0u] != wxT('.')))
714 f
= wxFindNextFile();
718 SortItems( ListCompare
, 0 );
720 if (my_style
& wxLC_REPORT
)
722 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
723 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
724 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
728 void wxFileCtrl::SetWild( const wxString
&wild
)
734 void wxFileCtrl::MakeDir()
736 wxString
new_name( wxT("NewName") );
737 wxString
path( m_dirName
);
738 path
+= wxFILE_SEP_PATH
;
740 if (wxFileExists(path
))
742 // try NewName0, NewName1 etc.
745 new_name
= _("NewName");
747 num
.Printf( wxT("%d"), i
);
751 path
+= wxFILE_SEP_PATH
;
754 } while (wxFileExists(path
));
760 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
765 wxFileData
*fd
= new wxFileData( new_name
, path
);
769 long id
= Add( fd
, item
);
773 SortItems( ListCompare
, 0 );
774 id
= FindItem( 0, (long)fd
);
780 void wxFileCtrl::GoToParentDir()
782 if (m_dirName
!= wxT("/"))
784 size_t len
= m_dirName
.Len();
785 if (m_dirName
[len
-1] == wxFILE_SEP_PATH
)
786 m_dirName
.Remove( len
-1, 1 );
787 wxString
fname( wxFileNameFromPath(m_dirName
) );
788 m_dirName
= wxPathOnly( m_dirName
);
789 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
791 long id
= FindItem( 0, fname
);
794 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
800 void wxFileCtrl::GoToHomeDir()
802 wxString s
= wxGetUserHome( wxString() );
805 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
809 void wxFileCtrl::GoToDir( const wxString
&dir
)
813 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
817 void wxFileCtrl::GetDir( wxString
&dir
)
822 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
824 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
828 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&WXUNUSED(event
) )
831 item
.m_mask
= wxLIST_MASK_DATA
;
833 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
834 while ( item
.m_itemId
!= -1 )
837 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
841 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
845 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
847 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
850 if ((event
.GetLabel().IsEmpty()) ||
851 (event
.GetLabel() == _(".")) ||
852 (event
.GetLabel() == _("..")) ||
853 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
855 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
861 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
862 new_name
+= wxFILE_SEP_PATH
;
863 new_name
+= event
.GetLabel();
867 if (wxFileExists(new_name
))
869 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
874 if (wxRenameFile(fd
->GetFullName(),new_name
))
876 fd
->SetNewName( new_name
, event
.GetLabel() );
877 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
878 EnsureVisible( event
.GetItem() );
882 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
888 //-----------------------------------------------------------------------------
890 //-----------------------------------------------------------------------------
892 #define ID_LIST_MODE wxID_FILEDLGG
893 #define ID_REPORT_MODE wxID_FILEDLGG + 1
894 #define ID_UP_DIR wxID_FILEDLGG + 5
895 #define ID_PARENT_DIR wxID_FILEDLGG + 6
896 #define ID_NEW_DIR wxID_FILEDLGG + 7
897 #define ID_CHOICE wxID_FILEDLGG + 8
898 #define ID_TEXT wxID_FILEDLGG + 9
899 #define ID_LIST_CTRL wxID_FILEDLGG + 10
900 #define ID_ACTIVATED wxID_FILEDLGG + 11
901 #define ID_CHECK wxID_FILEDLGG + 12
903 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
905 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
906 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
907 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
908 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
909 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
910 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
911 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
912 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
913 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
914 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
915 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
916 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
919 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
920 bool wxFileDialog::s_lastShowHidden
= FALSE
;
922 wxFileDialog::wxFileDialog(wxWindow
*parent
,
923 const wxString
& message
,
924 const wxString
& defaultDir
,
925 const wxString
& defaultFile
,
926 const wxString
& wildCard
,
928 const wxPoint
& pos
) :
929 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
933 if (wxConfig::Get(FALSE
))
935 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle
);
936 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden
);
940 m_dialogStyle
= style
;
942 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
943 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
944 m_dialogStyle
|= wxOPEN
;
947 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
952 size_t len
= m_dir
.Len();
953 if ((len
> 1) && (m_dir
[len
-1] == wxFILE_SEP_PATH
))
954 m_dir
.Remove( len
-1, 1 );
957 m_path
+= wxFILE_SEP_PATH
;
958 m_path
+= defaultFile
;
959 m_fileName
= defaultFile
;
960 m_wildCard
= wildCard
;
962 m_filterExtension
= wxEmptyString
;
964 // interpret wildcards
966 if (m_wildCard
.IsEmpty())
967 m_wildCard
= _("All files (*)|*");
969 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
971 wxString firstWildText
;
972 if (tokens
.CountTokens() == 1)
974 firstWildText
= tokens
.GetNextToken();
975 firstWild
= firstWildText
;
979 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
980 firstWildText
= tokens
.GetNextToken();
981 firstWild
= tokens
.GetNextToken();
983 if ( firstWild
.Left( 2 ) == wxT("*.") )
984 m_filterExtension
= firstWild
.Mid( 1 );
985 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
989 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
991 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
995 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
997 but
->SetToolTip( _("View files as a list view") );
999 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1001 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
1003 but
->SetToolTip( _("View files as a detailed view") );
1005 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1007 buttonsizer
->Add( 30, 5, 1 );
1009 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
1011 but
->SetToolTip( _("Go to parent directory") );
1013 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1015 #ifndef __DOS__ // VS: Home directory is senseless in MS-DOS...
1016 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
1018 but
->SetToolTip( _("Go to home directory") );
1020 buttonsizer
->Add( but
, 0, wxALL
, 5);
1022 buttonsizer
->Add( 20, 20 );
1025 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
1027 but
->SetToolTip( _("Create new directory") );
1029 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1031 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1033 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1034 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
1035 m_static
= new wxStaticText( this, -1, m_dir
);
1036 staticsizer
->Add( m_static
, 1 );
1037 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1039 if (m_dialogStyle
& wxMULTIPLE
)
1040 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
1041 wxSize(540,200), s_lastViewStyle
| wxSUNKEN_BORDER
);
1043 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
1044 wxSize(540,200), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
1045 m_list
-> ShowHidden(s_lastShowHidden
);
1046 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1048 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1049 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1050 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1051 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1052 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1054 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1055 m_choice
= new wxChoice( this, ID_CHOICE
);
1056 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1057 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1058 m_check
->SetValue( s_lastShowHidden
);
1059 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1060 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
1061 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1063 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1064 while (tokens
.HasMoreTokens())
1066 firstWildText
= tokens
.GetNextToken();
1067 firstWild
= tokens
.GetNextToken();
1068 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1070 m_choice
->SetSelection( 0 );
1072 SetAutoLayout( TRUE
);
1073 SetSizer( mainsizer
);
1075 mainsizer
->Fit( this );
1076 mainsizer
->SetSizeHints( this );
1081 if (m_fileName.IsEmpty())
1090 wxFileDialog::~wxFileDialog()
1092 if (wxConfig::Get(FALSE
))
1094 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle
);
1095 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden
);
1099 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
1101 int index
= (int)event
.GetInt();
1102 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
1103 m_list
->SetWild( *str
);
1104 m_filterIndex
= index
;
1105 if ( str
-> Left( 2 ) == wxT("*.") )
1107 m_filterExtension
= str
-> Mid( 1 );
1108 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
1111 m_filterExtension
= wxEmptyString
;
1114 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
1116 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
1119 void wxFileDialog::OnActivated( wxListEvent
&event
)
1121 HandleAction( event
.m_item
.m_text
);
1124 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1126 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1127 cevent
.SetEventObject( this );
1128 GetEventHandler()->ProcessEvent( cevent
);
1131 void wxFileDialog::OnSelected( wxListEvent
&event
)
1133 if (FindFocus() != m_list
) return;
1135 wxString
filename( event
.m_item
.m_text
);
1136 if (filename
== wxT("..")) return;
1139 m_list
->GetDir( dir
);
1140 if (dir
!= wxT("/")) dir
+= wxFILE_SEP_PATH
;
1142 if (wxDirExists(dir
)) return;
1144 m_text
->SetValue( filename
);
1147 void wxFileDialog::HandleAction( const wxString
&fn
)
1149 wxString
filename( fn
);
1151 m_list
->GetDir( dir
);
1152 if (filename
.IsEmpty()) return;
1153 if (filename
== wxT(".")) return;
1155 if (filename
== wxT(".."))
1157 m_list
->GoToParentDir();
1159 m_list
->GetDir( dir
);
1160 m_static
->SetLabel( dir
);
1165 if (filename
== wxT("~"))
1167 m_list
->GoToHomeDir();
1169 m_list
->GetDir( dir
);
1170 m_static
->SetLabel( dir
);
1174 if (filename
[0u] == wxT('~'))
1176 filename
.Remove( 0, 1 );
1177 wxString
tmp( wxGetUserHome() );
1184 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1185 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1187 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1189 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1192 m_list
->SetWild( filename
);
1196 if (dir
!= wxT("/")) dir
+= wxFILE_SEP_PATH
;
1197 if (filename
[0u] != wxT('/'))
1203 if (wxDirExists(filename
))
1205 m_list
->GoToDir( filename
);
1206 m_list
->GetDir( dir
);
1207 m_static
->SetLabel( dir
);
1212 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1214 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1215 filename
.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1216 filename
<< m_filterExtension
;
1217 if (wxFileExists( filename
))
1220 msg
.Printf( _("File '%s' already exists, do you really want to "
1221 "overwrite it?"), filename
.c_str() );
1223 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1227 else if ( m_dialogStyle
& wxOPEN
)
1229 if ( !wxFileExists( filename
) )
1230 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1231 filename
.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1232 filename
<< m_filterExtension
;
1234 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1236 if ( !wxFileExists( filename
) )
1238 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1244 SetPath( filename
);
1246 // change to the directory where the user went if asked
1247 if ( m_dialogStyle
& wxCHANGE_DIR
)
1250 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1252 if ( cwd
!= wxGetWorkingDirectory() )
1254 wxSetWorkingDirectory(cwd
);
1258 wxCommandEvent event
;
1259 wxDialog::OnOK(event
);
1262 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1264 HandleAction( m_text
->GetValue() );
1267 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1269 m_list
->ChangeToListMode();
1270 s_lastViewStyle
= wxLC_LIST
;
1274 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1276 m_list
->ChangeToReportMode();
1277 s_lastViewStyle
= wxLC_REPORT
;
1281 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1283 m_list
->GoToParentDir();
1286 m_list
->GetDir( dir
);
1287 m_static
->SetLabel( dir
);
1290 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1292 m_list
->GoToHomeDir();
1295 m_list
->GetDir( dir
);
1296 m_static
->SetLabel( dir
);
1299 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1304 void wxFileDialog::SetPath( const wxString
& path
)
1306 // not only set the full path but also update filename and dir
1311 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1314 m_fileName
+= wxT(".");
1320 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1323 if (m_list
->GetSelectedItemCount() == 0)
1325 paths
.Add( GetPath() );
1329 paths
.Alloc( m_list
->GetSelectedItemCount() );
1332 m_list
->GetDir( dir
);
1333 if (dir
!= wxT("/")) dir
+= wxFILE_SEP_PATH
;
1336 item
.m_mask
= wxLIST_MASK_TEXT
;
1338 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1339 while ( item
.m_itemId
!= -1 )
1341 m_list
->GetItem( item
);
1342 paths
.Add( dir
+ item
.m_text
);
1343 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1344 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1348 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1351 if (m_list
->GetSelectedItemCount() == 0)
1353 files
.Add( GetFilename() );
1356 files
.Alloc( m_list
->GetSelectedItemCount() );
1359 item
.m_mask
= wxLIST_MASK_TEXT
;
1361 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1362 while ( item
.m_itemId
!= -1 )
1364 m_list
->GetItem( item
);
1365 files
.Add( item
.m_text
);
1366 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1367 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1373 // ----------------------------------------------------------------------------
1375 // ----------------------------------------------------------------------------
1378 wxFileSelectorEx(const wxChar
*message
,
1379 const wxChar
*default_path
,
1380 const wxChar
*default_filename
,
1381 int *WXUNUSED(indexDefaultExtension
),
1382 const wxChar
*wildcard
,
1387 // TODO: implement this somehow
1388 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1389 wildcard
, flags
, parent
, x
, y
);
1392 wxString
wxFileSelector( const wxChar
*title
,
1393 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1394 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1395 wxWindow
*parent
, int x
, int y
)
1398 if ( defaultExtension
&& !filter
)
1399 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1403 wxString defaultDirString
;
1405 defaultDirString
= defaultDir
;
1407 wxString defaultFilenameString
;
1408 if (defaultFileName
)
1409 defaultFilenameString
= defaultFileName
;
1411 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1413 if ( fileDialog
.ShowModal() == wxID_OK
)
1415 return fileDialog
.GetPath();
1419 return wxEmptyString
;
1423 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*ext
, const wxChar
*default_name
, wxWindow
*parent
)
1425 wxString prompt
= wxString::Format(_("Load %s file"), what
);
1427 if (*ext
== wxT('.'))
1430 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1432 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1433 ext
, wild
, 0, parent
);
1436 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1439 wxChar
*ext
= (wxChar
*)extension
;
1441 wxString prompt
= wxString::Format(_("Save %s file"), what
);
1443 if (*ext
== wxT('.'))
1446 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1448 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1449 ext
, wild
, 0, parent
);
1457 // A module to allow icons table cleanup
1459 class wxFileDialogGenericModule
: public wxModule
1461 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1463 wxFileDialogGenericModule() {}
1464 bool OnInit() { return TRUE
; }
1465 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1468 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)
1470 #endif // wxUSE_FILEDLG