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