]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
Implemented HasContainerColumns() for GTK+
[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
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)
e5dd66e9
VZ
115
116 EVT_UPDATE_UI(ID_UP_DIR, wxGenericFileDialog::OnUpdateButtonsUI)
117#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
118 EVT_UPDATE_UI(ID_NEW_DIR, wxGenericFileDialog::OnUpdateButtonsUI)
119#endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
8b17ba72
RR
120END_EVENT_TABLE()
121
23254b13 122long wxGenericFileDialog::ms_lastViewStyle = wxLC_LIST;
ca65c044 123bool wxGenericFileDialog::ms_lastShowHidden = false;
655cf310 124
fe6cf128
VZ
125void wxGenericFileDialog::Init()
126{
127 m_bypassGenericImpl = false;
128
0cf3e587 129 m_filectrl = NULL;
fe6cf128
VZ
130 m_upDirButton = NULL;
131 m_newDirButton = NULL;
132}
133
23254b13 134wxGenericFileDialog::wxGenericFileDialog(wxWindow *parent,
9cedab37
VZ
135 const wxString& message,
136 const wxString& defaultDir,
137 const wxString& defaultFile,
138 const wxString& wildCard,
fe6cf128 139 long style,
4e1901b7 140 const wxPoint& pos,
ff3e84ff
VZ
141 const wxSize& sz,
142 const wxString& name,
fe6cf128 143 bool bypassGenericImpl ) : wxFileDialogBase()
8b17ba72 144{
fe6cf128 145 Init();
ff3e84ff 146 Create( parent, message, defaultDir, defaultFile, wildCard, style, pos, sz, name, bypassGenericImpl );
4e1901b7
RR
147}
148
149bool wxGenericFileDialog::Create( wxWindow *parent,
abc2a4cc 150 const wxString& message,
fe6cf128 151 const wxString& defaultDir,
abc2a4cc
WS
152 const wxString& defaultFile,
153 const wxString& wildCard,
fe6cf128
VZ
154 long style,
155 const wxPoint& pos,
ff3e84ff
VZ
156 const wxSize& sz,
157 const wxString& name,
fe6cf128 158 bool bypassGenericImpl )
4e1901b7 159{
fe6cf128
VZ
160 m_bypassGenericImpl = bypassGenericImpl;
161
2229243b
VZ
162 parent = GetParentForModalDialog(parent);
163
7e4fb3b8 164 if (!wxFileDialogBase::Create(parent, message, defaultDir, defaultFile,
ff3e84ff 165 wildCard, style, pos, sz, name))
fe6cf128
VZ
166 {
167 return false;
168 }
169
170 if (m_bypassGenericImpl)
171 return true;
172
ff3e84ff 173 if (!wxDialog::Create( parent, wxID_ANY, message, pos, sz,
6a305cfb 174 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | style, name
c81c9392 175 ))
4e1901b7
RR
176 {
177 return false;
178 }
f74172ab 179
1a51ca33 180#if wxUSE_CONFIG
ca65c044 181 if (wxConfig::Get(false))
eaf40b23 182 {
e7d9c398 183 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
9cedab37 184 &ms_lastViewStyle);
e7d9c398 185 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
9cedab37 186 &ms_lastShowHidden);
eaf40b23 187 }
1a51ca33 188#endif
eaf40b23 189
353f41cb 190 if ((m_dir.empty()) || (m_dir == wxT(".")))
ac2def68 191 {
75ec8bd4 192 m_dir = wxGetCwd();
619111b9
JS
193 if (m_dir.empty())
194 m_dir = wxFILE_SEP_PATH;
ac2def68 195 }
7a82dabc 196
0cf3e587 197 const size_t len = m_dir.length();
083f7497 198 if ((len > 1) && (wxEndsWithPathSeparator(m_dir)))
353f41cb
RR
199 m_dir.Remove( len-1, 1 );
200
655cf310 201 m_filterExtension = wxEmptyString;
c8c0e54c 202
cae5359f 203 // layout
4f5c180e 204
0cf3e587 205 const bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
c8c0e54c 206
8b17ba72 207 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 208
8b17ba72 209 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 210
e6daf794 211 wxBitmapButton *but;
c8c0e54c 212
7a82dabc 213 but = new wxBitmapButton(this, ID_LIST_MODE,
36668b35 214 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_BUTTON));
e6daf794
RR
215#if wxUSE_TOOLTIPS
216 but->SetToolTip( _("View files as a list view") );
217#endif
218 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 219
60d2cc25 220 but = new wxBitmapButton(this, ID_REPORT_MODE,
36668b35 221 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_BUTTON));
e6daf794
RR
222#if wxUSE_TOOLTIPS
223 but->SetToolTip( _("View files as a detailed view") );
224#endif
225 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 226
0b855868 227 buttonsizer->Add( 30, 5, 1 );
e6daf794 228
06cc1fb9 229 m_upDirButton = new wxBitmapButton(this, ID_UP_DIR,
36668b35 230 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_BUTTON));
e6daf794 231#if wxUSE_TOOLTIPS
06cc1fb9 232 m_upDirButton->SetToolTip( _("Go to parent directory") );
e6daf794 233#endif
06cc1fb9 234 buttonsizer->Add( m_upDirButton, 0, wxALL, 5 );
c8c0e54c 235
37fd1c97 236#ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
60d2cc25 237 but = new wxBitmapButton(this, ID_PARENT_DIR,
36668b35 238 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON));
e6daf794
RR
239#if wxUSE_TOOLTIPS
240 but->SetToolTip( _("Go to home directory") );
241#endif
242 buttonsizer->Add( but, 0, wxALL, 5);
c8c0e54c 243
e6daf794 244 buttonsizer->Add( 20, 20 );
75ec8bd4 245#endif //!__DOS__
c8c0e54c 246
06cc1fb9 247 m_newDirButton = new wxBitmapButton(this, ID_NEW_DIR,
36668b35 248 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON));
e6daf794 249#if wxUSE_TOOLTIPS
06cc1fb9 250 m_newDirButton->SetToolTip( _("Create new directory") );
e6daf794 251#endif
06cc1fb9 252 buttonsizer->Add( m_newDirButton, 0, wxALL, 5 );
c8c0e54c 253
2b5f62a0 254 if (is_pda)
c15521c6
RR
255 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
256 else
257 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
c8c0e54c 258
0cf3e587
VZ
259 long style2 = 0;
260 if ( HasFdFlag(wxFD_MULTIPLE) )
261 style2 |= wxFC_MULTIPLE;
9c884972 262
0cf3e587
VZ
263 m_filectrl = new wxGenericFileCtrl( this, ID_FILE_CTRL,
264 m_dir, defaultFile,
265 wildCard,
266 style2,
267 wxDefaultPosition, wxSize(540,200)
268 );
9cedab37 269
0cf3e587 270 m_filectrl->ShowHidden( ms_lastShowHidden );
619111b9 271
0cf3e587
VZ
272 if (ms_lastViewStyle == wxLC_LIST)
273 {
274 m_filectrl->ChangeToListMode();
275 }
276 else if (ms_lastViewStyle == wxLC_REPORT)
277 {
278 m_filectrl->ChangeToReportMode();
279 }
bd9f3519 280
2b5f62a0 281 if (is_pda)
c15521c6 282 {
41fecb44 283 // PDAs have a different screen layout
0cf3e587 284 mainsizer->Add(m_filectrl, wxSizerFlags(1).Expand().HorzBorder());
41fecb44 285
bd9f3519
VZ
286 wxSizer *bsizer = CreateButtonSizer(wxOK | wxCANCEL);
287 if ( bsizer )
288 mainsizer->Add(bsizer, wxSizerFlags().Expand().Border());
c15521c6 289 }
bd9f3519 290 else // !is_pda
c15521c6 291 {
0cf3e587 292 mainsizer->Add(m_filectrl, wxSizerFlags(1).Expand().DoubleHorzBorder());
bd9f3519 293
0cf3e587
VZ
294 wxBoxSizer *okcancelsizer = new wxBoxSizer( wxHORIZONTAL );
295 okcancelsizer->Add(new wxButton(this, wxID_OK), wxSizerFlags().DoubleBorder().Centre());
296 okcancelsizer->Add(new wxButton(this, wxID_CANCEL), wxSizerFlags().DoubleBorder().Centre());
297 mainsizer->Add(okcancelsizer, wxSizerFlags().Center());
298 }
8b17ba72 299
ca65c044 300 SetAutoLayout( true );
8b17ba72 301 SetSizer( mainsizer );
c8c0e54c 302
619111b9
JS
303 if (!is_pda)
304 {
305 mainsizer->Fit( this );
306 mainsizer->SetSizeHints( this );
ed58dbea 307
619111b9
JS
308 Centre( wxBOTH );
309 }
c81c9392 310
4e1901b7 311 return true;
8b17ba72
RR
312}
313
23254b13 314wxGenericFileDialog::~wxGenericFileDialog()
cae5359f 315{
4e1901b7 316 if (!m_bypassGenericImpl)
eaf40b23 317 {
1a51ca33 318#if wxUSE_CONFIG
4e1901b7
RR
319 if (wxConfig::Get(false))
320 {
321 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
322 ms_lastViewStyle);
323 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
324 ms_lastShowHidden);
325 }
1a51ca33 326#endif
c5ef41d3 327 }
cae5359f
RR
328}
329
23254b13 330int wxGenericFileDialog::ShowModal()
9cedab37 331{
0cf3e587 332 m_filectrl->SetDirectory(m_dir);
619111b9 333
9cedab37
VZ
334 return wxDialog::ShowModal();
335}
336
16dce3b3
RR
337bool wxGenericFileDialog::Show( bool show )
338{
619111b9
JS
339 // Called by ShowModal, so don't repeate the update
340#ifndef __WIN32__
16dce3b3
RR
341 if (show)
342 {
0cf3e587 343 m_filectrl->SetDirectory(m_dir);
16dce3b3 344 }
619111b9 345#endif
16dce3b3
RR
346
347 return wxDialog::Show( show );
348}
349
0cf3e587 350void wxGenericFileDialog::OnOk( wxCommandEvent &WXUNUSED(event) )
df413171 351{
0cf3e587
VZ
352 wxArrayString selectedFiles;
353 m_filectrl->GetFilenames(selectedFiles);
0b855868 354
0cf3e587 355 if (selectedFiles.Count() == 0)
c8c0e54c 356 return;
c8c0e54c 357
0cf3e587 358 if (selectedFiles.Count() == 1)
ed58dbea 359 {
0cf3e587 360 SetPath( selectedFiles[0] );
3f6638b8
VZ
361 }
362
b28a6678 363 EndModal(wxID_OK);
e1811a01
RR
364}
365
23254b13 366void wxGenericFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
8b17ba72 367{
0cf3e587 368 m_filectrl->ChangeToListMode();
9cedab37 369 ms_lastViewStyle = wxLC_LIST;
0cf3e587 370 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
371}
372
23254b13 373void wxGenericFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
8b17ba72 374{
0cf3e587 375 m_filectrl->ChangeToReportMode();
9cedab37 376 ms_lastViewStyle = wxLC_REPORT;
0cf3e587 377 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
378}
379
23254b13 380void wxGenericFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
8b17ba72 381{
0cf3e587
VZ
382 m_filectrl->GoToParentDir();
383 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
384}
385
23254b13 386void wxGenericFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
8b17ba72 387{
0cf3e587
VZ
388 m_filectrl->GoToHomeDir();
389 m_filectrl->SetFocus();
0b855868
RR
390}
391
23254b13 392void wxGenericFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
0b855868 393{
0cf3e587
VZ
394 m_filectrl->GetFileList()->MakeDir();
395}
619111b9 396
0cf3e587
VZ
397void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent &WXUNUSED(event) )
398{
399 wxCommandEvent dummy;
400 OnOk( dummy );
8b17ba72
RR
401}
402
e5dd66e9 403void wxGenericFileDialog::OnUpdateButtonsUI(wxUpdateUIEvent& event)
06cc1fb9 404{
91a865a4
VZ
405 // surprisingly, we can be called before m_filectrl is set in Create() as
406 // wxFileCtrl ctor itself can generate idle events, so we need this test
407 if ( m_filectrl )
398eebb1 408 event.Enable( !IsTopMostDir(m_filectrl->GetShownDirectory()) );
06cc1fb9
JS
409}
410
b2f0b934 411#ifdef wxHAS_GENERIC_FILEDIALOG
655cf310 412
412e0d47 413IMPLEMENT_DYNAMIC_CLASS(wxFileDialog, wxGenericFileDialog)
655cf310 414
b2f0b934 415#endif // wxHAS_GENERIC_FILEDIALOG
23254b13 416
4e1901b7 417#endif // wxUSE_FILEDLG