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