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