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"
36 #include "wx/tooltip.h"
39 #include "sys/types.h"
46 #include "wx/generic/home.xpm"
47 #include "wx/generic/listview.xpm"
48 #include "wx/generic/repview.xpm"
49 #include "wx/generic/new_dir.xpm"
50 #include "wx/generic/dir_up.xpm"
53 static char * folder_xpm
[] = {
54 /* width height ncolors chars_per_pixel */
81 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
85 IMPLEMENT_DYNAMIC_CLASS(wxFileData
,wxObject
);
87 wxFileData::wxFileData( const wxString
&name
, const wxString
&fname
)
93 stat( m_fileName
.GetData(), &buff
);
95 lstat( m_fileName
.GetData(), &lbuff
);
97 struct tm
*t
= localtime( &lbuff
.st_mtime
);
98 // struct passwd *user = getpwuid( buff.st_uid );
99 // struct group *grp = getgrgid( buff.st_gid );
101 m_isDir
= S_ISDIR( buff
.st_mode
);
102 m_isLink
= S_ISLNK( lbuff
.st_mode
);
103 m_isExe
= ((buff
.st_mode
& S_IXUSR
) == S_IXUSR
);
105 m_size
= buff
.st_size
;
108 m_minute
= t
->tm_min
;
109 m_month
= t
->tm_mon
+1;
113 m_permissions
.sprintf( "%c%c%c",
114 ((( buff
.st_mode
& S_IRUSR
) == S_IRUSR
) ? 'r' : '-'),
115 ((( buff
.st_mode
& S_IWUSR
) == S_IWUSR
) ? 'w' : '-'),
116 ((( buff
.st_mode
& S_IXUSR
) == S_IXUSR
) ? 'x' : '-') );
119 wxString
wxFileData::GetName() const
124 wxString
wxFileData::GetFullName() const
129 wxString
wxFileData::GetHint() const
131 wxString s
= m_fileName
;
133 if (m_isDir
) s
+= _("<DIR> ");
134 else if (m_isLink
) s
+= _("<LINK> ");
137 s
+= LongToString( m_size
);
140 s
+= IntToString( m_day
);
142 s
+= IntToString( m_month
);
144 s
+= IntToString( m_year
);
146 s
+= IntToString( m_hour
);
148 s
+= IntToString( m_minute
);
154 wxString
wxFileData::GetEntry( const int num
)
163 if (m_isDir
) s
= _("<DIR>");
164 else if (m_isLink
) s
= _("<LINK>");
165 else s
= LongToString( m_size
);
168 if (m_day
< 10) s
= _T("0"); else s
= _T("");
169 s
+= IntToString( m_day
);
171 if (m_month
< 10) s
+= _T("0");
172 s
+= IntToString( m_month
);
174 if (m_year
< 10) s
+= _T("0"); // this should happen real soon...
175 s
+= IntToString( m_year
);
178 if (m_hour
< 10) s
= _T("0"); else s
= _T("");
179 s
+= IntToString( m_hour
);
181 if (m_minute
< 10) s
+= _T("0");
182 s
+= IntToString( m_minute
);
194 bool wxFileData::IsDir()
199 bool wxFileData::IsExe()
204 bool wxFileData::IsLink()
209 long wxFileData::GetSize()
214 void wxFileData::SetNewName( const wxString
&name
, const wxString
&fname
)
220 void wxFileData::MakeItem( wxListItem
&item
)
222 item
.m_text
= m_name
;
223 item
.m_colour
= wxBLACK
;
224 if (IsExe()) item
.m_colour
= wxRED
;
225 if (IsDir()) item
.m_colour
= wxBLUE
;
226 if (IsDir()) item
.m_image
= 0; else item
.m_image
= -1;
229 wxColour
*dg
= wxTheColourDatabase
->FindColour( "MEDIUM GREY" );
232 item
.m_data
= (long)this;
235 //-----------------------------------------------------------------------------
237 //-----------------------------------------------------------------------------
239 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl
,wxListCtrl
);
241 BEGIN_EVENT_TABLE(wxFileCtrl
,wxListCtrl
)
242 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem
)
243 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit
)
246 wxFileCtrl::wxFileCtrl()
249 m_showHidden
= FALSE
;
252 wxFileCtrl::wxFileCtrl( wxWindow
*win
, wxWindowID id
,
253 const wxString
&dirName
, const wxString
&wild
,
254 const wxPoint
&pos
, const wxSize
&size
,
255 long style
, const wxValidator
&validator
, const wxString
&name
) :
256 wxListCtrl( win
, id
, pos
, size
, style
, validator
, name
)
258 wxImageList
*imageList
= new wxImageList( 16, 16 );
259 imageList
->Add( wxBitmap( folder_xpm
) );
260 SetImageList( imageList
, wxIMAGE_LIST_SMALL
);
264 m_showHidden
= FALSE
;
268 void wxFileCtrl::ChangeToListMode()
270 SetSingleStyle( wxLC_LIST
);
274 void wxFileCtrl::ChangeToReportMode()
276 SetSingleStyle( wxLC_REPORT
);
280 void wxFileCtrl::ChangeToIconMode()
282 SetSingleStyle( wxLC_ICON
);
286 void wxFileCtrl::ShowHidden( bool show
)
292 int ListCompare( const long data1
, const long data2
, const long WXUNUSED(data
) )
294 wxFileData
*fd1
= (wxFileData
*)data1
;
295 wxFileData
*fd2
= (wxFileData
*)data2
;
296 if (fd1
->GetName() == _T("..")) return -1;
297 if (fd2
->GetName() == _T("..")) return 1;
298 if (fd1
->IsDir() && !fd2
->IsDir()) return -1;
299 if (fd2
->IsDir() && !fd1
->IsDir()) return 1;
300 return strcmp( fd1
->GetName(), fd2
->GetName() );
303 long wxFileCtrl::Add( wxFileData
*fd
, wxListItem
&item
)
306 item
.m_mask
= wxLIST_MASK_TEXT
+ wxLIST_MASK_DATA
+ wxLIST_MASK_IMAGE
;
307 fd
->MakeItem( item
);
308 long my_style
= GetWindowStyleFlag();
309 if (my_style
& wxLC_REPORT
)
311 ret
= InsertItem( item
);
312 for (int i
= 1; i
< 5; i
++) SetItem( item
.m_itemId
, i
, fd
->GetEntry( i
) );
314 else if (my_style
& wxLC_LIST
)
316 ret
= InsertItem( item
);
321 void wxFileCtrl::Update()
324 long my_style
= GetWindowStyleFlag();
325 if (my_style
& wxLC_REPORT
)
327 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT
, 130 );
328 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT
, 60 );
329 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT
, 55 );
330 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT
, 50 );
331 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT
, 120 );
333 wxFileData
*fd
= (wxFileData
*) NULL
;
338 if (m_dirName
!= _T("/"))
340 wxString
p( wxPathOnly(m_dirName
) );
341 if (p
.IsEmpty()) p
= _T("/");
342 fd
= new wxFileData( _T(".."), p
);
347 wxString res
= m_dirName
+ _T("/") + m_wild
;
348 wxString
f( wxFindFirstFile( res
.GetData(), 0 ) );
351 res
= wxFileNameFromPath( f
);
352 fd
= new wxFileData( res
, f
);
353 wxString s
= fd
->GetName();
354 if (m_showHidden
|| (s
[0] != _T('.')))
359 f
= wxFindNextFile();
361 SortItems( ListCompare
, 0 );
364 void wxFileCtrl::SetWild( const wxString
&wild
)
370 void wxFileCtrl::MakeDir()
372 wxString
new_name( _T("NewName") );
373 wxString
path( m_dirName
);
376 if (wxFileExists(path
))
378 // try NewName0, NewName1 etc.
381 new_name
= _("NewName");
383 num
.Printf( _T("%d"), i
);
390 } while (wxFileExists(path
));
396 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
401 wxFileData
*fd
= new wxFileData( new_name
, path
);
405 int id
= Add( fd
, item
);
409 SortItems( ListCompare
, 0 );
410 id
= FindItem( 0, (long)fd
);
416 void wxFileCtrl::GoToParentDir()
418 if (m_dirName
!= _T("/"))
420 wxString
fname( wxFileNameFromPath(m_dirName
) );
421 m_dirName
= wxPathOnly( m_dirName
);
422 if (m_dirName
.IsEmpty()) m_dirName
= _T("/");
424 int id
= FindItem( 0, fname
);
427 SetItemState( id
, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
433 void wxFileCtrl::GoToHomeDir()
435 wxString s
= wxGetUserHome( wxString() );
438 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
442 void wxFileCtrl::GoToDir( const wxString
&dir
)
446 SetItemState( 0, wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
450 void wxFileCtrl::GetDir( wxString
&dir
)
455 void wxFileCtrl::OnListDeleteItem( wxListEvent
&event
)
457 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
461 void wxFileCtrl::OnListEndLabelEdit( wxListEvent
&event
)
463 wxFileData
*fd
= (wxFileData
*)event
.m_item
.m_data
;
466 if ((event
.GetLabel().IsEmpty()) ||
467 (event
.GetLabel() == _(".")) ||
468 (event
.GetLabel() == _("..")) ||
469 (event
.GetLabel().First( _T("/") ) != wxNOT_FOUND
))
471 wxMessageDialog
dialog(this, _("Illegal directory name."), _("Error"), wxOK
| wxICON_ERROR
);
477 wxString
new_name( wxPathOnly( fd
->GetFullName() ) );
479 new_name
+= event
.GetLabel();
483 if (wxFileExists(new_name
))
485 wxMessageDialog
dialog(this, _("File name exists already."), _("Error"), wxOK
| wxICON_ERROR
);
490 if (wxRenameFile(fd
->GetFullName(),new_name
))
492 fd
->SetNewName( new_name
, event
.GetLabel() );
493 SetItemState( event
.GetItem(), wxLIST_STATE_SELECTED
, wxLIST_STATE_SELECTED
);
494 EnsureVisible( event
.GetItem() );
498 wxMessageDialog
dialog(this, _("Operation not permitted."), _("Error"), wxOK
| wxICON_ERROR
);
504 //-----------------------------------------------------------------------------
506 //-----------------------------------------------------------------------------
508 #define ID_LIST_CTRL 5010
509 #define ID_LIST_MODE 5000
510 #define ID_REPORT_MODE 5001
511 #define ID_UP_DIR 5005
512 #define ID_PARENT_DIR 5006
513 #define ID_NEW_DIR 5007
515 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
,wxDialog
)
517 BEGIN_EVENT_TABLE(wxFileDialog
,wxDialog
)
518 EVT_BUTTON(ID_LIST_MODE
, wxFileDialog::OnList
)
519 EVT_BUTTON(ID_REPORT_MODE
, wxFileDialog::OnReport
)
520 EVT_BUTTON(ID_UP_DIR
, wxFileDialog::OnUp
)
521 EVT_BUTTON(ID_PARENT_DIR
, wxFileDialog::OnHome
)
522 EVT_BUTTON(ID_NEW_DIR
, wxFileDialog::OnNew
)
523 EVT_BUTTON(wxID_OK
, wxFileDialog::OnListOk
)
524 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL
, wxFileDialog::OnSelected
)
525 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL
, wxFileDialog::OnActivated
)
528 wxFileDialog::wxFileDialog(wxWindow
*parent
,
529 const wxString
& message
,
530 const wxString
& defaultDir
,
531 const wxString
& defaultFile
,
532 const wxString
& wildCard
,
534 const wxPoint
& pos
) :
535 wxDialog( parent
, -1, message
, pos
, wxDefaultSize
, style
| wxRESIZE_BORDER
)
540 m_dialogStyle
= style
;
542 if (m_dir
.IsEmpty()) m_dir
= wxGetUserHome();
545 m_path
+= defaultFile
;
546 m_fileName
= defaultFile
;
547 m_wildCard
= wildCard
;
550 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
552 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
556 but
= new wxBitmapButton( this, ID_LIST_MODE
, wxBitmap( listview_xpm
) );
558 but
->SetToolTip( _("View files as a list view") );
560 buttonsizer
->Add( but
, 0, wxALL
, 5 );
562 but
= new wxBitmapButton( this, ID_REPORT_MODE
, wxBitmap( repview_xpm
) );
564 but
->SetToolTip( _("View files as a detailed view") );
566 buttonsizer
->Add( but
, 0, wxALL
, 5 );
568 buttonsizer
->Add( 30, 5, 1 );
570 but
= new wxBitmapButton( this, ID_UP_DIR
, wxBitmap( dir_up_xpm
) );
572 but
->SetToolTip( _("Go to parent directory") );
574 buttonsizer
->Add( but
, 0, wxALL
, 5 );
576 but
= new wxBitmapButton( this, ID_PARENT_DIR
, wxBitmap(home_xpm
) );
578 but
->SetToolTip( _("Go to home directory") );
580 buttonsizer
->Add( but
, 0, wxALL
, 5);
582 buttonsizer
->Add( 20, 20 );
584 but
= new wxBitmapButton( this, ID_NEW_DIR
, wxBitmap(new_dir_xpm
) );
586 but
->SetToolTip( _("Create new directory") );
588 buttonsizer
->Add( but
, 0, wxALL
, 5 );
590 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
592 m_list
= new wxFileCtrl( this, ID_LIST_CTRL
, m_dir
, "*", wxDefaultPosition
, wxSize(440,180),
593 wxLC_LIST
| wxSUNKEN_BORDER
| wxLC_SINGLE_SEL
);
594 mainsizer
->Add( m_list
, 1, wxEXPAND
| wxLEFT
|wxRIGHT
, 10 );
596 wxBoxSizer
*textsizer
= new wxBoxSizer( wxHORIZONTAL
);
597 m_text
= new wxTextCtrl( this, -1, m_fileName
);
598 textsizer
->Add( m_text
, 1, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
599 textsizer
->Add( new wxButton( this, wxID_OK
, _("OK") ), 0, wxCENTER
| wxLEFT
|wxRIGHT
|wxTOP
, 10 );
600 mainsizer
->Add( textsizer
, 0, wxEXPAND
);
602 wxBoxSizer
*choicesizer
= new wxBoxSizer( wxHORIZONTAL
);
603 m_choice
= new wxChoice( this, -1 );
604 m_choice
->Append( "*.txt" );
605 choicesizer
->Add( m_choice
, 1, wxCENTER
|wxALL
, 10 );
606 choicesizer
->Add( new wxButton( this, wxID_CANCEL
, _("Cancel") ), 0, wxCENTER
| wxALL
, 10 );
607 mainsizer
->Add( choicesizer
, 0, wxEXPAND
);
609 SetAutoLayout( TRUE
);
610 SetSizer( mainsizer
);
612 mainsizer
->Fit( this );
613 mainsizer
->SetSizeHints( this );
622 void wxFileDialog::OnActivated( wxListEvent
&WXUNUSED(event
) )
624 wxCommandEvent
cevent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_OK
);
625 cevent
.SetEventObject( this );
626 GetEventHandler()->ProcessEvent( cevent
);
629 void wxFileDialog::OnSelected( wxListEvent
&event
)
631 m_text
->SetValue( event
.m_item
.m_text
);
634 void wxFileDialog::OnListOk( wxCommandEvent
&event
)
636 wxString
filename( m_text
->GetValue() );
638 m_list
->GetDir( dir
);
639 if (filename
.IsEmpty()) return;
641 if (filename
== _T(".."))
643 m_list
->GoToParentDir();
648 if (dir
!= _T("/")) dir
+= _T("/");
652 if (wxDirExists(filename
))
654 m_list
->GoToDir( filename
);
655 m_text
->SetValue( _T("") );
659 if ( (m_dialogStyle
& wxSAVE
) && (m_dialogStyle
& wxOVERWRITE_PROMPT
) )
661 if (wxFileExists( filename
))
664 msg
.Printf( _("File '%s' already exists, do you really want to "
665 "overwrite it?"), filename
.c_str() );
667 if (wxMessageBox(msg
, _("Confirm"), wxYES_NO
) != wxYES
)
671 else if ( (m_dialogStyle
& wxOPEN
) && (m_dialogStyle
& wxFILE_MUST_EXIST
) )
673 if ( !wxFileExists( filename
) )
675 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK
);
685 void wxFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
687 m_list
->ChangeToListMode();
691 void wxFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
693 m_list
->ChangeToReportMode();
697 void wxFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
699 m_list
->GoToParentDir();
703 void wxFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
705 m_list
->GoToHomeDir();
709 void wxFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
714 void wxFileDialog::SetPath( const wxString
& path
)
716 // not only set the full path but also update filename and dir
721 wxSplitPath(path
, &m_dir
, &m_fileName
, &ext
);
724 m_fileName
+= _T(".");
730 // ----------------------------------------------------------------------------
732 // ----------------------------------------------------------------------------
735 wxFileSelectorEx(const wxChar
*message
,
736 const wxChar
*default_path
,
737 const wxChar
*default_filename
,
738 int *indexDefaultExtension
,
739 const wxChar
*wildcard
,
744 // TODO: implement this somehow
745 return wxFileSelector(message
, default_path
, default_filename
, _T(""),
746 wildcard
, flags
, parent
, x
, y
);
749 wxString
wxFileSelector( const wxChar
*title
,
750 const wxChar
*defaultDir
, const wxChar
*defaultFileName
,
751 const wxChar
*defaultExtension
, const wxChar
*filter
, int flags
,
752 wxWindow
*parent
, int x
, int y
)
755 if ( defaultExtension
&& !filter
)
756 filter2
= wxString(_T("*.")) + wxString(defaultExtension
) ;
760 wxString defaultDirString
;
762 defaultDirString
= defaultDir
;
764 wxString defaultFilenameString
;
766 defaultFilenameString
= defaultFileName
;
768 wxFileDialog
fileDialog( parent
, title
, defaultDirString
, defaultFilenameString
, filter2
, flags
, wxPoint(x
, y
) );
770 if ( fileDialog
.ShowModal() == wxID_OK
)
772 return fileDialog
.GetPath();
776 return wxEmptyString
;
780 wxString
wxLoadFileSelector( const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
, wxWindow
*parent
)
782 wxChar
*ext
= (wxChar
*)extension
;
785 wxString str
= _("Load %s file");
786 wxSprintf(prompt
, str
, what
);
788 if (*ext
== _T('.')) ext
++;
790 wxSprintf(wild
, _T("*.%s"), ext
);
792 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);
795 wxString
wxSaveFileSelector(const wxChar
*what
, const wxChar
*extension
, const wxChar
*default_name
,
798 wxChar
*ext
= (wxChar
*)extension
;
801 wxString str
= _("Save %s file");
802 wxSprintf(prompt
, str
, what
);
804 if (*ext
== _T('.')) ext
++;
806 wxSprintf(wild
, _T("*.%s"), ext
);
808 return wxFileSelector (prompt
, (const wxChar
*) NULL
, default_name
, ext
, wild
, 0, parent
);