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"
19 #if wxUSE_FILEDLG && (defined(__WXUNIVERSAL__) || defined(__WXGTK__))
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
)
117 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
118 bool wxGenericFileDialog::ms_lastShowHidden
= false;
120 void wxGenericFileDialog::Init()
122 m_bypassGenericImpl
= false;
125 m_upDirButton
= NULL
;
126 m_newDirButton
= NULL
;
129 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
130 const wxString
& message
,
131 const wxString
& defaultDir
,
132 const wxString
& defaultFile
,
133 const wxString
& wildCard
,
137 const wxString
& name
,
138 bool bypassGenericImpl
) : wxFileDialogBase()
141 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, sz
, name
, bypassGenericImpl
);
144 bool wxGenericFileDialog::Create( wxWindow
*parent
,
145 const wxString
& message
,
146 const wxString
& defaultDir
,
147 const wxString
& defaultFile
,
148 const wxString
& wildCard
,
152 const wxString
& name
,
153 bool bypassGenericImpl
)
155 m_bypassGenericImpl
= bypassGenericImpl
;
157 parent
= GetParentForModalDialog(parent
);
159 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
160 wildCard
, style
, pos
, sz
, name
))
165 if (m_bypassGenericImpl
)
168 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, sz
,
169 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
| style
, name
176 if (wxConfig::Get(false))
178 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
180 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
185 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
189 m_dir
= wxFILE_SEP_PATH
;
192 const size_t len
= m_dir
.length();
193 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
194 m_dir
.Remove( len
-1, 1 );
197 m_path
+= wxFILE_SEP_PATH
;
198 m_path
+= defaultFile
;
199 m_filterExtension
= wxEmptyString
;
203 const bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
205 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
207 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
211 but
= new wxBitmapButton(this, ID_LIST_MODE
,
212 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
214 but
->SetToolTip( _("View files as a list view") );
216 buttonsizer
->Add( but
, 0, wxALL
, 5 );
218 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
219 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
221 but
->SetToolTip( _("View files as a detailed view") );
223 buttonsizer
->Add( but
, 0, wxALL
, 5 );
225 buttonsizer
->Add( 30, 5, 1 );
227 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
228 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
230 m_upDirButton
->SetToolTip( _("Go to parent directory") );
232 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
234 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
235 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
236 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
238 but
->SetToolTip( _("Go to home directory") );
240 buttonsizer
->Add( but
, 0, wxALL
, 5);
242 buttonsizer
->Add( 20, 20 );
245 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
246 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
248 m_newDirButton
->SetToolTip( _("Create new directory") );
250 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
253 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
255 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
258 if ( HasFdFlag(wxFD_MULTIPLE
) )
259 style2
|= wxFC_MULTIPLE
;
261 m_filectrl
= new wxGenericFileCtrl( this, ID_FILE_CTRL
,
265 wxDefaultPosition
, wxSize(540,200)
268 m_filectrl
->ShowHidden( ms_lastShowHidden
);
270 if (ms_lastViewStyle
== wxLC_LIST
)
272 m_filectrl
->ChangeToListMode();
274 else if (ms_lastViewStyle
== wxLC_REPORT
)
276 m_filectrl
->ChangeToReportMode();
281 // PDAs have a different screen layout
282 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().HorzBorder());
284 wxSizer
*bsizer
= CreateButtonSizer(wxOK
| wxCANCEL
);
286 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().Border());
290 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().DoubleHorzBorder());
292 wxBoxSizer
*okcancelsizer
= new wxBoxSizer( wxHORIZONTAL
);
293 okcancelsizer
->Add(new wxButton(this, wxID_OK
), wxSizerFlags().DoubleBorder().Centre());
294 okcancelsizer
->Add(new wxButton(this, wxID_CANCEL
), wxSizerFlags().DoubleBorder().Centre());
295 mainsizer
->Add(okcancelsizer
, wxSizerFlags().Center());
298 SetAutoLayout( true );
299 SetSizer( mainsizer
);
303 mainsizer
->Fit( this );
304 mainsizer
->SetSizeHints( this );
312 wxGenericFileDialog::~wxGenericFileDialog()
314 if (!m_bypassGenericImpl
)
317 if (wxConfig::Get(false))
319 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
321 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
328 int wxGenericFileDialog::ShowModal()
330 m_filectrl
->SetDirectory(m_dir
);
333 return wxDialog::ShowModal();
336 bool wxGenericFileDialog::Show( bool show
)
338 // Called by ShowModal, so don't repeate the update
342 m_filectrl
->SetDirectory(m_dir
);
347 return wxDialog::Show( show
);
350 void wxGenericFileDialog::SetWildcard(const wxString
& wildCard
)
352 m_filectrl
->SetWildcard(wildCard
);
355 void wxGenericFileDialog::SetFilterIndex( int filterindex
)
357 m_filectrl
->SetFilterIndex(filterindex
);
360 void wxGenericFileDialog::OnOk( wxCommandEvent
&WXUNUSED(event
) )
362 wxArrayString selectedFiles
;
363 m_filectrl
->GetFilenames(selectedFiles
);
365 if (selectedFiles
.Count() == 0)
368 if (selectedFiles
.Count() == 1)
370 SetPath( selectedFiles
[0] );
376 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
378 m_filectrl
->ChangeToListMode();
379 ms_lastViewStyle
= wxLC_LIST
;
380 m_filectrl
->GetFileList()->SetFocus();
383 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
385 m_filectrl
->ChangeToReportMode();
386 ms_lastViewStyle
= wxLC_REPORT
;
387 m_filectrl
->GetFileList()->SetFocus();
390 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
392 m_filectrl
->GoToParentDir();
393 m_filectrl
->GetFileList()->SetFocus();
397 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
399 m_filectrl
->GoToHomeDir();
400 m_filectrl
->SetFocus();
404 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
406 m_filectrl
->GetFileList()->MakeDir();
409 void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent
&WXUNUSED(event
) )
411 wxCommandEvent dummy
;
415 void wxGenericFileDialog::SetPath( const wxString
& path
)
417 // not only set the full path but also update filename and dir
420 m_filectrl
->SetPath(path
);
423 void wxGenericFileDialog::GetPaths( wxArrayString
& paths
) const
425 m_filectrl
->GetPaths(paths
);
428 void wxGenericFileDialog::GetFilenames(wxArrayString
& files
) const
430 m_filectrl
->GetFilenames(files
);
433 void wxGenericFileDialog::UpdateControls()
435 const bool enable
= !IsTopMostDir(m_filectrl
->GetDirectory());
436 m_upDirButton
->Enable(enable
);
438 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
439 m_newDirButton
->Enable(enable
);
440 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
443 #ifdef wxHAS_GENERIC_FILEDIALOG
445 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
447 #endif // wxHAS_GENERIC_FILEDIALOG
449 #endif // wxUSE_FILEDLG