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"
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 if (CreateExtraControl())
313 wxSizer
*sizer
= GetSizer();
314 sizer
->Insert(2 /* after m_filectrl */, m_extraControl
,
315 wxSizerFlags().Expand().HorzBorder());
319 m_filectrl
->SetDirectory(m_dir
);
321 return wxDialog::ShowModal();
324 bool wxGenericFileDialog::Show( bool show
)
326 // Called by ShowModal, so don't repeate the update
330 m_filectrl
->SetDirectory(m_dir
);
334 return wxDialog::Show( show
);
337 void wxGenericFileDialog::OnOk( wxCommandEvent
&WXUNUSED(event
) )
339 wxArrayString selectedFiles
;
340 m_filectrl
->GetFilenames(selectedFiles
);
342 if (selectedFiles
.Count() == 0)
345 if (selectedFiles
.Count() == 1)
347 SetPath( selectedFiles
[0] );
353 void wxGenericFileDialog::OnList( wxCommandEvent
&WXUNUSED(event
) )
355 m_filectrl
->ChangeToListMode();
356 ms_lastViewStyle
= wxLC_LIST
;
357 m_filectrl
->GetFileList()->SetFocus();
360 void wxGenericFileDialog::OnReport( wxCommandEvent
&WXUNUSED(event
) )
362 m_filectrl
->ChangeToReportMode();
363 ms_lastViewStyle
= wxLC_REPORT
;
364 m_filectrl
->GetFileList()->SetFocus();
367 void wxGenericFileDialog::OnUp( wxCommandEvent
&WXUNUSED(event
) )
369 m_filectrl
->GoToParentDir();
370 m_filectrl
->GetFileList()->SetFocus();
373 void wxGenericFileDialog::OnHome( wxCommandEvent
&WXUNUSED(event
) )
375 m_filectrl
->GoToHomeDir();
376 m_filectrl
->SetFocus();
379 void wxGenericFileDialog::OnNew( wxCommandEvent
&WXUNUSED(event
) )
381 m_filectrl
->GetFileList()->MakeDir();
384 void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent
&WXUNUSED(event
) )
386 wxCommandEvent dummy
;
390 void wxGenericFileDialog::OnUpdateButtonsUI(wxUpdateUIEvent
& event
)
392 // surprisingly, we can be called before m_filectrl is set in Create() as
393 // wxFileCtrl ctor itself can generate idle events, so we need this test
395 event
.Enable( !IsTopMostDir(m_filectrl
->GetShownDirectory()) );
398 #ifdef wxHAS_GENERIC_FILEDIALOG
400 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog
, wxGenericFileDialog
)
402 #endif // wxHAS_GENERIC_FILEDIALOG
404 #endif // wxUSE_FILEDLG