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>
73 #include "wx/generic/home.xpm"
75 #include "wx/generic/listview.xpm"
76 #include "wx/generic/repview.xpm"
77 #include "wx/generic/new_dir.xpm"
78 #include "wx/generic/dir_up.xpm"
79 #include "wx/generic/folder.xpm"
80 #include "wx/generic/deffile.xpm"
81 #include "wx/generic/exefile.xpm"
83 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
87 class wxFileData
: public wxObject
91 wxFileData( const wxString
&name
, const wxString
&fname
);
92 wxString
GetName() const;
93 wxString
GetFullName() const;
94 wxString
GetHint() const;
95 wxString
GetEntry( int num
);
100 void MakeItem( wxListItem
&item
);
101 void SetNewName( const wxString
&name
, const wxString
&fname
);
112 wxString m_permissions
;
117 DECLARE_DYNAMIC_CLASS(wxFileData
);
120 //-----------------------------------------------------------------------------
122 //-----------------------------------------------------------------------------
124 class wxFileCtrl
: public wxListCtrl
128 wxFileCtrl( wxWindow
*win
,
130 const wxString
&dirName
,
131 const wxString
&wild
,
132 const wxPoint
&pos
= wxDefaultPosition
,
133 const wxSize
&size
= wxDefaultSize
,
134 long style
= wxLC_LIST
,
135 const wxValidator
&validator
= wxDefaultValidator
,
136 const wxString
&name
= wxT("filelist") );
137 void ChangeToListMode();
138 void ChangeToReportMode();
139 void ChangeToIconMode();
140 void ShowHidden( bool show
= TRUE
);
141 long Add( wxFileData
*fd
, wxListItem
&item
);
143 virtual void StatusbarText( wxChar
*WXUNUSED(text
) ) {};
145 void GoToParentDir();
147 void GoToDir( const wxString
&dir
);
148 void SetWild( const wxString
&wild
);
149 void GetDir( wxString
&dir
);
150 void OnListDeleteItem( wxListEvent
&event
);
151 void OnListDeleteAllItems( wxListEvent
&event
);
152 void OnListEndLabelEdit( wxListEvent
&event
);
154 // Associate commonly used UI controls with wxFileCtrl so that they can be
155 // disabled when they cannot be used (e.g. can't go to parent directory
156 // if wxFileCtrl already is in the root dir):
157 void SetGoToParentControl(wxWindow
*ctrl
) { m_goToParentControl
= ctrl
; }
158 void SetNewDirControl(wxWindow
*ctrl
) { m_newDirControl
= ctrl
; }
165 wxWindow
*m_goToParentControl
;
166 wxWindow
*m_newDirControl
;
168 DECLARE_DYNAMIC_CLASS(wxFileCtrl
);
169 DECLARE_EVENT_TABLE()
172 // ----------------------------------------------------------------------------
173 // private classes - icons list management
174 // ----------------------------------------------------------------------------
176 class wxFileIconEntry
: public wxObject
179 wxFileIconEntry(int i
) { id
= i
; }
185 class wxFileIconsTable
190 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
191 wxImageList
*GetImageList() { return &m_ImageList
; }
194 wxImageList m_ImageList
;
195 wxHashTable m_HashTable
;
198 static wxFileIconsTable
*g_IconsTable
= NULL
;
202 #define FI_EXECUTABLE 2
204 wxFileIconsTable::wxFileIconsTable() :
206 m_HashTable(wxKEY_STRING
)
208 m_HashTable
.DeleteContents(TRUE
);
209 m_ImageList
.Add(wxBitmap(folder_xpm
)); // FI_FOLDER
210 m_ImageList
.Add(wxBitmap(deffile_xpm
)); // FI_UNKNOWN
211 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
213 m_ImageList
.Add(wxBitmap(exefile_xpm
));
214 m_HashTable
.Delete(_T("exe"));
215 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
217 /* else put into list by GetIconID
218 (KDE defines application/x-executable for *.exe and has nice icon)
225 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
226 // one icon and we won't resize it
228 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
230 wxImage
small(16, 16);
231 unsigned char *p1
, *p2
, *ps
;
232 unsigned char mr
= img
.GetMaskRed(),
233 mg
= img
.GetMaskGreen(),
234 mb
= img
.GetMaskBlue();
237 unsigned sr
, sg
, sb
, smask
;
239 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
240 small
.SetMaskColour(mr
, mr
, mr
);
242 for (y
= 0; y
< 16; y
++)
244 for (x
= 0; x
< 16; x
++)
246 sr
= sg
= sb
= smask
= 0;
247 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
248 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
251 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
252 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
255 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
256 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
259 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
260 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
265 ps
[0] = ps
[1] = ps
[2] = mr
;
267 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
270 p1
+= 32 * 3, p2
+= 32 * 3;
273 return small
.ConvertToBitmap();
276 // finds empty borders and return non-empty area of image:
277 static wxImage
CutEmptyBorders(const wxImage
& img
)
279 unsigned char mr
= img
.GetMaskRed(),
280 mg
= img
.GetMaskGreen(),
281 mb
= img
.GetMaskBlue();
282 unsigned char *dt
= img
.GetData(), *dttmp
;
283 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
285 unsigned top
, bottom
, left
, right
, i
;
288 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
289 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
291 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
294 for (i
= 0; i
< w
; i
++, dttmp
+=3)
297 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
300 for (i
= 0; i
< w
; i
++, dttmp
+=3)
303 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
306 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
309 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
312 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
315 top
--, left
--, bottom
++, right
++;
317 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
319 #endif // wxUSE_MIMETYPE
323 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
326 if (!extension
.IsEmpty())
328 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
329 if (entry
) return (entry
-> id
);
332 wxFileType
*ft
= (mime
.IsEmpty()) ?
333 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
334 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
336 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
338 int newid
= FI_UNKNOWN
;
339 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
345 int id
= m_ImageList
.GetImageCount();
346 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
347 m_ImageList
.Add(img
.ConvertToBitmap());
350 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
351 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
353 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
355 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
358 #else // !wxUSE_MIMETYPE
360 if (extension
== wxT("exe"))
361 return FI_EXECUTABLE
;
364 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
374 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
376 wxFileData
*fd1
= (wxFileData
*)data1
;
377 wxFileData
*fd2
= (wxFileData
*)data2
;
378 if (fd1
->GetName() == wxT("..")) return -1;
379 if (fd2
->GetName() == wxT("..")) return 1;
380 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
381 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
382 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
386 #define IsTopMostDir(dir) (dir == wxT("/"))
389 #if defined(__DOS__) || defined(__WINDOWS__)
390 #define IsTopMostDir(dir) (dir.IsEmpty())
393 #if defined(__DOS__) || defined(__WINDOWS__)
394 extern bool wxIsDriveAvailable(const wxString
& dirName
);
397 //-----------------------------------------------------------------------------
399 //-----------------------------------------------------------------------------
401 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
403 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
408 #if defined(__DOS__) || defined(__WINDOWS__)
409 // VS: In case the file is root directory of a volume (e.g. "C:"),
410 // we don't want it stat()ed, since the drive may not be in:
411 if (name
.length() == 2 && name
[1u] == wxT(':'))
414 m_isExe
= m_isLink
= FALSE
;
421 wxStat( m_fileName
.fn_str(), &buff
);
423 #if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
425 lstat( m_fileName
.fn_str(), &lbuff
);
426 m_isLink
= S_ISLNK( lbuff
.st_mode
);
427 struct tm
*t
= localtime( &lbuff
.st_mtime
);
430 struct tm
*t
= localtime( &buff
.st_mtime
);
433 // struct passwd *user = getpwuid( buff.st_uid );
434 // struct group *grp = getgrgid( buff.st_gid );
436 m_isDir
= S_ISDIR( buff
.st_mode
);
437 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
439 m_size
= buff
.st_size
;
442 m_minute
= t
->tm_min
;
443 m_month
= t
->tm_mon
+1;
448 m_permissions
.sprintf( wxT("%c%c%c"),
449 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? wxT('r') : wxT('-')),
450 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? wxT('w') : wxT('-')),
451 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? wxT('x') : wxT('-')) );
454 wxString
wxFileData::GetName() const
459 wxString
wxFileData::GetFullName() const
464 wxString
wxFileData::GetHint() const
466 wxString s
= m_fileName
;
468 if (m_isDir
) s
+= _("<DIR> ");
469 else if (m_isLink
) s
+= _("<LINK> ");
472 s
+= LongToString( m_size
);
475 s
+= IntToString( m_day
);
477 s
+= IntToString( m_month
);
479 s
+= IntToString( m_year
);
481 s
+= IntToString( m_hour
);
483 s
+= IntToString( m_minute
);
489 wxString
wxFileData::GetEntry( int num
)
501 if (m_isDir
) s
= _("<DIR>");
502 else if (m_isLink
) s
= _("<LINK>");
503 else s
= LongToString( m_size
);
508 if (m_day
< 10) s
= wxT("0"); else s
= wxT("");
509 s
+= IntToString( m_day
);
511 if (m_month
< 10) s
+= wxT("0");
512 s
+= IntToString( m_month
);
514 s
+= IntToString( m_year
);
519 if (m_hour
< 10) s
= wxT("0"); else s
= wxT("");
520 s
+= IntToString( m_hour
);
522 if (m_minute
< 10) s
+= wxT("0");
523 s
+= IntToString( m_minute
);
536 bool wxFileData::IsDir()
541 bool wxFileData::IsExe()
546 bool wxFileData::IsLink()
551 long wxFileData::GetSize()
556 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
562 void wxFileData::MakeItem( wxListItem
&item
)
564 item
.m_text
= m_name
;
565 item
.ClearAttributes();
566 if (IsExe()) item
.SetTextColour(*wxRED
);
567 if (IsDir()) item
.SetTextColour(*wxBLUE
);
570 item
.m_image
= FI_FOLDER
;
572 item
.m_image
= FI_EXECUTABLE
;
573 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
574 item
.m_image
= g_IconsTable
->GetIconID(m_name
.AfterLast(wxT('.')));
576 item
.m_image
= FI_UNKNOWN
;
580 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
581 item
.SetTextColour(*dg
);
583 item
.m_data
= (long)this;
586 //-----------------------------------------------------------------------------
588 //-----------------------------------------------------------------------------
590 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
592 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
593 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
594 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems
)
595 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
599 wxFileCtrl::wxFileCtrl()
601 #if defined(__UNIX__)
602 m_dirName
= wxT("/");
603 #elif defined(__DOS__)
606 m_showHidden
= FALSE
;
609 wxFileCtrl::wxFileCtrl(wxWindow
*win
, wxWindowID id
,
610 const wxString
&dirName
, const wxString
&wild
,
611 const wxPoint
&pos
, const wxSize
&size
,
612 long style
, const wxValidator
&validator
,
613 const wxString
&name
)
614 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
)
617 g_IconsTable
= new wxFileIconsTable
;
618 wxImageList
*imageList
= g_IconsTable
->GetImageList();
620 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
622 m_goToParentControl
= m_newDirControl
= NULL
;
626 m_showHidden
= FALSE
;
630 void wxFileCtrl::ChangeToListMode()
632 SetSingleStyle( wxLC_LIST
);
636 void wxFileCtrl::ChangeToReportMode()
638 SetSingleStyle( wxLC_REPORT
);
642 void wxFileCtrl::ChangeToIconMode()
644 SetSingleStyle( wxLC_ICON
);
648 void wxFileCtrl::ShowHidden( bool show
)
654 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
657 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
658 fd
->MakeItem( item
);
659 long my_style
= GetWindowStyleFlag();
660 if (my_style
& wxLC_REPORT
)
663 const int noEntries
= 5;
665 const int noEntries
= 4;
667 ret
= InsertItem( item
);
668 for (int i
= 1; i
< noEntries
; i
++)
669 SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
671 else if (my_style
& wxLC_LIST
)
673 ret
= InsertItem( item
);
678 void wxFileCtrl::Update()
680 wxBusyCursor bcur
; // this may take a while...
682 long my_style
= GetWindowStyleFlag();
683 int name_col_width
= 0;
684 if (my_style
& wxLC_REPORT
)
686 if (GetColumnCount() > 0)
687 name_col_width
= GetColumnWidth( 0 );
691 if (my_style
& wxLC_REPORT
)
693 if (name_col_width
< 140) name_col_width
= 140;
694 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
695 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
696 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
697 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
699 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
702 wxFileData
*fd
= (wxFileData
*) NULL
;
707 #if defined(__DOS__) || defined(__WINDOWS__)
708 if ( IsTopMostDir(m_dirName
) )
710 // Pseudo-directory with all available drives listed...
711 for (int drive
= 1; drive
<= 26; drive
++)
714 path
.Printf(wxT("%c:\\"), (char)(drive
+ 'A' - 1));
715 if ( wxIsDriveAvailable(path
) )
718 fd
= new wxFileData(path
, path
);
728 if ( !IsTopMostDir(m_dirName
) )
730 wxString
p(wxPathOnly(m_dirName
));
732 if (p
.IsEmpty()) p
= wxT("/");
734 fd
= new wxFileData( wxT(".."), p
);
739 wxString
dirname(m_dirName
);
740 #if defined(__DOS__) || defined(__WINDOWS__)
741 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
742 dirname
<< wxT('\\');
746 if ( dir
.IsOpened() )
748 wxString
dirPrefix(dirname
+ wxFILE_SEP_PATH
);
749 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
754 // Get the directories first (not matched against wildcards):
755 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
758 fd
= new wxFileData(f
, dirPrefix
+ f
);
761 cont
= dir
.GetNext(&f
);
764 // Tokenize the wildcard string, so we can handle more than 1
765 // search pattern in a wildcard.
766 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
767 while ( tokenWild
.HasMoreTokens() )
769 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
770 wxDIR_FILES
| hiddenFlag
);
773 fd
= new wxFileData(f
, dirPrefix
+ f
);
776 cont
= dir
.GetNext(&f
);
782 SortItems(ListCompare
, 0);
784 if ( my_style
& wxLC_REPORT
)
786 SetColumnWidth(1, wxLIST_AUTOSIZE
);
787 SetColumnWidth(2, wxLIST_AUTOSIZE
);
788 SetColumnWidth(3, wxLIST_AUTOSIZE
);
791 // Finally, enable/disable context-dependent controls:
792 if ( m_goToParentControl
)
793 m_goToParentControl
->Enable(!IsTopMostDir(m_dirName
));
794 #if defined(__DOS__) || defined(__WINDOWS__)
795 if ( m_newDirControl
)
796 m_newDirControl
->Enable(!IsTopMostDir(m_dirName
));
800 void wxFileCtrl::SetWild( const wxString
&wild
)
806 void wxFileCtrl::MakeDir()
808 wxString
new_name( _("NewName") );
809 wxString
path( m_dirName
);
810 path
+= wxFILE_SEP_PATH
;
812 if (wxFileExists(path
))
814 // try NewName0, NewName1 etc.
817 new_name
= _("NewName");
819 num
.Printf( wxT("%d"), i
);
823 path
+= wxFILE_SEP_PATH
;
826 } while (wxFileExists(path
));
832 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
837 wxFileData
*fd
= new wxFileData( new_name
, path
);
841 long id
= Add( fd
, item
);
845 SortItems( ListCompare
, 0 );
846 id
= FindItem( 0, (long)fd
);
852 void wxFileCtrl::GoToParentDir()
854 if (!IsTopMostDir(m_dirName
))
856 size_t len
= m_dirName
.Len();
857 if (m_dirName
[len
-1] == wxFILE_SEP_PATH
)
858 m_dirName
.Remove( len
-1, 1 );
859 wxString
fname( wxFileNameFromPath(m_dirName
) );
860 m_dirName
= wxPathOnly( m_dirName
);
862 if (m_dirName
.IsEmpty())
863 m_dirName
= wxT("/");
866 long id
= FindItem( 0, fname
);
869 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
875 void wxFileCtrl::GoToHomeDir()
877 wxString s
= wxGetUserHome( wxString() );
881 void wxFileCtrl::GoToDir( const wxString
&dir
)
885 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
889 void wxFileCtrl::GetDir( wxString
&dir
)
894 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
896 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
900 void wxFileCtrl::OnListDeleteAllItems( wxListEvent
&WXUNUSED(event
) )
903 item
.m_mask
= wxLIST_MASK_DATA
;
905 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
906 while ( item
.m_itemId
!= -1 )
909 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
913 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
917 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
919 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
922 if ((event
.GetLabel().IsEmpty()) ||
923 (event
.GetLabel() == _(".")) ||
924 (event
.GetLabel() == _("..")) ||
925 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
927 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
933 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
934 new_name
+= wxFILE_SEP_PATH
;
935 new_name
+= event
.GetLabel();
939 if (wxFileExists(new_name
))
941 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
946 if (wxRenameFile(fd
->GetFullName(),new_name
))
948 fd
->SetNewName( new_name
, event
.GetLabel() );
949 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
950 EnsureVisible( event
.GetItem() );
954 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
960 //-----------------------------------------------------------------------------
962 //-----------------------------------------------------------------------------
964 #define ID_LIST_MODE (wxID_FILEDLGG )
965 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
966 #define ID_UP_DIR (wxID_FILEDLGG + 5)
967 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
968 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
969 #define ID_CHOICE (wxID_FILEDLGG + 8)
970 #define ID_TEXT (wxID_FILEDLGG + 9)
971 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
972 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
973 #define ID_CHECK (wxID_FILEDLGG + 12)
975 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
977 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
978 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
979 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
980 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
981 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
982 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
983 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
984 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
985 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
986 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
987 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
988 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
991 long wxFileDialog::s_lastViewStyle
= wxLC_LIST
;
992 bool wxFileDialog::s_lastShowHidden
= FALSE
;
994 wxFileDialog::wxFileDialog(wxWindow
*parent
,
995 const wxString
& message
,
996 const wxString
& defaultDir
,
997 const wxString
& defaultFile
,
998 const wxString
& wildCard
,
1000 const wxPoint
& pos
) :
1001 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
1005 if (wxConfig::Get(FALSE
))
1007 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1009 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1013 m_message
= message
;
1014 m_dialogStyle
= style
;
1016 if (m_dialogStyle
== 0) m_dialogStyle
= wxOPEN
;
1017 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
1018 m_dialogStyle
|= wxOPEN
;
1021 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
1026 size_t len
= m_dir
.Len();
1027 if ((len
> 1) && (m_dir
[len
-1] == wxFILE_SEP_PATH
))
1028 m_dir
.Remove( len
-1, 1 );
1031 m_path
+= wxFILE_SEP_PATH
;
1032 m_path
+= defaultFile
;
1033 m_fileName
= defaultFile
;
1034 m_wildCard
= wildCard
;
1036 m_filterExtension
= wxEmptyString
;
1038 // interpret wildcards
1040 if (m_wildCard
.IsEmpty())
1041 m_wildCard
= _("All files (*)|*");
1043 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
1045 wxString firstWildText
;
1046 if (tokens
.CountTokens() == 1)
1048 firstWildText
= tokens
.GetNextToken();
1049 firstWild
= firstWildText
;
1053 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
1054 firstWildText
= tokens
.GetNextToken();
1055 firstWild
= tokens
.GetNextToken();
1057 if ( firstWild
.Left( 2 ) == wxT("*.") )
1058 m_filterExtension
= firstWild
.Mid( 1 );
1059 if ( m_filterExtension
== ".*" ) m_filterExtension
= wxEmptyString
;
1063 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
1065 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1067 wxBitmapButton
*but
;
1069 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
1071 but
->SetToolTip( _("View files as a list view") );
1073 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1075 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
1077 but
->SetToolTip( _("View files as a detailed view") );
1079 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1081 buttonsizer
->Add( 30, 5, 1 );
1083 wxWindow
*butDirUp
=
1084 new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
1086 butDirUp
->SetToolTip( _("Go to parent directory") );
1088 buttonsizer
->Add( butDirUp
, 0, wxALL
, 5 );
1090 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1091 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
1093 but
->SetToolTip( _("Go to home directory") );
1095 buttonsizer
->Add( but
, 0, wxALL
, 5);
1097 buttonsizer
->Add( 20, 20 );
1100 wxWindow
*butNewDir
=
1101 new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
1103 butNewDir
->SetToolTip( _("Create new directory") );
1105 buttonsizer
->Add( butNewDir
, 0, wxALL
, 5 );
1107 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1109 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1110 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
1111 m_static
= new wxStaticText( this, -1, m_dir
);
1112 staticsizer
->Add( m_static
, 1 );
1113 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1115 if (m_dialogStyle
& wxMULTIPLE
)
1116 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
1117 wxSize(540,200), s_lastViewStyle
| wxSUNKEN_BORDER
);
1119 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
,
1120 wxSize(540,200), s_lastViewStyle
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
1121 m_list
->ShowHidden(s_lastShowHidden
);
1122 m_list
->SetNewDirControl(butNewDir
);
1123 m_list
->SetGoToParentControl(butDirUp
);
1125 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1127 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1128 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1129 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1130 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1131 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1133 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1134 m_choice
= new wxChoice( this, ID_CHOICE
);
1135 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1136 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1137 m_check
->SetValue( s_lastShowHidden
);
1138 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1139 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
1140 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1142 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1143 while (tokens
.HasMoreTokens())
1145 firstWildText
= tokens
.GetNextToken();
1146 firstWild
= tokens
.GetNextToken();
1147 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1149 m_choice
->SetSelection( 0 );
1151 SetAutoLayout( TRUE
);
1152 SetSizer( mainsizer
);
1154 mainsizer
->Fit( this );
1155 mainsizer
->SetSizeHints( this );
1160 if (m_fileName.IsEmpty())
1167 wxFileDialog::~wxFileDialog()
1169 if (wxConfig::Get(FALSE
))
1171 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1173 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1178 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
1180 int index
= (int)event
.GetInt();
1181 wxString
*str
= (wxString
*) m_choice
->GetClientData( index
);
1182 m_list
->SetWild( *str
);
1183 m_filterIndex
= index
;
1184 if ( str
-> Left( 2 ) == wxT("*.") )
1186 m_filterExtension
= str
-> Mid( 1 );
1187 if (m_filterExtension
== ".*") m_filterExtension
= wxEmptyString
;
1190 m_filterExtension
= wxEmptyString
;
1193 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
1195 m_list
->ShowHidden( (s_lastShowHidden
= event
.GetInt() != 0) );
1198 void wxFileDialog::OnActivated( wxListEvent
&event
)
1200 HandleAction( event
.m_item
.m_text
);
1203 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1205 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1206 cevent
.SetEventObject( this );
1207 GetEventHandler()->ProcessEvent( cevent
);
1210 void wxFileDialog::OnSelected( wxListEvent
&event
)
1212 if (FindFocus() != m_list
) return;
1214 wxString
filename( event
.m_item
.m_text
);
1215 if (filename
== wxT("..")) return;
1218 m_list
->GetDir( dir
);
1219 if (!IsTopMostDir(dir
))
1220 dir
+= wxFILE_SEP_PATH
;
1222 if (wxDirExists(dir
)) return;
1224 m_text
->SetValue( filename
);
1227 void wxFileDialog::HandleAction( const wxString
&fn
)
1229 wxString
filename( fn
);
1231 m_list
->GetDir( dir
);
1232 if (filename
.IsEmpty()) return;
1233 if (filename
== wxT(".")) return;
1235 if (filename
== wxT(".."))
1237 m_list
->GoToParentDir();
1239 m_list
->GetDir( dir
);
1240 m_static
->SetLabel( dir
);
1245 if (filename
== wxT("~"))
1247 m_list
->GoToHomeDir();
1249 m_list
->GetDir( dir
);
1250 m_static
->SetLabel( dir
);
1254 if (filename
[0u] == wxT('~'))
1256 filename
.Remove( 0, 1 );
1257 wxString
tmp( wxGetUserHome() );
1264 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1265 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1267 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1269 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1272 m_list
->SetWild( filename
);
1276 if (!IsTopMostDir(dir
))
1277 dir
+= wxFILE_SEP_PATH
;
1278 if (!wxIsAbsolutePath(filename
))
1284 if (wxDirExists(filename
))
1286 m_list
->GoToDir( filename
);
1287 m_list
->GetDir( dir
);
1288 m_static
->SetLabel( dir
);
1293 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
1295 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1296 filename
.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1297 filename
<< m_filterExtension
;
1298 if (wxFileExists( filename
))
1301 msg
.Printf( _("File '%s' already exists, do you really want to "
1302 "overwrite it?"), filename
.c_str() );
1304 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1308 else if ( m_dialogStyle
& wxOPEN
)
1310 if ( !wxFileExists( filename
) )
1311 if (filename
.Find( wxT('.') ) == wxNOT_FOUND
||
1312 filename
.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1313 filename
<< m_filterExtension
;
1315 if ( m_dialogStyle
& wxFILE_MUST_EXIST
)
1317 if ( !wxFileExists( filename
) )
1319 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
1325 SetPath( filename
);
1327 // change to the directory where the user went if asked
1328 if ( m_dialogStyle
& wxCHANGE_DIR
)
1331 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1333 if ( cwd
!= wxGetWorkingDirectory() )
1335 wxSetWorkingDirectory(cwd
);
1339 wxCommandEvent event
;
1340 wxDialog::OnOK(event
);
1343 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1345 HandleAction( m_text
->GetValue() );
1348 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1350 m_list
->ChangeToListMode();
1351 s_lastViewStyle
= wxLC_LIST
;
1355 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1357 m_list
->ChangeToReportMode();
1358 s_lastViewStyle
= wxLC_REPORT
;
1362 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1364 m_list
->GoToParentDir();
1367 m_list
->GetDir( dir
);
1368 m_static
->SetLabel( dir
);
1371 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1373 m_list
->GoToHomeDir();
1376 m_list
->GetDir( dir
);
1377 m_static
->SetLabel( dir
);
1380 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1385 void wxFileDialog::SetPath( const wxString
& path
)
1387 // not only set the full path but also update filename and dir
1392 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1395 m_fileName
+= wxT(".");
1401 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1404 if (m_list
->GetSelectedItemCount() == 0)
1406 paths
.Add( GetPath() );
1410 paths
.Alloc( m_list
->GetSelectedItemCount() );
1413 m_list
->GetDir( dir
);
1415 if (dir
!= wxT("/"))
1417 dir
+= wxFILE_SEP_PATH
;
1420 item
.m_mask
= wxLIST_MASK_TEXT
;
1422 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1423 while ( item
.m_itemId
!= -1 )
1425 m_list
->GetItem( item
);
1426 paths
.Add( dir
+ item
.m_text
);
1427 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1428 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1432 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1435 if (m_list
->GetSelectedItemCount() == 0)
1437 files
.Add( GetFilename() );
1440 files
.Alloc( m_list
->GetSelectedItemCount() );
1443 item
.m_mask
= wxLIST_MASK_TEXT
;
1445 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1446 while ( item
.m_itemId
!= -1 )
1448 m_list
->GetItem( item
);
1449 files
.Add( item
.m_text
);
1450 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1451 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1457 // ----------------------------------------------------------------------------
1459 // ----------------------------------------------------------------------------
1462 wxFileSelectorEx(const wxChar
*message
,
1463 const wxChar
*default_path
,
1464 const wxChar
*default_filename
,
1465 int *WXUNUSED(indexDefaultExtension
),
1466 const wxChar
*wildcard
,
1471 // TODO: implement this somehow
1472 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1473 wildcard
, flags
, parent
, x
, y
);
1476 wxString
wxFileSelector( const wxChar
*title
,
1477 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1478 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1479 wxWindow
*parent
, int x
, int y
)
1482 if ( defaultExtension
&& !filter
)
1483 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1487 wxString defaultDirString
;
1489 defaultDirString
= defaultDir
;
1491 wxString defaultFilenameString
;
1492 if (defaultFileName
)
1493 defaultFilenameString
= defaultFileName
;
1495 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1497 if ( fileDialog
.ShowModal() == wxID_OK
)
1499 return fileDialog
.GetPath();
1503 return wxEmptyString
;
1507 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*ext
, const wxChar
*default_name
, wxWindow
*parent
)
1509 wxString prompt
= wxString::Format(_("Load %s file"), what
);
1511 if (*ext
== wxT('.'))
1514 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1516 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1517 ext
, wild
, 0, parent
);
1520 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
1523 wxChar
*ext
= (wxChar
*)extension
;
1525 wxString prompt
= wxString::Format(_("Save %s file"), what
);
1527 if (*ext
== wxT('.'))
1530 wxString wild
= wxString::Format(_T("*.%s"), ext
);
1532 return wxFileSelector(prompt
, (const wxChar
*) NULL
, default_name
,
1533 ext
, wild
, 0, parent
);
1541 // A module to allow icons table cleanup
1543 class wxFileDialogGenericModule
: public wxModule
1545 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1547 wxFileDialogGenericModule() {}
1548 bool OnInit() { return TRUE
; }
1549 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1552 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)
1554 #endif // wxUSE_FILEDLG