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