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