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"
39 #include "wx/imaglist.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
80 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
81 wxImageList
*GetImageList() { return &m_ImageList
; }
84 wxImageList m_ImageList
;
85 wxHashTable m_HashTable
;
88 static wxFileIconsTable
*g_IconsTable
= NULL
;
92 #define FI_EXECUTABLE 2
94 wxFileIconsTable::wxFileIconsTable() :
96 m_HashTable(wxKEY_STRING
)
98 m_HashTable
.DeleteContents(TRUE
);
99 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
100 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
101 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
103 m_ImageList
.Add(wxBitmap(exefile_xpm
));
104 m_HashTable
.Delete(_T("exe"));
105 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
107 /* else put into list by GetIconID
108 (KDE defines application/x-executable for *.exe and has nice icon)
114 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
116 wxImage
small(16, 16);
117 unsigned char *p1
, *p2
, *ps
;
118 unsigned char mr
= img
.GetMaskRed(),
119 mg
= img
.GetMaskGreen(),
120 mb
= img
.GetMaskBlue();
123 unsigned sr
, sg
, sb
, smask
;
125 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
126 small
.SetMaskColour(mr
, mr
, mr
);
128 for (y
= 0; y
< 16; y
++)
130 for (x
= 0; x
< 16; x
++)
132 sr
= sg
= sb
= smask
= 0;
133 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
134 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
137 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
138 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
141 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
142 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
145 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
146 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
151 ps
[0] = ps
[1] = ps
[2] = mr
;
153 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
156 p1
+= 32 * 3, p2
+= 32 * 3;
159 return small
.ConvertToBitmap();
163 // finds empty borders and return non-empty area of image:
164 static wxImage
CutEmptyBorders(const wxImage
& img
)
166 unsigned char mr
= img
.GetMaskRed(),
167 mg
= img
.GetMaskGreen(),
168 mb
= img
.GetMaskBlue();
169 unsigned char *dt
= img
.GetData(), *dttmp
;
170 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
172 unsigned top
, bottom
, left
, right
, i
;
175 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
176 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
178 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
181 for (i
= 0; i
< w
; i
++, dttmp
+=3)
184 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
187 for (i
= 0; i
< w
; i
++, dttmp
+=3)
190 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
193 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
196 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
199 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
202 top
--, left
--, bottom
++, right
++;
204 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
209 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
211 if (!extension
.IsEmpty())
213 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
214 if (entry
) return (entry
-> id
);
217 wxFileType
*ft
= (mime
.IsEmpty()) ?
218 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
219 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
221 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
223 int newid
= FI_UNKNOWN
;
224 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
230 int id
= m_ImageList
.GetImageCount();
231 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
232 m_ImageList
.Add(img
.ConvertToBitmap());
235 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
236 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
238 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
240 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
246 // ----------------------------------------------------------------------------
248 // ----------------------------------------------------------------------------
251 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
253 wxFileData
*fd1
= (wxFileData
*)data1
;
254 wxFileData
*fd2
= (wxFileData
*)data2
;
255 if (fd1
->GetName() == wxT("..")) return -1;
256 if (fd2
->GetName() == wxT("..")) return 1;
257 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
258 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
259 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
262 //-----------------------------------------------------------------------------
264 //-----------------------------------------------------------------------------
266 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
268 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
274 stat( m_fileName
.fn_str(), &buff
);
278 lstat( m_fileName
.fn_str(), &lbuff
);
279 m_isLink
= S_ISLNK( lbuff
.st_mode
);
280 struct tm
*t
= localtime( &lbuff
.st_mtime
);
283 struct tm
*t
= localtime( &buff
.st_mtime
);
286 // struct passwd *user = getpwuid( buff.st_uid );
287 // struct group *grp = getgrgid( buff.st_gid );
289 m_isDir
= S_ISDIR( buff
.st_mode
);
290 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
292 m_size
= buff
.st_size
;
295 m_minute
= t
->tm_min
;
296 m_month
= t
->tm_mon
+1;
301 m_permissions
.sprintf( wxT("%c%c%c"),
302 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
303 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
304 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
307 wxString
wxFileData::GetName() const
312 wxString
wxFileData::GetFullName() const
317 wxString
wxFileData::GetHint() const
319 wxString s
= m_fileName
;
321 if (m_isDir
) s
+= _("<DIR> ");
322 else if (m_isLink
) s
+= _("<LINK> ");
325 s
+= LongToString( m_size
);
328 s
+= IntToString( m_day
);
330 s
+= IntToString( m_month
);
332 s
+= IntToString( m_year
);
334 s
+= IntToString( m_hour
);
336 s
+= IntToString( m_minute
);
342 wxString
wxFileData::GetEntry( int num
)
354 if (m_isDir
) s
= _("<DIR>");
355 else if (m_isLink
) s
= _("<LINK>");
356 else s
= LongToString( m_size
);
361 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
362 s
+= IntToString( m_day
);
364 if (m_month
< 10) s
+= wxT("0");
365 s
+= IntToString( m_month
);
367 s
+= IntToString( m_year
);
372 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
373 s
+= IntToString( m_hour
);
375 if (m_minute
< 10) s
+= wxT("0");
376 s
+= IntToString( m_minute
);
389 bool wxFileData::IsDir()
394 bool wxFileData::IsExe()
399 bool wxFileData::IsLink()
404 long wxFileData::GetSize()
409 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
415 void wxFileData::MakeItem( wxListItem
&item
)
417 item
.m_text
= m_name
;
418 item
.ClearAttributes();
419 if (IsExe()) item
.SetTextColour(*wxRED
);
420 if (IsDir()) item
.SetTextColour(*wxBLUE
);
423 item
.m_image
= FI_FOLDER
;
425 item
.m_image
= FI_EXECUTABLE
;
426 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
427 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
429 item
.m_image
= FI_UNKNOWN
;
433 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
434 item
.SetTextColour(*dg
);
436 item
.m_data
= (long)this;
439 //-----------------------------------------------------------------------------
441 //-----------------------------------------------------------------------------
443 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
445 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
446 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
447 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
448 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
452 wxFileCtrl::wxFileCtrl()
454 m_dirName
= wxT("/");
455 m_showHidden
= FALSE
;
458 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
459 const wxString
&dirName
, const wxString
&wild
,
460 const wxPoint
&pos
, const wxSize
&size
,
461 long style
, const wxValidator
&validator
, const wxString
&name
) :
462 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
464 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
465 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
467 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
471 m_showHidden
= FALSE
;
475 void wxFileCtrl::ChangeToListMode()
477 SetSingleStyle( wxLC_LIST
);
481 void wxFileCtrl::ChangeToReportMode()
483 SetSingleStyle( wxLC_REPORT
);
487 void wxFileCtrl::ChangeToIconMode()
489 SetSingleStyle( wxLC_ICON
);
493 void wxFileCtrl::ShowHidden( bool show
)
499 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
502 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
503 fd
->MakeItem( item
);
504 long my_style
= GetWindowStyleFlag();
505 if (my_style
& wxLC_REPORT
)
507 ret
= InsertItem( item
);
508 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
510 else if (my_style
& wxLC_LIST
)
512 ret
= InsertItem( item
);
517 void wxFileCtrl::Update()
519 long my_style
= GetWindowStyleFlag();
520 int name_col_width
= 0;
521 if (my_style
& wxLC_REPORT
)
523 if (GetColumnCount() > 0)
524 name_col_width
= GetColumnWidth( 0 );
528 if (my_style
& wxLC_REPORT
)
530 if (name_col_width
< 140) name_col_width
= 140;
531 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
532 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
533 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
534 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
535 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
537 wxFileData
*fd
= (wxFileData
*) NULL
;
542 if (m_dirName
!= wxT("/"))
544 wxString
p( wxPathOnly(m_dirName
) );
545 if (p
.IsEmpty()) p
= wxT("/");
546 fd
= new wxFileData( wxT(".."), p
);
551 wxString res
= m_dirName
+ wxT("/*");
552 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
555 res
= wxFileNameFromPath( f
);
556 fd
= new wxFileData( res
, f
);
557 wxString s
= fd
->GetName();
558 if (m_showHidden
|| (s
[0] != wxT('.')))
563 f
= wxFindNextFile();
566 res
= m_dirName
+ wxT("/") + m_wild
;
567 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
570 res
= wxFileNameFromPath( f
);
571 fd
= new wxFileData( res
, f
);
572 wxString s
= fd
->GetName();
573 if (m_showHidden
|| (s
[0] != wxT('.')))
578 f
= wxFindNextFile();
581 SortItems( ListCompare
, 0 );
583 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
584 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
585 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
588 void wxFileCtrl::SetWild( const wxString
&wild
)
594 void wxFileCtrl::MakeDir()
596 wxString
new_name( wxT("NewName") );
597 wxString
path( m_dirName
);
600 if (wxFileExists(path
))
602 // try NewName0, NewName1 etc.
605 new_name
= _("NewName");
607 num
.Printf( wxT("%d"), i
);
614 } while (wxFileExists(path
));
620 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
625 wxFileData
*fd
= new wxFileData( new_name
, path
);
629 long id
= Add( fd
, item
);
633 SortItems( ListCompare
, 0 );
634 id
= FindItem( 0, (long)fd
);
640 void wxFileCtrl::GoToParentDir()
642 if (m_dirName
!= wxT("/"))
644 wxString
fname( wxFileNameFromPath(m_dirName
) );
645 m_dirName
= wxPathOnly( m_dirName
);
646 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
648 long id
= FindItem( 0, fname
);
651 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
657 void wxFileCtrl::GoToHomeDir()
659 wxString s
= wxGetUserHome( wxString() );
662 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
666 void wxFileCtrl::GoToDir( const wxString
&dir
)
670 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
674 void wxFileCtrl::GetDir( wxString
&dir
)
679 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
681 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
685 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&WXUNUSED(event
) )
688 item
.m_mask
= wxLIST_MASK_DATA
;
690 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
691 while ( item
.m_itemId
!= -1 )
694 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
698 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
702 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
704 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
707 if ((event
.GetLabel().IsEmpty()) ||
708 (event
.GetLabel() == _(".")) ||
709 (event
.GetLabel() == _("..")) ||
710 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
712 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
718 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
719 new_name
+= wxT("/");
720 new_name
+= event
.GetLabel();
724 if (wxFileExists(new_name
))
726 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
731 if (wxRenameFile(fd
->GetFullName(),new_name
))
733 fd
->SetNewName( new_name
, event
.GetLabel() );
734 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
735 EnsureVisible( event
.GetItem() );
739 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
745 //-----------------------------------------------------------------------------
747 //-----------------------------------------------------------------------------
749 #define ID_LIST_MODE wxID_FILEDLGG
750 #define ID_REPORT_MODE wxID_FILEDLGG + 1
751 #define ID_UP_DIR wxID_FILEDLGG + 5
752 #define ID_PARENT_DIR wxID_FILEDLGG + 6
753 #define ID_NEW_DIR wxID_FILEDLGG + 7
754 #define ID_CHOICE wxID_FILEDLGG + 8
755 #define ID_TEXT wxID_FILEDLGG + 9
756 #define ID_LIST_CTRL wxID_FILEDLGG + 10
757 #define ID_ACTIVATED wxID_FILEDLGG + 11
758 #define ID_CHECK wxID_FILEDLGG + 12
760 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
762 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
763 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
764 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
765 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
766 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
767 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
768 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
769 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
770 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
771 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
772 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
773 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
776 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
777 bool wxFileDialog::s_lastShowHidden
= FALSE
;
779 wxFileDialog::wxFileDialog(wxWindow
*parent
,
780 const wxString
& message
,
781 const wxString
& defaultDir
,
782 const wxString
& defaultFile
,
783 const wxString
& wildCard
,
785 const wxPoint
& pos
) :
786 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
790 if (wxConfig::Get(FALSE
))
792 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle
);
793 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden
);
797 m_dialogStyle
= style
;
799 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
800 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
801 m_dialogStyle
|= wxOPEN
;
804 if ((m_dir
.IsEmpty()) || (m_dir
== wxT(".")))
807 m_dir
= getcwd( buf
, sizeof(buf
) );
811 m_path
+= defaultFile
;
812 m_fileName
= defaultFile
;
813 m_wildCard
= wildCard
;
815 m_filterExtension
= wxEmptyString
;
817 // interpret wildcards
819 if (m_wildCard
.IsEmpty())
820 m_wildCard
= _("All files (*)|*");
822 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
824 wxString firstWildText
;
825 if (tokens
.CountTokens() == 1)
827 firstWildText
= tokens
.GetNextToken();
828 firstWild
= firstWildText
;
832 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
833 firstWildText
= tokens
.GetNextToken();
834 firstWild
= tokens
.GetNextToken();
836 if ( firstWild
.Left( 2 ) == wxT("*.") )
837 m_filterExtension
= firstWild
.Mid( 1 );
838 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
842 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
844 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
848 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
850 but
->SetToolTip( _("View files as a list view") );
852 buttonsizer
->Add( but
, 0, wxALL
, 5 );
854 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
856 but
->SetToolTip( _("View files as a detailed view") );
858 buttonsizer
->Add( but
, 0, wxALL
, 5 );
860 buttonsizer
->Add( 30, 5, 1 );
862 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
864 but
->SetToolTip( _("Go to parent directory") );
866 buttonsizer
->Add( but
, 0, wxALL
, 5 );
868 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
870 but
->SetToolTip( _("Go to home directory") );
872 buttonsizer
->Add( but
, 0, wxALL
, 5);
874 buttonsizer
->Add( 20, 20 );
876 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
878 but
->SetToolTip( _("Create new directory") );
880 buttonsizer
->Add( but
, 0, wxALL
, 5 );
882 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
884 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
885 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
886 m_static
= new wxStaticText( this, -1, m_dir
);
887 staticsizer
->Add( m_static
, 1 );
888 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
890 if (m_dialogStyle
& wxMULTIPLE
)
891 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
892 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
);
894 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
895 wxSize(440,180), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
896 m_list
-> ShowHidden(s_lastShowHidden
);
897 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
899 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
900 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
901 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
902 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
903 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
905 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
906 m_choice
= new wxChoice( this, ID_CHOICE
);
907 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
908 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
909 m_check
->SetValue( s_lastShowHidden
);
910 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
911 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
912 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
914 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
915 while (tokens
.HasMoreTokens())
917 firstWildText
= tokens
.GetNextToken();
918 firstWild
= tokens
.GetNextToken();
919 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
921 m_choice
->SetSelection( 0 );
923 SetAutoLayout( TRUE
);
924 SetSizer( mainsizer
);
926 mainsizer
->Fit( this );
927 mainsizer
->SetSizeHints( this );
931 if (m_fileName
.IsEmpty())
939 wxFileDialog::~wxFileDialog()
941 if (wxConfig::Get(FALSE
))
943 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle
);
944 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden
);
948 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
950 int index
= (int)event
.GetInt();
951 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
952 m_list
->SetWild( *str
);
953 m_filterIndex
= index
;
954 if ( str
-> Left( 2 ) == wxT("*.") )
956 m_filterExtension
= str
-> Mid( 1 );
957 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
960 m_filterExtension
= wxEmptyString
;
963 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
965 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
968 void wxFileDialog::OnActivated( wxListEvent
&event
)
970 HandleAction( event
.m_item
.m_text
);
973 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
975 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
976 cevent
.SetEventObject( this );
977 GetEventHandler()->ProcessEvent( cevent
);
980 void wxFileDialog::OnSelected( wxListEvent
&event
)
982 if (FindFocus() != m_list
) return;
984 wxString
filename( event
.m_item
.m_text
);
985 if (filename
== wxT("..")) return;
988 m_list
->GetDir( dir
);
989 if (dir
!= wxT("/")) dir
+= wxT("/");
991 if (wxDirExists(dir
)) return;
993 m_text
->SetValue( filename
);
996 void wxFileDialog::HandleAction( const wxString
&fn
)
998 wxString
filename( fn
);
1000 m_list
->GetDir( dir
);
1001 if (filename
.IsEmpty()) return;
1002 if (filename
== wxT(".")) return;
1004 if (filename
== wxT(".."))
1006 m_list
->GoToParentDir();
1008 m_list
->GetDir( dir
);
1009 m_static
->SetLabel( dir
);
1013 if (filename
== wxT("~"))
1015 m_list
->GoToHomeDir();
1017 m_list
->GetDir( dir
);
1018 m_static
->SetLabel( dir
);
1022 if (filename
[0] == wxT('~'))
1024 filename
.Remove( 0, 1 );
1025 wxString
tmp( wxGetUserHome() );
1031 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1032 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1034 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
1036 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1039 m_list
->SetWild( filename
);
1043 if (dir
!= wxT("/")) dir
+= wxT("/");
1044 if (filename
[0] != wxT('/'))
1050 if (wxDirExists(filename
))
1052 m_list
->GoToDir( filename
);
1053 m_list
->GetDir( dir
);
1054 m_static
->SetLabel( dir
);
1059 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1061 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1062 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1063 filename
<< m_filterExtension
;
1064 if (wxFileExists( filename
))
1067 msg
.Printf( _("File '%s' already exists, do you really want to "
1068 "overwrite it?"), filename
.c_str() );
1070 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1074 else if ( m_dialogStyle
& wxOPEN
)
1076 if ( !wxFileExists( filename
) )
1077 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1078 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1079 filename
<< m_filterExtension
;
1081 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1083 if ( !wxFileExists( filename
) )
1085 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1091 SetPath( filename
);
1093 wxCommandEvent event
;
1094 wxDialog::OnOK(event
);
1097 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1099 HandleAction( m_text
->GetValue() );
1102 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1104 m_list
->ChangeToListMode();
1105 s_lastViewStyle
= wxLC_LIST
;
1109 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1111 m_list
->ChangeToReportMode();
1112 s_lastViewStyle
= wxLC_REPORT
;
1116 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1118 m_list
->GoToParentDir();
1121 m_list
->GetDir( dir
);
1122 m_static
->SetLabel( dir
);
1125 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1127 m_list
->GoToHomeDir();
1130 m_list
->GetDir( dir
);
1131 m_static
->SetLabel( dir
);
1134 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1139 void wxFileDialog::SetPath( const wxString
& path
)
1141 // not only set the full path but also update filename and dir
1146 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1149 m_fileName
+= wxT(".");
1155 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1158 if (m_list
->GetSelectedItemCount() == 0)
1160 paths
.Add( GetPath() );
1164 paths
.Alloc( m_list
->GetSelectedItemCount() );
1167 m_list
->GetDir( dir
);
1168 if (dir
!= wxT("/")) dir
+= wxT("/");
1171 item
.m_mask
= wxLIST_MASK_TEXT
;
1173 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1174 while ( item
.m_itemId
!= -1 )
1176 m_list
->GetItem( item
);
1177 paths
.Add( dir
+ item
.m_text
);
1178 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1179 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1183 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1186 if (m_list
->GetSelectedItemCount() == 0)
1188 files
.Add( GetFilename() );
1191 files
.Alloc( m_list
->GetSelectedItemCount() );
1194 item
.m_mask
= wxLIST_MASK_TEXT
;
1196 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1197 while ( item
.m_itemId
!= -1 )
1199 m_list
->GetItem( item
);
1200 files
.Add( item
.m_text
);
1201 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1202 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1208 // ----------------------------------------------------------------------------
1210 // ----------------------------------------------------------------------------
1213 wxFileSelectorEx(const wxChar
*message
,
1214 const wxChar
*default_path
,
1215 const wxChar
*default_filename
,
1216 int *WXUNUSED(indexDefaultExtension
),
1217 const wxChar
*wildcard
,
1222 // TODO: implement this somehow
1223 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1224 wildcard
, flags
, parent
, x
, y
);
1227 wxString
wxFileSelector( const wxChar
*title
,
1228 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1229 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1230 wxWindow
*parent
, int x
, int y
)
1233 if ( defaultExtension
&& !filter
)
1234 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1238 wxString defaultDirString
;
1240 defaultDirString
= defaultDir
;
1242 wxString defaultFilenameString
;
1243 if (defaultFileName
)
1244 defaultFilenameString
= defaultFileName
;
1246 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1248 if ( fileDialog
.ShowModal() == wxID_OK
)
1250 return fileDialog
.GetPath();
1254 return wxEmptyString
;
1258 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
1260 wxChar
*ext
= (wxChar
*)extension
;
1263 wxString str
= _("Load %s file");
1264 wxSprintf(prompt
, str
, what
);
1266 if (*ext
== wxT('.')) ext
++;
1268 wxSprintf(wild
, wxT("*.%s"), ext
);
1270 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1273 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1276 wxChar
*ext
= (wxChar
*)extension
;
1279 wxString str
= _("Save %s file");
1280 wxSprintf(prompt
, str
, what
);
1282 if (*ext
== wxT('.')) ext
++;
1284 wxSprintf(wild
, wxT("*.%s"), ext
);
1286 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
1294 // A module to allow icons table cleanup
1296 class wxFileDialogGenericModule
: public wxModule
1298 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1300 wxFileDialogGenericModule() {}
1301 bool OnInit() { return TRUE
; }
1302 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1305 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)