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