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