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