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