]> git.saurik.com Git - wxWidgets.git/blob - src/generic/filedlgg.cpp
Small optical changes for MSW
[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 5000
527 #define ID_REPORT_MODE 5001
528 #define ID_UP_DIR 5005
529 #define ID_PARENT_DIR 5006
530 #define ID_NEW_DIR 5007
531 #define ID_CHOICE 5008
532 #define ID_TEXT 5009
533 #define ID_LIST_CTRL 5010
534
535 IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
536
537 BEGIN_EVENT_TABLE(wxFileDialog,wxDialog)
538 EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList)
539 EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport)
540 EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp)
541 EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome)
542 EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew)
543 EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk)
544 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected)
545 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated)
546 EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice)
547 EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter)
548 END_EVENT_TABLE()
549
550 wxFileDialog::wxFileDialog(wxWindow *parent,
551 const wxString& message,
552 const wxString& defaultDir,
553 const wxString& defaultFile,
554 const wxString& wildCard,
555 long style,
556 const wxPoint& pos ) :
557 wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
558 {
559 wxBeginBusyCursor();
560
561 m_message = message;
562 m_dialogStyle = style;
563 m_dir = defaultDir;
564 if (m_dir.IsEmpty())
565 {
566 char buf[200];
567 m_dir = getcwd( buf, sizeof(buf) );
568 }
569 m_path = defaultDir;
570 m_path += _T("/");
571 m_path += defaultFile;
572 m_fileName = defaultFile;
573 m_wildCard = wildCard;
574 m_filterIndex = 0;
575
576 // interpret wildcards
577
578 if (m_wildCard.IsEmpty())
579 m_wildCard = _("All files (*)|*");
580
581 wxStringTokenizer tokens( m_wildCard, _T("|") );
582 wxString firstWild;
583 wxString firstWildText;
584 if (tokens.CountTokens() == 1)
585 {
586 firstWildText = tokens.GetNextToken();
587 firstWild = firstWildText;
588 }
589 else
590 {
591 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, _T("Wrong file type descripition") );
592 firstWildText = tokens.GetNextToken();
593 firstWild = tokens.GetNextToken();
594 }
595
596 // layout
597
598 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
599
600 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
601
602 wxBitmapButton *but;
603
604 but = new wxBitmapButton( this, ID_LIST_MODE, wxBitmap( listview_xpm ) );
605 #if wxUSE_TOOLTIPS
606 but->SetToolTip( _("View files as a list view") );
607 #endif
608 buttonsizer->Add( but, 0, wxALL, 5 );
609
610 but = new wxBitmapButton( this, ID_REPORT_MODE, wxBitmap( repview_xpm ) );
611 #if wxUSE_TOOLTIPS
612 but->SetToolTip( _("View files as a detailed view") );
613 #endif
614 buttonsizer->Add( but, 0, wxALL, 5 );
615
616 buttonsizer->Add( 30, 5, 1 );
617
618 but = new wxBitmapButton( this, ID_UP_DIR, wxBitmap( dir_up_xpm ) );
619 #if wxUSE_TOOLTIPS
620 but->SetToolTip( _("Go to parent directory") );
621 #endif
622 buttonsizer->Add( but, 0, wxALL, 5 );
623
624 but = new wxBitmapButton( this, ID_PARENT_DIR, wxBitmap(home_xpm) );
625 #if wxUSE_TOOLTIPS
626 but->SetToolTip( _("Go to home directory") );
627 #endif
628 buttonsizer->Add( but, 0, wxALL, 5);
629
630 buttonsizer->Add( 20, 20 );
631
632 but = new wxBitmapButton( this, ID_NEW_DIR, wxBitmap(new_dir_xpm) );
633 #if wxUSE_TOOLTIPS
634 but->SetToolTip( _("Create new directory") );
635 #endif
636 buttonsizer->Add( but, 0, wxALL, 5 );
637
638 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
639
640 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
641 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
642 m_static = new wxStaticText( this, -1, m_dir );
643 staticsizer->Add( m_static, 1 );
644 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
645
646 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition, wxSize(440,180),
647 wxLC_LIST | wxSUNKEN_BORDER | wxLC_SINGLE_SEL );
648 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
649
650 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
651 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
652 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
653 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
654 mainsizer->Add( textsizer, 0, wxEXPAND );
655
656 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
657 m_choice = new wxChoice( this, ID_CHOICE );
658 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
659 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
660 mainsizer->Add( choicesizer, 0, wxEXPAND );
661
662 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
663 while (tokens.HasMoreTokens())
664 {
665 firstWildText = tokens.GetNextToken();
666 firstWild = tokens.GetNextToken();
667 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
668 }
669 m_choice->SetSelection( 0 );
670
671 SetAutoLayout( TRUE );
672 SetSizer( mainsizer );
673
674 mainsizer->Fit( this );
675 mainsizer->SetSizeHints( this );
676
677 Centre( wxBOTH );
678
679 if (m_fileName.IsEmpty())
680 m_list->SetFocus();
681 else
682 m_text->SetFocus();
683
684 wxEndBusyCursor();
685 }
686
687 wxFileDialog::~wxFileDialog()
688 {
689 }
690
691 void wxFileDialog::OnChoice( wxCommandEvent &event )
692 {
693 wxString *str = (wxString*) m_choice->GetClientData( event.GetInt() );
694 m_list->SetWild( *str );
695 }
696
697 void wxFileDialog::OnActivated( wxListEvent &WXUNUSED(event) )
698 {
699 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
700 cevent.SetEventObject( this );
701 GetEventHandler()->ProcessEvent( cevent );
702 }
703
704 void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
705 {
706 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
707 cevent.SetEventObject( this );
708 GetEventHandler()->ProcessEvent( cevent );
709 }
710
711 void wxFileDialog::OnSelected( wxListEvent &event )
712 {
713 if (FindFocus() == m_list)
714 m_text->SetValue( event.m_item.m_text );
715 }
716
717 void wxFileDialog::OnListOk( wxCommandEvent &event )
718 {
719 wxString filename( m_text->GetValue() );
720 wxString dir;
721 m_list->GetDir( dir );
722 if (filename.IsEmpty()) return;
723 if (filename == _T(".")) return;
724
725 if (filename == _T(".."))
726 {
727 m_list->GoToParentDir();
728 m_list->SetFocus();
729 m_list->GetDir( dir );
730 m_static->SetLabel( dir );
731 return;
732 }
733
734 if (filename == _T("~"))
735 {
736 m_list->GoToHomeDir();
737 m_list->SetFocus();
738 m_list->GetDir( dir );
739 m_static->SetLabel( dir );
740 return;
741 }
742
743 if (filename[0] == _T('~'))
744 {
745 filename.Remove( 0, 1 );
746 wxString tmp( wxGetUserHome() );
747 tmp += _T('/');
748 tmp += filename;
749 filename = tmp;
750 }
751
752 if ((filename.Find(_T('*')) != wxNOT_FOUND) ||
753 (filename.Find(_T('?')) != wxNOT_FOUND))
754 {
755 if (filename.Find(_T('/')) != wxNOT_FOUND)
756 {
757 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
758 return;
759 }
760 m_list->SetWild( filename );
761 return;
762 }
763
764 if (dir != _T("/")) dir += _T("/");
765 if (filename[0] != _T('/'))
766 {
767 dir += filename;
768 filename = dir;
769 }
770
771 if (wxDirExists(filename))
772 {
773 m_list->GoToDir( filename );
774 if (filename == _T("/"))
775 m_text->SetValue( _T("") );
776 else
777 m_text->SetValue( _T("..") );
778 m_list->GetDir( dir );
779 m_static->SetLabel( dir );
780 return;
781 }
782
783 if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) )
784 {
785 if (wxFileExists( filename ))
786 {
787 wxString msg;
788 msg.Printf( _("File '%s' already exists, do you really want to "
789 "overwrite it?"), filename.c_str() );
790
791 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
792 return;
793 }
794 }
795 else if ( (m_dialogStyle & wxOPEN) && (m_dialogStyle & wxFILE_MUST_EXIST) )
796 {
797 if ( !wxFileExists( filename ) )
798 {
799 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR );
800 return;
801 }
802 }
803
804 SetPath( filename );
805 event.Skip();
806 }
807
808 void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
809 {
810 m_list->ChangeToListMode();
811 m_list->SetFocus();
812 }
813
814 void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
815 {
816 m_list->ChangeToReportMode();
817 m_list->SetFocus();
818 }
819
820 void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
821 {
822 m_list->GoToParentDir();
823 m_list->SetFocus();
824 wxString dir;
825 m_list->GetDir( dir );
826 m_static->SetLabel( dir );
827 }
828
829 void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
830 {
831 m_list->GoToHomeDir();
832 m_list->SetFocus();
833 wxString dir;
834 m_list->GetDir( dir );
835 m_static->SetLabel( dir );
836 }
837
838 void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
839 {
840 m_list->MakeDir();
841 }
842
843 void wxFileDialog::SetPath( const wxString& path )
844 {
845 // not only set the full path but also update filename and dir
846 m_path = path;
847 if ( !!path )
848 {
849 wxString ext;
850 wxSplitPath(path, &m_dir, &m_fileName, &ext);
851 if (!ext.IsEmpty())
852 {
853 m_fileName += _T(".");
854 m_fileName += ext;
855 }
856 }
857 }
858
859 // ----------------------------------------------------------------------------
860 // global functions
861 // ----------------------------------------------------------------------------
862
863 wxString
864 wxFileSelectorEx(const wxChar *message,
865 const wxChar *default_path,
866 const wxChar *default_filename,
867 int *indexDefaultExtension,
868 const wxChar *wildcard,
869 int flags,
870 wxWindow *parent,
871 int x, int y)
872 {
873 // TODO: implement this somehow
874 return wxFileSelector(message, default_path, default_filename, _T(""),
875 wildcard, flags, parent, x, y);
876 }
877
878 wxString wxFileSelector( const wxChar *title,
879 const wxChar *defaultDir, const wxChar *defaultFileName,
880 const wxChar *defaultExtension, const wxChar *filter, int flags,
881 wxWindow *parent, int x, int y )
882 {
883 wxString filter2;
884 if ( defaultExtension && !filter )
885 filter2 = wxString(_T("*.")) + wxString(defaultExtension) ;
886 else if ( filter )
887 filter2 = filter;
888
889 wxString defaultDirString;
890 if (defaultDir)
891 defaultDirString = defaultDir;
892
893 wxString defaultFilenameString;
894 if (defaultFileName)
895 defaultFilenameString = defaultFileName;
896
897 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
898
899 if ( fileDialog.ShowModal() == wxID_OK )
900 {
901 return fileDialog.GetPath();
902 }
903 else
904 {
905 return wxEmptyString;
906 }
907 }
908
909 wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent )
910 {
911 wxChar *ext = (wxChar *)extension;
912
913 wxChar prompt[50];
914 wxString str = _("Load %s file");
915 wxSprintf(prompt, str, what);
916
917 if (*ext == _T('.')) ext++;
918 wxChar wild[60];
919 wxSprintf(wild, _T("*.%s"), ext);
920
921 return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent);
922 }
923
924 wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name,
925 wxWindow *parent )
926 {
927 wxChar *ext = (wxChar *)extension;
928
929 wxChar prompt[50];
930 wxString str = _("Save %s file");
931 wxSprintf(prompt, str, what);
932
933 if (*ext == _T('.')) ext++;
934 wxChar wild[60];
935 wxSprintf(wild, _T("*.%s"), ext);
936
937 return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent);
938 }
939