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_HOME_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_HOME_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
);
215 AddBitmapButton( ID_LIST_MODE
, wxART_LIST_VIEW
,
216 _("View files as a list view"), buttonsizer
);
217 AddBitmapButton( ID_REPORT_MODE
, wxART_REPORT_VIEW
,
218 _("View files as a detailed view"), buttonsizer
);
219 buttonsizer
->Add( 30, 5, 1 );
220 m_upDirButton
= AddBitmapButton( ID_UP_DIR
, wxART_GO_DIR_UP
,
221 _("Go to parent directory"), buttonsizer
);
223 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
224 AddBitmapButton( ID_HOME_DIR
, wxART_GO_HOME
,
225 _("Go to home directory"), buttonsizer
);
226 buttonsizer
->Add( 20, 20 );
229 m_newDirButton
= AddBitmapButton( ID_NEW_DIR
, wxART_NEW_DIR
,
230 _("Create new directory"), buttonsizer
);
233 mainsizer
->Add( buttonsizer
, wxSizerFlags().Expand() );
235 mainsizer
->Add( buttonsizer
, wxSizerFlags().Expand()
236 .Border( wxLEFT
| wxRIGHT
| wxTOP
) );
239 if ( HasFdFlag(wxFD_MULTIPLE
) )
240 style2
|= wxFC_MULTIPLE
;
242 m_filectrl
= new wxGenericFileCtrl( this, ID_FILE_CTRL
,
246 wxDefaultPosition
, wxSize(540,200)
249 m_filectrl
->ShowHidden( ms_lastShowHidden
);
251 if (ms_lastViewStyle
== wxLC_LIST
)
253 m_filectrl
->ChangeToListMode();
255 else if (ms_lastViewStyle
== wxLC_REPORT
)
257 m_filectrl
->ChangeToReportMode();
260 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().HorzBorder());
262 wxSizer
*bsizer
= CreateButtonSizer(wxOK
| wxCANCEL
);
266 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().Border());
268 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().DoubleBorder());
271 SetSizer( mainsizer
);
275 mainsizer
->SetSizeHints( this );
283 wxGenericFileDialog::~wxGenericFileDialog()
285 if (!m_bypassGenericImpl
)
288 if (wxConfig::Get(false))
290 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
292 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
299 wxBitmapButton
* wxGenericFileDialog::AddBitmapButton( wxWindowID winId
,
300 const wxArtID
& artId
,
304 wxBitmapButton
*but
= new wxBitmapButton(this, winId
,
305 wxArtProvider::GetBitmap(artId
, wxART_BUTTON
));
306 but
->SetToolTip(tip
);
307 sizer
->Add(but
, wxSizerFlags().Border());
311 int wxGenericFileDialog::ShowModal()
313 if (CreateExtraControl())
315 wxSizer
*sizer
= GetSizer();
316 sizer
->Insert(2 /* after m_filectrl */, m_extraControl
,
317 wxSizerFlags().Expand().HorzBorder());
321 m_filectrl
->SetDirectory(m_dir
);
323 return wxDialog::ShowModal();
326 bool wxGenericFileDialog::Show( bool show
)
328 // Called by ShowModal, so don't repeate the update
332 m_filectrl
->SetDirectory(m_dir
);
336 return wxDialog::Show( show
);
339 void wxGenericFileDialog::OnOk( wxCommandEvent
&WXUNUSED(event
) )
341 wxArrayString selectedFiles
;
342 m_filectrl
->GetFilenames(selectedFiles
);
344 if (selectedFiles
.Count() == 0)
347 if (selectedFiles
.Count() == 1)
349 SetPath( selectedFiles
[0] );
355 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
357 m_filectrl
->ChangeToListMode();
358 ms_lastViewStyle
= wxLC_LIST
;
359 m_filectrl
->GetFileList()->SetFocus();
362 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
364 m_filectrl
->ChangeToReportMode();
365 ms_lastViewStyle
= wxLC_REPORT
;
366 m_filectrl
->GetFileList()->SetFocus();
369 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
371 m_filectrl
->GoToParentDir();
372 m_filectrl
->GetFileList()->SetFocus();
375 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
377 m_filectrl
->GoToHomeDir();
378 m_filectrl
->SetFocus();
381 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
383 m_filectrl
->GetFileList()->MakeDir();
386 void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent
&WXUNUSED(event
) )
388 wxCommandEvent dummy
;
392 void wxGenericFileDialog::OnUpdateButtonsUI(wxUpdateUIEvent
& event
)
394 // surprisingly, we can be called before m_filectrl is set in Create() as
395 // wxFileCtrl ctor itself can generate idle events, so we need this test
397 event
.Enable( !IsTopMostDir(m_filectrl
->GetShownDirectory()) );
400 #ifdef wxHAS_GENERIC_FILEDIALOG
402 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
404 #endif // wxHAS_GENERIC_FILEDIALOG
406 #endif // wxUSE_FILEDLG