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