]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
fix for the last commit: reset the dummy wxRICHTEXT_DEFAULT_DROPTARGET pointer in...
[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
0cf3e587 19#if wxUSE_FILEDLG && (defined(__WXUNIVERSAL__) || defined(__WXGTK__))
1e6feb95 20
06cc1fb9 21// NOTE : it probably also supports MAC, untested
144cf6da 22#if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__)
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
56
619111b9 57#ifndef __WXWINCE__
e7c80f9e
WS
58 #include <sys/types.h>
59 #include <sys/stat.h>
619111b9 60#endif
4894ae18
VS
61
62#ifdef __UNIX__
63 #include <dirent.h>
64 #include <pwd.h>
65 #ifndef __VMS
66 # include <grp.h>
67 #endif
68#endif
69
6a8c7c70 70#ifdef __WINDOWS__
6a8c7c70
VS
71 #include "wx/msw/mslu.h"
72#endif
73
4894ae18
VS
74#ifdef __WATCOMC__
75 #include <direct.h>
27df579a 76#endif
4894ae18 77
619111b9 78#ifndef __WXWINCE__
7a82dabc 79#include <time.h>
619111b9
JS
80#endif
81
099d4217 82#if defined(__UNIX__) || defined(__DOS__)
ac2def68 83#include <unistd.h>
099d4217 84#endif
8b17ba72 85
619111b9
JS
86#if defined(__WXWINCE__)
87#define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/"))
88#elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__))
abc2a4cc 89#define IsTopMostDir(dir) (dir.empty())
70a7eb07
MW
90#else
91#define IsTopMostDir(dir) (dir == wxT("/"))
5e673a6a
VS
92#endif
93
8b17ba72 94//-----------------------------------------------------------------------------
23254b13 95// wxGenericFileDialog
8b17ba72
RR
96//-----------------------------------------------------------------------------
97
5e673a6a
VS
98#define ID_LIST_MODE (wxID_FILEDLGG )
99#define ID_REPORT_MODE (wxID_FILEDLGG + 1)
0cf3e587
VZ
100#define ID_UP_DIR (wxID_FILEDLGG + 2)
101#define ID_PARENT_DIR (wxID_FILEDLGG + 3)
102#define ID_NEW_DIR (wxID_FILEDLGG + 4)
103#define ID_FILE_CTRL (wxID_FILEDLGG + 5)
8b17ba72 104
f74172ab 105IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog, wxFileDialogBase)
23254b13
JS
106
107BEGIN_EVENT_TABLE(wxGenericFileDialog,wxDialog)
0cf3e587
VZ
108 EVT_BUTTON(ID_LIST_MODE, wxGenericFileDialog::OnList)
109 EVT_BUTTON(ID_REPORT_MODE, wxGenericFileDialog::OnReport)
110 EVT_BUTTON(ID_UP_DIR, wxGenericFileDialog::OnUp)
111 EVT_BUTTON(ID_PARENT_DIR, wxGenericFileDialog::OnHome)
112 EVT_BUTTON(ID_NEW_DIR, wxGenericFileDialog::OnNew)
113 EVT_BUTTON(wxID_OK, wxGenericFileDialog::OnOk)
114 EVT_FILECTRL_FILEACTIVATED(ID_FILE_CTRL, wxGenericFileDialog::OnFileActivated)
8b17ba72
RR
115END_EVENT_TABLE()
116
23254b13 117long wxGenericFileDialog::ms_lastViewStyle = wxLC_LIST;
ca65c044 118bool wxGenericFileDialog::ms_lastShowHidden = false;
655cf310 119
fe6cf128
VZ
120void wxGenericFileDialog::Init()
121{
122 m_bypassGenericImpl = false;
123
0cf3e587 124 m_filectrl = NULL;
fe6cf128
VZ
125 m_upDirButton = NULL;
126 m_newDirButton = NULL;
127}
128
23254b13 129wxGenericFileDialog::wxGenericFileDialog(wxWindow *parent,
9cedab37
VZ
130 const wxString& message,
131 const wxString& defaultDir,
132 const wxString& defaultFile,
133 const wxString& wildCard,
fe6cf128 134 long style,
4e1901b7 135 const wxPoint& pos,
ff3e84ff
VZ
136 const wxSize& sz,
137 const wxString& name,
fe6cf128 138 bool bypassGenericImpl ) : wxFileDialogBase()
8b17ba72 139{
fe6cf128 140 Init();
ff3e84ff 141 Create( parent, message, defaultDir, defaultFile, wildCard, style, pos, sz, name, bypassGenericImpl );
4e1901b7
RR
142}
143
144bool wxGenericFileDialog::Create( wxWindow *parent,
abc2a4cc 145 const wxString& message,
fe6cf128 146 const wxString& defaultDir,
abc2a4cc
WS
147 const wxString& defaultFile,
148 const wxString& wildCard,
fe6cf128
VZ
149 long style,
150 const wxPoint& pos,
ff3e84ff
VZ
151 const wxSize& sz,
152 const wxString& name,
fe6cf128 153 bool bypassGenericImpl )
4e1901b7 154{
fe6cf128
VZ
155 m_bypassGenericImpl = bypassGenericImpl;
156
2229243b
VZ
157 parent = GetParentForModalDialog(parent);
158
7e4fb3b8 159 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFile,
ff3e84ff 160 wildCard, style, pos, sz, name))
fe6cf128
VZ
161 {
162 return false;
163 }
164
165 if (m_bypassGenericImpl)
166 return true;
167
ff3e84ff 168 if (!wxDialog::Create( parent, wxID_ANY, message, pos, sz,
6a305cfb 169 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | style, name
c81c9392 170 ))
4e1901b7
RR
171 {
172 return false;
173 }
f74172ab 174
1a51ca33 175#if wxUSE_CONFIG
ca65c044 176 if (wxConfig::Get(false))
eaf40b23 177 {
e7d9c398 178 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
9cedab37 179 &ms_lastViewStyle);
e7d9c398 180 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
9cedab37 181 &ms_lastShowHidden);
eaf40b23 182 }
1a51ca33 183#endif
eaf40b23 184
353f41cb 185 if ((m_dir.empty()) || (m_dir == wxT(".")))
ac2def68 186 {
75ec8bd4 187 m_dir = wxGetCwd();
619111b9
JS
188 if (m_dir.empty())
189 m_dir = wxFILE_SEP_PATH;
ac2def68 190 }
7a82dabc 191
0cf3e587 192 const size_t len = m_dir.length();
083f7497 193 if ((len > 1) && (wxEndsWithPathSeparator(m_dir)))
353f41cb
RR
194 m_dir.Remove( len-1, 1 );
195
196 m_path = m_dir;
75ec8bd4 197 m_path += wxFILE_SEP_PATH;
8b17ba72 198 m_path += defaultFile;
655cf310 199 m_filterExtension = wxEmptyString;
c8c0e54c 200
cae5359f 201 // layout
4f5c180e 202
0cf3e587 203 const bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
c8c0e54c 204
8b17ba72 205 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 206
8b17ba72 207 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 208
e6daf794 209 wxBitmapButton *but;
c8c0e54c 210
7a82dabc 211 but = new wxBitmapButton(this, ID_LIST_MODE,
36668b35 212 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_BUTTON));
e6daf794
RR
213#if wxUSE_TOOLTIPS
214 but->SetToolTip( _("View files as a list view") );
215#endif
216 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 217
60d2cc25 218 but = new wxBitmapButton(this, ID_REPORT_MODE,
36668b35 219 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_BUTTON));
e6daf794
RR
220#if wxUSE_TOOLTIPS
221 but->SetToolTip( _("View files as a detailed view") );
222#endif
223 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 224
0b855868 225 buttonsizer->Add( 30, 5, 1 );
e6daf794 226
06cc1fb9 227 m_upDirButton = new wxBitmapButton(this, ID_UP_DIR,
36668b35 228 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_BUTTON));
e6daf794 229#if wxUSE_TOOLTIPS
06cc1fb9 230 m_upDirButton->SetToolTip( _("Go to parent directory") );
e6daf794 231#endif
06cc1fb9 232 buttonsizer->Add( m_upDirButton, 0, wxALL, 5 );
c8c0e54c 233
37fd1c97 234#ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
60d2cc25 235 but = new wxBitmapButton(this, ID_PARENT_DIR,
36668b35 236 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON));
e6daf794
RR
237#if wxUSE_TOOLTIPS
238 but->SetToolTip( _("Go to home directory") );
239#endif
240 buttonsizer->Add( but, 0, wxALL, 5);
c8c0e54c 241
e6daf794 242 buttonsizer->Add( 20, 20 );
75ec8bd4 243#endif //!__DOS__
c8c0e54c 244
06cc1fb9 245 m_newDirButton = new wxBitmapButton(this, ID_NEW_DIR,
36668b35 246 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON));
e6daf794 247#if wxUSE_TOOLTIPS
06cc1fb9 248 m_newDirButton->SetToolTip( _("Create new directory") );
e6daf794 249#endif
06cc1fb9 250 buttonsizer->Add( m_newDirButton, 0, wxALL, 5 );
c8c0e54c 251
2b5f62a0 252 if (is_pda)
c15521c6
RR
253 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
254 else
255 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
c8c0e54c 256
0cf3e587
VZ
257 long style2 = 0;
258 if ( HasFdFlag(wxFD_MULTIPLE) )
259 style2 |= wxFC_MULTIPLE;
9c884972 260
0cf3e587
VZ
261 m_filectrl = new wxGenericFileCtrl( this, ID_FILE_CTRL,
262 m_dir, defaultFile,
263 wildCard,
264 style2,
265 wxDefaultPosition, wxSize(540,200)
266 );
9cedab37 267
0cf3e587 268 m_filectrl->ShowHidden( ms_lastShowHidden );
619111b9 269
0cf3e587
VZ
270 if (ms_lastViewStyle == wxLC_LIST)
271 {
272 m_filectrl->ChangeToListMode();
273 }
274 else if (ms_lastViewStyle == wxLC_REPORT)
275 {
276 m_filectrl->ChangeToReportMode();
277 }
bd9f3519 278
2b5f62a0 279 if (is_pda)
c15521c6 280 {
41fecb44 281 // PDAs have a different screen layout
0cf3e587 282 mainsizer->Add(m_filectrl, wxSizerFlags(1).Expand().HorzBorder());
41fecb44 283
bd9f3519
VZ
284 wxSizer *bsizer = CreateButtonSizer(wxOK | wxCANCEL);
285 if ( bsizer )
286 mainsizer->Add(bsizer, wxSizerFlags().Expand().Border());
c15521c6 287 }
bd9f3519 288 else // !is_pda
c15521c6 289 {
0cf3e587 290 mainsizer->Add(m_filectrl, wxSizerFlags(1).Expand().DoubleHorzBorder());
bd9f3519 291
0cf3e587
VZ
292 wxBoxSizer *okcancelsizer = new wxBoxSizer( wxHORIZONTAL );
293 okcancelsizer->Add(new wxButton(this, wxID_OK), wxSizerFlags().DoubleBorder().Centre());
294 okcancelsizer->Add(new wxButton(this, wxID_CANCEL), wxSizerFlags().DoubleBorder().Centre());
295 mainsizer->Add(okcancelsizer, wxSizerFlags().Center());
296 }
8b17ba72 297
ca65c044 298 SetAutoLayout( true );
8b17ba72 299 SetSizer( mainsizer );
c8c0e54c 300
619111b9
JS
301 if (!is_pda)
302 {
303 mainsizer->Fit( this );
304 mainsizer->SetSizeHints( this );
ed58dbea 305
619111b9
JS
306 Centre( wxBOTH );
307 }
c81c9392 308
4e1901b7 309 return true;
8b17ba72
RR
310}
311
23254b13 312wxGenericFileDialog::~wxGenericFileDialog()
cae5359f 313{
4e1901b7 314 if (!m_bypassGenericImpl)
eaf40b23 315 {
1a51ca33 316#if wxUSE_CONFIG
4e1901b7
RR
317 if (wxConfig::Get(false))
318 {
319 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
320 ms_lastViewStyle);
321 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
322 ms_lastShowHidden);
323 }
1a51ca33 324#endif
c5ef41d3 325 }
cae5359f
RR
326}
327
23254b13 328int wxGenericFileDialog::ShowModal()
9cedab37 329{
0cf3e587 330 m_filectrl->SetDirectory(m_dir);
06cc1fb9 331 UpdateControls();
619111b9 332
9cedab37
VZ
333 return wxDialog::ShowModal();
334}
335
16dce3b3
RR
336bool wxGenericFileDialog::Show( bool show )
337{
619111b9
JS
338 // Called by ShowModal, so don't repeate the update
339#ifndef __WIN32__
16dce3b3
RR
340 if (show)
341 {
0cf3e587 342 m_filectrl->SetDirectory(m_dir);
16dce3b3 343 UpdateControls();
16dce3b3 344 }
619111b9 345#endif
16dce3b3
RR
346
347 return wxDialog::Show( show );
348}
349
b4cfe261
VZ
350void wxGenericFileDialog::SetWildcard(const wxString& wildCard)
351{
0cf3e587 352 m_filectrl->SetWildcard(wildCard);
b4cfe261
VZ
353}
354
23254b13 355void wxGenericFileDialog::SetFilterIndex( int filterindex )
04ea7f93 356{
0cf3e587 357 m_filectrl->SetFilterIndex(filterindex);
cae5359f
RR
358}
359
0cf3e587 360void wxGenericFileDialog::OnOk( wxCommandEvent &WXUNUSED(event) )
df413171 361{
0cf3e587
VZ
362 wxArrayString selectedFiles;
363 m_filectrl->GetFilenames(selectedFiles);
0b855868 364
0cf3e587 365 if (selectedFiles.Count() == 0)
c8c0e54c 366 return;
c8c0e54c 367
0cf3e587 368 if (selectedFiles.Count() == 1)
ed58dbea 369 {
0cf3e587 370 SetPath( selectedFiles[0] );
3f6638b8
VZ
371 }
372
b28a6678 373 EndModal(wxID_OK);
e1811a01
RR
374}
375
23254b13 376void wxGenericFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
8b17ba72 377{
0cf3e587 378 m_filectrl->ChangeToListMode();
9cedab37 379 ms_lastViewStyle = wxLC_LIST;
0cf3e587 380 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
381}
382
23254b13 383void wxGenericFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
8b17ba72 384{
0cf3e587 385 m_filectrl->ChangeToReportMode();
9cedab37 386 ms_lastViewStyle = wxLC_REPORT;
0cf3e587 387 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
388}
389
23254b13 390void wxGenericFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
8b17ba72 391{
0cf3e587
VZ
392 m_filectrl->GoToParentDir();
393 m_filectrl->GetFileList()->SetFocus();
06cc1fb9 394 UpdateControls();
8b17ba72
RR
395}
396
23254b13 397void wxGenericFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
8b17ba72 398{
0cf3e587
VZ
399 m_filectrl->GoToHomeDir();
400 m_filectrl->SetFocus();
06cc1fb9 401 UpdateControls();
0b855868
RR
402}
403
23254b13 404void wxGenericFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
0b855868 405{
0cf3e587
VZ
406 m_filectrl->GetFileList()->MakeDir();
407}
619111b9 408
0cf3e587
VZ
409void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent &WXUNUSED(event) )
410{
411 wxCommandEvent dummy;
412 OnOk( dummy );
8b17ba72
RR
413}
414
23254b13 415void wxGenericFileDialog::SetPath( const wxString& path )
8b17ba72
RR
416{
417 // not only set the full path but also update filename and dir
418 m_path = path;
619111b9 419
0cf3e587 420 m_filectrl->SetPath(path);
8b17ba72 421}
c8c0e54c 422
23254b13 423void wxGenericFileDialog::GetPaths( wxArrayString& paths ) const
7941ba11 424{
0cf3e587 425 m_filectrl->GetPaths(paths);
7941ba11
RR
426}
427
23254b13 428void wxGenericFileDialog::GetFilenames(wxArrayString& files) const
7941ba11 429{
0cf3e587 430 m_filectrl->GetFilenames(files);
7941ba11
RR
431}
432
06cc1fb9
JS
433void wxGenericFileDialog::UpdateControls()
434{
0cf3e587 435 const bool enable = !IsTopMostDir(m_filectrl->GetDirectory());
06cc1fb9
JS
436 m_upDirButton->Enable(enable);
437
144cf6da 438#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
06cc1fb9 439 m_newDirButton->Enable(enable);
144cf6da 440#endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
06cc1fb9
JS
441}
442
b2f0b934 443#ifdef wxHAS_GENERIC_FILEDIALOG
655cf310 444
412e0d47 445IMPLEMENT_DYNAMIC_CLASS(wxFileDialog, wxGenericFileDialog)
655cf310 446
b2f0b934 447#endif // wxHAS_GENERIC_FILEDIALOG
23254b13 448
4e1901b7 449#endif // wxUSE_FILEDLG