1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "filedlgg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #if !defined(__UNIX__) && !defined(__DOS__)
34 #error wxFileDialog currently only supports Unix and DOS
37 #include "wx/checkbox.h"
38 #include "wx/textctrl.h"
39 #include "wx/choice.h"
40 #include "wx/checkbox.h"
41 #include "wx/stattext.h"
42 #include "wx/filedlg.h"
46 #include "wx/listctrl.h"
47 #include "wx/msgdlg.h"
49 #include "wx/bmpbuttn.h"
50 #include "wx/tokenzr.h"
51 #include "wx/mimetype.h"
53 #include "wx/module.h"
54 #include "wx/config.h"
55 #include "wx/imaglist.h"
57 #include "wx/artprov.h"
60 #include "wx/tooltip.h"
63 #include <sys/types.h>
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 // the list ctrl fields in report view
98 //-----------------------------------------------------------------------------
100 //-----------------------------------------------------------------------------
105 wxFileData( const wxString
&name
, const wxString
&fname
);
106 wxString
GetName() const;
107 wxString
GetFullName() const;
108 wxString
GetHint() const;
109 wxString
GetEntry( FileListField num
) const;
111 bool IsDir() const { return m_isDir
; }
112 bool IsLink() const { return m_isLink
; }
113 bool IsExe() const { return m_isExe
; }
114 long GetSize() const { return m_size
; }
116 void MakeItem( wxListItem
&item
);
117 void SetNewName( const wxString
&name
, const wxString
&fname
);
128 wxString m_permissions
;
134 //-----------------------------------------------------------------------------
136 //-----------------------------------------------------------------------------
138 class wxFileCtrl
: public wxListCtrl
142 wxFileCtrl( wxWindow
*win
,
143 wxStaticText
*labelDir
,
145 const wxString
&wild
,
147 const wxPoint
&pos
= wxDefaultPosition
,
148 const wxSize
&size
= wxDefaultSize
,
149 long style
= wxLC_LIST
,
150 const wxValidator
&validator
= wxDefaultValidator
,
151 const wxString
&name
= wxT("filelist") );
152 virtual ~wxFileCtrl();
154 void ChangeToListMode();
155 void ChangeToReportMode();
156 void ChangeToIconMode();
157 void ShowHidden( bool show
= TRUE
);
158 long Add( wxFileData
*fd
, wxListItem
&item
);
160 virtual void StatusbarText( wxChar
*WXUNUSED(text
) ) {};
162 void GoToParentDir();
164 void GoToDir( const wxString
&dir
);
165 void SetWild( const wxString
&wild
);
166 void GetDir( wxString
&dir
);
167 void OnListDeleteItem( wxListEvent
&event
);
168 void OnListEndLabelEdit( wxListEvent
&event
);
170 // Associate commonly used UI controls with wxFileCtrl so that they can be
171 // disabled when they cannot be used (e.g. can't go to parent directory
172 // if wxFileCtrl already is in the root dir):
173 void SetGoToParentControl(wxWindow
*ctrl
) { m_goToParentControl
= ctrl
; }
174 void SetNewDirControl(wxWindow
*ctrl
) { m_newDirControl
= ctrl
; }
177 void FreeItemData(const wxListItem
& item
);
178 void FreeAllItemsData();
184 wxWindow
*m_goToParentControl
;
185 wxWindow
*m_newDirControl
;
187 // the label showing the current directory
188 wxStaticText
*m_labelDir
;
190 DECLARE_DYNAMIC_CLASS(wxFileCtrl
);
191 DECLARE_EVENT_TABLE()
194 // ----------------------------------------------------------------------------
195 // private classes - icons list management
196 // ----------------------------------------------------------------------------
198 class wxFileIconEntry
: public wxObject
201 wxFileIconEntry(int i
) { id
= i
; }
207 class wxFileIconsTable
212 int GetIconID(const wxString
& extension
, const wxString
& mime
= wxEmptyString
);
213 wxImageList
*GetImageList() { return &m_ImageList
; }
216 wxImageList m_ImageList
;
217 wxHashTable m_HashTable
;
220 static wxFileIconsTable
*g_IconsTable
= NULL
;
224 #define FI_EXECUTABLE 2
226 wxFileIconsTable::wxFileIconsTable() :
228 m_HashTable(wxKEY_STRING
)
230 m_HashTable
.DeleteContents(TRUE
);
232 m_ImageList
.Add(wxArtProvider::GetBitmap(wxART_FOLDER
, wxART_CMN_DIALOG
));
234 m_ImageList
.Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE
, wxART_CMN_DIALOG
));
236 if (GetIconID(wxEmptyString
, _T("application/x-executable")) == FI_UNKNOWN
)
238 m_ImageList
.Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE
, wxART_CMN_DIALOG
));
239 m_HashTable
.Delete(_T("exe"));
240 m_HashTable
.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE
));
242 /* else put into list by GetIconID
243 (KDE defines application/x-executable for *.exe and has nice icon)
250 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
251 // one icon and we won't resize it
253 static wxBitmap
CreateAntialiasedBitmap(const wxImage
& img
)
255 wxImage
small(16, 16);
256 unsigned char *p1
, *p2
, *ps
;
257 unsigned char mr
= img
.GetMaskRed(),
258 mg
= img
.GetMaskGreen(),
259 mb
= img
.GetMaskBlue();
262 unsigned sr
, sg
, sb
, smask
;
264 p1
= img
.GetData(), p2
= img
.GetData() + 3 * 32, ps
= small
.GetData();
265 small
.SetMaskColour(mr
, mr
, mr
);
267 for (y
= 0; y
< 16; y
++)
269 for (x
= 0; x
< 16; x
++)
271 sr
= sg
= sb
= smask
= 0;
272 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
273 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
276 if (p1
[0] != mr
|| p1
[1] != mg
|| p1
[2] != mb
)
277 sr
+= p1
[0], sg
+= p1
[1], sb
+= p1
[2];
280 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
281 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
284 if (p2
[0] != mr
|| p2
[1] != mg
|| p2
[2] != mb
)
285 sr
+= p2
[0], sg
+= p2
[1], sb
+= p2
[2];
290 ps
[0] = ps
[1] = ps
[2] = mr
;
292 ps
[0] = sr
>> 2, ps
[1] = sg
>> 2, ps
[2] = sb
>> 2;
295 p1
+= 32 * 3, p2
+= 32 * 3;
298 return wxBitmap(small
);
301 // finds empty borders and return non-empty area of image:
302 static wxImage
CutEmptyBorders(const wxImage
& img
)
304 unsigned char mr
= img
.GetMaskRed(),
305 mg
= img
.GetMaskGreen(),
306 mb
= img
.GetMaskBlue();
307 unsigned char *dt
= img
.GetData(), *dttmp
;
308 unsigned w
= img
.GetWidth(), h
= img
.GetHeight();
310 unsigned top
, bottom
, left
, right
, i
;
313 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
314 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
316 for (empt
= TRUE
, top
= 0; empt
&& top
< h
; top
++)
319 for (i
= 0; i
< w
; i
++, dttmp
+=3)
322 for (empt
= TRUE
, bottom
= h
-1; empt
&& bottom
> top
; bottom
--)
325 for (i
= 0; i
< w
; i
++, dttmp
+=3)
328 for (empt
= TRUE
, left
= 0; empt
&& left
< w
; left
++)
331 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
334 for (empt
= TRUE
, right
= w
-1; empt
&& right
> left
; right
--)
337 for (i
= 0; i
< h
; i
++, dttmp
+=3*w
)
340 top
--, left
--, bottom
++, right
++;
342 return img
.GetSubImage(wxRect(left
, top
, right
- left
+ 1, bottom
- top
+ 1));
344 #endif // wxUSE_MIMETYPE
348 int wxFileIconsTable::GetIconID(const wxString
& extension
, const wxString
& mime
)
351 if (!extension
.IsEmpty())
353 wxFileIconEntry
*entry
= (wxFileIconEntry
*) m_HashTable
.Get(extension
);
354 if (entry
) return (entry
-> id
);
357 wxFileType
*ft
= (mime
.IsEmpty()) ?
358 wxTheMimeTypesManager
-> GetFileTypeFromExtension(extension
) :
359 wxTheMimeTypesManager
-> GetFileTypeFromMimeType(mime
);
361 if (ft
== NULL
|| (!ft
-> GetIcon(&ic
)) || (!ic
.Ok()))
363 int newid
= FI_UNKNOWN
;
364 m_HashTable
.Put(extension
, new wxFileIconEntry(newid
));
367 wxImage img
= ic
.ConvertToImage();
370 int id
= m_ImageList
.GetImageCount();
371 if (img
.GetWidth() == 16 && img
.GetHeight() == 16)
372 m_ImageList
.Add(wxBitmap(img
));
375 if (img
.GetWidth() != 32 || img
.GetHeight() != 32)
376 m_ImageList
.Add(CreateAntialiasedBitmap(CutEmptyBorders(img
).Rescale(32, 32)));
378 m_ImageList
.Add(CreateAntialiasedBitmap(img
));
380 m_HashTable
.Put(extension
, new wxFileIconEntry(id
));
383 #else // !wxUSE_MIMETYPE
385 if (extension
== wxT("exe"))
386 return FI_EXECUTABLE
;
389 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
399 int ListCompare( long data1
, long data2
, long WXUNUSED(data
) )
401 wxFileData
*fd1
= (wxFileData
*)data1
;
402 wxFileData
*fd2
= (wxFileData
*)data2
;
403 if (fd1
->GetName() == wxT("..")) return -1;
404 if (fd2
->GetName() == wxT("..")) return 1;
405 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
406 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
407 return wxStrcmp( fd1
->GetName(), fd2
->GetName() );
411 #define IsTopMostDir(dir) (dir == wxT("/"))
414 #if defined(__DOS__) || defined(__WINDOWS__)
415 #define IsTopMostDir(dir) (dir.IsEmpty())
418 #if defined(__DOS__) || defined(__WINDOWS__)
419 extern bool wxIsDriveAvailable(const wxString
& dirName
);
422 //-----------------------------------------------------------------------------
424 //-----------------------------------------------------------------------------
426 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
431 #if defined(__DOS__) || defined(__WINDOWS__)
432 // VS: In case the file is root directory of a volume (e.g. "C:"),
433 // we don't want it stat()ed, since the drive may not be in:
434 if (name
.length() == 2 && name
[1u] == wxT(':'))
437 m_isExe
= m_isLink
= FALSE
;
444 wxStat( m_fileName
, &buff
);
446 #if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
448 lstat( m_fileName
.fn_str(), &lbuff
);
449 m_isLink
= S_ISLNK( lbuff
.st_mode
);
450 struct tm
*t
= localtime( &lbuff
.st_mtime
);
453 struct tm
*t
= localtime( &buff
.st_mtime
);
456 // struct passwd *user = getpwuid( buff.st_uid );
457 // struct group *grp = getgrgid( buff.st_gid );
459 m_isDir
= S_ISDIR( buff
.st_mode
);
460 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
462 m_size
= buff
.st_size
;
465 m_minute
= t
->tm_min
;
466 m_month
= t
->tm_mon
+1;
472 sprintf( buffer
, "%c%c%c",
473 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? 'r' : '-'),
474 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? 'w' : '-'),
475 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? 'x' : '-') );
477 m_permissions
= wxConvUTF8
.cMB2WC( buffer
);
479 m_permissions
= buffer
;
482 // m_permissions.sprintf( wxT("%c%c%c"),
483 // ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? wxT('r') : wxT('-')),
484 // ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? wxT('w') : wxT('-')),
485 // ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? wxT('x') : wxT('-')) );
488 wxString
wxFileData::GetName() const
493 wxString
wxFileData::GetFullName() const
498 wxString
wxFileData::GetHint() const
500 wxString s
= m_fileName
;
502 if (m_isDir
) s
+= wxT("<DIR> ");
503 else if (m_isLink
) s
+= wxT("<LINK> ");
506 s
+= LongToString( m_size
);
509 s
+= IntToString( m_day
);
511 s
+= IntToString( m_month
);
513 s
+= IntToString( m_year
);
515 s
+= IntToString( m_hour
);
517 s
+= IntToString( m_minute
);
523 wxString
wxFileData::GetEntry( FileListField num
) const
538 s
.Printf(_T("%ld"), m_size
);
542 s
.Printf(_T("%02d.%02d.%d"), m_day
, m_month
, m_year
);
546 s
.Printf(_T("%02d:%02d"), m_hour
, m_minute
);
556 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
562 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
568 void wxFileData::MakeItem( wxListItem
&item
)
570 item
.m_text
= m_name
;
571 item
.ClearAttributes();
573 item
.SetTextColour(*wxRED
);
575 item
.SetTextColour(*wxBLUE
);
578 item
.m_image
= FI_FOLDER
;
580 item
.m_image
= FI_EXECUTABLE
;
581 else if (m_name
.Find(wxT('.')) != wxNOT_FOUND
)
582 item
.m_image
= g_IconsTable
->GetIconID(m_name
.AfterLast(wxT('.')));
584 item
.m_image
= FI_UNKNOWN
;
588 wxColour
*dg
= wxTheColourDatabase
->FindColour( _T("MEDIUM GREY") );
589 item
.SetTextColour(*dg
);
591 item
.m_data
= (long)this;
594 //-----------------------------------------------------------------------------
596 //-----------------------------------------------------------------------------
598 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
)
600 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
601 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
602 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
606 wxFileCtrl::wxFileCtrl()
608 m_showHidden
= FALSE
;
611 wxFileCtrl::wxFileCtrl(wxWindow
*win
,
612 wxStaticText
*labelDir
,
614 const wxString
& wild
,
619 const wxValidator
&validator
,
620 const wxString
&name
)
621 : wxListCtrl(win
, id
, pos
, size
, style
, validator
, name
),
625 g_IconsTable
= new wxFileIconsTable
;
626 wxImageList
*imageList
= g_IconsTable
->GetImageList();
628 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
630 m_goToParentControl
=
631 m_newDirControl
= NULL
;
633 m_labelDir
= labelDir
;
635 m_showHidden
= showHidden
;
638 void wxFileCtrl::ChangeToListMode()
640 SetSingleStyle( wxLC_LIST
);
644 void wxFileCtrl::ChangeToReportMode()
646 SetSingleStyle( wxLC_REPORT
);
650 void wxFileCtrl::ChangeToIconMode()
652 SetSingleStyle( wxLC_ICON
);
656 void wxFileCtrl::ShowHidden( bool show
)
662 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
665 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
666 fd
->MakeItem( item
);
667 long my_style
= GetWindowStyleFlag();
668 if (my_style
& wxLC_REPORT
)
670 ret
= InsertItem( item
);
671 for (int i
= 1; i
< FileList_Max
; i
++)
672 SetItem( item
.m_itemId
, i
, fd
->GetEntry((FileListField
)i
) );
674 else if (my_style
& wxLC_LIST
)
676 ret
= InsertItem( item
);
681 void wxFileCtrl::UpdateFiles()
683 // don't do anything before ShowModal() call which sets m_dirName
684 if ( m_dirName
.empty() )
687 wxBusyCursor bcur
; // this may take a while...
689 long my_style
= GetWindowStyleFlag();
690 int name_col_width
= 0;
691 if (my_style
& wxLC_REPORT
)
693 if (GetColumnCount() > 0)
694 name_col_width
= GetColumnWidth( 0 );
700 if (my_style
& wxLC_REPORT
)
702 if (name_col_width
< 140) name_col_width
= 140;
703 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, name_col_width
);
704 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
705 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 65 );
706 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
708 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
711 wxFileData
*fd
= (wxFileData
*) NULL
;
716 #if defined(__DOS__) || defined(__WINDOWS__)
717 if ( IsTopMostDir(m_dirName
) )
719 // Pseudo-directory with all available drives listed...
720 for (int drive
= 1; drive
<= 26; drive
++)
723 path
.Printf(wxT("%c:\\"), (char)(drive
+ 'A' - 1));
724 if ( wxIsDriveAvailable(path
) )
727 fd
= new wxFileData(path
, path
);
737 if ( !IsTopMostDir(m_dirName
) )
739 wxString
p(wxPathOnly(m_dirName
));
741 if (p
.IsEmpty()) p
= wxT("/");
743 fd
= new wxFileData( wxT(".."), p
);
748 wxString
dirname(m_dirName
);
749 #if defined(__DOS__) || defined(__WINDOWS__)
750 if (dirname
.length() == 2 && dirname
[1u] == wxT(':'))
751 dirname
<< wxT('\\');
755 if ( dir
.IsOpened() )
757 wxString
dirPrefix(dirname
+ wxFILE_SEP_PATH
);
758 int hiddenFlag
= m_showHidden
? wxDIR_HIDDEN
: 0;
763 // Get the directories first (not matched against wildcards):
764 cont
= dir
.GetFirst(&f
, wxEmptyString
, wxDIR_DIRS
| hiddenFlag
);
767 fd
= new wxFileData(f
, dirPrefix
+ f
);
770 cont
= dir
.GetNext(&f
);
773 // Tokenize the wildcard string, so we can handle more than 1
774 // search pattern in a wildcard.
775 wxStringTokenizer
tokenWild(m_wild
, wxT(";"));
776 while ( tokenWild
.HasMoreTokens() )
778 cont
= dir
.GetFirst(&f
, tokenWild
.GetNextToken(),
779 wxDIR_FILES
| hiddenFlag
);
782 fd
= new wxFileData(f
, dirPrefix
+ f
);
785 cont
= dir
.GetNext(&f
);
791 SortItems(ListCompare
, 0);
793 if ( my_style
& wxLC_REPORT
)
795 SetColumnWidth(1, wxLIST_AUTOSIZE
);
796 SetColumnWidth(2, wxLIST_AUTOSIZE
);
797 SetColumnWidth(3, wxLIST_AUTOSIZE
);
800 // Finally, enable/disable context-dependent controls:
801 if ( m_goToParentControl
)
802 m_goToParentControl
->Enable(!IsTopMostDir(m_dirName
));
803 #if defined(__DOS__) || defined(__WINDOWS__)
804 if ( m_newDirControl
)
805 m_newDirControl
->Enable(!IsTopMostDir(m_dirName
));
809 void wxFileCtrl::SetWild( const wxString
&wild
)
815 void wxFileCtrl::MakeDir()
817 wxString
new_name( _("NewName") );
818 wxString
path( m_dirName
);
819 path
+= wxFILE_SEP_PATH
;
821 if (wxFileExists(path
))
823 // try NewName0, NewName1 etc.
826 new_name
= _("NewName");
828 num
.Printf( wxT("%d"), i
);
832 path
+= wxFILE_SEP_PATH
;
835 } while (wxFileExists(path
));
841 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
846 wxFileData
*fd
= new wxFileData( new_name
, path
);
850 long id
= Add( fd
, item
);
854 SortItems( ListCompare
, 0 );
855 id
= FindItem( 0, (long)fd
);
861 void wxFileCtrl::GoToParentDir()
863 if (!IsTopMostDir(m_dirName
))
865 size_t len
= m_dirName
.Len();
866 if (m_dirName
[len
-1] == wxFILE_SEP_PATH
)
867 m_dirName
.Remove( len
-1, 1 );
868 wxString
fname( wxFileNameFromPath(m_dirName
) );
869 m_dirName
= wxPathOnly( m_dirName
);
871 if (m_dirName
.IsEmpty())
872 m_dirName
= wxT("/");
875 long id
= FindItem( 0, fname
);
878 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
882 m_labelDir
->SetLabel(m_dirName
);
886 void wxFileCtrl::GoToHomeDir()
888 wxString s
= wxGetUserHome( wxString() );
892 void wxFileCtrl::GoToDir( const wxString
&dir
)
896 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
899 m_labelDir
->SetLabel(dir
);
902 void wxFileCtrl::GetDir( wxString
&dir
)
907 void wxFileCtrl::FreeItemData(const wxListItem
& item
)
909 wxFileData
*fd
= (wxFileData
*)item
.m_data
;
913 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
915 FreeItemData(event
.m_item
);
918 void wxFileCtrl::FreeAllItemsData()
921 item
.m_mask
= wxLIST_MASK_DATA
;
923 item
.m_itemId
= GetNextItem( -1, wxLIST_NEXT_ALL
);
924 while ( item
.m_itemId
!= -1 )
928 item
.m_itemId
= GetNextItem( item
.m_itemId
, wxLIST_NEXT_ALL
);
932 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
934 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
937 if ((event
.GetLabel().IsEmpty()) ||
938 (event
.GetLabel() == _(".")) ||
939 (event
.GetLabel() == _("..")) ||
940 (event
.GetLabel().First( wxFILE_SEP_PATH
) != wxNOT_FOUND
))
942 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
948 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
949 new_name
+= wxFILE_SEP_PATH
;
950 new_name
+= event
.GetLabel();
954 if (wxFileExists(new_name
))
956 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
961 if (wxRenameFile(fd
->GetFullName(),new_name
))
963 fd
->SetNewName( new_name
, event
.GetLabel() );
964 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
965 EnsureVisible( event
.GetItem() );
969 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
975 wxFileCtrl::~wxFileCtrl()
980 //-----------------------------------------------------------------------------
982 //-----------------------------------------------------------------------------
984 #define ID_LIST_MODE (wxID_FILEDLGG )
985 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
986 #define ID_UP_DIR (wxID_FILEDLGG + 5)
987 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
988 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
989 #define ID_CHOICE (wxID_FILEDLGG + 8)
990 #define ID_TEXT (wxID_FILEDLGG + 9)
991 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
992 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
993 #define ID_CHECK (wxID_FILEDLGG + 12)
995 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
997 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
998 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
999 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
1000 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
1001 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
1002 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
1003 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
1004 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
1005 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
1006 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoiceFilter
)
1007 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
1008 EVT_CHECKBOX(ID_CHECK
,wxFileDialog::OnCheck
)
1011 long wxFileDialog::ms_lastViewStyle
= wxLC_LIST
;
1012 bool wxFileDialog::ms_lastShowHidden
= FALSE
;
1014 wxFileDialog::wxFileDialog(wxWindow
*parent
,
1015 const wxString
& message
,
1016 const wxString
& defaultDir
,
1017 const wxString
& defaultFile
,
1018 const wxString
& wildCard
,
1020 const wxPoint
& pos
)
1021 : wxDialog( parent
, -1, message
, pos
, wxDefaultSize
,
1022 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
1024 if (wxConfig::Get(FALSE
))
1026 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1028 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1029 &ms_lastShowHidden
);
1032 m_message
= message
;
1033 m_dialogStyle
= style
;
1035 if (m_dialogStyle
== 0)
1036 m_dialogStyle
= wxOPEN
;
1037 if ((m_dialogStyle
& wxMULTIPLE
) && !(m_dialogStyle
& wxOPEN
))
1038 m_dialogStyle
|= wxOPEN
;
1041 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
1046 size_t len
= m_dir
.Len();
1047 if ((len
> 1) && (m_dir
[len
-1] == wxFILE_SEP_PATH
))
1048 m_dir
.Remove( len
-1, 1 );
1051 m_path
+= wxFILE_SEP_PATH
;
1052 m_path
+= defaultFile
;
1053 m_fileName
= defaultFile
;
1054 m_wildCard
= wildCard
;
1056 m_filterExtension
= wxEmptyString
;
1058 // interpret wildcards
1060 if (m_wildCard
.IsEmpty())
1061 m_wildCard
= _("All files (*)|*");
1063 wxStringTokenizer
tokens( m_wildCard
, wxT("|") );
1065 wxString firstWildText
;
1066 if (tokens
.CountTokens() == 1)
1068 firstWildText
= tokens
.GetNextToken();
1069 firstWild
= firstWildText
;
1073 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
1074 firstWildText
= tokens
.GetNextToken();
1075 firstWild
= tokens
.GetNextToken();
1077 if ( firstWild
.Left( 2 ) == wxT("*.") )
1078 m_filterExtension
= firstWild
.Mid( 1 );
1079 if ( m_filterExtension
== wxT(".*") )
1080 m_filterExtension
= wxEmptyString
;
1084 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1086 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
1088 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1090 wxBitmapButton
*but
;
1092 but
= new wxBitmapButton(this, ID_LIST_MODE
,
1093 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_CMN_DIALOG
));
1095 but
->SetToolTip( _("View files as a list view") );
1097 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1099 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
1100 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_CMN_DIALOG
));
1102 but
->SetToolTip( _("View files as a detailed view") );
1104 buttonsizer
->Add( but
, 0, wxALL
, 5 );
1106 buttonsizer
->Add( 30, 5, 1 );
1108 wxWindow
*butDirUp
=
1109 new wxBitmapButton(this, ID_UP_DIR
,
1110 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_CMN_DIALOG
));
1112 butDirUp
->SetToolTip( _("Go to parent directory") );
1114 buttonsizer
->Add( butDirUp
, 0, wxALL
, 5 );
1116 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1117 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
1118 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_CMN_DIALOG
));
1120 but
->SetToolTip( _("Go to home directory") );
1122 buttonsizer
->Add( but
, 0, wxALL
, 5);
1124 buttonsizer
->Add( 20, 20 );
1127 wxWindow
*butNewDir
=
1128 new wxBitmapButton(this, ID_NEW_DIR
,
1129 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_CMN_DIALOG
));
1131 butNewDir
->SetToolTip( _("Create new directory") );
1133 buttonsizer
->Add( butNewDir
, 0, wxALL
, 5 );
1136 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
1138 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
1140 wxBoxSizer
*staticsizer
= new wxBoxSizer( wxHORIZONTAL
);
1142 staticsizer
->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT
, 10 );
1143 m_static
= new wxStaticText( this, -1, m_dir
);
1144 staticsizer
->Add( m_static
, 1 );
1145 mainsizer
->Add( staticsizer
, 0, wxEXPAND
| wxLEFT
|wxRIGHT
|wxBOTTOM
, 10 );
1147 long style2
= ms_lastViewStyle
| wxSUNKEN_BORDER
;
1148 if ( !(m_dialogStyle
& wxMULTIPLE
) )
1149 style2
|= wxLC_SINGLE_SEL
;
1151 m_list
= new wxFileCtrl( this, m_static
, ID_LIST_CTRL
,
1152 firstWild
, ms_lastShowHidden
,
1153 wxDefaultPosition
, wxSize(540,200),
1156 m_list
->SetNewDirControl(butNewDir
);
1157 m_list
->SetGoToParentControl(butDirUp
);
1161 // PDAs have a different screen layout
1162 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 5 );
1164 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1165 m_choice
= new wxChoice( this, ID_CHOICE
);
1166 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 5 );
1167 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1169 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1170 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1171 textsizer
->Add( m_text
, 1, wxCENTER
| wxALL
, 5 );
1172 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1174 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1175 m_check
->SetValue( ms_lastShowHidden
);
1176 textsizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 5 );
1178 buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
1179 buttonsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxALL
, 5 );
1180 buttonsizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 5 );
1181 mainsizer
->Add( buttonsizer
, 0, wxALIGN_RIGHT
);
1185 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
1187 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
1188 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
1189 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1190 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
1191 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
1193 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
1194 m_choice
= new wxChoice( this, ID_CHOICE
);
1195 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
1196 m_check
= new wxCheckBox( this, ID_CHECK
, _("Show hidden files") );
1197 m_check
->SetValue( ms_lastShowHidden
);
1198 choicesizer
->Add( m_check
, 0, wxCENTER
|wxALL
, 10 );
1199 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
1200 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
1203 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1204 while (tokens
.HasMoreTokens())
1206 firstWildText
= tokens
.GetNextToken();
1207 firstWild
= tokens
.GetNextToken();
1208 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
1210 m_choice
->SetSelection( 0 );
1212 SetAutoLayout( TRUE
);
1213 SetSizer( mainsizer
);
1215 mainsizer
->Fit( this );
1216 mainsizer
->SetSizeHints( this );
1223 wxFileDialog::~wxFileDialog()
1225 if (wxConfig::Get(FALSE
))
1227 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1229 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1234 int wxFileDialog::ShowModal()
1236 m_list
->GoToDir(m_dir
);
1237 m_text
->SetValue(m_fileName
);
1239 return wxDialog::ShowModal();
1242 void wxFileDialog::DoSetFilterIndex(int filterindex
)
1244 wxString
*str
= (wxString
*) m_choice
->GetClientData( filterindex
);
1245 m_list
->SetWild( *str
);
1246 m_filterIndex
= filterindex
;
1247 if ( str
->Left(2) == wxT("*.") )
1249 m_filterExtension
= str
->Mid(2);
1250 if (m_filterExtension
== _T("*"))
1251 m_filterExtension
.clear();
1255 m_filterExtension
.clear();
1259 void wxFileDialog::SetFilterIndex( int filterindex
)
1261 m_choice
->SetSelection( filterindex
);
1263 DoSetFilterIndex(filterindex
);
1266 void wxFileDialog::OnChoiceFilter( wxCommandEvent
&event
)
1268 DoSetFilterIndex((int)event
.GetInt());
1271 void wxFileDialog::OnCheck( wxCommandEvent
&event
)
1273 m_list
->ShowHidden( (ms_lastShowHidden
= event
.GetInt() != 0) );
1276 void wxFileDialog::OnActivated( wxListEvent
&event
)
1278 HandleAction( event
.m_item
.m_text
);
1281 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
1283 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
1284 cevent
.SetEventObject( this );
1285 GetEventHandler()->ProcessEvent( cevent
);
1288 void wxFileDialog::OnSelected( wxListEvent
&event
)
1290 wxString
filename( event
.m_item
.m_text
);
1291 if (filename
== wxT("..")) return;
1294 m_list
->GetDir( dir
);
1295 if (!IsTopMostDir(dir
))
1296 dir
+= wxFILE_SEP_PATH
;
1298 if (wxDirExists(dir
)) return;
1300 m_text
->SetValue( filename
);
1303 void wxFileDialog::HandleAction( const wxString
&fn
)
1305 wxString
filename( fn
);
1307 m_list
->GetDir( dir
);
1308 if (filename
.IsEmpty()) return;
1309 if (filename
== wxT(".")) return;
1311 if (filename
== wxT(".."))
1313 m_list
->GoToParentDir();
1319 if (filename
== wxT("~"))
1321 m_list
->GoToHomeDir();
1326 if (filename
[0u] == wxT('~'))
1328 filename
.Remove( 0, 1 );
1329 wxString
tmp( wxGetUserHome() );
1336 if ((filename
.Find(wxT('*')) != wxNOT_FOUND
) ||
1337 (filename
.Find(wxT('?')) != wxNOT_FOUND
))
1339 if (filename
.Find(wxFILE_SEP_PATH
) != wxNOT_FOUND
)
1341 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
1344 m_list
->SetWild( filename
);
1348 if (!IsTopMostDir(dir
))
1349 dir
+= wxFILE_SEP_PATH
;
1350 if (!wxIsAbsolutePath(filename
))
1356 if (wxDirExists(filename
))
1358 m_list
->GoToDir( filename
);
1362 // append the default extension to the filename if it doesn't have any
1364 // VZ: the logic of testing for !wxFileExists() only for the open file
1365 // dialog is not entirely clear to me, why don't we allow saving to a
1366 // file without extension as well?
1367 if ( !(m_dialogStyle
& wxOPEN
) || !wxFileExists(filename
) )
1370 wxSplitPath(filename
, NULL
, NULL
, &ext
);
1373 // append the first extension of the filter string
1374 filename
+= m_filterExtension
.BeforeFirst(_T(';'));
1378 // check that the file [doesn't] exist if necessary
1379 if ( (m_dialogStyle
& wxSAVE
) &&
1380 (m_dialogStyle
& wxOVERWRITE_PROMPT
) &&
1381 wxFileExists( filename
) )
1384 msg
.Printf( _("File '%s' already exists, do you really want to "
1385 "overwrite it?"), filename
.c_str() );
1387 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
1390 else if ( (m_dialogStyle
& wxOPEN
) &&
1391 (m_dialogStyle
& wxFILE_MUST_EXIST
) &&
1392 !wxFileExists(filename
) )
1394 wxMessageBox(_("Please choose an existing file."), _("Error"),
1395 wxOK
| wxICON_ERROR
);
1398 SetPath( filename
);
1400 // change to the directory where the user went if asked
1401 if ( m_dialogStyle
& wxCHANGE_DIR
)
1404 wxSplitPath(filename
, &cwd
, NULL
, NULL
);
1406 if ( cwd
!= wxGetWorkingDirectory() )
1408 wxSetWorkingDirectory(cwd
);
1412 wxCommandEvent event
;
1413 wxDialog::OnOK(event
);
1416 void wxFileDialog::OnListOk( wxCommandEvent
&WXUNUSED(event
) )
1418 HandleAction( m_text
->GetValue() );
1421 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
1423 m_list
->ChangeToListMode();
1424 ms_lastViewStyle
= wxLC_LIST
;
1428 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
1430 m_list
->ChangeToReportMode();
1431 ms_lastViewStyle
= wxLC_REPORT
;
1435 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
1437 m_list
->GoToParentDir();
1441 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
1443 m_list
->GoToHomeDir();
1447 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
1452 void wxFileDialog::SetPath( const wxString
& path
)
1454 // not only set the full path but also update filename and dir
1456 if ( !path
.empty() )
1459 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
1462 m_fileName
+= wxT(".");
1468 void wxFileDialog::GetPaths( wxArrayString
& paths
) const
1471 if (m_list
->GetSelectedItemCount() == 0)
1473 paths
.Add( GetPath() );
1477 paths
.Alloc( m_list
->GetSelectedItemCount() );
1480 m_list
->GetDir( dir
);
1482 if (dir
!= wxT("/"))
1484 dir
+= wxFILE_SEP_PATH
;
1487 item
.m_mask
= wxLIST_MASK_TEXT
;
1489 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1490 while ( item
.m_itemId
!= -1 )
1492 m_list
->GetItem( item
);
1493 paths
.Add( dir
+ item
.m_text
);
1494 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1495 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1499 void wxFileDialog::GetFilenames(wxArrayString
& files
) const
1502 if (m_list
->GetSelectedItemCount() == 0)
1504 files
.Add( GetFilename() );
1507 files
.Alloc( m_list
->GetSelectedItemCount() );
1510 item
.m_mask
= wxLIST_MASK_TEXT
;
1512 item
.m_itemId
= m_list
->GetNextItem( -1, wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1513 while ( item
.m_itemId
!= -1 )
1515 m_list
->GetItem( item
);
1516 files
.Add( item
.m_text
);
1517 item
.m_itemId
= m_list
->GetNextItem( item
.m_itemId
,
1518 wxLIST_NEXT_ALL
, wxLIST_STATE_SELECTED
);
1524 // ----------------------------------------------------------------------------
1526 // ----------------------------------------------------------------------------
1529 wxFileSelectorEx(const wxChar
*message
,
1530 const wxChar
*default_path
,
1531 const wxChar
*default_filename
,
1532 int *WXUNUSED(indexDefaultExtension
),
1533 const wxChar
*wildcard
,
1538 // TODO: implement this somehow
1539 return wxFileSelector(message
, default_path
, default_filename
, wxT(""),
1540 wildcard
, flags
, parent
, x
, y
);
1543 wxString
wxFileSelector( const wxChar
*title
,
1544 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
1545 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
1546 wxWindow
*parent
, int x
, int y
)
1549 if ( defaultExtension
&& !filter
)
1550 filter2
= wxString(wxT("*.")) + wxString(defaultExtension
) ;
1554 wxString defaultDirString
;
1556 defaultDirString
= defaultDir
;
1558 wxString defaultFilenameString
;
1559 if (defaultFileName
)
1560 defaultFilenameString
= defaultFileName
;
1562 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
1564 if ( fileDialog
.ShowModal() == wxID_OK
)
1566 return fileDialog
.GetPath();
1570 return wxEmptyString
;
1574 static wxString
GetWildcardString(const wxChar
*ext
)
1579 if ( *ext
== wxT('.') )
1582 wild
<< _T("*.") << ext
;
1584 else // no extension specified
1586 wild
= wxFileSelectorDefaultWildcardStr
;
1592 wxString
wxLoadFileSelector(const wxChar
*what
,
1594 const wxChar
*nameDef
,
1598 if ( what
&& *what
)
1599 prompt
= wxString::Format(_("Load %s file"), what
);
1601 prompt
= _("Load file");
1603 return wxFileSelector(prompt
, NULL
, nameDef
, ext
,
1604 GetWildcardString(ext
), 0, parent
);
1607 wxString
wxSaveFileSelector(const wxChar
*what
,
1609 const wxChar
*nameDef
,
1613 if ( what
&& *what
)
1614 prompt
= wxString::Format(_("Save %s file"), what
);
1616 prompt
= _("Save file");
1618 return wxFileSelector(prompt
, NULL
, nameDef
, ext
,
1619 GetWildcardString(ext
), 0, parent
);
1622 // A module to allow icons table cleanup
1624 class wxFileDialogGenericModule
: public wxModule
1626 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule
)
1628 wxFileDialogGenericModule() {}
1629 bool OnInit() { return TRUE
; }
1630 void OnExit() { if (g_IconsTable
) {delete g_IconsTable
; g_IconsTable
= NULL
;} }
1633 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule
, wxModule
)
1635 #endif // wxUSE_FILEDLG