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"
24 #error wxFileDialog currently only supports unix
27 #include "wx/filedlg.h"
31 #include "wx/msgdlg.h"
33 #include "wx/bmpbuttn.h"
34 #include "wx/tokenzr.h"
35 #include "wx/mimetype.h"
37 #include "wx/module.h"
38 #include "wx/config.h"
39 #include "wx/imaglist.h"
42 #include "wx/tooltip.h"
45 #include <sys/types.h>
55 #include "wx/generic/home.xpm"
56 #include "wx/generic/listview.xpm"
57 #include "wx/generic/repview.xpm"
58 #include "wx/generic/new_dir.xpm"
59 #include "wx/generic/dir_up.xpm"
60 #include "wx/generic/folder.xpm"
61 #include "wx/generic/deffile.xpm"
62 #include "wx/generic/exefile.xpm"
64 // ----------------------------------------------------------------------------
65 // private classes - icons list management
66 // ----------------------------------------------------------------------------
68 class wxFileIconEntry
: public wxObject
71 wxFileIconEntry(int i
) { id
= i
; }
77 class wxFileIconsTable
82 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
83 wxImageList
*GetImageList() { return &m_ImageList
; }
86 wxImageList m_ImageList
;
87 wxHashTable m_HashTable
;
90 static wxFileIconsTable
*g_IconsTable
= NULL
;
94 #define FI_EXECUTABLE 2
96 wxFileIconsTable::wxFileIconsTable() :
98 m_HashTable(wxKEY_STRING
)
100 m_HashTable
.DeleteContents(TRUE
);
101 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
102 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
103 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
105 m_ImageList
.Add(wxBitmap(exefile_xpm
));
106 m_HashTable
.Delete(_T("exe"));
107 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
109 /* else put into list by GetIconID
110 (KDE defines application/x-executable for *.exe and has nice icon)
116 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
118 wxImage
small(16, 16);
119 unsigned char *p1
, *p2
, *ps
;
120 unsigned char mr
= img
.GetMaskRed(),
121 mg
= img
.GetMaskGreen(),
122 mb
= img
.GetMaskBlue();
125 unsigned sr
, sg
, sb
, smask
;
127 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
128 small
.SetMaskColour(mr
, mr
, mr
);
130 for (y
= 0; y
< 16; y
++)
132 for (x
= 0; x
< 16; x
++)
134 sr
= sg
= sb
= smask
= 0;
135 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
136 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
139 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
140 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
143 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
144 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
147 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
148 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
153 ps
[0] = ps
[1] = ps
[2] = mr
;
155 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
158 p1
+= 32 * 3, p2
+= 32 * 3;
161 return small
.ConvertToBitmap();
165 // finds empty borders and return non-empty area of image:
166 static wxImage
CutEmptyBorders(const wxImage
& img
)
168 unsigned char mr
= img
.GetMaskRed(),
169 mg
= img
.GetMaskGreen(),
170 mb
= img
.GetMaskBlue();
171 unsigned char *dt
= img
.GetData(), *dttmp
;
172 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
174 unsigned top
, bottom
, left
, right
, i
;
177 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
178 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
180 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
183 for (i
= 0; i
< w
; i
++, dttmp
+=3)
186 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
189 for (i
= 0; i
< w
; i
++, dttmp
+=3)
192 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
195 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
198 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
201 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
204 top
--, left
--, bottom
++, right
++;
206 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
211 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
213 if (!extension
.IsEmpty())
215 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
216 if (entry
) return (entry
-> id
);
219 wxFileType
*ft
= (mime
.IsEmpty()) ?
220 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
221 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
223 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
225 int newid
= FI_UNKNOWN
;
226 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
232 int id
= m_ImageList
.GetImageCount();
233 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
234 m_ImageList
.Add(img
.ConvertToBitmap());
237 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
238 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
240 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
242 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
248 // ----------------------------------------------------------------------------
250 // ----------------------------------------------------------------------------
253 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
255 wxFileData
*fd1
= (wxFileData
*)data1
;
256 wxFileData
*fd2
= (wxFileData
*)data2
;
257 if (fd1
->GetName() == wxT("..")) return -1;
258 if (fd2
->GetName() == wxT("..")) return 1;
259 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
260 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
261 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
264 //-----------------------------------------------------------------------------
266 //-----------------------------------------------------------------------------
268 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
270 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
276 stat( m_fileName
.fn_str(), &buff
);
278 #if !defined( __EMX__ ) && !defined(__VMS)
280 lstat( m_fileName
.fn_str(), &lbuff
);
281 m_isLink
= S_ISLNK( lbuff
.st_mode
);
282 struct tm
*t
= localtime( &lbuff
.st_mtime
);
285 struct tm
*t
= localtime( &buff
.st_mtime
);
288 // struct passwd *user = getpwuid( buff.st_uid );
289 // struct group *grp = getgrgid( buff.st_gid );
291 m_isDir
= S_ISDIR( buff
.st_mode
);
292 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
294 m_size
= buff
.st_size
;
297 m_minute
= t
->tm_min
;
298 m_month
= t
->tm_mon
+1;
303 m_permissions
.sprintf( wxT("%c%c%c"),
304 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
305 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
306 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
309 wxString
wxFileData::GetName() const
314 wxString
wxFileData::GetFullName() const
319 wxString
wxFileData::GetHint() const
321 wxString s
= m_fileName
;
323 if (m_isDir
) s
+= _("<DIR> ");
324 else if (m_isLink
) s
+= _("<LINK> ");
327 s
+= LongToString( m_size
);
330 s
+= IntToString( m_day
);
332 s
+= IntToString( m_month
);
334 s
+= IntToString( m_year
);
336 s
+= IntToString( m_hour
);
338 s
+= IntToString( m_minute
);
344 wxString
wxFileData::GetEntry( int num
)
356 if (m_isDir
) s
= _("<DIR>");
357 else if (m_isLink
) s
= _("<LINK>");
358 else s
= LongToString( m_size
);
363 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
364 s
+= IntToString( m_day
);
366 if (m_month
< 10) s
+= wxT("0");
367 s
+= IntToString( m_month
);
369 s
+= IntToString( m_year
);
374 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
375 s
+= IntToString( m_hour
);
377 if (m_minute
< 10) s
+= wxT("0");
378 s
+= IntToString( m_minute
);
391 bool wxFileData::IsDir()
396 bool wxFileData::IsExe()
401 bool wxFileData::IsLink()
406 long wxFileData::GetSize()
411 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
417 void wxFileData::MakeItem( wxListItem
&item
)
419 item
.m_text
= m_name
;
420 item
.ClearAttributes();
421 if (IsExe()) item
.SetTextColour(*wxRED
);
422 if (IsDir()) item
.SetTextColour(*wxBLUE
);
425 item
.m_image
= FI_FOLDER
;
427 item
.m_image
= FI_EXECUTABLE
;
428 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
429 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
431 item
.m_image
= FI_UNKNOWN
;
435 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
436 item
.SetTextColour(*dg
);
438 item
.m_data
= (long)this;
441 //-----------------------------------------------------------------------------
443 //-----------------------------------------------------------------------------
445 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
447 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
448 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
449 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
450 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
454 wxFileCtrl::wxFileCtrl()
456 m_dirName
= wxT("/");
457 m_showHidden
= FALSE
;
460 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
461 const wxString
&dirName
, const wxString
&wild
,
462 const wxPoint
&pos
, const wxSize
&size
,
463 long style
, const wxValidator
&validator
, const wxString
&name
) :
464 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
466 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
467 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
469 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
473 m_showHidden
= FALSE
;
477 void wxFileCtrl::ChangeToListMode()
479 SetSingleStyle( wxLC_LIST
);
483 void wxFileCtrl::ChangeToReportMode()
485 SetSingleStyle( wxLC_REPORT
);
489 void wxFileCtrl::ChangeToIconMode()
491 SetSingleStyle( wxLC_ICON
);
495 void wxFileCtrl::ShowHidden( bool show
)
501 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
504 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
505 fd
->MakeItem( item
);
506 long my_style
= GetWindowStyleFlag();
507 if (my_style
& wxLC_REPORT
)
509 ret
= InsertItem( item
);
510 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
512 else if (my_style
& wxLC_LIST
)
514 ret
= InsertItem( item
);
519 void wxFileCtrl::Update()
521 long my_style
= GetWindowStyleFlag();
522 int name_col_width
= 0;
523 if (my_style
& wxLC_REPORT
)
525 if (GetColumnCount() > 0)
526 name_col_width
= GetColumnWidth( 0 );
530 if (my_style
& wxLC_REPORT
)
532 if (name_col_width
< 140) name_col_width
= 140;
533 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
534 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
535 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
536 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
537 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
539 wxFileData
*fd
= (wxFileData
*) NULL
;
544 if (m_dirName
!= wxT("/"))
546 wxString
p( wxPathOnly(m_dirName
) );
547 if (p
.IsEmpty()) p
= wxT("/");
548 fd
= new wxFileData( wxT(".."), p
);
553 wxString res
= m_dirName
+ wxT("/*");
554 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
557 res
= wxFileNameFromPath( f
);
558 fd
= new wxFileData( res
, f
);
559 wxString s
= fd
->GetName();
560 if (m_showHidden
|| (s
[0u] != wxT('.')))
565 f
= wxFindNextFile();
568 res
= m_dirName
+ wxT("/") + m_wild
;
569 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
572 res
= wxFileNameFromPath( f
);
573 fd
= new wxFileData( res
, f
);
574 wxString s
= fd
->GetName();
575 if (m_showHidden
|| (s
[0u] != wxT('.')))
580 f
= wxFindNextFile();
583 SortItems( ListCompare
, 0 );
585 if (my_style
& wxLC_REPORT
)
587 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
588 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
589 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
593 void wxFileCtrl::SetWild( const wxString
&wild
)
599 void wxFileCtrl::MakeDir()
601 wxString
new_name( wxT("NewName") );
602 wxString
path( m_dirName
);
605 if (wxFileExists(path
))
607 // try NewName0, NewName1 etc.
610 new_name
= _("NewName");
612 num
.Printf( wxT("%d"), i
);
619 } while (wxFileExists(path
));
625 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
630 wxFileData
*fd
= new wxFileData( new_name
, path
);
634 long id
= Add( fd
, item
);
638 SortItems( ListCompare
, 0 );
639 id
= FindItem( 0, (long)fd
);
645 void wxFileCtrl::GoToParentDir()
647 if (m_dirName
!= wxT("/"))
649 wxString
fname( wxFileNameFromPath(m_dirName
) );
650 m_dirName
= wxPathOnly( m_dirName
);
651 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
653 long id
= FindItem( 0, fname
);
656 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
662 void wxFileCtrl::GoToHomeDir()
664 wxString s
= wxGetUserHome( wxString() );
667 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
671 void wxFileCtrl::GoToDir( const wxString
&dir
)
675 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
679 void wxFileCtrl::GetDir( wxString
&dir
)
684 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
686 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
690 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&WXUNUSED(event
) )
693 item
.m_mask
= wxLIST_MASK_DATA
;
695 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
696 while ( item
.m_itemId
!= -1 )
699 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
703 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
707 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
709 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
712 if ((event
.GetLabel().IsEmpty()) ||
713 (event
.GetLabel() == _(".")) ||
714 (event
.GetLabel() == _("..")) ||
715 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
717 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
723 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
724 new_name
+= wxT("/");
725 new_name
+= event
.GetLabel();
729 if (wxFileExists(new_name
))
731 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
736 if (wxRenameFile(fd
->GetFullName(),new_name
))
738 fd
->SetNewName( new_name
, event
.GetLabel() );
739 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
740 EnsureVisible( event
.GetItem() );
744 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
750 //-----------------------------------------------------------------------------
752 //-----------------------------------------------------------------------------
754 #define ID_LIST_MODE wxID_FILEDLGG
755 #define ID_REPORT_MODE wxID_FILEDLGG + 1
756 #define ID_UP_DIR wxID_FILEDLGG + 5
757 #define ID_PARENT_DIR wxID_FILEDLGG + 6
758 #define ID_NEW_DIR wxID_FILEDLGG + 7
759 #define ID_CHOICE wxID_FILEDLGG + 8
760 #define ID_TEXT wxID_FILEDLGG + 9
761 #define ID_LIST_CTRL wxID_FILEDLGG + 10
762 #define ID_ACTIVATED wxID_FILEDLGG + 11
763 #define ID_CHECK wxID_FILEDLGG + 12
765 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
767 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
768 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
769 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
770 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
771 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
772 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
773 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
774 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
775 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
776 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
777 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
778 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
781 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
782 bool wxFileDialog::s_lastShowHidden
= FALSE
;
784 wxFileDialog::wxFileDialog(wxWindow
*parent
,
785 const wxString
& message
,
786 const wxString
& defaultDir
,
787 const wxString
& defaultFile
,
788 const wxString
& wildCard
,
790 const wxPoint
& pos
) :
791 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
795 if (wxConfig::Get(FALSE
))
797 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle
);
798 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden
);
802 m_dialogStyle
= style
;
804 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
805 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
806 m_dialogStyle
|= wxOPEN
;
809 if ((m_dir
.IsEmpty()) || (m_dir
== wxT(".")))
812 m_dir
= getcwd( buf
, sizeof(buf
) );
816 m_path
+= defaultFile
;
817 m_fileName
= defaultFile
;
818 m_wildCard
= wildCard
;
820 m_filterExtension
= wxEmptyString
;
822 // interpret wildcards
824 if (m_wildCard
.IsEmpty())
825 m_wildCard
= _("All files (*)|*");
827 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
829 wxString firstWildText
;
830 if (tokens
.CountTokens() == 1)
832 firstWildText
= tokens
.GetNextToken();
833 firstWild
= firstWildText
;
837 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
838 firstWildText
= tokens
.GetNextToken();
839 firstWild
= tokens
.GetNextToken();
841 if ( firstWild
.Left( 2 ) == wxT("*.") )
842 m_filterExtension
= firstWild
.Mid( 1 );
843 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
847 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
849 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
853 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
855 but
->SetToolTip( _("View files as a list view") );
857 buttonsizer
->Add( but
, 0, wxALL
, 5 );
859 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
861 but
->SetToolTip( _("View files as a detailed view") );
863 buttonsizer
->Add( but
, 0, wxALL
, 5 );
865 buttonsizer
->Add( 30, 5, 1 );
867 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
869 but
->SetToolTip( _("Go to parent directory") );
871 buttonsizer
->Add( but
, 0, wxALL
, 5 );
873 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
875 but
->SetToolTip( _("Go to home directory") );
877 buttonsizer
->Add( but
, 0, wxALL
, 5);
879 buttonsizer
->Add( 20, 20 );
881 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
883 but
->SetToolTip( _("Create new directory") );
885 buttonsizer
->Add( but
, 0, wxALL
, 5 );
887 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
889 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
890 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
891 m_static
= new wxStaticText( this, -1, m_dir
);
892 staticsizer
->Add( m_static
, 1 );
893 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
895 if (m_dialogStyle
& wxMULTIPLE
)
896 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
897 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
);
899 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
900 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
901 m_list
-> ShowHidden(s_lastShowHidden
);
902 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
904 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
905 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
906 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
907 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
908 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
910 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
911 m_choice
= new wxChoice( this, ID_CHOICE
);
912 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
913 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
914 m_check
->SetValue( s_lastShowHidden
);
915 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
916 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
917 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
919 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
920 while (tokens
.HasMoreTokens())
922 firstWildText
= tokens
.GetNextToken();
923 firstWild
= tokens
.GetNextToken();
924 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
926 m_choice
->SetSelection( 0 );
928 SetAutoLayout( TRUE
);
929 SetSizer( mainsizer
);
931 mainsizer
->Fit( this );
932 mainsizer
->SetSizeHints( this );
937 if (m_fileName.IsEmpty())
946 wxFileDialog::~wxFileDialog()
948 if (wxConfig::Get(FALSE
))
950 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle
);
951 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden
);
955 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
957 int index
= (int)event
.GetInt();
958 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
959 m_list
->SetWild( *str
);
960 m_filterIndex
= index
;
961 if ( str
-> Left( 2 ) == wxT("*.") )
963 m_filterExtension
= str
-> Mid( 1 );
964 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
967 m_filterExtension
= wxEmptyString
;
970 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
972 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
975 void wxFileDialog::OnActivated( wxListEvent
&event
)
977 HandleAction( event
.m_item
.m_text
);
980 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
982 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
983 cevent
.SetEventObject( this );
984 GetEventHandler()->ProcessEvent( cevent
);
987 void wxFileDialog::OnSelected( wxListEvent
&event
)
989 if (FindFocus() != m_list
) return;
991 wxString
filename( event
.m_item
.m_text
);
992 if (filename
== wxT("..")) return;
995 m_list
->GetDir( dir
);
996 if (dir
!= wxT("/")) dir
+= wxT("/");
998 if (wxDirExists(dir
)) return;
1000 m_text
->SetValue( filename
);
1003 void wxFileDialog::HandleAction( const wxString
&fn
)
1005 wxString
filename( fn
);
1007 m_list
->GetDir( dir
);
1008 if (filename
.IsEmpty()) return;
1009 if (filename
== wxT(".")) return;
1011 if (filename
== wxT(".."))
1013 m_list
->GoToParentDir();
1015 m_list
->GetDir( dir
);
1016 m_static
->SetLabel( dir
);
1020 if (filename
== wxT("~"))
1022 m_list
->GoToHomeDir();
1024 m_list
->GetDir( dir
);
1025 m_static
->SetLabel( dir
);
1029 if (filename
[0u] == wxT('~'))
1031 filename
.Remove( 0, 1 );
1032 wxString
tmp( wxGetUserHome() );
1038 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1039 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1041 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
1043 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1046 m_list
->SetWild( filename
);
1050 if (dir
!= wxT("/")) dir
+= wxT("/");
1051 if (filename
[0u] != wxT('/'))
1057 if (wxDirExists(filename
))
1059 m_list
->GoToDir( filename
);
1060 m_list
->GetDir( dir
);
1061 m_static
->SetLabel( dir
);
1066 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1068 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1069 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1070 filename
<< m_filterExtension
;
1071 if (wxFileExists( filename
))
1074 msg
.Printf( _("File '%s' already exists, do you really want to "
1075 "overwrite it?"), filename
.c_str() );
1077 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1081 else if ( m_dialogStyle
& wxOPEN
)
1083 if ( !wxFileExists( filename
) )
1084 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1085 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1086 filename
<< m_filterExtension
;
1088 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1090 if ( !wxFileExists( filename
) )
1092 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1098 SetPath( filename
);
1100 wxCommandEvent event
;
1101 wxDialog::OnOK(event
);
1104 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1106 HandleAction( m_text
->GetValue() );
1109 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1111 m_list
->ChangeToListMode();
1112 s_lastViewStyle
= wxLC_LIST
;
1116 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1118 m_list
->ChangeToReportMode();
1119 s_lastViewStyle
= wxLC_REPORT
;
1123 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1125 m_list
->GoToParentDir();
1128 m_list
->GetDir( dir
);
1129 m_static
->SetLabel( dir
);
1132 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1134 m_list
->GoToHomeDir();
1137 m_list
->GetDir( dir
);
1138 m_static
->SetLabel( dir
);
1141 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1146 void wxFileDialog::SetPath( const wxString
& path
)
1148 // not only set the full path but also update filename and dir
1153 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1156 m_fileName
+= wxT(".");
1162 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1165 if (m_list
->GetSelectedItemCount() == 0)
1167 paths
.Add( GetPath() );
1171 paths
.Alloc( m_list
->GetSelectedItemCount() );
1174 m_list
->GetDir( dir
);
1175 if (dir
!= wxT("/")) dir
+= wxT("/");
1178 item
.m_mask
= wxLIST_MASK_TEXT
;
1180 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1181 while ( item
.m_itemId
!= -1 )
1183 m_list
->GetItem( item
);
1184 paths
.Add( dir
+ item
.m_text
);
1185 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1186 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1190 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1193 if (m_list
->GetSelectedItemCount() == 0)
1195 files
.Add( GetFilename() );
1198 files
.Alloc( m_list
->GetSelectedItemCount() );
1201 item
.m_mask
= wxLIST_MASK_TEXT
;
1203 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1204 while ( item
.m_itemId
!= -1 )
1206 m_list
->GetItem( item
);
1207 files
.Add( item
.m_text
);
1208 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1209 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1215 // ----------------------------------------------------------------------------
1217 // ----------------------------------------------------------------------------
1220 wxFileSelectorEx(const wxChar
*message
,
1221 const wxChar
*default_path
,
1222 const wxChar
*default_filename
,
1223 int *WXUNUSED(indexDefaultExtension
),
1224 const wxChar
*wildcard
,
1229 // TODO: implement this somehow
1230 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1231 wildcard
, flags
, parent
, x
, y
);
1234 wxString
wxFileSelector( const wxChar
*title
,
1235 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1236 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1237 wxWindow
*parent
, int x
, int y
)
1240 if ( defaultExtension
&& !filter
)
1241 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1245 wxString defaultDirString
;
1247 defaultDirString
= defaultDir
;
1249 wxString defaultFilenameString
;
1250 if (defaultFileName
)
1251 defaultFilenameString
= defaultFileName
;
1253 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1255 if ( fileDialog
.ShowModal() == wxID_OK
)
1257 return fileDialog
.GetPath();
1261 return wxEmptyString
;
1265 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
1267 wxChar
*ext
= (wxChar
*)extension
;
1270 wxString str
= _("Load %s file");
1271 wxSprintf(prompt
, str
, what
);
1273 if (*ext
== wxT('.')) ext
++;
1275 wxSprintf(wild
, wxT("*.%s"), ext
);
1277 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1280 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1283 wxChar
*ext
= (wxChar
*)extension
;
1286 wxString str
= _("Save %s file");
1287 wxSprintf(prompt
, str
, what
);
1289 if (*ext
== wxT('.')) ext
++;
1291 wxSprintf(wild
, wxT("*.%s"), ext
);
1293 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1301 // A module to allow icons table cleanup
1303 class wxFileDialogGenericModule
: public wxModule
1305 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1307 wxFileDialogGenericModule() {}
1308 bool OnInit() { return TRUE
; }
1309 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1312 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)