1 //////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/filedlgg.cpp
3 // Purpose: wxGenericFileDialog
4 // Author: Robert Roebling
7 // Copyright: (c) Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
20 // NOTE : it probably also supports MAC, untested
21 #if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__)
22 #error wxGenericFileDialog currently only supports Unix, win32 and DOS
27 #include "wx/msw/wrapwin.h"
31 #include "wx/settings.h"
33 #include "wx/msgdlg.h"
34 #include "wx/bmpbuttn.h"
35 #include "wx/checkbox.h"
36 #include "wx/choice.h"
37 #include "wx/stattext.h"
38 #include "wx/textctrl.h"
40 #include "wx/filedlg.h" // wxFD_OPEN, wxFD_SAVE...
43 #include "wx/longlong.h"
44 #include "wx/config.h"
45 #include "wx/imaglist.h"
46 #include "wx/artprov.h"
47 #include "wx/filefn.h"
48 #include "wx/filectrl.h"
49 #include "wx/generic/filedlgg.h"
51 #include "wx/modalhook.h"
54 #include "wx/tooltip.h"
57 #include "wx/config.h"
61 #include <sys/types.h>
74 #include "wx/msw/mslu.h"
85 #if defined(__UNIX__) || defined(__DOS__)
89 #if defined(__WXWINCE__)
90 #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
91 #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
92 #define IsTopMostDir(dir) (dir.empty())
94 #define IsTopMostDir(dir) (dir == wxT("/"))
97 //-----------------------------------------------------------------------------
98 // wxGenericFileDialog
99 //-----------------------------------------------------------------------------
101 #define ID_LIST_MODE (wxID_FILEDLGG )
102 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
103 #define ID_UP_DIR (wxID_FILEDLGG + 2)
104 #define ID_HOME_DIR (wxID_FILEDLGG + 3)
105 #define ID_NEW_DIR (wxID_FILEDLGG + 4)
106 #define ID_FILE_CTRL (wxID_FILEDLGG + 5)
108 IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog
, wxFileDialogBase
)
110 BEGIN_EVENT_TABLE(wxGenericFileDialog
,wxDialog
)
111 EVT_BUTTON(ID_LIST_MODE
, wxGenericFileDialog::OnList
)
112 EVT_BUTTON(ID_REPORT_MODE
, wxGenericFileDialog::OnReport
)
113 EVT_BUTTON(ID_UP_DIR
, wxGenericFileDialog::OnUp
)
114 EVT_BUTTON(ID_HOME_DIR
, wxGenericFileDialog::OnHome
)
115 EVT_BUTTON(ID_NEW_DIR
, wxGenericFileDialog::OnNew
)
116 EVT_BUTTON(wxID_OK
, wxGenericFileDialog::OnOk
)
117 EVT_FILECTRL_FILEACTIVATED(ID_FILE_CTRL
, wxGenericFileDialog::OnFileActivated
)
119 EVT_UPDATE_UI(ID_UP_DIR
, wxGenericFileDialog::OnUpdateButtonsUI
)
120 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
121 EVT_UPDATE_UI(ID_NEW_DIR
, wxGenericFileDialog::OnUpdateButtonsUI
)
122 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
125 long wxGenericFileDialog::ms_lastViewStyle
= wxLC_LIST
;
126 bool wxGenericFileDialog::ms_lastShowHidden
= false;
128 void wxGenericFileDialog::Init()
130 m_bypassGenericImpl
= false;
133 m_upDirButton
= NULL
;
134 m_newDirButton
= NULL
;
137 wxGenericFileDialog::wxGenericFileDialog(wxWindow
*parent
,
138 const wxString
& message
,
139 const wxString
& defaultDir
,
140 const wxString
& defaultFile
,
141 const wxString
& wildCard
,
145 const wxString
& name
,
146 bool bypassGenericImpl
) : wxFileDialogBase()
149 Create( parent
, message
, defaultDir
, defaultFile
, wildCard
, style
, pos
, sz
, name
, bypassGenericImpl
);
152 bool wxGenericFileDialog::Create( wxWindow
*parent
,
153 const wxString
& message
,
154 const wxString
& defaultDir
,
155 const wxString
& defaultFile
,
156 const wxString
& wildCard
,
160 const wxString
& name
,
161 bool bypassGenericImpl
)
163 m_bypassGenericImpl
= bypassGenericImpl
;
165 parent
= GetParentForModalDialog(parent
, style
);
167 if (!wxFileDialogBase::Create(parent
, message
, defaultDir
, defaultFile
,
168 wildCard
, style
, pos
, sz
, name
))
173 if (m_bypassGenericImpl
)
176 if (!wxDialog::Create( parent
, wxID_ANY
, message
, pos
, sz
,
177 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
| style
, name
184 if (wxConfig::Get(false))
186 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
188 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
193 if ((m_dir
.empty()) || (m_dir
== wxT(".")))
197 m_dir
= wxFILE_SEP_PATH
;
200 const size_t len
= m_dir
.length();
201 if ((len
> 1) && (wxEndsWithPathSeparator(m_dir
)))
202 m_dir
.Remove( len
-1, 1 );
204 m_filterExtension
= wxEmptyString
;
208 const bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
210 wxBoxSizer
*mainsizer
= new wxBoxSizer( wxVERTICAL
);
212 wxBoxSizer
*buttonsizer
= new wxBoxSizer( wxHORIZONTAL
);
213 AddBitmapButton( ID_LIST_MODE
, wxART_LIST_VIEW
,
214 _("View files as a list view"), buttonsizer
);
215 AddBitmapButton( ID_REPORT_MODE
, wxART_REPORT_VIEW
,
216 _("View files as a detailed view"), buttonsizer
);
217 buttonsizer
->Add( 30, 5, 1 );
218 m_upDirButton
= AddBitmapButton( ID_UP_DIR
, wxART_GO_DIR_UP
,
219 _("Go to parent directory"), buttonsizer
);
221 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
222 AddBitmapButton( ID_HOME_DIR
, wxART_GO_HOME
,
223 _("Go to home directory"), buttonsizer
);
224 buttonsizer
->Add( 20, 20 );
227 m_newDirButton
= AddBitmapButton( ID_NEW_DIR
, wxART_NEW_DIR
,
228 _("Create new directory"), buttonsizer
);
231 mainsizer
->Add( buttonsizer
, wxSizerFlags().Expand() );
233 mainsizer
->Add( buttonsizer
, wxSizerFlags().Expand()
234 .Border( wxLEFT
| wxRIGHT
| wxTOP
) );
237 if ( HasFdFlag(wxFD_MULTIPLE
) )
238 style2
|= wxFC_MULTIPLE
;
240 m_filectrl
= new wxGenericFileCtrl( this, ID_FILE_CTRL
,
244 wxDefaultPosition
, wxSize(540,200)
247 m_filectrl
->ShowHidden( ms_lastShowHidden
);
249 if (ms_lastViewStyle
== wxLC_LIST
)
251 m_filectrl
->ChangeToListMode();
253 else if (ms_lastViewStyle
== wxLC_REPORT
)
255 m_filectrl
->ChangeToReportMode();
258 mainsizer
->Add(m_filectrl
, wxSizerFlags(1).Expand().HorzBorder());
260 wxSizer
*bsizer
= CreateButtonSizer(wxOK
| wxCANCEL
);
264 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().Border());
266 mainsizer
->Add(bsizer
, wxSizerFlags().Expand().DoubleBorder());
269 SetSizer( mainsizer
);
273 mainsizer
->SetSizeHints( this );
281 wxGenericFileDialog::~wxGenericFileDialog()
283 if (!m_bypassGenericImpl
)
286 if (wxConfig::Get(false))
288 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
290 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
297 wxBitmapButton
* wxGenericFileDialog::AddBitmapButton( wxWindowID winId
,
298 const wxArtID
& artId
,
302 wxBitmapButton
*but
= new wxBitmapButton(this, winId
,
303 wxArtProvider::GetBitmap(artId
, wxART_BUTTON
));
304 but
->SetToolTip(tip
);
305 sizer
->Add(but
, wxSizerFlags().Border());
309 int wxGenericFileDialog::ShowModal()
311 WX_HOOK_MODAL_DIALOG();
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