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__) && !defined(__PALMOS__)
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"
57 #include "wx/config.h"
62 #include <sys/types.h>
75 #include "wx/msw/mslu.h"
86 #if defined(__UNIX__) || defined(__DOS__)
89 #endif // ! __WXPALMOS5__
91 #if defined(__WXWINCE__)
92 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
93 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
94 #define IsTopMostDir(dir) (dir.empty())
96 #define IsTopMostDir(dir) (dir == wxT("/"))
99 //-----------------------------------------------------------------------------
100 // wxGenericFileDialog
101 //-----------------------------------------------------------------------------
103 #define ID_LIST_MODE (wxID_FILEDLGG )
104 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
105 #define ID_UP_DIR (wxID_FILEDLGG + 2)
106 #define ID_PARENT_DIR (wxID_FILEDLGG + 3)
107 #define ID_NEW_DIR (wxID_FILEDLGG + 4)
108 #define ID_FILE_CTRL (wxID_FILEDLGG + 5)
110 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
112 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
113 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog
::OnList
)
114 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog
::OnReport
)
115 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog
::OnUp
)
116 EVT_BUTTON(ID_PARENT_DIR
, wxGenericFileDialog
::OnHome
)
117 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog
::OnNew
)
118 EVT_BUTTON(wxID_OK
, wxGenericFileDialog
::OnOk
)
119 EVT_FILECTRL_FILEACTIVATED(ID_FILE_CTRL
, wxGenericFileDialog
::OnFileActivated
)
121 EVT_UPDATE_UI(ID_UP_DIR
, wxGenericFileDialog
::OnUpdateButtonsUI
)
122 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
123 EVT_UPDATE_UI(ID_NEW_DIR
, wxGenericFileDialog
::OnUpdateButtonsUI
)
124 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
127 long wxGenericFileDialog
::ms_lastViewStyle
= wxLC_LIST
;
128 bool wxGenericFileDialog
::ms_lastShowHidden
= false;
130 void wxGenericFileDialog
::Init()
132 m_bypassGenericImpl
= false;
135 m_upDirButton
= NULL
;
136 m_newDirButton
= NULL
;
139 wxGenericFileDialog
::wxGenericFileDialog(wxWindow
*parent
,
140 const wxString
& message
,
141 const wxString
& defaultDir
,
142 const wxString
& defaultFile
,
143 const wxString
& wildCard
,
147 const wxString
& name
,
148 bool bypassGenericImpl
) : wxFileDialogBase()
151 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, sz
, name
, bypassGenericImpl
);
154 bool wxGenericFileDialog
::Create( wxWindow
*parent
,
155 const wxString
& message
,
156 const wxString
& defaultDir
,
157 const wxString
& defaultFile
,
158 const wxString
& wildCard
,
162 const wxString
& name
,
163 bool bypassGenericImpl
)
165 m_bypassGenericImpl
= bypassGenericImpl
;
167 parent
= GetParentForModalDialog(parent
);
169 if (!wxFileDialogBase
::Create(parent
, message
, defaultDir
, defaultFile
,
170 wildCard
, style
, pos
, sz
, name
))
175 if (m_bypassGenericImpl
)
178 if (!wxDialog
::Create( parent
, wxID_ANY
, message
, pos
, sz
,
179 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
| style
, name
186 if (wxConfig
::Get(false))
188 wxConfig
::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
190 wxConfig
::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
195 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
199 m_dir
= wxFILE_SEP_PATH
;
202 const size_t len
= m_dir
.length();
203 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
204 m_dir
.Remove( len
-1, 1 );
206 m_filterExtension
= wxEmptyString
;
210 const bool is_pda
= (wxSystemSettings
::GetScreenType() <= wxSYS_SCREEN_PDA
);
212 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
214 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
218 but
= new wxBitmapButton(this, ID_LIST_MODE
,
219 wxArtProvider
::GetBitmap(wxART_LIST_VIEW
, wxART_BUTTON
));
221 but
->SetToolTip( _("View files as a list view") );
223 buttonsizer
->Add( but
, 0, wxALL
, 5 );
225 but
= new wxBitmapButton(this, ID_REPORT_MODE
,
226 wxArtProvider
::GetBitmap(wxART_REPORT_VIEW
, wxART_BUTTON
));
228 but
->SetToolTip( _("View files as a detailed view") );
230 buttonsizer
->Add( but
, 0, wxALL
, 5 );
232 buttonsizer
->Add( 30, 5, 1 );
234 m_upDirButton
= new wxBitmapButton(this, ID_UP_DIR
,
235 wxArtProvider
::GetBitmap(wxART_GO_DIR_UP
, wxART_BUTTON
));
237 m_upDirButton
->SetToolTip( _("Go to parent directory") );
239 buttonsizer
->Add( m_upDirButton
, 0, wxALL
, 5 );
241 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
242 but
= new wxBitmapButton(this, ID_PARENT_DIR
,
243 wxArtProvider
::GetBitmap(wxART_GO_HOME
, wxART_BUTTON
));
245 but
->SetToolTip( _("Go to home directory") );
247 buttonsizer
->Add( but
, 0, wxALL
, 5);
249 buttonsizer
->Add( 20, 20 );
252 m_newDirButton
= new wxBitmapButton(this, ID_NEW_DIR
,
253 wxArtProvider
::GetBitmap(wxART_NEW_DIR
, wxART_BUTTON
));
255 m_newDirButton
->SetToolTip( _("Create new directory") );
257 buttonsizer
->Add( m_newDirButton
, 0, wxALL
, 5 );
260 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 0 );
262 mainsizer
->Add( buttonsizer
, 0, wxALL
| wxEXPAND
, 5 );
265 if ( HasFdFlag(wxFD_MULTIPLE
) )
266 style2
|= wxFC_MULTIPLE
;
268 m_filectrl
= new wxGenericFileCtrl( this, ID_FILE_CTRL
,
272 wxDefaultPosition
, wxSize(540,200)
275 m_filectrl
->ShowHidden( ms_lastShowHidden
);
277 if (ms_lastViewStyle
== wxLC_LIST
)
279 m_filectrl
->ChangeToListMode();
281 else if (ms_lastViewStyle
== wxLC_REPORT
)
283 m_filectrl
->ChangeToReportMode();
288 // PDAs have a different screen layout
289 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().HorzBorder());
291 wxSizer
*bsizer
= CreateButtonSizer(wxOK
| wxCANCEL
);
293 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().Border());
297 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().DoubleHorzBorder());
299 wxBoxSizer
*okcancelsizer
= new wxBoxSizer( wxHORIZONTAL
);
300 okcancelsizer
->Add(new wxButton(this, wxID_OK
), wxSizerFlags().DoubleBorder().Centre());
301 okcancelsizer
->Add(new wxButton(this, wxID_CANCEL
), wxSizerFlags().DoubleBorder().Centre());
302 mainsizer
->Add(okcancelsizer
, wxSizerFlags().Center());
305 SetAutoLayout( true );
306 SetSizer( mainsizer
);
310 mainsizer
->Fit( this );
311 mainsizer
->SetSizeHints( this );
319 wxGenericFileDialog
::~wxGenericFileDialog()
321 if (!m_bypassGenericImpl
)
324 if (wxConfig
::Get(false))
326 wxConfig
::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
328 wxConfig
::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
335 int wxGenericFileDialog
::ShowModal()
337 m_filectrl
->SetDirectory(m_dir
);
339 return wxDialog
::ShowModal();
342 bool wxGenericFileDialog
::Show( bool show
)
344 // Called by ShowModal, so don't repeate the update
348 m_filectrl
->SetDirectory(m_dir
);
352 return wxDialog
::Show( show
);
355 void wxGenericFileDialog
::OnOk( wxCommandEvent
&WXUNUSED(event
) )
357 wxArrayString selectedFiles
;
358 m_filectrl
->GetFilenames(selectedFiles
);
360 if (selectedFiles
.Count() == 0)
363 if (selectedFiles
.Count() == 1)
365 SetPath( selectedFiles
[0] );
371 void wxGenericFileDialog
::OnList( wxCommandEvent
&WXUNUSED(event
) )
373 m_filectrl
->ChangeToListMode();
374 ms_lastViewStyle
= wxLC_LIST
;
375 m_filectrl
->GetFileList()->SetFocus();
378 void wxGenericFileDialog
::OnReport( wxCommandEvent
&WXUNUSED(event
) )
380 m_filectrl
->ChangeToReportMode();
381 ms_lastViewStyle
= wxLC_REPORT
;
382 m_filectrl
->GetFileList()->SetFocus();
385 void wxGenericFileDialog
::OnUp( wxCommandEvent
&WXUNUSED(event
) )
387 m_filectrl
->GoToParentDir();
388 m_filectrl
->GetFileList()->SetFocus();
391 void wxGenericFileDialog
::OnHome( wxCommandEvent
&WXUNUSED(event
) )
393 m_filectrl
->GoToHomeDir();
394 m_filectrl
->SetFocus();
397 void wxGenericFileDialog
::OnNew( wxCommandEvent
&WXUNUSED(event
) )
399 m_filectrl
->GetFileList()->MakeDir();
402 void wxGenericFileDialog
::OnFileActivated( wxFileCtrlEvent
&WXUNUSED(event
) )
404 wxCommandEvent dummy
;
408 void wxGenericFileDialog
::OnUpdateButtonsUI(wxUpdateUIEvent
& event
)
410 // surprisingly, we can be called before m_filectrl is set in Create() as
411 // wxFileCtrl ctor itself can generate idle events, so we need this test
413 event
.Enable( !IsTopMostDir(m_filectrl
->GetShownDirectory()) );
416 #ifdef wxHAS_GENERIC_FILEDIALOG
418 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
420 #endif // wxHAS_GENERIC_FILEDIALOG
422 #endif // wxUSE_FILEDLG