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"
26 #error wxFileDialog currently only supports unix
29 #include "wx/filedlg.h"
33 #include "wx/msgdlg.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/tokenzr.h"
37 #include "wx/mimetype.h"
39 #include "wx/module.h"
40 #include "wx/config.h"
41 #include "wx/imaglist.h"
44 #include "wx/tooltip.h"
47 #include <sys/types.h>
57 #include "wx/generic/home.xpm"
58 #include "wx/generic/listview.xpm"
59 #include "wx/generic/repview.xpm"
60 #include "wx/generic/new_dir.xpm"
61 #include "wx/generic/dir_up.xpm"
62 #include "wx/generic/folder.xpm"
63 #include "wx/generic/deffile.xpm"
64 #include "wx/generic/exefile.xpm"
66 // ----------------------------------------------------------------------------
67 // private classes - icons list management
68 // ----------------------------------------------------------------------------
70 class wxFileIconEntry
: public wxObject
73 wxFileIconEntry(int i
) { id
= i
; }
79 class wxFileIconsTable
84 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
85 wxImageList
*GetImageList() { return &m_ImageList
; }
88 wxImageList m_ImageList
;
89 wxHashTable m_HashTable
;
92 static wxFileIconsTable
*g_IconsTable
= NULL
;
96 #define FI_EXECUTABLE 2
98 wxFileIconsTable::wxFileIconsTable() :
100 m_HashTable(wxKEY_STRING
)
102 m_HashTable
.DeleteContents(TRUE
);
103 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
104 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
105 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
107 m_ImageList
.Add(wxBitmap(exefile_xpm
));
108 m_HashTable
.Delete(_T("exe"));
109 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
111 /* else put into list by GetIconID
112 (KDE defines application/x-executable for *.exe and has nice icon)
118 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
120 wxImage
small(16, 16);
121 unsigned char *p1
, *p2
, *ps
;
122 unsigned char mr
= img
.GetMaskRed(),
123 mg
= img
.GetMaskGreen(),
124 mb
= img
.GetMaskBlue();
127 unsigned sr
, sg
, sb
, smask
;
129 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
130 small
.SetMaskColour(mr
, mr
, mr
);
132 for (y
= 0; y
< 16; y
++)
134 for (x
= 0; x
< 16; x
++)
136 sr
= sg
= sb
= smask
= 0;
137 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
138 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
141 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
142 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
145 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
146 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
149 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
150 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
155 ps
[0] = ps
[1] = ps
[2] = mr
;
157 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
160 p1
+= 32 * 3, p2
+= 32 * 3;
163 return small
.ConvertToBitmap();
167 // finds empty borders and return non-empty area of image:
168 static wxImage
CutEmptyBorders(const wxImage
& img
)
170 unsigned char mr
= img
.GetMaskRed(),
171 mg
= img
.GetMaskGreen(),
172 mb
= img
.GetMaskBlue();
173 unsigned char *dt
= img
.GetData(), *dttmp
;
174 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
176 unsigned top
, bottom
, left
, right
, i
;
179 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
180 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
182 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
185 for (i
= 0; i
< w
; i
++, dttmp
+=3)
188 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
191 for (i
= 0; i
< w
; i
++, dttmp
+=3)
194 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
197 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
200 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
203 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
206 top
--, left
--, bottom
++, right
++;
208 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
213 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
215 if (!extension
.IsEmpty())
217 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
218 if (entry
) return (entry
-> id
);
221 wxFileType
*ft
= (mime
.IsEmpty()) ?
222 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
223 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
225 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
227 int newid
= FI_UNKNOWN
;
228 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
234 int id
= m_ImageList
.GetImageCount();
235 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
236 m_ImageList
.Add(img
.ConvertToBitmap());
239 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
240 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
242 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
244 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
250 // ----------------------------------------------------------------------------
252 // ----------------------------------------------------------------------------
255 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
257 wxFileData
*fd1
= (wxFileData
*)data1
;
258 wxFileData
*fd2
= (wxFileData
*)data2
;
259 if (fd1
->GetName() == wxT("..")) return -1;
260 if (fd2
->GetName() == wxT("..")) return 1;
261 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
262 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
263 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
266 //-----------------------------------------------------------------------------
268 //-----------------------------------------------------------------------------
270 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
272 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
278 stat( m_fileName
.fn_str(), &buff
);
280 #if !defined( __EMX__ ) && !defined(__VMS)
282 lstat( m_fileName
.fn_str(), &lbuff
);
283 m_isLink
= S_ISLNK( lbuff
.st_mode
);
284 struct tm
*t
= localtime( &lbuff
.st_mtime
);
287 struct tm
*t
= localtime( &buff
.st_mtime
);
290 // struct passwd *user = getpwuid( buff.st_uid );
291 // struct group *grp = getgrgid( buff.st_gid );
293 m_isDir
= S_ISDIR( buff
.st_mode
);
294 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
296 m_size
= buff
.st_size
;
299 m_minute
= t
->tm_min
;
300 m_month
= t
->tm_mon
+1;
305 m_permissions
.sprintf( wxT("%c%c%c"),
306 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
307 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
308 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
311 wxString
wxFileData::GetName() const
316 wxString
wxFileData::GetFullName() const
321 wxString
wxFileData::GetHint() const
323 wxString s
= m_fileName
;
325 if (m_isDir
) s
+= _("<DIR> ");
326 else if (m_isLink
) s
+= _("<LINK> ");
329 s
+= LongToString( m_size
);
332 s
+= IntToString( m_day
);
334 s
+= IntToString( m_month
);
336 s
+= IntToString( m_year
);
338 s
+= IntToString( m_hour
);
340 s
+= IntToString( m_minute
);
346 wxString
wxFileData::GetEntry( int num
)
358 if (m_isDir
) s
= _("<DIR>");
359 else if (m_isLink
) s
= _("<LINK>");
360 else s
= LongToString( m_size
);
365 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
366 s
+= IntToString( m_day
);
368 if (m_month
< 10) s
+= wxT("0");
369 s
+= IntToString( m_month
);
371 s
+= IntToString( m_year
);
376 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
377 s
+= IntToString( m_hour
);
379 if (m_minute
< 10) s
+= wxT("0");
380 s
+= IntToString( m_minute
);
393 bool wxFileData::IsDir()
398 bool wxFileData::IsExe()
403 bool wxFileData::IsLink()
408 long wxFileData::GetSize()
413 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
419 void wxFileData::MakeItem( wxListItem
&item
)
421 item
.m_text
= m_name
;
422 item
.ClearAttributes();
423 if (IsExe()) item
.SetTextColour(*wxRED
);
424 if (IsDir()) item
.SetTextColour(*wxBLUE
);
427 item
.m_image
= FI_FOLDER
;
429 item
.m_image
= FI_EXECUTABLE
;
430 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
431 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
433 item
.m_image
= FI_UNKNOWN
;
437 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
438 item
.SetTextColour(*dg
);
440 item
.m_data
= (long)this;
443 //-----------------------------------------------------------------------------
445 //-----------------------------------------------------------------------------
447 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
449 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
450 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
451 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
452 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
456 wxFileCtrl::wxFileCtrl()
458 m_dirName
= wxT("/");
459 m_showHidden
= FALSE
;
462 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
463 const wxString
&dirName
, const wxString
&wild
,
464 const wxPoint
&pos
, const wxSize
&size
,
465 long style
, const wxValidator
&validator
, const wxString
&name
) :
466 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
468 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
469 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
471 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
475 m_showHidden
= FALSE
;
479 void wxFileCtrl::ChangeToListMode()
481 SetSingleStyle( wxLC_LIST
);
485 void wxFileCtrl::ChangeToReportMode()
487 SetSingleStyle( wxLC_REPORT
);
491 void wxFileCtrl::ChangeToIconMode()
493 SetSingleStyle( wxLC_ICON
);
497 void wxFileCtrl::ShowHidden( bool show
)
503 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
506 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
507 fd
->MakeItem( item
);
508 long my_style
= GetWindowStyleFlag();
509 if (my_style
& wxLC_REPORT
)
511 ret
= InsertItem( item
);
512 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
514 else if (my_style
& wxLC_LIST
)
516 ret
= InsertItem( item
);
521 void wxFileCtrl::Update()
523 long my_style
= GetWindowStyleFlag();
524 int name_col_width
= 0;
525 if (my_style
& wxLC_REPORT
)
527 if (GetColumnCount() > 0)
528 name_col_width
= GetColumnWidth( 0 );
532 if (my_style
& wxLC_REPORT
)
534 if (name_col_width
< 140) name_col_width
= 140;
535 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
536 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
537 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
538 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
539 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
541 wxFileData
*fd
= (wxFileData
*) NULL
;
546 if (m_dirName
!= wxT("/"))
548 wxString
p( wxPathOnly(m_dirName
) );
549 if (p
.IsEmpty()) p
= wxT("/");
550 fd
= new wxFileData( wxT(".."), p
);
555 wxString res
= m_dirName
+ wxT("/*");
556 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
559 res
= wxFileNameFromPath( f
);
560 fd
= new wxFileData( res
, f
);
561 wxString s
= fd
->GetName();
562 if (m_showHidden
|| (s
[0u] != wxT('.')))
567 f
= wxFindNextFile();
570 res
= m_dirName
+ wxT("/") + m_wild
;
571 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
574 res
= wxFileNameFromPath( f
);
575 fd
= new wxFileData( res
, f
);
576 wxString s
= fd
->GetName();
577 if (m_showHidden
|| (s
[0u] != wxT('.')))
582 f
= wxFindNextFile();
585 SortItems( ListCompare
, 0 );
587 if (my_style
& wxLC_REPORT
)
589 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
590 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
591 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
595 void wxFileCtrl::SetWild( const wxString
&wild
)
601 void wxFileCtrl::MakeDir()
603 wxString
new_name( wxT("NewName") );
604 wxString
path( m_dirName
);
607 if (wxFileExists(path
))
609 // try NewName0, NewName1 etc.
612 new_name
= _("NewName");
614 num
.Printf( wxT("%d"), i
);
621 } while (wxFileExists(path
));
627 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
632 wxFileData
*fd
= new wxFileData( new_name
, path
);
636 long id
= Add( fd
, item
);
640 SortItems( ListCompare
, 0 );
641 id
= FindItem( 0, (long)fd
);
647 void wxFileCtrl::GoToParentDir()
649 if (m_dirName
!= wxT("/"))
651 wxString
fname( wxFileNameFromPath(m_dirName
) );
652 m_dirName
= wxPathOnly( m_dirName
);
653 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
655 long id
= FindItem( 0, fname
);
658 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
664 void wxFileCtrl::GoToHomeDir()
666 wxString s
= wxGetUserHome( wxString() );
669 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
673 void wxFileCtrl::GoToDir( const wxString
&dir
)
677 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
681 void wxFileCtrl::GetDir( wxString
&dir
)
686 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
688 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
692 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&WXUNUSED(event
) )
695 item
.m_mask
= wxLIST_MASK_DATA
;
697 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
698 while ( item
.m_itemId
!= -1 )
701 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
705 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
709 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
711 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
714 if ((event
.GetLabel().IsEmpty()) ||
715 (event
.GetLabel() == _(".")) ||
716 (event
.GetLabel() == _("..")) ||
717 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
719 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
725 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
726 new_name
+= wxT("/");
727 new_name
+= event
.GetLabel();
731 if (wxFileExists(new_name
))
733 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
738 if (wxRenameFile(fd
->GetFullName(),new_name
))
740 fd
->SetNewName( new_name
, event
.GetLabel() );
741 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
742 EnsureVisible( event
.GetItem() );
746 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
752 //-----------------------------------------------------------------------------
754 //-----------------------------------------------------------------------------
756 #define ID_LIST_MODE wxID_FILEDLGG
757 #define ID_REPORT_MODE wxID_FILEDLGG + 1
758 #define ID_UP_DIR wxID_FILEDLGG + 5
759 #define ID_PARENT_DIR wxID_FILEDLGG + 6
760 #define ID_NEW_DIR wxID_FILEDLGG + 7
761 #define ID_CHOICE wxID_FILEDLGG + 8
762 #define ID_TEXT wxID_FILEDLGG + 9
763 #define ID_LIST_CTRL wxID_FILEDLGG + 10
764 #define ID_ACTIVATED wxID_FILEDLGG + 11
765 #define ID_CHECK wxID_FILEDLGG + 12
767 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
769 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
770 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
771 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
772 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
773 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
774 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
775 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
776 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
777 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
778 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
779 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
780 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
783 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
784 bool wxFileDialog::s_lastShowHidden
= FALSE
;
786 wxFileDialog::wxFileDialog(wxWindow
*parent
,
787 const wxString
& message
,
788 const wxString
& defaultDir
,
789 const wxString
& defaultFile
,
790 const wxString
& wildCard
,
792 const wxPoint
& pos
) :
793 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
797 if (wxConfig::Get(FALSE
))
799 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle
);
800 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden
);
804 m_dialogStyle
= style
;
806 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
807 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
808 m_dialogStyle
|= wxOPEN
;
811 if ((m_dir
.IsEmpty()) || (m_dir
== wxT(".")))
814 m_dir
= getcwd( buf
, sizeof(buf
) );
818 m_path
+= defaultFile
;
819 m_fileName
= defaultFile
;
820 m_wildCard
= wildCard
;
822 m_filterExtension
= wxEmptyString
;
824 // interpret wildcards
826 if (m_wildCard
.IsEmpty())
827 m_wildCard
= _("All files (*)|*");
829 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
831 wxString firstWildText
;
832 if (tokens
.CountTokens() == 1)
834 firstWildText
= tokens
.GetNextToken();
835 firstWild
= firstWildText
;
839 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
840 firstWildText
= tokens
.GetNextToken();
841 firstWild
= tokens
.GetNextToken();
843 if ( firstWild
.Left( 2 ) == wxT("*.") )
844 m_filterExtension
= firstWild
.Mid( 1 );
845 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
849 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
851 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
855 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
857 but
->SetToolTip( _("View files as a list view") );
859 buttonsizer
->Add( but
, 0, wxALL
, 5 );
861 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
863 but
->SetToolTip( _("View files as a detailed view") );
865 buttonsizer
->Add( but
, 0, wxALL
, 5 );
867 buttonsizer
->Add( 30, 5, 1 );
869 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
871 but
->SetToolTip( _("Go to parent directory") );
873 buttonsizer
->Add( but
, 0, wxALL
, 5 );
875 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
877 but
->SetToolTip( _("Go to home directory") );
879 buttonsizer
->Add( but
, 0, wxALL
, 5);
881 buttonsizer
->Add( 20, 20 );
883 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
885 but
->SetToolTip( _("Create new directory") );
887 buttonsizer
->Add( but
, 0, wxALL
, 5 );
889 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
891 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
892 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
893 m_static
= new wxStaticText( this, -1, m_dir
);
894 staticsizer
->Add( m_static
, 1 );
895 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
897 if (m_dialogStyle
& wxMULTIPLE
)
898 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
899 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
);
901 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
902 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
903 m_list
-> ShowHidden(s_lastShowHidden
);
904 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
906 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
907 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
908 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
909 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
910 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
912 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
913 m_choice
= new wxChoice( this, ID_CHOICE
);
914 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
915 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
916 m_check
->SetValue( s_lastShowHidden
);
917 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
918 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
919 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
921 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
922 while (tokens
.HasMoreTokens())
924 firstWildText
= tokens
.GetNextToken();
925 firstWild
= tokens
.GetNextToken();
926 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
928 m_choice
->SetSelection( 0 );
930 SetAutoLayout( TRUE
);
931 SetSizer( mainsizer
);
933 mainsizer
->Fit( this );
934 mainsizer
->SetSizeHints( this );
939 if (m_fileName.IsEmpty())
948 wxFileDialog::~wxFileDialog()
950 if (wxConfig::Get(FALSE
))
952 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle
);
953 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden
);
957 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
959 int index
= (int)event
.GetInt();
960 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
961 m_list
->SetWild( *str
);
962 m_filterIndex
= index
;
963 if ( str
-> Left( 2 ) == wxT("*.") )
965 m_filterExtension
= str
-> Mid( 1 );
966 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
969 m_filterExtension
= wxEmptyString
;
972 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
974 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
977 void wxFileDialog::OnActivated( wxListEvent
&event
)
979 HandleAction( event
.m_item
.m_text
);
982 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
984 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
985 cevent
.SetEventObject( this );
986 GetEventHandler()->ProcessEvent( cevent
);
989 void wxFileDialog::OnSelected( wxListEvent
&event
)
991 if (FindFocus() != m_list
) return;
993 wxString
filename( event
.m_item
.m_text
);
994 if (filename
== wxT("..")) return;
997 m_list
->GetDir( dir
);
998 if (dir
!= wxT("/")) dir
+= wxT("/");
1000 if (wxDirExists(dir
)) return;
1002 m_text
->SetValue( filename
);
1005 void wxFileDialog::HandleAction( const wxString
&fn
)
1007 wxString
filename( fn
);
1009 m_list
->GetDir( dir
);
1010 if (filename
.IsEmpty()) return;
1011 if (filename
== wxT(".")) return;
1013 if (filename
== wxT(".."))
1015 m_list
->GoToParentDir();
1017 m_list
->GetDir( dir
);
1018 m_static
->SetLabel( dir
);
1022 if (filename
== wxT("~"))
1024 m_list
->GoToHomeDir();
1026 m_list
->GetDir( dir
);
1027 m_static
->SetLabel( dir
);
1031 if (filename
[0u] == wxT('~'))
1033 filename
.Remove( 0, 1 );
1034 wxString
tmp( wxGetUserHome() );
1040 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1041 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1043 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
1045 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1048 m_list
->SetWild( filename
);
1052 if (dir
!= wxT("/")) dir
+= wxT("/");
1053 if (filename
[0u] != wxT('/'))
1059 if (wxDirExists(filename
))
1061 m_list
->GoToDir( filename
);
1062 m_list
->GetDir( dir
);
1063 m_static
->SetLabel( dir
);
1068 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1070 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1071 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1072 filename
<< m_filterExtension
;
1073 if (wxFileExists( filename
))
1076 msg
.Printf( _("File '%s' already exists, do you really want to "
1077 "overwrite it?"), filename
.c_str() );
1079 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1083 else if ( m_dialogStyle
& wxOPEN
)
1085 if ( !wxFileExists( filename
) )
1086 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1087 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1088 filename
<< m_filterExtension
;
1090 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1092 if ( !wxFileExists( filename
) )
1094 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1100 SetPath( filename
);
1102 // change to the directory where the user went if asked
1103 if ( m_dialogStyle
& wxCHANGE_DIR
)
1106 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1108 if ( cwd
!= wxGetWorkingDirectory() )
1110 wxSetWorkingDirectory(cwd
);
1114 wxCommandEvent event
;
1115 wxDialog::OnOK(event
);
1118 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1120 HandleAction( m_text
->GetValue() );
1123 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1125 m_list
->ChangeToListMode();
1126 s_lastViewStyle
= wxLC_LIST
;
1130 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1132 m_list
->ChangeToReportMode();
1133 s_lastViewStyle
= wxLC_REPORT
;
1137 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1139 m_list
->GoToParentDir();
1142 m_list
->GetDir( dir
);
1143 m_static
->SetLabel( dir
);
1146 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1148 m_list
->GoToHomeDir();
1151 m_list
->GetDir( dir
);
1152 m_static
->SetLabel( dir
);
1155 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1160 void wxFileDialog::SetPath( const wxString
& path
)
1162 // not only set the full path but also update filename and dir
1167 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1170 m_fileName
+= wxT(".");
1176 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1179 if (m_list
->GetSelectedItemCount() == 0)
1181 paths
.Add( GetPath() );
1185 paths
.Alloc( m_list
->GetSelectedItemCount() );
1188 m_list
->GetDir( dir
);
1189 if (dir
!= wxT("/")) dir
+= wxT("/");
1192 item
.m_mask
= wxLIST_MASK_TEXT
;
1194 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1195 while ( item
.m_itemId
!= -1 )
1197 m_list
->GetItem( item
);
1198 paths
.Add( dir
+ item
.m_text
);
1199 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1200 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1204 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1207 if (m_list
->GetSelectedItemCount() == 0)
1209 files
.Add( GetFilename() );
1212 files
.Alloc( m_list
->GetSelectedItemCount() );
1215 item
.m_mask
= wxLIST_MASK_TEXT
;
1217 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1218 while ( item
.m_itemId
!= -1 )
1220 m_list
->GetItem( item
);
1221 files
.Add( item
.m_text
);
1222 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1223 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1229 // ----------------------------------------------------------------------------
1231 // ----------------------------------------------------------------------------
1234 wxFileSelectorEx(const wxChar
*message
,
1235 const wxChar
*default_path
,
1236 const wxChar
*default_filename
,
1237 int *WXUNUSED(indexDefaultExtension
),
1238 const wxChar
*wildcard
,
1243 // TODO: implement this somehow
1244 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1245 wildcard
, flags
, parent
, x
, y
);
1248 wxString
wxFileSelector( const wxChar
*title
,
1249 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1250 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1251 wxWindow
*parent
, int x
, int y
)
1254 if ( defaultExtension
&& !filter
)
1255 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1259 wxString defaultDirString
;
1261 defaultDirString
= defaultDir
;
1263 wxString defaultFilenameString
;
1264 if (defaultFileName
)
1265 defaultFilenameString
= defaultFileName
;
1267 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1269 if ( fileDialog
.ShowModal() == wxID_OK
)
1271 return fileDialog
.GetPath();
1275 return wxEmptyString
;
1279 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*ext
, const wxChar
*default_name
, wxWindow
*parent
)
1281 wxString prompt
= wxString::Format(_("Load %s file"), what
);
1283 if (*ext
== wxT('.'))
1286 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1288 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1289 ext
, wild
, 0, parent
);
1292 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1295 wxChar
*ext
= (wxChar
*)extension
;
1297 wxString prompt
= wxString::Format(_("Save %s file"), what
);
1299 if (*ext
== wxT('.'))
1302 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1304 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1305 ext
, wild
, 0, parent
);
1313 // A module to allow icons table cleanup
1315 class wxFileDialogGenericModule
: public wxModule
1317 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1319 wxFileDialogGenericModule() {}
1320 bool OnInit() { return TRUE
; }
1321 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1324 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)
1326 #endif // wxUSE_FILEDLG