1 //////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/filedlgg.cpp
3 // Purpose: wxGenericFileDialog
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
21 // NOTE : it probably also supports MAC, untested
22 #if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__)
23 #error wxGenericFileDialog currently only supports Unix, win32 and DOS
28 #include "wx/msw/wrapwin.h"
32 #include "wx/settings.h"
34 #include "wx/msgdlg.h"
35 #include "wx/bmpbuttn.h"
36 #include "wx/checkbox.h"
37 #include "wx/choice.h"
38 #include "wx/stattext.h"
39 #include "wx/textctrl.h"
41 #include "wx/filedlg.h" // wxFD_OPEN, wxFD_SAVE...
44 #include "wx/longlong.h"
45 #include "wx/config.h"
46 #include "wx/imaglist.h"
47 #include "wx/artprov.h"
48 #include "wx/filefn.h"
49 #include "wx/filectrl.h"
50 #include "wx/generic/filedlgg.h"
54 #include "wx/tooltip.h"
58 #include <sys/types.h>
71 #include "wx/msw/mslu.h"
82 #if defined(__UNIX__) || defined(__DOS__)
86 #if defined(__WXWINCE__)
87 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
88 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
89 #define IsTopMostDir(dir) (dir.empty())
91 #define IsTopMostDir(dir) (dir == wxT("/"))
94 //-----------------------------------------------------------------------------
95 // wxGenericFileDialog
96 //-----------------------------------------------------------------------------
98 #define ID_LIST_MODE (wxID_FILEDLGG )
99 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
100 #define ID_UP_DIR (wxID_FILEDLGG + 2)
101 #define ID_PARENT_DIR (wxID_FILEDLGG + 3)
102 #define ID_NEW_DIR (wxID_FILEDLGG + 4)
103 #define ID_FILE_CTRL (wxID_FILEDLGG + 5)
105 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
107 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
108 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
109 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
110 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
111 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog::OnHome
)
112 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
113 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnOk
)
114 EVT_FILECTRL_FILEACTIVATED(ID_FILE_CTRL
, wxGenericFileDialog::OnFileActivated
)
116 EVT_UPDATE_UI(ID_UP_DIR
, wxGenericFileDialog::OnUpdateButtonsUI
)
117 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
118 EVT_UPDATE_UI(ID_NEW_DIR
, wxGenericFileDialog::OnUpdateButtonsUI
)
119 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
122 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
123 bool wxGenericFileDialog::ms_lastShowHidden
= false;
125 void wxGenericFileDialog::Init()
127 m_bypassGenericImpl
= false;
130 m_upDirButton
= NULL
;
131 m_newDirButton
= NULL
;
134 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
135 const wxString
& message
,
136 const wxString
& defaultDir
,
137 const wxString
& defaultFile
,
138 const wxString
& wildCard
,
142 const wxString
& name
,
143 bool bypassGenericImpl
) : wxFileDialogBase()
146 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, sz
, name
, bypassGenericImpl
);
149 bool wxGenericFileDialog::Create( wxWindow
*parent
,
150 const wxString
& message
,
151 const wxString
& defaultDir
,
152 const wxString
& defaultFile
,
153 const wxString
& wildCard
,
157 const wxString
& name
,
158 bool bypassGenericImpl
)
160 m_bypassGenericImpl
= bypassGenericImpl
;
162 parent
= GetParentForModalDialog(parent
);
164 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
165 wildCard
, style
, pos
, sz
, name
))
170 if (m_bypassGenericImpl
)
173 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, sz
,
174 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
| style
, name
181 if (wxConfig::Get(false))
183 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
185 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
190 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
194 m_dir
= wxFILE_SEP_PATH
;
197 const size_t len
= m_dir
.length();
198 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
199 m_dir
.Remove( len
-1, 1 );
202 m_path
+= wxFILE_SEP_PATH
;
203 m_path
+= defaultFile
;
204 m_filterExtension
= wxEmptyString
;
208 const bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
210 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
212 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
216 but
= new wxBitmapButton(this, ID_LIST_MODE
,
217 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
219 but
->SetToolTip( _("View files as a list view") );
221 buttonsizer
->Add( but
, 0, wxALL
, 5 );
223 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
224 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
226 but
->SetToolTip( _("View files as a detailed view") );
228 buttonsizer
->Add( but
, 0, wxALL
, 5 );
230 buttonsizer
->Add( 30, 5, 1 );
232 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
233 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
235 m_upDirButton
->SetToolTip( _("Go to parent directory") );
237 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
239 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
240 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
241 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
243 but
->SetToolTip( _("Go to home directory") );
245 buttonsizer
->Add( but
, 0, wxALL
, 5);
247 buttonsizer
->Add( 20, 20 );
250 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
251 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
253 m_newDirButton
->SetToolTip( _("Create new directory") );
255 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
258 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
260 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
263 if ( HasFdFlag(wxFD_MULTIPLE
) )
264 style2
|= wxFC_MULTIPLE
;
266 m_filectrl
= new wxGenericFileCtrl( this, ID_FILE_CTRL
,
270 wxDefaultPosition
, wxSize(540,200)
273 m_filectrl
->ShowHidden( ms_lastShowHidden
);
275 if (ms_lastViewStyle
== wxLC_LIST
)
277 m_filectrl
->ChangeToListMode();
279 else if (ms_lastViewStyle
== wxLC_REPORT
)
281 m_filectrl
->ChangeToReportMode();
286 // PDAs have a different screen layout
287 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().HorzBorder());
289 wxSizer
*bsizer
= CreateButtonSizer(wxOK
| wxCANCEL
);
291 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().Border());
295 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().DoubleHorzBorder());
297 wxBoxSizer
*okcancelsizer
= new wxBoxSizer( wxHORIZONTAL
);
298 okcancelsizer
->Add(new wxButton(this, wxID_OK
), wxSizerFlags().DoubleBorder().Centre());
299 okcancelsizer
->Add(new wxButton(this, wxID_CANCEL
), wxSizerFlags().DoubleBorder().Centre());
300 mainsizer
->Add(okcancelsizer
, wxSizerFlags().Center());
303 SetAutoLayout( true );
304 SetSizer( mainsizer
);
308 mainsizer
->Fit( this );
309 mainsizer
->SetSizeHints( this );
317 wxGenericFileDialog::~wxGenericFileDialog()
319 if (!m_bypassGenericImpl
)
322 if (wxConfig::Get(false))
324 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
326 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
333 int wxGenericFileDialog::ShowModal()
335 m_filectrl
->SetDirectory(m_dir
);
337 return wxDialog::ShowModal();
340 bool wxGenericFileDialog::Show( bool show
)
342 // Called by ShowModal, so don't repeate the update
346 m_filectrl
->SetDirectory(m_dir
);
350 return wxDialog::Show( show
);
353 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
355 m_filectrl
->SetWildcard(wildCard
);
358 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
360 m_filectrl
->SetFilterIndex(filterindex
);
363 void wxGenericFileDialog::OnOk( wxCommandEvent
&WXUNUSED(event
) )
365 wxArrayString selectedFiles
;
366 m_filectrl
->GetFilenames(selectedFiles
);
368 if (selectedFiles
.Count() == 0)
371 if (selectedFiles
.Count() == 1)
373 SetPath( selectedFiles
[0] );
379 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
381 m_filectrl
->ChangeToListMode();
382 ms_lastViewStyle
= wxLC_LIST
;
383 m_filectrl
->GetFileList()->SetFocus();
386 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
388 m_filectrl
->ChangeToReportMode();
389 ms_lastViewStyle
= wxLC_REPORT
;
390 m_filectrl
->GetFileList()->SetFocus();
393 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
395 m_filectrl
->GoToParentDir();
396 m_filectrl
->GetFileList()->SetFocus();
399 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
401 m_filectrl
->GoToHomeDir();
402 m_filectrl
->SetFocus();
405 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
407 m_filectrl
->GetFileList()->MakeDir();
410 void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent
&WXUNUSED(event
) )
412 wxCommandEvent dummy
;
416 void wxGenericFileDialog::SetPath( const wxString
& path
)
418 // not only set the full path but also update filename and dir
421 m_filectrl
->SetPath(path
);
424 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
426 m_filectrl
->GetPaths(paths
);
429 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
431 m_filectrl
->GetFilenames(files
);
434 void wxGenericFileDialog::OnUpdateButtonsUI(wxUpdateUIEvent
& event
)
436 event
.Enable( !IsTopMostDir(m_filectrl
->GetDirectory()) );
439 #ifdef wxHAS_GENERIC_FILEDIALOG
441 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
443 #endif // wxHAS_GENERIC_FILEDIALOG
445 #endif // wxUSE_FILEDLG