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