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"
52 #include "wx/testing.h"
55 #include "wx/tooltip.h"
58 #include "wx/config.h"
62 #include <sys/types.h>
75 #include "wx/msw/mslu.h"
86 #if defined(__UNIX__) || defined(__DOS__)
90 #if defined(__WXWINCE__)
91 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
92 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
93 #define IsTopMostDir(dir) (dir.empty())
95 #define IsTopMostDir(dir) (dir == wxT("/"))
98 //-----------------------------------------------------------------------------
99 // wxGenericFileDialog
100 //-----------------------------------------------------------------------------
102 #define ID_LIST_MODE (wxID_FILEDLGG )
103 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
104 #define ID_UP_DIR (wxID_FILEDLGG + 2)
105 #define ID_HOME_DIR (wxID_FILEDLGG + 3)
106 #define ID_NEW_DIR (wxID_FILEDLGG + 4)
107 #define ID_FILE_CTRL (wxID_FILEDLGG + 5)
109 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
111 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
112 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
113 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
114 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
115 EVT_BUTTON(ID_HOME_DIR
, wxGenericFileDialog::OnHome
)
116 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
117 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnOk
)
118 EVT_FILECTRL_FILEACTIVATED(ID_FILE_CTRL
, wxGenericFileDialog::OnFileActivated
)
120 EVT_UPDATE_UI(ID_UP_DIR
, wxGenericFileDialog::OnUpdateButtonsUI
)
121 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
122 EVT_UPDATE_UI(ID_NEW_DIR
, wxGenericFileDialog::OnUpdateButtonsUI
)
123 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
126 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
127 bool wxGenericFileDialog::ms_lastShowHidden
= false;
129 void wxGenericFileDialog::Init()
131 m_bypassGenericImpl
= false;
134 m_upDirButton
= NULL
;
135 m_newDirButton
= NULL
;
138 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
139 const wxString
& message
,
140 const wxString
& defaultDir
,
141 const wxString
& defaultFile
,
142 const wxString
& wildCard
,
146 const wxString
& name
,
147 bool bypassGenericImpl
) : wxFileDialogBase()
150 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, sz
, name
, bypassGenericImpl
);
153 bool wxGenericFileDialog::Create( wxWindow
*parent
,
154 const wxString
& message
,
155 const wxString
& defaultDir
,
156 const wxString
& defaultFile
,
157 const wxString
& wildCard
,
161 const wxString
& name
,
162 bool bypassGenericImpl
)
164 m_bypassGenericImpl
= bypassGenericImpl
;
166 parent
= GetParentForModalDialog(parent
, style
);
168 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
169 wildCard
, style
, pos
, sz
, name
))
174 if (m_bypassGenericImpl
)
177 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, sz
,
178 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
| style
, name
185 if (wxConfig::Get(false))
187 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
189 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
194 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
198 m_dir
= wxFILE_SEP_PATH
;
201 const size_t len
= m_dir
.length();
202 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
203 m_dir
.Remove( len
-1, 1 );
205 m_filterExtension
= wxEmptyString
;
209 const bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
211 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
213 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
214 AddBitmapButton( ID_LIST_MODE
, wxART_LIST_VIEW
,
215 _("View files as a list view"), buttonsizer
);
216 AddBitmapButton( ID_REPORT_MODE
, wxART_REPORT_VIEW
,
217 _("View files as a detailed view"), buttonsizer
);
218 buttonsizer
->Add( 30, 5, 1 );
219 m_upDirButton
= AddBitmapButton( ID_UP_DIR
, wxART_GO_DIR_UP
,
220 _("Go to parent directory"), buttonsizer
);
222 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
223 AddBitmapButton( ID_HOME_DIR
, wxART_GO_HOME
,
224 _("Go to home directory"), buttonsizer
);
225 buttonsizer
->Add( 20, 20 );
228 m_newDirButton
= AddBitmapButton( ID_NEW_DIR
, wxART_NEW_DIR
,
229 _("Create new directory"), buttonsizer
);
232 mainsizer
->Add( buttonsizer
, wxSizerFlags().Expand() );
234 mainsizer
->Add( buttonsizer
, wxSizerFlags().Expand()
235 .Border( wxLEFT
| wxRIGHT
| wxTOP
) );
238 if ( HasFdFlag(wxFD_MULTIPLE
) )
239 style2
|= wxFC_MULTIPLE
;
241 m_filectrl
= new wxGenericFileCtrl( this, ID_FILE_CTRL
,
245 wxDefaultPosition
, wxSize(540,200)
248 m_filectrl
->ShowHidden( ms_lastShowHidden
);
250 if (ms_lastViewStyle
== wxLC_LIST
)
252 m_filectrl
->ChangeToListMode();
254 else if (ms_lastViewStyle
== wxLC_REPORT
)
256 m_filectrl
->ChangeToReportMode();
259 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().HorzBorder());
261 wxSizer
*bsizer
= CreateButtonSizer(wxOK
| wxCANCEL
);
265 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().Border());
267 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().DoubleBorder());
270 SetSizer( mainsizer
);
274 mainsizer
->SetSizeHints( this );
282 wxGenericFileDialog::~wxGenericFileDialog()
284 if (!m_bypassGenericImpl
)
287 if (wxConfig::Get(false))
289 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
291 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
298 wxBitmapButton
* wxGenericFileDialog::AddBitmapButton( wxWindowID winId
,
299 const wxArtID
& artId
,
303 wxBitmapButton
*but
= new wxBitmapButton(this, winId
,
304 wxArtProvider::GetBitmap(artId
, wxART_BUTTON
));
305 but
->SetToolTip(tip
);
306 sizer
->Add(but
, wxSizerFlags().Border());
310 int wxGenericFileDialog::ShowModal()
312 WX_TESTING_SHOW_MODAL_HOOK();
314 if (CreateExtraControl())
316 wxSizer
*sizer
= GetSizer();
317 sizer
->Insert(2 /* after m_filectrl */, m_extraControl
,
318 wxSizerFlags().Expand().HorzBorder());
322 m_filectrl
->SetDirectory(m_dir
);
324 return wxDialog::ShowModal();
327 bool wxGenericFileDialog::Show( bool show
)
329 // Called by ShowModal, so don't repeate the update
333 m_filectrl
->SetDirectory(m_dir
);
337 return wxDialog::Show( show
);
340 void wxGenericFileDialog::OnOk( wxCommandEvent
&WXUNUSED(event
) )
342 wxArrayString selectedFiles
;
343 m_filectrl
->GetFilenames(selectedFiles
);
345 if (selectedFiles
.Count() == 0)
348 if (selectedFiles
.Count() == 1)
350 SetPath( selectedFiles
[0] );
356 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
358 m_filectrl
->ChangeToListMode();
359 ms_lastViewStyle
= wxLC_LIST
;
360 m_filectrl
->GetFileList()->SetFocus();
363 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
365 m_filectrl
->ChangeToReportMode();
366 ms_lastViewStyle
= wxLC_REPORT
;
367 m_filectrl
->GetFileList()->SetFocus();
370 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
372 m_filectrl
->GoToParentDir();
373 m_filectrl
->GetFileList()->SetFocus();
376 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
378 m_filectrl
->GoToHomeDir();
379 m_filectrl
->SetFocus();
382 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
384 m_filectrl
->GetFileList()->MakeDir();
387 void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent
&WXUNUSED(event
) )
389 wxCommandEvent dummy
;
393 void wxGenericFileDialog::OnUpdateButtonsUI(wxUpdateUIEvent
& event
)
395 // surprisingly, we can be called before m_filectrl is set in Create() as
396 // wxFileCtrl ctor itself can generate idle events, so we need this test
398 event
.Enable( !IsTopMostDir(m_filectrl
->GetShownDirectory()) );
401 #ifdef wxHAS_GENERIC_FILEDIALOG
403 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
405 #endif // wxHAS_GENERIC_FILEDIALOG
407 #endif // wxUSE_FILEDLG