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