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