]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
More doxygen topic overview cleanup.
[wxWidgets.git] / src / generic / filedlgg.cpp
CommitLineData
81169710 1//////////////////////////////////////////////////////////////////////////////
812a5849 2// Name: src/generic/filedlgg.cpp
23254b13 3// Purpose: wxGenericFileDialog
8b17ba72
RR
4// Author: Robert Roebling
5// Modified by:
6// Created: 12/12/98
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
65571936 9// Licence: wxWindows licence
8b17ba72
RR
10/////////////////////////////////////////////////////////////////////////////
11
8b17ba72
RR
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
246c5004 16 #pragma hdrstop
8b17ba72
RR
17#endif
18
1db2c81b 19#if wxUSE_FILEDLG
1e6feb95 20
06cc1fb9 21// NOTE : it probably also supports MAC, untested
a85ad1db 22#if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__) && !defined(__PALMOS__)
23254b13 23#error wxGenericFileDialog currently only supports Unix, win32 and DOS
8b17ba72
RR
24#endif
25
88a7a4e1 26#ifndef WX_PRECOMP
57bd4c60
WS
27 #ifdef __WXMSW__
28 #include "wx/msw/wrapwin.h"
29 #endif
88a7a4e1
WS
30 #include "wx/hash.h"
31 #include "wx/intl.h"
9eddec69 32 #include "wx/settings.h"
e7c80f9e 33 #include "wx/log.h"
246c5004 34 #include "wx/msgdlg.h"
910b0053 35 #include "wx/bmpbuttn.h"
ab1ce969 36 #include "wx/checkbox.h"
b36e08d0 37 #include "wx/choice.h"
ccdc11bb 38 #include "wx/stattext.h"
fec9cc08 39 #include "wx/textctrl.h"
ed2fbeb8 40 #include "wx/sizer.h"
949c9f74 41 #include "wx/filedlg.h" // wxFD_OPEN, wxFD_SAVE...
88a7a4e1
WS
42#endif
43
ec524671 44#include "wx/longlong.h"
eaf40b23 45#include "wx/config.h"
48fe8374 46#include "wx/imaglist.h"
60d2cc25 47#include "wx/artprov.h"
619111b9 48#include "wx/filefn.h"
0cf3e587 49#include "wx/filectrl.h"
94c47543 50#include "wx/generic/filedlgg.h"
0cf3e587 51#include "wx/debug.h"
8b17ba72 52
e6daf794
RR
53#if wxUSE_TOOLTIPS
54 #include "wx/tooltip.h"
55#endif
a85ad1db
VZ
56#if wxUSE_CONFIG
57 #include "wx/config.h"
58#endif
e6daf794 59
a85ad1db 60#ifndef __WXPALMOS5__
619111b9 61#ifndef __WXWINCE__
e7c80f9e
WS
62 #include <sys/types.h>
63 #include <sys/stat.h>
619111b9 64#endif
4894ae18
VS
65
66#ifdef __UNIX__
67 #include <dirent.h>
68 #include <pwd.h>
69 #ifndef __VMS
70 # include <grp.h>
71 #endif
72#endif
73
6a8c7c70 74#ifdef __WINDOWS__
6a8c7c70
VS
75 #include "wx/msw/mslu.h"
76#endif
77
4894ae18
VS
78#ifdef __WATCOMC__
79 #include <direct.h>
27df579a 80#endif
4894ae18 81
619111b9 82#ifndef __WXWINCE__
7a82dabc 83#include <time.h>
619111b9
JS
84#endif
85
099d4217 86#if defined(__UNIX__) || defined(__DOS__)
ac2def68 87#include <unistd.h>
099d4217 88#endif
a85ad1db 89#endif // ! __WXPALMOS5__
8b17ba72 90
619111b9
JS
91#if defined(__WXWINCE__)
92#define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
93#elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
abc2a4cc 94#define IsTopMostDir(dir) (dir.empty())
70a7eb07
MW
95#else
96#define IsTopMostDir(dir) (dir == wxT("/"))
5e673a6a
VS
97#endif
98
8b17ba72 99//-----------------------------------------------------------------------------
23254b13 100// wxGenericFileDialog
8b17ba72
RR
101//-----------------------------------------------------------------------------
102
5e673a6a
VS
103#define ID_LIST_MODE (wxID_FILEDLGG )
104#define ID_REPORT_MODE (wxID_FILEDLGG + 1)
0cf3e587
VZ
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)
8b17ba72 109
f74172ab 110IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog, wxFileDialogBase)
23254b13
JS
111
112BEGIN_EVENT_TABLE(wxGenericFileDialog,wxDialog)
0cf3e587
VZ
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)
e5dd66e9
VZ
120
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__)
8b17ba72
RR
125END_EVENT_TABLE()
126
23254b13 127long wxGenericFileDialog::ms_lastViewStyle = wxLC_LIST;
ca65c044 128bool wxGenericFileDialog::ms_lastShowHidden = false;
655cf310 129
fe6cf128
VZ
130void wxGenericFileDialog::Init()
131{
132 m_bypassGenericImpl = false;
133
0cf3e587 134 m_filectrl = NULL;
fe6cf128
VZ
135 m_upDirButton = NULL;
136 m_newDirButton = NULL;
137}
138
23254b13 139wxGenericFileDialog::wxGenericFileDialog(wxWindow *parent,
9cedab37
VZ
140 const wxString& message,
141 const wxString& defaultDir,
142 const wxString& defaultFile,
143 const wxString& wildCard,
fe6cf128 144 long style,
4e1901b7 145 const wxPoint& pos,
ff3e84ff
VZ
146 const wxSize& sz,
147 const wxString& name,
fe6cf128 148 bool bypassGenericImpl ) : wxFileDialogBase()
8b17ba72 149{
fe6cf128 150 Init();
ff3e84ff 151 Create( parent, message, defaultDir, defaultFile, wildCard, style, pos, sz, name, bypassGenericImpl );
4e1901b7
RR
152}
153
154bool wxGenericFileDialog::Create( wxWindow *parent,
abc2a4cc 155 const wxString& message,
fe6cf128 156 const wxString& defaultDir,
abc2a4cc
WS
157 const wxString& defaultFile,
158 const wxString& wildCard,
fe6cf128
VZ
159 long style,
160 const wxPoint& pos,
ff3e84ff
VZ
161 const wxSize& sz,
162 const wxString& name,
fe6cf128 163 bool bypassGenericImpl )
4e1901b7 164{
fe6cf128
VZ
165 m_bypassGenericImpl = bypassGenericImpl;
166
2229243b
VZ
167 parent = GetParentForModalDialog(parent);
168
7e4fb3b8 169 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFile,
ff3e84ff 170 wildCard, style, pos, sz, name))
fe6cf128
VZ
171 {
172 return false;
173 }
174
175 if (m_bypassGenericImpl)
176 return true;
177
ff3e84ff 178 if (!wxDialog::Create( parent, wxID_ANY, message, pos, sz,
6a305cfb 179 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | style, name
c81c9392 180 ))
4e1901b7
RR
181 {
182 return false;
183 }
f74172ab 184
1a51ca33 185#if wxUSE_CONFIG
ca65c044 186 if (wxConfig::Get(false))
eaf40b23 187 {
e7d9c398 188 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
9cedab37 189 &ms_lastViewStyle);
e7d9c398 190 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
9cedab37 191 &ms_lastShowHidden);
eaf40b23 192 }
1a51ca33 193#endif
eaf40b23 194
353f41cb 195 if ((m_dir.empty()) || (m_dir == wxT(".")))
ac2def68 196 {
75ec8bd4 197 m_dir = wxGetCwd();
619111b9
JS
198 if (m_dir.empty())
199 m_dir = wxFILE_SEP_PATH;
ac2def68 200 }
7a82dabc 201
0cf3e587 202 const size_t len = m_dir.length();
083f7497 203 if ((len > 1) && (wxEndsWithPathSeparator(m_dir)))
353f41cb
RR
204 m_dir.Remove( len-1, 1 );
205
655cf310 206 m_filterExtension = wxEmptyString;
c8c0e54c 207
cae5359f 208 // layout
4f5c180e 209
0cf3e587 210 const bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
c8c0e54c 211
8b17ba72 212 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 213
8b17ba72 214 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 215
e6daf794 216 wxBitmapButton *but;
c8c0e54c 217
7a82dabc 218 but = new wxBitmapButton(this, ID_LIST_MODE,
36668b35 219 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_BUTTON));
e6daf794
RR
220#if wxUSE_TOOLTIPS
221 but->SetToolTip( _("View files as a list view") );
222#endif
223 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 224
60d2cc25 225 but = new wxBitmapButton(this, ID_REPORT_MODE,
36668b35 226 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_BUTTON));
e6daf794
RR
227#if wxUSE_TOOLTIPS
228 but->SetToolTip( _("View files as a detailed view") );
229#endif
230 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 231
0b855868 232 buttonsizer->Add( 30, 5, 1 );
e6daf794 233
06cc1fb9 234 m_upDirButton = new wxBitmapButton(this, ID_UP_DIR,
36668b35 235 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_BUTTON));
e6daf794 236#if wxUSE_TOOLTIPS
06cc1fb9 237 m_upDirButton->SetToolTip( _("Go to parent directory") );
e6daf794 238#endif
06cc1fb9 239 buttonsizer->Add( m_upDirButton, 0, wxALL, 5 );
c8c0e54c 240
37fd1c97 241#ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
60d2cc25 242 but = new wxBitmapButton(this, ID_PARENT_DIR,
36668b35 243 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON));
e6daf794
RR
244#if wxUSE_TOOLTIPS
245 but->SetToolTip( _("Go to home directory") );
246#endif
247 buttonsizer->Add( but, 0, wxALL, 5);
c8c0e54c 248
e6daf794 249 buttonsizer->Add( 20, 20 );
75ec8bd4 250#endif //!__DOS__
c8c0e54c 251
06cc1fb9 252 m_newDirButton = new wxBitmapButton(this, ID_NEW_DIR,
36668b35 253 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON));
e6daf794 254#if wxUSE_TOOLTIPS
06cc1fb9 255 m_newDirButton->SetToolTip( _("Create new directory") );
e6daf794 256#endif
06cc1fb9 257 buttonsizer->Add( m_newDirButton, 0, wxALL, 5 );
c8c0e54c 258
2b5f62a0 259 if (is_pda)
c15521c6
RR
260 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
261 else
262 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
c8c0e54c 263
0cf3e587
VZ
264 long style2 = 0;
265 if ( HasFdFlag(wxFD_MULTIPLE) )
266 style2 |= wxFC_MULTIPLE;
9c884972 267
0cf3e587
VZ
268 m_filectrl = new wxGenericFileCtrl( this, ID_FILE_CTRL,
269 m_dir, defaultFile,
270 wildCard,
271 style2,
272 wxDefaultPosition, wxSize(540,200)
273 );
9cedab37 274
0cf3e587 275 m_filectrl->ShowHidden( ms_lastShowHidden );
619111b9 276
0cf3e587
VZ
277 if (ms_lastViewStyle == wxLC_LIST)
278 {
279 m_filectrl->ChangeToListMode();
280 }
281 else if (ms_lastViewStyle == wxLC_REPORT)
282 {
283 m_filectrl->ChangeToReportMode();
284 }
bd9f3519 285
2b5f62a0 286 if (is_pda)
c15521c6 287 {
41fecb44 288 // PDAs have a different screen layout
0cf3e587 289 mainsizer->Add(m_filectrl, wxSizerFlags(1).Expand().HorzBorder());
41fecb44 290
bd9f3519
VZ
291 wxSizer *bsizer = CreateButtonSizer(wxOK | wxCANCEL);
292 if ( bsizer )
293 mainsizer->Add(bsizer, wxSizerFlags().Expand().Border());
c15521c6 294 }
bd9f3519 295 else // !is_pda
c15521c6 296 {
0cf3e587 297 mainsizer->Add(m_filectrl, wxSizerFlags(1).Expand().DoubleHorzBorder());
bd9f3519 298
0cf3e587
VZ
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());
303 }
8b17ba72 304
ca65c044 305 SetAutoLayout( true );
8b17ba72 306 SetSizer( mainsizer );
c8c0e54c 307
619111b9
JS
308 if (!is_pda)
309 {
310 mainsizer->Fit( this );
311 mainsizer->SetSizeHints( this );
ed58dbea 312
619111b9
JS
313 Centre( wxBOTH );
314 }
c81c9392 315
4e1901b7 316 return true;
8b17ba72
RR
317}
318
23254b13 319wxGenericFileDialog::~wxGenericFileDialog()
cae5359f 320{
4e1901b7 321 if (!m_bypassGenericImpl)
eaf40b23 322 {
1a51ca33 323#if wxUSE_CONFIG
4e1901b7
RR
324 if (wxConfig::Get(false))
325 {
326 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
327 ms_lastViewStyle);
328 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
329 ms_lastShowHidden);
330 }
1a51ca33 331#endif
c5ef41d3 332 }
cae5359f
RR
333}
334
23254b13 335int wxGenericFileDialog::ShowModal()
9cedab37 336{
0cf3e587 337 m_filectrl->SetDirectory(m_dir);
619111b9 338
9cedab37
VZ
339 return wxDialog::ShowModal();
340}
341
16dce3b3
RR
342bool wxGenericFileDialog::Show( bool show )
343{
619111b9
JS
344 // Called by ShowModal, so don't repeate the update
345#ifndef __WIN32__
16dce3b3
RR
346 if (show)
347 {
0cf3e587 348 m_filectrl->SetDirectory(m_dir);
16dce3b3 349 }
619111b9 350#endif
16dce3b3
RR
351
352 return wxDialog::Show( show );
353}
354
0cf3e587 355void wxGenericFileDialog::OnOk( wxCommandEvent &WXUNUSED(event) )
df413171 356{
0cf3e587
VZ
357 wxArrayString selectedFiles;
358 m_filectrl->GetFilenames(selectedFiles);
0b855868 359
0cf3e587 360 if (selectedFiles.Count() == 0)
c8c0e54c 361 return;
c8c0e54c 362
0cf3e587 363 if (selectedFiles.Count() == 1)
ed58dbea 364 {
0cf3e587 365 SetPath( selectedFiles[0] );
3f6638b8
VZ
366 }
367
b28a6678 368 EndModal(wxID_OK);
e1811a01
RR
369}
370
23254b13 371void wxGenericFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
8b17ba72 372{
0cf3e587 373 m_filectrl->ChangeToListMode();
9cedab37 374 ms_lastViewStyle = wxLC_LIST;
0cf3e587 375 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
376}
377
23254b13 378void wxGenericFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
8b17ba72 379{
0cf3e587 380 m_filectrl->ChangeToReportMode();
9cedab37 381 ms_lastViewStyle = wxLC_REPORT;
0cf3e587 382 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
383}
384
23254b13 385void wxGenericFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
8b17ba72 386{
0cf3e587
VZ
387 m_filectrl->GoToParentDir();
388 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
389}
390
23254b13 391void wxGenericFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
8b17ba72 392{
0cf3e587
VZ
393 m_filectrl->GoToHomeDir();
394 m_filectrl->SetFocus();
0b855868
RR
395}
396
23254b13 397void wxGenericFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
0b855868 398{
0cf3e587
VZ
399 m_filectrl->GetFileList()->MakeDir();
400}
619111b9 401
0cf3e587
VZ
402void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent &WXUNUSED(event) )
403{
404 wxCommandEvent dummy;
405 OnOk( dummy );
8b17ba72
RR
406}
407
e5dd66e9 408void wxGenericFileDialog::OnUpdateButtonsUI(wxUpdateUIEvent& event)
06cc1fb9 409{
91a865a4
VZ
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
412 if ( m_filectrl )
398eebb1 413 event.Enable( !IsTopMostDir(m_filectrl->GetShownDirectory()) );
06cc1fb9
JS
414}
415
b2f0b934 416#ifdef wxHAS_GENERIC_FILEDIALOG
655cf310 417
412e0d47 418IMPLEMENT_DYNAMIC_CLASS(wxFileDialog, wxGenericFileDialog)
655cf310 419
b2f0b934 420#endif // wxHAS_GENERIC_FILEDIALOG
23254b13 421
4e1901b7 422#endif // wxUSE_FILEDLG