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