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