1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #error wxFileDialog currently only supports unix
27 #include "wx/filedlg.h"
31 #include "wx/msgdlg.h"
33 #include "wx/bmpbuttn.h"
34 #include "wx/tokenzr.h"
35 #include "wx/mimetype.h"
37 #include "wx/module.h"
38 #include "wx/config.h"
42 #include "wx/tooltip.h"
45 #include <sys/types.h>
53 #include "wx/generic/home.xpm"
54 #include "wx/generic/listview.xpm"
55 #include "wx/generic/repview.xpm"
56 #include "wx/generic/new_dir.xpm"
57 #include "wx/generic/dir_up.xpm"
58 #include "wx/generic/folder.xpm"
59 #include "wx/generic/deffile.xpm"
60 #include "wx/generic/exefile.xpm"
62 // ----------------------------------------------------------------------------
63 // private classes - icons list management
64 // ----------------------------------------------------------------------------
66 class wxFileIconEntry
: public wxObject
69 wxFileIconEntry(int i
) { id
= i
; }
75 class wxFileIconsTable
81 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
82 wxImageList
*GetImageList() { return &m_ImageList
; }
85 wxImageList m_ImageList
;
86 wxHashTable m_HashTable
;
87 wxMimeTypesManager m_Mime
;
90 static wxFileIconsTable
*g_IconsTable
= NULL
;
94 #define FI_EXECUTABLE 2
96 wxFileIconsTable::wxFileIconsTable() :
98 m_HashTable(wxKEY_STRING
),
101 m_HashTable
.DeleteContents(TRUE
);
102 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
103 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
104 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
106 m_ImageList
.Add(wxBitmap(exefile_xpm
));
107 m_HashTable
.Delete(_T("exe"));
108 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
110 /* else put into list by GetIconID
111 (KDE defines application/x-executable for *.exe and has nice icon)
117 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
119 wxImage
small(16, 16);
120 unsigned char *p1
, *p2
, *ps
;
121 unsigned char mr
= img
.GetMaskRed(),
122 mg
= img
.GetMaskGreen(),
123 mb
= img
.GetMaskBlue();
126 unsigned sr
, sg
, sb
, smask
;
128 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
129 small
.SetMaskColour(mr
, mr
, mr
);
131 for (y
= 0; y
< 16; y
++)
133 for (x
= 0; x
< 16; x
++)
135 sr
= sg
= sb
= smask
= 0;
136 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
137 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
140 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
141 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
144 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
145 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
148 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
149 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
154 ps
[0] = ps
[1] = ps
[2] = mr
;
156 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
159 p1
+= 32 * 3, p2
+= 32 * 3;
162 return small
.ConvertToBitmap();
166 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
168 if (!extension
.IsEmpty())
170 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
171 if (entry
) return (entry
-> id
);
174 wxFileType
*ft
= (mime
.IsEmpty()) ?
175 m_Mime
.GetFileTypeFromExtension(extension
) :
176 m_Mime
.GetFileTypeFromMimeType(mime
);
178 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)))
180 int newid
= FI_UNKNOWN
;
181 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
187 int id
= m_ImageList
.GetImageCount();
188 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
189 m_ImageList
.Add(img
.ConvertToBitmap());
192 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
194 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
196 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
202 // ----------------------------------------------------------------------------
204 // ----------------------------------------------------------------------------
207 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
209 wxFileData
*fd1
= (wxFileData
*)data1
;
210 wxFileData
*fd2
= (wxFileData
*)data2
;
211 if (fd1
->GetName() == wxT("..")) return -1;
212 if (fd2
->GetName() == wxT("..")) return 1;
213 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
214 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
215 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
218 //-----------------------------------------------------------------------------
220 //-----------------------------------------------------------------------------
222 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
224 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
230 stat( m_fileName
.fn_str(), &buff
);
234 lstat( m_fileName
.fn_str(), &lbuff
);
235 m_isLink
= S_ISLNK( lbuff
.st_mode
);
236 struct tm
*t
= localtime( &lbuff
.st_mtime
);
239 struct tm
*t
= localtime( &buff
.st_mtime
);
242 // struct passwd *user = getpwuid( buff.st_uid );
243 // struct group *grp = getgrgid( buff.st_gid );
245 m_isDir
= S_ISDIR( buff
.st_mode
);
246 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
248 m_size
= buff
.st_size
;
251 m_minute
= t
->tm_min
;
252 m_month
= t
->tm_mon
+1;
257 m_permissions
.sprintf( wxT("%c%c%c"),
258 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
259 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
260 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
263 wxString
wxFileData::GetName() const
268 wxString
wxFileData::GetFullName() const
273 wxString
wxFileData::GetHint() const
275 wxString s
= m_fileName
;
277 if (m_isDir
) s
+= _("<DIR> ");
278 else if (m_isLink
) s
+= _("<LINK> ");
281 s
+= LongToString( m_size
);
284 s
+= IntToString( m_day
);
286 s
+= IntToString( m_month
);
288 s
+= IntToString( m_year
);
290 s
+= IntToString( m_hour
);
292 s
+= IntToString( m_minute
);
298 wxString
wxFileData::GetEntry( int num
)
310 if (m_isDir
) s
= _("<DIR>");
311 else if (m_isLink
) s
= _("<LINK>");
312 else s
= LongToString( m_size
);
317 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
318 s
+= IntToString( m_day
);
320 if (m_month
< 10) s
+= wxT("0");
321 s
+= IntToString( m_month
);
323 s
+= IntToString( m_year
);
328 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
329 s
+= IntToString( m_hour
);
331 if (m_minute
< 10) s
+= wxT("0");
332 s
+= IntToString( m_minute
);
345 bool wxFileData::IsDir()
350 bool wxFileData::IsExe()
355 bool wxFileData::IsLink()
360 long wxFileData::GetSize()
365 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
371 void wxFileData::MakeItem( wxListItem
&item
)
373 item
.m_text
= m_name
;
374 item
.ClearAttributes();
375 if (IsExe()) item
.SetTextColour(*wxRED
);
376 if (IsDir()) item
.SetTextColour(*wxBLUE
);
379 item
.m_image
= FI_FOLDER
;
381 item
.m_image
= FI_EXECUTABLE
;
382 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
383 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
385 item
.m_image
= FI_UNKNOWN
;
389 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
390 item
.SetTextColour(*dg
);
392 item
.m_data
= (long)this;
395 //-----------------------------------------------------------------------------
397 //-----------------------------------------------------------------------------
399 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
401 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
402 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
403 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
408 wxFileCtrl::wxFileCtrl()
410 m_dirName
= wxT("/");
411 m_showHidden
= FALSE
;
414 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
415 const wxString
&dirName
, const wxString
&wild
,
416 const wxPoint
&pos
, const wxSize
&size
,
417 long style
, const wxValidator
&validator
, const wxString
&name
) :
418 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
420 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
421 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
423 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
427 m_showHidden
= FALSE
;
431 void wxFileCtrl::ChangeToListMode()
433 SetSingleStyle( wxLC_LIST
);
437 void wxFileCtrl::ChangeToReportMode()
439 SetSingleStyle( wxLC_REPORT
);
443 void wxFileCtrl::ChangeToIconMode()
445 SetSingleStyle( wxLC_ICON
);
449 void wxFileCtrl::ShowHidden( bool show
)
455 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
458 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
459 fd
->MakeItem( item
);
460 long my_style
= GetWindowStyleFlag();
461 if (my_style
& wxLC_REPORT
)
463 ret
= InsertItem( item
);
464 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
466 else if (my_style
& wxLC_LIST
)
468 ret
= InsertItem( item
);
473 void wxFileCtrl::Update()
475 long my_style
= GetWindowStyleFlag();
476 int name_col_width
= 0;
477 if (my_style
& wxLC_REPORT
)
479 if (GetColumnCount() > 0)
480 name_col_width
= GetColumnWidth( 0 );
484 if (my_style
& wxLC_REPORT
)
486 if (name_col_width
< 140) name_col_width
= 140;
487 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
488 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
489 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
490 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
491 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
493 wxFileData
*fd
= (wxFileData
*) NULL
;
498 if (m_dirName
!= wxT("/"))
500 wxString
p( wxPathOnly(m_dirName
) );
501 if (p
.IsEmpty()) p
= wxT("/");
502 fd
= new wxFileData( wxT(".."), p
);
507 wxString res
= m_dirName
+ wxT("/*");
508 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
511 res
= wxFileNameFromPath( f
);
512 fd
= new wxFileData( res
, f
);
513 wxString s
= fd
->GetName();
514 if (m_showHidden
|| (s
[0] != wxT('.')))
519 f
= wxFindNextFile();
522 res
= m_dirName
+ wxT("/") + m_wild
;
523 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
526 res
= wxFileNameFromPath( f
);
527 fd
= new wxFileData( res
, f
);
528 wxString s
= fd
->GetName();
529 if (m_showHidden
|| (s
[0] != wxT('.')))
534 f
= wxFindNextFile();
537 SortItems( ListCompare
, 0 );
539 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
540 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
541 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
544 void wxFileCtrl::SetWild( const wxString
&wild
)
550 void wxFileCtrl::MakeDir()
552 wxString
new_name( wxT("NewName") );
553 wxString
path( m_dirName
);
556 if (wxFileExists(path
))
558 // try NewName0, NewName1 etc.
561 new_name
= _("NewName");
563 num
.Printf( wxT("%d"), i
);
570 } while (wxFileExists(path
));
576 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
581 wxFileData
*fd
= new wxFileData( new_name
, path
);
585 long id
= Add( fd
, item
);
589 SortItems( ListCompare
, 0 );
590 id
= FindItem( 0, (long)fd
);
596 void wxFileCtrl::GoToParentDir()
598 if (m_dirName
!= wxT("/"))
600 wxString
fname( wxFileNameFromPath(m_dirName
) );
601 m_dirName
= wxPathOnly( m_dirName
);
602 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
604 long id
= FindItem( 0, fname
);
607 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
613 void wxFileCtrl::GoToHomeDir()
615 wxString s
= wxGetUserHome( wxString() );
618 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
622 void wxFileCtrl::GoToDir( const wxString
&dir
)
626 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
630 void wxFileCtrl::GetDir( wxString
&dir
)
635 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
637 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
641 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
643 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
646 if ((event
.GetLabel().IsEmpty()) ||
647 (event
.GetLabel() == _(".")) ||
648 (event
.GetLabel() == _("..")) ||
649 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
651 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
657 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
658 new_name
+= wxT("/");
659 new_name
+= event
.GetLabel();
663 if (wxFileExists(new_name
))
665 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
670 if (wxRenameFile(fd
->GetFullName(),new_name
))
672 fd
->SetNewName( new_name
, event
.GetLabel() );
673 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
674 EnsureVisible( event
.GetItem() );
678 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
684 //-----------------------------------------------------------------------------
686 //-----------------------------------------------------------------------------
688 #define ID_LIST_MODE wxID_FILEDLGG
689 #define ID_REPORT_MODE wxID_FILEDLGG + 1
690 #define ID_UP_DIR wxID_FILEDLGG + 5
691 #define ID_PARENT_DIR wxID_FILEDLGG + 6
692 #define ID_NEW_DIR wxID_FILEDLGG + 7
693 #define ID_CHOICE wxID_FILEDLGG + 8
694 #define ID_TEXT wxID_FILEDLGG + 9
695 #define ID_LIST_CTRL wxID_FILEDLGG + 10
696 #define ID_ACTIVATED wxID_FILEDLGG + 11
697 #define ID_CHECK wxID_FILEDLGG + 12
699 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
701 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
702 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
703 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
704 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
705 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
706 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
707 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
708 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
709 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
710 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
711 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
712 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
715 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
716 bool wxFileDialog::s_lastShowHidden
= FALSE
;
718 wxFileDialog::wxFileDialog(wxWindow
*parent
,
719 const wxString
& message
,
720 const wxString
& defaultDir
,
721 const wxString
& defaultFile
,
722 const wxString
& wildCard
,
724 const wxPoint
& pos
) :
725 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
729 if (wxConfig::Get(FALSE
))
731 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle
);
732 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden
);
736 m_dialogStyle
= style
;
738 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
739 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
740 m_dialogStyle
|= wxOPEN
;
743 if ((m_dir
.IsEmpty()) || (m_dir
== wxT(".")))
746 m_dir
= getcwd( buf
, sizeof(buf
) );
750 m_path
+= defaultFile
;
751 m_fileName
= defaultFile
;
752 m_wildCard
= wildCard
;
754 m_filterExtension
= wxEmptyString
;
756 // interpret wildcards
758 if (m_wildCard
.IsEmpty())
759 m_wildCard
= _("All files (*)|*");
761 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
763 wxString firstWildText
;
764 if (tokens
.CountTokens() == 1)
766 firstWildText
= tokens
.GetNextToken();
767 firstWild
= firstWildText
;
771 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
772 firstWildText
= tokens
.GetNextToken();
773 firstWild
= tokens
.GetNextToken();
775 if ( firstWild
.Left( 2 ) == wxT("*.") )
776 m_filterExtension
= firstWild
.Mid( 1 );
777 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
781 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
783 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
787 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
789 but
->SetToolTip( _("View files as a list view") );
791 buttonsizer
->Add( but
, 0, wxALL
, 5 );
793 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
795 but
->SetToolTip( _("View files as a detailed view") );
797 buttonsizer
->Add( but
, 0, wxALL
, 5 );
799 buttonsizer
->Add( 30, 5, 1 );
801 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
803 but
->SetToolTip( _("Go to parent directory") );
805 buttonsizer
->Add( but
, 0, wxALL
, 5 );
807 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
809 but
->SetToolTip( _("Go to home directory") );
811 buttonsizer
->Add( but
, 0, wxALL
, 5);
813 buttonsizer
->Add( 20, 20 );
815 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
817 but
->SetToolTip( _("Create new directory") );
819 buttonsizer
->Add( but
, 0, wxALL
, 5 );
821 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
823 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
824 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
825 m_static
= new wxStaticText( this, -1, m_dir
);
826 staticsizer
->Add( m_static
, 1 );
827 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
829 if (m_dialogStyle
& wxMULTIPLE
)
830 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
831 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
);
833 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
834 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
835 m_list
-> ShowHidden(s_lastShowHidden
);
836 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
838 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
839 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
840 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
841 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
842 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
844 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
845 m_choice
= new wxChoice( this, ID_CHOICE
);
846 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
847 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
848 m_check
->SetValue( s_lastShowHidden
);
849 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
850 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
851 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
853 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
854 while (tokens
.HasMoreTokens())
856 firstWildText
= tokens
.GetNextToken();
857 firstWild
= tokens
.GetNextToken();
858 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
860 m_choice
->SetSelection( 0 );
862 SetAutoLayout( TRUE
);
863 SetSizer( mainsizer
);
865 mainsizer
->Fit( this );
866 mainsizer
->SetSizeHints( this );
870 if (m_fileName
.IsEmpty())
878 wxFileDialog::~wxFileDialog()
880 if (wxConfig::Get(FALSE
))
882 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle
);
883 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden
);
887 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
889 int index
= (int)event
.GetInt();
890 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
891 m_list
->SetWild( *str
);
892 m_filterIndex
= index
;
893 if ( str
-> Left( 2 ) == wxT("*.") )
895 m_filterExtension
= str
-> Mid( 1 );
896 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
899 m_filterExtension
= wxEmptyString
;
902 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
904 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
907 void wxFileDialog::OnActivated( wxListEvent
&event
)
909 HandleAction( event
.m_item
.m_text
);
912 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
914 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
915 cevent
.SetEventObject( this );
916 GetEventHandler()->ProcessEvent( cevent
);
919 void wxFileDialog::OnSelected( wxListEvent
&event
)
921 if (FindFocus() != m_list
) return;
923 wxString
filename( event
.m_item
.m_text
);
924 if (filename
== wxT("..")) return;
927 m_list
->GetDir( dir
);
928 if (dir
!= wxT("/")) dir
+= wxT("/");
930 if (wxDirExists(dir
)) return;
932 m_text
->SetValue( filename
);
935 void wxFileDialog::HandleAction( const wxString
&fn
)
937 wxString
filename( fn
);
939 m_list
->GetDir( dir
);
940 if (filename
.IsEmpty()) return;
941 if (filename
== wxT(".")) return;
943 if (filename
== wxT(".."))
945 m_list
->GoToParentDir();
947 m_list
->GetDir( dir
);
948 m_static
->SetLabel( dir
);
952 if (filename
== wxT("~"))
954 m_list
->GoToHomeDir();
956 m_list
->GetDir( dir
);
957 m_static
->SetLabel( dir
);
961 if (filename
[0] == wxT('~'))
963 filename
.Remove( 0, 1 );
964 wxString
tmp( wxGetUserHome() );
970 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
971 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
973 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
975 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
978 m_list
->SetWild( filename
);
982 if (dir
!= wxT("/")) dir
+= wxT("/");
983 if (filename
[0] != wxT('/'))
989 if (wxDirExists(filename
))
991 m_list
->GoToDir( filename
);
992 m_list
->GetDir( dir
);
993 m_static
->SetLabel( dir
);
998 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1000 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1001 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1002 filename
<< m_filterExtension
;
1003 if (wxFileExists( filename
))
1006 msg
.Printf( _("File '%s' already exists, do you really want to "
1007 "overwrite it?"), filename
.c_str() );
1009 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1013 else if ( m_dialogStyle
& wxOPEN
)
1015 if ( !wxFileExists( filename
) )
1016 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1017 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1018 filename
<< m_filterExtension
;
1020 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1022 if ( !wxFileExists( filename
) )
1024 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1030 SetPath( filename
);
1032 wxCommandEvent event
;
1033 wxDialog::OnOK(event
);
1036 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1038 HandleAction( m_text
->GetValue() );
1041 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1043 m_list
->ChangeToListMode();
1044 s_lastViewStyle
= wxLC_LIST
;
1048 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1050 m_list
->ChangeToReportMode();
1051 s_lastViewStyle
= wxLC_REPORT
;
1055 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1057 m_list
->GoToParentDir();
1060 m_list
->GetDir( dir
);
1061 m_static
->SetLabel( dir
);
1064 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1066 m_list
->GoToHomeDir();
1069 m_list
->GetDir( dir
);
1070 m_static
->SetLabel( dir
);
1073 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1078 void wxFileDialog::SetPath( const wxString
& path
)
1080 // not only set the full path but also update filename and dir
1085 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1088 m_fileName
+= wxT(".");
1094 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1097 if (m_list
->GetSelectedItemCount() == 0)
1099 paths
.Add( GetPath() );
1103 paths
.Alloc( m_list
->GetSelectedItemCount() );
1106 m_list
->GetDir( dir
);
1107 if (dir
!= wxT("/")) dir
+= wxT("/");
1110 item
.m_mask
= wxLIST_MASK_TEXT
;
1112 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1113 while ( item
.m_itemId
!= -1 ) {
1114 m_list
->GetItem( item
);
1115 paths
.Add( dir
+ item
.m_text
);
1116 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1117 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1121 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1124 if (m_list
->GetSelectedItemCount() == 0)
1126 files
.Add( GetFilename() );
1129 files
.Alloc( m_list
->GetSelectedItemCount() );
1132 item
.m_mask
= wxLIST_MASK_TEXT
;
1134 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1135 while ( item
.m_itemId
!= -1 ) {
1136 m_list
->GetItem( item
);
1137 files
.Add( item
.m_text
);
1138 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1139 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1145 // ----------------------------------------------------------------------------
1147 // ----------------------------------------------------------------------------
1150 wxFileSelectorEx(const wxChar
*message
,
1151 const wxChar
*default_path
,
1152 const wxChar
*default_filename
,
1153 int *WXUNUSED(indexDefaultExtension
),
1154 const wxChar
*wildcard
,
1159 // TODO: implement this somehow
1160 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1161 wildcard
, flags
, parent
, x
, y
);
1164 wxString
wxFileSelector( const wxChar
*title
,
1165 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1166 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1167 wxWindow
*parent
, int x
, int y
)
1170 if ( defaultExtension
&& !filter
)
1171 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1175 wxString defaultDirString
;
1177 defaultDirString
= defaultDir
;
1179 wxString defaultFilenameString
;
1180 if (defaultFileName
)
1181 defaultFilenameString
= defaultFileName
;
1183 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1185 if ( fileDialog
.ShowModal() == wxID_OK
)
1187 return fileDialog
.GetPath();
1191 return wxEmptyString
;
1195 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
1197 wxChar
*ext
= (wxChar
*)extension
;
1200 wxString str
= _("Load %s file");
1201 wxSprintf(prompt
, str
, what
);
1203 if (*ext
== wxT('.')) ext
++;
1205 wxSprintf(wild
, wxT("*.%s"), ext
);
1207 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1210 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1213 wxChar
*ext
= (wxChar
*)extension
;
1216 wxString str
= _("Save %s file");
1217 wxSprintf(prompt
, str
, what
);
1219 if (*ext
== wxT('.')) ext
++;
1221 wxSprintf(wild
, wxT("*.%s"), ext
);
1223 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1231 // A module to allow icons table cleanup
1233 class wxFileDialogGenericModule
: public wxModule
1235 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1237 wxFileDialogGenericModule() {}
1238 bool OnInit() { return TRUE
; }
1239 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1242 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)