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());
198 m_ImageList
.Add(CreateAntialiasedBitmap(img
.Scale(32, 32)));
199 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
208 // ----------------------------------------------------------------------------
210 // ----------------------------------------------------------------------------
213 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
215 wxFileData
*fd1
= (wxFileData
*)data1
;
216 wxFileData
*fd2
= (wxFileData
*)data2
;
217 if (fd1
->GetName() == wxT("..")) return -1;
218 if (fd2
->GetName() == wxT("..")) return 1;
219 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
220 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
221 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
224 //-----------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------
228 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
230 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
236 stat( m_fileName
.fn_str(), &buff
);
240 lstat( m_fileName
.fn_str(), &lbuff
);
241 m_isLink
= S_ISLNK( lbuff
.st_mode
);
242 struct tm
*t
= localtime( &lbuff
.st_mtime
);
245 struct tm
*t
= localtime( &buff
.st_mtime
);
248 // struct passwd *user = getpwuid( buff.st_uid );
249 // struct group *grp = getgrgid( buff.st_gid );
251 m_isDir
= S_ISDIR( buff
.st_mode
);
252 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
254 m_size
= buff
.st_size
;
257 m_minute
= t
->tm_min
;
258 m_month
= t
->tm_mon
+1;
263 m_permissions
.sprintf( wxT("%c%c%c"),
264 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
265 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
266 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
269 wxString
wxFileData::GetName() const
274 wxString
wxFileData::GetFullName() const
279 wxString
wxFileData::GetHint() const
281 wxString s
= m_fileName
;
283 if (m_isDir
) s
+= _("<DIR> ");
284 else if (m_isLink
) s
+= _("<LINK> ");
287 s
+= LongToString( m_size
);
290 s
+= IntToString( m_day
);
292 s
+= IntToString( m_month
);
294 s
+= IntToString( m_year
);
296 s
+= IntToString( m_hour
);
298 s
+= IntToString( m_minute
);
304 wxString
wxFileData::GetEntry( int num
)
316 if (m_isDir
) s
= _("<DIR>");
317 else if (m_isLink
) s
= _("<LINK>");
318 else s
= LongToString( m_size
);
323 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
324 s
+= IntToString( m_day
);
326 if (m_month
< 10) s
+= wxT("0");
327 s
+= IntToString( m_month
);
329 s
+= IntToString( m_year
);
334 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
335 s
+= IntToString( m_hour
);
337 if (m_minute
< 10) s
+= wxT("0");
338 s
+= IntToString( m_minute
);
351 bool wxFileData::IsDir()
356 bool wxFileData::IsExe()
361 bool wxFileData::IsLink()
366 long wxFileData::GetSize()
371 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
377 void wxFileData::MakeItem( wxListItem
&item
)
379 item
.m_text
= m_name
;
380 item
.ClearAttributes();
381 if (IsExe()) item
.SetTextColour(*wxRED
);
382 if (IsDir()) item
.SetTextColour(*wxBLUE
);
386 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
387 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
393 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
394 item
.SetTextColour(*dg
);
396 item
.m_data
= (long)this;
399 //-----------------------------------------------------------------------------
401 //-----------------------------------------------------------------------------
403 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
405 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
406 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
407 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
412 wxFileCtrl::wxFileCtrl()
414 m_dirName
= wxT("/");
415 m_showHidden
= FALSE
;
418 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
419 const wxString
&dirName
, const wxString
&wild
,
420 const wxPoint
&pos
, const wxSize
&size
,
421 long style
, const wxValidator
&validator
, const wxString
&name
) :
422 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
424 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
425 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
427 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
431 m_showHidden
= FALSE
;
435 void wxFileCtrl::ChangeToListMode()
437 SetSingleStyle( wxLC_LIST
);
441 void wxFileCtrl::ChangeToReportMode()
443 SetSingleStyle( wxLC_REPORT
);
447 void wxFileCtrl::ChangeToIconMode()
449 SetSingleStyle( wxLC_ICON
);
453 void wxFileCtrl::ShowHidden( bool show
)
459 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
462 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
463 fd
->MakeItem( item
);
464 long my_style
= GetWindowStyleFlag();
465 if (my_style
& wxLC_REPORT
)
467 ret
= InsertItem( item
);
468 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
470 else if (my_style
& wxLC_LIST
)
472 ret
= InsertItem( item
);
477 void wxFileCtrl::Update()
480 long my_style
= GetWindowStyleFlag();
481 if (my_style
& wxLC_REPORT
)
483 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, 130 );
484 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
485 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
486 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
487 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
489 wxFileData
*fd
= (wxFileData
*) NULL
;
494 if (m_dirName
!= wxT("/"))
496 wxString
p( wxPathOnly(m_dirName
) );
497 if (p
.IsEmpty()) p
= wxT("/");
498 fd
= new wxFileData( wxT(".."), p
);
503 wxString res
= m_dirName
+ wxT("/*");
504 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
507 res
= wxFileNameFromPath( f
);
508 fd
= new wxFileData( res
, f
);
509 wxString s
= fd
->GetName();
510 if (m_showHidden
|| (s
[0] != wxT('.')))
515 f
= wxFindNextFile();
518 res
= m_dirName
+ wxT("/") + m_wild
;
519 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
522 res
= wxFileNameFromPath( f
);
523 fd
= new wxFileData( res
, f
);
524 wxString s
= fd
->GetName();
525 if (m_showHidden
|| (s
[0] != wxT('.')))
530 f
= wxFindNextFile();
533 SortItems( ListCompare
, 0 );
535 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
536 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
537 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
540 void wxFileCtrl::SetWild( const wxString
&wild
)
546 void wxFileCtrl::MakeDir()
548 wxString
new_name( wxT("NewName") );
549 wxString
path( m_dirName
);
552 if (wxFileExists(path
))
554 // try NewName0, NewName1 etc.
557 new_name
= _("NewName");
559 num
.Printf( wxT("%d"), i
);
566 } while (wxFileExists(path
));
572 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
577 wxFileData
*fd
= new wxFileData( new_name
, path
);
581 long id
= Add( fd
, item
);
585 SortItems( ListCompare
, 0 );
586 id
= FindItem( 0, (long)fd
);
592 void wxFileCtrl::GoToParentDir()
594 if (m_dirName
!= wxT("/"))
596 wxString
fname( wxFileNameFromPath(m_dirName
) );
597 m_dirName
= wxPathOnly( m_dirName
);
598 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
600 long id
= FindItem( 0, fname
);
603 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
609 void wxFileCtrl::GoToHomeDir()
611 wxString s
= wxGetUserHome( wxString() );
614 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
618 void wxFileCtrl::GoToDir( const wxString
&dir
)
622 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
626 void wxFileCtrl::GetDir( wxString
&dir
)
631 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
633 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
637 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
639 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
642 if ((event
.GetLabel().IsEmpty()) ||
643 (event
.GetLabel() == _(".")) ||
644 (event
.GetLabel() == _("..")) ||
645 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
647 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
653 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
654 new_name
+= wxT("/");
655 new_name
+= event
.GetLabel();
659 if (wxFileExists(new_name
))
661 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
666 if (wxRenameFile(fd
->GetFullName(),new_name
))
668 fd
->SetNewName( new_name
, event
.GetLabel() );
669 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
670 EnsureVisible( event
.GetItem() );
674 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
680 //-----------------------------------------------------------------------------
682 //-----------------------------------------------------------------------------
684 #define ID_LIST_MODE wxID_FILEDLGG
685 #define ID_REPORT_MODE wxID_FILEDLGG + 1
686 #define ID_UP_DIR wxID_FILEDLGG + 5
687 #define ID_PARENT_DIR wxID_FILEDLGG + 6
688 #define ID_NEW_DIR wxID_FILEDLGG + 7
689 #define ID_CHOICE wxID_FILEDLGG + 8
690 #define ID_TEXT wxID_FILEDLGG + 9
691 #define ID_LIST_CTRL wxID_FILEDLGG + 10
692 #define ID_ACTIVATED wxID_FILEDLGG + 11
693 #define ID_CHECK wxID_FILEDLGG + 12
695 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
697 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
698 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
699 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
700 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
701 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
702 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
703 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
704 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
705 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
706 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
707 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
708 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
711 long wxFileDialog::m_lastViewStyle
= wxLC_LIST
;
712 bool wxFileDialog::m_lastShowHidden
= FALSE
;
714 wxFileDialog::wxFileDialog(wxWindow
*parent
,
715 const wxString
& message
,
716 const wxString
& defaultDir
,
717 const wxString
& defaultFile
,
718 const wxString
& wildCard
,
720 const wxPoint
& pos
) :
721 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
726 m_dialogStyle
= style
;
728 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
729 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
730 m_dialogStyle
|= wxOPEN
;
733 if ((m_dir
.IsEmpty()) || (m_dir
== wxT(".")))
736 m_dir
= getcwd( buf
, sizeof(buf
) );
740 m_path
+= defaultFile
;
741 m_fileName
= defaultFile
;
742 m_wildCard
= wildCard
;
744 m_filterExtension
= wxEmptyString
;
746 // interpret wildcards
748 if (m_wildCard
.IsEmpty())
749 m_wildCard
= _("All files (*)|*");
751 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
753 wxString firstWildText
;
754 if (tokens
.CountTokens() == 1)
756 firstWildText
= tokens
.GetNextToken();
757 firstWild
= firstWildText
;
761 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
762 firstWildText
= tokens
.GetNextToken();
763 firstWild
= tokens
.GetNextToken();
765 if ( firstWild
.Left( 2 ) == wxT("*.") )
766 m_filterExtension
= firstWild
.Mid( 1 );
767 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
771 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
773 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
777 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
779 but
->SetToolTip( _("View files as a list view") );
781 buttonsizer
->Add( but
, 0, wxALL
, 5 );
783 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
785 but
->SetToolTip( _("View files as a detailed view") );
787 buttonsizer
->Add( but
, 0, wxALL
, 5 );
789 buttonsizer
->Add( 30, 5, 1 );
791 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
793 but
->SetToolTip( _("Go to parent directory") );
795 buttonsizer
->Add( but
, 0, wxALL
, 5 );
797 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
799 but
->SetToolTip( _("Go to home directory") );
801 buttonsizer
->Add( but
, 0, wxALL
, 5);
803 buttonsizer
->Add( 20, 20 );
805 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
807 but
->SetToolTip( _("Create new directory") );
809 buttonsizer
->Add( but
, 0, wxALL
, 5 );
811 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
813 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
814 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
815 m_static
= new wxStaticText( this, -1, m_dir
);
816 staticsizer
->Add( m_static
, 1 );
817 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
819 if (m_dialogStyle
& wxMULTIPLE
)
820 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
821 wxSize(440,180), m_lastViewStyle
| wxSUNKEN_BORDER
);
823 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
824 wxSize(440,180), m_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
825 m_list
-> ShowHidden(m_lastShowHidden
);
826 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
828 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
829 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
830 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
831 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
832 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
834 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
835 m_choice
= new wxChoice( this, ID_CHOICE
);
836 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
837 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
838 m_check
->SetValue( m_lastShowHidden
);
839 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
840 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
841 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
843 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
844 while (tokens
.HasMoreTokens())
846 firstWildText
= tokens
.GetNextToken();
847 firstWild
= tokens
.GetNextToken();
848 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
850 m_choice
->SetSelection( 0 );
852 SetAutoLayout( TRUE
);
853 SetSizer( mainsizer
);
855 mainsizer
->Fit( this );
856 mainsizer
->SetSizeHints( this );
860 if (m_fileName
.IsEmpty())
868 wxFileDialog::~wxFileDialog()
872 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
874 int index
= (int)event
.GetInt();
875 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
876 m_list
->SetWild( *str
);
877 m_filterIndex
= index
;
878 if ( str
-> Left( 2 ) == wxT("*.") )
880 m_filterExtension
= str
-> Mid( 1 );
881 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
884 m_filterExtension
= wxEmptyString
;
887 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
889 m_list
->ShowHidden( (m_lastShowHidden
= event
.GetInt() != 0) );
892 void wxFileDialog::OnActivated( wxListEvent
&event
)
894 HandleAction( event
.m_item
.m_text
);
897 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
899 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
900 cevent
.SetEventObject( this );
901 GetEventHandler()->ProcessEvent( cevent
);
904 void wxFileDialog::OnSelected( wxListEvent
&event
)
906 if (FindFocus() != m_list
) return;
908 wxString
filename( event
.m_item
.m_text
);
909 if (filename
== wxT("..")) return;
912 m_list
->GetDir( dir
);
913 if (dir
!= wxT("/")) dir
+= wxT("/");
915 if (wxDirExists(dir
)) return;
917 m_text
->SetValue( filename
);
920 void wxFileDialog::HandleAction( const wxString
&fn
)
922 wxString
filename( fn
);
924 m_list
->GetDir( dir
);
925 if (filename
.IsEmpty()) return;
926 if (filename
== wxT(".")) return;
928 if (filename
== wxT(".."))
930 m_list
->GoToParentDir();
932 m_list
->GetDir( dir
);
933 m_static
->SetLabel( dir
);
937 if (filename
== wxT("~"))
939 m_list
->GoToHomeDir();
941 m_list
->GetDir( dir
);
942 m_static
->SetLabel( dir
);
946 if (filename
[0] == wxT('~'))
948 filename
.Remove( 0, 1 );
949 wxString
tmp( wxGetUserHome() );
955 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
956 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
958 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
960 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
963 m_list
->SetWild( filename
);
967 if (dir
!= wxT("/")) dir
+= wxT("/");
968 if (filename
[0] != wxT('/'))
974 if (wxDirExists(filename
))
976 m_list
->GoToDir( filename
);
977 m_list
->GetDir( dir
);
978 m_static
->SetLabel( dir
);
983 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
985 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
986 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
987 filename
<< m_filterExtension
;
988 if (wxFileExists( filename
))
991 msg
.Printf( _("File '%s' already exists, do you really want to "
992 "overwrite it?"), filename
.c_str() );
994 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
998 else if ( m_dialogStyle
& wxOPEN
)
1000 if ( !wxFileExists( filename
) )
1001 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1002 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1003 filename
<< m_filterExtension
;
1005 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1007 if ( !wxFileExists( filename
) )
1009 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1015 SetPath( filename
);
1017 wxCommandEvent event
;
1018 wxDialog::OnOK(event
);
1021 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1023 HandleAction( m_text
->GetValue() );
1026 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1028 m_list
->ChangeToListMode();
1029 m_lastViewStyle
= wxLC_LIST
;
1033 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1035 m_list
->ChangeToReportMode();
1036 m_lastViewStyle
= wxLC_REPORT
;
1040 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1042 m_list
->GoToParentDir();
1045 m_list
->GetDir( dir
);
1046 m_static
->SetLabel( dir
);
1049 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1051 m_list
->GoToHomeDir();
1054 m_list
->GetDir( dir
);
1055 m_static
->SetLabel( dir
);
1058 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1063 void wxFileDialog::SetPath( const wxString
& path
)
1065 // not only set the full path but also update filename and dir
1070 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1073 m_fileName
+= wxT(".");
1079 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1082 if (m_list
->GetSelectedItemCount() == 0)
1084 paths
.Add( GetPath() );
1088 paths
.Alloc( m_list
->GetSelectedItemCount() );
1091 m_list
->GetDir( dir
);
1092 if (dir
!= wxT("/")) dir
+= wxT("/");
1095 item
.m_mask
= wxLIST_MASK_TEXT
;
1097 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1098 while ( item
.m_itemId
!= -1 ) {
1099 m_list
->GetItem( item
);
1100 paths
.Add( dir
+ item
.m_text
);
1101 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1102 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1106 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1109 if (m_list
->GetSelectedItemCount() == 0)
1111 files
.Add( GetFilename() );
1114 files
.Alloc( m_list
->GetSelectedItemCount() );
1117 item
.m_mask
= wxLIST_MASK_TEXT
;
1119 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1120 while ( item
.m_itemId
!= -1 ) {
1121 m_list
->GetItem( item
);
1122 files
.Add( item
.m_text
);
1123 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1124 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1130 // ----------------------------------------------------------------------------
1132 // ----------------------------------------------------------------------------
1135 wxFileSelectorEx(const wxChar
*message
,
1136 const wxChar
*default_path
,
1137 const wxChar
*default_filename
,
1138 int *WXUNUSED(indexDefaultExtension
),
1139 const wxChar
*wildcard
,
1144 // TODO: implement this somehow
1145 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1146 wildcard
, flags
, parent
, x
, y
);
1149 wxString
wxFileSelector( const wxChar
*title
,
1150 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1151 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1152 wxWindow
*parent
, int x
, int y
)
1155 if ( defaultExtension
&& !filter
)
1156 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1160 wxString defaultDirString
;
1162 defaultDirString
= defaultDir
;
1164 wxString defaultFilenameString
;
1165 if (defaultFileName
)
1166 defaultFilenameString
= defaultFileName
;
1168 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1170 if ( fileDialog
.ShowModal() == wxID_OK
)
1172 return fileDialog
.GetPath();
1176 return wxEmptyString
;
1180 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
1182 wxChar
*ext
= (wxChar
*)extension
;
1185 wxString str
= _("Load %s file");
1186 wxSprintf(prompt
, str
, what
);
1188 if (*ext
== wxT('.')) ext
++;
1190 wxSprintf(wild
, wxT("*.%s"), ext
);
1192 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1195 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1198 wxChar
*ext
= (wxChar
*)extension
;
1201 wxString str
= _("Save %s file");
1202 wxSprintf(prompt
, str
, what
);
1204 if (*ext
== wxT('.')) ext
++;
1206 wxSprintf(wild
, wxT("*.%s"), ext
);
1208 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1216 // A module to allow icons table cleanup
1218 class wxFileDialogGenericModule
: public wxModule
1220 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1222 wxFileDialogGenericModule() {}
1223 bool OnInit() { return TRUE
; }
1224 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1227 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)