compilation fix after recent patch
[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_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck)
1009 END_EVENT_TABLE()
1010
1011 long wxFileDialog::ms_lastViewStyle = wxLC_LIST;
1012 bool wxFileDialog::ms_lastShowHidden = FALSE;
1013
1014 wxFileDialog::wxFileDialog(wxWindow *parent,
1015 const wxString& message,
1016 const wxString& defaultDir,
1017 const wxString& defaultFile,
1018 const wxString& wildCard,
1019 long style,
1020 const wxPoint& pos )
1021 : wxDialog( parent, -1, message, pos, wxDefaultSize,
1022 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
1023 {
1024 if (wxConfig::Get(FALSE))
1025 {
1026 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1027 &ms_lastViewStyle);
1028 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1029 &ms_lastShowHidden);
1030 }
1031
1032 m_message = message;
1033 m_dialogStyle = style;
1034
1035 if (m_dialogStyle == 0)
1036 m_dialogStyle = wxOPEN;
1037 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
1038 m_dialogStyle |= wxOPEN;
1039
1040 m_dir = defaultDir;
1041 if ((m_dir.empty()) || (m_dir == wxT(".")))
1042 {
1043 m_dir = wxGetCwd();
1044 }
1045
1046 size_t len = m_dir.Len();
1047 if ((len > 1) && (m_dir[len-1] == wxFILE_SEP_PATH))
1048 m_dir.Remove( len-1, 1 );
1049
1050 m_path = m_dir;
1051 m_path += wxFILE_SEP_PATH;
1052 m_path += defaultFile;
1053 m_fileName = defaultFile;
1054 m_wildCard = wildCard;
1055 m_filterIndex = 0;
1056 m_filterExtension = wxEmptyString;
1057
1058 // interpret wildcards
1059
1060 if (m_wildCard.IsEmpty())
1061 m_wildCard = _("All files (*)|*");
1062
1063 wxStringTokenizer tokens( m_wildCard, wxT("|") );
1064 wxString firstWild;
1065 wxString firstWildText;
1066 if (tokens.CountTokens() == 1)
1067 {
1068 firstWildText = tokens.GetNextToken();
1069 firstWild = firstWildText;
1070 }
1071 else
1072 {
1073 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
1074 firstWildText = tokens.GetNextToken();
1075 firstWild = tokens.GetNextToken();
1076 }
1077 if ( firstWild.Left( 2 ) == wxT("*.") )
1078 m_filterExtension = firstWild.Mid( 1 );
1079 if ( m_filterExtension == wxT(".*") )
1080 m_filterExtension = wxEmptyString;
1081
1082 // layout
1083
1084 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
1085
1086 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
1087
1088 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
1089
1090 wxBitmapButton *but;
1091
1092 but = new wxBitmapButton(this, ID_LIST_MODE,
1093 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_CMN_DIALOG));
1094 #if wxUSE_TOOLTIPS
1095 but->SetToolTip( _("View files as a list view") );
1096 #endif
1097 buttonsizer->Add( but, 0, wxALL, 5 );
1098
1099 but = new wxBitmapButton(this, ID_REPORT_MODE,
1100 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_CMN_DIALOG));
1101 #if wxUSE_TOOLTIPS
1102 but->SetToolTip( _("View files as a detailed view") );
1103 #endif
1104 buttonsizer->Add( but, 0, wxALL, 5 );
1105
1106 buttonsizer->Add( 30, 5, 1 );
1107
1108 wxWindow *butDirUp =
1109 new wxBitmapButton(this, ID_UP_DIR,
1110 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_CMN_DIALOG));
1111 #if wxUSE_TOOLTIPS
1112 butDirUp->SetToolTip( _("Go to parent directory") );
1113 #endif
1114 buttonsizer->Add( butDirUp, 0, wxALL, 5 );
1115
1116 #ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
1117 but = new wxBitmapButton(this, ID_PARENT_DIR,
1118 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_CMN_DIALOG));
1119 #if wxUSE_TOOLTIPS
1120 but->SetToolTip( _("Go to home directory") );
1121 #endif
1122 buttonsizer->Add( but, 0, wxALL, 5);
1123
1124 buttonsizer->Add( 20, 20 );
1125 #endif //!__DOS__
1126
1127 wxWindow *butNewDir =
1128 new wxBitmapButton(this, ID_NEW_DIR,
1129 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_CMN_DIALOG));
1130 #if wxUSE_TOOLTIPS
1131 butNewDir->SetToolTip( _("Create new directory") );
1132 #endif
1133 buttonsizer->Add( butNewDir, 0, wxALL, 5 );
1134
1135 if (is_pda)
1136 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
1137 else
1138 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
1139
1140 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
1141 if (is_pda)
1142 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
1143 m_static = new wxStaticText( this, -1, m_dir );
1144 staticsizer->Add( m_static, 1 );
1145 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
1146
1147 long style2 = ms_lastViewStyle | wxSUNKEN_BORDER;
1148 if ( !(m_dialogStyle & wxMULTIPLE) )
1149 style2 |= wxLC_SINGLE_SEL;
1150
1151 m_list = new wxFileCtrl( this, m_static, ID_LIST_CTRL,
1152 firstWild, ms_lastShowHidden,
1153 wxDefaultPosition, wxSize(540,200),
1154 style2);
1155
1156 m_list->SetNewDirControl(butNewDir);
1157 m_list->SetGoToParentControl(butDirUp);
1158
1159 if (is_pda)
1160 {
1161 // PDAs have a different screen layout
1162 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
1163
1164 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1165 m_choice = new wxChoice( this, ID_CHOICE );
1166 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 5 );
1167 mainsizer->Add( choicesizer, 0, wxEXPAND );
1168
1169 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1170 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1171 textsizer->Add( m_text, 1, wxCENTER | wxALL, 5 );
1172 mainsizer->Add( textsizer, 0, wxEXPAND );
1173
1174 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
1175 m_check->SetValue( ms_lastShowHidden );
1176 textsizer->Add( m_check, 0, wxCENTER|wxALL, 5 );
1177
1178 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
1179 buttonsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxALL, 5 );
1180 buttonsizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 5 );
1181 mainsizer->Add( buttonsizer, 0, wxALIGN_RIGHT );
1182 }
1183 else
1184 {
1185 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
1186
1187 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1188 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1189 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1190 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1191 mainsizer->Add( textsizer, 0, wxEXPAND );
1192
1193 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1194 m_choice = new wxChoice( this, ID_CHOICE );
1195 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
1196 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
1197 m_check->SetValue( ms_lastShowHidden );
1198 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
1199 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
1200 mainsizer->Add( choicesizer, 0, wxEXPAND );
1201 }
1202
1203 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
1204 while (tokens.HasMoreTokens())
1205 {
1206 firstWildText = tokens.GetNextToken();
1207 firstWild = tokens.GetNextToken();
1208 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
1209 }
1210 m_choice->SetSelection( 0 );
1211
1212 SetAutoLayout( TRUE );
1213 SetSizer( mainsizer );
1214
1215 mainsizer->Fit( this );
1216 mainsizer->SetSizeHints( this );
1217
1218 Centre( wxBOTH );
1219
1220 m_text->SetFocus();
1221 }
1222
1223 wxFileDialog::~wxFileDialog()
1224 {
1225 if (wxConfig::Get(FALSE))
1226 {
1227 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1228 ms_lastViewStyle);
1229 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1230 ms_lastShowHidden);
1231 }
1232 }
1233
1234 int wxFileDialog::ShowModal()
1235 {
1236 m_list->GoToDir(m_dir);
1237 m_text->SetValue(m_fileName);
1238
1239 return wxDialog::ShowModal();
1240 }
1241
1242 void wxFileDialog::DoSetFilterIndex(int filterindex)
1243 {
1244 wxString *str = (wxString*) m_choice->GetClientData( filterindex );
1245 m_list->SetWild( *str );
1246 m_filterIndex = filterindex;
1247 if ( str->Left(2) == wxT("*.") )
1248 {
1249 m_filterExtension = str->Mid(2);
1250 if (m_filterExtension == _T("*"))
1251 m_filterExtension.clear();
1252 }
1253 else
1254 {
1255 m_filterExtension.clear();
1256 }
1257 }
1258
1259 void wxFileDialog::SetFilterIndex( int filterindex )
1260 {
1261 m_choice->SetSelection( filterindex );
1262
1263 DoSetFilterIndex(filterindex);
1264 }
1265
1266 void wxFileDialog::OnChoiceFilter( wxCommandEvent &event )
1267 {
1268 DoSetFilterIndex((int)event.GetInt());
1269 }
1270
1271 void wxFileDialog::OnCheck( wxCommandEvent &event )
1272 {
1273 m_list->ShowHidden( (ms_lastShowHidden = event.GetInt() != 0) );
1274 }
1275
1276 void wxFileDialog::OnActivated( wxListEvent &event )
1277 {
1278 HandleAction( event.m_item.m_text );
1279 }
1280
1281 void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
1282 {
1283 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
1284 cevent.SetEventObject( this );
1285 GetEventHandler()->ProcessEvent( cevent );
1286 }
1287
1288 void wxFileDialog::OnSelected( wxListEvent &event )
1289 {
1290 wxString filename( event.m_item.m_text );
1291 if (filename == wxT("..")) return;
1292
1293 wxString dir;
1294 m_list->GetDir( dir );
1295 if (!IsTopMostDir(dir))
1296 dir += wxFILE_SEP_PATH;
1297 dir += filename;
1298 if (wxDirExists(dir)) return;
1299
1300 m_text->SetValue( filename );
1301 }
1302
1303 void wxFileDialog::HandleAction( const wxString &fn )
1304 {
1305 wxString filename( fn );
1306 wxString dir;
1307 m_list->GetDir( dir );
1308 if (filename.IsEmpty()) return;
1309 if (filename == wxT(".")) return;
1310
1311 if (filename == wxT(".."))
1312 {
1313 m_list->GoToParentDir();
1314 m_list->SetFocus();
1315 return;
1316 }
1317
1318 #ifdef __UNIX__
1319 if (filename == wxT("~"))
1320 {
1321 m_list->GoToHomeDir();
1322 m_list->SetFocus();
1323 return;
1324 }
1325
1326 if (filename[0u] == wxT('~'))
1327 {
1328 filename.Remove( 0, 1 );
1329 wxString tmp( wxGetUserHome() );
1330 tmp += wxT('/');
1331 tmp += filename;
1332 filename = tmp;
1333 }
1334 #endif // __UNIX__
1335
1336 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
1337 (filename.Find(wxT('?')) != wxNOT_FOUND))
1338 {
1339 if (filename.Find(wxFILE_SEP_PATH) != wxNOT_FOUND)
1340 {
1341 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
1342 return;
1343 }
1344 m_list->SetWild( filename );
1345 return;
1346 }
1347
1348 if (!IsTopMostDir(dir))
1349 dir += wxFILE_SEP_PATH;
1350 if (!wxIsAbsolutePath(filename))
1351 {
1352 dir += filename;
1353 filename = dir;
1354 }
1355
1356 if (wxDirExists(filename))
1357 {
1358 m_list->GoToDir( filename );
1359 return;
1360 }
1361
1362 // append the default extension to the filename if it doesn't have any
1363 //
1364 // VZ: the logic of testing for !wxFileExists() only for the open file
1365 // dialog is not entirely clear to me, why don't we allow saving to a
1366 // file without extension as well?
1367 if ( !(m_dialogStyle & wxOPEN) || !wxFileExists(filename) )
1368 {
1369 wxString ext;
1370 wxSplitPath(filename, NULL, NULL, &ext);
1371 if ( ext.empty() )
1372 {
1373 // append the first extension of the filter string
1374 filename += m_filterExtension.BeforeFirst(_T(';'));
1375 }
1376 }
1377
1378 // check that the file [doesn't] exist if necessary
1379 if ( (m_dialogStyle & wxSAVE) &&
1380 (m_dialogStyle & wxOVERWRITE_PROMPT) &&
1381 wxFileExists( filename ) )
1382 {
1383 wxString msg;
1384 msg.Printf( _("File '%s' already exists, do you really want to "
1385 "overwrite it?"), filename.c_str() );
1386
1387 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1388 return;
1389 }
1390 else if ( (m_dialogStyle & wxOPEN) &&
1391 (m_dialogStyle & wxFILE_MUST_EXIST) &&
1392 !wxFileExists(filename) )
1393 {
1394 wxMessageBox(_("Please choose an existing file."), _("Error"),
1395 wxOK | wxICON_ERROR );
1396 }
1397
1398 SetPath( filename );
1399
1400 // change to the directory where the user went if asked
1401 if ( m_dialogStyle & wxCHANGE_DIR )
1402 {
1403 wxString cwd;
1404 wxSplitPath(filename, &cwd, NULL, NULL);
1405
1406 if ( cwd != wxGetWorkingDirectory() )
1407 {
1408 wxSetWorkingDirectory(cwd);
1409 }
1410 }
1411
1412 wxCommandEvent event;
1413 wxDialog::OnOK(event);
1414 }
1415
1416 void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
1417 {
1418 HandleAction( m_text->GetValue() );
1419 }
1420
1421 void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
1422 {
1423 m_list->ChangeToListMode();
1424 ms_lastViewStyle = wxLC_LIST;
1425 m_list->SetFocus();
1426 }
1427
1428 void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
1429 {
1430 m_list->ChangeToReportMode();
1431 ms_lastViewStyle = wxLC_REPORT;
1432 m_list->SetFocus();
1433 }
1434
1435 void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
1436 {
1437 m_list->GoToParentDir();
1438 m_list->SetFocus();
1439 }
1440
1441 void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
1442 {
1443 m_list->GoToHomeDir();
1444 m_list->SetFocus();
1445 }
1446
1447 void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
1448 {
1449 m_list->MakeDir();
1450 }
1451
1452 void wxFileDialog::SetPath( const wxString& path )
1453 {
1454 // not only set the full path but also update filename and dir
1455 m_path = path;
1456 if ( !path.empty() )
1457 {
1458 wxString ext;
1459 wxSplitPath(path, &m_dir, &m_fileName, &ext);
1460 if (!ext.empty())
1461 {
1462 m_fileName += wxT(".");
1463 m_fileName += ext;
1464 }
1465 }
1466 }
1467
1468 void wxFileDialog::GetPaths( wxArrayString& paths ) const
1469 {
1470 paths.Empty();
1471 if (m_list->GetSelectedItemCount() == 0)
1472 {
1473 paths.Add( GetPath() );
1474 return;
1475 }
1476
1477 paths.Alloc( m_list->GetSelectedItemCount() );
1478
1479 wxString dir;
1480 m_list->GetDir( dir );
1481 #ifdef __UNIX__
1482 if (dir != wxT("/"))
1483 #endif
1484 dir += wxFILE_SEP_PATH;
1485
1486 wxListItem item;
1487 item.m_mask = wxLIST_MASK_TEXT;
1488
1489 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1490 while ( item.m_itemId != -1 )
1491 {
1492 m_list->GetItem( item );
1493 paths.Add( dir + item.m_text );
1494 item.m_itemId = m_list->GetNextItem( item.m_itemId,
1495 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1496 }
1497 }
1498
1499 void wxFileDialog::GetFilenames(wxArrayString& files) const
1500 {
1501 files.Empty();
1502 if (m_list->GetSelectedItemCount() == 0)
1503 {
1504 files.Add( GetFilename() );
1505 return;
1506 }
1507 files.Alloc( m_list->GetSelectedItemCount() );
1508
1509 wxListItem item;
1510 item.m_mask = wxLIST_MASK_TEXT;
1511
1512 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1513 while ( item.m_itemId != -1 )
1514 {
1515 m_list->GetItem( item );
1516 files.Add( item.m_text );
1517 item.m_itemId = m_list->GetNextItem( item.m_itemId,
1518 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1519 }
1520 }
1521
1522
1523
1524 // ----------------------------------------------------------------------------
1525 // global functions
1526 // ----------------------------------------------------------------------------
1527
1528 wxString
1529 wxFileSelectorEx(const wxChar *message,
1530 const wxChar *default_path,
1531 const wxChar *default_filename,
1532 int *WXUNUSED(indexDefaultExtension),
1533 const wxChar *wildcard,
1534 int flags,
1535 wxWindow *parent,
1536 int x, int y)
1537 {
1538 // TODO: implement this somehow
1539 return wxFileSelector(message, default_path, default_filename, wxT(""),
1540 wildcard, flags, parent, x, y);
1541 }
1542
1543 wxString wxFileSelector( const wxChar *title,
1544 const wxChar *defaultDir, const wxChar *defaultFileName,
1545 const wxChar *defaultExtension, const wxChar *filter, int flags,
1546 wxWindow *parent, int x, int y )
1547 {
1548 wxString filter2;
1549 if ( defaultExtension && !filter )
1550 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
1551 else if ( filter )
1552 filter2 = filter;
1553
1554 wxString defaultDirString;
1555 if (defaultDir)
1556 defaultDirString = defaultDir;
1557
1558 wxString defaultFilenameString;
1559 if (defaultFileName)
1560 defaultFilenameString = defaultFileName;
1561
1562 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
1563
1564 if ( fileDialog.ShowModal() == wxID_OK )
1565 {
1566 return fileDialog.GetPath();
1567 }
1568 else
1569 {
1570 return wxEmptyString;
1571 }
1572 }
1573
1574 static wxString GetWildcardString(const wxChar *ext)
1575 {
1576 wxString wild;
1577 if ( ext )
1578 {
1579 if ( *ext == wxT('.') )
1580 ext++;
1581
1582 wild << _T("*.") << ext;
1583 }
1584 else // no extension specified
1585 {
1586 wild = wxFileSelectorDefaultWildcardStr;
1587 }
1588
1589 return wild;
1590 }
1591
1592 wxString wxLoadFileSelector(const wxChar *what,
1593 const wxChar *ext,
1594 const wxChar *nameDef,
1595 wxWindow *parent)
1596 {
1597 wxString prompt;
1598 if ( what && *what )
1599 prompt = wxString::Format(_("Load %s file"), what);
1600 else
1601 prompt = _("Load file");
1602
1603 return wxFileSelector(prompt, NULL, nameDef, ext,
1604 GetWildcardString(ext), 0, parent);
1605 }
1606
1607 wxString wxSaveFileSelector(const wxChar *what,
1608 const wxChar *ext,
1609 const wxChar *nameDef,
1610 wxWindow *parent)
1611 {
1612 wxString prompt;
1613 if ( what && *what )
1614 prompt = wxString::Format(_("Save %s file"), what);
1615 else
1616 prompt = _("Save file");
1617
1618 return wxFileSelector(prompt, NULL, nameDef, ext,
1619 GetWildcardString(ext), 0, parent);
1620 }
1621
1622 // A module to allow icons table cleanup
1623
1624 class wxFileDialogGenericModule: public wxModule
1625 {
1626 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule)
1627 public:
1628 wxFileDialogGenericModule() {}
1629 bool OnInit() { return TRUE; }
1630 void OnExit() { if (g_IconsTable) {delete g_IconsTable; g_IconsTable = NULL;} }
1631 };
1632
1633 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule, wxModule)
1634
1635 #endif // wxUSE_FILEDLG
1636