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