]> git.saurik.com Git - wxWidgets.git/blob - src/generic/filedlgg.cpp
234b5b841a79b85ca3a2260e5dfc8a6d8439f54c
[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
36 #if wxUSE_TOOLTIPS
37 #include "wx/tooltip.h"
38 #endif
39
40 #include "sys/types.h"
41 #include "sys/stat.h"
42 #include "dirent.h"
43 #include "pwd.h"
44 #include "grp.h"
45 #include "time.h"
46
47 #include "wx/generic/home.xpm"
48 #include "wx/generic/listview.xpm"
49 #include "wx/generic/repview.xpm"
50 #include "wx/generic/new_dir.xpm"
51 #include "wx/generic/dir_up.xpm"
52
53 /* XPM */
54 static char * folder_xpm[] = {
55 /* width height ncolors chars_per_pixel */
56 "16 16 6 1",
57 /* colors */
58 " s None c None",
59 ". c #000000",
60 "+ c #c0c0c0",
61 "@ c #808080",
62 "# c #ffff00",
63 "$ c #ffffff",
64 /* pixels */
65 " ",
66 " @@@@@ ",
67 " @#+#+#@ ",
68 " @#+#+#+#@@@@@@ ",
69 " @$$$$$$$$$$$$@.",
70 " @$#+#+#+#+#+#@.",
71 " @$+#+#+#+#+#+@.",
72 " @$#+#+#+#+#+#@.",
73 " @$+#+#+#+#+#+@.",
74 " @$#+#+#+#+#+#@.",
75 " @$+#+#+#+#+#+@.",
76 " @$#+#+#+#+#+#@.",
77 " @@@@@@@@@@@@@@.",
78 " ..............",
79 " ",
80 " "};
81
82 //-----------------------------------------------------------------------------
83 // wxFileData
84 //-----------------------------------------------------------------------------
85
86 IMPLEMENT_DYNAMIC_CLASS(wxFileData,wxObject);
87
88 wxFileData::wxFileData( const wxString &name, const wxString &fname )
89 {
90 m_name = name;
91 m_fileName = fname;
92
93 struct stat buff;
94 stat( m_fileName.GetData(), &buff );
95 struct stat lbuff;
96 lstat( m_fileName.GetData(), &lbuff );
97
98 struct tm *t = localtime( &lbuff.st_mtime );
99 // struct passwd *user = getpwuid( buff.st_uid );
100 // struct group *grp = getgrgid( buff.st_gid );
101
102 m_isDir = S_ISDIR( buff.st_mode );
103 m_isLink = S_ISLNK( lbuff.st_mode );
104 m_isExe = ((buff.st_mode & S_IXUSR ) == S_IXUSR );
105
106 m_size = buff.st_size;
107
108 m_hour = t->tm_hour;
109 m_minute = t->tm_min;
110 m_month = t->tm_mon+1;
111 m_day = t->tm_mday;
112 m_year = t->tm_year;
113
114 m_permissions.sprintf( "%c%c%c",
115 ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? 'r' : '-'),
116 ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? 'w' : '-'),
117 ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? 'x' : '-') );
118 }
119
120 wxString wxFileData::GetName() const
121 {
122 return m_name;
123 }
124
125 wxString wxFileData::GetFullName() const
126 {
127 return m_fileName;
128 }
129
130 wxString wxFileData::GetHint() const
131 {
132 wxString s = m_fileName;
133 s += " ";
134 if (m_isDir) s += _("<DIR> ");
135 else if (m_isLink) s += _("<LINK> ");
136 else
137 {
138 s += LongToString( m_size );
139 s += _(" bytes ");
140 }
141 s += IntToString( m_day );
142 s += _T(".");
143 s += IntToString( m_month );
144 s += _T(".");
145 s += IntToString( m_year );
146 s += _T(" ");
147 s += IntToString( m_hour );
148 s += _T(":");
149 s += IntToString( m_minute );
150 s += _T(" ");
151 s += m_permissions;
152 return s;
153 };
154
155 wxString wxFileData::GetEntry( const int num )
156 {
157 wxString s;
158 switch (num)
159 {
160 case 0:
161 s = m_name;
162 break;
163 case 1:
164 if (m_isDir) s = _("<DIR>");
165 else if (m_isLink) s = _("<LINK>");
166 else s = LongToString( m_size );
167 break;
168 case 2:
169 if (m_day < 10) s = _T("0"); else s = _T("");
170 s += IntToString( m_day );
171 s += _T(".");
172 if (m_month < 10) s += _T("0");
173 s += IntToString( m_month );
174 s += _T(".");
175 if (m_year < 10) s += _T("0"); // this should happen real soon...
176 s += IntToString( m_year );
177 break;
178 case 3:
179 if (m_hour < 10) s = _T("0"); else s = _T("");
180 s += IntToString( m_hour );
181 s += _T(":");
182 if (m_minute < 10) s += _T("0");
183 s += IntToString( m_minute );
184 break;
185 case 4:
186 s = m_permissions;
187 break;
188 default:
189 s = _T("No entry");
190 break;
191 }
192 return s;
193 }
194
195 bool wxFileData::IsDir()
196 {
197 return m_isDir;
198 }
199
200 bool wxFileData::IsExe()
201 {
202 return m_isExe;
203 }
204
205 bool wxFileData::IsLink()
206 {
207 return m_isLink;
208 }
209
210 long wxFileData::GetSize()
211 {
212 return m_size;
213 }
214
215 void wxFileData::SetNewName( const wxString &name, const wxString &fname )
216 {
217 m_name = name;
218 m_fileName = fname;
219 }
220
221 void wxFileData::MakeItem( wxListItem &item )
222 {
223 item.m_text = m_name;
224 item.m_colour = wxBLACK;
225 if (IsExe()) item.m_colour = wxRED;
226 if (IsDir()) item.m_colour = wxBLUE;
227 if (IsDir()) item.m_image = 0; else item.m_image = -1;
228 if (IsLink())
229 {
230 wxColour *dg = wxTheColourDatabase->FindColour( "MEDIUM GREY" );
231 item.m_colour = dg;
232 }
233 item.m_data = (long)this;
234 }
235
236 //-----------------------------------------------------------------------------
237 // wxFileCtrl
238 //-----------------------------------------------------------------------------
239
240 IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl);
241
242 BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
243 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem)
244 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit)
245 END_EVENT_TABLE()
246
247 wxFileCtrl::wxFileCtrl()
248 {
249 m_dirName = _T("/");
250 m_showHidden = FALSE;
251 }
252
253 wxFileCtrl::wxFileCtrl( wxWindow *win, wxWindowID id,
254 const wxString &dirName, const wxString &wild,
255 const wxPoint &pos, const wxSize &size,
256 long style, const wxValidator &validator, const wxString &name ) :
257 wxListCtrl( win, id, pos, size, style, validator, name )
258 {
259 wxImageList *imageList = new wxImageList( 16, 16 );
260 imageList->Add( wxBitmap( folder_xpm ) );
261 SetImageList( imageList, wxIMAGE_LIST_SMALL );
262
263 m_dirName = dirName;
264 m_wild = wild;
265 m_showHidden = FALSE;
266 Update();
267 }
268
269 void wxFileCtrl::ChangeToListMode()
270 {
271 SetSingleStyle( wxLC_LIST );
272 Update();
273 }
274
275 void wxFileCtrl::ChangeToReportMode()
276 {
277 SetSingleStyle( wxLC_REPORT );
278 Update();
279 }
280
281 void wxFileCtrl::ChangeToIconMode()
282 {
283 SetSingleStyle( wxLC_ICON );
284 Update();
285 }
286
287 void wxFileCtrl::ShowHidden( bool show )
288 {
289 m_showHidden = show;
290 Update();
291 }
292
293 int ListCompare( const long data1, const long data2, const long WXUNUSED(data) )
294 {
295 wxFileData *fd1 = (wxFileData*)data1 ;
296 wxFileData *fd2 = (wxFileData*)data2 ;
297 if (fd1->GetName() == _T("..")) return -1;
298 if (fd2->GetName() == _T("..")) return 1;
299 if (fd1->IsDir() && !fd2->IsDir()) return -1;
300 if (fd2->IsDir() && !fd1->IsDir()) return 1;
301 return strcmp( fd1->GetName(), fd2->GetName() );
302 }
303
304 long wxFileCtrl::Add( wxFileData *fd, wxListItem &item )
305 {
306 long ret = -1;
307 item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
308 fd->MakeItem( item );
309 long my_style = GetWindowStyleFlag();
310 if (my_style & wxLC_REPORT)
311 {
312 ret = InsertItem( item );
313 for (int i = 1; i < 5; i++) SetItem( item.m_itemId, i, fd->GetEntry( i) );
314 }
315 else if (my_style & wxLC_LIST)
316 {
317 ret = InsertItem( item );
318 }
319 return ret;
320 }
321
322 void wxFileCtrl::Update()
323 {
324 ClearAll();
325 long my_style = GetWindowStyleFlag();
326 if (my_style & wxLC_REPORT)
327 {
328 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, 130 );
329 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, 60 );
330 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT, 55 );
331 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT, 50 );
332 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, 120 );
333 }
334 wxFileData *fd = (wxFileData *) NULL;
335 wxListItem item;
336 item.m_itemId = 0;
337 item.m_col = 0;
338
339 if (m_dirName != _T("/"))
340 {
341 wxString p( wxPathOnly(m_dirName) );
342 if (p.IsEmpty()) p = _T("/");
343 fd = new wxFileData( _T(".."), p );
344 Add( fd, item );
345 item.m_itemId++;
346 }
347
348 wxString res = m_dirName + _T("/*");
349 wxString f( wxFindFirstFile( res.GetData(), wxDIR ) );
350 while (!f.IsEmpty())
351 {
352 res = wxFileNameFromPath( f );
353 fd = new wxFileData( res, f );
354 wxString s = fd->GetName();
355 if (m_showHidden || (s[0] != _T('.')))
356 {
357 Add( fd, item );
358 item.m_itemId++;
359 }
360 f = wxFindNextFile();
361 }
362
363 res = m_dirName + _T("/") + m_wild;
364 f = wxFindFirstFile( res.GetData(), wxFILE );
365 while (!f.IsEmpty())
366 {
367 res = wxFileNameFromPath( f );
368 fd = new wxFileData( res, f );
369 wxString s = fd->GetName();
370 if (m_showHidden || (s[0] != _T('.')))
371 {
372 Add( fd, item );
373 item.m_itemId++;
374 }
375 f = wxFindNextFile();
376 }
377
378 SortItems( ListCompare, 0 );
379 }
380
381 void wxFileCtrl::SetWild( const wxString &wild )
382 {
383 m_wild = wild;
384 Update();
385 }
386
387 void wxFileCtrl::MakeDir()
388 {
389 wxString new_name( _T("NewName") );
390 wxString path( m_dirName );
391 path += _T( "/" );
392 path += new_name;
393 if (wxFileExists(path))
394 {
395 // try NewName0, NewName1 etc.
396 int i = 0;
397 do {
398 new_name = _("NewName");
399 wxString num;
400 num.Printf( _T("%d"), i );
401 new_name += num;
402
403 path = m_dirName;
404 path += _T("/");
405 path += new_name;
406 i++;
407 } while (wxFileExists(path));
408 }
409
410 wxLogNull log;
411 if (!wxMkdir(path))
412 {
413 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
414 dialog.ShowModal();
415 return;
416 }
417
418 wxFileData *fd = new wxFileData( new_name, path );
419 wxListItem item;
420 item.m_itemId = 0;
421 item.m_col = 0;
422 int id = Add( fd, item );
423
424 if (id != -1)
425 {
426 SortItems( ListCompare, 0 );
427 id = FindItem( 0, (long)fd );
428 EnsureVisible( id );
429 EditLabel( id );
430 }
431 }
432
433 void wxFileCtrl::GoToParentDir()
434 {
435 if (m_dirName != _T("/"))
436 {
437 wxString fname( wxFileNameFromPath(m_dirName) );
438 m_dirName = wxPathOnly( m_dirName );
439 if (m_dirName.IsEmpty()) m_dirName = _T("/");
440 Update();
441 int id = FindItem( 0, fname );
442 if (id != -1)
443 {
444 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
445 EnsureVisible( id );
446 }
447 }
448 }
449
450 void wxFileCtrl::GoToHomeDir()
451 {
452 wxString s = wxGetUserHome( wxString() );
453 m_dirName = s;
454 Update();
455 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
456 EnsureVisible( 0 );
457 }
458
459 void wxFileCtrl::GoToDir( const wxString &dir )
460 {
461 m_dirName = dir;
462 Update();
463 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
464 EnsureVisible( 0 );
465 }
466
467 void wxFileCtrl::GetDir( wxString &dir )
468 {
469 dir = m_dirName;
470 }
471
472 void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
473 {
474 wxFileData *fd = (wxFileData*)event.m_item.m_data;
475 delete fd;
476 }
477
478 void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
479 {
480 wxFileData *fd = (wxFileData*)event.m_item.m_data;
481 wxASSERT( fd );
482
483 if ((event.GetLabel().IsEmpty()) ||
484 (event.GetLabel() == _(".")) ||
485 (event.GetLabel() == _("..")) ||
486 (event.GetLabel().First( _T("/") ) != wxNOT_FOUND))
487 {
488 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
489 dialog.ShowModal();
490 event.Veto();
491 return;
492 }
493
494 wxString new_name( wxPathOnly( fd->GetFullName() ) );
495 new_name += _T("/");
496 new_name += event.GetLabel();
497
498 wxLogNull log;
499
500 if (wxFileExists(new_name))
501 {
502 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
503 dialog.ShowModal();
504 event.Veto();
505 }
506
507 if (wxRenameFile(fd->GetFullName(),new_name))
508 {
509 fd->SetNewName( new_name, event.GetLabel() );
510 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
511 EnsureVisible( event.GetItem() );
512 }
513 else
514 {
515 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
516 dialog.ShowModal();
517 event.Veto();
518 }
519 }
520
521 //-----------------------------------------------------------------------------
522 // wxFileDialog
523 //-----------------------------------------------------------------------------
524
525 #define ID_LIST_MODE 5000
526 #define ID_REPORT_MODE 5001
527 #define ID_UP_DIR 5005
528 #define ID_PARENT_DIR 5006
529 #define ID_NEW_DIR 5007
530 #define ID_CHOICE 5008
531 #define ID_TEXT 5009
532 #define ID_LIST_CTRL 5010
533
534 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
535
536 BEGIN_EVENT_TABLE(wxFileDialog,wxDialog)
537 EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList)
538 EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport)
539 EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp)
540 EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome)
541 EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew)
542 EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk)
543 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected)
544 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated)
545 EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice)
546 EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter)
547 END_EVENT_TABLE()
548
549 wxFileDialog::wxFileDialog(wxWindow *parent,
550 const wxString& message,
551 const wxString& defaultDir,
552 const wxString& defaultFile,
553 const wxString& wildCard,
554 long style,
555 const wxPoint& pos ) :
556 wxDialog( parent, -1, message, pos, wxDefaultSize, style | wxRESIZE_BORDER )
557 {
558 wxBeginBusyCursor();
559
560 m_message = message;
561 m_dialogStyle = style;
562 m_dir = defaultDir;
563 if (m_dir.IsEmpty()) m_dir = wxGetUserHome();
564 m_path = defaultDir;
565 m_path += _T("/");
566 m_path += defaultFile;
567 m_fileName = defaultFile;
568 m_wildCard = wildCard;
569 m_filterIndex = 0;
570
571 // interpret wildcards
572
573 if (m_wildCard.IsEmpty())
574 m_wildCard = _("All files (*)|*");
575
576 wxStringTokenizer tokens( m_wildCard, _T("|") );
577 wxString firstWild;
578 wxString firstWildText;
579 if (tokens.CountTokens() == 1)
580 {
581 firstWildText = tokens.GetNextToken();
582 firstWild = firstWildText;
583 }
584 else
585 {
586 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, _T("Wrong file type descripition") );
587 firstWildText = tokens.GetNextToken();
588 firstWild = tokens.GetNextToken();
589 }
590
591 // layout
592
593 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
594
595 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
596
597 wxBitmapButton *but;
598
599 but = new wxBitmapButton( this, ID_LIST_MODE, wxBitmap( listview_xpm ) );
600 #if wxUSE_TOOLTIPS
601 but->SetToolTip( _("View files as a list view") );
602 #endif
603 buttonsizer->Add( but, 0, wxALL, 5 );
604
605 but = new wxBitmapButton( this, ID_REPORT_MODE, wxBitmap( repview_xpm ) );
606 #if wxUSE_TOOLTIPS
607 but->SetToolTip( _("View files as a detailed view") );
608 #endif
609 buttonsizer->Add( but, 0, wxALL, 5 );
610
611 buttonsizer->Add( 30, 5, 1 );
612
613 but = new wxBitmapButton( this, ID_UP_DIR, wxBitmap( dir_up_xpm ) );
614 #if wxUSE_TOOLTIPS
615 but->SetToolTip( _("Go to parent directory") );
616 #endif
617 buttonsizer->Add( but, 0, wxALL, 5 );
618
619 but = new wxBitmapButton( this, ID_PARENT_DIR, wxBitmap(home_xpm) );
620 #if wxUSE_TOOLTIPS
621 but->SetToolTip( _("Go to home directory") );
622 #endif
623 buttonsizer->Add( but, 0, wxALL, 5);
624
625 buttonsizer->Add( 20, 20 );
626
627 but = new wxBitmapButton( this, ID_NEW_DIR, wxBitmap(new_dir_xpm) );
628 #if wxUSE_TOOLTIPS
629 but->SetToolTip( _("Create new directory") );
630 #endif
631 buttonsizer->Add( but, 0, wxALL, 5 );
632
633 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
634
635 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition, wxSize(440,180),
636 wxLC_LIST | wxSUNKEN_BORDER | wxLC_SINGLE_SEL );
637 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
638
639 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
640 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
641 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
642 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
643 mainsizer->Add( textsizer, 0, wxEXPAND );
644
645 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
646 m_choice = new wxChoice( this, ID_CHOICE );
647 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
648 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
649 mainsizer->Add( choicesizer, 0, wxEXPAND );
650
651 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
652 while (tokens.HasMoreTokens())
653 {
654 firstWildText = tokens.GetNextToken();
655 firstWild = tokens.GetNextToken();
656 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
657 }
658 m_choice->SetSelection( 0 );
659
660 SetAutoLayout( TRUE );
661 SetSizer( mainsizer );
662
663 mainsizer->Fit( this );
664 mainsizer->SetSizeHints( this );
665
666 Centre( wxBOTH );
667
668 m_list->SetFocus();
669
670 wxEndBusyCursor();
671 }
672
673 wxFileDialog::~wxFileDialog()
674 {
675 }
676
677 void wxFileDialog::OnChoice( wxCommandEvent &event )
678 {
679 wxString *str = (wxString*) m_choice->GetClientData( event.GetInt() );
680 m_list->SetWild( *str );
681 }
682
683 void wxFileDialog::OnActivated( wxListEvent &WXUNUSED(event) )
684 {
685 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
686 cevent.SetEventObject( this );
687 GetEventHandler()->ProcessEvent( cevent );
688 }
689
690 void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
691 {
692 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
693 cevent.SetEventObject( this );
694 GetEventHandler()->ProcessEvent( cevent );
695 }
696
697 void wxFileDialog::OnSelected( wxListEvent &event )
698 {
699 if (FindFocus() == m_list)
700 m_text->SetValue( event.m_item.m_text );
701 }
702
703 void wxFileDialog::OnListOk( wxCommandEvent &event )
704 {
705 wxString filename( m_text->GetValue() );
706 wxString dir;
707 m_list->GetDir( dir );
708 if (filename.IsEmpty()) return;
709 if (filename == _T(".")) return;
710
711 if (filename == _T(".."))
712 {
713 m_list->GoToParentDir();
714 m_list->SetFocus();
715 return;
716 }
717
718 if ((filename.Find(_T('*')) != wxNOT_FOUND) ||
719 (filename.Find(_T('?')) != wxNOT_FOUND))
720 {
721 if (filename.Find(_T('/')) != wxNOT_FOUND)
722 {
723 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
724 return;
725 }
726 m_list->SetWild( filename );
727 return;
728 }
729
730 if (dir != _T("/")) dir += _T("/");
731 dir += filename;
732 filename = dir;
733
734 if (wxDirExists(filename))
735 {
736 m_list->GoToDir( filename );
737 m_text->SetValue( _T("") );
738 return;
739 }
740
741 if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) )
742 {
743 if (wxFileExists( filename ))
744 {
745 wxString msg;
746 msg.Printf( _("File '%s' already exists, do you really want to "
747 "overwrite it?"), filename.c_str() );
748
749 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
750 return;
751 }
752 }
753 else if ( (m_dialogStyle & wxOPEN) && (m_dialogStyle & wxFILE_MUST_EXIST) )
754 {
755 if ( !wxFileExists( filename ) )
756 {
757 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR );
758 return;
759 }
760 }
761
762 SetPath( filename );
763 event.Skip();
764 }
765
766 void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
767 {
768 m_list->ChangeToListMode();
769 m_list->SetFocus();
770 }
771
772 void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
773 {
774 m_list->ChangeToReportMode();
775 m_list->SetFocus();
776 }
777
778 void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
779 {
780 m_list->GoToParentDir();
781 m_list->SetFocus();
782 }
783
784 void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
785 {
786 m_list->GoToHomeDir();
787 m_list->SetFocus();
788 }
789
790 void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
791 {
792 m_list->MakeDir();
793 }
794
795 void wxFileDialog::SetPath( const wxString& path )
796 {
797 // not only set the full path but also update filename and dir
798 m_path = path;
799 if ( !!path )
800 {
801 wxString ext;
802 wxSplitPath(path, &m_dir, &m_fileName, &ext);
803 if (!ext.IsEmpty())
804 {
805 m_fileName += _T(".");
806 m_fileName += ext;
807 }
808 }
809 }
810
811 // ----------------------------------------------------------------------------
812 // global functions
813 // ----------------------------------------------------------------------------
814
815 wxString
816 wxFileSelectorEx(const wxChar *message,
817 const wxChar *default_path,
818 const wxChar *default_filename,
819 int *indexDefaultExtension,
820 const wxChar *wildcard,
821 int flags,
822 wxWindow *parent,
823 int x, int y)
824 {
825 // TODO: implement this somehow
826 return wxFileSelector(message, default_path, default_filename, _T(""),
827 wildcard, flags, parent, x, y);
828 }
829
830 wxString wxFileSelector( const wxChar *title,
831 const wxChar *defaultDir, const wxChar *defaultFileName,
832 const wxChar *defaultExtension, const wxChar *filter, int flags,
833 wxWindow *parent, int x, int y )
834 {
835 wxString filter2;
836 if ( defaultExtension && !filter )
837 filter2 = wxString(_T("*.")) + wxString(defaultExtension) ;
838 else if ( filter )
839 filter2 = filter;
840
841 wxString defaultDirString;
842 if (defaultDir)
843 defaultDirString = defaultDir;
844
845 wxString defaultFilenameString;
846 if (defaultFileName)
847 defaultFilenameString = defaultFileName;
848
849 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
850
851 if ( fileDialog.ShowModal() == wxID_OK )
852 {
853 return fileDialog.GetPath();
854 }
855 else
856 {
857 return wxEmptyString;
858 }
859 }
860
861 wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent )
862 {
863 wxChar *ext = (wxChar *)extension;
864
865 wxChar prompt[50];
866 wxString str = _("Load %s file");
867 wxSprintf(prompt, str, what);
868
869 if (*ext == _T('.')) ext++;
870 wxChar wild[60];
871 wxSprintf(wild, _T("*.%s"), ext);
872
873 return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent);
874 }
875
876 wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name,
877 wxWindow *parent )
878 {
879 wxChar *ext = (wxChar *)extension;
880
881 wxChar prompt[50];
882 wxString str = _("Save %s file");
883 wxSprintf(prompt, str, what);
884
885 if (*ext == _T('.')) ext++;
886 wxChar wild[60];
887 wxSprintf(wild, _T("*.%s"), ext);
888
889 return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent);
890 }
891