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