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