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