added default ctor and fixed copying of wxFileData (patch 1149384)
[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__)
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 wxGenericFileDialog::wxGenericFileDialog(wxWindow *parent,
882 const wxString& message,
883 const wxString& defaultDir,
884 const wxString& defaultFile,
885 const wxString& wildCard,
886 long style,
887 const wxPoint& pos,
888 bool bypassGenericImpl )
889 :wxFileDialogBase(parent, message, defaultDir, defaultFile, wildCard, style, pos)
890 {
891 m_bypassGenericImpl = bypassGenericImpl;
892
893 if (!m_bypassGenericImpl)
894 Create( parent, message, defaultDir, defaultFile, wildCard, style, pos );
895 }
896
897 bool wxGenericFileDialog::Create( wxWindow *parent,
898 const wxString& message,
899 const wxString& WXUNUSED(defaultDir),
900 const wxString& defaultFile,
901 const wxString& wildCard,
902 long WXUNUSED(style),
903 const wxPoint& pos )
904 {
905 if (!wxDialog::Create( parent, wxID_ANY, message, pos, wxDefaultSize,
906 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER ))
907 {
908 return false;
909 }
910
911 if (wxConfig::Get(false))
912 {
913 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
914 &ms_lastViewStyle);
915 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
916 &ms_lastShowHidden);
917 }
918
919 if (m_dialogStyle == 0)
920 m_dialogStyle = wxOPEN;
921 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
922 m_dialogStyle |= wxOPEN;
923
924 if ((m_dir.empty()) || (m_dir == wxT(".")))
925 {
926 m_dir = wxGetCwd();
927 }
928
929 size_t len = m_dir.Len();
930 if ((len > 1) && (wxEndsWithPathSeparator(m_dir)))
931 m_dir.Remove( len-1, 1 );
932
933 m_path = m_dir;
934 m_path += wxFILE_SEP_PATH;
935 m_path += defaultFile;
936 m_filterExtension = wxEmptyString;
937
938 // layout
939
940 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
941
942 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
943
944 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
945
946 wxBitmapButton *but;
947
948 but = new wxBitmapButton(this, ID_LIST_MODE,
949 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_BUTTON));
950 #if wxUSE_TOOLTIPS
951 but->SetToolTip( _("View files as a list view") );
952 #endif
953 buttonsizer->Add( but, 0, wxALL, 5 );
954
955 but = new wxBitmapButton(this, ID_REPORT_MODE,
956 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_BUTTON));
957 #if wxUSE_TOOLTIPS
958 but->SetToolTip( _("View files as a detailed view") );
959 #endif
960 buttonsizer->Add( but, 0, wxALL, 5 );
961
962 buttonsizer->Add( 30, 5, 1 );
963
964 m_upDirButton = new wxBitmapButton(this, ID_UP_DIR,
965 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_BUTTON));
966 #if wxUSE_TOOLTIPS
967 m_upDirButton->SetToolTip( _("Go to parent directory") );
968 #endif
969 buttonsizer->Add( m_upDirButton, 0, wxALL, 5 );
970
971 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
972 but = new wxBitmapButton(this, ID_PARENT_DIR,
973 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_BUTTON));
974 #if wxUSE_TOOLTIPS
975 but->SetToolTip( _("Go to home directory") );
976 #endif
977 buttonsizer->Add( but, 0, wxALL, 5);
978
979 buttonsizer->Add( 20, 20 );
980 #endif //!__DOS__
981
982 m_newDirButton = new wxBitmapButton(this, ID_NEW_DIR,
983 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_BUTTON));
984 #if wxUSE_TOOLTIPS
985 m_newDirButton->SetToolTip( _("Create new directory") );
986 #endif
987 buttonsizer->Add( m_newDirButton, 0, wxALL, 5 );
988
989 if (is_pda)
990 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
991 else
992 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
993
994 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
995 if (is_pda)
996 staticsizer->Add( new wxStaticText( this, wxID_ANY, _("Current directory:") ), 0, wxRIGHT, 10 );
997 m_static = new wxStaticText( this, wxID_ANY, m_dir );
998 staticsizer->Add( m_static, 1 );
999 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
1000
1001 long style2 = ms_lastViewStyle | wxSUNKEN_BORDER;
1002 if ( !(m_dialogStyle & wxMULTIPLE) )
1003 style2 |= wxLC_SINGLE_SEL;
1004
1005 m_list = new wxFileCtrl( this, ID_LIST_CTRL,
1006 wxEmptyString, ms_lastShowHidden,
1007 wxDefaultPosition, wxSize(540,200),
1008 style2);
1009
1010 if (is_pda)
1011 {
1012 // PDAs have a different screen layout
1013 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
1014
1015 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1016 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1017 textsizer->Add( m_text, 1, wxCENTER | wxALL, 5 );
1018 mainsizer->Add( textsizer, 0, wxEXPAND );
1019
1020 m_check = NULL;
1021 m_choice = new wxChoice( this, ID_CHOICE );
1022 textsizer->Add( m_choice, 1, wxCENTER|wxALL, 5 );
1023
1024 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
1025 buttonsizer->Add( new wxButton( this, wxID_OK ), 0, wxCENTER | wxALL, 5 );
1026 buttonsizer->Add( new wxButton( this, wxID_CANCEL ), 0, wxCENTER | wxALL, 5 );
1027 mainsizer->Add( buttonsizer, 0, wxALIGN_RIGHT );
1028 }
1029 else
1030 {
1031 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
1032
1033 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1034 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1035 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1036 textsizer->Add( new wxButton( this, wxID_OK ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1037 mainsizer->Add( textsizer, 0, wxEXPAND );
1038
1039 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1040 m_choice = new wxChoice( this, ID_CHOICE );
1041 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
1042 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
1043 m_check->SetValue( ms_lastShowHidden );
1044 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
1045 choicesizer->Add( new wxButton( this, wxID_CANCEL ), 0, wxCENTER | wxALL, 10 );
1046 mainsizer->Add( choicesizer, 0, wxEXPAND );
1047 }
1048
1049 SetWildcard(wildCard);
1050
1051 SetAutoLayout( true );
1052 SetSizer( mainsizer );
1053
1054 mainsizer->Fit( this );
1055 mainsizer->SetSizeHints( this );
1056
1057 Centre( wxBOTH );
1058
1059 m_text->SetFocus();
1060
1061 return true;
1062 }
1063
1064 wxGenericFileDialog::~wxGenericFileDialog()
1065 {
1066 if (!m_bypassGenericImpl)
1067 {
1068 if (wxConfig::Get(false))
1069 {
1070 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1071 ms_lastViewStyle);
1072 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1073 ms_lastShowHidden);
1074 }
1075
1076 const int count = m_choice->GetCount();
1077 for ( int i = 0; i < count; i++ )
1078 {
1079 delete (wxString *)m_choice->GetClientData(i);
1080 }
1081 }
1082 }
1083
1084 int wxGenericFileDialog::ShowModal()
1085 {
1086 m_list->GoToDir(m_dir);
1087 UpdateControls();
1088 m_text->SetValue(m_fileName);
1089
1090 return wxDialog::ShowModal();
1091 }
1092
1093 bool wxGenericFileDialog::Show( bool show )
1094 {
1095 if (show)
1096 {
1097 m_list->GoToDir(m_dir);
1098 UpdateControls();
1099 m_text->SetValue(m_fileName);
1100 }
1101
1102 return wxDialog::Show( show );
1103 }
1104
1105 void wxGenericFileDialog::DoSetFilterIndex(int filterindex)
1106 {
1107 wxString *str = (wxString*) m_choice->GetClientData( filterindex );
1108 m_list->SetWild( *str );
1109 m_filterIndex = filterindex;
1110 if ( str->Left(2) == wxT("*.") )
1111 {
1112 m_filterExtension = str->Mid(1);
1113 if (m_filterExtension == _T(".*"))
1114 m_filterExtension.clear();
1115 }
1116 else
1117 {
1118 m_filterExtension.clear();
1119 }
1120 }
1121
1122 void wxGenericFileDialog::SetWildcard(const wxString& wildCard)
1123 {
1124 wxFileDialogBase::SetWildcard(wildCard);
1125
1126 wxArrayString wildDescriptions, wildFilters;
1127 const size_t count = wxParseCommonDialogsFilter(m_wildCard,
1128 wildDescriptions,
1129 wildFilters);
1130 wxCHECK_RET( count, wxT("wxFileDialog: bad wildcard string") );
1131
1132 m_choice->Clear();
1133 for ( size_t n = 0; n < count; n++ )
1134 {
1135 m_choice->Append( wildDescriptions[n], new wxString( wildFilters[n] ) );
1136 }
1137
1138 SetFilterIndex( 0 );
1139 }
1140
1141 void wxGenericFileDialog::SetFilterIndex( int filterindex )
1142 {
1143 m_choice->SetSelection( filterindex );
1144
1145 DoSetFilterIndex(filterindex);
1146 }
1147
1148 void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent &event )
1149 {
1150 DoSetFilterIndex((int)event.GetInt());
1151 }
1152
1153 void wxGenericFileDialog::OnCheck( wxCommandEvent &event )
1154 {
1155 m_list->ShowHidden( (ms_lastShowHidden = event.GetInt() != 0) );
1156 }
1157
1158 void wxGenericFileDialog::OnActivated( wxListEvent &event )
1159 {
1160 HandleAction( event.m_item.m_text );
1161 }
1162
1163 void wxGenericFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
1164 {
1165 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
1166 cevent.SetEventObject( this );
1167 GetEventHandler()->ProcessEvent( cevent );
1168 }
1169
1170 static bool ignoreChanges = false;
1171
1172 void wxGenericFileDialog::OnTextChange( wxCommandEvent &WXUNUSED(event) )
1173 {
1174 if (!ignoreChanges)
1175 {
1176 // Clear selections. Otherwise when the user types in a value they may
1177 // not get the file whose name they typed.
1178 if (m_list->GetSelectedItemCount() > 0)
1179 {
1180 long item = m_list->GetNextItem(-1, wxLIST_NEXT_ALL,
1181 wxLIST_STATE_SELECTED);
1182 while ( item != -1 )
1183 {
1184 m_list->SetItemState(item,0, wxLIST_STATE_SELECTED);
1185 item = m_list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1186 }
1187 }
1188 }
1189 }
1190
1191 void wxGenericFileDialog::OnSelected( wxListEvent &event )
1192 {
1193 wxString filename( event.m_item.m_text );
1194 if (filename == wxT("..")) return;
1195
1196 wxString dir = m_list->GetDir();
1197 if (!IsTopMostDir(dir))
1198 dir += wxFILE_SEP_PATH;
1199 dir += filename;
1200 if (wxDirExists(dir)) return;
1201
1202 ignoreChanges = true;
1203 m_text->SetValue( filename );
1204 ignoreChanges = false;
1205 }
1206
1207 void wxGenericFileDialog::HandleAction( const wxString &fn )
1208 {
1209 wxString filename( fn );
1210 wxString dir = m_list->GetDir();
1211 if (filename.empty()) return;
1212 if (filename == wxT(".")) return;
1213
1214 // "some/place/" means they want to chdir not try to load "place"
1215 bool want_dir = filename.Last() == wxFILE_SEP_PATH;
1216 if (want_dir)
1217 filename = filename.RemoveLast();
1218
1219 if (filename == wxT(".."))
1220 {
1221 m_list->GoToParentDir();
1222 m_list->SetFocus();
1223 UpdateControls();
1224 return;
1225 }
1226
1227 #ifdef __UNIX__
1228 if (filename == wxT("~"))
1229 {
1230 m_list->GoToHomeDir();
1231 m_list->SetFocus();
1232 UpdateControls();
1233 return;
1234 }
1235
1236 if (filename.BeforeFirst(wxT('/')) == wxT("~"))
1237 {
1238 filename = wxString(wxGetUserHome()) + filename.Remove(0, 1);
1239 }
1240 #endif // __UNIX__
1241
1242 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
1243 (filename.Find(wxT('?')) != wxNOT_FOUND))
1244 {
1245 if (filename.Find(wxFILE_SEP_PATH) != wxNOT_FOUND)
1246 {
1247 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
1248 return;
1249 }
1250 m_list->SetWild( filename );
1251 return;
1252 }
1253
1254 if (!IsTopMostDir(dir))
1255 dir += wxFILE_SEP_PATH;
1256 if (!wxIsAbsolutePath(filename))
1257 {
1258 dir += filename;
1259 filename = dir;
1260 }
1261
1262 if (wxDirExists(filename))
1263 {
1264 m_list->GoToDir( filename );
1265 UpdateControls();
1266 return;
1267 }
1268
1269 // they really wanted a dir, but it doesn't exist
1270 if (want_dir)
1271 {
1272 wxMessageBox(_("Directory doesn't exist."), _("Error"),
1273 wxOK | wxICON_ERROR );
1274 return;
1275 }
1276
1277 // append the default extension to the filename if it doesn't have any
1278 //
1279 // VZ: the logic of testing for !wxFileExists() only for the open file
1280 // dialog is not entirely clear to me, why don't we allow saving to a
1281 // file without extension as well?
1282 if ( !(m_dialogStyle & wxOPEN) || !wxFileExists(filename) )
1283 {
1284 filename = AppendExtension(filename, m_filterExtension);
1285 }
1286
1287 // check that the file [doesn't] exist if necessary
1288 if ( (m_dialogStyle & wxSAVE) &&
1289 (m_dialogStyle & wxOVERWRITE_PROMPT) &&
1290 wxFileExists( filename ) )
1291 {
1292 wxString msg;
1293 msg.Printf( _("File '%s' already exists, do you really want to overwrite it?"), filename.c_str() );
1294
1295 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1296 return;
1297 }
1298 else if ( (m_dialogStyle & wxOPEN) &&
1299 (m_dialogStyle & wxFILE_MUST_EXIST) &&
1300 !wxFileExists(filename) )
1301 {
1302 wxMessageBox(_("Please choose an existing file."), _("Error"),
1303 wxOK | wxICON_ERROR );
1304 }
1305
1306 SetPath( filename );
1307
1308 // change to the directory where the user went if asked
1309 if ( m_dialogStyle & wxCHANGE_DIR )
1310 {
1311 wxString cwd;
1312 wxSplitPath(filename, &cwd, NULL, NULL);
1313
1314 if ( cwd != wxGetCwd() )
1315 {
1316 wxSetWorkingDirectory(cwd);
1317 }
1318 }
1319
1320 wxCommandEvent event;
1321 wxDialog::OnOK(event);
1322 }
1323
1324 void wxGenericFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
1325 {
1326 HandleAction( m_text->GetValue() );
1327 }
1328
1329 void wxGenericFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
1330 {
1331 m_list->ChangeToListMode();
1332 ms_lastViewStyle = wxLC_LIST;
1333 m_list->SetFocus();
1334 }
1335
1336 void wxGenericFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
1337 {
1338 m_list->ChangeToReportMode();
1339 ms_lastViewStyle = wxLC_REPORT;
1340 m_list->SetFocus();
1341 }
1342
1343 void wxGenericFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
1344 {
1345 m_list->GoToParentDir();
1346 m_list->SetFocus();
1347 UpdateControls();
1348 }
1349
1350 void wxGenericFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
1351 {
1352 m_list->GoToHomeDir();
1353 m_list->SetFocus();
1354 UpdateControls();
1355 }
1356
1357 void wxGenericFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
1358 {
1359 m_list->MakeDir();
1360 }
1361
1362 void wxGenericFileDialog::SetPath( const wxString& path )
1363 {
1364 // not only set the full path but also update filename and dir
1365 m_path = path;
1366 if ( !path.empty() )
1367 {
1368 wxString ext;
1369 wxSplitPath(path, &m_dir, &m_fileName, &ext);
1370 if (!ext.empty())
1371 {
1372 m_fileName += wxT(".");
1373 m_fileName += ext;
1374 }
1375 }
1376 }
1377
1378 void wxGenericFileDialog::GetPaths( wxArrayString& paths ) const
1379 {
1380 paths.Empty();
1381 if (m_list->GetSelectedItemCount() == 0)
1382 {
1383 paths.Add( GetPath() );
1384 return;
1385 }
1386
1387 paths.Alloc( m_list->GetSelectedItemCount() );
1388
1389 wxString dir = m_list->GetDir();
1390 #ifdef __UNIX__
1391 if (dir != wxT("/"))
1392 #endif
1393 dir += wxFILE_SEP_PATH;
1394
1395 wxListItem item;
1396 item.m_mask = wxLIST_MASK_TEXT;
1397
1398 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1399 while ( item.m_itemId != -1 )
1400 {
1401 m_list->GetItem( item );
1402 paths.Add( dir + item.m_text );
1403 item.m_itemId = m_list->GetNextItem( item.m_itemId,
1404 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1405 }
1406 }
1407
1408 void wxGenericFileDialog::GetFilenames(wxArrayString& files) const
1409 {
1410 files.Empty();
1411 if (m_list->GetSelectedItemCount() == 0)
1412 {
1413 files.Add( GetFilename() );
1414 return;
1415 }
1416 files.Alloc( m_list->GetSelectedItemCount() );
1417
1418 wxListItem item;
1419 item.m_mask = wxLIST_MASK_TEXT;
1420
1421 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1422 while ( item.m_itemId != -1 )
1423 {
1424 m_list->GetItem( item );
1425 files.Add( item.m_text );
1426 item.m_itemId = m_list->GetNextItem( item.m_itemId,
1427 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1428 }
1429 }
1430
1431 void wxGenericFileDialog::UpdateControls()
1432 {
1433 wxString dir = m_list->GetDir();
1434 m_static->SetLabel(dir);
1435
1436 bool enable = !IsTopMostDir(dir);
1437 m_upDirButton->Enable(enable);
1438
1439 #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1440 m_newDirButton->Enable(enable);
1441 #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
1442 }
1443
1444 #ifdef USE_GENERIC_FILEDIALOG
1445
1446 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog, wxGenericFileDialog);
1447
1448 #endif // USE_GENERIC_FILEDIALOG
1449
1450 #endif // wxUSE_FILEDLG
1451