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