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 );
201 m_filterExtension
= wxEmptyString
;
205 const bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
207 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
209 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
213 but
= new wxBitmapButton(this, ID_LIST_MODE
,
214 wxArtProvider::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
216 but
->SetToolTip( _("View files as a list view") );
218 buttonsizer
->Add( but
, 0, wxALL
, 5 );
220 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
221 wxArtProvider::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
223 but
->SetToolTip( _("View files as a detailed view") );
225 buttonsizer
->Add( but
, 0, wxALL
, 5 );
227 buttonsizer
->Add( 30, 5, 1 );
229 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
230 wxArtProvider::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
232 m_upDirButton
->SetToolTip( _("Go to parent directory") );
234 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
236 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
237 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
238 wxArtProvider::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
240 but
->SetToolTip( _("Go to home directory") );
242 buttonsizer
->Add( but
, 0, wxALL
, 5);
244 buttonsizer
->Add( 20, 20 );
247 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
248 wxArtProvider::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
250 m_newDirButton
->SetToolTip( _("Create new directory") );
252 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
255 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
257 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
260 if ( HasFdFlag(wxFD_MULTIPLE
) )
261 style2
|= wxFC_MULTIPLE
;
263 m_filectrl
= new wxGenericFileCtrl( this, ID_FILE_CTRL
,
267 wxDefaultPosition
, wxSize(540,200)
270 m_filectrl
->ShowHidden( ms_lastShowHidden
);
272 if (ms_lastViewStyle
== wxLC_LIST
)
274 m_filectrl
->ChangeToListMode();
276 else if (ms_lastViewStyle
== wxLC_REPORT
)
278 m_filectrl
->ChangeToReportMode();
283 // PDAs have a different screen layout
284 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().HorzBorder());
286 wxSizer
*bsizer
= CreateButtonSizer(wxOK
| wxCANCEL
);
288 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().Border());
292 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().DoubleHorzBorder());
294 wxBoxSizer
*okcancelsizer
= new wxBoxSizer( wxHORIZONTAL
);
295 okcancelsizer
->Add(new wxButton(this, wxID_OK
), wxSizerFlags().DoubleBorder().Centre());
296 okcancelsizer
->Add(new wxButton(this, wxID_CANCEL
), wxSizerFlags().DoubleBorder().Centre());
297 mainsizer
->Add(okcancelsizer
, wxSizerFlags().Center());
300 SetAutoLayout( true );
301 SetSizer( mainsizer
);
305 mainsizer
->Fit( this );
306 mainsizer
->SetSizeHints( this );
314 wxGenericFileDialog::~wxGenericFileDialog()
316 if (!m_bypassGenericImpl
)
319 if (wxConfig::Get(false))
321 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
323 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
330 int wxGenericFileDialog::ShowModal()
332 m_filectrl
->SetDirectory(m_dir
);
334 return wxDialog::ShowModal();
337 bool wxGenericFileDialog::Show( bool show
)
339 // Called by ShowModal, so don't repeate the update
343 m_filectrl
->SetDirectory(m_dir
);
347 return wxDialog::Show( show
);
350 void wxGenericFileDialog::OnOk( wxCommandEvent
&WXUNUSED(event
) )
352 wxArrayString selectedFiles
;
353 m_filectrl
->GetFilenames(selectedFiles
);
355 if (selectedFiles
.Count() == 0)
358 if (selectedFiles
.Count() == 1)
360 SetPath( selectedFiles
[0] );
366 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
368 m_filectrl
->ChangeToListMode();
369 ms_lastViewStyle
= wxLC_LIST
;
370 m_filectrl
->GetFileList()->SetFocus();
373 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
375 m_filectrl
->ChangeToReportMode();
376 ms_lastViewStyle
= wxLC_REPORT
;
377 m_filectrl
->GetFileList()->SetFocus();
380 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
382 m_filectrl
->GoToParentDir();
383 m_filectrl
->GetFileList()->SetFocus();
386 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
388 m_filectrl
->GoToHomeDir();
389 m_filectrl
->SetFocus();
392 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
394 m_filectrl
->GetFileList()->MakeDir();
397 void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent
&WXUNUSED(event
) )
399 wxCommandEvent dummy
;
403 void wxGenericFileDialog::OnUpdateButtonsUI(wxUpdateUIEvent
& event
)
405 // surprisingly, we can be called before m_filectrl is set in Create() as
406 // wxFileCtrl ctor itself can generate idle events, so we need this test
408 event
.Enable( !IsTopMostDir(m_filectrl
->GetShownDirectory()) );
411 #ifdef wxHAS_GENERIC_FILEDIALOG
413 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
415 #endif // wxHAS_GENERIC_FILEDIALOG
417 #endif // wxUSE_FILEDLG