]> git.saurik.com Git - wxWidgets.git/blob - src/generic/filedlgg.cpp
Applied patch [ 649590 ] fixes bug 611264: wxFileDialog entry
[wxWidgets.git] / src / generic / filedlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: filedlgg.cpp
3 // Purpose: wxFileDialog
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 #if !defined(__UNIX__) && !defined(__DOS__)
34 #error wxFileDialog currently only supports Unix and DOS
35 #endif
36
37 #include "wx/checkbox.h"
38 #include "wx/textctrl.h"
39 #include "wx/choice.h"
40 #include "wx/checkbox.h"
41 #include "wx/stattext.h"
42 #include "wx/filedlg.h"
43 #include "wx/debug.h"
44 #include "wx/log.h"
45 #include "wx/intl.h"
46 #include "wx/listctrl.h"
47 #include "wx/msgdlg.h"
48 #include "wx/sizer.h"
49 #include "wx/bmpbuttn.h"
50 #include "wx/tokenzr.h"
51 #include "wx/mimetype.h"
52 #include "wx/image.h"
53 #include "wx/module.h"
54 #include "wx/config.h"
55 #include "wx/imaglist.h"
56 #include "wx/dir.h"
57 #include "wx/artprov.h"
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 #include <unistd.h>
80
81 // ----------------------------------------------------------------------------
82 // constants
83 // ----------------------------------------------------------------------------
84
85 // the list ctrl fields in report view
86 enum FileListField
87 {
88 FileList_Name,
89 FileList_Type,
90 FileList_Date,
91 FileList_Time,
92 #ifdef __UNIX__
93 FileList_Perm,
94 #endif // __UNIX__
95 FileList_Max
96 };
97
98 //-----------------------------------------------------------------------------
99 // wxFileData
100 //-----------------------------------------------------------------------------
101
102 class wxFileData
103 {
104 public:
105 wxFileData( const wxString &name, const wxString &fname );
106 wxString GetName() const;
107 wxString GetFullName() const;
108 wxString GetHint() const;
109 wxString GetEntry( FileListField num ) const;
110
111 bool IsDir() const { return m_isDir; }
112 bool IsLink() const { return m_isLink; }
113 bool IsExe() const { return m_isExe; }
114 long GetSize() const { return m_size; }
115
116 void MakeItem( wxListItem &item );
117 void SetNewName( const wxString &name, const wxString &fname );
118
119 private:
120 wxString m_name;
121 wxString m_fileName;
122 long m_size;
123 int m_hour;
124 int m_minute;
125 int m_year;
126 int m_month;
127 int m_day;
128 wxString m_permissions;
129 bool m_isDir;
130 bool m_isLink;
131 bool m_isExe;
132 };
133
134 //-----------------------------------------------------------------------------
135 // wxFileCtrl
136 //-----------------------------------------------------------------------------
137
138 class wxFileCtrl : public wxListCtrl
139 {
140 public:
141 wxFileCtrl();
142 wxFileCtrl( wxWindow *win,
143 wxStaticText *labelDir,
144 wxWindowID id,
145 const wxString &wild,
146 bool showHidden,
147 const wxPoint &pos = wxDefaultPosition,
148 const wxSize &size = wxDefaultSize,
149 long style = wxLC_LIST,
150 const wxValidator &validator = wxDefaultValidator,
151 const wxString &name = wxT("filelist") );
152 virtual ~wxFileCtrl();
153
154 void ChangeToListMode();
155 void ChangeToReportMode();
156 void ChangeToIconMode();
157 void ShowHidden( bool show = TRUE );
158 long Add( wxFileData *fd, wxListItem &item );
159 void UpdateFiles();
160 virtual void StatusbarText( wxChar *WXUNUSED(text) ) {};
161 void MakeDir();
162 void GoToParentDir();
163 void GoToHomeDir();
164 void GoToDir( const wxString &dir );
165 void SetWild( const wxString &wild );
166 void GetDir( wxString &dir );
167 void OnListDeleteItem( wxListEvent &event );
168 void OnListEndLabelEdit( wxListEvent &event );
169
170 // Associate commonly used UI controls with wxFileCtrl so that they can be
171 // disabled when they cannot be used (e.g. can't go to parent directory
172 // if wxFileCtrl already is in the root dir):
173 void SetGoToParentControl(wxWindow *ctrl) { m_goToParentControl = ctrl; }
174 void SetNewDirControl(wxWindow *ctrl) { m_newDirControl = ctrl; }
175
176 private:
177 void FreeItemData(const wxListItem& item);
178 void FreeAllItemsData();
179
180 wxString m_dirName;
181 bool m_showHidden;
182 wxString m_wild;
183
184 wxWindow *m_goToParentControl;
185 wxWindow *m_newDirControl;
186
187 // the label showing the current directory
188 wxStaticText *m_labelDir;
189
190 DECLARE_DYNAMIC_CLASS(wxFileCtrl);
191 DECLARE_EVENT_TABLE()
192 };
193
194 // ----------------------------------------------------------------------------
195 // private classes - icons list management
196 // ----------------------------------------------------------------------------
197
198 class wxFileIconEntry : public wxObject
199 {
200 public:
201 wxFileIconEntry(int i) { id = i; }
202
203 int id;
204 };
205
206
207 class wxFileIconsTable
208 {
209 public:
210 wxFileIconsTable();
211
212 int GetIconID(const wxString& extension, const wxString& mime = wxEmptyString);
213 wxImageList *GetImageList() { return &m_ImageList; }
214
215 protected:
216 wxImageList m_ImageList;
217 wxHashTable m_HashTable;
218 };
219
220 static wxFileIconsTable *g_IconsTable = NULL;
221
222 #define FI_FOLDER 0
223 #define FI_UNKNOWN 1
224 #define FI_EXECUTABLE 2
225
226 wxFileIconsTable::wxFileIconsTable() :
227 m_ImageList(16, 16),
228 m_HashTable(wxKEY_STRING)
229 {
230 m_HashTable.DeleteContents(TRUE);
231 // FI_FOLDER:
232 m_ImageList.Add(wxArtProvider::GetBitmap(wxART_FOLDER, wxART_CMN_DIALOG));
233 // FI_UNKNOWN:
234 m_ImageList.Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_CMN_DIALOG));
235 // FI_EXECUTABLE:
236 if (GetIconID(wxEmptyString, _T("application/x-executable")) == FI_UNKNOWN)
237 {
238 m_ImageList.Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE, wxART_CMN_DIALOG));
239 m_HashTable.Delete(_T("exe"));
240 m_HashTable.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE));
241 }
242 /* else put into list by GetIconID
243 (KDE defines application/x-executable for *.exe and has nice icon)
244 */
245 }
246
247
248
249 #if wxUSE_MIMETYPE
250 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
251 // one icon and we won't resize it
252
253 static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
254 {
255 wxImage small(16, 16);
256 unsigned char *p1, *p2, *ps;
257 unsigned char mr = img.GetMaskRed(),
258 mg = img.GetMaskGreen(),
259 mb = img.GetMaskBlue();
260
261 unsigned x, y;
262 unsigned sr, sg, sb, smask;
263
264 p1 = img.GetData(), p2 = img.GetData() + 3 * 32, ps = small.GetData();
265 small.SetMaskColour(mr, mr, mr);
266
267 for (y = 0; y < 16; y++)
268 {
269 for (x = 0; x < 16; x++)
270 {
271 sr = sg = sb = smask = 0;
272 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
273 sr += p1[0], sg += p1[1], sb += p1[2];
274 else smask++;
275 p1 += 3;
276 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
277 sr += p1[0], sg += p1[1], sb += p1[2];
278 else smask++;
279 p1 += 3;
280 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
281 sr += p2[0], sg += p2[1], sb += p2[2];
282 else smask++;
283 p2 += 3;
284 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
285 sr += p2[0], sg += p2[1], sb += p2[2];
286 else smask++;
287 p2 += 3;
288
289 if (smask > 2)
290 ps[0] = ps[1] = ps[2] = mr;
291 else
292 ps[0] = sr >> 2, ps[1] = sg >> 2, ps[2] = sb >> 2;
293 ps += 3;
294 }
295 p1 += 32 * 3, p2 += 32 * 3;
296 }
297
298 return wxBitmap(small);
299 }
300
301 // finds empty borders and return non-empty area of image:
302 static wxImage CutEmptyBorders(const wxImage& img)
303 {
304 unsigned char mr = img.GetMaskRed(),
305 mg = img.GetMaskGreen(),
306 mb = img.GetMaskBlue();
307 unsigned char *dt = img.GetData(), *dttmp;
308 unsigned w = img.GetWidth(), h = img.GetHeight();
309
310 unsigned top, bottom, left, right, i;
311 bool empt;
312
313 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
314 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
315
316 for (empt = TRUE, top = 0; empt && top < h; top++)
317 {
318 MK_DTTMP(0, top);
319 for (i = 0; i < w; i++, dttmp+=3)
320 NOEMPTY_PIX(empt)
321 }
322 for (empt = TRUE, bottom = h-1; empt && bottom > top; bottom--)
323 {
324 MK_DTTMP(0, bottom);
325 for (i = 0; i < w; i++, dttmp+=3)
326 NOEMPTY_PIX(empt)
327 }
328 for (empt = TRUE, left = 0; empt && left < w; left++)
329 {
330 MK_DTTMP(left, 0);
331 for (i = 0; i < h; i++, dttmp+=3*w)
332 NOEMPTY_PIX(empt)
333 }
334 for (empt = TRUE, right = w-1; empt && right > left; right--)
335 {
336 MK_DTTMP(right, 0);
337 for (i = 0; i < h; i++, dttmp+=3*w)
338 NOEMPTY_PIX(empt)
339 }
340 top--, left--, bottom++, right++;
341
342 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
343 }
344 #endif // wxUSE_MIMETYPE
345
346
347
348 int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
349 {
350 #if wxUSE_MIMETYPE
351 if (!extension.IsEmpty())
352 {
353 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable.Get(extension);
354 if (entry) return (entry -> id);
355 }
356
357 wxFileType *ft = (mime.IsEmpty()) ?
358 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
359 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
360 wxIcon ic;
361 if (ft == NULL || (!ft -> GetIcon(&ic)) || (!ic.Ok()))
362 {
363 int newid = FI_UNKNOWN;
364 m_HashTable.Put(extension, new wxFileIconEntry(newid));
365 return newid;
366 }
367 wxImage img = ic.ConvertToImage();
368 delete ft;
369
370 int id = m_ImageList.GetImageCount();
371 if (img.GetWidth() == 16 && img.GetHeight() == 16)
372 m_ImageList.Add(wxBitmap(img));
373 else
374 {
375 if (img.GetWidth() != 32 || img.GetHeight() != 32)
376 m_ImageList.Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(32, 32)));
377 else
378 m_ImageList.Add(CreateAntialiasedBitmap(img));
379 }
380 m_HashTable.Put(extension, new wxFileIconEntry(id));
381 return id;
382
383 #else // !wxUSE_MIMETYPE
384
385 if (extension == wxT("exe"))
386 return FI_EXECUTABLE;
387 else
388 return FI_UNKNOWN;
389 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
390 }
391
392
393
394 // ----------------------------------------------------------------------------
395 // private functions
396 // ----------------------------------------------------------------------------
397
398 static
399 int ListCompare( long data1, long data2, long WXUNUSED(data) )
400 {
401 wxFileData *fd1 = (wxFileData*)data1 ;
402 wxFileData *fd2 = (wxFileData*)data2 ;
403 if (fd1->GetName() == wxT("..")) return -1;
404 if (fd2->GetName() == wxT("..")) return 1;
405 if (fd1->IsDir() && !fd2->IsDir()) return -1;
406 if (fd2->IsDir() && !fd1->IsDir()) return 1;
407 return wxStrcmp( fd1->GetName(), fd2->GetName() );
408 }
409
410 #ifdef __UNIX__
411 #define IsTopMostDir(dir) (dir == wxT("/"))
412 #endif
413
414 #if defined(__DOS__) || defined(__WINDOWS__)
415 #define IsTopMostDir(dir) (dir.IsEmpty())
416 #endif
417
418 #if defined(__DOS__) || defined(__WINDOWS__)
419 extern bool wxIsDriveAvailable(const wxString& dirName);
420 #endif
421
422 //-----------------------------------------------------------------------------
423 // wxFileData
424 //-----------------------------------------------------------------------------
425
426 wxFileData::wxFileData( const wxString &name, const wxString &fname )
427 {
428 m_name = name;
429 m_fileName = fname;
430
431 #if defined(__DOS__) || defined(__WINDOWS__)
432 // VS: In case the file is root directory of a volume (e.g. "C:"),
433 // we don't want it stat()ed, since the drive may not be in:
434 if (name.length() == 2 && name[1u] == wxT(':'))
435 {
436 m_isDir = TRUE;
437 m_isExe = m_isLink = FALSE;
438 m_size = 0;
439 return;
440 }
441 #endif
442
443 wxStructStat buff;
444 wxStat( m_fileName, &buff );
445
446 #if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
447 struct stat lbuff;
448 lstat( m_fileName.fn_str(), &lbuff );
449 m_isLink = S_ISLNK( lbuff.st_mode );
450 struct tm *t = localtime( &lbuff.st_mtime );
451 #else
452 m_isLink = FALSE;
453 struct tm *t = localtime( &buff.st_mtime );
454 #endif
455
456 // struct passwd *user = getpwuid( buff.st_uid );
457 // struct group *grp = getgrgid( buff.st_gid );
458
459 m_isDir = S_ISDIR( buff.st_mode );
460 m_isExe = ((buff.st_mode & S_IXUSR ) == S_IXUSR );
461
462 m_size = buff.st_size;
463
464 m_hour = t->tm_hour;
465 m_minute = t->tm_min;
466 m_month = t->tm_mon+1;
467 m_day = t->tm_mday;
468 m_year = t->tm_year;
469 m_year += 1900;
470
471 char buffer[10];
472 sprintf( buffer, "%c%c%c",
473 ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? 'r' : '-'),
474 ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? 'w' : '-'),
475 ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? 'x' : '-') );
476 #if wxUSE_UNICODE
477 m_permissions = wxConvUTF8.cMB2WC( buffer );
478 #else
479 m_permissions = buffer;
480 #endif
481
482 // m_permissions.sprintf( wxT("%c%c%c"),
483 // ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? wxT('r') : wxT('-')),
484 // ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? wxT('w') : wxT('-')),
485 // ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? wxT('x') : wxT('-')) );
486 }
487
488 wxString wxFileData::GetName() const
489 {
490 return m_name;
491 }
492
493 wxString wxFileData::GetFullName() const
494 {
495 return m_fileName;
496 }
497
498 wxString wxFileData::GetHint() const
499 {
500 wxString s = m_fileName;
501 s += wxT(" ");
502 if (m_isDir) s += wxT("<DIR> ");
503 else if (m_isLink) s += wxT("<LINK> ");
504 else
505 {
506 s += LongToString( m_size );
507 s += wxT(" bytes ");
508 }
509 s += IntToString( m_day );
510 s += wxT(".");
511 s += IntToString( m_month );
512 s += wxT(".");
513 s += IntToString( m_year );
514 s += wxT(" ");
515 s += IntToString( m_hour );
516 s += wxT(":");
517 s += IntToString( m_minute );
518 s += wxT(" ");
519 s += m_permissions;
520 return s;
521 };
522
523 wxString wxFileData::GetEntry( FileListField num ) const
524 {
525 wxString s;
526 switch ( num )
527 {
528 case FileList_Name:
529 s = m_name;
530 break;
531
532 case FileList_Type:
533 if (m_isDir)
534 s = _("<DIR>");
535 else if (m_isLink)
536 s = _("<LINK>");
537 else
538 s.Printf(_T("%ld"), m_size);
539 break;
540
541 case FileList_Date:
542 s.Printf(_T("%02d.%02d.%d"), m_day, m_month, m_year);
543 break;
544
545 case FileList_Time:
546 s.Printf(_T("%02d:%02d"), m_hour, m_minute);
547 break;
548
549 #ifdef __UNIX__
550 case FileList_Perm:
551 s = m_permissions;
552 break;
553 #endif // __UNIX__
554
555 default:
556 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
557 }
558
559 return s;
560 }
561
562 void wxFileData::SetNewName( const wxString &name, const wxString &fname )
563 {
564 m_name = name;
565 m_fileName = fname;
566 }
567
568 void wxFileData::MakeItem( wxListItem &item )
569 {
570 item.m_text = m_name;
571 item.ClearAttributes();
572 if (IsExe())
573 item.SetTextColour(*wxRED);
574 if (IsDir())
575 item.SetTextColour(*wxBLUE);
576
577 if (IsDir())
578 item.m_image = FI_FOLDER;
579 else if (IsExe())
580 item.m_image = FI_EXECUTABLE;
581 else if (m_name.Find(wxT('.')) != wxNOT_FOUND)
582 item.m_image = g_IconsTable->GetIconID(m_name.AfterLast(wxT('.')));
583 else
584 item.m_image = FI_UNKNOWN;
585
586 if (IsLink())
587 {
588 wxColour *dg = wxTheColourDatabase->FindColour( _T("MEDIUM GREY") );
589 item.SetTextColour(*dg);
590 }
591 item.m_data = (long)this;
592 }
593
594 //-----------------------------------------------------------------------------
595 // wxFileCtrl
596 //-----------------------------------------------------------------------------
597
598 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl)
599
600 BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
601 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem)
602 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit)
603 END_EVENT_TABLE()
604
605
606 wxFileCtrl::wxFileCtrl()
607 {
608 m_showHidden = FALSE;
609 }
610
611 wxFileCtrl::wxFileCtrl(wxWindow *win,
612 wxStaticText *labelDir,
613 wxWindowID id,
614 const wxString& wild,
615 bool showHidden,
616 const wxPoint& pos,
617 const wxSize& size,
618 long style,
619 const wxValidator &validator,
620 const wxString &name)
621 : wxListCtrl(win, id, pos, size, style, validator, name),
622 m_wild(wild)
623 {
624 if (! g_IconsTable)
625 g_IconsTable = new wxFileIconsTable;
626 wxImageList *imageList = g_IconsTable->GetImageList();
627
628 SetImageList( imageList, wxIMAGE_LIST_SMALL );
629
630 m_goToParentControl =
631 m_newDirControl = NULL;
632
633 m_labelDir = labelDir;
634
635 m_showHidden = showHidden;
636 }
637
638 void wxFileCtrl::ChangeToListMode()
639 {
640 SetSingleStyle( wxLC_LIST );
641 UpdateFiles();
642 }
643
644 void wxFileCtrl::ChangeToReportMode()
645 {
646 SetSingleStyle( wxLC_REPORT );
647 UpdateFiles();
648 }
649
650 void wxFileCtrl::ChangeToIconMode()
651 {
652 SetSingleStyle( wxLC_ICON );
653 UpdateFiles();
654 }
655
656 void wxFileCtrl::ShowHidden( bool show )
657 {
658 m_showHidden = show;
659 UpdateFiles();
660 }
661
662 long wxFileCtrl::Add( wxFileData *fd, wxListItem &item )
663 {
664 long ret = -1;
665 item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
666 fd->MakeItem( item );
667 long my_style = GetWindowStyleFlag();
668 if (my_style & wxLC_REPORT)
669 {
670 ret = InsertItem( item );
671 for (int i = 1; i < FileList_Max; i++)
672 SetItem( item.m_itemId, i, fd->GetEntry((FileListField)i) );
673 }
674 else if (my_style & wxLC_LIST)
675 {
676 ret = InsertItem( item );
677 }
678 return ret;
679 }
680
681 void wxFileCtrl::UpdateFiles()
682 {
683 // don't do anything before ShowModal() call which sets m_dirName
684 if ( m_dirName.empty() )
685 return;
686
687 wxBusyCursor bcur; // this may take a while...
688
689 long my_style = GetWindowStyleFlag();
690 int name_col_width = 0;
691 if (my_style & wxLC_REPORT)
692 {
693 if (GetColumnCount() > 0)
694 name_col_width = GetColumnWidth( 0 );
695 }
696
697 FreeAllItemsData();
698 ClearAll();
699
700 if (my_style & wxLC_REPORT)
701 {
702 if (name_col_width < 140) name_col_width = 140;
703 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, name_col_width );
704 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, 60 );
705 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT, 65 );
706 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT, 50 );
707 #ifdef __UNIX__
708 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, 120 );
709 #endif
710 }
711 wxFileData *fd = (wxFileData *) NULL;
712 wxListItem item;
713 item.m_itemId = 0;
714 item.m_col = 0;
715
716 #if defined(__DOS__) || defined(__WINDOWS__)
717 if ( IsTopMostDir(m_dirName) )
718 {
719 // Pseudo-directory with all available drives listed...
720 for (int drive = 1; drive <= 26; drive++)
721 {
722 wxString path;
723 path.Printf(wxT("%c:\\"), (char)(drive + 'A' - 1));
724 if ( wxIsDriveAvailable(path) )
725 {
726 path.RemoveLast();
727 fd = new wxFileData(path, path);
728 Add(fd, item);
729 item.m_itemId++;
730 }
731 }
732 }
733 else
734 #endif
735 {
736 // Real directory...
737 if ( !IsTopMostDir(m_dirName) )
738 {
739 wxString p(wxPathOnly(m_dirName));
740 #ifdef __UNIX__
741 if (p.IsEmpty()) p = wxT("/");
742 #endif
743 fd = new wxFileData( wxT(".."), p );
744 Add(fd, item);
745 item.m_itemId++;
746 }
747
748 wxString dirname(m_dirName);
749 #if defined(__DOS__) || defined(__WINDOWS__)
750 if (dirname.length() == 2 && dirname[1u] == wxT(':'))
751 dirname << wxT('\\');
752 #endif
753 wxDir dir(dirname);
754
755 if ( dir.IsOpened() )
756 {
757 wxString dirPrefix(dirname + wxFILE_SEP_PATH);
758 int hiddenFlag = m_showHidden ? wxDIR_HIDDEN : 0;
759
760 bool cont;
761 wxString f;
762
763 // Get the directories first (not matched against wildcards):
764 cont = dir.GetFirst(&f, wxEmptyString, wxDIR_DIRS | hiddenFlag);
765 while (cont)
766 {
767 fd = new wxFileData(f, dirPrefix + f);
768 Add(fd, item);
769 item.m_itemId++;
770 cont = dir.GetNext(&f);
771 }
772
773 // Tokenize the wildcard string, so we can handle more than 1
774 // search pattern in a wildcard.
775 wxStringTokenizer tokenWild(m_wild, wxT(";"));
776 while ( tokenWild.HasMoreTokens() )
777 {
778 cont = dir.GetFirst(&f, tokenWild.GetNextToken(),
779 wxDIR_FILES | hiddenFlag);
780 while (cont)
781 {
782 fd = new wxFileData(f, dirPrefix + f);
783 Add(fd, item);
784 item.m_itemId++;
785 cont = dir.GetNext(&f);
786 }
787 }
788 }
789 }
790
791 SortItems(ListCompare, 0);
792
793 if ( my_style & wxLC_REPORT )
794 {
795 SetColumnWidth(1, wxLIST_AUTOSIZE);
796 SetColumnWidth(2, wxLIST_AUTOSIZE);
797 SetColumnWidth(3, wxLIST_AUTOSIZE);
798 }
799
800 // Finally, enable/disable context-dependent controls:
801 if ( m_goToParentControl )
802 m_goToParentControl->Enable(!IsTopMostDir(m_dirName));
803 #if defined(__DOS__) || defined(__WINDOWS__)
804 if ( m_newDirControl )
805 m_newDirControl->Enable(!IsTopMostDir(m_dirName));
806 #endif
807 }
808
809 void wxFileCtrl::SetWild( const wxString &wild )
810 {
811 m_wild = wild;
812 UpdateFiles();
813 }
814
815 void wxFileCtrl::MakeDir()
816 {
817 wxString new_name( _("NewName") );
818 wxString path( m_dirName );
819 path += wxFILE_SEP_PATH;
820 path += new_name;
821 if (wxFileExists(path))
822 {
823 // try NewName0, NewName1 etc.
824 int i = 0;
825 do {
826 new_name = _("NewName");
827 wxString num;
828 num.Printf( wxT("%d"), i );
829 new_name += num;
830
831 path = m_dirName;
832 path += wxFILE_SEP_PATH;
833 path += new_name;
834 i++;
835 } while (wxFileExists(path));
836 }
837
838 wxLogNull log;
839 if (!wxMkdir(path))
840 {
841 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
842 dialog.ShowModal();
843 return;
844 }
845
846 wxFileData *fd = new wxFileData( new_name, path );
847 wxListItem item;
848 item.m_itemId = 0;
849 item.m_col = 0;
850 long id = Add( fd, item );
851
852 if (id != -1)
853 {
854 SortItems( ListCompare, 0 );
855 id = FindItem( 0, (long)fd );
856 EnsureVisible( id );
857 EditLabel( id );
858 }
859 }
860
861 void wxFileCtrl::GoToParentDir()
862 {
863 if (!IsTopMostDir(m_dirName))
864 {
865 size_t len = m_dirName.Len();
866 if (m_dirName[len-1] == wxFILE_SEP_PATH)
867 m_dirName.Remove( len-1, 1 );
868 wxString fname( wxFileNameFromPath(m_dirName) );
869 m_dirName = wxPathOnly( m_dirName );
870 #ifdef __UNIX__
871 if (m_dirName.IsEmpty())
872 m_dirName = wxT("/");
873 #endif
874 UpdateFiles();
875 long id = FindItem( 0, fname );
876 if (id != -1)
877 {
878 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
879 EnsureVisible( id );
880 }
881
882 m_labelDir->SetLabel(m_dirName);
883 }
884 }
885
886 void wxFileCtrl::GoToHomeDir()
887 {
888 wxString s = wxGetUserHome( wxString() );
889 GoToDir(s);
890 }
891
892 void wxFileCtrl::GoToDir( const wxString &dir )
893 {
894 m_dirName = dir;
895 UpdateFiles();
896 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
897 EnsureVisible( 0 );
898
899 m_labelDir->SetLabel(dir);
900 }
901
902 void wxFileCtrl::GetDir( wxString &dir )
903 {
904 dir = m_dirName;
905 }
906
907 void wxFileCtrl::FreeItemData(const wxListItem& item)
908 {
909 wxFileData *fd = (wxFileData*)item.m_data;
910 delete fd;
911 }
912
913 void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
914 {
915 FreeItemData(event.m_item);
916 }
917
918 void wxFileCtrl::FreeAllItemsData()
919 {
920 wxListItem item;
921 item.m_mask = wxLIST_MASK_DATA;
922
923 item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL );
924 while ( item.m_itemId != -1 )
925 {
926 GetItem( item );
927 FreeItemData(item);
928 item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL );
929 }
930 }
931
932 void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
933 {
934 wxFileData *fd = (wxFileData*)event.m_item.m_data;
935 wxASSERT( fd );
936
937 if ((event.GetLabel().IsEmpty()) ||
938 (event.GetLabel() == _(".")) ||
939 (event.GetLabel() == _("..")) ||
940 (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND))
941 {
942 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
943 dialog.ShowModal();
944 event.Veto();
945 return;
946 }
947
948 wxString new_name( wxPathOnly( fd->GetFullName() ) );
949 new_name += wxFILE_SEP_PATH;
950 new_name += event.GetLabel();
951
952 wxLogNull log;
953
954 if (wxFileExists(new_name))
955 {
956 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
957 dialog.ShowModal();
958 event.Veto();
959 }
960
961 if (wxRenameFile(fd->GetFullName(),new_name))
962 {
963 fd->SetNewName( new_name, event.GetLabel() );
964 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
965 EnsureVisible( event.GetItem() );
966 }
967 else
968 {
969 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
970 dialog.ShowModal();
971 event.Veto();
972 }
973 }
974
975 wxFileCtrl::~wxFileCtrl()
976 {
977 FreeAllItemsData();
978 }
979
980 //-----------------------------------------------------------------------------
981 // wxFileDialog
982 //-----------------------------------------------------------------------------
983
984 #define ID_LIST_MODE (wxID_FILEDLGG )
985 #define ID_REPORT_MODE (wxID_FILEDLGG + 1)
986 #define ID_UP_DIR (wxID_FILEDLGG + 5)
987 #define ID_PARENT_DIR (wxID_FILEDLGG + 6)
988 #define ID_NEW_DIR (wxID_FILEDLGG + 7)
989 #define ID_CHOICE (wxID_FILEDLGG + 8)
990 #define ID_TEXT (wxID_FILEDLGG + 9)
991 #define ID_LIST_CTRL (wxID_FILEDLGG + 10)
992 #define ID_ACTIVATED (wxID_FILEDLGG + 11)
993 #define ID_CHECK (wxID_FILEDLGG + 12)
994
995 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
996
997 BEGIN_EVENT_TABLE(wxFileDialog,wxDialog)
998 EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList)
999 EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport)
1000 EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp)
1001 EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome)
1002 EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew)
1003 EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk)
1004 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected)
1005 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated)
1006 EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoiceFilter)
1007 EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter)
1008 EVT_TEXT(ID_TEXT,wxFileDialog::OnTextChange)
1009 EVT_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck)
1010 END_EVENT_TABLE()
1011
1012 long wxFileDialog::ms_lastViewStyle = wxLC_LIST;
1013 bool wxFileDialog::ms_lastShowHidden = FALSE;
1014
1015 wxFileDialog::wxFileDialog(wxWindow *parent,
1016 const wxString& message,
1017 const wxString& defaultDir,
1018 const wxString& defaultFile,
1019 const wxString& wildCard,
1020 long style,
1021 const wxPoint& pos )
1022 : wxDialog( parent, -1, message, pos, wxDefaultSize,
1023 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
1024 {
1025 if (wxConfig::Get(FALSE))
1026 {
1027 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1028 &ms_lastViewStyle);
1029 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1030 &ms_lastShowHidden);
1031 }
1032
1033 m_message = message;
1034 m_dialogStyle = style;
1035
1036 if (m_dialogStyle == 0)
1037 m_dialogStyle = wxOPEN;
1038 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
1039 m_dialogStyle |= wxOPEN;
1040
1041 m_dir = defaultDir;
1042 if ((m_dir.empty()) || (m_dir == wxT(".")))
1043 {
1044 m_dir = wxGetCwd();
1045 }
1046
1047 size_t len = m_dir.Len();
1048 if ((len > 1) && (m_dir[len-1] == wxFILE_SEP_PATH))
1049 m_dir.Remove( len-1, 1 );
1050
1051 m_path = m_dir;
1052 m_path += wxFILE_SEP_PATH;
1053 m_path += defaultFile;
1054 m_fileName = defaultFile;
1055 m_wildCard = wildCard;
1056 m_filterIndex = 0;
1057 m_filterExtension = wxEmptyString;
1058
1059 // interpret wildcards
1060
1061 if (m_wildCard.IsEmpty())
1062 m_wildCard = _("All files (*)|*");
1063
1064 wxStringTokenizer tokens( m_wildCard, wxT("|") );
1065 wxString firstWild;
1066 wxString firstWildText;
1067 if (tokens.CountTokens() == 1)
1068 {
1069 firstWildText = tokens.GetNextToken();
1070 firstWild = firstWildText;
1071 }
1072 else
1073 {
1074 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
1075 firstWildText = tokens.GetNextToken();
1076 firstWild = tokens.GetNextToken();
1077 }
1078 if ( firstWild.Left( 2 ) == wxT("*.") )
1079 m_filterExtension = firstWild.Mid( 1 );
1080 if ( m_filterExtension == wxT(".*") )
1081 m_filterExtension = wxEmptyString;
1082
1083 // layout
1084
1085 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
1086
1087 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
1088
1089 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
1090
1091 wxBitmapButton *but;
1092
1093 but = new wxBitmapButton(this, ID_LIST_MODE,
1094 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_CMN_DIALOG));
1095 #if wxUSE_TOOLTIPS
1096 but->SetToolTip( _("View files as a list view") );
1097 #endif
1098 buttonsizer->Add( but, 0, wxALL, 5 );
1099
1100 but = new wxBitmapButton(this, ID_REPORT_MODE,
1101 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_CMN_DIALOG));
1102 #if wxUSE_TOOLTIPS
1103 but->SetToolTip( _("View files as a detailed view") );
1104 #endif
1105 buttonsizer->Add( but, 0, wxALL, 5 );
1106
1107 buttonsizer->Add( 30, 5, 1 );
1108
1109 wxWindow *butDirUp =
1110 new wxBitmapButton(this, ID_UP_DIR,
1111 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_CMN_DIALOG));
1112 #if wxUSE_TOOLTIPS
1113 butDirUp->SetToolTip( _("Go to parent directory") );
1114 #endif
1115 buttonsizer->Add( butDirUp, 0, wxALL, 5 );
1116
1117 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1118 but = new wxBitmapButton(this, ID_PARENT_DIR,
1119 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_CMN_DIALOG));
1120 #if wxUSE_TOOLTIPS
1121 but->SetToolTip( _("Go to home directory") );
1122 #endif
1123 buttonsizer->Add( but, 0, wxALL, 5);
1124
1125 buttonsizer->Add( 20, 20 );
1126 #endif //!__DOS__
1127
1128 wxWindow *butNewDir =
1129 new wxBitmapButton(this, ID_NEW_DIR,
1130 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_CMN_DIALOG));
1131 #if wxUSE_TOOLTIPS
1132 butNewDir->SetToolTip( _("Create new directory") );
1133 #endif
1134 buttonsizer->Add( butNewDir, 0, wxALL, 5 );
1135
1136 if (is_pda)
1137 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
1138 else
1139 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
1140
1141 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
1142 if (is_pda)
1143 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
1144 m_static = new wxStaticText( this, -1, m_dir );
1145 staticsizer->Add( m_static, 1 );
1146 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
1147
1148 long style2 = ms_lastViewStyle | wxSUNKEN_BORDER;
1149 if ( !(m_dialogStyle & wxMULTIPLE) )
1150 style2 |= wxLC_SINGLE_SEL;
1151
1152 m_list = new wxFileCtrl( this, m_static, ID_LIST_CTRL,
1153 firstWild, ms_lastShowHidden,
1154 wxDefaultPosition, wxSize(540,200),
1155 style2);
1156
1157 m_list->SetNewDirControl(butNewDir);
1158 m_list->SetGoToParentControl(butDirUp);
1159
1160 if (is_pda)
1161 {
1162 // PDAs have a different screen layout
1163 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
1164
1165 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1166 m_choice = new wxChoice( this, ID_CHOICE );
1167 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 5 );
1168 mainsizer->Add( choicesizer, 0, wxEXPAND );
1169
1170 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1171 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1172 textsizer->Add( m_text, 1, wxCENTER | wxALL, 5 );
1173 mainsizer->Add( textsizer, 0, wxEXPAND );
1174
1175 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
1176 m_check->SetValue( ms_lastShowHidden );
1177 textsizer->Add( m_check, 0, wxCENTER|wxALL, 5 );
1178
1179 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
1180 buttonsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxALL, 5 );
1181 buttonsizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 5 );
1182 mainsizer->Add( buttonsizer, 0, wxALIGN_RIGHT );
1183 }
1184 else
1185 {
1186 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
1187
1188 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1189 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1190 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1191 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1192 mainsizer->Add( textsizer, 0, wxEXPAND );
1193
1194 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1195 m_choice = new wxChoice( this, ID_CHOICE );
1196 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
1197 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
1198 m_check->SetValue( ms_lastShowHidden );
1199 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
1200 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
1201 mainsizer->Add( choicesizer, 0, wxEXPAND );
1202 }
1203
1204 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
1205 while (tokens.HasMoreTokens())
1206 {
1207 firstWildText = tokens.GetNextToken();
1208 firstWild = tokens.GetNextToken();
1209 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
1210 }
1211 m_choice->SetSelection( 0 );
1212
1213 SetAutoLayout( TRUE );
1214 SetSizer( mainsizer );
1215
1216 mainsizer->Fit( this );
1217 mainsizer->SetSizeHints( this );
1218
1219 Centre( wxBOTH );
1220
1221 m_text->SetFocus();
1222 }
1223
1224 wxFileDialog::~wxFileDialog()
1225 {
1226 if (wxConfig::Get(FALSE))
1227 {
1228 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1229 ms_lastViewStyle);
1230 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1231 ms_lastShowHidden);
1232 }
1233 }
1234
1235 int wxFileDialog::ShowModal()
1236 {
1237 m_list->GoToDir(m_dir);
1238 m_text->SetValue(m_fileName);
1239
1240 return wxDialog::ShowModal();
1241 }
1242
1243 void wxFileDialog::DoSetFilterIndex(int filterindex)
1244 {
1245 wxString *str = (wxString*) m_choice->GetClientData( filterindex );
1246 m_list->SetWild( *str );
1247 m_filterIndex = filterindex;
1248 if ( str->Left(2) == wxT("*.") )
1249 {
1250 m_filterExtension = str->Mid(2);
1251 if (m_filterExtension == _T("*"))
1252 m_filterExtension.clear();
1253 }
1254 else
1255 {
1256 m_filterExtension.clear();
1257 }
1258 }
1259
1260 void wxFileDialog::SetFilterIndex( int filterindex )
1261 {
1262 m_choice->SetSelection( filterindex );
1263
1264 DoSetFilterIndex(filterindex);
1265 }
1266
1267 void wxFileDialog::OnChoiceFilter( wxCommandEvent &event )
1268 {
1269 DoSetFilterIndex((int)event.GetInt());
1270 }
1271
1272 void wxFileDialog::OnCheck( wxCommandEvent &event )
1273 {
1274 m_list->ShowHidden( (ms_lastShowHidden = event.GetInt() != 0) );
1275 }
1276
1277 void wxFileDialog::OnActivated( wxListEvent &event )
1278 {
1279 HandleAction( event.m_item.m_text );
1280 }
1281
1282 void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
1283 {
1284 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
1285 cevent.SetEventObject( this );
1286 GetEventHandler()->ProcessEvent( cevent );
1287 }
1288
1289 static bool ignoreChanges = FALSE;
1290
1291 void wxFileDialog::OnTextChange( wxCommandEvent &WXUNUSED(event) )
1292 {
1293 if (!ignoreChanges)
1294 {
1295 // Clear selections. Otherwise when the user types in a value they may
1296 // not get the file whose name they typed.
1297 if (m_list->GetSelectedItemCount() > 0)
1298 {
1299 long item = m_list->GetNextItem(-1, wxLIST_NEXT_ALL,
1300 wxLIST_STATE_SELECTED);
1301 while ( item != -1 )
1302 {
1303 m_list->SetItemState(item,0, wxLIST_STATE_SELECTED);
1304 item = m_list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
1305 }
1306 }
1307 }
1308 }
1309
1310 void wxFileDialog::OnSelected( wxListEvent &event )
1311 {
1312 wxString filename( event.m_item.m_text );
1313 if (filename == wxT("..")) return;
1314
1315 wxString dir;
1316 m_list->GetDir( dir );
1317 if (!IsTopMostDir(dir))
1318 dir += wxFILE_SEP_PATH;
1319 dir += filename;
1320 if (wxDirExists(dir)) return;
1321
1322 ignoreChanges = TRUE;
1323 m_text->SetValue( filename );
1324 ignoreChanges = FALSE;
1325 }
1326
1327 void wxFileDialog::HandleAction( const wxString &fn )
1328 {
1329 wxString filename( fn );
1330 wxString dir;
1331 m_list->GetDir( dir );
1332 if (filename.IsEmpty()) return;
1333 if (filename == wxT(".")) return;
1334
1335 if (filename == wxT(".."))
1336 {
1337 m_list->GoToParentDir();
1338 m_list->SetFocus();
1339 return;
1340 }
1341
1342 #ifdef __UNIX__
1343 if (filename == wxT("~"))
1344 {
1345 m_list->GoToHomeDir();
1346 m_list->SetFocus();
1347 return;
1348 }
1349
1350 if (filename[0u] == wxT('~'))
1351 {
1352 filename.Remove( 0, 1 );
1353 wxString tmp( wxGetUserHome() );
1354 tmp += wxT('/');
1355 tmp += filename;
1356 filename = tmp;
1357 }
1358 #endif // __UNIX__
1359
1360 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
1361 (filename.Find(wxT('?')) != wxNOT_FOUND))
1362 {
1363 if (filename.Find(wxFILE_SEP_PATH) != wxNOT_FOUND)
1364 {
1365 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
1366 return;
1367 }
1368 m_list->SetWild( filename );
1369 return;
1370 }
1371
1372 if (!IsTopMostDir(dir))
1373 dir += wxFILE_SEP_PATH;
1374 if (!wxIsAbsolutePath(filename))
1375 {
1376 dir += filename;
1377 filename = dir;
1378 }
1379
1380 if (wxDirExists(filename))
1381 {
1382 m_list->GoToDir( filename );
1383 return;
1384 }
1385
1386 // append the default extension to the filename if it doesn't have any
1387 //
1388 // VZ: the logic of testing for !wxFileExists() only for the open file
1389 // dialog is not entirely clear to me, why don't we allow saving to a
1390 // file without extension as well?
1391 if ( !(m_dialogStyle & wxOPEN) || !wxFileExists(filename) )
1392 {
1393 wxString ext;
1394 wxSplitPath(filename, NULL, NULL, &ext);
1395 if ( ext.empty() )
1396 {
1397 // append the first extension of the filter string
1398 filename += m_filterExtension.BeforeFirst(_T(';'));
1399 }
1400 }
1401
1402 // check that the file [doesn't] exist if necessary
1403 if ( (m_dialogStyle & wxSAVE) &&
1404 (m_dialogStyle & wxOVERWRITE_PROMPT) &&
1405 wxFileExists( filename ) )
1406 {
1407 wxString msg;
1408 msg.Printf( _("File '%s' already exists, do you really want to "
1409 "overwrite it?"), filename.c_str() );
1410
1411 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1412 return;
1413 }
1414 else if ( (m_dialogStyle & wxOPEN) &&
1415 (m_dialogStyle & wxFILE_MUST_EXIST) &&
1416 !wxFileExists(filename) )
1417 {
1418 wxMessageBox(_("Please choose an existing file."), _("Error"),
1419 wxOK | wxICON_ERROR );
1420 }
1421
1422 SetPath( filename );
1423
1424 // change to the directory where the user went if asked
1425 if ( m_dialogStyle & wxCHANGE_DIR )
1426 {
1427 wxString cwd;
1428 wxSplitPath(filename, &cwd, NULL, NULL);
1429
1430 if ( cwd != wxGetWorkingDirectory() )
1431 {
1432 wxSetWorkingDirectory(cwd);
1433 }
1434 }
1435
1436 wxCommandEvent event;
1437 wxDialog::OnOK(event);
1438 }
1439
1440 void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
1441 {
1442 HandleAction( m_text->GetValue() );
1443 }
1444
1445 void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
1446 {
1447 m_list->ChangeToListMode();
1448 ms_lastViewStyle = wxLC_LIST;
1449 m_list->SetFocus();
1450 }
1451
1452 void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
1453 {
1454 m_list->ChangeToReportMode();
1455 ms_lastViewStyle = wxLC_REPORT;
1456 m_list->SetFocus();
1457 }
1458
1459 void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
1460 {
1461 m_list->GoToParentDir();
1462 m_list->SetFocus();
1463 }
1464
1465 void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
1466 {
1467 m_list->GoToHomeDir();
1468 m_list->SetFocus();
1469 }
1470
1471 void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
1472 {
1473 m_list->MakeDir();
1474 }
1475
1476 void wxFileDialog::SetPath( const wxString& path )
1477 {
1478 // not only set the full path but also update filename and dir
1479 m_path = path;
1480 if ( !path.empty() )
1481 {
1482 wxString ext;
1483 wxSplitPath(path, &m_dir, &m_fileName, &ext);
1484 if (!ext.empty())
1485 {
1486 m_fileName += wxT(".");
1487 m_fileName += ext;
1488 }
1489 }
1490 }
1491
1492 void wxFileDialog::GetPaths( wxArrayString& paths ) const
1493 {
1494 paths.Empty();
1495 if (m_list->GetSelectedItemCount() == 0)
1496 {
1497 paths.Add( GetPath() );
1498 return;
1499 }
1500
1501 paths.Alloc( m_list->GetSelectedItemCount() );
1502
1503 wxString dir;
1504 m_list->GetDir( dir );
1505 #ifdef __UNIX__
1506 if (dir != wxT("/"))
1507 #endif
1508 dir += wxFILE_SEP_PATH;
1509
1510 wxListItem item;
1511 item.m_mask = wxLIST_MASK_TEXT;
1512
1513 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1514 while ( item.m_itemId != -1 )
1515 {
1516 m_list->GetItem( item );
1517 paths.Add( dir + item.m_text );
1518 item.m_itemId = m_list->GetNextItem( item.m_itemId,
1519 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1520 }
1521 }
1522
1523 void wxFileDialog::GetFilenames(wxArrayString& files) const
1524 {
1525 files.Empty();
1526 if (m_list->GetSelectedItemCount() == 0)
1527 {
1528 files.Add( GetFilename() );
1529 return;
1530 }
1531 files.Alloc( m_list->GetSelectedItemCount() );
1532
1533 wxListItem item;
1534 item.m_mask = wxLIST_MASK_TEXT;
1535
1536 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1537 while ( item.m_itemId != -1 )
1538 {
1539 m_list->GetItem( item );
1540 files.Add( item.m_text );
1541 item.m_itemId = m_list->GetNextItem( item.m_itemId,
1542 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1543 }
1544 }
1545
1546
1547
1548 // ----------------------------------------------------------------------------
1549 // global functions
1550 // ----------------------------------------------------------------------------
1551
1552 wxString
1553 wxFileSelectorEx(const wxChar *message,
1554 const wxChar *default_path,
1555 const wxChar *default_filename,
1556 int *WXUNUSED(indexDefaultExtension),
1557 const wxChar *wildcard,
1558 int flags,
1559 wxWindow *parent,
1560 int x, int y)
1561 {
1562 // TODO: implement this somehow
1563 return wxFileSelector(message, default_path, default_filename, wxT(""),
1564 wildcard, flags, parent, x, y);
1565 }
1566
1567 wxString wxFileSelector( const wxChar *title,
1568 const wxChar *defaultDir, const wxChar *defaultFileName,
1569 const wxChar *defaultExtension, const wxChar *filter, int flags,
1570 wxWindow *parent, int x, int y )
1571 {
1572 wxString filter2;
1573 if ( defaultExtension && !filter )
1574 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
1575 else if ( filter )
1576 filter2 = filter;
1577
1578 wxString defaultDirString;
1579 if (defaultDir)
1580 defaultDirString = defaultDir;
1581
1582 wxString defaultFilenameString;
1583 if (defaultFileName)
1584 defaultFilenameString = defaultFileName;
1585
1586 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
1587
1588 if ( fileDialog.ShowModal() == wxID_OK )
1589 {
1590 return fileDialog.GetPath();
1591 }
1592 else
1593 {
1594 return wxEmptyString;
1595 }
1596 }
1597
1598 static wxString GetWildcardString(const wxChar *ext)
1599 {
1600 wxString wild;
1601 if ( ext )
1602 {
1603 if ( *ext == wxT('.') )
1604 ext++;
1605
1606 wild << _T("*.") << ext;
1607 }
1608 else // no extension specified
1609 {
1610 wild = wxFileSelectorDefaultWildcardStr;
1611 }
1612
1613 return wild;
1614 }
1615
1616 wxString wxLoadFileSelector(const wxChar *what,
1617 const wxChar *ext,
1618 const wxChar *nameDef,
1619 wxWindow *parent)
1620 {
1621 wxString prompt;
1622 if ( what && *what )
1623 prompt = wxString::Format(_("Load %s file"), what);
1624 else
1625 prompt = _("Load file");
1626
1627 return wxFileSelector(prompt, NULL, nameDef, ext,
1628 GetWildcardString(ext), 0, parent);
1629 }
1630
1631 wxString wxSaveFileSelector(const wxChar *what,
1632 const wxChar *ext,
1633 const wxChar *nameDef,
1634 wxWindow *parent)
1635 {
1636 wxString prompt;
1637 if ( what && *what )
1638 prompt = wxString::Format(_("Save %s file"), what);
1639 else
1640 prompt = _("Save file");
1641
1642 return wxFileSelector(prompt, NULL, nameDef, ext,
1643 GetWildcardString(ext), 0, parent);
1644 }
1645
1646 // A module to allow icons table cleanup
1647
1648 class wxFileDialogGenericModule: public wxModule
1649 {
1650 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule)
1651 public:
1652 wxFileDialogGenericModule() {}
1653 bool OnInit() { return TRUE; }
1654 void OnExit() { if (g_IconsTable) {delete g_IconsTable; g_IconsTable = NULL;} }
1655 };
1656
1657 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule, wxModule)
1658
1659 #endif // wxUSE_FILEDLG
1660