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