]> git.saurik.com Git - wxWidgets.git/blob - src/generic/filedlgg.cpp
Project file updates; cast in strconv.cpp to compile with VC++ 6
[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[0u] != 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[0u] != wxT('.')))
576 {
577 Add( fd, item );
578 item.m_itemId++;
579 }
580 f = wxFindNextFile();
581 }
582
583 SortItems( ListCompare, 0 );
584
585 if (my_style & wxLC_REPORT)
586 {
587 SetColumnWidth( 1, wxLIST_AUTOSIZE );
588 SetColumnWidth( 2, wxLIST_AUTOSIZE );
589 SetColumnWidth( 3, wxLIST_AUTOSIZE );
590 }
591 }
592
593 void wxFileCtrl::SetWild( const wxString &wild )
594 {
595 m_wild = wild;
596 Update();
597 }
598
599 void wxFileCtrl::MakeDir()
600 {
601 wxString new_name( wxT("NewName") );
602 wxString path( m_dirName );
603 path += wxT("/");
604 path += new_name;
605 if (wxFileExists(path))
606 {
607 // try NewName0, NewName1 etc.
608 int i = 0;
609 do {
610 new_name = _("NewName");
611 wxString num;
612 num.Printf( wxT("%d"), i );
613 new_name += num;
614
615 path = m_dirName;
616 path += wxT("/");
617 path += new_name;
618 i++;
619 } while (wxFileExists(path));
620 }
621
622 wxLogNull log;
623 if (!wxMkdir(path))
624 {
625 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
626 dialog.ShowModal();
627 return;
628 }
629
630 wxFileData *fd = new wxFileData( new_name, path );
631 wxListItem item;
632 item.m_itemId = 0;
633 item.m_col = 0;
634 long id = Add( fd, item );
635
636 if (id != -1)
637 {
638 SortItems( ListCompare, 0 );
639 id = FindItem( 0, (long)fd );
640 EnsureVisible( id );
641 EditLabel( id );
642 }
643 }
644
645 void wxFileCtrl::GoToParentDir()
646 {
647 if (m_dirName != wxT("/"))
648 {
649 wxString fname( wxFileNameFromPath(m_dirName) );
650 m_dirName = wxPathOnly( m_dirName );
651 if (m_dirName.IsEmpty()) m_dirName = wxT("/");
652 Update();
653 long id = FindItem( 0, fname );
654 if (id != -1)
655 {
656 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
657 EnsureVisible( id );
658 }
659 }
660 }
661
662 void wxFileCtrl::GoToHomeDir()
663 {
664 wxString s = wxGetUserHome( wxString() );
665 m_dirName = s;
666 Update();
667 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
668 EnsureVisible( 0 );
669 }
670
671 void wxFileCtrl::GoToDir( const wxString &dir )
672 {
673 m_dirName = dir;
674 Update();
675 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
676 EnsureVisible( 0 );
677 }
678
679 void wxFileCtrl::GetDir( wxString &dir )
680 {
681 dir = m_dirName;
682 }
683
684 void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
685 {
686 wxFileData *fd = (wxFileData*)event.m_item.m_data;
687 delete fd;
688 }
689
690 void wxFileCtrl::OnListDeleteAllItems( wxListEvent &WXUNUSED(event) )
691 {
692 wxListItem item;
693 item.m_mask = wxLIST_MASK_DATA;
694
695 item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL );
696 while ( item.m_itemId != -1 )
697 {
698 GetItem( item );
699 wxFileData *fd = (wxFileData*)item.m_data;
700 delete fd;
701 item.m_data = 0;
702 SetItem( item );
703 item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL );
704 }
705 }
706
707 void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
708 {
709 wxFileData *fd = (wxFileData*)event.m_item.m_data;
710 wxASSERT( fd );
711
712 if ((event.GetLabel().IsEmpty()) ||
713 (event.GetLabel() == _(".")) ||
714 (event.GetLabel() == _("..")) ||
715 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
716 {
717 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
718 dialog.ShowModal();
719 event.Veto();
720 return;
721 }
722
723 wxString new_name( wxPathOnly( fd->GetFullName() ) );
724 new_name += wxT("/");
725 new_name += event.GetLabel();
726
727 wxLogNull log;
728
729 if (wxFileExists(new_name))
730 {
731 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
732 dialog.ShowModal();
733 event.Veto();
734 }
735
736 if (wxRenameFile(fd->GetFullName(),new_name))
737 {
738 fd->SetNewName( new_name, event.GetLabel() );
739 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
740 EnsureVisible( event.GetItem() );
741 }
742 else
743 {
744 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
745 dialog.ShowModal();
746 event.Veto();
747 }
748 }
749
750 //-----------------------------------------------------------------------------
751 // wxFileDialog
752 //-----------------------------------------------------------------------------
753
754 #define ID_LIST_MODE wxID_FILEDLGG
755 #define ID_REPORT_MODE wxID_FILEDLGG + 1
756 #define ID_UP_DIR wxID_FILEDLGG + 5
757 #define ID_PARENT_DIR wxID_FILEDLGG + 6
758 #define ID_NEW_DIR wxID_FILEDLGG + 7
759 #define ID_CHOICE wxID_FILEDLGG + 8
760 #define ID_TEXT wxID_FILEDLGG + 9
761 #define ID_LIST_CTRL wxID_FILEDLGG + 10
762 #define ID_ACTIVATED wxID_FILEDLGG + 11
763 #define ID_CHECK wxID_FILEDLGG + 12
764
765 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
766
767 BEGIN_EVENT_TABLE(wxFileDialog,wxDialog)
768 EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList)
769 EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport)
770 EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp)
771 EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome)
772 EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew)
773 EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk)
774 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected)
775 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated)
776 EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice)
777 EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter)
778 EVT_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck)
779 END_EVENT_TABLE()
780
781 long wxFileDialog::s_lastViewStyle = wxLC_LIST;
782 bool wxFileDialog::s_lastShowHidden = FALSE;
783
784 wxFileDialog::wxFileDialog(wxWindow *parent,
785 const wxString& message,
786 const wxString& defaultDir,
787 const wxString& defaultFile,
788 const wxString& wildCard,
789 long style,
790 const wxPoint& pos ) :
791 wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
792 {
793 wxBeginBusyCursor();
794
795 if (wxConfig::Get(FALSE))
796 {
797 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle);
798 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden);
799 }
800
801 m_message = message;
802 m_dialogStyle = style;
803
804 if (m_dialogStyle == 0) m_dialogStyle = wxOPEN;
805 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
806 m_dialogStyle |= wxOPEN;
807
808 m_dir = defaultDir;
809 if ((m_dir.IsEmpty()) || (m_dir == wxT(".")))
810 {
811 char buf[200];
812 m_dir = getcwd( buf, sizeof(buf) );
813 }
814 m_path = defaultDir;
815 m_path += wxT("/");
816 m_path += defaultFile;
817 m_fileName = defaultFile;
818 m_wildCard = wildCard;
819 m_filterIndex = 0;
820 m_filterExtension = wxEmptyString;
821
822 // interpret wildcards
823
824 if (m_wildCard.IsEmpty())
825 m_wildCard = _("All files (*)|*");
826
827 wxStringTokenizer tokens( m_wildCard, wxT("|") );
828 wxString firstWild;
829 wxString firstWildText;
830 if (tokens.CountTokens() == 1)
831 {
832 firstWildText = tokens.GetNextToken();
833 firstWild = firstWildText;
834 }
835 else
836 {
837 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
838 firstWildText = tokens.GetNextToken();
839 firstWild = tokens.GetNextToken();
840 }
841 if ( firstWild.Left( 2 ) == wxT("*.") )
842 m_filterExtension = firstWild.Mid( 1 );
843 if ( m_filterExtension == ".*" ) m_filterExtension = wxEmptyString;
844
845 // layout
846
847 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
848
849 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
850
851 wxBitmapButton *but;
852
853 but = new wxBitmapButton( this, ID_LIST_MODE, wxBitmap( listview_xpm ) );
854 #if wxUSE_TOOLTIPS
855 but->SetToolTip( _("View files as a list view") );
856 #endif
857 buttonsizer->Add( but, 0, wxALL, 5 );
858
859 but = new wxBitmapButton( this, ID_REPORT_MODE, wxBitmap( repview_xpm ) );
860 #if wxUSE_TOOLTIPS
861 but->SetToolTip( _("View files as a detailed view") );
862 #endif
863 buttonsizer->Add( but, 0, wxALL, 5 );
864
865 buttonsizer->Add( 30, 5, 1 );
866
867 but = new wxBitmapButton( this, ID_UP_DIR, wxBitmap( dir_up_xpm ) );
868 #if wxUSE_TOOLTIPS
869 but->SetToolTip( _("Go to parent directory") );
870 #endif
871 buttonsizer->Add( but, 0, wxALL, 5 );
872
873 but = new wxBitmapButton( this, ID_PARENT_DIR, wxBitmap(home_xpm) );
874 #if wxUSE_TOOLTIPS
875 but->SetToolTip( _("Go to home directory") );
876 #endif
877 buttonsizer->Add( but, 0, wxALL, 5);
878
879 buttonsizer->Add( 20, 20 );
880
881 but = new wxBitmapButton( this, ID_NEW_DIR, wxBitmap(new_dir_xpm) );
882 #if wxUSE_TOOLTIPS
883 but->SetToolTip( _("Create new directory") );
884 #endif
885 buttonsizer->Add( but, 0, wxALL, 5 );
886
887 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
888
889 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
890 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
891 m_static = new wxStaticText( this, -1, m_dir );
892 staticsizer->Add( m_static, 1 );
893 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
894
895 if (m_dialogStyle & wxMULTIPLE)
896 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition,
897 wxSize(440,180), s_lastViewStyle | wxSUNKEN_BORDER );
898 else
899 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition,
900 wxSize(440,180), s_lastViewStyle | wxSUNKEN_BORDER | wxLC_SINGLE_SEL );
901 m_list -> ShowHidden(s_lastShowHidden);
902 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
903
904 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
905 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
906 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
907 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
908 mainsizer->Add( textsizer, 0, wxEXPAND );
909
910 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
911 m_choice = new wxChoice( this, ID_CHOICE );
912 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
913 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
914 m_check->SetValue( s_lastShowHidden );
915 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
916 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
917 mainsizer->Add( choicesizer, 0, wxEXPAND );
918
919 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
920 while (tokens.HasMoreTokens())
921 {
922 firstWildText = tokens.GetNextToken();
923 firstWild = tokens.GetNextToken();
924 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
925 }
926 m_choice->SetSelection( 0 );
927
928 SetAutoLayout( TRUE );
929 SetSizer( mainsizer );
930
931 mainsizer->Fit( this );
932 mainsizer->SetSizeHints( this );
933
934 Centre( wxBOTH );
935
936 /*
937 if (m_fileName.IsEmpty())
938 m_list->SetFocus();
939 else
940 */
941 m_text->SetFocus();
942
943 wxEndBusyCursor();
944 }
945
946 wxFileDialog::~wxFileDialog()
947 {
948 if (wxConfig::Get(FALSE))
949 {
950 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle);
951 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden);
952 }
953 }
954
955 void wxFileDialog::OnChoice( wxCommandEvent &event )
956 {
957 int index = (int)event.GetInt();
958 wxString *str = (wxString*) m_choice->GetClientData( index );
959 m_list->SetWild( *str );
960 m_filterIndex = index;
961 if ( str -> Left( 2 ) == wxT("*.") )
962 {
963 m_filterExtension = str -> Mid( 1 );
964 if (m_filterExtension == ".*") m_filterExtension = wxEmptyString;
965 }
966 else
967 m_filterExtension = wxEmptyString;
968 }
969
970 void wxFileDialog::OnCheck( wxCommandEvent &event )
971 {
972 m_list->ShowHidden( (s_lastShowHidden = event.GetInt() != 0) );
973 }
974
975 void wxFileDialog::OnActivated( wxListEvent &event )
976 {
977 HandleAction( event.m_item.m_text );
978 }
979
980 void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
981 {
982 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
983 cevent.SetEventObject( this );
984 GetEventHandler()->ProcessEvent( cevent );
985 }
986
987 void wxFileDialog::OnSelected( wxListEvent &event )
988 {
989 if (FindFocus() != m_list) return;
990
991 wxString filename( event.m_item.m_text );
992 if (filename == wxT("..")) return;
993
994 wxString dir;
995 m_list->GetDir( dir );
996 if (dir != wxT("/")) dir += wxT("/");
997 dir += filename;
998 if (wxDirExists(dir)) return;
999
1000 m_text->SetValue( filename );
1001 }
1002
1003 void wxFileDialog::HandleAction( const wxString &fn )
1004 {
1005 wxString filename( fn );
1006 wxString dir;
1007 m_list->GetDir( dir );
1008 if (filename.IsEmpty()) return;
1009 if (filename == wxT(".")) return;
1010
1011 if (filename == wxT(".."))
1012 {
1013 m_list->GoToParentDir();
1014 m_list->SetFocus();
1015 m_list->GetDir( dir );
1016 m_static->SetLabel( dir );
1017 return;
1018 }
1019
1020 if (filename == wxT("~"))
1021 {
1022 m_list->GoToHomeDir();
1023 m_list->SetFocus();
1024 m_list->GetDir( dir );
1025 m_static->SetLabel( dir );
1026 return;
1027 }
1028
1029 if (filename[0u] == wxT('~'))
1030 {
1031 filename.Remove( 0, 1 );
1032 wxString tmp( wxGetUserHome() );
1033 tmp += wxT('/');
1034 tmp += filename;
1035 filename = tmp;
1036 }
1037
1038 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
1039 (filename.Find(wxT('?')) != wxNOT_FOUND))
1040 {
1041 if (filename.Find(wxT('/')) != wxNOT_FOUND)
1042 {
1043 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
1044 return;
1045 }
1046 m_list->SetWild( filename );
1047 return;
1048 }
1049
1050 if (dir != wxT("/")) dir += wxT("/");
1051 if (filename[0u] != wxT('/'))
1052 {
1053 dir += filename;
1054 filename = dir;
1055 }
1056
1057 if (wxDirExists(filename))
1058 {
1059 m_list->GoToDir( filename );
1060 m_list->GetDir( dir );
1061 m_static->SetLabel( dir );
1062 return;
1063 }
1064
1065
1066 if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) )
1067 {
1068 if (filename.Find( wxT('.') ) == wxNOT_FOUND ||
1069 filename.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND)
1070 filename << m_filterExtension;
1071 if (wxFileExists( filename ))
1072 {
1073 wxString msg;
1074 msg.Printf( _("File '%s' already exists, do you really want to "
1075 "overwrite it?"), filename.c_str() );
1076
1077 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1078 return;
1079 }
1080 }
1081 else if ( m_dialogStyle & wxOPEN )
1082 {
1083 if ( !wxFileExists( filename ) )
1084 if (filename.Find( wxT('.') ) == wxNOT_FOUND ||
1085 filename.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND)
1086 filename << m_filterExtension;
1087
1088 if ( m_dialogStyle & wxFILE_MUST_EXIST )
1089 {
1090 if ( !wxFileExists( filename ) )
1091 {
1092 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR );
1093 return;
1094 }
1095 }
1096 }
1097
1098 SetPath( filename );
1099
1100 // change to the directory where the user went if asked
1101 if ( m_dialogStyle & wxCHANGE_DIR )
1102 {
1103 wxString cwd;
1104 wxSplitPath(filename, &cwd, NULL, NULL);
1105
1106 if ( cwd != wxGetWorkingDirectory() )
1107 {
1108 wxSetWorkingDirectory(cwd);
1109 }
1110 }
1111
1112 wxCommandEvent event;
1113 wxDialog::OnOK(event);
1114 }
1115
1116 void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
1117 {
1118 HandleAction( m_text->GetValue() );
1119 }
1120
1121 void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
1122 {
1123 m_list->ChangeToListMode();
1124 s_lastViewStyle = wxLC_LIST;
1125 m_list->SetFocus();
1126 }
1127
1128 void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
1129 {
1130 m_list->ChangeToReportMode();
1131 s_lastViewStyle = wxLC_REPORT;
1132 m_list->SetFocus();
1133 }
1134
1135 void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
1136 {
1137 m_list->GoToParentDir();
1138 m_list->SetFocus();
1139 wxString dir;
1140 m_list->GetDir( dir );
1141 m_static->SetLabel( dir );
1142 }
1143
1144 void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
1145 {
1146 m_list->GoToHomeDir();
1147 m_list->SetFocus();
1148 wxString dir;
1149 m_list->GetDir( dir );
1150 m_static->SetLabel( dir );
1151 }
1152
1153 void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
1154 {
1155 m_list->MakeDir();
1156 }
1157
1158 void wxFileDialog::SetPath( const wxString& path )
1159 {
1160 // not only set the full path but also update filename and dir
1161 m_path = path;
1162 if ( !!path )
1163 {
1164 wxString ext;
1165 wxSplitPath(path, &m_dir, &m_fileName, &ext);
1166 if (!ext.IsEmpty())
1167 {
1168 m_fileName += wxT(".");
1169 m_fileName += ext;
1170 }
1171 }
1172 }
1173
1174 void wxFileDialog::GetPaths( wxArrayString& paths ) const
1175 {
1176 paths.Empty();
1177 if (m_list->GetSelectedItemCount() == 0)
1178 {
1179 paths.Add( GetPath() );
1180 return;
1181 }
1182
1183 paths.Alloc( m_list->GetSelectedItemCount() );
1184
1185 wxString dir;
1186 m_list->GetDir( dir );
1187 if (dir != wxT("/")) dir += wxT("/");
1188
1189 wxListItem item;
1190 item.m_mask = wxLIST_MASK_TEXT;
1191
1192 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1193 while ( item.m_itemId != -1 )
1194 {
1195 m_list->GetItem( item );
1196 paths.Add( dir + item.m_text );
1197 item.m_itemId = m_list->GetNextItem( item.m_itemId,
1198 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1199 }
1200 }
1201
1202 void wxFileDialog::GetFilenames(wxArrayString& files) const
1203 {
1204 files.Empty();
1205 if (m_list->GetSelectedItemCount() == 0)
1206 {
1207 files.Add( GetFilename() );
1208 return;
1209 }
1210 files.Alloc( m_list->GetSelectedItemCount() );
1211
1212 wxListItem item;
1213 item.m_mask = wxLIST_MASK_TEXT;
1214
1215 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1216 while ( item.m_itemId != -1 )
1217 {
1218 m_list->GetItem( item );
1219 files.Add( item.m_text );
1220 item.m_itemId = m_list->GetNextItem( item.m_itemId,
1221 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1222 }
1223 }
1224
1225
1226
1227 // ----------------------------------------------------------------------------
1228 // global functions
1229 // ----------------------------------------------------------------------------
1230
1231 wxString
1232 wxFileSelectorEx(const wxChar *message,
1233 const wxChar *default_path,
1234 const wxChar *default_filename,
1235 int *WXUNUSED(indexDefaultExtension),
1236 const wxChar *wildcard,
1237 int flags,
1238 wxWindow *parent,
1239 int x, int y)
1240 {
1241 // TODO: implement this somehow
1242 return wxFileSelector(message, default_path, default_filename, wxT(""),
1243 wildcard, flags, parent, x, y);
1244 }
1245
1246 wxString wxFileSelector( const wxChar *title,
1247 const wxChar *defaultDir, const wxChar *defaultFileName,
1248 const wxChar *defaultExtension, const wxChar *filter, int flags,
1249 wxWindow *parent, int x, int y )
1250 {
1251 wxString filter2;
1252 if ( defaultExtension && !filter )
1253 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
1254 else if ( filter )
1255 filter2 = filter;
1256
1257 wxString defaultDirString;
1258 if (defaultDir)
1259 defaultDirString = defaultDir;
1260
1261 wxString defaultFilenameString;
1262 if (defaultFileName)
1263 defaultFilenameString = defaultFileName;
1264
1265 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
1266
1267 if ( fileDialog.ShowModal() == wxID_OK )
1268 {
1269 return fileDialog.GetPath();
1270 }
1271 else
1272 {
1273 return wxEmptyString;
1274 }
1275 }
1276
1277 wxString wxLoadFileSelector( const wxChar *what, const wxChar *ext, const wxChar *default_name, wxWindow *parent )
1278 {
1279 wxString prompt = wxString::Format(_("Load %s file"), what);
1280
1281 if (*ext == wxT('.'))
1282 ext++;
1283
1284 wxString wild = wxString::Format(_T("*.%s"), ext);
1285
1286 return wxFileSelector(prompt, (const wxChar *) NULL, default_name,
1287 ext, wild, 0, parent);
1288 }
1289
1290 wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name,
1291 wxWindow *parent )
1292 {
1293 wxChar *ext = (wxChar *)extension;
1294
1295 wxString prompt = wxString::Format(_("Save %s file"), what);
1296
1297 if (*ext == wxT('.'))
1298 ext++;
1299
1300 wxString wild = wxString::Format(_T("*.%s"), ext);
1301
1302 return wxFileSelector(prompt, (const wxChar *) NULL, default_name,
1303 ext, wild, 0, parent);
1304 }
1305
1306
1307
1308
1309
1310
1311 // A module to allow icons table cleanup
1312
1313 class wxFileDialogGenericModule: public wxModule
1314 {
1315 DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule)
1316 public:
1317 wxFileDialogGenericModule() {}
1318 bool OnInit() { return TRUE; }
1319 void OnExit() { if (g_IconsTable) {delete g_IconsTable; g_IconsTable = NULL;} }
1320 };
1321
1322 IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule, wxModule)