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