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