]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
Correct Mac OX/iPhone check in libpng.
[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 105#define ID_UP_DIR (wxID_FILEDLGG + 2)
8ce68f7f 106#define ID_HOME_DIR (wxID_FILEDLGG + 3)
0cf3e587
VZ
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)
8ce68f7f 116 EVT_BUTTON(ID_HOME_DIR, wxGenericFileDialog::OnHome)
0cf3e587
VZ
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
cdc48273 167 parent = GetParentForModalDialog(parent, style);
2229243b 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 );
8ce68f7f
VZ
215 AddBitmapButton( ID_LIST_MODE, wxART_LIST_VIEW,
216 _("View files as a list view"), buttonsizer );
217 AddBitmapButton( ID_REPORT_MODE, wxART_REPORT_VIEW,
218 _("View files as a detailed view"), buttonsizer );
0b855868 219 buttonsizer->Add( 30, 5, 1 );
8ce68f7f
VZ
220 m_upDirButton = AddBitmapButton( ID_UP_DIR, wxART_GO_DIR_UP,
221 _("Go to parent directory"), buttonsizer );
c8c0e54c 222
37fd1c97 223#ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
8ce68f7f
VZ
224 AddBitmapButton( ID_HOME_DIR, wxART_GO_HOME,
225 _("Go to home directory"), buttonsizer );
e6daf794 226 buttonsizer->Add( 20, 20 );
75ec8bd4 227#endif //!__DOS__
c8c0e54c 228
8ce68f7f
VZ
229 m_newDirButton = AddBitmapButton( ID_NEW_DIR, wxART_NEW_DIR,
230 _("Create new directory"), buttonsizer );
c8c0e54c 231
2b5f62a0 232 if (is_pda)
8ce68f7f 233 mainsizer->Add( buttonsizer, wxSizerFlags().Expand() );
c15521c6 234 else
8ce68f7f
VZ
235 mainsizer->Add( buttonsizer, wxSizerFlags().Expand()
236 .Border( wxLEFT | wxRIGHT | wxTOP ) );
c8c0e54c 237
0cf3e587
VZ
238 long style2 = 0;
239 if ( HasFdFlag(wxFD_MULTIPLE) )
240 style2 |= wxFC_MULTIPLE;
9c884972 241
0cf3e587
VZ
242 m_filectrl = new wxGenericFileCtrl( this, ID_FILE_CTRL,
243 m_dir, defaultFile,
244 wildCard,
245 style2,
246 wxDefaultPosition, wxSize(540,200)
247 );
9cedab37 248
0cf3e587 249 m_filectrl->ShowHidden( ms_lastShowHidden );
619111b9 250
0cf3e587
VZ
251 if (ms_lastViewStyle == wxLC_LIST)
252 {
253 m_filectrl->ChangeToListMode();
254 }
255 else if (ms_lastViewStyle == wxLC_REPORT)
256 {
257 m_filectrl->ChangeToReportMode();
258 }
bd9f3519 259
8ce68f7f 260 mainsizer->Add(m_filectrl, wxSizerFlags(1).Expand().HorzBorder());
41fecb44 261
8ce68f7f
VZ
262 wxSizer *bsizer = CreateButtonSizer(wxOK | wxCANCEL);
263 if ( bsizer )
264 {
265 if (is_pda)
bd9f3519 266 mainsizer->Add(bsizer, wxSizerFlags().Expand().Border());
8ce68f7f
VZ
267 else
268 mainsizer->Add(bsizer, wxSizerFlags().Expand().DoubleBorder());
c15521c6 269 }
8b17ba72 270
8b17ba72 271 SetSizer( mainsizer );
c8c0e54c 272
619111b9
JS
273 if (!is_pda)
274 {
619111b9 275 mainsizer->SetSizeHints( this );
ed58dbea 276
619111b9
JS
277 Centre( wxBOTH );
278 }
c81c9392 279
4e1901b7 280 return true;
8b17ba72
RR
281}
282
23254b13 283wxGenericFileDialog::~wxGenericFileDialog()
cae5359f 284{
4e1901b7 285 if (!m_bypassGenericImpl)
eaf40b23 286 {
1a51ca33 287#if wxUSE_CONFIG
4e1901b7
RR
288 if (wxConfig::Get(false))
289 {
290 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
291 ms_lastViewStyle);
292 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
293 ms_lastShowHidden);
294 }
1a51ca33 295#endif
c5ef41d3 296 }
cae5359f
RR
297}
298
8ce68f7f
VZ
299wxBitmapButton* wxGenericFileDialog::AddBitmapButton( wxWindowID winId,
300 const wxArtID& artId,
301 const wxString& tip,
302 wxSizer *sizer)
303{
304 wxBitmapButton *but = new wxBitmapButton(this, winId,
305 wxArtProvider::GetBitmap(artId, wxART_BUTTON));
306 but->SetToolTip(tip);
307 sizer->Add(but, wxSizerFlags().Border());
308 return but;
309}
310
23254b13 311int wxGenericFileDialog::ShowModal()
9cedab37 312{
8ce68f7f
VZ
313 if (CreateExtraControl())
314 {
315 wxSizer *sizer = GetSizer();
316 sizer->Insert(2 /* after m_filectrl */, m_extraControl,
317 wxSizerFlags().Expand().HorzBorder());
318 sizer->Fit(this);
319 }
320
0cf3e587 321 m_filectrl->SetDirectory(m_dir);
619111b9 322
9cedab37
VZ
323 return wxDialog::ShowModal();
324}
325
16dce3b3
RR
326bool wxGenericFileDialog::Show( bool show )
327{
619111b9
JS
328 // Called by ShowModal, so don't repeate the update
329#ifndef __WIN32__
16dce3b3
RR
330 if (show)
331 {
0cf3e587 332 m_filectrl->SetDirectory(m_dir);
16dce3b3 333 }
619111b9 334#endif
16dce3b3
RR
335
336 return wxDialog::Show( show );
337}
338
0cf3e587 339void wxGenericFileDialog::OnOk( wxCommandEvent &WXUNUSED(event) )
df413171 340{
0cf3e587
VZ
341 wxArrayString selectedFiles;
342 m_filectrl->GetFilenames(selectedFiles);
0b855868 343
0cf3e587 344 if (selectedFiles.Count() == 0)
c8c0e54c 345 return;
c8c0e54c 346
0cf3e587 347 if (selectedFiles.Count() == 1)
ed58dbea 348 {
0cf3e587 349 SetPath( selectedFiles[0] );
3f6638b8
VZ
350 }
351
b28a6678 352 EndModal(wxID_OK);
e1811a01
RR
353}
354
23254b13 355void wxGenericFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
8b17ba72 356{
0cf3e587 357 m_filectrl->ChangeToListMode();
9cedab37 358 ms_lastViewStyle = wxLC_LIST;
0cf3e587 359 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
360}
361
23254b13 362void wxGenericFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
8b17ba72 363{
0cf3e587 364 m_filectrl->ChangeToReportMode();
9cedab37 365 ms_lastViewStyle = wxLC_REPORT;
0cf3e587 366 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
367}
368
23254b13 369void wxGenericFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
8b17ba72 370{
0cf3e587
VZ
371 m_filectrl->GoToParentDir();
372 m_filectrl->GetFileList()->SetFocus();
8b17ba72
RR
373}
374
23254b13 375void wxGenericFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
8b17ba72 376{
0cf3e587
VZ
377 m_filectrl->GoToHomeDir();
378 m_filectrl->SetFocus();
0b855868
RR
379}
380
23254b13 381void wxGenericFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
0b855868 382{
0cf3e587
VZ
383 m_filectrl->GetFileList()->MakeDir();
384}
619111b9 385
0cf3e587
VZ
386void wxGenericFileDialog::OnFileActivated( wxFileCtrlEvent &WXUNUSED(event) )
387{
388 wxCommandEvent dummy;
389 OnOk( dummy );
8b17ba72
RR
390}
391
e5dd66e9 392void wxGenericFileDialog::OnUpdateButtonsUI(wxUpdateUIEvent& event)
06cc1fb9 393{
91a865a4
VZ
394 // surprisingly, we can be called before m_filectrl is set in Create() as
395 // wxFileCtrl ctor itself can generate idle events, so we need this test
396 if ( m_filectrl )
398eebb1 397 event.Enable( !IsTopMostDir(m_filectrl->GetShownDirectory()) );
06cc1fb9
JS
398}
399
b2f0b934 400#ifdef wxHAS_GENERIC_FILEDIALOG
655cf310 401
412e0d47 402IMPLEMENT_DYNAMIC_CLASS(wxFileDialog, wxGenericFileDialog)
655cf310 403
b2f0b934 404#endif // wxHAS_GENERIC_FILEDIALOG
23254b13 405
4e1901b7 406#endif // wxUSE_FILEDLG