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