]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
documented existence of wxTheMimeTypesManager
[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"
655cf310
VS
35#include "wx/mimetype.h"
36#include "wx/image.h"
37#include "wx/module.h"
eaf40b23 38#include "wx/config.h"
655cf310 39
8b17ba72 40
e6daf794
RR
41#if wxUSE_TOOLTIPS
42 #include "wx/tooltip.h"
43#endif
44
ac2def68
RR
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <dirent.h>
48#include <pwd.h>
49#include <grp.h>
50#include <time.h>
51#include <unistd.h>
8b17ba72 52
0b855868
RR
53#include "wx/generic/home.xpm"
54#include "wx/generic/listview.xpm"
55#include "wx/generic/repview.xpm"
e6daf794
RR
56#include "wx/generic/new_dir.xpm"
57#include "wx/generic/dir_up.xpm"
b0c5c421
VS
58#include "wx/generic/folder.xpm"
59#include "wx/generic/deffile.xpm"
60#include "wx/generic/exefile.xpm"
655cf310
VS
61
62// ----------------------------------------------------------------------------
63// private classes - icons list management
64// ----------------------------------------------------------------------------
65
66class wxFileIconEntry : public wxObject
67{
68 public:
69 wxFileIconEntry(int i) { id = i; }
70
71 int id;
72};
73
74
75class wxFileIconsTable
76{
77 public:
78
79 wxFileIconsTable();
80
b0c5c421 81 int GetIconID(const wxString& extension, const wxString& mime = wxEmptyString);
655cf310
VS
82 wxImageList *GetImageList() { return &m_ImageList; }
83
84 protected:
85 wxImageList m_ImageList;
86 wxHashTable m_HashTable;
87 wxMimeTypesManager m_Mime;
88};
89
90static wxFileIconsTable *g_IconsTable = NULL;
91
b0c5c421
VS
92#define FI_FOLDER 0
93#define FI_UNKNOWN 1
94#define FI_EXECUTABLE 2
655cf310
VS
95
96wxFileIconsTable::wxFileIconsTable() :
97 m_ImageList(16, 16),
98 m_HashTable(wxKEY_STRING),
99 m_Mime()
100{
101 m_HashTable.DeleteContents(TRUE);
b0c5c421
VS
102 m_ImageList.Add(wxBitmap(folder_xpm)); // FI_FOLDER
103 m_ImageList.Add(wxBitmap(deffile_xpm)); // FI_UNKNOWN
104 if (GetIconID(wxEmptyString, _T("application/x-executable")) == FI_UNKNOWN)
105 { // FI_EXECUTABLE
106 m_ImageList.Add(wxBitmap(exefile_xpm));
107 m_HashTable.Delete(_T("exe"));
108 m_HashTable.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE));
109 }
110 /* else put into list by GetIconID
111 (KDE defines application/x-executable for *.exe and has nice icon)
112 */
655cf310
VS
113}
114
115
116
117static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
118{
119 wxImage small(16, 16);
120 unsigned char *p1, *p2, *ps;
b0c5c421
VS
121 unsigned char mr = img.GetMaskRed(),
122 mg = img.GetMaskGreen(),
123 mb = img.GetMaskBlue();
655cf310
VS
124
125 unsigned x, y;
126 unsigned sr, sg, sb, smask;
127
128 p1 = img.GetData(), p2 = img.GetData() + 3 * 32, ps = small.GetData();
129 small.SetMaskColour(mr, mr, mr);
130
131 for (y = 0; y < 16; y++)
132 {
133 for (x = 0; x < 16; x++)
134 {
135 sr = sg = sb = smask = 0;
136 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
137 sr += p1[0], sg += p1[1], sb += p1[2];
138 else smask++;
139 p1 += 3;
140 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
141 sr += p1[0], sg += p1[1], sb += p1[2];
142 else smask++;
143 p1 += 3;
144 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
145 sr += p2[0], sg += p2[1], sb += p2[2];
146 else smask++;
147 p2 += 3;
148 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
149 sr += p2[0], sg += p2[1], sb += p2[2];
150 else smask++;
151 p2 += 3;
152
153 if (smask > 2)
154 ps[0] = ps[1] = ps[2] = mr;
155 else
156 ps[0] = sr >> 2, ps[1] = sg >> 2, ps[2] = sb >> 2;
157 ps += 3;
158 }
159 p1 += 32 * 3, p2 += 32 * 3;
160 }
161
162 return small.ConvertToBitmap();
163}
164
165
b0c5c421 166int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
655cf310 167{
b0c5c421
VS
168 if (!extension.IsEmpty())
169 {
170 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable.Get(extension);
171 if (entry) return (entry -> id);
172 }
173
174 wxFileType *ft = (mime.IsEmpty()) ?
175 m_Mime.GetFileTypeFromExtension(extension) :
176 m_Mime.GetFileTypeFromMimeType(mime);
655cf310
VS
177 wxIcon ic;
178 if (ft == NULL || (!ft -> GetIcon(&ic)))
179 {
b0c5c421 180 int newid = FI_UNKNOWN;
655cf310
VS
181 m_HashTable.Put(extension, new wxFileIconEntry(newid));
182 return newid;
183 }
184 wxImage img(ic);
185 delete ft;
186
187 int id = m_ImageList.GetImageCount();
188 if (img.GetWidth() == 16 && img.GetHeight() == 16)
189 m_ImageList.Add(img.ConvertToBitmap());
190 else
b4b58c41
VS
191 {
192 if (img.GetWidth() != 32 || img.GetHeight() != 32)
193 img.Rescale(32, 32);
194 m_ImageList.Add(CreateAntialiasedBitmap(img));
195 }
655cf310
VS
196 m_HashTable.Put(extension, new wxFileIconEntry(id));
197 return id;
198}
199
200
201
c8c0e54c
VZ
202// ----------------------------------------------------------------------------
203// private functions
204// ----------------------------------------------------------------------------
205
206static
207int ListCompare( long data1, long data2, long WXUNUSED(data) )
208{
209 wxFileData *fd1 = (wxFileData*)data1 ;
210 wxFileData *fd2 = (wxFileData*)data2 ;
223d09f6
KB
211 if (fd1->GetName() == wxT("..")) return -1;
212 if (fd2->GetName() == wxT("..")) return 1;
c8c0e54c
VZ
213 if (fd1->IsDir() && !fd2->IsDir()) return -1;
214 if (fd2->IsDir() && !fd1->IsDir()) return 1;
6eec2bee 215 return wxStrcmp( fd1->GetName(), fd2->GetName() );
c8c0e54c
VZ
216}
217
8b17ba72
RR
218//-----------------------------------------------------------------------------
219// wxFileData
220//-----------------------------------------------------------------------------
221
222IMPLEMENT_DYNAMIC_CLASS(wxFileData,wxObject);
223
224wxFileData::wxFileData( const wxString &name, const wxString &fname )
225{
226 m_name = name;
227 m_fileName = fname;
c8c0e54c 228
8b17ba72 229 struct stat buff;
6eec2bee 230 stat( m_fileName.fn_str(), &buff );
479cd5de 231
9f2d09aa 232#ifndef __EMX__
8b17ba72 233 struct stat lbuff;
6eec2bee 234 lstat( m_fileName.fn_str(), &lbuff );
9f2d09aa 235 m_isLink = S_ISLNK( lbuff.st_mode );
8b17ba72 236 struct tm *t = localtime( &lbuff.st_mtime );
9f2d09aa
RR
237#else
238 m_isLink = FALSE;
239 struct tm *t = localtime( &buff.st_mtime );
240#endif
241
8b17ba72
RR
242// struct passwd *user = getpwuid( buff.st_uid );
243// struct group *grp = getgrgid( buff.st_gid );
244
245 m_isDir = S_ISDIR( buff.st_mode );
8b17ba72
RR
246 m_isExe = ((buff.st_mode & S_IXUSR ) == S_IXUSR );
247
248 m_size = buff.st_size;
249
250 m_hour = t->tm_hour;
251 m_minute = t->tm_min;
252 m_month = t->tm_mon+1;
253 m_day = t->tm_mday;
254 m_year = t->tm_year;
d3e90957 255 m_year += 1900;
8b17ba72 256
6eec2bee
OK
257 m_permissions.sprintf( wxT("%c%c%c"),
258 ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? wxT('r') : wxT('-')),
259 ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? wxT('w') : wxT('-')),
260 ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? wxT('x') : wxT('-')) );
8b17ba72
RR
261}
262
263wxString wxFileData::GetName() const
264{
265 return m_name;
266}
267
268wxString wxFileData::GetFullName() const
269{
270 return m_fileName;
271}
272
273wxString wxFileData::GetHint() const
274{
275 wxString s = m_fileName;
276 s += " ";
277 if (m_isDir) s += _("<DIR> ");
278 else if (m_isLink) s += _("<LINK> ");
279 else
280 {
281 s += LongToString( m_size );
282 s += _(" bytes ");
283 }
284 s += IntToString( m_day );
223d09f6 285 s += wxT(".");
8b17ba72 286 s += IntToString( m_month );
223d09f6 287 s += wxT(".");
8b17ba72 288 s += IntToString( m_year );
223d09f6 289 s += wxT(" ");
8b17ba72 290 s += IntToString( m_hour );
223d09f6 291 s += wxT(":");
8b17ba72 292 s += IntToString( m_minute );
223d09f6 293 s += wxT(" ");
8b17ba72
RR
294 s += m_permissions;
295 return s;
296};
297
c8c0e54c 298wxString wxFileData::GetEntry( int num )
8b17ba72
RR
299{
300 wxString s;
301 switch (num)
302 {
303 case 0:
d3e90957 304 {
8b17ba72 305 s = m_name;
d3e90957
RR
306 }
307 break;
8b17ba72 308 case 1:
479cd5de 309 {
8b17ba72
RR
310 if (m_isDir) s = _("<DIR>");
311 else if (m_isLink) s = _("<LINK>");
312 else s = LongToString( m_size );
d3e90957
RR
313 }
314 break;
8b17ba72 315 case 2:
d3e90957 316 {
223d09f6 317 if (m_day < 10) s = wxT("0"); else s = wxT("");
8b17ba72 318 s += IntToString( m_day );
223d09f6
KB
319 s += wxT(".");
320 if (m_month < 10) s += wxT("0");
8b17ba72 321 s += IntToString( m_month );
223d09f6 322 s += wxT(".");
8b17ba72 323 s += IntToString( m_year );
d3e90957
RR
324 }
325 break;
8b17ba72 326 case 3:
d3e90957 327 {
223d09f6 328 if (m_hour < 10) s = wxT("0"); else s = wxT("");
8b17ba72 329 s += IntToString( m_hour );
223d09f6
KB
330 s += wxT(":");
331 if (m_minute < 10) s += wxT("0");
8b17ba72
RR
332 s += IntToString( m_minute );
333 break;
d3e90957 334 }
8b17ba72
RR
335 case 4:
336 s = m_permissions;
337 break;
338 default:
223d09f6 339 s = wxT("No entry");
8b17ba72
RR
340 break;
341 }
342 return s;
343}
344
345bool wxFileData::IsDir()
346{
347 return m_isDir;
348}
349
350bool wxFileData::IsExe()
351{
352 return m_isExe;
353}
354
355bool wxFileData::IsLink()
356{
357 return m_isLink;
358}
359
360long wxFileData::GetSize()
361{
362 return m_size;
363}
364
0b855868 365void wxFileData::SetNewName( const wxString &name, const wxString &fname )
8b17ba72 366{
0b855868
RR
367 m_name = name;
368 m_fileName = fname;
8b17ba72
RR
369}
370
371void wxFileData::MakeItem( wxListItem &item )
372{
373 item.m_text = m_name;
9b00bb16 374 item.ClearAttributes();
aaa37c0d
VZ
375 if (IsExe()) item.SetTextColour(*wxRED);
376 if (IsDir()) item.SetTextColour(*wxBLUE);
655cf310
VS
377
378 if (IsDir())
b0c5c421
VS
379 item.m_image = FI_FOLDER;
380 else if (IsExe())
381 item.m_image = FI_EXECUTABLE;
655cf310
VS
382 else if (m_name.Find(wxT('.')) != wxNOT_FOUND)
383 item.m_image = g_IconsTable -> GetIconID(m_name.AfterLast(wxT('.')));
384 else
b0c5c421 385 item.m_image = FI_UNKNOWN;
655cf310 386
8b17ba72
RR
387 if (IsLink())
388 {
389 wxColour *dg = wxTheColourDatabase->FindColour( "MEDIUM GREY" );
aaa37c0d 390 item.SetTextColour(*dg);
8b17ba72
RR
391 }
392 item.m_data = (long)this;
393}
c8c0e54c 394
8b17ba72
RR
395//-----------------------------------------------------------------------------
396// wxFileCtrl
397//-----------------------------------------------------------------------------
398
399IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl);
400
401BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
0b855868
RR
402 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem)
403 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit)
8b17ba72
RR
404END_EVENT_TABLE()
405
655cf310
VS
406
407
8b17ba72
RR
408wxFileCtrl::wxFileCtrl()
409{
223d09f6 410 m_dirName = wxT("/");
8b17ba72
RR
411 m_showHidden = FALSE;
412}
413
c8c0e54c 414wxFileCtrl::wxFileCtrl( wxWindow *win, wxWindowID id,
0b855868 415 const wxString &dirName, const wxString &wild,
8b17ba72
RR
416 const wxPoint &pos, const wxSize &size,
417 long style, const wxValidator &validator, const wxString &name ) :
418 wxListCtrl( win, id, pos, size, style, validator, name )
419{
655cf310
VS
420 if (! g_IconsTable) g_IconsTable = new wxFileIconsTable;
421 wxImageList *imageList = g_IconsTable -> GetImageList();
422
0b855868 423 SetImageList( imageList, wxIMAGE_LIST_SMALL );
c8c0e54c 424
8b17ba72 425 m_dirName = dirName;
0b855868 426 m_wild = wild;
8b17ba72
RR
427 m_showHidden = FALSE;
428 Update();
8b17ba72
RR
429}
430
431void wxFileCtrl::ChangeToListMode()
432{
433 SetSingleStyle( wxLC_LIST );
434 Update();
435}
436
437void wxFileCtrl::ChangeToReportMode()
438{
439 SetSingleStyle( wxLC_REPORT );
440 Update();
441}
442
443void wxFileCtrl::ChangeToIconMode()
444{
445 SetSingleStyle( wxLC_ICON );
446 Update();
447}
448
449void wxFileCtrl::ShowHidden( bool show )
450{
451 m_showHidden = show;
452 Update();
453}
454
0b855868
RR
455long wxFileCtrl::Add( wxFileData *fd, wxListItem &item )
456{
457 long ret = -1;
458 item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
459 fd->MakeItem( item );
460 long my_style = GetWindowStyleFlag();
461 if (my_style & wxLC_REPORT)
462 {
463 ret = InsertItem( item );
464 for (int i = 1; i < 5; i++) SetItem( item.m_itemId, i, fd->GetEntry( i) );
465 }
466 else if (my_style & wxLC_LIST)
467 {
468 ret = InsertItem( item );
c8c0e54c 469 }
0b855868
RR
470 return ret;
471}
472
8b17ba72 473void wxFileCtrl::Update()
c8c0e54c 474{
8b17ba72 475 long my_style = GetWindowStyleFlag();
1317fd58
RR
476 int name_col_width = 0;
477 if (my_style & wxLC_REPORT)
478 {
479 if (GetColumnCount() > 0)
480 name_col_width = GetColumnWidth( 0 );
481 }
482
483 ClearAll();
8b17ba72
RR
484 if (my_style & wxLC_REPORT)
485 {
1317fd58
RR
486 if (name_col_width < 140) name_col_width = 140;
487 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, name_col_width );
8b17ba72 488 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, 60 );
e6527f9d 489 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT, 65 );
8b17ba72
RR
490 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT, 50 );
491 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, 120 );
492 }
493 wxFileData *fd = (wxFileData *) NULL;
494 wxListItem item;
8b17ba72
RR
495 item.m_itemId = 0;
496 item.m_col = 0;
0b855868 497
223d09f6 498 if (m_dirName != wxT("/"))
0b855868
RR
499 {
500 wxString p( wxPathOnly(m_dirName) );
223d09f6
KB
501 if (p.IsEmpty()) p = wxT("/");
502 fd = new wxFileData( wxT(".."), p );
c8c0e54c 503 Add( fd, item );
0b855868
RR
504 item.m_itemId++;
505 }
506
223d09f6 507 wxString res = m_dirName + wxT("/*");
cae5359f 508 wxString f( wxFindFirstFile( res.GetData(), wxDIR ) );
8b17ba72
RR
509 while (!f.IsEmpty())
510 {
511 res = wxFileNameFromPath( f );
512 fd = new wxFileData( res, f );
0b855868 513 wxString s = fd->GetName();
223d09f6 514 if (m_showHidden || (s[0] != wxT('.')))
8b17ba72 515 {
c8c0e54c 516 Add( fd, item );
8b17ba72
RR
517 item.m_itemId++;
518 }
519 f = wxFindNextFile();
520 }
c8c0e54c 521
223d09f6 522 res = m_dirName + wxT("/") + m_wild;
cae5359f
RR
523 f = wxFindFirstFile( res.GetData(), wxFILE );
524 while (!f.IsEmpty())
525 {
526 res = wxFileNameFromPath( f );
527 fd = new wxFileData( res, f );
528 wxString s = fd->GetName();
223d09f6 529 if (m_showHidden || (s[0] != wxT('.')))
cae5359f 530 {
c8c0e54c 531 Add( fd, item );
cae5359f
RR
532 item.m_itemId++;
533 }
534 f = wxFindNextFile();
535 }
c8c0e54c 536
8b17ba72 537 SortItems( ListCompare, 0 );
479cd5de 538
e6527f9d 539 SetColumnWidth( 1, wxLIST_AUTOSIZE );
d3e90957
RR
540 SetColumnWidth( 2, wxLIST_AUTOSIZE );
541 SetColumnWidth( 3, wxLIST_AUTOSIZE );
8b17ba72
RR
542}
543
0b855868 544void wxFileCtrl::SetWild( const wxString &wild )
8b17ba72 545{
0b855868
RR
546 m_wild = wild;
547 Update();
548}
549
550void wxFileCtrl::MakeDir()
551{
223d09f6 552 wxString new_name( wxT("NewName") );
0b855868 553 wxString path( m_dirName );
223d09f6 554 path += wxT("/");
0b855868
RR
555 path += new_name;
556 if (wxFileExists(path))
8b17ba72 557 {
0b855868
RR
558 // try NewName0, NewName1 etc.
559 int i = 0;
c8c0e54c 560 do {
0b855868 561 new_name = _("NewName");
c8c0e54c 562 wxString num;
223d09f6 563 num.Printf( wxT("%d"), i );
c8c0e54c
VZ
564 new_name += num;
565
0b855868 566 path = m_dirName;
223d09f6 567 path += wxT("/");
0b855868 568 path += new_name;
c8c0e54c
VZ
569 i++;
570 } while (wxFileExists(path));
8b17ba72 571 }
c8c0e54c 572
0b855868 573 wxLogNull log;
c8c0e54c 574 if (!wxMkdir(path))
8b17ba72 575 {
0b855868 576 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 577 dialog.ShowModal();
0b855868 578 return;
8b17ba72 579 }
8b17ba72 580
0b855868
RR
581 wxFileData *fd = new wxFileData( new_name, path );
582 wxListItem item;
583 item.m_itemId = 0;
584 item.m_col = 0;
13111b2a 585 long id = Add( fd, item );
c8c0e54c 586
0b855868
RR
587 if (id != -1)
588 {
589 SortItems( ListCompare, 0 );
c8c0e54c 590 id = FindItem( 0, (long)fd );
0b855868
RR
591 EnsureVisible( id );
592 EditLabel( id );
593 }
8b17ba72
RR
594}
595
596void wxFileCtrl::GoToParentDir()
597{
223d09f6 598 if (m_dirName != wxT("/"))
8b17ba72 599 {
c8c0e54c 600 wxString fname( wxFileNameFromPath(m_dirName) );
0b855868 601 m_dirName = wxPathOnly( m_dirName );
223d09f6 602 if (m_dirName.IsEmpty()) m_dirName = wxT("/");
8b17ba72 603 Update();
13111b2a 604 long id = FindItem( 0, fname );
c8c0e54c
VZ
605 if (id != -1)
606 {
607 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
608 EnsureVisible( id );
609 }
8b17ba72
RR
610 }
611}
612
613void wxFileCtrl::GoToHomeDir()
614{
615 wxString s = wxGetUserHome( wxString() );
616 m_dirName = s;
617 Update();
e6daf794
RR
618 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
619 EnsureVisible( 0 );
8b17ba72
RR
620}
621
622void wxFileCtrl::GoToDir( const wxString &dir )
623{
624 m_dirName = dir;
625 Update();
e6daf794
RR
626 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
627 EnsureVisible( 0 );
8b17ba72
RR
628}
629
630void wxFileCtrl::GetDir( wxString &dir )
631{
632 dir = m_dirName;
633}
634
8b17ba72
RR
635void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
636{
637 wxFileData *fd = (wxFileData*)event.m_item.m_data;
638 delete fd;
639}
640
0b855868 641void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
8b17ba72
RR
642{
643 wxFileData *fd = (wxFileData*)event.m_item.m_data;
0b855868 644 wxASSERT( fd );
c8c0e54c 645
0b855868
RR
646 if ((event.GetLabel().IsEmpty()) ||
647 (event.GetLabel() == _(".")) ||
648 (event.GetLabel() == _("..")) ||
223d09f6 649 (event.GetLabel().First( wxT("/") ) != wxNOT_FOUND))
8b17ba72 650 {
0b855868 651 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 652 dialog.ShowModal();
0b855868 653 event.Veto();
c8c0e54c 654 return;
8b17ba72 655 }
c8c0e54c 656
0b855868 657 wxString new_name( wxPathOnly( fd->GetFullName() ) );
223d09f6 658 new_name += wxT("/");
0b855868 659 new_name += event.GetLabel();
c8c0e54c 660
0b855868 661 wxLogNull log;
c8c0e54c 662
0b855868 663 if (wxFileExists(new_name))
8b17ba72 664 {
0b855868 665 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 666 dialog.ShowModal();
0b855868 667 event.Veto();
8b17ba72 668 }
c8c0e54c 669
0b855868 670 if (wxRenameFile(fd->GetFullName(),new_name))
8b17ba72 671 {
0b855868 672 fd->SetNewName( new_name, event.GetLabel() );
c8c0e54c 673 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
0b855868 674 EnsureVisible( event.GetItem() );
8b17ba72
RR
675 }
676 else
677 {
0b855868 678 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 679 dialog.ShowModal();
0b855868 680 event.Veto();
8b17ba72 681 }
8b17ba72
RR
682}
683
684//-----------------------------------------------------------------------------
685// wxFileDialog
686//-----------------------------------------------------------------------------
687
a16d3c04
KB
688#define ID_LIST_MODE wxID_FILEDLGG
689#define ID_REPORT_MODE wxID_FILEDLGG + 1
690#define ID_UP_DIR wxID_FILEDLGG + 5
691#define ID_PARENT_DIR wxID_FILEDLGG + 6
692#define ID_NEW_DIR wxID_FILEDLGG + 7
693#define ID_CHOICE wxID_FILEDLGG + 8
694#define ID_TEXT wxID_FILEDLGG + 9
695#define ID_LIST_CTRL wxID_FILEDLGG + 10
696#define ID_ACTIVATED wxID_FILEDLGG + 11
bf9e3e73 697#define ID_CHECK wxID_FILEDLGG + 12
8b17ba72
RR
698
699IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
700
701BEGIN_EVENT_TABLE(wxFileDialog,wxDialog)
702 EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList)
703 EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport)
8b17ba72
RR
704 EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp)
705 EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome)
0b855868 706 EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew)
8b17ba72
RR
707 EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk)
708 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected)
c8c0e54c
VZ
709 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated)
710 EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice)
711 EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter)
bf9e3e73 712 EVT_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck)
8b17ba72
RR
713END_EVENT_TABLE()
714
eaf40b23
VS
715long wxFileDialog::s_lastViewStyle = wxLC_LIST;
716bool wxFileDialog::s_lastShowHidden = FALSE;
655cf310 717
8b17ba72
RR
718wxFileDialog::wxFileDialog(wxWindow *parent,
719 const wxString& message,
720 const wxString& defaultDir,
721 const wxString& defaultFile,
722 const wxString& wildCard,
723 long style,
724 const wxPoint& pos ) :
9c884972 725 wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
8b17ba72
RR
726{
727 wxBeginBusyCursor();
c8c0e54c 728
eaf40b23
VS
729 if (wxConfig::Get(FALSE))
730 {
731 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ViewStyle"), &s_lastViewStyle);
732 wxConfig::Get() -> Read(wxT("/wxWindows/wxFileDialog/ShowHidden"), &s_lastShowHidden);
733 }
734
8b17ba72
RR
735 m_message = message;
736 m_dialogStyle = style;
479cd5de 737
655cf310 738 if (m_dialogStyle == 0) m_dialogStyle = wxOPEN;
7941ba11 739 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
9b00bb16 740 m_dialogStyle |= wxOPEN;
479cd5de 741
8b17ba72 742 m_dir = defaultDir;
f59d80ca 743 if ((m_dir.IsEmpty()) || (m_dir == wxT(".")))
ac2def68
RR
744 {
745 char buf[200];
c8c0e54c 746 m_dir = getcwd( buf, sizeof(buf) );
ac2def68 747 }
8b17ba72 748 m_path = defaultDir;
223d09f6 749 m_path += wxT("/");
8b17ba72
RR
750 m_path += defaultFile;
751 m_fileName = defaultFile;
752 m_wildCard = wildCard;
753 m_filterIndex = 0;
655cf310 754 m_filterExtension = wxEmptyString;
c8c0e54c 755
cae5359f 756 // interpret wildcards
c8c0e54c 757
cae5359f
RR
758 if (m_wildCard.IsEmpty())
759 m_wildCard = _("All files (*)|*");
c8c0e54c 760
223d09f6 761 wxStringTokenizer tokens( m_wildCard, wxT("|") );
cae5359f
RR
762 wxString firstWild;
763 wxString firstWildText;
764 if (tokens.CountTokens() == 1)
765 {
766 firstWildText = tokens.GetNextToken();
767 firstWild = firstWildText;
768 }
769 else
770 {
223d09f6 771 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
cae5359f
RR
772 firstWildText = tokens.GetNextToken();
773 firstWild = tokens.GetNextToken();
774 }
655cf310
VS
775 if ( firstWild.Left( 2 ) == wxT("*.") )
776 m_filterExtension = firstWild.Mid( 1 );
777 if ( m_filterExtension == ".*" ) m_filterExtension = wxEmptyString;
cae5359f
RR
778
779 // layout
c8c0e54c 780
8b17ba72 781 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 782
8b17ba72 783 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 784
e6daf794 785 wxBitmapButton *but;
c8c0e54c 786
e6daf794
RR
787 but = new wxBitmapButton( this, ID_LIST_MODE, wxBitmap( listview_xpm ) );
788#if wxUSE_TOOLTIPS
789 but->SetToolTip( _("View files as a list view") );
790#endif
791 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 792
e6daf794
RR
793 but = new wxBitmapButton( this, ID_REPORT_MODE, wxBitmap( repview_xpm ) );
794#if wxUSE_TOOLTIPS
795 but->SetToolTip( _("View files as a detailed view") );
796#endif
797 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 798
0b855868 799 buttonsizer->Add( 30, 5, 1 );
e6daf794
RR
800
801 but = new wxBitmapButton( this, ID_UP_DIR, wxBitmap( dir_up_xpm ) );
802#if wxUSE_TOOLTIPS
803 but->SetToolTip( _("Go to parent directory") );
804#endif
805 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 806
e6daf794
RR
807 but = new wxBitmapButton( this, ID_PARENT_DIR, wxBitmap(home_xpm) );
808#if wxUSE_TOOLTIPS
809 but->SetToolTip( _("Go to home directory") );
810#endif
811 buttonsizer->Add( but, 0, wxALL, 5);
c8c0e54c 812
e6daf794 813 buttonsizer->Add( 20, 20 );
c8c0e54c 814
e6daf794
RR
815 but = new wxBitmapButton( this, ID_NEW_DIR, wxBitmap(new_dir_xpm) );
816#if wxUSE_TOOLTIPS
817 but->SetToolTip( _("Create new directory") );
818#endif
819 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 820
0b855868 821 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
c8c0e54c 822
9c884972
RR
823 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
824 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
825 m_static = new wxStaticText( this, -1, m_dir );
826 staticsizer->Add( m_static, 1 );
827 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
828
7941ba11 829 if (m_dialogStyle & wxMULTIPLE)
479cd5de 830 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition,
eaf40b23 831 wxSize(440,180), s_lastViewStyle | wxSUNKEN_BORDER );
7941ba11 832 else
479cd5de 833 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition,
eaf40b23
VS
834 wxSize(440,180), s_lastViewStyle | wxSUNKEN_BORDER | wxLC_SINGLE_SEL );
835 m_list -> ShowHidden(s_lastShowHidden);
0b855868 836 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
c8c0e54c 837
8b17ba72 838 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
cae5359f 839 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
0b855868 840 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
8b17ba72
RR
841 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
842 mainsizer->Add( textsizer, 0, wxEXPAND );
843
844 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
cae5359f 845 m_choice = new wxChoice( this, ID_CHOICE );
8b17ba72 846 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
bf9e3e73 847 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
eaf40b23 848 m_check->SetValue( s_lastShowHidden );
bf9e3e73 849 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
8b17ba72
RR
850 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
851 mainsizer->Add( choicesizer, 0, wxEXPAND );
c8c0e54c 852
cae5359f
RR
853 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
854 while (tokens.HasMoreTokens())
855 {
856 firstWildText = tokens.GetNextToken();
857 firstWild = tokens.GetNextToken();
858 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
859 }
860 m_choice->SetSelection( 0 );
8b17ba72
RR
861
862 SetAutoLayout( TRUE );
863 SetSizer( mainsizer );
c8c0e54c 864
8b17ba72
RR
865 mainsizer->Fit( this );
866 mainsizer->SetSizeHints( this );
eaf40b23 867
8b17ba72 868 Centre( wxBOTH );
ed58dbea
RR
869
870 if (m_fileName.IsEmpty())
871 m_list->SetFocus();
872 else
873 m_text->SetFocus();
c8c0e54c 874
8b17ba72
RR
875 wxEndBusyCursor();
876}
877
cae5359f
RR
878wxFileDialog::~wxFileDialog()
879{
eaf40b23
VS
880 if (wxConfig::Get(FALSE))
881 {
882 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ViewStyle"), s_lastViewStyle);
883 wxConfig::Get() -> Write(wxT("/wxWindows/wxFileDialog/ShowHidden"), s_lastShowHidden);
884 }
cae5359f
RR
885}
886
887void wxFileDialog::OnChoice( wxCommandEvent &event )
888{
13111b2a
VZ
889 int index = (int)event.GetInt();
890 wxString *str = (wxString*) m_choice->GetClientData( index );
cae5359f 891 m_list->SetWild( *str );
13111b2a 892 m_filterIndex = index;
655cf310
VS
893 if ( str -> Left( 2 ) == wxT("*.") )
894 {
895 m_filterExtension = str -> Mid( 1 );
896 if (m_filterExtension == ".*") m_filterExtension = wxEmptyString;
897 }
898 else
899 m_filterExtension = wxEmptyString;
cae5359f
RR
900}
901
bf9e3e73
RR
902void wxFileDialog::OnCheck( wxCommandEvent &event )
903{
eaf40b23 904 m_list->ShowHidden( (s_lastShowHidden = event.GetInt() != 0) );
bf9e3e73
RR
905}
906
e1811a01 907void wxFileDialog::OnActivated( wxListEvent &event )
8b17ba72 908{
e1811a01 909 HandleAction( event.m_item.m_text );
8b17ba72
RR
910}
911
cae5359f
RR
912void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
913{
914 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
915 cevent.SetEventObject( this );
916 GetEventHandler()->ProcessEvent( cevent );
917}
918
8b17ba72
RR
919void wxFileDialog::OnSelected( wxListEvent &event )
920{
e1811a01 921 if (FindFocus() != m_list) return;
479cd5de 922
e1811a01 923 wxString filename( event.m_item.m_text );
223d09f6 924 if (filename == wxT("..")) return;
479cd5de 925
e1811a01
RR
926 wxString dir;
927 m_list->GetDir( dir );
223d09f6 928 if (dir != wxT("/")) dir += wxT("/");
e1811a01
RR
929 dir += filename;
930 if (wxDirExists(dir)) return;
479cd5de 931
e1811a01 932 m_text->SetValue( filename );
8b17ba72
RR
933}
934
e1811a01 935void wxFileDialog::HandleAction( const wxString &fn )
8b17ba72 936{
e1811a01 937 wxString filename( fn );
8b17ba72
RR
938 wxString dir;
939 m_list->GetDir( dir );
940 if (filename.IsEmpty()) return;
223d09f6 941 if (filename == wxT(".")) return;
c8c0e54c 942
223d09f6 943 if (filename == wxT(".."))
0b855868
RR
944 {
945 m_list->GoToParentDir();
946 m_list->SetFocus();
9c884972
RR
947 m_list->GetDir( dir );
948 m_static->SetLabel( dir );
c8c0e54c 949 return;
0b855868
RR
950 }
951
223d09f6 952 if (filename == wxT("~"))
ed58dbea
RR
953 {
954 m_list->GoToHomeDir();
955 m_list->SetFocus();
956 m_list->GetDir( dir );
957 m_static->SetLabel( dir );
c8c0e54c 958 return;
ed58dbea 959 }
c8c0e54c 960
223d09f6 961 if (filename[0] == wxT('~'))
ed58dbea
RR
962 {
963 filename.Remove( 0, 1 );
c8c0e54c 964 wxString tmp( wxGetUserHome() );
223d09f6 965 tmp += wxT('/');
c8c0e54c
VZ
966 tmp += filename;
967 filename = tmp;
ed58dbea
RR
968 }
969
223d09f6
KB
970 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
971 (filename.Find(wxT('?')) != wxNOT_FOUND))
cae5359f 972 {
223d09f6 973 if (filename.Find(wxT('/')) != wxNOT_FOUND)
c8c0e54c
VZ
974 {
975 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
976 return;
977 }
978 m_list->SetWild( filename );
979 return;
cae5359f
RR
980 }
981
223d09f6
KB
982 if (dir != wxT("/")) dir += wxT("/");
983 if (filename[0] != wxT('/'))
ed58dbea
RR
984 {
985 dir += filename;
986 filename = dir;
987 }
c8c0e54c 988
8b17ba72
RR
989 if (wxDirExists(filename))
990 {
991 m_list->GoToDir( filename );
9c884972
RR
992 m_list->GetDir( dir );
993 m_static->SetLabel( dir );
c8c0e54c 994 return;
8b17ba72 995 }
c8c0e54c 996
655cf310 997
8b17ba72
RR
998 if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) )
999 {
655cf310
VS
1000 if (filename.Find( wxT('.') ) == wxNOT_FOUND ||
1001 filename.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND)
1002 filename << m_filterExtension;
8b17ba72
RR
1003 if (wxFileExists( filename ))
1004 {
1005 wxString msg;
1006 msg.Printf( _("File '%s' already exists, do you really want to "
1007 "overwrite it?"), filename.c_str() );
1008
1009 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1010 return;
1011 }
1012 }
655cf310 1013 else if ( m_dialogStyle & wxOPEN )
8b17ba72
RR
1014 {
1015 if ( !wxFileExists( filename ) )
655cf310
VS
1016 if (filename.Find( wxT('.') ) == wxNOT_FOUND ||
1017 filename.AfterLast( wxT('.') ).Find( wxT('/') ) != wxNOT_FOUND)
1018 filename << m_filterExtension;
1019
1020 if ( m_dialogStyle & wxFILE_MUST_EXIST )
8b17ba72 1021 {
655cf310
VS
1022 if ( !wxFileExists( filename ) )
1023 {
1024 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR );
1025 return;
1026 }
8b17ba72
RR
1027 }
1028 }
1029
1030 SetPath( filename );
479cd5de 1031
9b7e522a
RR
1032 wxCommandEvent event;
1033 wxDialog::OnOK(event);
e1811a01
RR
1034}
1035
5e0201ea 1036void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
e1811a01
RR
1037{
1038 HandleAction( m_text->GetValue() );
8b17ba72
RR
1039}
1040
1041void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
1042{
1043 m_list->ChangeToListMode();
eaf40b23 1044 s_lastViewStyle = wxLC_LIST;
0b855868 1045 m_list->SetFocus();
8b17ba72
RR
1046}
1047
1048void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
1049{
1050 m_list->ChangeToReportMode();
eaf40b23 1051 s_lastViewStyle = wxLC_REPORT;
0b855868 1052 m_list->SetFocus();
8b17ba72
RR
1053}
1054
1055void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
1056{
1057 m_list->GoToParentDir();
0b855868 1058 m_list->SetFocus();
9c884972
RR
1059 wxString dir;
1060 m_list->GetDir( dir );
1061 m_static->SetLabel( dir );
8b17ba72
RR
1062}
1063
1064void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
1065{
1066 m_list->GoToHomeDir();
0b855868 1067 m_list->SetFocus();
9c884972
RR
1068 wxString dir;
1069 m_list->GetDir( dir );
1070 m_static->SetLabel( dir );
0b855868
RR
1071}
1072
1073void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
1074{
1075 m_list->MakeDir();
8b17ba72
RR
1076}
1077
1078void wxFileDialog::SetPath( const wxString& path )
1079{
1080 // not only set the full path but also update filename and dir
1081 m_path = path;
1082 if ( !!path )
1083 {
1084 wxString ext;
1085 wxSplitPath(path, &m_dir, &m_fileName, &ext);
c8c0e54c
VZ
1086 if (!ext.IsEmpty())
1087 {
223d09f6 1088 m_fileName += wxT(".");
8b17ba72 1089 m_fileName += ext;
c8c0e54c 1090 }
8b17ba72
RR
1091 }
1092}
c8c0e54c 1093
7941ba11
RR
1094void wxFileDialog::GetPaths( wxArrayString& paths ) const
1095{
1096 paths.Empty();
28c9c76e
RR
1097 if (m_list->GetSelectedItemCount() == 0)
1098 {
1099 paths.Add( GetPath() );
1100 return;
1101 }
479cd5de 1102
7941ba11
RR
1103 paths.Alloc( m_list->GetSelectedItemCount() );
1104
1105 wxString dir;
1106 m_list->GetDir( dir );
1107 if (dir != wxT("/")) dir += wxT("/");
1108
1109 wxListItem item;
1110 item.m_mask = wxLIST_MASK_TEXT;
1111
1112 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1113 while ( item.m_itemId != -1 ) {
1114 m_list->GetItem( item );
1115 paths.Add( dir + item.m_text );
1116 item.m_itemId = m_list->GetNextItem( item.m_itemId + 1,
1117 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1118 }
1119}
1120
1121void wxFileDialog::GetFilenames(wxArrayString& files) const
1122{
1123 files.Empty();
28c9c76e
RR
1124 if (m_list->GetSelectedItemCount() == 0)
1125 {
1126 files.Add( GetFilename() );
1127 return;
1128 }
7941ba11
RR
1129 files.Alloc( m_list->GetSelectedItemCount() );
1130
1131 wxListItem item;
1132 item.m_mask = wxLIST_MASK_TEXT;
1133
1134 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1135 while ( item.m_itemId != -1 ) {
1136 m_list->GetItem( item );
1137 files.Add( item.m_text );
1138 item.m_itemId = m_list->GetNextItem( item.m_itemId + 1,
1139 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1140 }
1141}
1142
655cf310
VS
1143
1144
8b17ba72
RR
1145// ----------------------------------------------------------------------------
1146// global functions
1147// ----------------------------------------------------------------------------
1148
1149wxString
1150wxFileSelectorEx(const wxChar *message,
1151 const wxChar *default_path,
1152 const wxChar *default_filename,
5e0201ea 1153 int *WXUNUSED(indexDefaultExtension),
8b17ba72
RR
1154 const wxChar *wildcard,
1155 int flags,
1156 wxWindow *parent,
1157 int x, int y)
1158{
1159 // TODO: implement this somehow
223d09f6 1160 return wxFileSelector(message, default_path, default_filename, wxT(""),
8b17ba72
RR
1161 wildcard, flags, parent, x, y);
1162}
1163
1164wxString wxFileSelector( const wxChar *title,
1165 const wxChar *defaultDir, const wxChar *defaultFileName,
1166 const wxChar *defaultExtension, const wxChar *filter, int flags,
1167 wxWindow *parent, int x, int y )
1168{
1169 wxString filter2;
1170 if ( defaultExtension && !filter )
223d09f6 1171 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
8b17ba72
RR
1172 else if ( filter )
1173 filter2 = filter;
1174
1175 wxString defaultDirString;
1176 if (defaultDir)
1177 defaultDirString = defaultDir;
1178
1179 wxString defaultFilenameString;
1180 if (defaultFileName)
1181 defaultFilenameString = defaultFileName;
1182
1183 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
1184
1185 if ( fileDialog.ShowModal() == wxID_OK )
1186 {
1187 return fileDialog.GetPath();
1188 }
1189 else
1190 {
1191 return wxEmptyString;
1192 }
1193}
1194
1195wxString wxLoadFileSelector( const wxChar *what, const wxChar *extension, const wxChar *default_name, wxWindow *parent )
1196{
1197 wxChar *ext = (wxChar *)extension;
1198
1199 wxChar prompt[50];
1200 wxString str = _("Load %s file");
1201 wxSprintf(prompt, str, what);
1202
223d09f6 1203 if (*ext == wxT('.')) ext++;
8b17ba72 1204 wxChar wild[60];
223d09f6 1205 wxSprintf(wild, wxT("*.%s"), ext);
8b17ba72
RR
1206
1207 return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent);
1208}
1209
1210wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name,
1211 wxWindow *parent )
1212{
1213 wxChar *ext = (wxChar *)extension;
1214
1215 wxChar prompt[50];
1216 wxString str = _("Save %s file");
1217 wxSprintf(prompt, str, what);
1218
223d09f6 1219 if (*ext == wxT('.')) ext++;
8b17ba72 1220 wxChar wild[60];
223d09f6 1221 wxSprintf(wild, wxT("*.%s"), ext);
8b17ba72
RR
1222
1223 return wxFileSelector (prompt, (const wxChar *) NULL, default_name, ext, wild, 0, parent);
1224}
1225
655cf310
VS
1226
1227
1228
1229
1230
1231// A module to allow icons table cleanup
1232
1233class wxFileDialogGenericModule: public wxModule
1234{
1235DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule)
1236public:
1237 wxFileDialogGenericModule() {}
1238 bool OnInit() { return TRUE; }
1239 void OnExit() { if (g_IconsTable) {delete g_IconsTable; g_IconsTable = NULL;} }
1240};
1241
1242IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule, wxModule)