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