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