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