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