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