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