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