]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
bitmap and image updates
[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;
aaa37c0d
VZ
247 if (IsExe()) item.SetTextColour(*wxRED);
248 if (IsDir()) item.SetTextColour(*wxBLUE);
249 item.m_image = IsDir() ? 0 : -1;
8b17ba72
RR
250 if (IsLink())
251 {
252 wxColour *dg = wxTheColourDatabase->FindColour( "MEDIUM GREY" );
aaa37c0d 253 item.SetTextColour(*dg);
8b17ba72
RR
254 }
255 item.m_data = (long)this;
256}
c8c0e54c 257
8b17ba72
RR
258//-----------------------------------------------------------------------------
259// wxFileCtrl
260//-----------------------------------------------------------------------------
261
262IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl);
263
264BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
0b855868
RR
265 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem)
266 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit)
8b17ba72
RR
267END_EVENT_TABLE()
268
269wxFileCtrl::wxFileCtrl()
270{
223d09f6 271 m_dirName = wxT("/");
8b17ba72
RR
272 m_showHidden = FALSE;
273}
274
c8c0e54c 275wxFileCtrl::wxFileCtrl( wxWindow *win, wxWindowID id,
0b855868 276 const wxString &dirName, const wxString &wild,
8b17ba72
RR
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{
0b855868 281 wxImageList *imageList = new wxImageList( 16, 16 );
8b17ba72 282 imageList->Add( wxBitmap( folder_xpm ) );
0b855868 283 SetImageList( imageList, wxIMAGE_LIST_SMALL );
c8c0e54c 284
8b17ba72 285 m_dirName = dirName;
0b855868 286 m_wild = wild;
8b17ba72
RR
287 m_showHidden = FALSE;
288 Update();
8b17ba72
RR
289}
290
291void wxFileCtrl::ChangeToListMode()
292{
293 SetSingleStyle( wxLC_LIST );
294 Update();
295}
296
297void wxFileCtrl::ChangeToReportMode()
298{
299 SetSingleStyle( wxLC_REPORT );
300 Update();
301}
302
303void wxFileCtrl::ChangeToIconMode()
304{
305 SetSingleStyle( wxLC_ICON );
306 Update();
307}
308
309void wxFileCtrl::ShowHidden( bool show )
310{
311 m_showHidden = show;
312 Update();
313}
314
0b855868
RR
315long 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 );
c8c0e54c 329 }
0b855868
RR
330 return ret;
331}
332
8b17ba72 333void wxFileCtrl::Update()
c8c0e54c 334{
8b17ba72
RR
335 ClearAll();
336 long my_style = GetWindowStyleFlag();
337 if (my_style & wxLC_REPORT)
338 {
e6daf794 339 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, 130 );
8b17ba72
RR
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;
8b17ba72
RR
347 item.m_itemId = 0;
348 item.m_col = 0;
0b855868 349
223d09f6 350 if (m_dirName != wxT("/"))
0b855868
RR
351 {
352 wxString p( wxPathOnly(m_dirName) );
223d09f6
KB
353 if (p.IsEmpty()) p = wxT("/");
354 fd = new wxFileData( wxT(".."), p );
c8c0e54c 355 Add( fd, item );
0b855868
RR
356 item.m_itemId++;
357 }
358
223d09f6 359 wxString res = m_dirName + wxT("/*");
cae5359f 360 wxString f( wxFindFirstFile( res.GetData(), wxDIR ) );
8b17ba72
RR
361 while (!f.IsEmpty())
362 {
363 res = wxFileNameFromPath( f );
364 fd = new wxFileData( res, f );
0b855868 365 wxString s = fd->GetName();
223d09f6 366 if (m_showHidden || (s[0] != wxT('.')))
8b17ba72 367 {
c8c0e54c 368 Add( fd, item );
8b17ba72
RR
369 item.m_itemId++;
370 }
371 f = wxFindNextFile();
372 }
c8c0e54c 373
223d09f6 374 res = m_dirName + wxT("/") + m_wild;
cae5359f
RR
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();
223d09f6 381 if (m_showHidden || (s[0] != wxT('.')))
cae5359f 382 {
c8c0e54c 383 Add( fd, item );
cae5359f
RR
384 item.m_itemId++;
385 }
386 f = wxFindNextFile();
387 }
c8c0e54c 388
8b17ba72
RR
389 SortItems( ListCompare, 0 );
390}
391
0b855868 392void wxFileCtrl::SetWild( const wxString &wild )
8b17ba72 393{
0b855868
RR
394 m_wild = wild;
395 Update();
396}
397
398void wxFileCtrl::MakeDir()
399{
223d09f6 400 wxString new_name( wxT("NewName") );
0b855868 401 wxString path( m_dirName );
223d09f6 402 path += wxT("/");
0b855868
RR
403 path += new_name;
404 if (wxFileExists(path))
8b17ba72 405 {
0b855868
RR
406 // try NewName0, NewName1 etc.
407 int i = 0;
c8c0e54c 408 do {
0b855868 409 new_name = _("NewName");
c8c0e54c 410 wxString num;
223d09f6 411 num.Printf( wxT("%d"), i );
c8c0e54c
VZ
412 new_name += num;
413
0b855868 414 path = m_dirName;
223d09f6 415 path += wxT("/");
0b855868 416 path += new_name;
c8c0e54c
VZ
417 i++;
418 } while (wxFileExists(path));
8b17ba72 419 }
c8c0e54c 420
0b855868 421 wxLogNull log;
c8c0e54c 422 if (!wxMkdir(path))
8b17ba72 423 {
0b855868 424 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 425 dialog.ShowModal();
0b855868 426 return;
8b17ba72 427 }
8b17ba72 428
0b855868
RR
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 );
c8c0e54c 434
0b855868
RR
435 if (id != -1)
436 {
437 SortItems( ListCompare, 0 );
c8c0e54c 438 id = FindItem( 0, (long)fd );
0b855868
RR
439 EnsureVisible( id );
440 EditLabel( id );
441 }
8b17ba72
RR
442}
443
444void wxFileCtrl::GoToParentDir()
445{
223d09f6 446 if (m_dirName != wxT("/"))
8b17ba72 447 {
c8c0e54c 448 wxString fname( wxFileNameFromPath(m_dirName) );
0b855868 449 m_dirName = wxPathOnly( m_dirName );
223d09f6 450 if (m_dirName.IsEmpty()) m_dirName = wxT("/");
8b17ba72 451 Update();
c8c0e54c
VZ
452 int id = FindItem( 0, fname );
453 if (id != -1)
454 {
455 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
456 EnsureVisible( id );
457 }
8b17ba72
RR
458 }
459}
460
461void wxFileCtrl::GoToHomeDir()
462{
463 wxString s = wxGetUserHome( wxString() );
464 m_dirName = s;
465 Update();
e6daf794
RR
466 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
467 EnsureVisible( 0 );
8b17ba72
RR
468}
469
470void wxFileCtrl::GoToDir( const wxString &dir )
471{
472 m_dirName = dir;
473 Update();
e6daf794
RR
474 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
475 EnsureVisible( 0 );
8b17ba72
RR
476}
477
478void wxFileCtrl::GetDir( wxString &dir )
479{
480 dir = m_dirName;
481}
482
8b17ba72
RR
483void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
484{
485 wxFileData *fd = (wxFileData*)event.m_item.m_data;
486 delete fd;
487}
488
0b855868 489void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
8b17ba72
RR
490{
491 wxFileData *fd = (wxFileData*)event.m_item.m_data;
0b855868 492 wxASSERT( fd );
c8c0e54c 493
0b855868
RR
494 if ((event.GetLabel().IsEmpty()) ||
495 (event.GetLabel() == _(".")) ||
496 (event.GetLabel() == _("..")) ||
223d09f6 497 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
8b17ba72 498 {
0b855868 499 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 500 dialog.ShowModal();
0b855868 501 event.Veto();
c8c0e54c 502 return;
8b17ba72 503 }
c8c0e54c 504
0b855868 505 wxString new_name( wxPathOnly( fd->GetFullName() ) );
223d09f6 506 new_name += wxT("/");
0b855868 507 new_name += event.GetLabel();
c8c0e54c 508
0b855868 509 wxLogNull log;
c8c0e54c 510
0b855868 511 if (wxFileExists(new_name))
8b17ba72 512 {
0b855868 513 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 514 dialog.ShowModal();
0b855868 515 event.Veto();
8b17ba72 516 }
c8c0e54c 517
0b855868 518 if (wxRenameFile(fd->GetFullName(),new_name))
8b17ba72 519 {
0b855868 520 fd->SetNewName( new_name, event.GetLabel() );
c8c0e54c 521 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
0b855868 522 EnsureVisible( event.GetItem() );
8b17ba72
RR
523 }
524 else
525 {
0b855868 526 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 527 dialog.ShowModal();
0b855868 528 event.Veto();
8b17ba72 529 }
8b17ba72
RR
530}
531
532//-----------------------------------------------------------------------------
533// wxFileDialog
534//-----------------------------------------------------------------------------
535
a16d3c04
KB
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
bf9e3e73 545#define ID_CHECK wxID_FILEDLGG + 12
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)
bf9e3e73 560 EVT_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck)
8b17ba72
RR
561END_EVENT_TABLE()
562
563wxFileDialog::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 ) :
9c884972 570 wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
8b17ba72
RR
571{
572 wxBeginBusyCursor();
c8c0e54c 573
8b17ba72
RR
574 m_message = message;
575 m_dialogStyle = style;
7941ba11
RR
576
577 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
578 m_dialogStyle &= ~wxMULTIPLE;
579
8b17ba72 580 m_dir = defaultDir;
ac2def68
RR
581 if (m_dir.IsEmpty())
582 {
583 char buf[200];
c8c0e54c 584 m_dir = getcwd( buf, sizeof(buf) );
ac2def68 585 }
8b17ba72 586 m_path = defaultDir;
223d09f6 587 m_path += wxT("/");
8b17ba72
RR
588 m_path += defaultFile;
589 m_fileName = defaultFile;
590 m_wildCard = wildCard;
591 m_filterIndex = 0;
c8c0e54c 592
cae5359f 593 // interpret wildcards
c8c0e54c 594
cae5359f
RR
595 if (m_wildCard.IsEmpty())
596 m_wildCard = _("All files (*)|*");
c8c0e54c 597
223d09f6 598 wxStringTokenizer tokens( m_wildCard, wxT("|") );
cae5359f
RR
599 wxString firstWild;
600 wxString firstWildText;
601 if (tokens.CountTokens() == 1)
602 {
603 firstWildText = tokens.GetNextToken();
604 firstWild = firstWildText;
605 }
606 else
607 {
223d09f6 608 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
cae5359f
RR
609 firstWildText = tokens.GetNextToken();
610 firstWild = tokens.GetNextToken();
611 }
612
613 // layout
c8c0e54c 614
8b17ba72 615 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 616
8b17ba72 617 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 618
e6daf794 619 wxBitmapButton *but;
c8c0e54c 620
e6daf794
RR
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 );
c8c0e54c 626
e6daf794
RR
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 );
c8c0e54c 632
0b855868 633 buttonsizer->Add( 30, 5, 1 );
e6daf794
RR
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 );
c8c0e54c 640
e6daf794
RR
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);
c8c0e54c 646
e6daf794 647 buttonsizer->Add( 20, 20 );
c8c0e54c 648
e6daf794
RR
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 );
c8c0e54c 654
0b855868 655 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
c8c0e54c 656
9c884972
RR
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
7941ba11
RR
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 );
0b855868 669 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
c8c0e54c 670
8b17ba72 671 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
cae5359f 672 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
0b855868 673 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
8b17ba72
RR
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 );
cae5359f 678 m_choice = new wxChoice( this, ID_CHOICE );
8b17ba72 679 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
bf9e3e73
RR
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 );
8b17ba72
RR
683 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
684 mainsizer->Add( choicesizer, 0, wxEXPAND );
c8c0e54c 685
cae5359f
RR
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 );
8b17ba72
RR
694
695 SetAutoLayout( TRUE );
696 SetSizer( mainsizer );
c8c0e54c 697
8b17ba72
RR
698 mainsizer->Fit( this );
699 mainsizer->SetSizeHints( this );
c8c0e54c 700
8b17ba72 701 Centre( wxBOTH );
ed58dbea
RR
702
703 if (m_fileName.IsEmpty())
704 m_list->SetFocus();
705 else
706 m_text->SetFocus();
c8c0e54c 707
8b17ba72
RR
708 wxEndBusyCursor();
709}
710
cae5359f
RR
711wxFileDialog::~wxFileDialog()
712{
713}
714
715void wxFileDialog::OnChoice( wxCommandEvent &event )
716{
717 wxString *str = (wxString*) m_choice->GetClientData( event.GetInt() );
718 m_list->SetWild( *str );
f49f2b0c 719 m_filterIndex = event.GetInt();
cae5359f
RR
720}
721
bf9e3e73
RR
722void wxFileDialog::OnCheck( wxCommandEvent &event )
723{
724 m_list->ShowHidden( event.GetInt() );
725}
726
e1811a01 727void wxFileDialog::OnActivated( wxListEvent &event )
8b17ba72 728{
e1811a01 729 HandleAction( event.m_item.m_text );
8b17ba72
RR
730}
731
cae5359f
RR
732void 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
8b17ba72
RR
739void wxFileDialog::OnSelected( wxListEvent &event )
740{
e1811a01
RR
741 if (FindFocus() != m_list) return;
742
743 wxString filename( event.m_item.m_text );
223d09f6 744 if (filename == wxT("..")) return;
e1811a01
RR
745
746 wxString dir;
747 m_list->GetDir( dir );
223d09f6 748 if (dir != wxT("/")) dir += wxT("/");
e1811a01
RR
749 dir += filename;
750 if (wxDirExists(dir)) return;
751
752 m_text->SetValue( filename );
8b17ba72
RR
753}
754
e1811a01 755void wxFileDialog::HandleAction( const wxString &fn )
8b17ba72 756{
e1811a01 757 wxString filename( fn );
8b17ba72
RR
758 wxString dir;
759 m_list->GetDir( dir );
760 if (filename.IsEmpty()) return;
223d09f6 761 if (filename == wxT(".")) return;
c8c0e54c 762
223d09f6 763 if (filename == wxT(".."))
0b855868
RR
764 {
765 m_list->GoToParentDir();
766 m_list->SetFocus();
9c884972
RR
767 m_list->GetDir( dir );
768 m_static->SetLabel( dir );
c8c0e54c 769 return;
0b855868
RR
770 }
771
223d09f6 772 if (filename == wxT("~"))
ed58dbea
RR
773 {
774 m_list->GoToHomeDir();
775 m_list->SetFocus();
776 m_list->GetDir( dir );
777 m_static->SetLabel( dir );
c8c0e54c 778 return;
ed58dbea 779 }
c8c0e54c 780
223d09f6 781 if (filename[0] == wxT('~'))
ed58dbea
RR
782 {
783 filename.Remove( 0, 1 );
c8c0e54c 784 wxString tmp( wxGetUserHome() );
223d09f6 785 tmp += wxT('/');
c8c0e54c
VZ
786 tmp += filename;
787 filename = tmp;
ed58dbea
RR
788 }
789
223d09f6
KB
790 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
791 (filename.Find(wxT('?')) != wxNOT_FOUND))
cae5359f 792 {
223d09f6 793 if (filename.Find(wxT('/')) != wxNOT_FOUND)
c8c0e54c
VZ
794 {
795 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
796 return;
797 }
798 m_list->SetWild( filename );
799 return;
cae5359f
RR
800 }
801
223d09f6
KB
802 if (dir != wxT("/")) dir += wxT("/");
803 if (filename[0] != wxT('/'))
ed58dbea
RR
804 {
805 dir += filename;
806 filename = dir;
807 }
c8c0e54c 808
8b17ba72
RR
809 if (wxDirExists(filename))
810 {
811 m_list->GoToDir( filename );
9c884972
RR
812 m_list->GetDir( dir );
813 m_static->SetLabel( dir );
c8c0e54c 814 return;
8b17ba72 815 }
c8c0e54c 816
8b17ba72
RR
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 {
cae5359f 833 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR );
8b17ba72
RR
834 return;
835 }
836 }
837
838 SetPath( filename );
9b7e522a
RR
839
840 wxCommandEvent event;
841 wxDialog::OnOK(event);
e1811a01
RR
842}
843
5e0201ea 844void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
e1811a01
RR
845{
846 HandleAction( m_text->GetValue() );
8b17ba72
RR
847}
848
849void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
850{
851 m_list->ChangeToListMode();
0b855868 852 m_list->SetFocus();
8b17ba72
RR
853}
854
855void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
856{
857 m_list->ChangeToReportMode();
0b855868 858 m_list->SetFocus();
8b17ba72
RR
859}
860
861void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
862{
863 m_list->GoToParentDir();
0b855868 864 m_list->SetFocus();
9c884972
RR
865 wxString dir;
866 m_list->GetDir( dir );
867 m_static->SetLabel( dir );
8b17ba72
RR
868}
869
870void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
871{
872 m_list->GoToHomeDir();
0b855868 873 m_list->SetFocus();
9c884972
RR
874 wxString dir;
875 m_list->GetDir( dir );
876 m_static->SetLabel( dir );
0b855868
RR
877}
878
879void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
880{
881 m_list->MakeDir();
8b17ba72
RR
882}
883
884void 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);
c8c0e54c
VZ
892 if (!ext.IsEmpty())
893 {
223d09f6 894 m_fileName += wxT(".");
8b17ba72 895 m_fileName += ext;
c8c0e54c 896 }
8b17ba72
RR
897 }
898}
c8c0e54c 899
7941ba11
RR
900void 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
921void 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
8b17ba72
RR
938// ----------------------------------------------------------------------------
939// global functions
940// ----------------------------------------------------------------------------
941
942wxString
943wxFileSelectorEx(const wxChar *message,
944 const wxChar *default_path,
945 const wxChar *default_filename,
5e0201ea 946 int *WXUNUSED(indexDefaultExtension),
8b17ba72
RR
947 const wxChar *wildcard,
948 int flags,
949 wxWindow *parent,
950 int x, int y)
951{
952 // TODO: implement this somehow
223d09f6 953 return wxFileSelector(message, default_path, default_filename, wxT(""),
8b17ba72
RR
954 wildcard, flags, parent, x, y);
955}
956
957wxString 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 )
223d09f6 964 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
8b17ba72
RR
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
988wxString 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
223d09f6 996 if (*ext == wxT('.')) ext++;
8b17ba72 997 wxChar wild[60];
223d09f6 998 wxSprintf(wild, wxT("*.%s"), ext);
8b17ba72
RR
999
1000 return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent);
1001}
1002
1003wxString 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
223d09f6 1012 if (*ext == wxT('.')) ext++;
8b17ba72 1013 wxChar wild[60];
223d09f6 1014 wxSprintf(wild, wxT("*.%s"), ext);
8b17ba72
RR
1015
1016 return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent);
1017}
1018