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