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