1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "filedlgg.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
24 #error wxFileDialog currently only supports unix
27 #include "wx/filedlg.h"
31 #include "wx/msgdlg.h"
33 #include "wx/bmpbuttn.h"
34 #include "wx/tokenzr.h"
37 #include "wx/tooltip.h"
40 #include "sys/types.h"
47 #include "wx/generic/home.xpm"
48 #include "wx/generic/listview.xpm"
49 #include "wx/generic/repview.xpm"
50 #include "wx/generic/new_dir.xpm"
51 #include "wx/generic/dir_up.xpm"
54 static char * folder_xpm
[] = {
55 /* width height ncolors chars_per_pixel */
82 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
86 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
88 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
94 stat( m_fileName
.GetData(), &buff
);
96 lstat( m_fileName
.GetData(), &lbuff
);
98 struct tm
*t
= localtime( &lbuff
.st_mtime
);
99 // struct passwd *user = getpwuid( buff.st_uid );
100 // struct group *grp = getgrgid( buff.st_gid );
102 m_isDir
= S_ISDIR( buff
.st_mode
);
103 m_isLink
= S_ISLNK( lbuff
.st_mode
);
104 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
106 m_size
= buff
.st_size
;
109 m_minute
= t
->tm_min
;
110 m_month
= t
->tm_mon
+1;
114 m_permissions
.sprintf( "%c%c%c",
115 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? 'r' : '-'),
116 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? 'w' : '-'),
117 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? 'x' : '-') );
120 wxString
wxFileData::GetName() const
125 wxString
wxFileData::GetFullName() const
130 wxString
wxFileData::GetHint() const
132 wxString s
= m_fileName
;
134 if (m_isDir
) s
+= _("<DIR> ");
135 else if (m_isLink
) s
+= _("<LINK> ");
138 s
+= LongToString( m_size
);
141 s
+= IntToString( m_day
);
143 s
+= IntToString( m_month
);
145 s
+= IntToString( m_year
);
147 s
+= IntToString( m_hour
);
149 s
+= IntToString( m_minute
);
155 wxString
wxFileData::GetEntry( const int num
)
164 if (m_isDir
) s
= _("<DIR>");
165 else if (m_isLink
) s
= _("<LINK>");
166 else s
= LongToString( m_size
);
169 if (m_day
< 10) s
= _T("0"); else s
= _T("");
170 s
+= IntToString( m_day
);
172 if (m_month
< 10) s
+= _T("0");
173 s
+= IntToString( m_month
);
175 if (m_year
< 10) s
+= _T("0"); // this should happen real soon...
176 s
+= IntToString( m_year
);
179 if (m_hour
< 10) s
= _T("0"); else s
= _T("");
180 s
+= IntToString( m_hour
);
182 if (m_minute
< 10) s
+= _T("0");
183 s
+= IntToString( m_minute
);
195 bool wxFileData::IsDir()
200 bool wxFileData::IsExe()
205 bool wxFileData::IsLink()
210 long wxFileData::GetSize()
215 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
221 void wxFileData::MakeItem( wxListItem
&item
)
223 item
.m_text
= m_name
;
224 item
.m_colour
= wxBLACK
;
225 if (IsExe()) item
.m_colour
= wxRED
;
226 if (IsDir()) item
.m_colour
= wxBLUE
;
227 if (IsDir()) item
.m_image
= 0; else item
.m_image
= -1;
230 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
233 item
.m_data
= (long)this;
236 //-----------------------------------------------------------------------------
238 //-----------------------------------------------------------------------------
240 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
242 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
243 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
244 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
247 wxFileCtrl::wxFileCtrl()
250 m_showHidden
= FALSE
;
253 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
254 const wxString
&dirName
, const wxString
&wild
,
255 const wxPoint
&pos
, const wxSize
&size
,
256 long style
, const wxValidator
&validator
, const wxString
&name
) :
257 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
259 wxImageList
*imageList
= new wxImageList( 16, 16 );
260 imageList
->Add( wxBitmap( folder_xpm
) );
261 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
265 m_showHidden
= FALSE
;
269 void wxFileCtrl::ChangeToListMode()
271 SetSingleStyle( wxLC_LIST
);
275 void wxFileCtrl::ChangeToReportMode()
277 SetSingleStyle( wxLC_REPORT
);
281 void wxFileCtrl::ChangeToIconMode()
283 SetSingleStyle( wxLC_ICON
);
287 void wxFileCtrl::ShowHidden( bool show
)
293 int ListCompare( const long data1
, const long data2
, const long WXUNUSED(data
) )
295 wxFileData
*fd1
= (wxFileData
*)data1
;
296 wxFileData
*fd2
= (wxFileData
*)data2
;
297 if (fd1
->GetName() == _T("..")) return -1;
298 if (fd2
->GetName() == _T("..")) return 1;
299 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
300 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
301 return strcmp( fd1
->GetName(), fd2
->GetName() );
304 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
307 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
308 fd
->MakeItem( item
);
309 long my_style
= GetWindowStyleFlag();
310 if (my_style
& wxLC_REPORT
)
312 ret
= InsertItem( item
);
313 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
315 else if (my_style
& wxLC_LIST
)
317 ret
= InsertItem( item
);
322 void wxFileCtrl::Update()
325 long my_style
= GetWindowStyleFlag();
326 if (my_style
& wxLC_REPORT
)
328 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, 130 );
329 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
330 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 55 );
331 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
332 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
334 wxFileData
*fd
= (wxFileData
*) NULL
;
339 if (m_dirName
!= _T("/"))
341 wxString
p( wxPathOnly(m_dirName
) );
342 if (p
.IsEmpty()) p
= _T("/");
343 fd
= new wxFileData( _T(".."), p
);
348 wxString res
= m_dirName
+ _T("/*");
349 wxString
f( wxFindFirstFile( res
.GetData(), wxDIR
) );
352 res
= wxFileNameFromPath( f
);
353 fd
= new wxFileData( res
, f
);
354 wxString s
= fd
->GetName();
355 if (m_showHidden
|| (s
[0] != _T('.')))
360 f
= wxFindNextFile();
363 res
= m_dirName
+ _T("/") + m_wild
;
364 f
= wxFindFirstFile( res
.GetData(), wxFILE
);
367 res
= wxFileNameFromPath( f
);
368 fd
= new wxFileData( res
, f
);
369 wxString s
= fd
->GetName();
370 if (m_showHidden
|| (s
[0] != _T('.')))
375 f
= wxFindNextFile();
378 SortItems( ListCompare
, 0 );
381 void wxFileCtrl::SetWild( const wxString
&wild
)
387 void wxFileCtrl::MakeDir()
389 wxString
new_name( _T("NewName") );
390 wxString
path( m_dirName
);
393 if (wxFileExists(path
))
395 // try NewName0, NewName1 etc.
398 new_name
= _("NewName");
400 num
.Printf( _T("%d"), i
);
407 } while (wxFileExists(path
));
413 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
418 wxFileData
*fd
= new wxFileData( new_name
, path
);
422 int id
= Add( fd
, item
);
426 SortItems( ListCompare
, 0 );
427 id
= FindItem( 0, (long)fd
);
433 void wxFileCtrl::GoToParentDir()
435 if (m_dirName
!= _T("/"))
437 wxString
fname( wxFileNameFromPath(m_dirName
) );
438 m_dirName
= wxPathOnly( m_dirName
);
439 if (m_dirName
.IsEmpty()) m_dirName
= _T("/");
441 int id
= FindItem( 0, fname
);
444 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
450 void wxFileCtrl::GoToHomeDir()
452 wxString s
= wxGetUserHome( wxString() );
455 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
459 void wxFileCtrl::GoToDir( const wxString
&dir
)
463 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
467 void wxFileCtrl::GetDir( wxString
&dir
)
472 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
474 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
478 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
480 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
483 if ((event
.GetLabel().IsEmpty()) ||
484 (event
.GetLabel() == _(".")) ||
485 (event
.GetLabel() == _("..")) ||
486 (event
.GetLabel().First( _T("/") ) != wxNOT_FOUND
))
488 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
494 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
496 new_name
+= event
.GetLabel();
500 if (wxFileExists(new_name
))
502 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
507 if (wxRenameFile(fd
->GetFullName(),new_name
))
509 fd
->SetNewName( new_name
, event
.GetLabel() );
510 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
511 EnsureVisible( event
.GetItem() );
515 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
521 //-----------------------------------------------------------------------------
523 //-----------------------------------------------------------------------------
525 #define ID_LIST_MODE 5000
526 #define ID_REPORT_MODE 5001
527 #define ID_UP_DIR 5005
528 #define ID_PARENT_DIR 5006
529 #define ID_NEW_DIR 5007
530 #define ID_CHOICE 5008
532 #define ID_LIST_CTRL 5010
534 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
536 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
537 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
538 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
539 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
540 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
541 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
542 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
543 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
544 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
545 EVT_CHOICE(ID_CHOICE
,wxFileDialog::OnChoice
)
546 EVT_TEXT_ENTER(ID_TEXT
,wxFileDialog::OnTextEnter
)
549 wxFileDialog::wxFileDialog(wxWindow
*parent
,
550 const wxString
& message
,
551 const wxString
& defaultDir
,
552 const wxString
& defaultFile
,
553 const wxString
& wildCard
,
555 const wxPoint
& pos
) :
556 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, style
| wxRESIZE_BORDER
)
561 m_dialogStyle
= style
;
563 if (m_dir
.IsEmpty()) m_dir
= wxGetUserHome();
566 m_path
+= defaultFile
;
567 m_fileName
= defaultFile
;
568 m_wildCard
= wildCard
;
571 // interpret wildcards
573 if (m_wildCard
.IsEmpty())
574 m_wildCard
= _("All files (*)|*");
576 wxStringTokenizer
tokens( m_wildCard
, _T("|") );
578 wxString firstWildText
;
579 if (tokens
.CountTokens() == 1)
581 firstWildText
= tokens
.GetNextToken();
582 firstWild
= firstWildText
;
586 wxASSERT_MSG( tokens
.CountTokens() % 2 == 0, _T("Wrong file type descripition") );
587 firstWildText
= tokens
.GetNextToken();
588 firstWild
= tokens
.GetNextToken();
593 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
595 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
599 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
601 but
->SetToolTip( _("View files as a list view") );
603 buttonsizer
->Add( but
, 0, wxALL
, 5 );
605 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
607 but
->SetToolTip( _("View files as a detailed view") );
609 buttonsizer
->Add( but
, 0, wxALL
, 5 );
611 buttonsizer
->Add( 30, 5, 1 );
613 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
615 but
->SetToolTip( _("Go to parent directory") );
617 buttonsizer
->Add( but
, 0, wxALL
, 5 );
619 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
621 but
->SetToolTip( _("Go to home directory") );
623 buttonsizer
->Add( but
, 0, wxALL
, 5);
625 buttonsizer
->Add( 20, 20 );
627 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
629 but
->SetToolTip( _("Create new directory") );
631 buttonsizer
->Add( but
, 0, wxALL
, 5 );
633 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
635 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, firstWild
, wxDefaultPosition
, wxSize(440,180),
636 wxLC_LIST
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
637 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
639 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
640 m_text
= new wxTextCtrl( this, ID_TEXT
, m_fileName
, wxDefaultPosition
, wxDefaultSize
, wxPROCESS_ENTER
);
641 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
642 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
643 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
645 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
646 m_choice
= new wxChoice( this, ID_CHOICE
);
647 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
648 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
649 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
651 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
652 while (tokens
.HasMoreTokens())
654 firstWildText
= tokens
.GetNextToken();
655 firstWild
= tokens
.GetNextToken();
656 m_choice
->Append( firstWildText
, (void*) new wxString( firstWild
) );
658 m_choice
->SetSelection( 0 );
660 SetAutoLayout( TRUE
);
661 SetSizer( mainsizer
);
663 mainsizer
->Fit( this );
664 mainsizer
->SetSizeHints( this );
673 wxFileDialog::~wxFileDialog()
677 void wxFileDialog::OnChoice( wxCommandEvent
&event
)
679 wxString
*str
= (wxString
*) m_choice
->GetClientData( event
.GetInt() );
680 m_list
->SetWild( *str
);
683 void wxFileDialog::OnActivated( wxListEvent
&WXUNUSED(event
) )
685 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
686 cevent
.SetEventObject( this );
687 GetEventHandler()->ProcessEvent( cevent
);
690 void wxFileDialog::OnTextEnter( wxCommandEvent
&WXUNUSED(event
) )
692 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
693 cevent
.SetEventObject( this );
694 GetEventHandler()->ProcessEvent( cevent
);
697 void wxFileDialog::OnSelected( wxListEvent
&event
)
699 if (FindFocus() == m_list
)
700 m_text
->SetValue( event
.m_item
.m_text
);
703 void wxFileDialog::OnListOk( wxCommandEvent
&event
)
705 wxString
filename( m_text
->GetValue() );
707 m_list
->GetDir( dir
);
708 if (filename
.IsEmpty()) return;
709 if (filename
== _T(".")) return;
711 if (filename
== _T(".."))
713 m_list
->GoToParentDir();
718 if ((filename
.Find(_T('*')) != wxNOT_FOUND
) ||
719 (filename
.Find(_T('?')) != wxNOT_FOUND
))
721 if (filename
.Find(_T('/')) != wxNOT_FOUND
)
723 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK
| wxICON_ERROR
);
726 m_list
->SetWild( filename
);
730 if (dir
!= _T("/")) dir
+= _T("/");
734 if (wxDirExists(filename
))
736 m_list
->GoToDir( filename
);
737 m_text
->SetValue( _T("") );
741 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
743 if (wxFileExists( filename
))
746 msg
.Printf( _("File '%s' already exists, do you really want to "
747 "overwrite it?"), filename
.c_str() );
749 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
753 else if ( (m_dialogStyle
& wxOPEN
) && (m_dialogStyle
& wxFILE_MUST_EXIST
) )
755 if ( !wxFileExists( filename
) )
757 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
| wxICON_ERROR
);
766 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
768 m_list
->ChangeToListMode();
772 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
774 m_list
->ChangeToReportMode();
778 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
780 m_list
->GoToParentDir();
784 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
786 m_list
->GoToHomeDir();
790 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
795 void wxFileDialog::SetPath( const wxString
& path
)
797 // not only set the full path but also update filename and dir
802 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
805 m_fileName
+= _T(".");
811 // ----------------------------------------------------------------------------
813 // ----------------------------------------------------------------------------
816 wxFileSelectorEx(const wxChar
*message
,
817 const wxChar
*default_path
,
818 const wxChar
*default_filename
,
819 int *indexDefaultExtension
,
820 const wxChar
*wildcard
,
825 // TODO: implement this somehow
826 return wxFileSelector(message
, default_path
, default_filename
, _T(""),
827 wildcard
, flags
, parent
, x
, y
);
830 wxString
wxFileSelector( const wxChar
*title
,
831 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
832 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
833 wxWindow
*parent
, int x
, int y
)
836 if ( defaultExtension
&& !filter
)
837 filter2
= wxString(_T("*.")) + wxString(defaultExtension
) ;
841 wxString defaultDirString
;
843 defaultDirString
= defaultDir
;
845 wxString defaultFilenameString
;
847 defaultFilenameString
= defaultFileName
;
849 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
851 if ( fileDialog
.ShowModal() == wxID_OK
)
853 return fileDialog
.GetPath();
857 return wxEmptyString
;
861 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
863 wxChar
*ext
= (wxChar
*)extension
;
866 wxString str
= _("Load %s file");
867 wxSprintf(prompt
, str
, what
);
869 if (*ext
== _T('.')) ext
++;
871 wxSprintf(wild
, _T("*.%s"), ext
);
873 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
876 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
879 wxChar
*ext
= (wxChar
*)extension
;
882 wxString str
= _("Save %s file");
883 wxSprintf(prompt
, str
, what
);
885 if (*ext
== _T('.')) ext
++;
887 wxSprintf(wild
, _T("*.%s"), ext
);
889 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);