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