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