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"
51 #include "wx/tooltip.h"
54 #include <sys/types.h>
72 // XPM hack: make the arrays const
73 #define static static const
76 #include "wx/generic/home.xpm"
78 #include "wx/generic/listview.xpm"
79 #include "wx/generic/repview.xpm"
80 #include "wx/generic/new_dir.xpm"
81 #include "wx/generic/dir_up.xpm"
82 #include "wx/generic/folder.xpm"
83 #include "wx/generic/deffile.xpm"
84 #include "wx/generic/exefile.xpm"
88 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
92 class wxFileData
: public wxObject
96 wxFileData( const wxString
&name
, const wxString
&fname
);
97 wxString
GetName() const;
98 wxString
GetFullName() const;
99 wxString
GetHint() const;
100 wxString
GetEntry( int num
);
105 void MakeItem( wxListItem
&item
);
106 void SetNewName( const wxString
&name
, const wxString
&fname
);
117 wxString m_permissions
;
122 DECLARE_DYNAMIC_CLASS(wxFileData
);
125 //-----------------------------------------------------------------------------
127 //-----------------------------------------------------------------------------
129 class wxFileCtrl
: public wxListCtrl
133 wxFileCtrl( wxWindow
*win
,
135 const wxString
&dirName
,
136 const wxString
&wild
,
137 const wxPoint
&pos
= wxDefaultPosition
,
138 const wxSize
&size
= wxDefaultSize
,
139 long style
= wxLC_LIST
,
140 const wxValidator
&validator
= wxDefaultValidator
,
141 const wxString
&name
= wxT("filelist") );
142 void ChangeToListMode();
143 void ChangeToReportMode();
144 void ChangeToIconMode();
145 void ShowHidden( bool show
= TRUE
);
146 long Add( wxFileData
*fd
, wxListItem
&item
);
148 virtual void StatusbarText( wxChar
*WXUNUSED(text
) ) {};
150 void GoToParentDir();
152 void GoToDir( const wxString
&dir
);
153 void SetWild( const wxString
&wild
);
154 void GetDir( wxString
&dir
);
155 void OnListDeleteItem( wxListEvent
&event
);
156 void OnListDeleteAllItems( wxListEvent
&event
);
157 void OnListEndLabelEdit( wxListEvent
&event
);
159 // Associate commonly used UI controls with wxFileCtrl so that they can be
160 // disabled when they cannot be used (e.g. can't go to parent directory
161 // if wxFileCtrl already is in the root dir):
162 void SetGoToParentControl(wxWindow
*ctrl
) { m_goToParentControl
= ctrl
; }
163 void SetNewDirControl(wxWindow
*ctrl
) { m_newDirControl
= ctrl
; }
170 wxWindow
*m_goToParentControl
;
171 wxWindow
*m_newDirControl
;
173 DECLARE_DYNAMIC_CLASS(wxFileCtrl
);
174 DECLARE_EVENT_TABLE()
177 // ----------------------------------------------------------------------------
178 // private classes - icons list management
179 // ----------------------------------------------------------------------------
181 class wxFileIconEntry
: public wxObject
184 wxFileIconEntry(int i
) { id
= i
; }
190 class wxFileIconsTable
195 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
196 wxImageList
*GetImageList() { return &m_ImageList
; }
199 wxImageList m_ImageList
;
200 wxHashTable m_HashTable
;
203 static wxFileIconsTable
*g_IconsTable
= NULL
;
207 #define FI_EXECUTABLE 2
209 wxFileIconsTable::wxFileIconsTable() :
211 m_HashTable(wxKEY_STRING
)
213 m_HashTable
.DeleteContents(TRUE
);
214 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
215 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
216 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
218 m_ImageList
.Add(wxBitmap(exefile_xpm
));
219 m_HashTable
.Delete(_T("exe"));
220 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
222 /* else put into list by GetIconID
223 (KDE defines application/x-executable for *.exe and has nice icon)
230 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
231 // one icon and we won't resize it
233 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
235 wxImage
small(16, 16);
236 unsigned char *p1
, *p2
, *ps
;
237 unsigned char mr
= img
.GetMaskRed(),
238 mg
= img
.GetMaskGreen(),
239 mb
= img
.GetMaskBlue();
242 unsigned sr
, sg
, sb
, smask
;
244 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
245 small
.SetMaskColour(mr
, mr
, mr
);
247 for (y
= 0; y
< 16; y
++)
249 for (x
= 0; x
< 16; x
++)
251 sr
= sg
= sb
= smask
= 0;
252 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
253 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
256 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
257 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
260 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
261 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
264 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
265 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
270 ps
[0] = ps
[1] = ps
[2] = mr
;
272 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
275 p1
+= 32 * 3, p2
+= 32 * 3;
278 return small
.ConvertToBitmap();
281 // finds empty borders and return non-empty area of image:
282 static wxImage
CutEmptyBorders(const wxImage
& img
)
284 unsigned char mr
= img
.GetMaskRed(),
285 mg
= img
.GetMaskGreen(),
286 mb
= img
.GetMaskBlue();
287 unsigned char *dt
= img
.GetData(), *dttmp
;
288 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
290 unsigned top
, bottom
, left
, right
, i
;
293 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
294 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
296 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
299 for (i
= 0; i
< w
; i
++, dttmp
+=3)
302 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
305 for (i
= 0; i
< w
; i
++, dttmp
+=3)
308 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
311 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
314 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
317 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
320 top
--, left
--, bottom
++, right
++;
322 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
324 #endif // wxUSE_MIMETYPE
328 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
331 if (!extension
.IsEmpty())
333 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
334 if (entry
) return (entry
-> id
);
337 wxFileType
*ft
= (mime
.IsEmpty()) ?
338 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
339 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
341 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
343 int newid
= FI_UNKNOWN
;
344 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
350 int id
= m_ImageList
.GetImageCount();
351 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
352 m_ImageList
.Add(img
.ConvertToBitmap());
355 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
356 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
358 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
360 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
363 #else // !wxUSE_MIMETYPE
365 if (extension
== wxT("exe"))
366 return FI_EXECUTABLE
;
369 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
374 // ----------------------------------------------------------------------------
376 // ----------------------------------------------------------------------------
379 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
381 wxFileData
*fd1
= (wxFileData
*)data1
;
382 wxFileData
*fd2
= (wxFileData
*)data2
;
383 if (fd1
->GetName() == wxT("..")) return -1;
384 if (fd2
->GetName() == wxT("..")) return 1;
385 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
386 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
387 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
391 #define IsTopMostDir(dir) (dir == wxT("/"))
394 #if defined(__DOS__) || defined(__WINDOWS__)
395 #define IsTopMostDir(dir) (dir.IsEmpty())
398 #if defined(__DOS__) || defined(__WINDOWS__)
399 extern bool wxIsDriveAvailable(const wxString
& dirName
);
402 //-----------------------------------------------------------------------------
404 //-----------------------------------------------------------------------------
406 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
408 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
413 #if defined(__DOS__) || defined(__WINDOWS__)
414 // VS: In case the file is root directory of a volume (e.g. "C:"),
415 // we don't want it stat()ed, since the drive may not be in:
416 if (name
.length() == 2 && name
[1u] == wxT(':'))
419 m_isExe
= m_isLink
= FALSE
;
426 wxStat( m_fileName
.fn_str(), &buff
);
428 #if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
430 lstat( m_fileName
.fn_str(), &lbuff
);
431 m_isLink
= S_ISLNK( lbuff
.st_mode
);
432 struct tm
*t
= localtime( &lbuff
.st_mtime
);
435 struct tm
*t
= localtime( &buff
.st_mtime
);
438 // struct passwd *user = getpwuid( buff.st_uid );
439 // struct group *grp = getgrgid( buff.st_gid );
441 m_isDir
= S_ISDIR( buff
.st_mode
);
442 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
444 m_size
= buff
.st_size
;
447 m_minute
= t
->tm_min
;
448 m_month
= t
->tm_mon
+1;
453 m_permissions
.sprintf( wxT("%c%c%c"),
454 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
455 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
456 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
459 wxString
wxFileData::GetName() const
464 wxString
wxFileData::GetFullName() const
469 wxString
wxFileData::GetHint() const
471 wxString s
= m_fileName
;
473 if (m_isDir
) s
+= _("<DIR> ");
474 else if (m_isLink
) s
+= _("<LINK> ");
477 s
+= LongToString( m_size
);
480 s
+= IntToString( m_day
);
482 s
+= IntToString( m_month
);
484 s
+= IntToString( m_year
);
486 s
+= IntToString( m_hour
);
488 s
+= IntToString( m_minute
);
494 wxString
wxFileData::GetEntry( int num
)
506 if (m_isDir
) s
= _("<DIR>");
507 else if (m_isLink
) s
= _("<LINK>");
508 else s
= LongToString( m_size
);
513 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
514 s
+= IntToString( m_day
);
516 if (m_month
< 10) s
+= wxT("0");
517 s
+= IntToString( m_month
);
519 s
+= IntToString( m_year
);
524 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
525 s
+= IntToString( m_hour
);
527 if (m_minute
< 10) s
+= wxT("0");
528 s
+= IntToString( m_minute
);
541 bool wxFileData::IsDir()
546 bool wxFileData::IsExe()
551 bool wxFileData::IsLink()
556 long wxFileData::GetSize()
561 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
567 void wxFileData::MakeItem( wxListItem
&item
)
569 item
.m_text
= m_name
;
570 item
.ClearAttributes();
571 if (IsExe()) item
.SetTextColour(*wxRED
);
572 if (IsDir()) item
.SetTextColour(*wxBLUE
);
575 item
.m_image
= FI_FOLDER
;
577 item
.m_image
= FI_EXECUTABLE
;
578 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
579 item
.m_image
= g_IconsTable
->GetIconID(m_name
.AfterLast(wxT('.')));
581 item
.m_image
= FI_UNKNOWN
;
585 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
586 item
.SetTextColour(*dg
);
588 item
.m_data
= (long)this;
591 //-----------------------------------------------------------------------------
593 //-----------------------------------------------------------------------------
595 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
597 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
598 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
599 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
600 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
604 wxFileCtrl::wxFileCtrl()
606 #if defined(__UNIX__)
607 m_dirName
= wxT("/");
608 #elif defined(__DOS__)
611 m_showHidden
= FALSE
;
614 wxFileCtrl::wxFileCtrl(wxWindow
*win
, wxWindowID id
,
615 const wxString
&dirName
, const wxString
&wild
,
616 const wxPoint
&pos
, const wxSize
&size
,
617 long style
, const wxValidator
&validator
,
618 const wxString
&name
)
619 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
)
622 g_IconsTable
= new wxFileIconsTable
;
623 wxImageList
*imageList
= g_IconsTable
->GetImageList();
625 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
627 m_goToParentControl
= m_newDirControl
= NULL
;
631 m_showHidden
= FALSE
;
635 void wxFileCtrl::ChangeToListMode()
637 SetSingleStyle( wxLC_LIST
);
641 void wxFileCtrl::ChangeToReportMode()
643 SetSingleStyle( wxLC_REPORT
);
647 void wxFileCtrl::ChangeToIconMode()
649 SetSingleStyle( wxLC_ICON
);
653 void wxFileCtrl::ShowHidden( bool show
)
659 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
662 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
663 fd
->MakeItem( item
);
664 long my_style
= GetWindowStyleFlag();
665 if (my_style
& wxLC_REPORT
)
668 const int noEntries
= 5;
670 const int noEntries
= 4;
672 ret
= InsertItem( item
);
673 for (int i
= 1; i
< noEntries
; i
++)
674 SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
676 else if (my_style
& wxLC_LIST
)
678 ret
= InsertItem( item
);
683 void wxFileCtrl::UpdateFiles()
685 wxBusyCursor bcur
; // this may take a while...
687 long my_style
= GetWindowStyleFlag();
688 int name_col_width
= 0;
689 if (my_style
& wxLC_REPORT
)
691 if (GetColumnCount() > 0)
692 name_col_width
= GetColumnWidth( 0 );
696 if (my_style
& wxLC_REPORT
)
698 if (name_col_width
< 140) name_col_width
= 140;
699 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
700 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
701 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
702 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
704 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
707 wxFileData
*fd
= (wxFileData
*) NULL
;
712 #if defined(__DOS__) || defined(__WINDOWS__)
713 if ( IsTopMostDir(m_dirName
) )
715 // Pseudo-directory with all available drives listed...
716 for (int drive
= 1; drive
<= 26; drive
++)
719 path
.Printf(wxT("%c:\\"), (char)(drive
+ 'A' - 1));
720 if ( wxIsDriveAvailable(path
) )
723 fd
= new wxFileData(path
, path
);
733 if ( !IsTopMostDir(m_dirName
) )
735 wxString
p(wxPathOnly(m_dirName
));
737 if (p
.IsEmpty()) p
= wxT("/");
739 fd
= new wxFileData( wxT(".."), p
);
744 wxString
dirname(m_dirName
);
745 #if defined(__DOS__) || defined(__WINDOWS__)
746 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
747 dirname
<< wxT('\\');
751 if ( dir
.IsOpened() )
753 wxString
dirPrefix(dirname
+ wxFILE_SEP_PATH
);
754 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
759 // Get the directories first (not matched against wildcards):
760 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
763 fd
= new wxFileData(f
, dirPrefix
+ f
);
766 cont
= dir
.GetNext(&f
);
769 // Tokenize the wildcard string, so we can handle more than 1
770 // search pattern in a wildcard.
771 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
772 while ( tokenWild
.HasMoreTokens() )
774 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
775 wxDIR_FILES
| hiddenFlag
);
778 fd
= new wxFileData(f
, dirPrefix
+ f
);
781 cont
= dir
.GetNext(&f
);
787 SortItems(ListCompare
, 0);
789 if ( my_style
& wxLC_REPORT
)
791 SetColumnWidth(1, wxLIST_AUTOSIZE
);
792 SetColumnWidth(2, wxLIST_AUTOSIZE
);
793 SetColumnWidth(3, wxLIST_AUTOSIZE
);
796 // Finally, enable/disable context-dependent controls:
797 if ( m_goToParentControl
)
798 m_goToParentControl
->Enable(!IsTopMostDir(m_dirName
));
799 #if defined(__DOS__) || defined(__WINDOWS__)
800 if ( m_newDirControl
)
801 m_newDirControl
->Enable(!IsTopMostDir(m_dirName
));
805 void wxFileCtrl::SetWild( const wxString
&wild
)
811 void wxFileCtrl::MakeDir()
813 wxString
new_name( _("NewName") );
814 wxString
path( m_dirName
);
815 path
+= wxFILE_SEP_PATH
;
817 if (wxFileExists(path
))
819 // try NewName0, NewName1 etc.
822 new_name
= _("NewName");
824 num
.Printf( wxT("%d"), i
);
828 path
+= wxFILE_SEP_PATH
;
831 } while (wxFileExists(path
));
837 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
842 wxFileData
*fd
= new wxFileData( new_name
, path
);
846 long id
= Add( fd
, item
);
850 SortItems( ListCompare
, 0 );
851 id
= FindItem( 0, (long)fd
);
857 void wxFileCtrl::GoToParentDir()
859 if (!IsTopMostDir(m_dirName
))
861 size_t len
= m_dirName
.Len();
862 if (m_dirName
[len
-1] == wxFILE_SEP_PATH
)
863 m_dirName
.Remove( len
-1, 1 );
864 wxString
fname( wxFileNameFromPath(m_dirName
) );
865 m_dirName
= wxPathOnly( m_dirName
);
867 if (m_dirName
.IsEmpty())
868 m_dirName
= wxT("/");
871 long id
= FindItem( 0, fname
);
874 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
880 void wxFileCtrl::GoToHomeDir()
882 wxString s
= wxGetUserHome( wxString() );
886 void wxFileCtrl::GoToDir( const wxString
&dir
)
890 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
894 void wxFileCtrl::GetDir( wxString
&dir
)
899 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
901 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
905 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&WXUNUSED(event
) )
908 item
.m_mask
= wxLIST_MASK_DATA
;
910 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
911 while ( item
.m_itemId
!= -1 )
914 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
918 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
922 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
924 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
927 if ((event
.GetLabel().IsEmpty()) ||
928 (event
.GetLabel() == _(".")) ||
929 (event
.GetLabel() == _("..")) ||
930 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
932 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
938 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
939 new_name
+= wxFILE_SEP_PATH
;
940 new_name
+= event
.GetLabel();
944 if (wxFileExists(new_name
))
946 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
951 if (wxRenameFile(fd
->GetFullName(),new_name
))
953 fd
->SetNewName( new_name
, event
.GetLabel() );
954 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
955 EnsureVisible( event
.GetItem() );
959 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
965 //-----------------------------------------------------------------------------
967 //-----------------------------------------------------------------------------
969 #define ID_LIST_MODE (wxID_FILEDLGG )
970 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
971 #define ID_UP_DIR (wxID_FILEDLGG + 5)
972 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
973 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
974 #define ID_CHOICE (wxID_FILEDLGG + 8)
975 #define ID_TEXT (wxID_FILEDLGG + 9)
976 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
977 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
978 #define ID_CHECK (wxID_FILEDLGG + 12)
980 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
982 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
983 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
984 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
985 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
986 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
987 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
988 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
989 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
990 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
991 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
992 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
993 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
996 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
997 bool wxFileDialog::s_lastShowHidden
= FALSE
;
999 wxFileDialog::wxFileDialog(wxWindow
*parent
,
1000 const wxString
& message
,
1001 const wxString
& defaultDir
,
1002 const wxString
& defaultFile
,
1003 const wxString
& wildCard
,
1005 const wxPoint
& pos
) :
1006 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
1010 if (wxConfig::Get(FALSE
))
1012 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1014 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1018 m_message
= message
;
1019 m_dialogStyle
= style
;
1021 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
1022 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
1023 m_dialogStyle
|= wxOPEN
;
1026 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
1031 size_t len
= m_dir
.Len();
1032 if ((len
> 1) && (m_dir
[len
-1] == wxFILE_SEP_PATH
))
1033 m_dir
.Remove( len
-1, 1 );
1036 m_path
+= wxFILE_SEP_PATH
;
1037 m_path
+= defaultFile
;
1038 m_fileName
= defaultFile
;
1039 m_wildCard
= wildCard
;
1041 m_filterExtension
= wxEmptyString
;
1043 // interpret wildcards
1045 if (m_wildCard
.IsEmpty())
1046 m_wildCard
= _("All files (*)|*");
1048 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
1050 wxString firstWildText
;
1051 if (tokens
.CountTokens() == 1)
1053 firstWildText
= tokens
.GetNextToken();
1054 firstWild
= firstWildText
;
1058 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
1059 firstWildText
= tokens
.GetNextToken();
1060 firstWild
= tokens
.GetNextToken();
1062 if ( firstWild
.Left( 2 ) == wxT("*.") )
1063 m_filterExtension
= firstWild
.Mid( 1 );
1064 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
1068 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
1070 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1072 wxBitmapButton
*but
;
1074 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
1076 but
->SetToolTip( _("View files as a list view") );
1078 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1080 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
1082 but
->SetToolTip( _("View files as a detailed view") );
1084 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1086 buttonsizer
->Add( 30, 5, 1 );
1088 wxWindow
*butDirUp
=
1089 new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
1091 butDirUp
->SetToolTip( _("Go to parent directory") );
1093 buttonsizer
->Add( butDirUp
, 0, wxALL
, 5 );
1095 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1096 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
1098 but
->SetToolTip( _("Go to home directory") );
1100 buttonsizer
->Add( but
, 0, wxALL
, 5);
1102 buttonsizer
->Add( 20, 20 );
1105 wxWindow
*butNewDir
=
1106 new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
1108 butNewDir
->SetToolTip( _("Create new directory") );
1110 buttonsizer
->Add( butNewDir
, 0, wxALL
, 5 );
1112 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1114 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1115 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
1116 m_static
= new wxStaticText( this, -1, m_dir
);
1117 staticsizer
->Add( m_static
, 1 );
1118 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1120 if (m_dialogStyle
& wxMULTIPLE
)
1121 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
1122 wxSize(540,200), s_lastViewStyle
| wxSUNKEN_BORDER
);
1124 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
1125 wxSize(540,200), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
1126 m_list
->ShowHidden(s_lastShowHidden
);
1127 m_list
->SetNewDirControl(butNewDir
);
1128 m_list
->SetGoToParentControl(butDirUp
);
1130 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1132 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1133 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1134 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1135 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1136 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1138 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1139 m_choice
= new wxChoice( this, ID_CHOICE
);
1140 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1141 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1142 m_check
->SetValue( s_lastShowHidden
);
1143 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1144 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
1145 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1147 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1148 while (tokens
.HasMoreTokens())
1150 firstWildText
= tokens
.GetNextToken();
1151 firstWild
= tokens
.GetNextToken();
1152 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1154 m_choice
->SetSelection( 0 );
1156 SetAutoLayout( TRUE
);
1157 SetSizer( mainsizer
);
1159 mainsizer
->Fit( this );
1160 mainsizer
->SetSizeHints( this );
1165 if (m_fileName.IsEmpty())
1172 wxFileDialog::~wxFileDialog()
1174 if (wxConfig::Get(FALSE
))
1176 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1178 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1183 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
1185 int index
= (int)event
.GetInt();
1186 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
1187 m_list
->SetWild( *str
);
1188 m_filterIndex
= index
;
1189 if ( str
-> Left( 2 ) == wxT("*.") )
1191 m_filterExtension
= str
-> Mid( 1 );
1192 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
1195 m_filterExtension
= wxEmptyString
;
1198 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
1200 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
1203 void wxFileDialog::OnActivated( wxListEvent
&event
)
1205 HandleAction( event
.m_item
.m_text
);
1208 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1210 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1211 cevent
.SetEventObject( this );
1212 GetEventHandler()->ProcessEvent( cevent
);
1215 void wxFileDialog::OnSelected( wxListEvent
&event
)
1217 if (FindFocus() != m_list
) return;
1219 wxString
filename( event
.m_item
.m_text
);
1220 if (filename
== wxT("..")) return;
1223 m_list
->GetDir( dir
);
1224 if (!IsTopMostDir(dir
))
1225 dir
+= wxFILE_SEP_PATH
;
1227 if (wxDirExists(dir
)) return;
1229 m_text
->SetValue( filename
);
1232 void wxFileDialog::HandleAction( const wxString
&fn
)
1234 wxString
filename( fn
);
1236 m_list
->GetDir( dir
);
1237 if (filename
.IsEmpty()) return;
1238 if (filename
== wxT(".")) return;
1240 if (filename
== wxT(".."))
1242 m_list
->GoToParentDir();
1244 m_list
->GetDir( dir
);
1245 m_static
->SetLabel( dir
);
1250 if (filename
== wxT("~"))
1252 m_list
->GoToHomeDir();
1254 m_list
->GetDir( dir
);
1255 m_static
->SetLabel( dir
);
1259 if (filename
[0u] == wxT('~'))
1261 filename
.Remove( 0, 1 );
1262 wxString
tmp( wxGetUserHome() );
1269 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1270 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1272 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1274 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1277 m_list
->SetWild( filename
);
1281 if (!IsTopMostDir(dir
))
1282 dir
+= wxFILE_SEP_PATH
;
1283 if (!wxIsAbsolutePath(filename
))
1289 if (wxDirExists(filename
))
1291 m_list
->GoToDir( filename
);
1292 m_list
->GetDir( dir
);
1293 m_static
->SetLabel( dir
);
1298 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1300 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1301 filename
.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1302 filename
<< m_filterExtension
;
1303 if (wxFileExists( filename
))
1306 msg
.Printf( _("File '%s' already exists, do you really want to "
1307 "overwrite it?"), filename
.c_str() );
1309 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1313 else if ( m_dialogStyle
& wxOPEN
)
1315 if ( !wxFileExists( filename
) )
1316 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1317 filename
.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1318 filename
<< m_filterExtension
;
1320 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1322 if ( !wxFileExists( filename
) )
1324 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1330 SetPath( filename
);
1332 // change to the directory where the user went if asked
1333 if ( m_dialogStyle
& wxCHANGE_DIR
)
1336 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1338 if ( cwd
!= wxGetWorkingDirectory() )
1340 wxSetWorkingDirectory(cwd
);
1344 wxCommandEvent event
;
1345 wxDialog::OnOK(event
);
1348 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1350 HandleAction( m_text
->GetValue() );
1353 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1355 m_list
->ChangeToListMode();
1356 s_lastViewStyle
= wxLC_LIST
;
1360 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1362 m_list
->ChangeToReportMode();
1363 s_lastViewStyle
= wxLC_REPORT
;
1367 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1369 m_list
->GoToParentDir();
1372 m_list
->GetDir( dir
);
1373 m_static
->SetLabel( dir
);
1376 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1378 m_list
->GoToHomeDir();
1381 m_list
->GetDir( dir
);
1382 m_static
->SetLabel( dir
);
1385 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1390 void wxFileDialog::SetPath( const wxString
& path
)
1392 // not only set the full path but also update filename and dir
1397 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1400 m_fileName
+= wxT(".");
1406 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1409 if (m_list
->GetSelectedItemCount() == 0)
1411 paths
.Add( GetPath() );
1415 paths
.Alloc( m_list
->GetSelectedItemCount() );
1418 m_list
->GetDir( dir
);
1420 if (dir
!= wxT("/"))
1422 dir
+= wxFILE_SEP_PATH
;
1425 item
.m_mask
= wxLIST_MASK_TEXT
;
1427 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1428 while ( item
.m_itemId
!= -1 )
1430 m_list
->GetItem( item
);
1431 paths
.Add( dir
+ item
.m_text
);
1432 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1433 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1437 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1440 if (m_list
->GetSelectedItemCount() == 0)
1442 files
.Add( GetFilename() );
1445 files
.Alloc( m_list
->GetSelectedItemCount() );
1448 item
.m_mask
= wxLIST_MASK_TEXT
;
1450 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1451 while ( item
.m_itemId
!= -1 )
1453 m_list
->GetItem( item
);
1454 files
.Add( item
.m_text
);
1455 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1456 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1462 // ----------------------------------------------------------------------------
1464 // ----------------------------------------------------------------------------
1467 wxFileSelectorEx(const wxChar
*message
,
1468 const wxChar
*default_path
,
1469 const wxChar
*default_filename
,
1470 int *WXUNUSED(indexDefaultExtension
),
1471 const wxChar
*wildcard
,
1476 // TODO: implement this somehow
1477 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1478 wildcard
, flags
, parent
, x
, y
);
1481 wxString
wxFileSelector( const wxChar
*title
,
1482 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1483 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1484 wxWindow
*parent
, int x
, int y
)
1487 if ( defaultExtension
&& !filter
)
1488 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1492 wxString defaultDirString
;
1494 defaultDirString
= defaultDir
;
1496 wxString defaultFilenameString
;
1497 if (defaultFileName
)
1498 defaultFilenameString
= defaultFileName
;
1500 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1502 if ( fileDialog
.ShowModal() == wxID_OK
)
1504 return fileDialog
.GetPath();
1508 return wxEmptyString
;
1512 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*ext
, const wxChar
*default_name
, wxWindow
*parent
)
1514 wxString prompt
= wxString::Format(_("Load %s file"), what
);
1516 if (*ext
== wxT('.'))
1519 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1521 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1522 ext
, wild
, 0, parent
);
1525 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1528 wxChar
*ext
= (wxChar
*)extension
;
1530 wxString prompt
= wxString::Format(_("Save %s file"), what
);
1532 if (*ext
== wxT('.'))
1535 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1537 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1538 ext
, wild
, 0, parent
);
1546 // A module to allow icons table cleanup
1548 class wxFileDialogGenericModule
: public wxModule
1550 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1552 wxFileDialogGenericModule() {}
1553 bool OnInit() { return TRUE
; }
1554 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1557 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)
1559 #endif // wxUSE_FILEDLG