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"
25 #if !defined(__UNIX__) && !defined(__DOS__)
26 #error wxFileDialog currently only supports Unix and DOS
29 #include "wx/checkbox.h"
30 #include "wx/textctrl.h"
31 #include "wx/choice.h"
32 #include "wx/checkbox.h"
33 #include "wx/stattext.h"
34 #include "wx/filedlg.h"
38 #include "wx/listctrl.h"
39 #include "wx/msgdlg.h"
41 #include "wx/bmpbuttn.h"
42 #include "wx/tokenzr.h"
43 #include "wx/mimetype.h"
45 #include "wx/module.h"
46 #include "wx/config.h"
47 #include "wx/imaglist.h"
50 #include "wx/tooltip.h"
53 #include <sys/types.h>
71 #include "wx/generic/home.xpm"
72 #include "wx/generic/listview.xpm"
73 #include "wx/generic/repview.xpm"
74 #include "wx/generic/new_dir.xpm"
75 #include "wx/generic/dir_up.xpm"
76 #include "wx/generic/folder.xpm"
77 #include "wx/generic/deffile.xpm"
78 #include "wx/generic/exefile.xpm"
80 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
84 class wxFileData
: public wxObject
95 wxString m_permissions
;
102 wxFileData( const wxString
&name
, const wxString
&fname
);
103 wxString
GetName() const;
104 wxString
GetFullName() const;
105 wxString
GetHint() const;
106 wxString
GetEntry( int num
);
111 void MakeItem( wxListItem
&item
);
112 void SetNewName( const wxString
&name
, const wxString
&fname
);
115 DECLARE_DYNAMIC_CLASS(wxFileData
);
118 //-----------------------------------------------------------------------------
120 //-----------------------------------------------------------------------------
122 class wxFileCtrl
: public wxListCtrl
131 wxFileCtrl( wxWindow
*win
,
133 const wxString
&dirName
,
134 const wxString
&wild
,
135 const wxPoint
&pos
= wxDefaultPosition
,
136 const wxSize
&size
= wxDefaultSize
,
137 long style
= wxLC_LIST
,
138 const wxValidator
&validator
= wxDefaultValidator
,
139 const wxString
&name
= wxT("filelist") );
140 void ChangeToListMode();
141 void ChangeToReportMode();
142 void ChangeToIconMode();
143 void ShowHidden( bool show
= TRUE
);
144 long Add( wxFileData
*fd
, wxListItem
&item
);
146 virtual void StatusbarText( wxChar
*WXUNUSED(text
) ) {};
148 void GoToParentDir();
150 void GoToDir( const wxString
&dir
);
151 void SetWild( const wxString
&wild
);
152 void GetDir( wxString
&dir
);
153 void OnListDeleteItem( wxListEvent
&event
);
154 void OnListDeleteAllItems( wxListEvent
&event
);
155 void OnListEndLabelEdit( wxListEvent
&event
);
158 DECLARE_DYNAMIC_CLASS(wxFileCtrl
);
159 DECLARE_EVENT_TABLE()
162 // ----------------------------------------------------------------------------
163 // private classes - icons list management
164 // ----------------------------------------------------------------------------
166 class wxFileIconEntry
: public wxObject
169 wxFileIconEntry(int i
) { id
= i
; }
175 class wxFileIconsTable
180 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
181 wxImageList
*GetImageList() { return &m_ImageList
; }
184 wxImageList m_ImageList
;
185 wxHashTable m_HashTable
;
188 static wxFileIconsTable
*g_IconsTable
= NULL
;
192 #define FI_EXECUTABLE 2
194 wxFileIconsTable::wxFileIconsTable() :
196 m_HashTable(wxKEY_STRING
)
198 m_HashTable
.DeleteContents(TRUE
);
199 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
200 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
201 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
203 m_ImageList
.Add(wxBitmap(exefile_xpm
));
204 m_HashTable
.Delete(_T("exe"));
205 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
207 /* else put into list by GetIconID
208 (KDE defines application/x-executable for *.exe and has nice icon)
214 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
216 wxImage
small(16, 16);
217 unsigned char *p1
, *p2
, *ps
;
218 unsigned char mr
= img
.GetMaskRed(),
219 mg
= img
.GetMaskGreen(),
220 mb
= img
.GetMaskBlue();
223 unsigned sr
, sg
, sb
, smask
;
225 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
226 small
.SetMaskColour(mr
, mr
, mr
);
228 for (y
= 0; y
< 16; y
++)
230 for (x
= 0; x
< 16; x
++)
232 sr
= sg
= sb
= smask
= 0;
233 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
234 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
237 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
238 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
241 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
242 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
245 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
246 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
251 ps
[0] = ps
[1] = ps
[2] = mr
;
253 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
256 p1
+= 32 * 3, p2
+= 32 * 3;
259 return small
.ConvertToBitmap();
263 // finds empty borders and return non-empty area of image:
264 static wxImage
CutEmptyBorders(const wxImage
& img
)
266 unsigned char mr
= img
.GetMaskRed(),
267 mg
= img
.GetMaskGreen(),
268 mb
= img
.GetMaskBlue();
269 unsigned char *dt
= img
.GetData(), *dttmp
;
270 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
272 unsigned top
, bottom
, left
, right
, i
;
275 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
276 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
278 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
281 for (i
= 0; i
< w
; i
++, dttmp
+=3)
284 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
287 for (i
= 0; i
< w
; i
++, dttmp
+=3)
290 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
293 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
296 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
299 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
302 top
--, left
--, bottom
++, right
++;
304 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
309 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
312 if (!extension
.IsEmpty())
314 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
315 if (entry
) return (entry
-> id
);
318 wxFileType
*ft
= (mime
.IsEmpty()) ?
319 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
320 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
322 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
324 int newid
= FI_UNKNOWN
;
325 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
331 int id
= m_ImageList
.GetImageCount();
332 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
333 m_ImageList
.Add(img
.ConvertToBitmap());
336 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
337 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
339 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
341 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
344 #else // !wxUSE_MIMETYPE
346 if (extension
== wxT("exe"))
347 return FI_EXECUTABLE
;
350 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
355 // ----------------------------------------------------------------------------
357 // ----------------------------------------------------------------------------
360 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
362 wxFileData
*fd1
= (wxFileData
*)data1
;
363 wxFileData
*fd2
= (wxFileData
*)data2
;
364 if (fd1
->GetName() == wxT("..")) return -1;
365 if (fd2
->GetName() == wxT("..")) return 1;
366 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
367 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
368 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
371 //-----------------------------------------------------------------------------
373 //-----------------------------------------------------------------------------
375 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
377 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
383 stat( m_fileName
.fn_str(), &buff
);
385 #if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
387 lstat( m_fileName
.fn_str(), &lbuff
);
388 m_isLink
= S_ISLNK( lbuff
.st_mode
);
389 struct tm
*t
= localtime( &lbuff
.st_mtime
);
392 struct tm
*t
= localtime( &buff
.st_mtime
);
395 // struct passwd *user = getpwuid( buff.st_uid );
396 // struct group *grp = getgrgid( buff.st_gid );
398 m_isDir
= S_ISDIR( buff
.st_mode
);
399 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
401 m_size
= buff
.st_size
;
404 m_minute
= t
->tm_min
;
405 m_month
= t
->tm_mon
+1;
410 m_permissions
.sprintf( wxT("%c%c%c"),
411 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
412 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
413 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
416 wxString
wxFileData::GetName() const
421 wxString
wxFileData::GetFullName() const
426 wxString
wxFileData::GetHint() const
428 wxString s
= m_fileName
;
430 if (m_isDir
) s
+= _("<DIR> ");
431 else if (m_isLink
) s
+= _("<LINK> ");
434 s
+= LongToString( m_size
);
437 s
+= IntToString( m_day
);
439 s
+= IntToString( m_month
);
441 s
+= IntToString( m_year
);
443 s
+= IntToString( m_hour
);
445 s
+= IntToString( m_minute
);
451 wxString
wxFileData::GetEntry( int num
)
463 if (m_isDir
) s
= _("<DIR>");
464 else if (m_isLink
) s
= _("<LINK>");
465 else s
= LongToString( m_size
);
470 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
471 s
+= IntToString( m_day
);
473 if (m_month
< 10) s
+= wxT("0");
474 s
+= IntToString( m_month
);
476 s
+= IntToString( m_year
);
481 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
482 s
+= IntToString( m_hour
);
484 if (m_minute
< 10) s
+= wxT("0");
485 s
+= IntToString( m_minute
);
498 bool wxFileData::IsDir()
503 bool wxFileData::IsExe()
508 bool wxFileData::IsLink()
513 long wxFileData::GetSize()
518 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
524 void wxFileData::MakeItem( wxListItem
&item
)
526 item
.m_text
= m_name
;
527 item
.ClearAttributes();
528 if (IsExe()) item
.SetTextColour(*wxRED
);
529 if (IsDir()) item
.SetTextColour(*wxBLUE
);
532 item
.m_image
= FI_FOLDER
;
534 item
.m_image
= FI_EXECUTABLE
;
535 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
536 item
.m_image
= g_IconsTable
-> GetIconID(m_name
.AfterLast(wxT('.')));
538 item
.m_image
= FI_UNKNOWN
;
542 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
543 item
.SetTextColour(*dg
);
545 item
.m_data
= (long)this;
548 //-----------------------------------------------------------------------------
550 //-----------------------------------------------------------------------------
552 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
554 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
555 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
556 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
557 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
561 wxFileCtrl::wxFileCtrl()
563 m_dirName
= wxT("/");
564 m_showHidden
= FALSE
;
567 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
568 const wxString
&dirName
, const wxString
&wild
,
569 const wxPoint
&pos
, const wxSize
&size
,
570 long style
, const wxValidator
&validator
, const wxString
&name
) :
571 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
573 if (! g_IconsTable
) g_IconsTable
= new wxFileIconsTable
;
574 wxImageList
*imageList
= g_IconsTable
-> GetImageList();
576 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
580 m_showHidden
= FALSE
;
584 void wxFileCtrl::ChangeToListMode()
586 SetSingleStyle( wxLC_LIST
);
590 void wxFileCtrl::ChangeToReportMode()
592 SetSingleStyle( wxLC_REPORT
);
596 void wxFileCtrl::ChangeToIconMode()
598 SetSingleStyle( wxLC_ICON
);
602 void wxFileCtrl::ShowHidden( bool show
)
608 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
611 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
612 fd
->MakeItem( item
);
613 long my_style
= GetWindowStyleFlag();
614 if (my_style
& wxLC_REPORT
)
616 ret
= InsertItem( item
);
617 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
619 else if (my_style
& wxLC_LIST
)
621 ret
= InsertItem( item
);
626 void wxFileCtrl::Update()
628 long my_style
= GetWindowStyleFlag();
629 int name_col_width
= 0;
630 if (my_style
& wxLC_REPORT
)
632 if (GetColumnCount() > 0)
633 name_col_width
= GetColumnWidth( 0 );
637 if (my_style
& wxLC_REPORT
)
639 if (name_col_width
< 140) name_col_width
= 140;
640 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
641 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
642 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
643 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
644 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
646 wxFileData
*fd
= (wxFileData
*) NULL
;
651 if (m_dirName
!= wxT("/"))
653 wxString
p( wxPathOnly(m_dirName
) );
654 if (p
.IsEmpty()) p
= wxT("/");
655 fd
= new wxFileData( wxT(".."), p
);
660 wxString res
= m_dirName
+ wxT("/*");
661 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
664 res
= wxFileNameFromPath( f
);
665 fd
= new wxFileData( res
, f
);
666 wxString s
= fd
->GetName();
667 if (m_showHidden
|| (s
[0u] != wxT('.')))
672 f
= wxFindNextFile();
675 // Tokenize the wildcard string, so we can handle more than 1
676 // search pattern in a wildcard.
677 wxStringTokenizer
tokenWild( m_wild
, ";" );
678 while ( tokenWild
.HasMoreTokens() )
680 res
= m_dirName
+ wxT("/") + tokenWild
.GetNextToken();
681 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
684 res
= wxFileNameFromPath( f
);
685 fd
= new wxFileData( res
, f
);
686 wxString s
= fd
->GetName();
687 if (m_showHidden
|| (s
[0u] != wxT('.')))
692 f
= wxFindNextFile();
696 SortItems( ListCompare
, 0 );
698 if (my_style
& wxLC_REPORT
)
700 SetColumnWidth( 1, wxLIST_AUTOSIZE
);
701 SetColumnWidth( 2, wxLIST_AUTOSIZE
);
702 SetColumnWidth( 3, wxLIST_AUTOSIZE
);
706 void wxFileCtrl::SetWild( const wxString
&wild
)
712 void wxFileCtrl::MakeDir()
714 wxString
new_name( wxT("NewName") );
715 wxString
path( m_dirName
);
718 if (wxFileExists(path
))
720 // try NewName0, NewName1 etc.
723 new_name
= _("NewName");
725 num
.Printf( wxT("%d"), i
);
732 } while (wxFileExists(path
));
738 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
743 wxFileData
*fd
= new wxFileData( new_name
, path
);
747 long id
= Add( fd
, item
);
751 SortItems( ListCompare
, 0 );
752 id
= FindItem( 0, (long)fd
);
758 void wxFileCtrl::GoToParentDir()
760 if (m_dirName
!= wxT("/"))
762 size_t len
= m_dirName
.Len();
763 if (m_dirName
[len
-1] == wxT('/'))
764 m_dirName
.Remove( len
-1, 1 );
765 wxString
fname( wxFileNameFromPath(m_dirName
) );
766 m_dirName
= wxPathOnly( m_dirName
);
767 if (m_dirName
.IsEmpty()) m_dirName
= wxT("/");
769 long id
= FindItem( 0, fname
);
772 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
778 void wxFileCtrl::GoToHomeDir()
780 wxString s
= wxGetUserHome( wxString() );
783 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
787 void wxFileCtrl::GoToDir( const wxString
&dir
)
791 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
795 void wxFileCtrl::GetDir( wxString
&dir
)
800 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
802 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
806 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&WXUNUSED(event
) )
809 item
.m_mask
= wxLIST_MASK_DATA
;
811 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
812 while ( item
.m_itemId
!= -1 )
815 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
819 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
823 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
825 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
828 if ((event
.GetLabel().IsEmpty()) ||
829 (event
.GetLabel() == _(".")) ||
830 (event
.GetLabel() == _("..")) ||
831 (event
.GetLabel().First( wxT("/") ) != wxNOT_FOUND
))
833 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
839 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
840 new_name
+= wxT("/");
841 new_name
+= event
.GetLabel();
845 if (wxFileExists(new_name
))
847 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
852 if (wxRenameFile(fd
->GetFullName(),new_name
))
854 fd
->SetNewName( new_name
, event
.GetLabel() );
855 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
856 EnsureVisible( event
.GetItem() );
860 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
866 //-----------------------------------------------------------------------------
868 //-----------------------------------------------------------------------------
870 #define ID_LIST_MODE wxID_FILEDLGG
871 #define ID_REPORT_MODE wxID_FILEDLGG + 1
872 #define ID_UP_DIR wxID_FILEDLGG + 5
873 #define ID_PARENT_DIR wxID_FILEDLGG + 6
874 #define ID_NEW_DIR wxID_FILEDLGG + 7
875 #define ID_CHOICE wxID_FILEDLGG + 8
876 #define ID_TEXT wxID_FILEDLGG + 9
877 #define ID_LIST_CTRL wxID_FILEDLGG + 10
878 #define ID_ACTIVATED wxID_FILEDLGG + 11
879 #define ID_CHECK wxID_FILEDLGG + 12
881 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
883 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
884 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
885 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
886 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
887 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
888 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
889 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
890 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
891 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
892 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
893 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
894 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
897 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
898 bool wxFileDialog::s_lastShowHidden
= FALSE
;
900 wxFileDialog::wxFileDialog(wxWindow
*parent
,
901 const wxString
& message
,
902 const wxString
& defaultDir
,
903 const wxString
& defaultFile
,
904 const wxString
& wildCard
,
906 const wxPoint
& pos
) :
907 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
911 if (wxConfig::Get(FALSE
))
913 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle
);
914 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden
);
918 m_dialogStyle
= style
;
920 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
921 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
922 m_dialogStyle
|= wxOPEN
;
925 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
928 m_dir
= getcwd( buf
, sizeof(buf
) );
931 size_t len
= m_dir
.Len();
932 if ((len
> 1) && (m_dir
[len
-1] == wxT('/')))
933 m_dir
.Remove( len
-1, 1 );
937 m_path
+= defaultFile
;
938 m_fileName
= defaultFile
;
939 m_wildCard
= wildCard
;
941 m_filterExtension
= wxEmptyString
;
943 // interpret wildcards
945 if (m_wildCard
.IsEmpty())
946 m_wildCard
= _("All files (*)|*");
948 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
950 wxString firstWildText
;
951 if (tokens
.CountTokens() == 1)
953 firstWildText
= tokens
.GetNextToken();
954 firstWild
= firstWildText
;
958 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
959 firstWildText
= tokens
.GetNextToken();
960 firstWild
= tokens
.GetNextToken();
962 if ( firstWild
.Left( 2 ) == wxT("*.") )
963 m_filterExtension
= firstWild
.Mid( 1 );
964 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
968 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
970 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
974 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
976 but
->SetToolTip( _("View files as a list view") );
978 buttonsizer
->Add( but
, 0, wxALL
, 5 );
980 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
982 but
->SetToolTip( _("View files as a detailed view") );
984 buttonsizer
->Add( but
, 0, wxALL
, 5 );
986 buttonsizer
->Add( 30, 5, 1 );
988 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
990 but
->SetToolTip( _("Go to parent directory") );
992 buttonsizer
->Add( but
, 0, wxALL
, 5 );
994 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
996 but
->SetToolTip( _("Go to home directory") );
998 buttonsizer
->Add( but
, 0, wxALL
, 5);
1000 buttonsizer
->Add( 20, 20 );
1002 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
1004 but
->SetToolTip( _("Create new directory") );
1006 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1008 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1010 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1011 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
1012 m_static
= new wxStaticText( this, -1, m_dir
);
1013 staticsizer
->Add( m_static
, 1 );
1014 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1016 if (m_dialogStyle
& wxMULTIPLE
)
1017 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
1018 wxSize(540,200), s_lastViewStyle
| wxSUNKEN_BORDER
);
1020 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
1021 wxSize(540,200), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
1022 m_list
-> ShowHidden(s_lastShowHidden
);
1023 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1025 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1026 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1027 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1028 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1029 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1031 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1032 m_choice
= new wxChoice( this, ID_CHOICE
);
1033 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1034 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1035 m_check
->SetValue( s_lastShowHidden
);
1036 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1037 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
1038 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1040 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1041 while (tokens
.HasMoreTokens())
1043 firstWildText
= tokens
.GetNextToken();
1044 firstWild
= tokens
.GetNextToken();
1045 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1047 m_choice
->SetSelection( 0 );
1049 SetAutoLayout( TRUE
);
1050 SetSizer( mainsizer
);
1052 mainsizer
->Fit( this );
1053 mainsizer
->SetSizeHints( this );
1058 if (m_fileName.IsEmpty())
1067 wxFileDialog::~wxFileDialog()
1069 if (wxConfig::Get(FALSE
))
1071 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle
);
1072 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden
);
1076 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
1078 int index
= (int)event
.GetInt();
1079 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
1080 m_list
->SetWild( *str
);
1081 m_filterIndex
= index
;
1082 if ( str
-> Left( 2 ) == wxT("*.") )
1084 m_filterExtension
= str
-> Mid( 1 );
1085 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
1088 m_filterExtension
= wxEmptyString
;
1091 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
1093 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
1096 void wxFileDialog::OnActivated( wxListEvent
&event
)
1098 HandleAction( event
.m_item
.m_text
);
1101 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1103 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1104 cevent
.SetEventObject( this );
1105 GetEventHandler()->ProcessEvent( cevent
);
1108 void wxFileDialog::OnSelected( wxListEvent
&event
)
1110 if (FindFocus() != m_list
) return;
1112 wxString
filename( event
.m_item
.m_text
);
1113 if (filename
== wxT("..")) return;
1116 m_list
->GetDir( dir
);
1117 if (dir
!= wxT("/")) dir
+= wxT("/");
1119 if (wxDirExists(dir
)) return;
1121 m_text
->SetValue( filename
);
1124 void wxFileDialog::HandleAction( const wxString
&fn
)
1126 wxString
filename( fn
);
1128 m_list
->GetDir( dir
);
1129 if (filename
.IsEmpty()) return;
1130 if (filename
== wxT(".")) return;
1132 if (filename
== wxT(".."))
1134 m_list
->GoToParentDir();
1136 m_list
->GetDir( dir
);
1137 m_static
->SetLabel( dir
);
1141 if (filename
== wxT("~"))
1143 m_list
->GoToHomeDir();
1145 m_list
->GetDir( dir
);
1146 m_static
->SetLabel( dir
);
1150 if (filename
[0u] == wxT('~'))
1152 filename
.Remove( 0, 1 );
1153 wxString
tmp( wxGetUserHome() );
1159 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1160 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1162 if (filename
.Find(wxT('/')) != wxNOT_FOUND
)
1164 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1167 m_list
->SetWild( filename
);
1171 if (dir
!= wxT("/")) dir
+= wxT("/");
1172 if (filename
[0u] != wxT('/'))
1178 if (wxDirExists(filename
))
1180 m_list
->GoToDir( filename
);
1181 m_list
->GetDir( dir
);
1182 m_static
->SetLabel( dir
);
1187 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1189 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1190 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1191 filename
<< m_filterExtension
;
1192 if (wxFileExists( filename
))
1195 msg
.Printf( _("File '%s' already exists, do you really want to "
1196 "overwrite it?"), filename
.c_str() );
1198 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1202 else if ( m_dialogStyle
& wxOPEN
)
1204 if ( !wxFileExists( filename
) )
1205 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1206 filename
.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND
)
1207 filename
<< m_filterExtension
;
1209 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1211 if ( !wxFileExists( filename
) )
1213 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1219 SetPath( filename
);
1221 // change to the directory where the user went if asked
1222 if ( m_dialogStyle
& wxCHANGE_DIR
)
1225 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1227 if ( cwd
!= wxGetWorkingDirectory() )
1229 wxSetWorkingDirectory(cwd
);
1233 wxCommandEvent event
;
1234 wxDialog::OnOK(event
);
1237 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1239 HandleAction( m_text
->GetValue() );
1242 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1244 m_list
->ChangeToListMode();
1245 s_lastViewStyle
= wxLC_LIST
;
1249 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1251 m_list
->ChangeToReportMode();
1252 s_lastViewStyle
= wxLC_REPORT
;
1256 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1258 m_list
->GoToParentDir();
1261 m_list
->GetDir( dir
);
1262 m_static
->SetLabel( dir
);
1265 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1267 m_list
->GoToHomeDir();
1270 m_list
->GetDir( dir
);
1271 m_static
->SetLabel( dir
);
1274 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1279 void wxFileDialog::SetPath( const wxString
& path
)
1281 // not only set the full path but also update filename and dir
1286 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1289 m_fileName
+= wxT(".");
1295 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1298 if (m_list
->GetSelectedItemCount() == 0)
1300 paths
.Add( GetPath() );
1304 paths
.Alloc( m_list
->GetSelectedItemCount() );
1307 m_list
->GetDir( dir
);
1308 if (dir
!= wxT("/")) dir
+= wxT("/");
1311 item
.m_mask
= wxLIST_MASK_TEXT
;
1313 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1314 while ( item
.m_itemId
!= -1 )
1316 m_list
->GetItem( item
);
1317 paths
.Add( dir
+ item
.m_text
);
1318 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1319 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1323 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1326 if (m_list
->GetSelectedItemCount() == 0)
1328 files
.Add( GetFilename() );
1331 files
.Alloc( m_list
->GetSelectedItemCount() );
1334 item
.m_mask
= wxLIST_MASK_TEXT
;
1336 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1337 while ( item
.m_itemId
!= -1 )
1339 m_list
->GetItem( item
);
1340 files
.Add( item
.m_text
);
1341 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1342 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1348 // ----------------------------------------------------------------------------
1350 // ----------------------------------------------------------------------------
1353 wxFileSelectorEx(const wxChar
*message
,
1354 const wxChar
*default_path
,
1355 const wxChar
*default_filename
,
1356 int *WXUNUSED(indexDefaultExtension
),
1357 const wxChar
*wildcard
,
1362 // TODO: implement this somehow
1363 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1364 wildcard
, flags
, parent
, x
, y
);
1367 wxString
wxFileSelector( const wxChar
*title
,
1368 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1369 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1370 wxWindow
*parent
, int x
, int y
)
1373 if ( defaultExtension
&& !filter
)
1374 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1378 wxString defaultDirString
;
1380 defaultDirString
= defaultDir
;
1382 wxString defaultFilenameString
;
1383 if (defaultFileName
)
1384 defaultFilenameString
= defaultFileName
;
1386 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1388 if ( fileDialog
.ShowModal() == wxID_OK
)
1390 return fileDialog
.GetPath();
1394 return wxEmptyString
;
1398 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*ext
, const wxChar
*default_name
, wxWindow
*parent
)
1400 wxString prompt
= wxString::Format(_("Load %s file"), what
);
1402 if (*ext
== wxT('.'))
1405 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1407 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1408 ext
, wild
, 0, parent
);
1411 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1414 wxChar
*ext
= (wxChar
*)extension
;
1416 wxString prompt
= wxString::Format(_("Save %s file"), what
);
1418 if (*ext
== wxT('.'))
1421 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1423 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1424 ext
, wild
, 0, parent
);
1432 // A module to allow icons table cleanup
1434 class wxFileDialogGenericModule
: public wxModule
1436 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1438 wxFileDialogGenericModule() {}
1439 bool OnInit() { return TRUE
; }
1440 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1443 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)
1445 #endif // wxUSE_FILEDLG