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