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