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