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