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"
41 #include "wx/tooltip.h"
44 #include <sys/types.h>
52 #include "wx/generic/home.xpm"
53 #include "wx/generic/listview.xpm"
54 #include "wx/generic/repview.xpm"
55 #include "wx/generic/new_dir.xpm"
56 #include "wx/generic/dir_up.xpm"
57 #include "wx/generic/folder.xpm"
58 #include "wx/generic/deffile.xpm"
59 #include "wx/generic/exefile.xpm"
61 // ----------------------------------------------------------------------------
62 // private classes - icons list management
63 // ----------------------------------------------------------------------------
65 class wxFileIconEntry
: public wxObject
68 wxFileIconEntry(int i
) { id
= i
; }
74 class wxFileIconsTable
80 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
81 wxImageList
*GetImageList() { return &m_ImageList
; }
84 wxImageList m_ImageList
;
85 wxHashTable m_HashTable
;
86 wxMimeTypesManager m_Mime
;
89 static wxFileIconsTable
*g_IconsTable
= NULL
;
93 #define FI_EXECUTABLE 2
95 wxFileIconsTable::wxFileIconsTable() :
97 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 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
167 if (!extension
.IsEmpty())
169 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
170 if (entry
) return (entry
-> id
);
173 wxFileType
*ft
= (mime
.IsEmpty()) ?
174 m_Mime
.GetFileTypeFromExtension(extension
) :
175 m_Mime
.GetFileTypeFromMimeType(mime
);
177 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)))
179 int newid
= FI_UNKNOWN
;
180 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
186 int id
= m_ImageList
.GetImageCount();
187 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
188 m_ImageList
.Add(img
.ConvertToBitmap());
191 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
193 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
195 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
201 // ----------------------------------------------------------------------------
203 // ----------------------------------------------------------------------------
206 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
208 wxFileData
*fd1
= (wxFileData
*)data1
;
209 wxFileData
*fd2
= (wxFileData
*)data2
;
210 if (fd1
->GetName() == wxT("..")) return -1;
211 if (fd2
->GetName() == wxT("..")) return 1;
212 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
213 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
214 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
217 //-----------------------------------------------------------------------------
219 //-----------------------------------------------------------------------------
221 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
223 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
229 stat( m_fileName
.fn_str(), &buff
);
233 lstat( m_fileName
.fn_str(), &lbuff
);
234 m_isLink
= S_ISLNK( lbuff
.st_mode
);
235 struct tm
*t
= localtime( &lbuff
.st_mtime
);
238 struct tm
*t
= localtime( &buff
.st_mtime
);
241 // struct passwd *user = getpwuid( buff.st_uid );
242 // struct group *grp = getgrgid( buff.st_gid );
244 m_isDir
= S_ISDIR( buff
.st_mode
);
245 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
247 m_size
= buff
.st_size
;
250 m_minute
= t
->tm_min
;
251 m_month
= t
->tm_mon
+1;
256 m_permissions
.sprintf( wxT("%c%c%c"),
257 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
258 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
259 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
262 wxString
wxFileData::GetName() const
267 wxString
wxFileData::GetFullName() const
272 wxString
wxFileData::GetHint() const
274 wxString s
= m_fileName
;
276 if (m_isDir
) s
+= _("<DIR> ");
277 else if (m_isLink
) s
+= _("<LINK> ");
280 s
+= LongToString( m_size
);
283 s
+= IntToString( m_day
);
285 s
+= IntToString( m_month
);
287 s
+= IntToString( m_year
);
289 s
+= IntToString( m_hour
);
291 s
+= IntToString( m_minute
);
297 wxString
wxFileData::GetEntry( int num
)
309 if (m_isDir
) s
= _("<DIR>");
310 else if (m_isLink
) s
= _("<LINK>");
311 else s
= LongToString( m_size
);
316 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
317 s
+= IntToString( m_day
);
319 if (m_month
< 10) s
+= wxT("0");
320 s
+= IntToString( m_month
);
322 s
+= IntToString( m_year
);
327 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
328 s
+= IntToString( m_hour
);
330 if (m_minute
< 10) s
+= wxT("0");
331 s
+= IntToString( m_minute
);
344 bool wxFileData::IsDir()
349 bool wxFileData::IsExe()
354 bool wxFileData::IsLink()
359 long wxFileData::GetSize()
364 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
370 void wxFileData::MakeItem( wxListItem
&item
)
372 item
.m_text
= m_name
;
373 item
.ClearAttributes();
374 if (IsExe()) item
.SetTextColour(*wxRED
);
375 if (IsDir()) item
.SetTextColour(*wxBLUE
);
378 item
.m_image
= FI_FOLDER
;
380 item
.m_image
= FI_EXECUTABLE
;
381 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
382 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
384 item
.m_image
= FI_UNKNOWN
;
388 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
389 item
.SetTextColour(*dg
);
391 item
.m_data
= (long)this;
394 //-----------------------------------------------------------------------------
396 //-----------------------------------------------------------------------------
398 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
400 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
401 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
402 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
407 wxFileCtrl::wxFileCtrl()
409 m_dirName
= wxT("/");
410 m_showHidden
= FALSE
;
413 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
414 const wxString
&dirName
, const wxString
&wild
,
415 const wxPoint
&pos
, const wxSize
&size
,
416 long style
, const wxValidator
&validator
, const wxString
&name
) :
417 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
419 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
420 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
422 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
426 m_showHidden
= FALSE
;
430 void wxFileCtrl::ChangeToListMode()
432 SetSingleStyle( wxLC_LIST
);
436 void wxFileCtrl::ChangeToReportMode()
438 SetSingleStyle( wxLC_REPORT
);
442 void wxFileCtrl::ChangeToIconMode()
444 SetSingleStyle( wxLC_ICON
);
448 void wxFileCtrl::ShowHidden( bool show
)
454 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
457 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
458 fd
->MakeItem( item
);
459 long my_style
= GetWindowStyleFlag();
460 if (my_style
& wxLC_REPORT
)
462 ret
= InsertItem( item
);
463 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
465 else if (my_style
& wxLC_LIST
)
467 ret
= InsertItem( item
);
472 void wxFileCtrl::Update()
474 long my_style
= GetWindowStyleFlag();
475 int name_col_width
= 0;
476 if (my_style
& wxLC_REPORT
)
478 if (GetColumnCount() > 0)
479 name_col_width
= GetColumnWidth( 0 );
483 if (my_style
& wxLC_REPORT
)
485 if (name_col_width
< 140) name_col_width
= 140;
486 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
487 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
488 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
489 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
490 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
492 wxFileData
*fd
= (wxFileData
*) NULL
;
497 if (m_dirName
!= wxT("/"))
499 wxString
p( wxPathOnly(m_dirName
) );
500 if (p
.IsEmpty()) p
= wxT("/");
501 fd
= new wxFileData( wxT(".."), p
);
506 wxString res
= m_dirName
+ wxT("/*");
507 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
510 res
= wxFileNameFromPath( f
);
511 fd
= new wxFileData( res
, f
);
512 wxString s
= fd
->GetName();
513 if (m_showHidden
|| (s
[0] != wxT('.')))
518 f
= wxFindNextFile();
521 res
= m_dirName
+ wxT("/") + m_wild
;
522 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
525 res
= wxFileNameFromPath( f
);
526 fd
= new wxFileData( res
, f
);
527 wxString s
= fd
->GetName();
528 if (m_showHidden
|| (s
[0] != wxT('.')))
533 f
= wxFindNextFile();
536 SortItems( ListCompare
, 0 );
538 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
539 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
540 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
543 void wxFileCtrl::SetWild( const wxString
&wild
)
549 void wxFileCtrl::MakeDir()
551 wxString
new_name( wxT("NewName") );
552 wxString
path( m_dirName
);
555 if (wxFileExists(path
))
557 // try NewName0, NewName1 etc.
560 new_name
= _("NewName");
562 num
.Printf( wxT("%d"), i
);
569 } while (wxFileExists(path
));
575 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
580 wxFileData
*fd
= new wxFileData( new_name
, path
);
584 long id
= Add( fd
, item
);
588 SortItems( ListCompare
, 0 );
589 id
= FindItem( 0, (long)fd
);
595 void wxFileCtrl::GoToParentDir()
597 if (m_dirName
!= wxT("/"))
599 wxString
fname( wxFileNameFromPath(m_dirName
) );
600 m_dirName
= wxPathOnly( m_dirName
);
601 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
603 long id
= FindItem( 0, fname
);
606 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
612 void wxFileCtrl::GoToHomeDir()
614 wxString s
= wxGetUserHome( wxString() );
617 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
621 void wxFileCtrl::GoToDir( const wxString
&dir
)
625 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
629 void wxFileCtrl::GetDir( wxString
&dir
)
634 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
636 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
640 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
642 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
645 if ((event
.GetLabel().IsEmpty()) ||
646 (event
.GetLabel() == _(".")) ||
647 (event
.GetLabel() == _("..")) ||
648 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
650 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
656 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
657 new_name
+= wxT("/");
658 new_name
+= event
.GetLabel();
662 if (wxFileExists(new_name
))
664 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
669 if (wxRenameFile(fd
->GetFullName(),new_name
))
671 fd
->SetNewName( new_name
, event
.GetLabel() );
672 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
673 EnsureVisible( event
.GetItem() );
677 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
683 //-----------------------------------------------------------------------------
685 //-----------------------------------------------------------------------------
687 #define ID_LIST_MODE wxID_FILEDLGG
688 #define ID_REPORT_MODE wxID_FILEDLGG + 1
689 #define ID_UP_DIR wxID_FILEDLGG + 5
690 #define ID_PARENT_DIR wxID_FILEDLGG + 6
691 #define ID_NEW_DIR wxID_FILEDLGG + 7
692 #define ID_CHOICE wxID_FILEDLGG + 8
693 #define ID_TEXT wxID_FILEDLGG + 9
694 #define ID_LIST_CTRL wxID_FILEDLGG + 10
695 #define ID_ACTIVATED wxID_FILEDLGG + 11
696 #define ID_CHECK wxID_FILEDLGG + 12
698 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
700 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
701 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
702 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
703 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
704 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
705 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
706 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
707 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
708 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
709 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
710 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
711 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
714 long wxFileDialog::m_lastViewStyle
= wxLC_LIST
;
715 bool wxFileDialog::m_lastShowHidden
= FALSE
;
717 wxFileDialog::wxFileDialog(wxWindow
*parent
,
718 const wxString
& message
,
719 const wxString
& defaultDir
,
720 const wxString
& defaultFile
,
721 const wxString
& wildCard
,
723 const wxPoint
& pos
) :
724 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
729 m_dialogStyle
= style
;
731 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
732 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
733 m_dialogStyle
|= wxOPEN
;
736 if ((m_dir
.IsEmpty()) || (m_dir
== wxT(".")))
739 m_dir
= getcwd( buf
, sizeof(buf
) );
743 m_path
+= defaultFile
;
744 m_fileName
= defaultFile
;
745 m_wildCard
= wildCard
;
747 m_filterExtension
= wxEmptyString
;
749 // interpret wildcards
751 if (m_wildCard
.IsEmpty())
752 m_wildCard
= _("All files (*)|*");
754 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
756 wxString firstWildText
;
757 if (tokens
.CountTokens() == 1)
759 firstWildText
= tokens
.GetNextToken();
760 firstWild
= firstWildText
;
764 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
765 firstWildText
= tokens
.GetNextToken();
766 firstWild
= tokens
.GetNextToken();
768 if ( firstWild
.Left( 2 ) == wxT("*.") )
769 m_filterExtension
= firstWild
.Mid( 1 );
770 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
774 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
776 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
780 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
782 but
->SetToolTip( _("View files as a list view") );
784 buttonsizer
->Add( but
, 0, wxALL
, 5 );
786 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
788 but
->SetToolTip( _("View files as a detailed view") );
790 buttonsizer
->Add( but
, 0, wxALL
, 5 );
792 buttonsizer
->Add( 30, 5, 1 );
794 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
796 but
->SetToolTip( _("Go to parent directory") );
798 buttonsizer
->Add( but
, 0, wxALL
, 5 );
800 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
802 but
->SetToolTip( _("Go to home directory") );
804 buttonsizer
->Add( but
, 0, wxALL
, 5);
806 buttonsizer
->Add( 20, 20 );
808 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
810 but
->SetToolTip( _("Create new directory") );
812 buttonsizer
->Add( but
, 0, wxALL
, 5 );
814 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
816 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
817 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
818 m_static
= new wxStaticText( this, -1, m_dir
);
819 staticsizer
->Add( m_static
, 1 );
820 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
822 if (m_dialogStyle
& wxMULTIPLE
)
823 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
824 wxSize(440,180), m_lastViewStyle
| wxSUNKEN_BORDER
);
826 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
827 wxSize(440,180), m_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
828 m_list
-> ShowHidden(m_lastShowHidden
);
829 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
831 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
832 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
833 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
834 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
835 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
837 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
838 m_choice
= new wxChoice( this, ID_CHOICE
);
839 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
840 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
841 m_check
->SetValue( m_lastShowHidden
);
842 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
843 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
844 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
846 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
847 while (tokens
.HasMoreTokens())
849 firstWildText
= tokens
.GetNextToken();
850 firstWild
= tokens
.GetNextToken();
851 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
853 m_choice
->SetSelection( 0 );
855 SetAutoLayout( TRUE
);
856 SetSizer( mainsizer
);
858 mainsizer
->Fit( this );
859 mainsizer
->SetSizeHints( this );
863 if (m_fileName
.IsEmpty())
871 wxFileDialog::~wxFileDialog()
875 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
877 int index
= (int)event
.GetInt();
878 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
879 m_list
->SetWild( *str
);
880 m_filterIndex
= index
;
881 if ( str
-> Left( 2 ) == wxT("*.") )
883 m_filterExtension
= str
-> Mid( 1 );
884 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
887 m_filterExtension
= wxEmptyString
;
890 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
892 m_list
->ShowHidden( (m_lastShowHidden
= event
.GetInt() != 0) );
895 void wxFileDialog::OnActivated( wxListEvent
&event
)
897 HandleAction( event
.m_item
.m_text
);
900 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
902 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
903 cevent
.SetEventObject( this );
904 GetEventHandler()->ProcessEvent( cevent
);
907 void wxFileDialog::OnSelected( wxListEvent
&event
)
909 if (FindFocus() != m_list
) return;
911 wxString
filename( event
.m_item
.m_text
);
912 if (filename
== wxT("..")) return;
915 m_list
->GetDir( dir
);
916 if (dir
!= wxT("/")) dir
+= wxT("/");
918 if (wxDirExists(dir
)) return;
920 m_text
->SetValue( filename
);
923 void wxFileDialog::HandleAction( const wxString
&fn
)
925 wxString
filename( fn
);
927 m_list
->GetDir( dir
);
928 if (filename
.IsEmpty()) return;
929 if (filename
== wxT(".")) return;
931 if (filename
== wxT(".."))
933 m_list
->GoToParentDir();
935 m_list
->GetDir( dir
);
936 m_static
->SetLabel( dir
);
940 if (filename
== wxT("~"))
942 m_list
->GoToHomeDir();
944 m_list
->GetDir( dir
);
945 m_static
->SetLabel( dir
);
949 if (filename
[0] == wxT('~'))
951 filename
.Remove( 0, 1 );
952 wxString
tmp( wxGetUserHome() );
958 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
959 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
961 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
963 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
966 m_list
->SetWild( filename
);
970 if (dir
!= wxT("/")) dir
+= wxT("/");
971 if (filename
[0] != wxT('/'))
977 if (wxDirExists(filename
))
979 m_list
->GoToDir( filename
);
980 m_list
->GetDir( dir
);
981 m_static
->SetLabel( dir
);
986 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
988 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
989 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
990 filename
<< m_filterExtension
;
991 if (wxFileExists( filename
))
994 msg
.Printf( _("File '%s' already exists, do you really want to "
995 "overwrite it?"), filename
.c_str() );
997 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1001 else if ( m_dialogStyle
& wxOPEN
)
1003 if ( !wxFileExists( filename
) )
1004 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1005 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1006 filename
<< m_filterExtension
;
1008 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1010 if ( !wxFileExists( filename
) )
1012 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1018 SetPath( filename
);
1020 wxCommandEvent event
;
1021 wxDialog::OnOK(event
);
1024 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1026 HandleAction( m_text
->GetValue() );
1029 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1031 m_list
->ChangeToListMode();
1032 m_lastViewStyle
= wxLC_LIST
;
1036 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1038 m_list
->ChangeToReportMode();
1039 m_lastViewStyle
= wxLC_REPORT
;
1043 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1045 m_list
->GoToParentDir();
1048 m_list
->GetDir( dir
);
1049 m_static
->SetLabel( dir
);
1052 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1054 m_list
->GoToHomeDir();
1057 m_list
->GetDir( dir
);
1058 m_static
->SetLabel( dir
);
1061 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1066 void wxFileDialog::SetPath( const wxString
& path
)
1068 // not only set the full path but also update filename and dir
1073 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1076 m_fileName
+= wxT(".");
1082 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1085 if (m_list
->GetSelectedItemCount() == 0)
1087 paths
.Add( GetPath() );
1091 paths
.Alloc( m_list
->GetSelectedItemCount() );
1094 m_list
->GetDir( dir
);
1095 if (dir
!= wxT("/")) dir
+= wxT("/");
1098 item
.m_mask
= wxLIST_MASK_TEXT
;
1100 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1101 while ( item
.m_itemId
!= -1 ) {
1102 m_list
->GetItem( item
);
1103 paths
.Add( dir
+ item
.m_text
);
1104 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1105 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1109 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1112 if (m_list
->GetSelectedItemCount() == 0)
1114 files
.Add( GetFilename() );
1117 files
.Alloc( m_list
->GetSelectedItemCount() );
1120 item
.m_mask
= wxLIST_MASK_TEXT
;
1122 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1123 while ( item
.m_itemId
!= -1 ) {
1124 m_list
->GetItem( item
);
1125 files
.Add( item
.m_text
);
1126 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1127 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1133 // ----------------------------------------------------------------------------
1135 // ----------------------------------------------------------------------------
1138 wxFileSelectorEx(const wxChar
*message
,
1139 const wxChar
*default_path
,
1140 const wxChar
*default_filename
,
1141 int *WXUNUSED(indexDefaultExtension
),
1142 const wxChar
*wildcard
,
1147 // TODO: implement this somehow
1148 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1149 wildcard
, flags
, parent
, x
, y
);
1152 wxString
wxFileSelector( const wxChar
*title
,
1153 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1154 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1155 wxWindow
*parent
, int x
, int y
)
1158 if ( defaultExtension
&& !filter
)
1159 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1163 wxString defaultDirString
;
1165 defaultDirString
= defaultDir
;
1167 wxString defaultFilenameString
;
1168 if (defaultFileName
)
1169 defaultFilenameString
= defaultFileName
;
1171 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1173 if ( fileDialog
.ShowModal() == wxID_OK
)
1175 return fileDialog
.GetPath();
1179 return wxEmptyString
;
1183 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
1185 wxChar
*ext
= (wxChar
*)extension
;
1188 wxString str
= _("Load %s file");
1189 wxSprintf(prompt
, str
, what
);
1191 if (*ext
== wxT('.')) ext
++;
1193 wxSprintf(wild
, wxT("*.%s"), ext
);
1195 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1198 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1201 wxChar
*ext
= (wxChar
*)extension
;
1204 wxString str
= _("Save %s file");
1205 wxSprintf(prompt
, str
, what
);
1207 if (*ext
== wxT('.')) ext
++;
1209 wxSprintf(wild
, wxT("*.%s"), ext
);
1211 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1219 // A module to allow icons table cleanup
1221 class wxFileDialogGenericModule
: public wxModule
1223 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1225 wxFileDialogGenericModule() {}
1226 bool OnInit() { return TRUE
; }
1227 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1230 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)