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