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