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