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"
59 static char * folder_xpm
[] = {
60 /* width height ncolors chars_per_pixel */
88 // ----------------------------------------------------------------------------
89 // private classes - icons list management
90 // ----------------------------------------------------------------------------
92 class wxFileIconEntry
: public wxObject
95 wxFileIconEntry(int i
) { id
= i
; }
101 class wxFileIconsTable
107 int GetIconID(const wxString
& extension
);
108 wxImageList
*GetImageList() { return &m_ImageList
; }
111 wxImageList m_ImageList
;
112 wxHashTable m_HashTable
;
113 wxMimeTypesManager m_Mime
;
116 static wxFileIconsTable
*g_IconsTable
= NULL
;
119 wxFileIconsTable::wxFileIconsTable() :
121 m_HashTable(wxKEY_STRING
),
124 m_HashTable
.DeleteContents(TRUE
);
125 m_ImageList
.Add(wxBitmap(folder_xpm
));
130 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
132 wxImage
small(16, 16);
133 unsigned char *p1
, *p2
, *ps
;
134 unsigned char mr
= img
.GetMaskRed(), mg
= img
.GetMaskGreen(), mb
= img
.GetMaskBlue();
137 unsigned sr
, sg
, sb
, smask
;
139 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
140 small
.SetMaskColour(mr
, mr
, mr
);
142 for (y
= 0; y
< 16; y
++)
144 for (x
= 0; x
< 16; x
++)
146 sr
= sg
= sb
= smask
= 0;
147 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
148 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
151 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
152 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
155 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
156 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
159 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
160 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
165 ps
[0] = ps
[1] = ps
[2] = mr
;
167 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
170 p1
+= 32 * 3, p2
+= 32 * 3;
173 return small
.ConvertToBitmap();
177 int wxFileIconsTable::GetIconID(const wxString
& extension
)
179 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
181 if (entry
) return (entry
-> id
);
183 wxFileType
*ft
= m_Mime
.GetFileTypeFromExtension(extension
);
185 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)))
187 int newid
= GetIconID(wxT("txt"));
188 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
194 int id
= m_ImageList
.GetImageCount();
195 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
196 m_ImageList
.Add(img
.ConvertToBitmap());
199 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
201 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
203 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
209 // ----------------------------------------------------------------------------
211 // ----------------------------------------------------------------------------
214 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
216 wxFileData
*fd1
= (wxFileData
*)data1
;
217 wxFileData
*fd2
= (wxFileData
*)data2
;
218 if (fd1
->GetName() == wxT("..")) return -1;
219 if (fd2
->GetName() == wxT("..")) return 1;
220 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
221 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
222 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
225 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
229 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
231 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
237 stat( m_fileName
.fn_str(), &buff
);
241 lstat( m_fileName
.fn_str(), &lbuff
);
242 m_isLink
= S_ISLNK( lbuff
.st_mode
);
243 struct tm
*t
= localtime( &lbuff
.st_mtime
);
246 struct tm
*t
= localtime( &buff
.st_mtime
);
249 // struct passwd *user = getpwuid( buff.st_uid );
250 // struct group *grp = getgrgid( buff.st_gid );
252 m_isDir
= S_ISDIR( buff
.st_mode
);
253 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
255 m_size
= buff
.st_size
;
258 m_minute
= t
->tm_min
;
259 m_month
= t
->tm_mon
+1;
264 m_permissions
.sprintf( wxT("%c%c%c"),
265 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
266 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
267 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
270 wxString
wxFileData::GetName() const
275 wxString
wxFileData::GetFullName() const
280 wxString
wxFileData::GetHint() const
282 wxString s
= m_fileName
;
284 if (m_isDir
) s
+= _("<DIR> ");
285 else if (m_isLink
) s
+= _("<LINK> ");
288 s
+= LongToString( m_size
);
291 s
+= IntToString( m_day
);
293 s
+= IntToString( m_month
);
295 s
+= IntToString( m_year
);
297 s
+= IntToString( m_hour
);
299 s
+= IntToString( m_minute
);
305 wxString
wxFileData::GetEntry( int num
)
317 if (m_isDir
) s
= _("<DIR>");
318 else if (m_isLink
) s
= _("<LINK>");
319 else s
= LongToString( m_size
);
324 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
325 s
+= IntToString( m_day
);
327 if (m_month
< 10) s
+= wxT("0");
328 s
+= IntToString( m_month
);
330 s
+= IntToString( m_year
);
335 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
336 s
+= IntToString( m_hour
);
338 if (m_minute
< 10) s
+= wxT("0");
339 s
+= IntToString( m_minute
);
352 bool wxFileData::IsDir()
357 bool wxFileData::IsExe()
362 bool wxFileData::IsLink()
367 long wxFileData::GetSize()
372 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
378 void wxFileData::MakeItem( wxListItem
&item
)
380 item
.m_text
= m_name
;
381 item
.ClearAttributes();
382 if (IsExe()) item
.SetTextColour(*wxRED
);
383 if (IsDir()) item
.SetTextColour(*wxBLUE
);
387 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
388 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
394 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
395 item
.SetTextColour(*dg
);
397 item
.m_data
= (long)this;
400 //-----------------------------------------------------------------------------
402 //-----------------------------------------------------------------------------
404 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
406 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
407 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
408 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
413 wxFileCtrl::wxFileCtrl()
415 m_dirName
= wxT("/");
416 m_showHidden
= FALSE
;
419 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
420 const wxString
&dirName
, const wxString
&wild
,
421 const wxPoint
&pos
, const wxSize
&size
,
422 long style
, const wxValidator
&validator
, const wxString
&name
) :
423 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
425 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
426 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
428 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
432 m_showHidden
= FALSE
;
436 void wxFileCtrl::ChangeToListMode()
438 SetSingleStyle( wxLC_LIST
);
442 void wxFileCtrl::ChangeToReportMode()
444 SetSingleStyle( wxLC_REPORT
);
448 void wxFileCtrl::ChangeToIconMode()
450 SetSingleStyle( wxLC_ICON
);
454 void wxFileCtrl::ShowHidden( bool show
)
460 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
463 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
464 fd
->MakeItem( item
);
465 long my_style
= GetWindowStyleFlag();
466 if (my_style
& wxLC_REPORT
)
468 ret
= InsertItem( item
);
469 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
471 else if (my_style
& wxLC_LIST
)
473 ret
= InsertItem( item
);
478 void wxFileCtrl::Update()
480 long my_style
= GetWindowStyleFlag();
481 int name_col_width
= 0;
482 if (my_style
& wxLC_REPORT
)
484 if (GetColumnCount() > 0)
485 name_col_width
= GetColumnWidth( 0 );
489 if (my_style
& wxLC_REPORT
)
491 if (name_col_width
< 140) name_col_width
= 140;
492 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
493 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
494 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
495 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
496 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
498 wxFileData
*fd
= (wxFileData
*) NULL
;
503 if (m_dirName
!= wxT("/"))
505 wxString
p( wxPathOnly(m_dirName
) );
506 if (p
.IsEmpty()) p
= wxT("/");
507 fd
= new wxFileData( wxT(".."), p
);
512 wxString res
= m_dirName
+ wxT("/*");
513 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
516 res
= wxFileNameFromPath( f
);
517 fd
= new wxFileData( res
, f
);
518 wxString s
= fd
->GetName();
519 if (m_showHidden
|| (s
[0] != wxT('.')))
524 f
= wxFindNextFile();
527 res
= m_dirName
+ wxT("/") + m_wild
;
528 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
531 res
= wxFileNameFromPath( f
);
532 fd
= new wxFileData( res
, f
);
533 wxString s
= fd
->GetName();
534 if (m_showHidden
|| (s
[0] != wxT('.')))
539 f
= wxFindNextFile();
542 SortItems( ListCompare
, 0 );
544 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
545 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
546 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
549 void wxFileCtrl::SetWild( const wxString
&wild
)
555 void wxFileCtrl::MakeDir()
557 wxString
new_name( wxT("NewName") );
558 wxString
path( m_dirName
);
561 if (wxFileExists(path
))
563 // try NewName0, NewName1 etc.
566 new_name
= _("NewName");
568 num
.Printf( wxT("%d"), i
);
575 } while (wxFileExists(path
));
581 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
586 wxFileData
*fd
= new wxFileData( new_name
, path
);
590 long id
= Add( fd
, item
);
594 SortItems( ListCompare
, 0 );
595 id
= FindItem( 0, (long)fd
);
601 void wxFileCtrl::GoToParentDir()
603 if (m_dirName
!= wxT("/"))
605 wxString
fname( wxFileNameFromPath(m_dirName
) );
606 m_dirName
= wxPathOnly( m_dirName
);
607 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
609 long id
= FindItem( 0, fname
);
612 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
618 void wxFileCtrl::GoToHomeDir()
620 wxString s
= wxGetUserHome( wxString() );
623 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
627 void wxFileCtrl::GoToDir( const wxString
&dir
)
631 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
635 void wxFileCtrl::GetDir( wxString
&dir
)
640 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
642 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
646 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
648 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
651 if ((event
.GetLabel().IsEmpty()) ||
652 (event
.GetLabel() == _(".")) ||
653 (event
.GetLabel() == _("..")) ||
654 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
656 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
662 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
663 new_name
+= wxT("/");
664 new_name
+= event
.GetLabel();
668 if (wxFileExists(new_name
))
670 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
675 if (wxRenameFile(fd
->GetFullName(),new_name
))
677 fd
->SetNewName( new_name
, event
.GetLabel() );
678 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
679 EnsureVisible( event
.GetItem() );
683 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
689 //-----------------------------------------------------------------------------
691 //-----------------------------------------------------------------------------
693 #define ID_LIST_MODE wxID_FILEDLGG
694 #define ID_REPORT_MODE wxID_FILEDLGG + 1
695 #define ID_UP_DIR wxID_FILEDLGG + 5
696 #define ID_PARENT_DIR wxID_FILEDLGG + 6
697 #define ID_NEW_DIR wxID_FILEDLGG + 7
698 #define ID_CHOICE wxID_FILEDLGG + 8
699 #define ID_TEXT wxID_FILEDLGG + 9
700 #define ID_LIST_CTRL wxID_FILEDLGG + 10
701 #define ID_ACTIVATED wxID_FILEDLGG + 11
702 #define ID_CHECK wxID_FILEDLGG + 12
704 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
706 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
707 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
708 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
709 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
710 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
711 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
712 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
713 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
714 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
715 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
716 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
717 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
720 long wxFileDialog::m_lastViewStyle
= wxLC_LIST
;
721 bool wxFileDialog::m_lastShowHidden
= FALSE
;
723 wxFileDialog::wxFileDialog(wxWindow
*parent
,
724 const wxString
& message
,
725 const wxString
& defaultDir
,
726 const wxString
& defaultFile
,
727 const wxString
& wildCard
,
729 const wxPoint
& pos
) :
730 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
735 m_dialogStyle
= style
;
737 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
738 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
739 m_dialogStyle
|= wxOPEN
;
742 if ((m_dir
.IsEmpty()) || (m_dir
== wxT(".")))
745 m_dir
= getcwd( buf
, sizeof(buf
) );
749 m_path
+= defaultFile
;
750 m_fileName
= defaultFile
;
751 m_wildCard
= wildCard
;
753 m_filterExtension
= wxEmptyString
;
755 // interpret wildcards
757 if (m_wildCard
.IsEmpty())
758 m_wildCard
= _("All files (*)|*");
760 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
762 wxString firstWildText
;
763 if (tokens
.CountTokens() == 1)
765 firstWildText
= tokens
.GetNextToken();
766 firstWild
= firstWildText
;
770 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
771 firstWildText
= tokens
.GetNextToken();
772 firstWild
= tokens
.GetNextToken();
774 if ( firstWild
.Left( 2 ) == wxT("*.") )
775 m_filterExtension
= firstWild
.Mid( 1 );
776 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
780 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
782 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
786 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
788 but
->SetToolTip( _("View files as a list view") );
790 buttonsizer
->Add( but
, 0, wxALL
, 5 );
792 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
794 but
->SetToolTip( _("View files as a detailed view") );
796 buttonsizer
->Add( but
, 0, wxALL
, 5 );
798 buttonsizer
->Add( 30, 5, 1 );
800 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
802 but
->SetToolTip( _("Go to parent directory") );
804 buttonsizer
->Add( but
, 0, wxALL
, 5 );
806 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
808 but
->SetToolTip( _("Go to home directory") );
810 buttonsizer
->Add( but
, 0, wxALL
, 5);
812 buttonsizer
->Add( 20, 20 );
814 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
816 but
->SetToolTip( _("Create new directory") );
818 buttonsizer
->Add( but
, 0, wxALL
, 5 );
820 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
822 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
823 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
824 m_static
= new wxStaticText( this, -1, m_dir
);
825 staticsizer
->Add( m_static
, 1 );
826 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
828 if (m_dialogStyle
& wxMULTIPLE
)
829 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
830 wxSize(440,180), m_lastViewStyle
| wxSUNKEN_BORDER
);
832 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
833 wxSize(440,180), m_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
834 m_list
-> ShowHidden(m_lastShowHidden
);
835 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
837 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
838 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
839 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
840 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
841 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
843 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
844 m_choice
= new wxChoice( this, ID_CHOICE
);
845 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
846 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
847 m_check
->SetValue( m_lastShowHidden
);
848 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
849 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
850 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
852 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
853 while (tokens
.HasMoreTokens())
855 firstWildText
= tokens
.GetNextToken();
856 firstWild
= tokens
.GetNextToken();
857 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
859 m_choice
->SetSelection( 0 );
861 SetAutoLayout( TRUE
);
862 SetSizer( mainsizer
);
864 mainsizer
->Fit( this );
865 mainsizer
->SetSizeHints( this );
869 if (m_fileName
.IsEmpty())
877 wxFileDialog::~wxFileDialog()
881 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
883 int index
= (int)event
.GetInt();
884 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
885 m_list
->SetWild( *str
);
886 m_filterIndex
= index
;
887 if ( str
-> Left( 2 ) == wxT("*.") )
889 m_filterExtension
= str
-> Mid( 1 );
890 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
893 m_filterExtension
= wxEmptyString
;
896 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
898 m_list
->ShowHidden( (m_lastShowHidden
= event
.GetInt() != 0) );
901 void wxFileDialog::OnActivated( wxListEvent
&event
)
903 HandleAction( event
.m_item
.m_text
);
906 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
908 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
909 cevent
.SetEventObject( this );
910 GetEventHandler()->ProcessEvent( cevent
);
913 void wxFileDialog::OnSelected( wxListEvent
&event
)
915 if (FindFocus() != m_list
) return;
917 wxString
filename( event
.m_item
.m_text
);
918 if (filename
== wxT("..")) return;
921 m_list
->GetDir( dir
);
922 if (dir
!= wxT("/")) dir
+= wxT("/");
924 if (wxDirExists(dir
)) return;
926 m_text
->SetValue( filename
);
929 void wxFileDialog::HandleAction( const wxString
&fn
)
931 wxString
filename( fn
);
933 m_list
->GetDir( dir
);
934 if (filename
.IsEmpty()) return;
935 if (filename
== wxT(".")) return;
937 if (filename
== wxT(".."))
939 m_list
->GoToParentDir();
941 m_list
->GetDir( dir
);
942 m_static
->SetLabel( dir
);
946 if (filename
== wxT("~"))
948 m_list
->GoToHomeDir();
950 m_list
->GetDir( dir
);
951 m_static
->SetLabel( dir
);
955 if (filename
[0] == wxT('~'))
957 filename
.Remove( 0, 1 );
958 wxString
tmp( wxGetUserHome() );
964 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
965 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
967 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
969 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
972 m_list
->SetWild( filename
);
976 if (dir
!= wxT("/")) dir
+= wxT("/");
977 if (filename
[0] != wxT('/'))
983 if (wxDirExists(filename
))
985 m_list
->GoToDir( filename
);
986 m_list
->GetDir( dir
);
987 m_static
->SetLabel( dir
);
992 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
994 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
995 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
996 filename
<< m_filterExtension
;
997 if (wxFileExists( filename
))
1000 msg
.Printf( _("File '%s' already exists, do you really want to "
1001 "overwrite it?"), filename
.c_str() );
1003 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1007 else if ( m_dialogStyle
& wxOPEN
)
1009 if ( !wxFileExists( filename
) )
1010 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1011 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1012 filename
<< m_filterExtension
;
1014 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1016 if ( !wxFileExists( filename
) )
1018 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1024 SetPath( filename
);
1026 wxCommandEvent event
;
1027 wxDialog::OnOK(event
);
1030 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1032 HandleAction( m_text
->GetValue() );
1035 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1037 m_list
->ChangeToListMode();
1038 m_lastViewStyle
= wxLC_LIST
;
1042 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1044 m_list
->ChangeToReportMode();
1045 m_lastViewStyle
= wxLC_REPORT
;
1049 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1051 m_list
->GoToParentDir();
1054 m_list
->GetDir( dir
);
1055 m_static
->SetLabel( dir
);
1058 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1060 m_list
->GoToHomeDir();
1063 m_list
->GetDir( dir
);
1064 m_static
->SetLabel( dir
);
1067 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1072 void wxFileDialog::SetPath( const wxString
& path
)
1074 // not only set the full path but also update filename and dir
1079 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1082 m_fileName
+= wxT(".");
1088 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1091 if (m_list
->GetSelectedItemCount() == 0)
1093 paths
.Add( GetPath() );
1097 paths
.Alloc( m_list
->GetSelectedItemCount() );
1100 m_list
->GetDir( dir
);
1101 if (dir
!= wxT("/")) dir
+= wxT("/");
1104 item
.m_mask
= wxLIST_MASK_TEXT
;
1106 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1107 while ( item
.m_itemId
!= -1 ) {
1108 m_list
->GetItem( item
);
1109 paths
.Add( dir
+ item
.m_text
);
1110 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1111 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1115 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1118 if (m_list
->GetSelectedItemCount() == 0)
1120 files
.Add( GetFilename() );
1123 files
.Alloc( m_list
->GetSelectedItemCount() );
1126 item
.m_mask
= wxLIST_MASK_TEXT
;
1128 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1129 while ( item
.m_itemId
!= -1 ) {
1130 m_list
->GetItem( item
);
1131 files
.Add( item
.m_text
);
1132 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1133 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1139 // ----------------------------------------------------------------------------
1141 // ----------------------------------------------------------------------------
1144 wxFileSelectorEx(const wxChar
*message
,
1145 const wxChar
*default_path
,
1146 const wxChar
*default_filename
,
1147 int *WXUNUSED(indexDefaultExtension
),
1148 const wxChar
*wildcard
,
1153 // TODO: implement this somehow
1154 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1155 wildcard
, flags
, parent
, x
, y
);
1158 wxString
wxFileSelector( const wxChar
*title
,
1159 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1160 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1161 wxWindow
*parent
, int x
, int y
)
1164 if ( defaultExtension
&& !filter
)
1165 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1169 wxString defaultDirString
;
1171 defaultDirString
= defaultDir
;
1173 wxString defaultFilenameString
;
1174 if (defaultFileName
)
1175 defaultFilenameString
= defaultFileName
;
1177 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1179 if ( fileDialog
.ShowModal() == wxID_OK
)
1181 return fileDialog
.GetPath();
1185 return wxEmptyString
;
1189 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
1191 wxChar
*ext
= (wxChar
*)extension
;
1194 wxString str
= _("Load %s file");
1195 wxSprintf(prompt
, str
, what
);
1197 if (*ext
== wxT('.')) ext
++;
1199 wxSprintf(wild
, wxT("*.%s"), ext
);
1201 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1204 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1207 wxChar
*ext
= (wxChar
*)extension
;
1210 wxString str
= _("Save %s file");
1211 wxSprintf(prompt
, str
, what
);
1213 if (*ext
== wxT('.')) ext
++;
1215 wxSprintf(wild
, wxT("*.%s"), ext
);
1217 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1225 // A module to allow icons table cleanup
1227 class wxFileDialogGenericModule
: public wxModule
1229 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1231 wxFileDialogGenericModule() {}
1232 bool OnInit() { return TRUE
; }
1233 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1236 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)