]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
Applied patch [ 603104 ] wxX11 wxClientDC, wxPaintDC fix
[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
1e6feb95
VZ
23#if wxUSE_FILEDLG
24
4894ae18
VS
25#if !defined(__UNIX__) && !defined(__DOS__)
26#error wxFileDialog currently only supports Unix and DOS
8b17ba72
RR
27#endif
28
04dbb646
VZ
29#include "wx/checkbox.h"
30#include "wx/textctrl.h"
31#include "wx/choice.h"
32#include "wx/checkbox.h"
33#include "wx/stattext.h"
8b17ba72 34#include "wx/filedlg.h"
8b17ba72
RR
35#include "wx/debug.h"
36#include "wx/log.h"
37#include "wx/intl.h"
04dbb646 38#include "wx/listctrl.h"
8b17ba72
RR
39#include "wx/msgdlg.h"
40#include "wx/sizer.h"
0b855868 41#include "wx/bmpbuttn.h"
cae5359f 42#include "wx/tokenzr.h"
655cf310
VS
43#include "wx/mimetype.h"
44#include "wx/image.h"
45#include "wx/module.h"
eaf40b23 46#include "wx/config.h"
48fe8374 47#include "wx/imaglist.h"
5e673a6a 48#include "wx/dir.h"
60d2cc25 49#include "wx/artprov.h"
8b17ba72 50
e6daf794
RR
51#if wxUSE_TOOLTIPS
52 #include "wx/tooltip.h"
53#endif
54
ac2def68
RR
55#include <sys/types.h>
56#include <sys/stat.h>
4894ae18
VS
57
58#ifdef __UNIX__
59 #include <dirent.h>
60 #include <pwd.h>
61 #ifndef __VMS
62 # include <grp.h>
63 #endif
64#endif
65
66#ifdef __WATCOMC__
67 #include <direct.h>
27df579a 68#endif
4894ae18 69
7a82dabc 70#include <time.h>
ac2def68 71#include <unistd.h>
8b17ba72 72
04dbb646
VZ
73//-----------------------------------------------------------------------------
74// wxFileData
75//-----------------------------------------------------------------------------
76
7a82dabc 77class wxFileData
04dbb646 78{
04dbb646 79public:
04dbb646
VZ
80 wxFileData( const wxString &name, const wxString &fname );
81 wxString GetName() const;
82 wxString GetFullName() const;
83 wxString GetHint() const;
84 wxString GetEntry( int num );
85 bool IsDir();
86 bool IsLink();
87 bool IsExe();
88 long GetSize();
89 void MakeItem( wxListItem &item );
90 void SetNewName( const wxString &name, const wxString &fname );
91
92private:
37fd1c97
VS
93 wxString m_name;
94 wxString m_fileName;
95 long m_size;
96 int m_hour;
97 int m_minute;
98 int m_year;
99 int m_month;
100 int m_day;
101 wxString m_permissions;
102 bool m_isDir;
103 bool m_isLink;
104 bool m_isExe;
04dbb646
VZ
105};
106
107//-----------------------------------------------------------------------------
108// wxFileCtrl
109//-----------------------------------------------------------------------------
110
111class wxFileCtrl : public wxListCtrl
112{
04dbb646
VZ
113public:
114 wxFileCtrl();
115 wxFileCtrl( wxWindow *win,
116 wxWindowID id,
117 const wxString &dirName,
118 const wxString &wild,
119 const wxPoint &pos = wxDefaultPosition,
120 const wxSize &size = wxDefaultSize,
121 long style = wxLC_LIST,
122 const wxValidator &validator = wxDefaultValidator,
123 const wxString &name = wxT("filelist") );
7a82dabc
VZ
124 virtual ~wxFileCtrl();
125
04dbb646
VZ
126 void ChangeToListMode();
127 void ChangeToReportMode();
128 void ChangeToIconMode();
129 void ShowHidden( bool show = TRUE );
130 long Add( wxFileData *fd, wxListItem &item );
8f4fcc4e 131 void UpdateFiles();
04dbb646
VZ
132 virtual void StatusbarText( wxChar *WXUNUSED(text) ) {};
133 void MakeDir();
134 void GoToParentDir();
135 void GoToHomeDir();
136 void GoToDir( const wxString &dir );
137 void SetWild( const wxString &wild );
138 void GetDir( wxString &dir );
139 void OnListDeleteItem( wxListEvent &event );
04dbb646 140 void OnListEndLabelEdit( wxListEvent &event );
7a82dabc 141
37fd1c97
VS
142 // Associate commonly used UI controls with wxFileCtrl so that they can be
143 // disabled when they cannot be used (e.g. can't go to parent directory
144 // if wxFileCtrl already is in the root dir):
145 void SetGoToParentControl(wxWindow *ctrl) { m_goToParentControl = ctrl; }
146 void SetNewDirControl(wxWindow *ctrl) { m_newDirControl = ctrl; }
04dbb646
VZ
147
148private:
7a82dabc
VZ
149 void FreeItemData(const wxListItem& item);
150 void FreeAllItemsData();
151
37fd1c97
VS
152 wxString m_dirName;
153 bool m_showHidden;
154 wxString m_wild;
7a82dabc 155
37fd1c97
VS
156 wxWindow *m_goToParentControl;
157 wxWindow *m_newDirControl;
158
04dbb646
VZ
159 DECLARE_DYNAMIC_CLASS(wxFileCtrl);
160 DECLARE_EVENT_TABLE()
161};
162
655cf310
VS
163// ----------------------------------------------------------------------------
164// private classes - icons list management
165// ----------------------------------------------------------------------------
166
167class wxFileIconEntry : public wxObject
168{
473d087e
RR
169public:
170 wxFileIconEntry(int i) { id = i; }
48fe8374 171
473d087e 172 int id;
655cf310
VS
173};
174
175
176class wxFileIconsTable
177{
473d087e
RR
178public:
179 wxFileIconsTable();
655cf310 180
473d087e
RR
181 int GetIconID(const wxString& extension, const wxString& mime = wxEmptyString);
182 wxImageList *GetImageList() { return &m_ImageList; }
48fe8374 183
473d087e
RR
184protected:
185 wxImageList m_ImageList;
186 wxHashTable m_HashTable;
655cf310
VS
187};
188
189static wxFileIconsTable *g_IconsTable = NULL;
190
b0c5c421
VS
191#define FI_FOLDER 0
192#define FI_UNKNOWN 1
193#define FI_EXECUTABLE 2
655cf310
VS
194
195wxFileIconsTable::wxFileIconsTable() :
196 m_ImageList(16, 16),
73725567 197 m_HashTable(wxKEY_STRING)
655cf310
VS
198{
199 m_HashTable.DeleteContents(TRUE);
60d2cc25
VS
200 // FI_FOLDER:
201 m_ImageList.Add(wxArtProvider::GetBitmap(wxART_FOLDER, wxART_CMN_DIALOG));
202 // FI_UNKNOWN:
7a82dabc 203 m_ImageList.Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_CMN_DIALOG));
60d2cc25 204 // FI_EXECUTABLE:
48fe8374 205 if (GetIconID(wxEmptyString, _T("application/x-executable")) == FI_UNKNOWN)
60d2cc25
VS
206 {
207 m_ImageList.Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE, wxART_CMN_DIALOG));
b0c5c421
VS
208 m_HashTable.Delete(_T("exe"));
209 m_HashTable.Put(_T("exe"), new wxFileIconEntry(FI_EXECUTABLE));
210 }
211 /* else put into list by GetIconID
212 (KDE defines application/x-executable for *.exe and has nice icon)
213 */
655cf310
VS
214}
215
216
217
75ec8bd4
VS
218#if wxUSE_MIMETYPE
219// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
220// one icon and we won't resize it
221
655cf310
VS
222static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
223{
224 wxImage small(16, 16);
225 unsigned char *p1, *p2, *ps;
48fe8374
VZ
226 unsigned char mr = img.GetMaskRed(),
227 mg = img.GetMaskGreen(),
b0c5c421 228 mb = img.GetMaskBlue();
48fe8374 229
655cf310
VS
230 unsigned x, y;
231 unsigned sr, sg, sb, smask;
48fe8374 232
655cf310
VS
233 p1 = img.GetData(), p2 = img.GetData() + 3 * 32, ps = small.GetData();
234 small.SetMaskColour(mr, mr, mr);
48fe8374 235
655cf310
VS
236 for (y = 0; y < 16; y++)
237 {
238 for (x = 0; x < 16; x++)
239 {
240 sr = sg = sb = smask = 0;
241 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
242 sr += p1[0], sg += p1[1], sb += p1[2];
243 else smask++;
244 p1 += 3;
245 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
246 sr += p1[0], sg += p1[1], sb += p1[2];
247 else smask++;
248 p1 += 3;
249 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
250 sr += p2[0], sg += p2[1], sb += p2[2];
251 else smask++;
252 p2 += 3;
253 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
254 sr += p2[0], sg += p2[1], sb += p2[2];
255 else smask++;
256 p2 += 3;
48fe8374
VZ
257
258 if (smask > 2)
655cf310
VS
259 ps[0] = ps[1] = ps[2] = mr;
260 else
261 ps[0] = sr >> 2, ps[1] = sg >> 2, ps[2] = sb >> 2;
262 ps += 3;
263 }
264 p1 += 32 * 3, p2 += 32 * 3;
265 }
78316bbe 266
368d59f0 267 return wxBitmap(small);
655cf310
VS
268}
269
78316bbe
VS
270// finds empty borders and return non-empty area of image:
271static wxImage CutEmptyBorders(const wxImage& img)
272{
48fe8374
VZ
273 unsigned char mr = img.GetMaskRed(),
274 mg = img.GetMaskGreen(),
78316bbe
VS
275 mb = img.GetMaskBlue();
276 unsigned char *dt = img.GetData(), *dttmp;
277 unsigned w = img.GetWidth(), h = img.GetHeight();
48fe8374 278
78316bbe
VS
279 unsigned top, bottom, left, right, i;
280 bool empt;
281
282#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
283#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
48fe8374 284
78316bbe
VS
285 for (empt = TRUE, top = 0; empt && top < h; top++)
286 {
287 MK_DTTMP(0, top);
288 for (i = 0; i < w; i++, dttmp+=3)
289 NOEMPTY_PIX(empt)
290 }
291 for (empt = TRUE, bottom = h-1; empt && bottom > top; bottom--)
292 {
293 MK_DTTMP(0, bottom);
294 for (i = 0; i < w; i++, dttmp+=3)
295 NOEMPTY_PIX(empt)
296 }
297 for (empt = TRUE, left = 0; empt && left < w; left++)
298 {
299 MK_DTTMP(left, 0);
300 for (i = 0; i < h; i++, dttmp+=3*w)
301 NOEMPTY_PIX(empt)
302 }
303 for (empt = TRUE, right = w-1; empt && right > left; right--)
304 {
305 MK_DTTMP(right, 0);
306 for (i = 0; i < h; i++, dttmp+=3*w)
307 NOEMPTY_PIX(empt)
308 }
309 top--, left--, bottom++, right++;
310
311 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
312}
75ec8bd4 313#endif // wxUSE_MIMETYPE
78316bbe
VS
314
315
316
b0c5c421 317int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
655cf310 318{
4894ae18 319#if wxUSE_MIMETYPE
b0c5c421
VS
320 if (!extension.IsEmpty())
321 {
322 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable.Get(extension);
323 if (entry) return (entry -> id);
324 }
325
48fe8374 326 wxFileType *ft = (mime.IsEmpty()) ?
73725567
VS
327 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
328 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
655cf310 329 wxIcon ic;
26065a88 330 if (ft == NULL || (!ft -> GetIcon(&ic)) || (!ic.Ok()))
655cf310 331 {
b0c5c421 332 int newid = FI_UNKNOWN;
655cf310
VS
333 m_HashTable.Put(extension, new wxFileIconEntry(newid));
334 return newid;
335 }
368d59f0 336 wxImage img = ic.ConvertToImage();
655cf310 337 delete ft;
48fe8374 338
655cf310
VS
339 int id = m_ImageList.GetImageCount();
340 if (img.GetWidth() == 16 && img.GetHeight() == 16)
368d59f0 341 m_ImageList.Add(wxBitmap(img));
655cf310 342 else
b4b58c41
VS
343 {
344 if (img.GetWidth() != 32 || img.GetHeight() != 32)
78316bbe
VS
345 m_ImageList.Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(32, 32)));
346 else
347 m_ImageList.Add(CreateAntialiasedBitmap(img));
b4b58c41 348 }
655cf310
VS
349 m_HashTable.Put(extension, new wxFileIconEntry(id));
350 return id;
4894ae18
VS
351
352#else // !wxUSE_MIMETYPE
353
354 if (extension == wxT("exe"))
355 return FI_EXECUTABLE;
356 else
357 return FI_UNKNOWN;
358#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
655cf310
VS
359}
360
361
362
c8c0e54c
VZ
363// ----------------------------------------------------------------------------
364// private functions
365// ----------------------------------------------------------------------------
366
367static
368int ListCompare( long data1, long data2, long WXUNUSED(data) )
369{
370 wxFileData *fd1 = (wxFileData*)data1 ;
371 wxFileData *fd2 = (wxFileData*)data2 ;
223d09f6
KB
372 if (fd1->GetName() == wxT("..")) return -1;
373 if (fd2->GetName() == wxT("..")) return 1;
c8c0e54c
VZ
374 if (fd1->IsDir() && !fd2->IsDir()) return -1;
375 if (fd2->IsDir() && !fd1->IsDir()) return 1;
6eec2bee 376 return wxStrcmp( fd1->GetName(), fd2->GetName() );
c8c0e54c
VZ
377}
378
5e673a6a
VS
379#ifdef __UNIX__
380#define IsTopMostDir(dir) (dir == wxT("/"))
381#endif
382
383#if defined(__DOS__) || defined(__WINDOWS__)
384#define IsTopMostDir(dir) (dir.IsEmpty())
385#endif
386
37fd1c97
VS
387#if defined(__DOS__) || defined(__WINDOWS__)
388extern bool wxIsDriveAvailable(const wxString& dirName);
389#endif
390
8b17ba72
RR
391//-----------------------------------------------------------------------------
392// wxFileData
393//-----------------------------------------------------------------------------
394
8b17ba72
RR
395wxFileData::wxFileData( const wxString &name, const wxString &fname )
396{
397 m_name = name;
398 m_fileName = fname;
7a82dabc 399
37fd1c97
VS
400#if defined(__DOS__) || defined(__WINDOWS__)
401 // VS: In case the file is root directory of a volume (e.g. "C:"),
402 // we don't want it stat()ed, since the drive may not be in:
403 if (name.length() == 2 && name[1u] == wxT(':'))
404 {
405 m_isDir = TRUE;
406 m_isExe = m_isLink = FALSE;
407 m_size = 0;
408 return;
409 }
410#endif
c8c0e54c 411
8115f9e7
RR
412 wxStructStat buff;
413 wxStat( m_fileName, &buff );
479cd5de 414
4894ae18 415#if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
8b17ba72 416 struct stat lbuff;
6eec2bee 417 lstat( m_fileName.fn_str(), &lbuff );
9f2d09aa 418 m_isLink = S_ISLNK( lbuff.st_mode );
8b17ba72 419 struct tm *t = localtime( &lbuff.st_mtime );
9f2d09aa
RR
420#else
421 m_isLink = FALSE;
422 struct tm *t = localtime( &buff.st_mtime );
423#endif
424
8b17ba72
RR
425// struct passwd *user = getpwuid( buff.st_uid );
426// struct group *grp = getgrgid( buff.st_gid );
427
428 m_isDir = S_ISDIR( buff.st_mode );
8b17ba72
RR
429 m_isExe = ((buff.st_mode & S_IXUSR ) == S_IXUSR );
430
431 m_size = buff.st_size;
432
433 m_hour = t->tm_hour;
434 m_minute = t->tm_min;
435 m_month = t->tm_mon+1;
436 m_day = t->tm_mday;
437 m_year = t->tm_year;
d3e90957 438 m_year += 1900;
8b17ba72 439
8115f9e7
RR
440 char buffer[10];
441 sprintf( buffer, "%c%c%c",
442 ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? 'r' : '-'),
443 ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? 'w' : '-'),
444 ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? 'x' : '-') );
445#if wxUSE_UNICODE
446 m_permissions = wxConvUTF8.cMB2WC( buffer );
447#else
448 m_permissions = buffer;
449#endif
450
451// m_permissions.sprintf( wxT("%c%c%c"),
452// ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? wxT('r') : wxT('-')),
453// ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? wxT('w') : wxT('-')),
454// ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? wxT('x') : wxT('-')) );
8b17ba72
RR
455}
456
457wxString wxFileData::GetName() const
458{
459 return m_name;
460}
461
462wxString wxFileData::GetFullName() const
463{
464 return m_fileName;
465}
466
467wxString wxFileData::GetHint() const
468{
469 wxString s = m_fileName;
470 s += " ";
471 if (m_isDir) s += _("<DIR> ");
472 else if (m_isLink) s += _("<LINK> ");
473 else
474 {
475 s += LongToString( m_size );
476 s += _(" bytes ");
477 }
478 s += IntToString( m_day );
223d09f6 479 s += wxT(".");
8b17ba72 480 s += IntToString( m_month );
223d09f6 481 s += wxT(".");
8b17ba72 482 s += IntToString( m_year );
223d09f6 483 s += wxT(" ");
8b17ba72 484 s += IntToString( m_hour );
223d09f6 485 s += wxT(":");
8b17ba72 486 s += IntToString( m_minute );
223d09f6 487 s += wxT(" ");
8b17ba72
RR
488 s += m_permissions;
489 return s;
490};
491
c8c0e54c 492wxString wxFileData::GetEntry( int num )
8b17ba72
RR
493{
494 wxString s;
495 switch (num)
496 {
497 case 0:
d3e90957 498 {
8b17ba72 499 s = m_name;
d3e90957
RR
500 }
501 break;
8b17ba72 502 case 1:
479cd5de 503 {
8b17ba72
RR
504 if (m_isDir) s = _("<DIR>");
505 else if (m_isLink) s = _("<LINK>");
506 else s = LongToString( m_size );
d3e90957
RR
507 }
508 break;
8b17ba72 509 case 2:
d3e90957 510 {
223d09f6 511 if (m_day < 10) s = wxT("0"); else s = wxT("");
8b17ba72 512 s += IntToString( m_day );
223d09f6
KB
513 s += wxT(".");
514 if (m_month < 10) s += wxT("0");
8b17ba72 515 s += IntToString( m_month );
223d09f6 516 s += wxT(".");
8b17ba72 517 s += IntToString( m_year );
d3e90957
RR
518 }
519 break;
8b17ba72 520 case 3:
d3e90957 521 {
223d09f6 522 if (m_hour < 10) s = wxT("0"); else s = wxT("");
8b17ba72 523 s += IntToString( m_hour );
223d09f6
KB
524 s += wxT(":");
525 if (m_minute < 10) s += wxT("0");
8b17ba72
RR
526 s += IntToString( m_minute );
527 break;
d3e90957 528 }
8b17ba72
RR
529 case 4:
530 s = m_permissions;
531 break;
532 default:
223d09f6 533 s = wxT("No entry");
8b17ba72
RR
534 break;
535 }
536 return s;
537}
538
539bool wxFileData::IsDir()
540{
541 return m_isDir;
542}
543
544bool wxFileData::IsExe()
545{
546 return m_isExe;
547}
548
549bool wxFileData::IsLink()
550{
551 return m_isLink;
552}
553
554long wxFileData::GetSize()
555{
556 return m_size;
557}
558
0b855868 559void wxFileData::SetNewName( const wxString &name, const wxString &fname )
8b17ba72 560{
0b855868
RR
561 m_name = name;
562 m_fileName = fname;
8b17ba72
RR
563}
564
565void wxFileData::MakeItem( wxListItem &item )
566{
567 item.m_text = m_name;
9b00bb16 568 item.ClearAttributes();
aaa37c0d
VZ
569 if (IsExe()) item.SetTextColour(*wxRED);
570 if (IsDir()) item.SetTextColour(*wxBLUE);
48fe8374 571
655cf310 572 if (IsDir())
b0c5c421 573 item.m_image = FI_FOLDER;
48fe8374 574 else if (IsExe())
b0c5c421 575 item.m_image = FI_EXECUTABLE;
655cf310 576 else if (m_name.Find(wxT('.')) != wxNOT_FOUND)
37fd1c97 577 item.m_image = g_IconsTable->GetIconID(m_name.AfterLast(wxT('.')));
655cf310 578 else
b0c5c421 579 item.m_image = FI_UNKNOWN;
655cf310 580
8b17ba72
RR
581 if (IsLink())
582 {
583 wxColour *dg = wxTheColourDatabase->FindColour( "MEDIUM GREY" );
aaa37c0d 584 item.SetTextColour(*dg);
8b17ba72
RR
585 }
586 item.m_data = (long)this;
587}
c8c0e54c 588
8b17ba72
RR
589//-----------------------------------------------------------------------------
590// wxFileCtrl
591//-----------------------------------------------------------------------------
592
d84afea9 593IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl)
8b17ba72
RR
594
595BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
0b855868
RR
596 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem)
597 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit)
8b17ba72
RR
598END_EVENT_TABLE()
599
655cf310 600
8b17ba72
RR
601wxFileCtrl::wxFileCtrl()
602{
75ec8bd4 603#if defined(__UNIX__)
223d09f6 604 m_dirName = wxT("/");
75ec8bd4 605#elif defined(__DOS__)
5e673a6a 606 m_dirName = wxT("");
75ec8bd4 607#endif
8b17ba72
RR
608 m_showHidden = FALSE;
609}
610
5e673a6a
VS
611wxFileCtrl::wxFileCtrl(wxWindow *win, wxWindowID id,
612 const wxString &dirName, const wxString &wild,
613 const wxPoint &pos, const wxSize &size,
7a82dabc 614 long style, const wxValidator &validator,
5e673a6a
VS
615 const wxString &name)
616 : wxListCtrl(win, id, pos, size, style, validator, name)
8b17ba72 617{
7a82dabc 618 if (! g_IconsTable)
5e673a6a 619 g_IconsTable = new wxFileIconsTable;
37fd1c97 620 wxImageList *imageList = g_IconsTable->GetImageList();
655cf310 621
0b855868 622 SetImageList( imageList, wxIMAGE_LIST_SMALL );
c8c0e54c 623
37fd1c97
VS
624 m_goToParentControl = m_newDirControl = NULL;
625
8b17ba72 626 m_dirName = dirName;
0b855868 627 m_wild = wild;
8b17ba72 628 m_showHidden = FALSE;
8f4fcc4e 629 UpdateFiles();
8b17ba72
RR
630}
631
632void wxFileCtrl::ChangeToListMode()
633{
634 SetSingleStyle( wxLC_LIST );
8f4fcc4e 635 UpdateFiles();
8b17ba72
RR
636}
637
638void wxFileCtrl::ChangeToReportMode()
639{
640 SetSingleStyle( wxLC_REPORT );
8f4fcc4e 641 UpdateFiles();
8b17ba72
RR
642}
643
644void wxFileCtrl::ChangeToIconMode()
645{
646 SetSingleStyle( wxLC_ICON );
8f4fcc4e 647 UpdateFiles();
8b17ba72
RR
648}
649
650void wxFileCtrl::ShowHidden( bool show )
651{
652 m_showHidden = show;
8f4fcc4e 653 UpdateFiles();
8b17ba72
RR
654}
655
0b855868
RR
656long wxFileCtrl::Add( wxFileData *fd, wxListItem &item )
657{
658 long ret = -1;
659 item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
660 fd->MakeItem( item );
661 long my_style = GetWindowStyleFlag();
662 if (my_style & wxLC_REPORT)
663 {
75ec8bd4
VS
664#ifdef __UNIX__
665 const int noEntries = 5;
666#else
667 const int noEntries = 4;
668#endif
0b855868 669 ret = InsertItem( item );
7a82dabc 670 for (int i = 1; i < noEntries; i++)
75ec8bd4 671 SetItem( item.m_itemId, i, fd->GetEntry( i) );
0b855868
RR
672 }
673 else if (my_style & wxLC_LIST)
674 {
675 ret = InsertItem( item );
c8c0e54c 676 }
0b855868
RR
677 return ret;
678}
679
8f4fcc4e 680void wxFileCtrl::UpdateFiles()
c8c0e54c 681{
37fd1c97 682 wxBusyCursor bcur; // this may take a while...
7a82dabc 683
8b17ba72 684 long my_style = GetWindowStyleFlag();
1317fd58
RR
685 int name_col_width = 0;
686 if (my_style & wxLC_REPORT)
687 {
688 if (GetColumnCount() > 0)
689 name_col_width = GetColumnWidth( 0 );
690 }
48fe8374 691
7a82dabc 692 FreeAllItemsData();
1317fd58 693 ClearAll();
7a82dabc 694
8b17ba72
RR
695 if (my_style & wxLC_REPORT)
696 {
1317fd58
RR
697 if (name_col_width < 140) name_col_width = 140;
698 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, name_col_width );
8b17ba72 699 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, 60 );
e6527f9d 700 InsertColumn( 2, _("Date"), wxLIST_FORMAT_LEFT, 65 );
8b17ba72 701 InsertColumn( 3, _("Time"), wxLIST_FORMAT_LEFT, 50 );
75ec8bd4 702#ifdef __UNIX__
8b17ba72 703 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, 120 );
75ec8bd4 704#endif
8b17ba72
RR
705 }
706 wxFileData *fd = (wxFileData *) NULL;
707 wxListItem item;
8b17ba72
RR
708 item.m_itemId = 0;
709 item.m_col = 0;
0b855868 710
5e673a6a 711#if defined(__DOS__) || defined(__WINDOWS__)
37fd1c97 712 if ( IsTopMostDir(m_dirName) )
7a82dabc 713 {
5e673a6a 714 // Pseudo-directory with all available drives listed...
37fd1c97
VS
715 for (int drive = 1; drive <= 26; drive++)
716 {
717 wxString path;
718 path.Printf(wxT("%c:\\"), (char)(drive + 'A' - 1));
719 if ( wxIsDriveAvailable(path) )
720 {
721 path.RemoveLast();
722 fd = new wxFileData(path, path);
723 Add(fd, item);
724 item.m_itemId++;
725 }
726 }
0b855868 727 }
5e673a6a 728 else
75ec8bd4 729#endif
8b17ba72 730 {
5e673a6a
VS
731 // Real directory...
732 if ( !IsTopMostDir(m_dirName) )
8b17ba72 733 {
5e673a6a
VS
734 wxString p(wxPathOnly(m_dirName));
735#ifdef __UNIX__
736 if (p.IsEmpty()) p = wxT("/");
737#endif
738 fd = new wxFileData( wxT(".."), p );
739 Add(fd, item);
8b17ba72
RR
740 item.m_itemId++;
741 }
c8c0e54c 742
5e673a6a
VS
743 wxString dirname(m_dirName);
744#if defined(__DOS__) || defined(__WINDOWS__)
745 if (dirname.length() == 2 && dirname[1u] == wxT(':'))
746 dirname << wxT('\\');
747#endif
748 wxDir dir(dirname);
749
750 if ( dir.IsOpened() )
cae5359f 751 {
5e673a6a
VS
752 wxString dirPrefix(dirname + wxFILE_SEP_PATH);
753 int hiddenFlag = m_showHidden ? wxDIR_HIDDEN : 0;
7a82dabc 754
5e673a6a
VS
755 bool cont;
756 wxString f;
757
758 // Get the directories first (not matched against wildcards):
759 cont = dir.GetFirst(&f, wxEmptyString, wxDIR_DIRS | hiddenFlag);
760 while (cont)
551adc4b 761 {
5e673a6a
VS
762 fd = new wxFileData(f, dirPrefix + f);
763 Add(fd, item);
551adc4b 764 item.m_itemId++;
5e673a6a
VS
765 cont = dir.GetNext(&f);
766 }
767
7a82dabc 768 // Tokenize the wildcard string, so we can handle more than 1
5e673a6a
VS
769 // search pattern in a wildcard.
770 wxStringTokenizer tokenWild(m_wild, wxT(";"));
771 while ( tokenWild.HasMoreTokens() )
772 {
7a82dabc 773 cont = dir.GetFirst(&f, tokenWild.GetNextToken(),
5e673a6a
VS
774 wxDIR_FILES | hiddenFlag);
775 while (cont)
776 {
777 fd = new wxFileData(f, dirPrefix + f);
778 Add(fd, item);
779 item.m_itemId++;
780 cont = dir.GetNext(&f);
781 }
551adc4b 782 }
cae5359f 783 }
cae5359f 784 }
c8c0e54c 785
5e673a6a 786 SortItems(ListCompare, 0);
479cd5de 787
5e673a6a 788 if ( my_style & wxLC_REPORT )
f6bcfd97 789 {
5e673a6a
VS
790 SetColumnWidth(1, wxLIST_AUTOSIZE);
791 SetColumnWidth(2, wxLIST_AUTOSIZE);
792 SetColumnWidth(3, wxLIST_AUTOSIZE);
f6bcfd97 793 }
7a82dabc 794
37fd1c97
VS
795 // Finally, enable/disable context-dependent controls:
796 if ( m_goToParentControl )
797 m_goToParentControl->Enable(!IsTopMostDir(m_dirName));
798#if defined(__DOS__) || defined(__WINDOWS__)
799 if ( m_newDirControl )
800 m_newDirControl->Enable(!IsTopMostDir(m_dirName));
801#endif
8b17ba72
RR
802}
803
0b855868 804void wxFileCtrl::SetWild( const wxString &wild )
8b17ba72 805{
0b855868 806 m_wild = wild;
8f4fcc4e 807 UpdateFiles();
0b855868
RR
808}
809
810void wxFileCtrl::MakeDir()
811{
5e673a6a 812 wxString new_name( _("NewName") );
0b855868 813 wxString path( m_dirName );
75ec8bd4 814 path += wxFILE_SEP_PATH;
0b855868
RR
815 path += new_name;
816 if (wxFileExists(path))
8b17ba72 817 {
0b855868
RR
818 // try NewName0, NewName1 etc.
819 int i = 0;
c8c0e54c 820 do {
0b855868 821 new_name = _("NewName");
c8c0e54c 822 wxString num;
223d09f6 823 num.Printf( wxT("%d"), i );
c8c0e54c
VZ
824 new_name += num;
825
0b855868 826 path = m_dirName;
75ec8bd4 827 path += wxFILE_SEP_PATH;
0b855868 828 path += new_name;
c8c0e54c
VZ
829 i++;
830 } while (wxFileExists(path));
8b17ba72 831 }
c8c0e54c 832
0b855868 833 wxLogNull log;
c8c0e54c 834 if (!wxMkdir(path))
8b17ba72 835 {
0b855868 836 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 837 dialog.ShowModal();
0b855868 838 return;
8b17ba72 839 }
8b17ba72 840
0b855868
RR
841 wxFileData *fd = new wxFileData( new_name, path );
842 wxListItem item;
843 item.m_itemId = 0;
844 item.m_col = 0;
13111b2a 845 long id = Add( fd, item );
c8c0e54c 846
0b855868
RR
847 if (id != -1)
848 {
849 SortItems( ListCompare, 0 );
c8c0e54c 850 id = FindItem( 0, (long)fd );
0b855868
RR
851 EnsureVisible( id );
852 EditLabel( id );
853 }
8b17ba72
RR
854}
855
856void wxFileCtrl::GoToParentDir()
857{
5e673a6a 858 if (!IsTopMostDir(m_dirName))
8b17ba72 859 {
353f41cb 860 size_t len = m_dirName.Len();
75ec8bd4 861 if (m_dirName[len-1] == wxFILE_SEP_PATH)
353f41cb 862 m_dirName.Remove( len-1, 1 );
c8c0e54c 863 wxString fname( wxFileNameFromPath(m_dirName) );
0b855868 864 m_dirName = wxPathOnly( m_dirName );
5e673a6a 865#ifdef __UNIX__
7a82dabc 866 if (m_dirName.IsEmpty())
5e673a6a
VS
867 m_dirName = wxT("/");
868#endif
8f4fcc4e 869 UpdateFiles();
13111b2a 870 long id = FindItem( 0, fname );
c8c0e54c
VZ
871 if (id != -1)
872 {
873 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
874 EnsureVisible( id );
875 }
8b17ba72
RR
876 }
877}
878
879void wxFileCtrl::GoToHomeDir()
880{
881 wxString s = wxGetUserHome( wxString() );
5e673a6a 882 GoToDir(s);
8b17ba72
RR
883}
884
885void wxFileCtrl::GoToDir( const wxString &dir )
886{
887 m_dirName = dir;
8f4fcc4e 888 UpdateFiles();
e6daf794
RR
889 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
890 EnsureVisible( 0 );
8b17ba72
RR
891}
892
893void wxFileCtrl::GetDir( wxString &dir )
894{
895 dir = m_dirName;
896}
897
7a82dabc 898void wxFileCtrl::FreeItemData(const wxListItem& item)
8b17ba72 899{
7a82dabc 900 wxFileData *fd = (wxFileData*)item.m_data;
8b17ba72
RR
901 delete fd;
902}
903
7a82dabc
VZ
904void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
905{
906 FreeItemData(event.m_item);
907}
908
909void wxFileCtrl::FreeAllItemsData()
12c1b46a
RR
910{
911 wxListItem item;
912 item.m_mask = wxLIST_MASK_DATA;
913
914 item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL );
92da8bde 915 while ( item.m_itemId != -1 )
12c1b46a
RR
916 {
917 GetItem( item );
7a82dabc 918 FreeItemData(item);
12c1b46a
RR
919 item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL );
920 }
921}
922
0b855868 923void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
8b17ba72
RR
924{
925 wxFileData *fd = (wxFileData*)event.m_item.m_data;
0b855868 926 wxASSERT( fd );
c8c0e54c 927
0b855868
RR
928 if ((event.GetLabel().IsEmpty()) ||
929 (event.GetLabel() == _(".")) ||
930 (event.GetLabel() == _("..")) ||
75ec8bd4 931 (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND))
8b17ba72 932 {
0b855868 933 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 934 dialog.ShowModal();
0b855868 935 event.Veto();
c8c0e54c 936 return;
8b17ba72 937 }
c8c0e54c 938
0b855868 939 wxString new_name( wxPathOnly( fd->GetFullName() ) );
75ec8bd4 940 new_name += wxFILE_SEP_PATH;
0b855868 941 new_name += event.GetLabel();
c8c0e54c 942
0b855868 943 wxLogNull log;
c8c0e54c 944
0b855868 945 if (wxFileExists(new_name))
8b17ba72 946 {
0b855868 947 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 948 dialog.ShowModal();
0b855868 949 event.Veto();
8b17ba72 950 }
c8c0e54c 951
0b855868 952 if (wxRenameFile(fd->GetFullName(),new_name))
8b17ba72 953 {
0b855868 954 fd->SetNewName( new_name, event.GetLabel() );
c8c0e54c 955 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
0b855868 956 EnsureVisible( event.GetItem() );
8b17ba72
RR
957 }
958 else
959 {
0b855868 960 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 961 dialog.ShowModal();
0b855868 962 event.Veto();
8b17ba72 963 }
8b17ba72
RR
964}
965
7a82dabc
VZ
966wxFileCtrl::~wxFileCtrl()
967{
968 FreeAllItemsData();
969}
970
8b17ba72
RR
971//-----------------------------------------------------------------------------
972// wxFileDialog
973//-----------------------------------------------------------------------------
974
5e673a6a
VS
975#define ID_LIST_MODE (wxID_FILEDLGG )
976#define ID_REPORT_MODE (wxID_FILEDLGG + 1)
977#define ID_UP_DIR (wxID_FILEDLGG + 5)
978#define ID_PARENT_DIR (wxID_FILEDLGG + 6)
979#define ID_NEW_DIR (wxID_FILEDLGG + 7)
980#define ID_CHOICE (wxID_FILEDLGG + 8)
981#define ID_TEXT (wxID_FILEDLGG + 9)
982#define ID_LIST_CTRL (wxID_FILEDLGG + 10)
983#define ID_ACTIVATED (wxID_FILEDLGG + 11)
984#define ID_CHECK (wxID_FILEDLGG + 12)
8b17ba72
RR
985
986IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
987
988BEGIN_EVENT_TABLE(wxFileDialog,wxDialog)
989 EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList)
990 EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport)
8b17ba72
RR
991 EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp)
992 EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome)
0b855868 993 EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew)
8b17ba72
RR
994 EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk)
995 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected)
c8c0e54c
VZ
996 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated)
997 EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice)
998 EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter)
bf9e3e73 999 EVT_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck)
8b17ba72
RR
1000END_EVENT_TABLE()
1001
eaf40b23
VS
1002long wxFileDialog::s_lastViewStyle = wxLC_LIST;
1003bool wxFileDialog::s_lastShowHidden = FALSE;
655cf310 1004
8b17ba72
RR
1005wxFileDialog::wxFileDialog(wxWindow *parent,
1006 const wxString& message,
1007 const wxString& defaultDir,
1008 const wxString& defaultFile,
1009 const wxString& wildCard,
1010 long style,
1011 const wxPoint& pos ) :
9c884972 1012 wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
8b17ba72 1013{
37fd1c97 1014 wxBusyCursor bcur;
c8c0e54c 1015
eaf40b23
VS
1016 if (wxConfig::Get(FALSE))
1017 {
7a82dabc 1018 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
5e673a6a 1019 &s_lastViewStyle);
7a82dabc 1020 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
5e673a6a 1021 &s_lastShowHidden);
eaf40b23
VS
1022 }
1023
8b17ba72
RR
1024 m_message = message;
1025 m_dialogStyle = style;
479cd5de 1026
655cf310 1027 if (m_dialogStyle == 0) m_dialogStyle = wxOPEN;
7941ba11 1028 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
9b00bb16 1029 m_dialogStyle |= wxOPEN;
479cd5de 1030
8b17ba72 1031 m_dir = defaultDir;
353f41cb 1032 if ((m_dir.empty()) || (m_dir == wxT(".")))
ac2def68 1033 {
75ec8bd4 1034 m_dir = wxGetCwd();
ac2def68 1035 }
7a82dabc 1036
353f41cb 1037 size_t len = m_dir.Len();
75ec8bd4 1038 if ((len > 1) && (m_dir[len-1] == wxFILE_SEP_PATH))
353f41cb
RR
1039 m_dir.Remove( len-1, 1 );
1040
1041 m_path = m_dir;
75ec8bd4 1042 m_path += wxFILE_SEP_PATH;
8b17ba72
RR
1043 m_path += defaultFile;
1044 m_fileName = defaultFile;
1045 m_wildCard = wildCard;
1046 m_filterIndex = 0;
655cf310 1047 m_filterExtension = wxEmptyString;
c8c0e54c 1048
cae5359f 1049 // interpret wildcards
c8c0e54c 1050
cae5359f
RR
1051 if (m_wildCard.IsEmpty())
1052 m_wildCard = _("All files (*)|*");
c8c0e54c 1053
223d09f6 1054 wxStringTokenizer tokens( m_wildCard, wxT("|") );
cae5359f
RR
1055 wxString firstWild;
1056 wxString firstWildText;
1057 if (tokens.CountTokens() == 1)
1058 {
1059 firstWildText = tokens.GetNextToken();
1060 firstWild = firstWildText;
1061 }
1062 else
1063 {
223d09f6 1064 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
cae5359f
RR
1065 firstWildText = tokens.GetNextToken();
1066 firstWild = tokens.GetNextToken();
1067 }
655cf310
VS
1068 if ( firstWild.Left( 2 ) == wxT("*.") )
1069 m_filterExtension = firstWild.Mid( 1 );
1070 if ( m_filterExtension == ".*" ) m_filterExtension = wxEmptyString;
cae5359f
RR
1071
1072 // layout
c8c0e54c 1073
8b17ba72 1074 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 1075
8b17ba72 1076 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 1077
e6daf794 1078 wxBitmapButton *but;
c8c0e54c 1079
7a82dabc 1080 but = new wxBitmapButton(this, ID_LIST_MODE,
60d2cc25 1081 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_CMN_DIALOG));
e6daf794
RR
1082#if wxUSE_TOOLTIPS
1083 but->SetToolTip( _("View files as a list view") );
1084#endif
1085 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 1086
60d2cc25
VS
1087 but = new wxBitmapButton(this, ID_REPORT_MODE,
1088 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_CMN_DIALOG));
e6daf794
RR
1089#if wxUSE_TOOLTIPS
1090 but->SetToolTip( _("View files as a detailed view") );
1091#endif
1092 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 1093
0b855868 1094 buttonsizer->Add( 30, 5, 1 );
e6daf794 1095
7a82dabc 1096 wxWindow *butDirUp =
60d2cc25
VS
1097 new wxBitmapButton(this, ID_UP_DIR,
1098 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_CMN_DIALOG));
e6daf794 1099#if wxUSE_TOOLTIPS
37fd1c97 1100 butDirUp->SetToolTip( _("Go to parent directory") );
e6daf794 1101#endif
37fd1c97 1102 buttonsizer->Add( butDirUp, 0, wxALL, 5 );
c8c0e54c 1103
37fd1c97 1104#ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
60d2cc25
VS
1105 but = new wxBitmapButton(this, ID_PARENT_DIR,
1106 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_CMN_DIALOG));
e6daf794
RR
1107#if wxUSE_TOOLTIPS
1108 but->SetToolTip( _("Go to home directory") );
1109#endif
1110 buttonsizer->Add( but, 0, wxALL, 5);
c8c0e54c 1111
e6daf794 1112 buttonsizer->Add( 20, 20 );
75ec8bd4 1113#endif //!__DOS__
c8c0e54c 1114
7a82dabc 1115 wxWindow *butNewDir =
60d2cc25
VS
1116 new wxBitmapButton(this, ID_NEW_DIR,
1117 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_CMN_DIALOG));
e6daf794 1118#if wxUSE_TOOLTIPS
37fd1c97 1119 butNewDir->SetToolTip( _("Create new directory") );
e6daf794 1120#endif
37fd1c97 1121 buttonsizer->Add( butNewDir, 0, wxALL, 5 );
c8c0e54c 1122
41fecb44 1123 if (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA)
c15521c6
RR
1124 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
1125 else
1126 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
c8c0e54c 1127
9c884972 1128 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
41fecb44 1129 if (wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA)
c15521c6 1130 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
9c884972
RR
1131 m_static = new wxStaticText( this, -1, m_dir );
1132 staticsizer->Add( m_static, 1 );
1133 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
1134
7941ba11 1135 if (m_dialogStyle & wxMULTIPLE)
479cd5de 1136 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition,
01ec8f96 1137 wxSize(540,200), s_lastViewStyle | wxSUNKEN_BORDER );
7941ba11 1138 else
479cd5de 1139 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition,
01ec8f96 1140 wxSize(540,200), s_lastViewStyle | wxSUNKEN_BORDER | wxLC_SINGLE_SEL );
37fd1c97
VS
1141 m_list->ShowHidden(s_lastShowHidden);
1142 m_list->SetNewDirControl(butNewDir);
1143 m_list->SetGoToParentControl(butDirUp);
24f932d2 1144
41fecb44 1145 if (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA)
c15521c6 1146 {
41fecb44
VS
1147 // PDAs have a different screen layout
1148 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
1149
1150 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1151 m_choice = new wxChoice( this, ID_CHOICE );
1152 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 5 );
1153 mainsizer->Add( choicesizer, 0, wxEXPAND );
1154
1155 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1156 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1157 textsizer->Add( m_text, 1, wxCENTER | wxALL, 5 );
1158 mainsizer->Add( textsizer, 0, wxEXPAND );
1159
1160 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
1161 m_check->SetValue( s_lastShowHidden );
1162 textsizer->Add( m_check, 0, wxCENTER|wxALL, 5 );
1163
1164 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
1165 buttonsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxALL, 5 );
1166 buttonsizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 5 );
1167 mainsizer->Add( buttonsizer, 0, wxALIGN_RIGHT );
c15521c6
RR
1168 }
1169 else
1170 {
41fecb44
VS
1171 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
1172
1173 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1174 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1175 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1176 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1177 mainsizer->Add( textsizer, 0, wxEXPAND );
1178
1179 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1180 m_choice = new wxChoice( this, ID_CHOICE );
1181 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
1182 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
1183 m_check->SetValue( s_lastShowHidden );
1184 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
1185 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
1186 mainsizer->Add( choicesizer, 0, wxEXPAND );
c15521c6 1187 }
24f932d2 1188
cae5359f
RR
1189 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
1190 while (tokens.HasMoreTokens())
1191 {
1192 firstWildText = tokens.GetNextToken();
1193 firstWild = tokens.GetNextToken();
1194 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
1195 }
1196 m_choice->SetSelection( 0 );
8b17ba72
RR
1197
1198 SetAutoLayout( TRUE );
1199 SetSizer( mainsizer );
c8c0e54c 1200
8b17ba72
RR
1201 mainsizer->Fit( this );
1202 mainsizer->SetSizeHints( this );
48fe8374 1203
8b17ba72 1204 Centre( wxBOTH );
ed58dbea 1205
b6fa52db 1206/*
ed58dbea
RR
1207 if (m_fileName.IsEmpty())
1208 m_list->SetFocus();
1209 else
b6fa52db 1210*/
ed58dbea 1211 m_text->SetFocus();
8b17ba72
RR
1212}
1213
cae5359f
RR
1214wxFileDialog::~wxFileDialog()
1215{
eaf40b23
VS
1216 if (wxConfig::Get(FALSE))
1217 {
5e673a6a
VS
1218 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1219 s_lastViewStyle);
1220 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1221 s_lastShowHidden);
eaf40b23 1222 }
cae5359f
RR
1223}
1224
04ea7f93
RR
1225void wxFileDialog::SetFilterIndex( int filterindex )
1226{
1227 m_choice->SetSelection( filterindex );
1228 wxCommandEvent event;
1229 event.SetInt( filterindex );
1230 OnChoice( event );
1231}
1232
cae5359f
RR
1233void wxFileDialog::OnChoice( wxCommandEvent &event )
1234{
13111b2a
VZ
1235 int index = (int)event.GetInt();
1236 wxString *str = (wxString*) m_choice->GetClientData( index );
cae5359f 1237 m_list->SetWild( *str );
13111b2a 1238 m_filterIndex = index;
655cf310
VS
1239 if ( str -> Left( 2 ) == wxT("*.") )
1240 {
1241 m_filterExtension = str -> Mid( 1 );
1242 if (m_filterExtension == ".*") m_filterExtension = wxEmptyString;
1243 }
1244 else
1245 m_filterExtension = wxEmptyString;
cae5359f
RR
1246}
1247
bf9e3e73
RR
1248void wxFileDialog::OnCheck( wxCommandEvent &event )
1249{
eaf40b23 1250 m_list->ShowHidden( (s_lastShowHidden = event.GetInt() != 0) );
bf9e3e73
RR
1251}
1252
e1811a01 1253void wxFileDialog::OnActivated( wxListEvent &event )
8b17ba72 1254{
e1811a01 1255 HandleAction( event.m_item.m_text );
8b17ba72
RR
1256}
1257
cae5359f
RR
1258void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
1259{
1260 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
1261 cevent.SetEventObject( this );
1262 GetEventHandler()->ProcessEvent( cevent );
1263}
1264
8b17ba72
RR
1265void wxFileDialog::OnSelected( wxListEvent &event )
1266{
e1811a01 1267 wxString filename( event.m_item.m_text );
223d09f6 1268 if (filename == wxT("..")) return;
479cd5de 1269
e1811a01
RR
1270 wxString dir;
1271 m_list->GetDir( dir );
5e673a6a
VS
1272 if (!IsTopMostDir(dir))
1273 dir += wxFILE_SEP_PATH;
e1811a01
RR
1274 dir += filename;
1275 if (wxDirExists(dir)) return;
479cd5de 1276
e1811a01 1277 m_text->SetValue( filename );
8b17ba72
RR
1278}
1279
e1811a01 1280void wxFileDialog::HandleAction( const wxString &fn )
8b17ba72 1281{
e1811a01 1282 wxString filename( fn );
8b17ba72
RR
1283 wxString dir;
1284 m_list->GetDir( dir );
1285 if (filename.IsEmpty()) return;
223d09f6 1286 if (filename == wxT(".")) return;
c8c0e54c 1287
223d09f6 1288 if (filename == wxT(".."))
0b855868
RR
1289 {
1290 m_list->GoToParentDir();
1291 m_list->SetFocus();
9c884972
RR
1292 m_list->GetDir( dir );
1293 m_static->SetLabel( dir );
c8c0e54c 1294 return;
0b855868
RR
1295 }
1296
75ec8bd4 1297#ifdef __UNIX__
223d09f6 1298 if (filename == wxT("~"))
ed58dbea
RR
1299 {
1300 m_list->GoToHomeDir();
1301 m_list->SetFocus();
1302 m_list->GetDir( dir );
1303 m_static->SetLabel( dir );
c8c0e54c 1304 return;
ed58dbea 1305 }
c8c0e54c 1306
f6bcfd97 1307 if (filename[0u] == wxT('~'))
ed58dbea
RR
1308 {
1309 filename.Remove( 0, 1 );
c8c0e54c 1310 wxString tmp( wxGetUserHome() );
223d09f6 1311 tmp += wxT('/');
c8c0e54c
VZ
1312 tmp += filename;
1313 filename = tmp;
ed58dbea 1314 }
75ec8bd4 1315#endif // __UNIX__
ed58dbea 1316
223d09f6
KB
1317 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
1318 (filename.Find(wxT('?')) != wxNOT_FOUND))
cae5359f 1319 {
75ec8bd4 1320 if (filename.Find(wxFILE_SEP_PATH) != wxNOT_FOUND)
c8c0e54c
VZ
1321 {
1322 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
1323 return;
1324 }
1325 m_list->SetWild( filename );
1326 return;
cae5359f
RR
1327 }
1328
5e673a6a
VS
1329 if (!IsTopMostDir(dir))
1330 dir += wxFILE_SEP_PATH;
1331 if (!wxIsAbsolutePath(filename))
ed58dbea
RR
1332 {
1333 dir += filename;
1334 filename = dir;
1335 }
c8c0e54c 1336
8b17ba72
RR
1337 if (wxDirExists(filename))
1338 {
1339 m_list->GoToDir( filename );
9c884972
RR
1340 m_list->GetDir( dir );
1341 m_static->SetLabel( dir );
c8c0e54c 1342 return;
8b17ba72 1343 }
c8c0e54c 1344
655cf310 1345
8b17ba72
RR
1346 if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) )
1347 {
655cf310 1348 if (filename.Find( wxT('.') ) == wxNOT_FOUND ||
75ec8bd4 1349 filename.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH ) != wxNOT_FOUND)
655cf310 1350 filename << m_filterExtension;
8b17ba72
RR
1351 if (wxFileExists( filename ))
1352 {
1353 wxString msg;
1354 msg.Printf( _("File '%s' already exists, do you really want to "
1355 "overwrite it?"), filename.c_str() );
1356
1357 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1358 return;
1359 }
1360 }
655cf310 1361 else if ( m_dialogStyle & wxOPEN )
8b17ba72
RR
1362 {
1363 if ( !wxFileExists( filename ) )
655cf310 1364 if (filename.Find( wxT('.') ) == wxNOT_FOUND ||
75ec8bd4 1365 filename.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH ) != wxNOT_FOUND)
655cf310
VS
1366 filename << m_filterExtension;
1367
1368 if ( m_dialogStyle & wxFILE_MUST_EXIST )
8b17ba72 1369 {
655cf310
VS
1370 if ( !wxFileExists( filename ) )
1371 {
1372 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR );
1373 return;
1374 }
8b17ba72
RR
1375 }
1376 }
1377
1378 SetPath( filename );
479cd5de 1379
3f6638b8 1380 // change to the directory where the user went if asked
fda09b3f 1381 if ( m_dialogStyle & wxCHANGE_DIR )
3f6638b8
VZ
1382 {
1383 wxString cwd;
1384 wxSplitPath(filename, &cwd, NULL, NULL);
1385
1386 if ( cwd != wxGetWorkingDirectory() )
1387 {
1388 wxSetWorkingDirectory(cwd);
1389 }
1390 }
1391
9b7e522a
RR
1392 wxCommandEvent event;
1393 wxDialog::OnOK(event);
e1811a01
RR
1394}
1395
5e0201ea 1396void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
e1811a01
RR
1397{
1398 HandleAction( m_text->GetValue() );
8b17ba72
RR
1399}
1400
1401void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
1402{
1403 m_list->ChangeToListMode();
eaf40b23 1404 s_lastViewStyle = wxLC_LIST;
0b855868 1405 m_list->SetFocus();
8b17ba72
RR
1406}
1407
1408void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
1409{
1410 m_list->ChangeToReportMode();
eaf40b23 1411 s_lastViewStyle = wxLC_REPORT;
0b855868 1412 m_list->SetFocus();
8b17ba72
RR
1413}
1414
1415void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
1416{
1417 m_list->GoToParentDir();
0b855868 1418 m_list->SetFocus();
9c884972
RR
1419 wxString dir;
1420 m_list->GetDir( dir );
1421 m_static->SetLabel( dir );
8b17ba72
RR
1422}
1423
1424void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
1425{
1426 m_list->GoToHomeDir();
0b855868 1427 m_list->SetFocus();
9c884972
RR
1428 wxString dir;
1429 m_list->GetDir( dir );
1430 m_static->SetLabel( dir );
7a82dabc 1431
d7fa7eaa 1432 m_text->SetFocus();
0b855868
RR
1433}
1434
1435void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
1436{
1437 m_list->MakeDir();
8b17ba72
RR
1438}
1439
1440void wxFileDialog::SetPath( const wxString& path )
1441{
1442 // not only set the full path but also update filename and dir
1443 m_path = path;
1444 if ( !!path )
1445 {
1446 wxString ext;
1447 wxSplitPath(path, &m_dir, &m_fileName, &ext);
c8c0e54c
VZ
1448 if (!ext.IsEmpty())
1449 {
223d09f6 1450 m_fileName += wxT(".");
8b17ba72 1451 m_fileName += ext;
c8c0e54c 1452 }
8b17ba72
RR
1453 }
1454}
c8c0e54c 1455
7941ba11
RR
1456void wxFileDialog::GetPaths( wxArrayString& paths ) const
1457{
1458 paths.Empty();
28c9c76e
RR
1459 if (m_list->GetSelectedItemCount() == 0)
1460 {
1461 paths.Add( GetPath() );
1462 return;
1463 }
479cd5de 1464
7941ba11
RR
1465 paths.Alloc( m_list->GetSelectedItemCount() );
1466
1467 wxString dir;
1468 m_list->GetDir( dir );
5e673a6a 1469#ifdef __UNIX__
7a82dabc 1470 if (dir != wxT("/"))
5e673a6a
VS
1471#endif
1472 dir += wxFILE_SEP_PATH;
7941ba11
RR
1473
1474 wxListItem item;
1475 item.m_mask = wxLIST_MASK_TEXT;
1476
1477 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
92da8bde 1478 while ( item.m_itemId != -1 )
68df5777 1479 {
7941ba11
RR
1480 m_list->GetItem( item );
1481 paths.Add( dir + item.m_text );
68df5777 1482 item.m_itemId = m_list->GetNextItem( item.m_itemId,
7941ba11
RR
1483 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1484 }
1485}
1486
1487void wxFileDialog::GetFilenames(wxArrayString& files) const
1488{
1489 files.Empty();
28c9c76e
RR
1490 if (m_list->GetSelectedItemCount() == 0)
1491 {
1492 files.Add( GetFilename() );
1493 return;
1494 }
7941ba11
RR
1495 files.Alloc( m_list->GetSelectedItemCount() );
1496
1497 wxListItem item;
1498 item.m_mask = wxLIST_MASK_TEXT;
1499
1500 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
92da8bde 1501 while ( item.m_itemId != -1 )
68df5777 1502 {
7941ba11
RR
1503 m_list->GetItem( item );
1504 files.Add( item.m_text );
68df5777 1505 item.m_itemId = m_list->GetNextItem( item.m_itemId,
7941ba11
RR
1506 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1507 }
1508}
1509
655cf310
VS
1510
1511
8b17ba72
RR
1512// ----------------------------------------------------------------------------
1513// global functions
1514// ----------------------------------------------------------------------------
1515
1516wxString
1517wxFileSelectorEx(const wxChar *message,
1518 const wxChar *default_path,
1519 const wxChar *default_filename,
5e0201ea 1520 int *WXUNUSED(indexDefaultExtension),
8b17ba72
RR
1521 const wxChar *wildcard,
1522 int flags,
1523 wxWindow *parent,
1524 int x, int y)
1525{
1526 // TODO: implement this somehow
223d09f6 1527 return wxFileSelector(message, default_path, default_filename, wxT(""),
8b17ba72
RR
1528 wildcard, flags, parent, x, y);
1529}
1530
1531wxString wxFileSelector( const wxChar *title,
1532 const wxChar *defaultDir, const wxChar *defaultFileName,
1533 const wxChar *defaultExtension, const wxChar *filter, int flags,
1534 wxWindow *parent, int x, int y )
1535{
1536 wxString filter2;
1537 if ( defaultExtension && !filter )
223d09f6 1538 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
8b17ba72
RR
1539 else if ( filter )
1540 filter2 = filter;
1541
1542 wxString defaultDirString;
1543 if (defaultDir)
1544 defaultDirString = defaultDir;
1545
1546 wxString defaultFilenameString;
1547 if (defaultFileName)
1548 defaultFilenameString = defaultFileName;
1549
1550 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
1551
1552 if ( fileDialog.ShowModal() == wxID_OK )
1553 {
1554 return fileDialog.GetPath();
1555 }
1556 else
1557 {
1558 return wxEmptyString;
1559 }
1560}
1561
2645b45a 1562static wxString GetWildcardString(const wxChar *ext)
8b17ba72 1563{
2645b45a
VZ
1564 wxString wild;
1565 if ( ext )
1566 {
1567 if ( *ext == wxT('.') )
1568 ext++;
8b17ba72 1569
2645b45a
VZ
1570 wild << _T("*.") << ext;
1571 }
1572 else // no extension specified
1573 {
1574 wild = wxFileSelectorDefaultWildcardStr;
1575 }
8b17ba72 1576
2645b45a 1577 return wild;
8b17ba72
RR
1578}
1579
2645b45a
VZ
1580wxString wxLoadFileSelector(const wxChar *what,
1581 const wxChar *ext,
1582 const wxChar *nameDef,
1583 wxWindow *parent)
8b17ba72 1584{
2645b45a
VZ
1585 wxString prompt;
1586 if ( what && *what )
1587 prompt = wxString::Format(_("Load %s file"), what);
1588 else
1589 prompt = _("Load file");
8b17ba72 1590
2645b45a
VZ
1591 return wxFileSelector(prompt, NULL, nameDef, ext,
1592 GetWildcardString(ext), 0, parent);
8b17ba72
RR
1593}
1594
2645b45a
VZ
1595wxString wxSaveFileSelector(const wxChar *what,
1596 const wxChar *ext,
1597 const wxChar *nameDef,
1598 wxWindow *parent)
1599{
1600 wxString prompt;
1601 if ( what && *what )
1602 prompt = wxString::Format(_("Save %s file"), what);
1603 else
1604 prompt = _("Save file");
655cf310 1605
2645b45a
VZ
1606 return wxFileSelector(prompt, NULL, nameDef, ext,
1607 GetWildcardString(ext), 0, parent);
1608}
655cf310
VS
1609
1610// A module to allow icons table cleanup
1611
1612class wxFileDialogGenericModule: public wxModule
1613{
1614DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule)
1615public:
1616 wxFileDialogGenericModule() {}
1617 bool OnInit() { return TRUE; }
1618 void OnExit() { if (g_IconsTable) {delete g_IconsTable; g_IconsTable = NULL;} }
1619};
1620
1621IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule, wxModule)
1e6feb95
VZ
1622
1623#endif // wxUSE_FILEDLG