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