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
;
89 static wxFileIconsTable
*g_IconsTable
= NULL
;
93 #define FI_EXECUTABLE 2
95 wxFileIconsTable::wxFileIconsTable() :
97 m_HashTable(wxKEY_STRING
)
99 m_HashTable
.DeleteContents(TRUE
);
100 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
101 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
102 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
104 m_ImageList
.Add(wxBitmap(exefile_xpm
));
105 m_HashTable
.Delete(_T("exe"));
106 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
108 /* else put into list by GetIconID
109 (KDE defines application/x-executable for *.exe and has nice icon)
115 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
117 wxImage
small(16, 16);
118 unsigned char *p1
, *p2
, *ps
;
119 unsigned char mr
= img
.GetMaskRed(),
120 mg
= img
.GetMaskGreen(),
121 mb
= img
.GetMaskBlue();
124 unsigned sr
, sg
, sb
, smask
;
126 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
127 small
.SetMaskColour(mr
, mr
, mr
);
129 for (y
= 0; y
< 16; y
++)
131 for (x
= 0; x
< 16; x
++)
133 sr
= sg
= sb
= smask
= 0;
134 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
135 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
138 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
139 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
142 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
143 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
146 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
147 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
152 ps
[0] = ps
[1] = ps
[2] = mr
;
154 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
157 p1
+= 32 * 3, p2
+= 32 * 3;
160 return small
.ConvertToBitmap();
164 // finds empty borders and return non-empty area of image:
165 static wxImage
CutEmptyBorders(const wxImage
& img
)
167 unsigned char mr
= img
.GetMaskRed(),
168 mg
= img
.GetMaskGreen(),
169 mb
= img
.GetMaskBlue();
170 unsigned char *dt
= img
.GetData(), *dttmp
;
171 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
173 unsigned top
, bottom
, left
, right
, i
;
176 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
177 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
179 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
182 for (i
= 0; i
< w
; i
++, dttmp
+=3)
185 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
188 for (i
= 0; i
< w
; i
++, dttmp
+=3)
191 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
194 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
197 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
200 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
203 top
--, left
--, bottom
++, right
++;
205 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
210 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
212 if (!extension
.IsEmpty())
214 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
215 if (entry
) return (entry
-> id
);
218 wxFileType
*ft
= (mime
.IsEmpty()) ?
219 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
220 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
222 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
224 int newid
= FI_UNKNOWN
;
225 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
231 int id
= m_ImageList
.GetImageCount();
232 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
233 m_ImageList
.Add(img
.ConvertToBitmap());
236 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
237 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
239 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
241 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
247 // ----------------------------------------------------------------------------
249 // ----------------------------------------------------------------------------
252 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
254 wxFileData
*fd1
= (wxFileData
*)data1
;
255 wxFileData
*fd2
= (wxFileData
*)data2
;
256 if (fd1
->GetName() == wxT("..")) return -1;
257 if (fd2
->GetName() == wxT("..")) return 1;
258 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
259 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
260 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
263 //-----------------------------------------------------------------------------
265 //-----------------------------------------------------------------------------
267 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
269 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
275 stat( m_fileName
.fn_str(), &buff
);
279 lstat( m_fileName
.fn_str(), &lbuff
);
280 m_isLink
= S_ISLNK( lbuff
.st_mode
);
281 struct tm
*t
= localtime( &lbuff
.st_mtime
);
284 struct tm
*t
= localtime( &buff
.st_mtime
);
287 // struct passwd *user = getpwuid( buff.st_uid );
288 // struct group *grp = getgrgid( buff.st_gid );
290 m_isDir
= S_ISDIR( buff
.st_mode
);
291 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
293 m_size
= buff
.st_size
;
296 m_minute
= t
->tm_min
;
297 m_month
= t
->tm_mon
+1;
302 m_permissions
.sprintf( wxT("%c%c%c"),
303 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
304 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
305 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
308 wxString
wxFileData::GetName() const
313 wxString
wxFileData::GetFullName() const
318 wxString
wxFileData::GetHint() const
320 wxString s
= m_fileName
;
322 if (m_isDir
) s
+= _("<DIR> ");
323 else if (m_isLink
) s
+= _("<LINK> ");
326 s
+= LongToString( m_size
);
329 s
+= IntToString( m_day
);
331 s
+= IntToString( m_month
);
333 s
+= IntToString( m_year
);
335 s
+= IntToString( m_hour
);
337 s
+= IntToString( m_minute
);
343 wxString
wxFileData::GetEntry( int num
)
355 if (m_isDir
) s
= _("<DIR>");
356 else if (m_isLink
) s
= _("<LINK>");
357 else s
= LongToString( m_size
);
362 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
363 s
+= IntToString( m_day
);
365 if (m_month
< 10) s
+= wxT("0");
366 s
+= IntToString( m_month
);
368 s
+= IntToString( m_year
);
373 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
374 s
+= IntToString( m_hour
);
376 if (m_minute
< 10) s
+= wxT("0");
377 s
+= IntToString( m_minute
);
390 bool wxFileData::IsDir()
395 bool wxFileData::IsExe()
400 bool wxFileData::IsLink()
405 long wxFileData::GetSize()
410 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
416 void wxFileData::MakeItem( wxListItem
&item
)
418 item
.m_text
= m_name
;
419 item
.ClearAttributes();
420 if (IsExe()) item
.SetTextColour(*wxRED
);
421 if (IsDir()) item
.SetTextColour(*wxBLUE
);
424 item
.m_image
= FI_FOLDER
;
426 item
.m_image
= FI_EXECUTABLE
;
427 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
428 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
430 item
.m_image
= FI_UNKNOWN
;
434 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
435 item
.SetTextColour(*dg
);
437 item
.m_data
= (long)this;
440 //-----------------------------------------------------------------------------
442 //-----------------------------------------------------------------------------
444 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
446 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
447 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
448 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
453 wxFileCtrl::wxFileCtrl()
455 m_dirName
= wxT("/");
456 m_showHidden
= FALSE
;
459 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
460 const wxString
&dirName
, const wxString
&wild
,
461 const wxPoint
&pos
, const wxSize
&size
,
462 long style
, const wxValidator
&validator
, const wxString
&name
) :
463 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
465 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
466 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
468 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
472 m_showHidden
= FALSE
;
476 void wxFileCtrl::ChangeToListMode()
478 SetSingleStyle( wxLC_LIST
);
482 void wxFileCtrl::ChangeToReportMode()
484 SetSingleStyle( wxLC_REPORT
);
488 void wxFileCtrl::ChangeToIconMode()
490 SetSingleStyle( wxLC_ICON
);
494 void wxFileCtrl::ShowHidden( bool show
)
500 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
503 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
504 fd
->MakeItem( item
);
505 long my_style
= GetWindowStyleFlag();
506 if (my_style
& wxLC_REPORT
)
508 ret
= InsertItem( item
);
509 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
511 else if (my_style
& wxLC_LIST
)
513 ret
= InsertItem( item
);
518 void wxFileCtrl::Update()
520 long my_style
= GetWindowStyleFlag();
521 int name_col_width
= 0;
522 if (my_style
& wxLC_REPORT
)
524 if (GetColumnCount() > 0)
525 name_col_width
= GetColumnWidth( 0 );
529 if (my_style
& wxLC_REPORT
)
531 if (name_col_width
< 140) name_col_width
= 140;
532 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
533 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
534 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
535 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
536 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
538 wxFileData
*fd
= (wxFileData
*) NULL
;
543 if (m_dirName
!= wxT("/"))
545 wxString
p( wxPathOnly(m_dirName
) );
546 if (p
.IsEmpty()) p
= wxT("/");
547 fd
= new wxFileData( wxT(".."), p
);
552 wxString res
= m_dirName
+ wxT("/*");
553 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
556 res
= wxFileNameFromPath( f
);
557 fd
= new wxFileData( res
, f
);
558 wxString s
= fd
->GetName();
559 if (m_showHidden
|| (s
[0] != wxT('.')))
564 f
= wxFindNextFile();
567 res
= m_dirName
+ wxT("/") + m_wild
;
568 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
571 res
= wxFileNameFromPath( f
);
572 fd
= new wxFileData( res
, f
);
573 wxString s
= fd
->GetName();
574 if (m_showHidden
|| (s
[0] != wxT('.')))
579 f
= wxFindNextFile();
582 SortItems( ListCompare
, 0 );
584 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
585 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
586 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
589 void wxFileCtrl::SetWild( const wxString
&wild
)
595 void wxFileCtrl::MakeDir()
597 wxString
new_name( wxT("NewName") );
598 wxString
path( m_dirName
);
601 if (wxFileExists(path
))
603 // try NewName0, NewName1 etc.
606 new_name
= _("NewName");
608 num
.Printf( wxT("%d"), i
);
615 } while (wxFileExists(path
));
621 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
626 wxFileData
*fd
= new wxFileData( new_name
, path
);
630 long id
= Add( fd
, item
);
634 SortItems( ListCompare
, 0 );
635 id
= FindItem( 0, (long)fd
);
641 void wxFileCtrl::GoToParentDir()
643 if (m_dirName
!= wxT("/"))
645 wxString
fname( wxFileNameFromPath(m_dirName
) );
646 m_dirName
= wxPathOnly( m_dirName
);
647 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
649 long id
= FindItem( 0, fname
);
652 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
658 void wxFileCtrl::GoToHomeDir()
660 wxString s
= wxGetUserHome( wxString() );
663 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
667 void wxFileCtrl::GoToDir( const wxString
&dir
)
671 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
675 void wxFileCtrl::GetDir( wxString
&dir
)
680 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
682 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
686 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
688 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
691 if ((event
.GetLabel().IsEmpty()) ||
692 (event
.GetLabel() == _(".")) ||
693 (event
.GetLabel() == _("..")) ||
694 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
696 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
702 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
703 new_name
+= wxT("/");
704 new_name
+= event
.GetLabel();
708 if (wxFileExists(new_name
))
710 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
715 if (wxRenameFile(fd
->GetFullName(),new_name
))
717 fd
->SetNewName( new_name
, event
.GetLabel() );
718 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
719 EnsureVisible( event
.GetItem() );
723 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
729 //-----------------------------------------------------------------------------
731 //-----------------------------------------------------------------------------
733 #define ID_LIST_MODE wxID_FILEDLGG
734 #define ID_REPORT_MODE wxID_FILEDLGG + 1
735 #define ID_UP_DIR wxID_FILEDLGG + 5
736 #define ID_PARENT_DIR wxID_FILEDLGG + 6
737 #define ID_NEW_DIR wxID_FILEDLGG + 7
738 #define ID_CHOICE wxID_FILEDLGG + 8
739 #define ID_TEXT wxID_FILEDLGG + 9
740 #define ID_LIST_CTRL wxID_FILEDLGG + 10
741 #define ID_ACTIVATED wxID_FILEDLGG + 11
742 #define ID_CHECK wxID_FILEDLGG + 12
744 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
746 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
747 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
748 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
749 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
750 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
751 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
752 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
753 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
754 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
755 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
756 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
757 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
760 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
761 bool wxFileDialog::s_lastShowHidden
= FALSE
;
763 wxFileDialog::wxFileDialog(wxWindow
*parent
,
764 const wxString
& message
,
765 const wxString
& defaultDir
,
766 const wxString
& defaultFile
,
767 const wxString
& wildCard
,
769 const wxPoint
& pos
) :
770 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
774 if (wxConfig::Get(FALSE
))
776 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle
);
777 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden
);
781 m_dialogStyle
= style
;
783 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
784 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
785 m_dialogStyle
|= wxOPEN
;
788 if ((m_dir
.IsEmpty()) || (m_dir
== wxT(".")))
791 m_dir
= getcwd( buf
, sizeof(buf
) );
795 m_path
+= defaultFile
;
796 m_fileName
= defaultFile
;
797 m_wildCard
= wildCard
;
799 m_filterExtension
= wxEmptyString
;
801 // interpret wildcards
803 if (m_wildCard
.IsEmpty())
804 m_wildCard
= _("All files (*)|*");
806 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
808 wxString firstWildText
;
809 if (tokens
.CountTokens() == 1)
811 firstWildText
= tokens
.GetNextToken();
812 firstWild
= firstWildText
;
816 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
817 firstWildText
= tokens
.GetNextToken();
818 firstWild
= tokens
.GetNextToken();
820 if ( firstWild
.Left( 2 ) == wxT("*.") )
821 m_filterExtension
= firstWild
.Mid( 1 );
822 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
826 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
828 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
832 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
834 but
->SetToolTip( _("View files as a list view") );
836 buttonsizer
->Add( but
, 0, wxALL
, 5 );
838 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
840 but
->SetToolTip( _("View files as a detailed view") );
842 buttonsizer
->Add( but
, 0, wxALL
, 5 );
844 buttonsizer
->Add( 30, 5, 1 );
846 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
848 but
->SetToolTip( _("Go to parent directory") );
850 buttonsizer
->Add( but
, 0, wxALL
, 5 );
852 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
854 but
->SetToolTip( _("Go to home directory") );
856 buttonsizer
->Add( but
, 0, wxALL
, 5);
858 buttonsizer
->Add( 20, 20 );
860 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
862 but
->SetToolTip( _("Create new directory") );
864 buttonsizer
->Add( but
, 0, wxALL
, 5 );
866 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
868 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
869 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
870 m_static
= new wxStaticText( this, -1, m_dir
);
871 staticsizer
->Add( m_static
, 1 );
872 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
874 if (m_dialogStyle
& wxMULTIPLE
)
875 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
876 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
);
878 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
879 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
880 m_list
-> ShowHidden(s_lastShowHidden
);
881 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
883 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
884 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
885 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
886 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
887 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
889 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
890 m_choice
= new wxChoice( this, ID_CHOICE
);
891 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
892 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
893 m_check
->SetValue( s_lastShowHidden
);
894 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
895 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
896 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
898 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
899 while (tokens
.HasMoreTokens())
901 firstWildText
= tokens
.GetNextToken();
902 firstWild
= tokens
.GetNextToken();
903 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
905 m_choice
->SetSelection( 0 );
907 SetAutoLayout( TRUE
);
908 SetSizer( mainsizer
);
910 mainsizer
->Fit( this );
911 mainsizer
->SetSizeHints( this );
915 if (m_fileName
.IsEmpty())
923 wxFileDialog::~wxFileDialog()
925 if (wxConfig::Get(FALSE
))
927 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle
);
928 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden
);
932 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
934 int index
= (int)event
.GetInt();
935 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
936 m_list
->SetWild( *str
);
937 m_filterIndex
= index
;
938 if ( str
-> Left( 2 ) == wxT("*.") )
940 m_filterExtension
= str
-> Mid( 1 );
941 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
944 m_filterExtension
= wxEmptyString
;
947 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
949 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
952 void wxFileDialog::OnActivated( wxListEvent
&event
)
954 HandleAction( event
.m_item
.m_text
);
957 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
959 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
960 cevent
.SetEventObject( this );
961 GetEventHandler()->ProcessEvent( cevent
);
964 void wxFileDialog::OnSelected( wxListEvent
&event
)
966 if (FindFocus() != m_list
) return;
968 wxString
filename( event
.m_item
.m_text
);
969 if (filename
== wxT("..")) return;
972 m_list
->GetDir( dir
);
973 if (dir
!= wxT("/")) dir
+= wxT("/");
975 if (wxDirExists(dir
)) return;
977 m_text
->SetValue( filename
);
980 void wxFileDialog::HandleAction( const wxString
&fn
)
982 wxString
filename( fn
);
984 m_list
->GetDir( dir
);
985 if (filename
.IsEmpty()) return;
986 if (filename
== wxT(".")) return;
988 if (filename
== wxT(".."))
990 m_list
->GoToParentDir();
992 m_list
->GetDir( dir
);
993 m_static
->SetLabel( dir
);
997 if (filename
== wxT("~"))
999 m_list
->GoToHomeDir();
1001 m_list
->GetDir( dir
);
1002 m_static
->SetLabel( dir
);
1006 if (filename
[0] == wxT('~'))
1008 filename
.Remove( 0, 1 );
1009 wxString
tmp( wxGetUserHome() );
1015 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1016 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1018 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
1020 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1023 m_list
->SetWild( filename
);
1027 if (dir
!= wxT("/")) dir
+= wxT("/");
1028 if (filename
[0] != wxT('/'))
1034 if (wxDirExists(filename
))
1036 m_list
->GoToDir( filename
);
1037 m_list
->GetDir( dir
);
1038 m_static
->SetLabel( dir
);
1043 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1045 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1046 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1047 filename
<< m_filterExtension
;
1048 if (wxFileExists( filename
))
1051 msg
.Printf( _("File '%s' already exists, do you really want to "
1052 "overwrite it?"), filename
.c_str() );
1054 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1058 else if ( m_dialogStyle
& wxOPEN
)
1060 if ( !wxFileExists( filename
) )
1061 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1062 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1063 filename
<< m_filterExtension
;
1065 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1067 if ( !wxFileExists( filename
) )
1069 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1075 SetPath( filename
);
1077 wxCommandEvent event
;
1078 wxDialog::OnOK(event
);
1081 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1083 HandleAction( m_text
->GetValue() );
1086 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1088 m_list
->ChangeToListMode();
1089 s_lastViewStyle
= wxLC_LIST
;
1093 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1095 m_list
->ChangeToReportMode();
1096 s_lastViewStyle
= wxLC_REPORT
;
1100 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1102 m_list
->GoToParentDir();
1105 m_list
->GetDir( dir
);
1106 m_static
->SetLabel( dir
);
1109 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1111 m_list
->GoToHomeDir();
1114 m_list
->GetDir( dir
);
1115 m_static
->SetLabel( dir
);
1118 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1123 void wxFileDialog::SetPath( const wxString
& path
)
1125 // not only set the full path but also update filename and dir
1130 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1133 m_fileName
+= wxT(".");
1139 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1142 if (m_list
->GetSelectedItemCount() == 0)
1144 paths
.Add( GetPath() );
1148 paths
.Alloc( m_list
->GetSelectedItemCount() );
1151 m_list
->GetDir( dir
);
1152 if (dir
!= wxT("/")) dir
+= wxT("/");
1155 item
.m_mask
= wxLIST_MASK_TEXT
;
1157 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1158 while ( item
.m_itemId
!= -1 ) {
1159 m_list
->GetItem( item
);
1160 paths
.Add( dir
+ item
.m_text
);
1161 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1162 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1166 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1169 if (m_list
->GetSelectedItemCount() == 0)
1171 files
.Add( GetFilename() );
1174 files
.Alloc( m_list
->GetSelectedItemCount() );
1177 item
.m_mask
= wxLIST_MASK_TEXT
;
1179 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1180 while ( item
.m_itemId
!= -1 ) {
1181 m_list
->GetItem( item
);
1182 files
.Add( item
.m_text
);
1183 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
+ 1,
1184 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1190 // ----------------------------------------------------------------------------
1192 // ----------------------------------------------------------------------------
1195 wxFileSelectorEx(const wxChar
*message
,
1196 const wxChar
*default_path
,
1197 const wxChar
*default_filename
,
1198 int *WXUNUSED(indexDefaultExtension
),
1199 const wxChar
*wildcard
,
1204 // TODO: implement this somehow
1205 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1206 wildcard
, flags
, parent
, x
, y
);
1209 wxString
wxFileSelector( const wxChar
*title
,
1210 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1211 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1212 wxWindow
*parent
, int x
, int y
)
1215 if ( defaultExtension
&& !filter
)
1216 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1220 wxString defaultDirString
;
1222 defaultDirString
= defaultDir
;
1224 wxString defaultFilenameString
;
1225 if (defaultFileName
)
1226 defaultFilenameString
= defaultFileName
;
1228 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1230 if ( fileDialog
.ShowModal() == wxID_OK
)
1232 return fileDialog
.GetPath();
1236 return wxEmptyString
;
1240 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
1242 wxChar
*ext
= (wxChar
*)extension
;
1245 wxString str
= _("Load %s file");
1246 wxSprintf(prompt
, str
, what
);
1248 if (*ext
== wxT('.')) ext
++;
1250 wxSprintf(wild
, wxT("*.%s"), ext
);
1252 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1255 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1258 wxChar
*ext
= (wxChar
*)extension
;
1261 wxString str
= _("Save %s file");
1262 wxSprintf(prompt
, str
, what
);
1264 if (*ext
== wxT('.')) ext
++;
1266 wxSprintf(wild
, wxT("*.%s"), ext
);
1268 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1276 // A module to allow icons table cleanup
1278 class wxFileDialogGenericModule
: public wxModule
1280 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1282 wxFileDialogGenericModule() {}
1283 bool OnInit() { return TRUE
; }
1284 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1287 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)