]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
made wxFFile a bit more safe: don't crash when Tell() and Length() are called on...
[wxWidgets.git] / src / generic / filedlgg.cpp
CommitLineData
81169710 1//////////////////////////////////////////////////////////////////////////////
8b17ba72 2// Name: 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
c8c0e54c 9// Licence: wxWindows licence
8b17ba72
RR
10/////////////////////////////////////////////////////////////////////////////
11
14f355c2 12#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
8b17ba72
RR
13#pragma implementation "filedlgg.h"
14#endif
15
16// For compilers that support precompilation, includes "wx.h".
17#include "wx/wxprec.h"
18
19#ifdef __BORLANDC__
20#pragma hdrstop
21#endif
22
1e6feb95
VZ
23#if wxUSE_FILEDLG
24
06cc1fb9 25// NOTE : it probably also supports MAC, untested
144cf6da 26#if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__) && !defined(__OS2__)
23254b13 27#error wxGenericFileDialog currently only supports Unix, win32 and DOS
8b17ba72
RR
28#endif
29
04dbb646
VZ
30#include "wx/checkbox.h"
31#include "wx/textctrl.h"
32#include "wx/choice.h"
33#include "wx/checkbox.h"
34#include "wx/stattext.h"
8b17ba72
RR
35#include "wx/debug.h"
36#include "wx/log.h"
37#include "wx/intl.h"
38#include "wx/msgdlg.h"
39#include "wx/sizer.h"
0b855868 40#include "wx/bmpbuttn.h"
cae5359f 41#include "wx/tokenzr.h"
eaf40b23 42#include "wx/config.h"
48fe8374 43#include "wx/imaglist.h"
5e673a6a 44#include "wx/dir.h"
60d2cc25 45#include "wx/artprov.h"
1265e13d 46#include "wx/file.h" // for wxS_IXXX constants only
23254b13
JS
47#include "wx/filedlg.h" // wxOPEN, wxSAVE...
48#include "wx/generic/filedlgg.h"
06cc1fb9 49#include "wx/generic/dirctrlg.h" // for wxFileIconsTable
8b17ba72 50
e6daf794
RR
51#if wxUSE_TOOLTIPS
52 #include "wx/tooltip.h"
53#endif
54
ac2def68
RR
55#include <sys/types.h>
56#include <sys/stat.h>
4894ae18
VS
57
58#ifdef __UNIX__
59 #include <dirent.h>
60 #include <pwd.h>
61 #ifndef __VMS
62 # include <grp.h>
63 #endif
64#endif
65
6a8c7c70
VS
66#ifdef __WINDOWS__
67 #include "wx/msw/wrapwin.h"
68 #include "wx/msw/mslu.h"
69#endif
70
4894ae18
VS
71#ifdef __WATCOMC__
72 #include <direct.h>
27df579a 73#endif
4894ae18 74
7a82dabc 75#include <time.h>
099d4217 76#if defined(__UNIX__) || defined(__DOS__)
ac2def68 77#include <unistd.h>
099d4217 78#endif
8b17ba72 79
655cf310 80// ----------------------------------------------------------------------------
06cc1fb9 81// private functions
655cf310
VS
82// ----------------------------------------------------------------------------
83
06cc1fb9 84static
7a1e00a1 85int wxCALLBACK wxFileDataNameCompare( long data1, long data2, long data)
655cf310 86{
06cc1fb9
JS
87 wxFileData *fd1 = (wxFileData*)data1;
88 wxFileData *fd2 = (wxFileData*)data2;
89 if (fd1->GetFileName() == wxT("..")) return -data;
90 if (fd2->GetFileName() == wxT("..")) return data;
91 if (fd1->IsDir() && !fd2->IsDir()) return -data;
92 if (fd2->IsDir() && !fd1->IsDir()) return data;
93 return data*wxStrcmp( fd1->GetFileName(), fd2->GetFileName() );
655cf310
VS
94}
95
c8c0e54c 96static
7a1e00a1 97int wxCALLBACK wxFileDataSizeCompare( long data1, long data2, long data)
23254b13
JS
98{
99 wxFileData *fd1 = (wxFileData*)data1;
100 wxFileData *fd2 = (wxFileData*)data2;
06cc1fb9
JS
101 if (fd1->GetFileName() == wxT("..")) return -data;
102 if (fd2->GetFileName() == wxT("..")) return data;
23254b13
JS
103 if (fd1->IsDir() && !fd2->IsDir()) return -data;
104 if (fd2->IsDir() && !fd1->IsDir()) return data;
06cc1fb9
JS
105 if (fd1->IsLink() && !fd2->IsLink()) return -data;
106 if (fd2->IsLink() && !fd1->IsLink()) return data;
107 return data*(fd1->GetSize() - fd2->GetSize());
23254b13
JS
108}
109
110static
7a1e00a1 111int wxCALLBACK wxFileDataTypeCompare( long data1, long data2, long data)
23254b13
JS
112{
113 wxFileData *fd1 = (wxFileData*)data1;
114 wxFileData *fd2 = (wxFileData*)data2;
06cc1fb9
JS
115 if (fd1->GetFileName() == wxT("..")) return -data;
116 if (fd2->GetFileName() == wxT("..")) return data;
23254b13
JS
117 if (fd1->IsDir() && !fd2->IsDir()) return -data;
118 if (fd2->IsDir() && !fd1->IsDir()) return data;
119 if (fd1->IsLink() && !fd2->IsLink()) return -data;
120 if (fd2->IsLink() && !fd1->IsLink()) return data;
b600ed13 121 return data*wxStrcmp( fd1->GetFileType(), fd2->GetFileType() );
23254b13
JS
122}
123
124static
7a1e00a1 125int wxCALLBACK wxFileDataTimeCompare( long data1, long data2, long data)
23254b13
JS
126{
127 wxFileData *fd1 = (wxFileData*)data1;
128 wxFileData *fd2 = (wxFileData*)data2;
06cc1fb9
JS
129 if (fd1->GetFileName() == wxT("..")) return -data;
130 if (fd2->GetFileName() == wxT("..")) return data;
23254b13
JS
131 if (fd1->IsDir() && !fd2->IsDir()) return -data;
132 if (fd2->IsDir() && !fd1->IsDir()) return data;
133
b600ed13 134 return fd1->GetDateTime().IsLaterThan(fd2->GetDateTime()) ? int(data) : -int(data);
c8c0e54c
VZ
135}
136
144cf6da 137#if defined(__UNIX__) && !defined(__OS2__)
5e673a6a
VS
138#define IsTopMostDir(dir) (dir == wxT("/"))
139#endif
140
144cf6da 141#if defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)
5e673a6a
VS
142#define IsTopMostDir(dir) (dir.IsEmpty())
143#endif
144
4f5c180e 145#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
06cc1fb9 146// defined in src/generic/dirctrlg.cpp
37fd1c97
VS
147extern bool wxIsDriveAvailable(const wxString& dirName);
148#endif
149
06cc1fb9
JS
150// defined in src/generic/dirctrlg.cpp
151extern size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids);
152
8b17ba72
RR
153//-----------------------------------------------------------------------------
154// wxFileData
155//-----------------------------------------------------------------------------
156
06cc1fb9 157wxFileData::wxFileData( const wxString &filePath, const wxString &fileName, fileType type, int image_id )
8b17ba72 158{
06cc1fb9
JS
159 m_fileName = fileName;
160 m_filePath = filePath;
23254b13 161 m_type = type;
06cc1fb9 162 m_image = image_id;
7a82dabc 163
b600ed13
VZ
164 ReadData();
165}
166
81169710
VZ
167void wxFileData::Copy( const wxFileData& fileData )
168{
169 m_fileName = fileData.GetFileName();
170 m_filePath = fileData.GetFilePath();
171 m_size = fileData.GetSize();
172 m_dateTime = fileData.GetDateTime();
173 m_permissions = fileData.GetPermissions();
174 m_type = fileData.GetType();
175 m_image = GetImageId();
176}
177
b600ed13
VZ
178void wxFileData::ReadData()
179{
06cc1fb9 180 if (IsDrive())
37fd1c97 181 {
23254b13
JS
182 m_size = 0;
183 return;
184 }
185
144cf6da 186#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
06cc1fb9 187 // c:\.. is a drive don't stat it
567be187 188 if ((m_fileName == wxT("..")) && (m_filePath.length() <= 5))
23254b13
JS
189 {
190 m_type = is_drive;
37fd1c97
VS
191 m_size = 0;
192 return;
193 }
7f04165e 194#endif // __DOS__ || __WINDOWS__
c8c0e54c 195
8115f9e7 196 wxStructStat buff;
479cd5de 197
144cf6da 198#if defined(__UNIX__) && (!defined( __OS2__ ) && !defined(__VMS))
06cc1fb9 199 lstat( m_filePath.fn_str(), &buff );
23254b13 200 m_type |= S_ISLNK( buff.st_mode ) != 0 ? is_link : 0;
7f04165e 201#else // no lstat()
06cc1fb9 202 wxStat( m_filePath, &buff );
9f2d09aa
RR
203#endif
204
23254b13
JS
205 m_type |= (buff.st_mode & S_IFDIR) != 0 ? is_dir : 0;
206 m_type |= (buff.st_mode & wxS_IXUSR) != 0 ? is_exe : 0;
8b17ba72 207
06cc1fb9
JS
208 // try to get a better icon
209 if (m_image == wxFileIconsTable::file)
210 {
211 if (IsExe())
212 m_image = wxFileIconsTable::executable;
213 else if (m_fileName.Find(wxT('.'), TRUE) != wxNOT_FOUND)
214 m_image = wxTheFileIconsTable->GetIconID(m_fileName.AfterLast(wxT('.')));
215 }
216
8b17ba72
RR
217 m_size = buff.st_size;
218
06cc1fb9 219 m_dateTime = buff.st_mtime;
8b17ba72 220
06cc1fb9
JS
221#if defined(__UNIX__)
222 m_permissions.Printf(_T("%c%c%c%c%c%c%c%c%c"),
7f04165e
VZ
223 buff.st_mode & wxS_IRUSR ? _T('r') : _T('-'),
224 buff.st_mode & wxS_IWUSR ? _T('w') : _T('-'),
06cc1fb9
JS
225 buff.st_mode & wxS_IXUSR ? _T('x') : _T('-'),
226 buff.st_mode & wxS_IRGRP ? _T('r') : _T('-'),
227 buff.st_mode & wxS_IWGRP ? _T('w') : _T('-'),
228 buff.st_mode & wxS_IXGRP ? _T('x') : _T('-'),
229 buff.st_mode & wxS_IROTH ? _T('r') : _T('-'),
230 buff.st_mode & wxS_IWOTH ? _T('w') : _T('-'),
231 buff.st_mode & wxS_IXOTH ? _T('x') : _T('-'));
232#elif defined(__WIN32__)
567be187 233 DWORD attribs = GetFileAttributes(m_filePath);
06cc1fb9
JS
234 if (attribs != (DWORD)-1)
235 {
236 m_permissions.Printf(_T("%c%c%c%c"),
237 attribs & FILE_ATTRIBUTE_ARCHIVE ? _T('A') : _T(' '),
238 attribs & FILE_ATTRIBUTE_READONLY ? _T('R') : _T(' '),
239 attribs & FILE_ATTRIBUTE_HIDDEN ? _T('H') : _T(' '),
240 attribs & FILE_ATTRIBUTE_SYSTEM ? _T('S') : _T(' '));
241 }
242#endif
8b17ba72
RR
243}
244
b600ed13 245wxString wxFileData::GetFileType() const
8b17ba72 246{
06cc1fb9
JS
247 if (IsDir())
248 return _("<DIR>");
249 else if (IsLink())
250 return _("<LINK>");
251 else if (IsDrive())
252 return _("<DRIVE>");
253 else if (m_fileName.Find(wxT('.'), TRUE) != wxNOT_FOUND)
254 return m_fileName.AfterLast(wxT('.'));
8b17ba72 255
06cc1fb9 256 return wxEmptyString;
8b17ba72
RR
257}
258
06cc1fb9 259wxString wxFileData::GetModificationTime() const
23254b13 260{
06cc1fb9
JS
261 // want time as 01:02 so they line up nicely, no %r in WIN32
262 return m_dateTime.FormatDate() + wxT(" ") + m_dateTime.Format(wxT("%I:%M:%S %p"));
23254b13
JS
263}
264
8b17ba72
RR
265wxString wxFileData::GetHint() const
266{
06cc1fb9 267 wxString s = m_filePath;
2b5f62a0 268 s += wxT(" ");
b8985048 269
23254b13 270 if (IsDir())
b8985048 271 s += _("<DIR>");
23254b13 272 else if (IsLink())
b8985048 273 s += _("<LINK>");
23254b13 274 else if (IsDrive())
b8985048
VZ
275 s += _("<DRIVE>");
276 else // plain file
277 s += wxString::Format( _("%ld bytes"), m_size );
278
279 s += wxT(' ');
280
281 if ( !IsDrive() )
23254b13 282 {
b8985048
VZ
283 s << GetModificationTime()
284 << wxT(" ")
285 << m_permissions;
8b17ba72 286 }
23254b13 287
8b17ba72
RR
288 return s;
289};
290
23254b13 291wxString wxFileData::GetEntry( fileListFieldType num ) const
8b17ba72
RR
292{
293 wxString s;
9cedab37 294 switch ( num )
8b17ba72 295 {
9cedab37 296 case FileList_Name:
06cc1fb9 297 s = m_fileName;
8b17ba72 298 break;
9cedab37 299
06cc1fb9
JS
300 case FileList_Size:
301 if (!IsDir() && !IsLink() && !IsDrive())
9cedab37 302 s.Printf(_T("%ld"), m_size);
8b17ba72 303 break;
9cedab37 304
06cc1fb9 305 case FileList_Type:
b600ed13 306 s = GetFileType();
8b17ba72 307 break;
8b17ba72 308
9cedab37 309 case FileList_Time:
23254b13 310 if (!IsDrive())
06cc1fb9 311 s = GetModificationTime();
9cedab37 312 break;
8b17ba72 313
06cc1fb9 314#if defined(__UNIX__) || defined(__WIN32__)
9cedab37
VZ
315 case FileList_Perm:
316 s = m_permissions;
317 break;
06cc1fb9 318#endif // defined(__UNIX__) || defined(__WIN32__)
8b17ba72 319
9cedab37
VZ
320 default:
321 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
322 }
8b17ba72 323
9cedab37 324 return s;
8b17ba72
RR
325}
326
06cc1fb9 327void wxFileData::SetNewName( const wxString &filePath, const wxString &fileName )
8b17ba72 328{
06cc1fb9
JS
329 m_fileName = fileName;
330 m_filePath = filePath;
8b17ba72
RR
331}
332
333void wxFileData::MakeItem( wxListItem &item )
334{
06cc1fb9 335 item.m_text = m_fileName;
9b00bb16 336 item.ClearAttributes();
9cedab37
VZ
337 if (IsExe())
338 item.SetTextColour(*wxRED);
339 if (IsDir())
340 item.SetTextColour(*wxBLUE);
48fe8374 341
06cc1fb9 342 item.m_image = m_image;
655cf310 343
8b17ba72
RR
344 if (IsLink())
345 {
564a150b
VZ
346 wxColour dg = wxTheColourDatabase->Find( _T("MEDIUM GREY") );
347 if ( dg.Ok() )
348 item.SetTextColour(dg);
8b17ba72
RR
349 }
350 item.m_data = (long)this;
351}
c8c0e54c 352
8b17ba72
RR
353//-----------------------------------------------------------------------------
354// wxFileCtrl
355//-----------------------------------------------------------------------------
356
d84afea9 357IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl)
8b17ba72
RR
358
359BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
0b855868 360 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem)
81169710 361 EVT_LIST_DELETE_ALL_ITEMS(-1, wxFileCtrl::OnListDeleteAllItems)
0b855868 362 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit)
23254b13 363 EVT_LIST_COL_CLICK(-1, wxFileCtrl::OnListColClick)
8b17ba72
RR
364END_EVENT_TABLE()
365
655cf310 366
8b17ba72
RR
367wxFileCtrl::wxFileCtrl()
368{
8b17ba72 369 m_showHidden = FALSE;
23254b13
JS
370 m_sort_foward = 1;
371 m_sort_field = wxFileData::FileList_Name;
8b17ba72
RR
372}
373
9cedab37
VZ
374wxFileCtrl::wxFileCtrl(wxWindow *win,
375 wxWindowID id,
376 const wxString& wild,
377 bool showHidden,
378 const wxPoint& pos,
379 const wxSize& size,
380 long style,
381 const wxValidator &validator,
5e673a6a 382 const wxString &name)
9cedab37
VZ
383 : wxListCtrl(win, id, pos, size, style, validator, name),
384 m_wild(wild)
8b17ba72 385{
06cc1fb9 386 wxImageList *imageList = wxTheFileIconsTable->GetSmallImageList();
655cf310 387
0b855868 388 SetImageList( imageList, wxIMAGE_LIST_SMALL );
c8c0e54c 389
9cedab37 390 m_showHidden = showHidden;
23254b13
JS
391
392 m_sort_foward = 1;
393 m_sort_field = wxFileData::FileList_Name;
394
395 m_dirName = wxT("*");
396
397 if (style & wxLC_REPORT)
398 ChangeToReportMode();
8b17ba72
RR
399}
400
401void wxFileCtrl::ChangeToListMode()
402{
23254b13 403 ClearAll();
8b17ba72 404 SetSingleStyle( wxLC_LIST );
8f4fcc4e 405 UpdateFiles();
8b17ba72
RR
406}
407
408void wxFileCtrl::ChangeToReportMode()
409{
23254b13 410 ClearAll();
8b17ba72 411 SetSingleStyle( wxLC_REPORT );
23254b13 412
06cc1fb9
JS
413 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
414 // don't hardcode since mm/dd is dd/mm elsewhere
23254b13 415 int w, h;
06cc1fb9
JS
416 wxDateTime dt(22, wxDateTime::Dec, 2002, 22, 22, 22);
417 wxString txt = dt.FormatDate() + wxT("22") + dt.Format(wxT("%I:%M:%S %p"));
418 GetTextExtent(txt, &w, &h);
419
420 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, w );
421 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, w/2 );
422 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT, w/2 );
423 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT, w );
424#if defined(__UNIX__)
425 GetTextExtent(wxT("Permissions 2"), &w, &h);
426 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, w );
427#elif defined(__WIN32__)
428 GetTextExtent(wxT("Attributes 2"), &w, &h);
429 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT, w );
23254b13
JS
430#endif
431
8f4fcc4e 432 UpdateFiles();
8b17ba72
RR
433}
434
06cc1fb9 435void wxFileCtrl::ChangeToSmallIconMode()
8b17ba72 436{
23254b13 437 ClearAll();
06cc1fb9 438 SetSingleStyle( wxLC_SMALL_ICON );
8f4fcc4e 439 UpdateFiles();
8b17ba72
RR
440}
441
442void wxFileCtrl::ShowHidden( bool show )
443{
444 m_showHidden = show;
8f4fcc4e 445 UpdateFiles();
8b17ba72
RR
446}
447
0b855868
RR
448long wxFileCtrl::Add( wxFileData *fd, wxListItem &item )
449{
450 long ret = -1;
451 item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
452 fd->MakeItem( item );
453 long my_style = GetWindowStyleFlag();
454 if (my_style & wxLC_REPORT)
455 {
456 ret = InsertItem( item );
23254b13
JS
457 for (int i = 1; i < wxFileData::FileList_Max; i++)
458 SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) );
0b855868 459 }
06cc1fb9 460 else if ((my_style & wxLC_LIST) || (my_style & wxLC_SMALL_ICON))
0b855868
RR
461 {
462 ret = InsertItem( item );
c8c0e54c 463 }
0b855868
RR
464 return ret;
465}
466
b600ed13
VZ
467void wxFileCtrl::UpdateItem(const wxListItem &item)
468{
469 wxFileData *fd = (wxFileData*)GetItemData(item);
470 wxCHECK_RET(fd, wxT("invalid filedata"));
471
472 fd->ReadData();
473
474 SetItemText(item, fd->GetFileName());
475 SetItemImage(item, fd->GetImageId(), fd->GetImageId());
476
477 if (GetWindowStyleFlag() & wxLC_REPORT)
478 {
479 for (int i = 1; i < wxFileData::FileList_Max; i++)
480 SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) );
481 }
482}
483
8f4fcc4e 484void wxFileCtrl::UpdateFiles()
c8c0e54c 485{
2b5f62a0 486 // don't do anything before ShowModal() call which sets m_dirName
23254b13 487 if ( m_dirName == wxT("*") )
2b5f62a0
VZ
488 return;
489
37fd1c97 490 wxBusyCursor bcur; // this may take a while...
7a82dabc 491
23254b13 492 DeleteAllItems();
7a82dabc 493
8b17ba72 494 wxListItem item;
8b17ba72
RR
495 item.m_itemId = 0;
496 item.m_col = 0;
0b855868 497
144cf6da 498#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)
37fd1c97 499 if ( IsTopMostDir(m_dirName) )
37fd1c97 500 {
06cc1fb9
JS
501 wxArrayString names, paths;
502 wxArrayInt icons;
503 size_t n, count = wxGetAvailableDrives(paths, names, icons);
504
505 for (n=0; n<count; n++)
37fd1c97 506 {
999836aa 507 wxFileData *fd = new wxFileData(paths[n], names[n], wxFileData::is_drive, icons[n]);
81169710 508 if (Add(fd, item) != -1)
37fd1c97 509 item.m_itemId++;
81169710
VZ
510 else
511 delete fd;
37fd1c97
VS
512 }
513 }
5e673a6a 514 else
06cc1fb9 515#endif // defined(__DOS__) || defined(__WINDOWS__)
8b17ba72 516 {
5e673a6a
VS
517 // Real directory...
518 if ( !IsTopMostDir(m_dirName) )
8b17ba72 519 {
5e673a6a 520 wxString p(wxPathOnly(m_dirName));
144cf6da 521#if defined(__UNIX__) && !defined(__OS2__)
5e673a6a 522 if (p.IsEmpty()) p = wxT("/");
06cc1fb9 523#endif // __UNIX__
999836aa 524 wxFileData *fd = new wxFileData(p, wxT(".."), wxFileData::is_dir, wxFileIconsTable::folder);
81169710 525 if (Add(fd, item) != -1)
8b17ba72 526 item.m_itemId++;
81169710
VZ
527 else
528 delete fd;
8b17ba72 529 }
c8c0e54c 530
5e673a6a 531 wxString dirname(m_dirName);
144cf6da 532#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
5e673a6a
VS
533 if (dirname.length() == 2 && dirname[1u] == wxT(':'))
534 dirname << wxT('\\');
144cf6da 535#endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
5e673a6a
VS
536 wxDir dir(dirname);
537
538 if ( dir.IsOpened() )
cae5359f 539 {
23254b13
JS
540 wxString dirPrefix(dirname);
541 if (dirPrefix.Last() != wxFILE_SEP_PATH)
542 dirPrefix += wxFILE_SEP_PATH;
543
5e673a6a 544 int hiddenFlag = m_showHidden ? wxDIR_HIDDEN : 0;
7a82dabc 545
5e673a6a
VS
546 bool cont;
547 wxString f;
548
549 // Get the directories first (not matched against wildcards):
550 cont = dir.GetFirst(&f, wxEmptyString, wxDIR_DIRS | hiddenFlag);
551 while (cont)
551adc4b 552 {
999836aa 553 wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_dir, wxFileIconsTable::folder);
81169710 554 if (Add(fd, item) != -1)
551adc4b 555 item.m_itemId++;
81169710
VZ
556 else
557 delete fd;
558
5e673a6a
VS
559 cont = dir.GetNext(&f);
560 }
561
7a82dabc 562 // Tokenize the wildcard string, so we can handle more than 1
5e673a6a
VS
563 // search pattern in a wildcard.
564 wxStringTokenizer tokenWild(m_wild, wxT(";"));
565 while ( tokenWild.HasMoreTokens() )
566 {
7a82dabc 567 cont = dir.GetFirst(&f, tokenWild.GetNextToken(),
5e673a6a
VS
568 wxDIR_FILES | hiddenFlag);
569 while (cont)
570 {
999836aa 571 wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_file, wxFileIconsTable::file);
81169710 572 if (Add(fd, item) != -1)
5e673a6a 573 item.m_itemId++;
81169710
VZ
574 else
575 delete fd;
576
5e673a6a
VS
577 cont = dir.GetNext(&f);
578 }
551adc4b 579 }
cae5359f 580 }
cae5359f 581 }
c8c0e54c 582
06cc1fb9 583 SortItems(m_sort_field, m_sort_foward);
8b17ba72
RR
584}
585
0b855868 586void wxFileCtrl::SetWild( const wxString &wild )
8b17ba72 587{
06cc1fb9
JS
588 if (wild.Find(wxT('|')) != wxNOT_FOUND)
589 return;
590
0b855868 591 m_wild = wild;
8f4fcc4e 592 UpdateFiles();
0b855868
RR
593}
594
595void wxFileCtrl::MakeDir()
596{
5e673a6a 597 wxString new_name( _("NewName") );
0b855868 598 wxString path( m_dirName );
75ec8bd4 599 path += wxFILE_SEP_PATH;
0b855868
RR
600 path += new_name;
601 if (wxFileExists(path))
8b17ba72 602 {
0b855868
RR
603 // try NewName0, NewName1 etc.
604 int i = 0;
c8c0e54c 605 do {
0b855868 606 new_name = _("NewName");
c8c0e54c 607 wxString num;
223d09f6 608 num.Printf( wxT("%d"), i );
c8c0e54c
VZ
609 new_name += num;
610
0b855868 611 path = m_dirName;
75ec8bd4 612 path += wxFILE_SEP_PATH;
0b855868 613 path += new_name;
c8c0e54c
VZ
614 i++;
615 } while (wxFileExists(path));
8b17ba72 616 }
c8c0e54c 617
0b855868 618 wxLogNull log;
c8c0e54c 619 if (!wxMkdir(path))
8b17ba72 620 {
0b855868 621 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 622 dialog.ShowModal();
0b855868 623 return;
8b17ba72 624 }
8b17ba72 625
06cc1fb9 626 wxFileData *fd = new wxFileData( path, new_name, wxFileData::is_dir, wxFileIconsTable::folder );
0b855868
RR
627 wxListItem item;
628 item.m_itemId = 0;
629 item.m_col = 0;
13111b2a 630 long id = Add( fd, item );
c8c0e54c 631
0b855868
RR
632 if (id != -1)
633 {
06cc1fb9 634 SortItems(m_sort_field, m_sort_foward);
c8c0e54c 635 id = FindItem( 0, (long)fd );
0b855868
RR
636 EnsureVisible( id );
637 EditLabel( id );
638 }
81169710
VZ
639 else
640 delete fd;
8b17ba72
RR
641}
642
643void wxFileCtrl::GoToParentDir()
644{
5e673a6a 645 if (!IsTopMostDir(m_dirName))
8b17ba72 646 {
353f41cb 647 size_t len = m_dirName.Len();
083f7497 648 if (wxEndsWithPathSeparator(m_dirName))
353f41cb 649 m_dirName.Remove( len-1, 1 );
c8c0e54c 650 wxString fname( wxFileNameFromPath(m_dirName) );
0b855868 651 m_dirName = wxPathOnly( m_dirName );
144cf6da 652#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
23254b13
JS
653 if (!m_dirName.IsEmpty())
654 {
655 if (m_dirName.Last() == wxT('.'))
656 m_dirName = wxT("");
657 }
658#elif defined(__UNIX__)
7a82dabc 659 if (m_dirName.IsEmpty())
5e673a6a
VS
660 m_dirName = wxT("/");
661#endif
8f4fcc4e 662 UpdateFiles();
13111b2a 663 long id = FindItem( 0, fname );
c8c0e54c
VZ
664 if (id != -1)
665 {
666 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
667 EnsureVisible( id );
668 }
8b17ba72
RR
669 }
670}
671
672void wxFileCtrl::GoToHomeDir()
673{
674 wxString s = wxGetUserHome( wxString() );
5e673a6a 675 GoToDir(s);
8b17ba72
RR
676}
677
678void wxFileCtrl::GoToDir( const wxString &dir )
679{
06cc1fb9
JS
680 if (!wxDirExists(dir)) return;
681
8b17ba72 682 m_dirName = dir;
8f4fcc4e 683 UpdateFiles();
e6daf794
RR
684 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
685 EnsureVisible( 0 );
8b17ba72
RR
686}
687
81169710 688void wxFileCtrl::FreeItemData(wxListItem& item)
8b17ba72 689{
81169710
VZ
690 if ( item.m_data )
691 {
692 wxFileData *fd = (wxFileData*)item.m_data;
693 delete fd;
694
695 item.m_data = 0;
696 }
8b17ba72
RR
697}
698
7a82dabc
VZ
699void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
700{
701 FreeItemData(event.m_item);
702}
703
81169710
VZ
704void wxFileCtrl::OnListDeleteAllItems( wxListEvent &event )
705{
706 FreeAllItemsData();
707}
708
7a82dabc 709void wxFileCtrl::FreeAllItemsData()
12c1b46a
RR
710{
711 wxListItem item;
712 item.m_mask = wxLIST_MASK_DATA;
713
714 item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL );
92da8bde 715 while ( item.m_itemId != -1 )
12c1b46a
RR
716 {
717 GetItem( item );
7a82dabc 718 FreeItemData(item);
12c1b46a
RR
719 item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL );
720 }
721}
722
0b855868 723void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
8b17ba72
RR
724{
725 wxFileData *fd = (wxFileData*)event.m_item.m_data;
0b855868 726 wxASSERT( fd );
c8c0e54c 727
0b855868
RR
728 if ((event.GetLabel().IsEmpty()) ||
729 (event.GetLabel() == _(".")) ||
730 (event.GetLabel() == _("..")) ||
75ec8bd4 731 (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND))
8b17ba72 732 {
0b855868 733 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 734 dialog.ShowModal();
0b855868 735 event.Veto();
c8c0e54c 736 return;
8b17ba72 737 }
c8c0e54c 738
06cc1fb9 739 wxString new_name( wxPathOnly( fd->GetFilePath() ) );
75ec8bd4 740 new_name += wxFILE_SEP_PATH;
0b855868 741 new_name += event.GetLabel();
c8c0e54c 742
0b855868 743 wxLogNull log;
c8c0e54c 744
0b855868 745 if (wxFileExists(new_name))
8b17ba72 746 {
0b855868 747 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 748 dialog.ShowModal();
0b855868 749 event.Veto();
8b17ba72 750 }
c8c0e54c 751
06cc1fb9 752 if (wxRenameFile(fd->GetFilePath(),new_name))
8b17ba72 753 {
0b855868 754 fd->SetNewName( new_name, event.GetLabel() );
c8c0e54c 755 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
b600ed13 756 UpdateItem( event.GetItem() );
0b855868 757 EnsureVisible( event.GetItem() );
8b17ba72
RR
758 }
759 else
760 {
0b855868 761 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 762 dialog.ShowModal();
0b855868 763 event.Veto();
8b17ba72 764 }
8b17ba72
RR
765}
766
23254b13
JS
767void wxFileCtrl::OnListColClick( wxListEvent &event )
768{
769 int col = event.GetColumn();
770
771 switch (col)
772 {
773 case wxFileData::FileList_Name :
06cc1fb9 774 case wxFileData::FileList_Size :
23254b13 775 case wxFileData::FileList_Type :
23254b13
JS
776 case wxFileData::FileList_Time : break;
777 default : return;
778 }
779
780 if ((wxFileData::fileListFieldType)col == m_sort_field)
06cc1fb9 781 m_sort_foward = !m_sort_foward;
23254b13
JS
782 else
783 m_sort_field = (wxFileData::fileListFieldType)col;
784
06cc1fb9 785 SortItems(m_sort_field, m_sort_foward);
23254b13
JS
786}
787
788void wxFileCtrl::SortItems(wxFileData::fileListFieldType field, bool foward)
789{
790 m_sort_field = field;
06cc1fb9
JS
791 m_sort_foward = foward;
792 long sort_dir = foward ? 1 : -1;
23254b13
JS
793
794 switch (m_sort_field)
795 {
796 case wxFileData::FileList_Name :
797 {
06cc1fb9
JS
798 wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataNameCompare, sort_dir);
799 break;
800 }
801 case wxFileData::FileList_Size :
802 {
803 wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataSizeCompare, sort_dir);
23254b13
JS
804 break;
805 }
806 case wxFileData::FileList_Type :
807 {
06cc1fb9 808 wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataTypeCompare, sort_dir);
23254b13
JS
809 break;
810 }
23254b13
JS
811 case wxFileData::FileList_Time :
812 {
06cc1fb9 813 wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataTimeCompare, sort_dir);
23254b13
JS
814 break;
815 }
816 default : break;
817 }
818}
819
7a82dabc
VZ
820wxFileCtrl::~wxFileCtrl()
821{
7a82dabc
VZ
822}
823
8b17ba72 824//-----------------------------------------------------------------------------
23254b13 825// wxGenericFileDialog
8b17ba72
RR
826//-----------------------------------------------------------------------------
827
5e673a6a
VS
828#define ID_LIST_MODE (wxID_FILEDLGG )
829#define ID_REPORT_MODE (wxID_FILEDLGG + 1)
830#define ID_UP_DIR (wxID_FILEDLGG + 5)
831#define ID_PARENT_DIR (wxID_FILEDLGG + 6)
832#define ID_NEW_DIR (wxID_FILEDLGG + 7)
833#define ID_CHOICE (wxID_FILEDLGG + 8)
834#define ID_TEXT (wxID_FILEDLGG + 9)
835#define ID_LIST_CTRL (wxID_FILEDLGG + 10)
836#define ID_ACTIVATED (wxID_FILEDLGG + 11)
837#define ID_CHECK (wxID_FILEDLGG + 12)
8b17ba72 838
f74172ab 839IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog, wxFileDialogBase)
23254b13
JS
840
841BEGIN_EVENT_TABLE(wxGenericFileDialog,wxDialog)
842 EVT_BUTTON(ID_LIST_MODE, wxGenericFileDialog::OnList)
843 EVT_BUTTON(ID_REPORT_MODE, wxGenericFileDialog::OnReport)
844 EVT_BUTTON(ID_UP_DIR, wxGenericFileDialog::OnUp)
845 EVT_BUTTON(ID_PARENT_DIR, wxGenericFileDialog::OnHome)
846 EVT_BUTTON(ID_NEW_DIR, wxGenericFileDialog::OnNew)
847 EVT_BUTTON(wxID_OK, wxGenericFileDialog::OnListOk)
848 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxGenericFileDialog::OnSelected)
849 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxGenericFileDialog::OnActivated)
850 EVT_CHOICE(ID_CHOICE,wxGenericFileDialog::OnChoiceFilter)
851 EVT_TEXT_ENTER(ID_TEXT,wxGenericFileDialog::OnTextEnter)
852 EVT_TEXT(ID_TEXT,wxGenericFileDialog::OnTextChange)
853 EVT_CHECKBOX(ID_CHECK,wxGenericFileDialog::OnCheck)
8b17ba72
RR
854END_EVENT_TABLE()
855
23254b13
JS
856long wxGenericFileDialog::ms_lastViewStyle = wxLC_LIST;
857bool wxGenericFileDialog::ms_lastShowHidden = FALSE;
655cf310 858
23254b13 859wxGenericFileDialog::wxGenericFileDialog(wxWindow *parent,
9cedab37
VZ
860 const wxString& message,
861 const wxString& defaultDir,
862 const wxString& defaultFile,
863 const wxString& wildCard,
864 long style,
865 const wxPoint& pos )
f74172ab 866 :wxFileDialogBase(parent, message, defaultDir, defaultFile, wildCard, style, pos)
8b17ba72 867{
f74172ab
VZ
868 wxDialog::Create( parent, -1, message, pos, wxDefaultSize,
869 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER );
870
eaf40b23
VS
871 if (wxConfig::Get(FALSE))
872 {
7a82dabc 873 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
9cedab37 874 &ms_lastViewStyle);
7a82dabc 875 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
9cedab37 876 &ms_lastShowHidden);
eaf40b23
VS
877 }
878
9cedab37
VZ
879 if (m_dialogStyle == 0)
880 m_dialogStyle = wxOPEN;
7941ba11 881 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
9b00bb16 882 m_dialogStyle |= wxOPEN;
479cd5de 883
353f41cb 884 if ((m_dir.empty()) || (m_dir == wxT(".")))
ac2def68 885 {
75ec8bd4 886 m_dir = wxGetCwd();
ac2def68 887 }
7a82dabc 888
353f41cb 889 size_t len = m_dir.Len();
083f7497 890 if ((len > 1) && (wxEndsWithPathSeparator(m_dir)))
353f41cb
RR
891 m_dir.Remove( len-1, 1 );
892
893 m_path = m_dir;
75ec8bd4 894 m_path += wxFILE_SEP_PATH;
8b17ba72 895 m_path += defaultFile;
655cf310 896 m_filterExtension = wxEmptyString;
c8c0e54c 897
cae5359f 898 // interpret wildcards
b600ed13 899 wxArrayString wildDescriptions, wildFilters;
a8e25474
VZ
900 if ( !ParseWildcard(m_wildCard, wildDescriptions, wildFilters) )
901 {
a9e3ce8f 902 wxFAIL_MSG( wxT("Wrong file type description") );
a8e25474 903 }
b600ed13 904
cae5359f 905 // layout
4f5c180e 906
2b5f62a0 907 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
c8c0e54c 908
8b17ba72 909 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 910
8b17ba72 911 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 912
e6daf794 913 wxBitmapButton *but;
c8c0e54c 914
7a82dabc 915 but = new wxBitmapButton(this, ID_LIST_MODE,
60d2cc25 916 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_CMN_DIALOG));
e6daf794
RR
917#if wxUSE_TOOLTIPS
918 but->SetToolTip( _("View files as a list view") );
919#endif
920 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 921
60d2cc25
VS
922 but = new wxBitmapButton(this, ID_REPORT_MODE,
923 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_CMN_DIALOG));
e6daf794
RR
924#if wxUSE_TOOLTIPS
925 but->SetToolTip( _("View files as a detailed view") );
926#endif
927 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 928
0b855868 929 buttonsizer->Add( 30, 5, 1 );
e6daf794 930
06cc1fb9 931 m_upDirButton = new wxBitmapButton(this, ID_UP_DIR,
60d2cc25 932 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_CMN_DIALOG));
e6daf794 933#if wxUSE_TOOLTIPS
06cc1fb9 934 m_upDirButton->SetToolTip( _("Go to parent directory") );
e6daf794 935#endif
06cc1fb9 936 buttonsizer->Add( m_upDirButton, 0, wxALL, 5 );
c8c0e54c 937
37fd1c97 938#ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
60d2cc25
VS
939 but = new wxBitmapButton(this, ID_PARENT_DIR,
940 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_CMN_DIALOG));
e6daf794
RR
941#if wxUSE_TOOLTIPS
942 but->SetToolTip( _("Go to home directory") );
943#endif
944 buttonsizer->Add( but, 0, wxALL, 5);
c8c0e54c 945
e6daf794 946 buttonsizer->Add( 20, 20 );
75ec8bd4 947#endif //!__DOS__
c8c0e54c 948
06cc1fb9 949 m_newDirButton = new wxBitmapButton(this, ID_NEW_DIR,
60d2cc25 950 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_CMN_DIALOG));
e6daf794 951#if wxUSE_TOOLTIPS
06cc1fb9 952 m_newDirButton->SetToolTip( _("Create new directory") );
e6daf794 953#endif
06cc1fb9 954 buttonsizer->Add( m_newDirButton, 0, wxALL, 5 );
c8c0e54c 955
2b5f62a0 956 if (is_pda)
c15521c6
RR
957 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
958 else
959 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
c8c0e54c 960
9c884972 961 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
2b5f62a0 962 if (is_pda)
c15521c6 963 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
9c884972
RR
964 m_static = new wxStaticText( this, -1, m_dir );
965 staticsizer->Add( m_static, 1 );
966 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
967
cd6b752b 968 long style2 = ms_lastViewStyle | wxSUNKEN_BORDER;
9cedab37 969 if ( !(m_dialogStyle & wxMULTIPLE) )
cd6b752b 970 style2 |= wxLC_SINGLE_SEL;
9cedab37 971
23254b13 972 m_list = new wxFileCtrl( this, ID_LIST_CTRL,
b600ed13 973 wildFilters[0], ms_lastShowHidden,
9cedab37 974 wxDefaultPosition, wxSize(540,200),
cd6b752b 975 style2);
9cedab37 976
2b5f62a0 977 if (is_pda)
c15521c6 978 {
41fecb44
VS
979 // PDAs have a different screen layout
980 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
981
982 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
983 m_choice = new wxChoice( this, ID_CHOICE );
984 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 5 );
985 mainsizer->Add( choicesizer, 0, wxEXPAND );
986
987 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
988 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
989 textsizer->Add( m_text, 1, wxCENTER | wxALL, 5 );
990 mainsizer->Add( textsizer, 0, wxEXPAND );
991
992 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
9cedab37 993 m_check->SetValue( ms_lastShowHidden );
41fecb44
VS
994 textsizer->Add( m_check, 0, wxCENTER|wxALL, 5 );
995
996 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
997 buttonsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxALL, 5 );
998 buttonsizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 5 );
999 mainsizer->Add( buttonsizer, 0, wxALIGN_RIGHT );
c15521c6
RR
1000 }
1001 else
1002 {
41fecb44
VS
1003 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
1004
1005 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1006 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1007 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1008 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1009 mainsizer->Add( textsizer, 0, wxEXPAND );
1010
1011 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1012 m_choice = new wxChoice( this, ID_CHOICE );
1013 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
1014 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
9cedab37 1015 m_check->SetValue( ms_lastShowHidden );
41fecb44
VS
1016 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
1017 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
1018 mainsizer->Add( choicesizer, 0, wxEXPAND );
c15521c6 1019 }
24f932d2 1020
b600ed13 1021 for (size_t n=0; n<wildFilters.GetCount(); n++)
cae5359f 1022 {
b600ed13 1023 m_choice->Append( wildDescriptions[n], (void*) new wxString( wildFilters[n] ) );
cae5359f 1024 }
b600ed13 1025 SetFilterIndex( 0 );
8b17ba72
RR
1026
1027 SetAutoLayout( TRUE );
1028 SetSizer( mainsizer );
c8c0e54c 1029
8b17ba72
RR
1030 mainsizer->Fit( this );
1031 mainsizer->SetSizeHints( this );
48fe8374 1032
8b17ba72 1033 Centre( wxBOTH );
ed58dbea 1034
9cedab37 1035 m_text->SetFocus();
8b17ba72
RR
1036}
1037
23254b13 1038wxGenericFileDialog::~wxGenericFileDialog()
cae5359f 1039{
eaf40b23
VS
1040 if (wxConfig::Get(FALSE))
1041 {
5e673a6a 1042 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
9cedab37 1043 ms_lastViewStyle);
5e673a6a 1044 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
9cedab37 1045 ms_lastShowHidden);
eaf40b23 1046 }
c5ef41d3
VZ
1047
1048 const int count = m_choice->GetCount();
1049 for ( int i = 0; i < count; i++ )
1050 {
1051 delete (wxString *)m_choice->GetClientData(i);
1052 }
cae5359f
RR
1053}
1054
23254b13 1055int wxGenericFileDialog::ShowModal()
9cedab37
VZ
1056{
1057 m_list->GoToDir(m_dir);
06cc1fb9 1058 UpdateControls();
2b5f62a0 1059 m_text->SetValue(m_fileName);
9cedab37
VZ
1060
1061 return wxDialog::ShowModal();
1062}
1063
23254b13 1064void wxGenericFileDialog::DoSetFilterIndex(int filterindex)
2b5f62a0
VZ
1065{
1066 wxString *str = (wxString*) m_choice->GetClientData( filterindex );
1067 m_list->SetWild( *str );
1068 m_filterIndex = filterindex;
1069 if ( str->Left(2) == wxT("*.") )
1070 {
787d22ec
JS
1071 m_filterExtension = str->Mid(1);
1072 if (m_filterExtension == _T(".*"))
2b5f62a0
VZ
1073 m_filterExtension.clear();
1074 }
1075 else
1076 {
1077 m_filterExtension.clear();
1078 }
1079}
1080
23254b13 1081void wxGenericFileDialog::SetFilterIndex( int filterindex )
04ea7f93
RR
1082{
1083 m_choice->SetSelection( filterindex );
2b5f62a0
VZ
1084
1085 DoSetFilterIndex(filterindex);
04ea7f93
RR
1086}
1087
23254b13 1088void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent &event )
cae5359f 1089{
2b5f62a0 1090 DoSetFilterIndex((int)event.GetInt());
cae5359f
RR
1091}
1092
23254b13 1093void wxGenericFileDialog::OnCheck( wxCommandEvent &event )
bf9e3e73 1094{
9cedab37 1095 m_list->ShowHidden( (ms_lastShowHidden = event.GetInt() != 0) );
bf9e3e73
RR
1096}
1097
23254b13 1098void wxGenericFileDialog::OnActivated( wxListEvent &event )
8b17ba72 1099{
e1811a01 1100 HandleAction( event.m_item.m_text );
8b17ba72
RR
1101}
1102
23254b13 1103void wxGenericFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
cae5359f
RR
1104{
1105 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
1106 cevent.SetEventObject( this );
1107 GetEventHandler()->ProcessEvent( cevent );
1108}
1109
df413171
JS
1110static bool ignoreChanges = FALSE;
1111
23254b13 1112void wxGenericFileDialog::OnTextChange( wxCommandEvent &WXUNUSED(event) )
df413171
JS
1113{
1114 if (!ignoreChanges)
1115 {
1116 // Clear selections. Otherwise when the user types in a value they may
1117 // not get the file whose name they typed.
1118 if (m_list->GetSelectedItemCount() > 0)
1119 {
7f04165e 1120 long item = m_list->GetNextItem(-1, wxLIST_NEXT_ALL,
df413171
JS
1121 wxLIST_STATE_SELECTED);
1122 while ( item != -1 )
7f04165e 1123 {
df413171
JS
1124 m_list->SetItemState(item,0, wxLIST_STATE_SELECTED);
1125 item = m_list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
7f04165e 1126 }
df413171
JS
1127 }
1128 }
1129}
1130
23254b13 1131void wxGenericFileDialog::OnSelected( wxListEvent &event )
8b17ba72 1132{
e1811a01 1133 wxString filename( event.m_item.m_text );
223d09f6 1134 if (filename == wxT("..")) return;
479cd5de 1135
06cc1fb9 1136 wxString dir = m_list->GetDir();
5e673a6a
VS
1137 if (!IsTopMostDir(dir))
1138 dir += wxFILE_SEP_PATH;
e1811a01
RR
1139 dir += filename;
1140 if (wxDirExists(dir)) return;
479cd5de 1141
df413171 1142 ignoreChanges = TRUE;
e1811a01 1143 m_text->SetValue( filename );
df413171 1144 ignoreChanges = FALSE;
8b17ba72
RR
1145}
1146
23254b13 1147void wxGenericFileDialog::HandleAction( const wxString &fn )
8b17ba72 1148{
e1811a01 1149 wxString filename( fn );
06cc1fb9 1150 wxString dir = m_list->GetDir();
8b17ba72 1151 if (filename.IsEmpty()) return;
223d09f6 1152 if (filename == wxT(".")) return;
c8c0e54c 1153
b600ed13
VZ
1154 // "some/place/" means they want to chdir not try to load "place"
1155 bool want_dir = filename.Last() == wxFILE_SEP_PATH;
1156 if (want_dir)
1157 filename = filename.RemoveLast();
1158
223d09f6 1159 if (filename == wxT(".."))
0b855868
RR
1160 {
1161 m_list->GoToParentDir();
1162 m_list->SetFocus();
06cc1fb9 1163 UpdateControls();
c8c0e54c 1164 return;
0b855868
RR
1165 }
1166
75ec8bd4 1167#ifdef __UNIX__
223d09f6 1168 if (filename == wxT("~"))
ed58dbea
RR
1169 {
1170 m_list->GoToHomeDir();
1171 m_list->SetFocus();
06cc1fb9 1172 UpdateControls();
c8c0e54c 1173 return;
ed58dbea 1174 }
c8c0e54c 1175
b600ed13 1176 if (filename.BeforeFirst(wxT('/')) == wxT("~"))
ed58dbea 1177 {
b600ed13 1178 filename = wxGetUserHome() + filename.Remove(0, 1);
ed58dbea 1179 }
75ec8bd4 1180#endif // __UNIX__
ed58dbea 1181
223d09f6
KB
1182 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
1183 (filename.Find(wxT('?')) != wxNOT_FOUND))
cae5359f 1184 {
75ec8bd4 1185 if (filename.Find(wxFILE_SEP_PATH) != wxNOT_FOUND)
c8c0e54c
VZ
1186 {
1187 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
1188 return;
1189 }
1190 m_list->SetWild( filename );
1191 return;
cae5359f
RR
1192 }
1193
5e673a6a
VS
1194 if (!IsTopMostDir(dir))
1195 dir += wxFILE_SEP_PATH;
1196 if (!wxIsAbsolutePath(filename))
ed58dbea
RR
1197 {
1198 dir += filename;
1199 filename = dir;
1200 }
c8c0e54c 1201
8b17ba72
RR
1202 if (wxDirExists(filename))
1203 {
1204 m_list->GoToDir( filename );
06cc1fb9 1205 UpdateControls();
c8c0e54c 1206 return;
8b17ba72 1207 }
c8c0e54c 1208
b600ed13
VZ
1209 // they really wanted a dir, but it doesn't exist
1210 if (want_dir)
1211 {
1212 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1213 wxOK | wxICON_ERROR );
1214 return;
1215 }
1216
2b5f62a0
VZ
1217 // append the default extension to the filename if it doesn't have any
1218 //
1219 // VZ: the logic of testing for !wxFileExists() only for the open file
1220 // dialog is not entirely clear to me, why don't we allow saving to a
1221 // file without extension as well?
1222 if ( !(m_dialogStyle & wxOPEN) || !wxFileExists(filename) )
8b17ba72 1223 {
f74172ab 1224 filename = AppendExtension(filename, m_filterExtension);
8b17ba72 1225 }
2b5f62a0
VZ
1226
1227 // check that the file [doesn't] exist if necessary
1228 if ( (m_dialogStyle & wxSAVE) &&
1229 (m_dialogStyle & wxOVERWRITE_PROMPT) &&
1230 wxFileExists( filename ) )
8b17ba72 1231 {
2b5f62a0 1232 wxString msg;
88d6c988 1233 msg.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename.c_str() );
655cf310 1234
2b5f62a0
VZ
1235 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1236 return;
1237 }
1238 else if ( (m_dialogStyle & wxOPEN) &&
1239 (m_dialogStyle & wxFILE_MUST_EXIST) &&
1240 !wxFileExists(filename) )
1241 {
1242 wxMessageBox(_("Please choose an existing file."), _("Error"),
1243 wxOK | wxICON_ERROR );
8b17ba72
RR
1244 }
1245
1246 SetPath( filename );
479cd5de 1247
3f6638b8 1248 // change to the directory where the user went if asked
fda09b3f 1249 if ( m_dialogStyle & wxCHANGE_DIR )
3f6638b8
VZ
1250 {
1251 wxString cwd;
1252 wxSplitPath(filename, &cwd, NULL, NULL);
1253
0b98a252 1254 if ( cwd != wxGetCwd() )
3f6638b8
VZ
1255 {
1256 wxSetWorkingDirectory(cwd);
1257 }
1258 }
1259
9b7e522a
RR
1260 wxCommandEvent event;
1261 wxDialog::OnOK(event);
e1811a01
RR
1262}
1263
23254b13 1264void wxGenericFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
e1811a01
RR
1265{
1266 HandleAction( m_text->GetValue() );
8b17ba72
RR
1267}
1268
23254b13 1269void wxGenericFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
8b17ba72
RR
1270{
1271 m_list->ChangeToListMode();
9cedab37 1272 ms_lastViewStyle = wxLC_LIST;
0b855868 1273 m_list->SetFocus();
8b17ba72
RR
1274}
1275
23254b13 1276void wxGenericFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
8b17ba72
RR
1277{
1278 m_list->ChangeToReportMode();
9cedab37 1279 ms_lastViewStyle = wxLC_REPORT;
0b855868 1280 m_list->SetFocus();
8b17ba72
RR
1281}
1282
23254b13 1283void wxGenericFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
8b17ba72
RR
1284{
1285 m_list->GoToParentDir();
0b855868 1286 m_list->SetFocus();
06cc1fb9 1287 UpdateControls();
8b17ba72
RR
1288}
1289
23254b13 1290void wxGenericFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
8b17ba72
RR
1291{
1292 m_list->GoToHomeDir();
0b855868 1293 m_list->SetFocus();
06cc1fb9 1294 UpdateControls();
0b855868
RR
1295}
1296
23254b13 1297void wxGenericFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
0b855868
RR
1298{
1299 m_list->MakeDir();
8b17ba72
RR
1300}
1301
23254b13 1302void wxGenericFileDialog::SetPath( const wxString& path )
8b17ba72
RR
1303{
1304 // not only set the full path but also update filename and dir
1305 m_path = path;
9cedab37 1306 if ( !path.empty() )
8b17ba72
RR
1307 {
1308 wxString ext;
1309 wxSplitPath(path, &m_dir, &m_fileName, &ext);
9cedab37 1310 if (!ext.empty())
c8c0e54c 1311 {
223d09f6 1312 m_fileName += wxT(".");
8b17ba72 1313 m_fileName += ext;
c8c0e54c 1314 }
8b17ba72
RR
1315 }
1316}
c8c0e54c 1317
23254b13 1318void wxGenericFileDialog::GetPaths( wxArrayString& paths ) const
7941ba11
RR
1319{
1320 paths.Empty();
28c9c76e
RR
1321 if (m_list->GetSelectedItemCount() == 0)
1322 {
1323 paths.Add( GetPath() );
1324 return;
1325 }
479cd5de 1326
7941ba11
RR
1327 paths.Alloc( m_list->GetSelectedItemCount() );
1328
06cc1fb9 1329 wxString dir = m_list->GetDir();
5e673a6a 1330#ifdef __UNIX__
7a82dabc 1331 if (dir != wxT("/"))
5e673a6a
VS
1332#endif
1333 dir += wxFILE_SEP_PATH;
7941ba11
RR
1334
1335 wxListItem item;
1336 item.m_mask = wxLIST_MASK_TEXT;
1337
1338 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
92da8bde 1339 while ( item.m_itemId != -1 )
68df5777 1340 {
7941ba11
RR
1341 m_list->GetItem( item );
1342 paths.Add( dir + item.m_text );
68df5777 1343 item.m_itemId = m_list->GetNextItem( item.m_itemId,
7941ba11
RR
1344 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1345 }
1346}
1347
23254b13 1348void wxGenericFileDialog::GetFilenames(wxArrayString& files) const
7941ba11
RR
1349{
1350 files.Empty();
28c9c76e
RR
1351 if (m_list->GetSelectedItemCount() == 0)
1352 {
1353 files.Add( GetFilename() );
1354 return;
1355 }
7941ba11
RR
1356 files.Alloc( m_list->GetSelectedItemCount() );
1357
1358 wxListItem item;
1359 item.m_mask = wxLIST_MASK_TEXT;
1360
1361 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
92da8bde 1362 while ( item.m_itemId != -1 )
68df5777 1363 {
7941ba11
RR
1364 m_list->GetItem( item );
1365 files.Add( item.m_text );
68df5777 1366 item.m_itemId = m_list->GetNextItem( item.m_itemId,
7941ba11
RR
1367 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1368 }
1369}
1370
06cc1fb9
JS
1371void wxGenericFileDialog::UpdateControls()
1372{
1373 wxString dir = m_list->GetDir();
1374 m_static->SetLabel(dir);
1375
1376 bool enable = !IsTopMostDir(dir);
1377 m_upDirButton->Enable(enable);
1378
144cf6da 1379#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
06cc1fb9 1380 m_newDirButton->Enable(enable);
144cf6da 1381#endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
06cc1fb9
JS
1382}
1383
23254b13 1384#ifdef USE_GENERIC_FILEDIALOG
655cf310 1385
23254b13 1386IMPLEMENT_DYNAMIC_CLASS(wxFileDialog, wxGenericFileDialog);
655cf310 1387
23254b13
JS
1388#endif // USE_GENERIC_FILEDIALOG
1389
1e6feb95 1390#endif // wxUSE_FILEDLG
9cedab37 1391