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