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