]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
Bunch of GTK theme changes.
[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
8b17ba72 412 struct stat buff;
37fd1c97 413 wxStat( m_fileName.fn_str(), &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
6eec2bee
OK
440 m_permissions.sprintf( wxT("%c%c%c"),
441 ((( buff.st_mode & S_IRUSR ) == S_IRUSR ) ? wxT('r') : wxT('-')),
442 ((( buff.st_mode & S_IWUSR ) == S_IWUSR ) ? wxT('w') : wxT('-')),
443 ((( buff.st_mode & S_IXUSR ) == S_IXUSR ) ? wxT('x') : wxT('-')) );
8b17ba72
RR
444}
445
446wxString wxFileData::GetName() const
447{
448 return m_name;
449}
450
451wxString wxFileData::GetFullName() const
452{
453 return m_fileName;
454}
455
456wxString wxFileData::GetHint() const
457{
458 wxString s = m_fileName;
459 s += " ";
460 if (m_isDir) s += _("<DIR> ");
461 else if (m_isLink) s += _("<LINK> ");
462 else
463 {
464 s += LongToString( m_size );
465 s += _(" bytes ");
466 }
467 s += IntToString( m_day );
223d09f6 468 s += wxT(".");
8b17ba72 469 s += IntToString( m_month );
223d09f6 470 s += wxT(".");
8b17ba72 471 s += IntToString( m_year );
223d09f6 472 s += wxT(" ");
8b17ba72 473 s += IntToString( m_hour );
223d09f6 474 s += wxT(":");
8b17ba72 475 s += IntToString( m_minute );
223d09f6 476 s += wxT(" ");
8b17ba72
RR
477 s += m_permissions;
478 return s;
479};
480
c8c0e54c 481wxString wxFileData::GetEntry( int num )
8b17ba72
RR
482{
483 wxString s;
484 switch (num)
485 {
486 case 0:
d3e90957 487 {
8b17ba72 488 s = m_name;
d3e90957
RR
489 }
490 break;
8b17ba72 491 case 1:
479cd5de 492 {
8b17ba72
RR
493 if (m_isDir) s = _("<DIR>");
494 else if (m_isLink) s = _("<LINK>");
495 else s = LongToString( m_size );
d3e90957
RR
496 }
497 break;
8b17ba72 498 case 2:
d3e90957 499 {
223d09f6 500 if (m_day < 10) s = wxT("0"); else s = wxT("");
8b17ba72 501 s += IntToString( m_day );
223d09f6
KB
502 s += wxT(".");
503 if (m_month < 10) s += wxT("0");
8b17ba72 504 s += IntToString( m_month );
223d09f6 505 s += wxT(".");
8b17ba72 506 s += IntToString( m_year );
d3e90957
RR
507 }
508 break;
8b17ba72 509 case 3:
d3e90957 510 {
223d09f6 511 if (m_hour < 10) s = wxT("0"); else s = wxT("");
8b17ba72 512 s += IntToString( m_hour );
223d09f6
KB
513 s += wxT(":");
514 if (m_minute < 10) s += wxT("0");
8b17ba72
RR
515 s += IntToString( m_minute );
516 break;
d3e90957 517 }
8b17ba72
RR
518 case 4:
519 s = m_permissions;
520 break;
521 default:
223d09f6 522 s = wxT("No entry");
8b17ba72
RR
523 break;
524 }
525 return s;
526}
527
528bool wxFileData::IsDir()
529{
530 return m_isDir;
531}
532
533bool wxFileData::IsExe()
534{
535 return m_isExe;
536}
537
538bool wxFileData::IsLink()
539{
540 return m_isLink;
541}
542
543long wxFileData::GetSize()
544{
545 return m_size;
546}
547
0b855868 548void wxFileData::SetNewName( const wxString &name, const wxString &fname )
8b17ba72 549{
0b855868
RR
550 m_name = name;
551 m_fileName = fname;
8b17ba72
RR
552}
553
554void wxFileData::MakeItem( wxListItem &item )
555{
556 item.m_text = m_name;
9b00bb16 557 item.ClearAttributes();
aaa37c0d
VZ
558 if (IsExe()) item.SetTextColour(*wxRED);
559 if (IsDir()) item.SetTextColour(*wxBLUE);
48fe8374 560
655cf310 561 if (IsDir())
b0c5c421 562 item.m_image = FI_FOLDER;
48fe8374 563 else if (IsExe())
b0c5c421 564 item.m_image = FI_EXECUTABLE;
655cf310 565 else if (m_name.Find(wxT('.')) != wxNOT_FOUND)
37fd1c97 566 item.m_image = g_IconsTable->GetIconID(m_name.AfterLast(wxT('.')));
655cf310 567 else
b0c5c421 568 item.m_image = FI_UNKNOWN;
655cf310 569
8b17ba72
RR
570 if (IsLink())
571 {
572 wxColour *dg = wxTheColourDatabase->FindColour( "MEDIUM GREY" );
aaa37c0d 573 item.SetTextColour(*dg);
8b17ba72
RR
574 }
575 item.m_data = (long)this;
576}
c8c0e54c 577
8b17ba72
RR
578//-----------------------------------------------------------------------------
579// wxFileCtrl
580//-----------------------------------------------------------------------------
581
582IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl);
583
584BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
0b855868
RR
585 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem)
586 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit)
8b17ba72
RR
587END_EVENT_TABLE()
588
655cf310 589
8b17ba72
RR
590wxFileCtrl::wxFileCtrl()
591{
75ec8bd4 592#if defined(__UNIX__)
223d09f6 593 m_dirName = wxT("/");
75ec8bd4 594#elif defined(__DOS__)
5e673a6a 595 m_dirName = wxT("");
75ec8bd4 596#endif
8b17ba72
RR
597 m_showHidden = FALSE;
598}
599
5e673a6a
VS
600wxFileCtrl::wxFileCtrl(wxWindow *win, wxWindowID id,
601 const wxString &dirName, const wxString &wild,
602 const wxPoint &pos, const wxSize &size,
7a82dabc 603 long style, const wxValidator &validator,
5e673a6a
VS
604 const wxString &name)
605 : wxListCtrl(win, id, pos, size, style, validator, name)
8b17ba72 606{
7a82dabc 607 if (! g_IconsTable)
5e673a6a 608 g_IconsTable = new wxFileIconsTable;
37fd1c97 609 wxImageList *imageList = g_IconsTable->GetImageList();
655cf310 610
0b855868 611 SetImageList( imageList, wxIMAGE_LIST_SMALL );
c8c0e54c 612
37fd1c97
VS
613 m_goToParentControl = m_newDirControl = NULL;
614
8b17ba72 615 m_dirName = dirName;
0b855868 616 m_wild = wild;
8b17ba72 617 m_showHidden = FALSE;
8f4fcc4e 618 UpdateFiles();
8b17ba72
RR
619}
620
621void wxFileCtrl::ChangeToListMode()
622{
623 SetSingleStyle( wxLC_LIST );
8f4fcc4e 624 UpdateFiles();
8b17ba72
RR
625}
626
627void wxFileCtrl::ChangeToReportMode()
628{
629 SetSingleStyle( wxLC_REPORT );
8f4fcc4e 630 UpdateFiles();
8b17ba72
RR
631}
632
633void wxFileCtrl::ChangeToIconMode()
634{
635 SetSingleStyle( wxLC_ICON );
8f4fcc4e 636 UpdateFiles();
8b17ba72
RR
637}
638
639void wxFileCtrl::ShowHidden( bool show )
640{
641 m_showHidden = show;
8f4fcc4e 642 UpdateFiles();
8b17ba72
RR
643}
644
0b855868
RR
645long wxFileCtrl::Add( wxFileData *fd, wxListItem &item )
646{
647 long ret = -1;
648 item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
649 fd->MakeItem( item );
650 long my_style = GetWindowStyleFlag();
651 if (my_style & wxLC_REPORT)
652 {
75ec8bd4
VS
653#ifdef __UNIX__
654 const int noEntries = 5;
655#else
656 const int noEntries = 4;
657#endif
0b855868 658 ret = InsertItem( item );
7a82dabc 659 for (int i = 1; i < noEntries; i++)
75ec8bd4 660 SetItem( item.m_itemId, i, fd->GetEntry( i) );
0b855868
RR
661 }
662 else if (my_style & wxLC_LIST)
663 {
664 ret = InsertItem( item );
c8c0e54c 665 }
0b855868
RR
666 return ret;
667}
668
8f4fcc4e 669void wxFileCtrl::UpdateFiles()
c8c0e54c 670{
37fd1c97 671 wxBusyCursor bcur; // this may take a while...
7a82dabc 672
8b17ba72 673 long my_style = GetWindowStyleFlag();
1317fd58
RR
674 int name_col_width = 0;
675 if (my_style & wxLC_REPORT)
676 {
677 if (GetColumnCount() > 0)
678 name_col_width = GetColumnWidth( 0 );
679 }
48fe8374 680
7a82dabc 681 FreeAllItemsData();
1317fd58 682 ClearAll();
7a82dabc 683
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) )
7a82dabc 702 {
5e673a6a 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;
7a82dabc 743
5e673a6a
VS
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
7a82dabc 757 // Tokenize the wildcard string, so we can handle more than 1
5e673a6a
VS
758 // search pattern in a wildcard.
759 wxStringTokenizer tokenWild(m_wild, wxT(";"));
760 while ( tokenWild.HasMoreTokens() )
761 {
7a82dabc 762 cont = dir.GetFirst(&f, tokenWild.GetNextToken(),
5e673a6a
VS
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 }
7a82dabc 783
37fd1c97
VS
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 854#ifdef __UNIX__
7a82dabc 855 if (m_dirName.IsEmpty())
5e673a6a
VS
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
7a82dabc 887void wxFileCtrl::FreeItemData(const wxListItem& item)
8b17ba72 888{
7a82dabc 889 wxFileData *fd = (wxFileData*)item.m_data;
8b17ba72
RR
890 delete fd;
891}
892
7a82dabc
VZ
893void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
894{
895 FreeItemData(event.m_item);
896}
897
898void wxFileCtrl::FreeAllItemsData()
12c1b46a
RR
899{
900 wxListItem item;
901 item.m_mask = wxLIST_MASK_DATA;
902
903 item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL );
92da8bde 904 while ( item.m_itemId != -1 )
12c1b46a
RR
905 {
906 GetItem( item );
7a82dabc 907 FreeItemData(item);
12c1b46a
RR
908 item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL );
909 }
910}
911
0b855868 912void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
8b17ba72
RR
913{
914 wxFileData *fd = (wxFileData*)event.m_item.m_data;
0b855868 915 wxASSERT( fd );
c8c0e54c 916
0b855868
RR
917 if ((event.GetLabel().IsEmpty()) ||
918 (event.GetLabel() == _(".")) ||
919 (event.GetLabel() == _("..")) ||
75ec8bd4 920 (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND))
8b17ba72 921 {
0b855868 922 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 923 dialog.ShowModal();
0b855868 924 event.Veto();
c8c0e54c 925 return;
8b17ba72 926 }
c8c0e54c 927
0b855868 928 wxString new_name( wxPathOnly( fd->GetFullName() ) );
75ec8bd4 929 new_name += wxFILE_SEP_PATH;
0b855868 930 new_name += event.GetLabel();
c8c0e54c 931
0b855868 932 wxLogNull log;
c8c0e54c 933
0b855868 934 if (wxFileExists(new_name))
8b17ba72 935 {
0b855868 936 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 937 dialog.ShowModal();
0b855868 938 event.Veto();
8b17ba72 939 }
c8c0e54c 940
0b855868 941 if (wxRenameFile(fd->GetFullName(),new_name))
8b17ba72 942 {
0b855868 943 fd->SetNewName( new_name, event.GetLabel() );
c8c0e54c 944 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
0b855868 945 EnsureVisible( event.GetItem() );
8b17ba72
RR
946 }
947 else
948 {
0b855868 949 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 950 dialog.ShowModal();
0b855868 951 event.Veto();
8b17ba72 952 }
8b17ba72
RR
953}
954
7a82dabc
VZ
955wxFileCtrl::~wxFileCtrl()
956{
957 FreeAllItemsData();
958}
959
8b17ba72
RR
960//-----------------------------------------------------------------------------
961// wxFileDialog
962//-----------------------------------------------------------------------------
963
5e673a6a
VS
964#define ID_LIST_MODE (wxID_FILEDLGG )
965#define ID_REPORT_MODE (wxID_FILEDLGG + 1)
966#define ID_UP_DIR (wxID_FILEDLGG + 5)
967#define ID_PARENT_DIR (wxID_FILEDLGG + 6)
968#define ID_NEW_DIR (wxID_FILEDLGG + 7)
969#define ID_CHOICE (wxID_FILEDLGG + 8)
970#define ID_TEXT (wxID_FILEDLGG + 9)
971#define ID_LIST_CTRL (wxID_FILEDLGG + 10)
972#define ID_ACTIVATED (wxID_FILEDLGG + 11)
973#define ID_CHECK (wxID_FILEDLGG + 12)
8b17ba72
RR
974
975IMPLEMENT_DYNAMIC_CLASS(wxFileDialog,wxDialog)
976
977BEGIN_EVENT_TABLE(wxFileDialog,wxDialog)
978 EVT_BUTTON(ID_LIST_MODE, wxFileDialog::OnList)
979 EVT_BUTTON(ID_REPORT_MODE, wxFileDialog::OnReport)
8b17ba72
RR
980 EVT_BUTTON(ID_UP_DIR, wxFileDialog::OnUp)
981 EVT_BUTTON(ID_PARENT_DIR, wxFileDialog::OnHome)
0b855868 982 EVT_BUTTON(ID_NEW_DIR, wxFileDialog::OnNew)
8b17ba72
RR
983 EVT_BUTTON(wxID_OK, wxFileDialog::OnListOk)
984 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxFileDialog::OnSelected)
c8c0e54c
VZ
985 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxFileDialog::OnActivated)
986 EVT_CHOICE(ID_CHOICE,wxFileDialog::OnChoice)
987 EVT_TEXT_ENTER(ID_TEXT,wxFileDialog::OnTextEnter)
bf9e3e73 988 EVT_CHECKBOX(ID_CHECK,wxFileDialog::OnCheck)
8b17ba72
RR
989END_EVENT_TABLE()
990
eaf40b23
VS
991long wxFileDialog::s_lastViewStyle = wxLC_LIST;
992bool wxFileDialog::s_lastShowHidden = FALSE;
655cf310 993
8b17ba72
RR
994wxFileDialog::wxFileDialog(wxWindow *parent,
995 const wxString& message,
996 const wxString& defaultDir,
997 const wxString& defaultFile,
998 const wxString& wildCard,
999 long style,
1000 const wxPoint& pos ) :
9c884972 1001 wxDialog( parent, -1, message, pos, wxDefaultSize, wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
8b17ba72 1002{
37fd1c97 1003 wxBusyCursor bcur;
c8c0e54c 1004
eaf40b23
VS
1005 if (wxConfig::Get(FALSE))
1006 {
7a82dabc 1007 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
5e673a6a 1008 &s_lastViewStyle);
7a82dabc 1009 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
5e673a6a 1010 &s_lastShowHidden);
eaf40b23
VS
1011 }
1012
8b17ba72
RR
1013 m_message = message;
1014 m_dialogStyle = style;
479cd5de 1015
655cf310 1016 if (m_dialogStyle == 0) m_dialogStyle = wxOPEN;
7941ba11 1017 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
9b00bb16 1018 m_dialogStyle |= wxOPEN;
479cd5de 1019
8b17ba72 1020 m_dir = defaultDir;
353f41cb 1021 if ((m_dir.empty()) || (m_dir == wxT(".")))
ac2def68 1022 {
75ec8bd4 1023 m_dir = wxGetCwd();
ac2def68 1024 }
7a82dabc 1025
353f41cb 1026 size_t len = m_dir.Len();
75ec8bd4 1027 if ((len > 1) && (m_dir[len-1] == wxFILE_SEP_PATH))
353f41cb
RR
1028 m_dir.Remove( len-1, 1 );
1029
1030 m_path = m_dir;
75ec8bd4 1031 m_path += wxFILE_SEP_PATH;
8b17ba72
RR
1032 m_path += defaultFile;
1033 m_fileName = defaultFile;
1034 m_wildCard = wildCard;
1035 m_filterIndex = 0;
655cf310 1036 m_filterExtension = wxEmptyString;
c8c0e54c 1037
cae5359f 1038 // interpret wildcards
c8c0e54c 1039
cae5359f
RR
1040 if (m_wildCard.IsEmpty())
1041 m_wildCard = _("All files (*)|*");
c8c0e54c 1042
223d09f6 1043 wxStringTokenizer tokens( m_wildCard, wxT("|") );
cae5359f
RR
1044 wxString firstWild;
1045 wxString firstWildText;
1046 if (tokens.CountTokens() == 1)
1047 {
1048 firstWildText = tokens.GetNextToken();
1049 firstWild = firstWildText;
1050 }
1051 else
1052 {
223d09f6 1053 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
cae5359f
RR
1054 firstWildText = tokens.GetNextToken();
1055 firstWild = tokens.GetNextToken();
1056 }
655cf310
VS
1057 if ( firstWild.Left( 2 ) == wxT("*.") )
1058 m_filterExtension = firstWild.Mid( 1 );
1059 if ( m_filterExtension == ".*" ) m_filterExtension = wxEmptyString;
cae5359f
RR
1060
1061 // layout
c8c0e54c 1062
8b17ba72 1063 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 1064
8b17ba72 1065 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 1066
e6daf794 1067 wxBitmapButton *but;
c8c0e54c 1068
7a82dabc 1069 but = new wxBitmapButton(this, ID_LIST_MODE,
60d2cc25 1070 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_CMN_DIALOG));
e6daf794
RR
1071#if wxUSE_TOOLTIPS
1072 but->SetToolTip( _("View files as a list view") );
1073#endif
1074 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 1075
60d2cc25
VS
1076 but = new wxBitmapButton(this, ID_REPORT_MODE,
1077 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_CMN_DIALOG));
e6daf794
RR
1078#if wxUSE_TOOLTIPS
1079 but->SetToolTip( _("View files as a detailed view") );
1080#endif
1081 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 1082
0b855868 1083 buttonsizer->Add( 30, 5, 1 );
e6daf794 1084
7a82dabc 1085 wxWindow *butDirUp =
60d2cc25
VS
1086 new wxBitmapButton(this, ID_UP_DIR,
1087 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_CMN_DIALOG));
e6daf794 1088#if wxUSE_TOOLTIPS
37fd1c97 1089 butDirUp->SetToolTip( _("Go to parent directory") );
e6daf794 1090#endif
37fd1c97 1091 buttonsizer->Add( butDirUp, 0, wxALL, 5 );
c8c0e54c 1092
37fd1c97 1093#ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
60d2cc25
VS
1094 but = new wxBitmapButton(this, ID_PARENT_DIR,
1095 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_CMN_DIALOG));
e6daf794
RR
1096#if wxUSE_TOOLTIPS
1097 but->SetToolTip( _("Go to home directory") );
1098#endif
1099 buttonsizer->Add( but, 0, wxALL, 5);
c8c0e54c 1100
e6daf794 1101 buttonsizer->Add( 20, 20 );
75ec8bd4 1102#endif //!__DOS__
c8c0e54c 1103
7a82dabc 1104 wxWindow *butNewDir =
60d2cc25
VS
1105 new wxBitmapButton(this, ID_NEW_DIR,
1106 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_CMN_DIALOG));
e6daf794 1107#if wxUSE_TOOLTIPS
37fd1c97 1108 butNewDir->SetToolTip( _("Create new directory") );
e6daf794 1109#endif
37fd1c97 1110 buttonsizer->Add( butNewDir, 0, wxALL, 5 );
c8c0e54c 1111
052c02b3
RR
1112#ifdef __WXX11__
1113 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
1114#else
0b855868 1115 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
052c02b3 1116#endif
c8c0e54c 1117
9c884972 1118 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
052c02b3 1119#ifndef __WXX11__
9c884972 1120 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
052c02b3 1121#endif
9c884972
RR
1122 m_static = new wxStaticText( this, -1, m_dir );
1123 staticsizer->Add( m_static, 1 );
1124 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
1125
7941ba11 1126 if (m_dialogStyle & wxMULTIPLE)
479cd5de 1127 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition,
01ec8f96 1128 wxSize(540,200), s_lastViewStyle | wxSUNKEN_BORDER );
7941ba11 1129 else
479cd5de 1130 m_list = new wxFileCtrl( this, ID_LIST_CTRL, m_dir, firstWild, wxDefaultPosition,
01ec8f96 1131 wxSize(540,200), s_lastViewStyle | wxSUNKEN_BORDER | wxLC_SINGLE_SEL );
37fd1c97
VS
1132 m_list->ShowHidden(s_lastShowHidden);
1133 m_list->SetNewDirControl(butNewDir);
1134 m_list->SetGoToParentControl(butDirUp);
24f932d2
RR
1135
1136#ifdef __WXX11__
1137 // PDAs have a different screen layout
1138 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
1139
1140 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
1141 m_choice = new wxChoice( this, ID_CHOICE );
1142 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 5 );
1143 mainsizer->Add( choicesizer, 0, wxEXPAND );
7a82dabc 1144
24f932d2
RR
1145 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
1146 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
1147 textsizer->Add( m_text, 1, wxCENTER | wxALL, 5 );
1148 mainsizer->Add( textsizer, 0, wxEXPAND );
1149
1150 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
1151 m_check->SetValue( s_lastShowHidden );
1152 textsizer->Add( m_check, 0, wxCENTER|wxALL, 5 );
7a82dabc 1153
24f932d2
RR
1154 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
1155 buttonsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxALL, 5 );
1156 buttonsizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 5 );
1157 mainsizer->Add( buttonsizer, 0, wxALIGN_RIGHT );
1158
1159#else
0b855868 1160 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
c8c0e54c 1161
8b17ba72 1162 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
cae5359f 1163 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
0b855868 1164 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
8b17ba72
RR
1165 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
1166 mainsizer->Add( textsizer, 0, wxEXPAND );
1167
1168 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
cae5359f 1169 m_choice = new wxChoice( this, ID_CHOICE );
8b17ba72 1170 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
bf9e3e73 1171 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
eaf40b23 1172 m_check->SetValue( s_lastShowHidden );
bf9e3e73 1173 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
8b17ba72
RR
1174 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
1175 mainsizer->Add( choicesizer, 0, wxEXPAND );
c8c0e54c 1176
24f932d2
RR
1177#endif
1178
cae5359f
RR
1179 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
1180 while (tokens.HasMoreTokens())
1181 {
1182 firstWildText = tokens.GetNextToken();
1183 firstWild = tokens.GetNextToken();
1184 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
1185 }
1186 m_choice->SetSelection( 0 );
8b17ba72
RR
1187
1188 SetAutoLayout( TRUE );
1189 SetSizer( mainsizer );
c8c0e54c 1190
8b17ba72
RR
1191 mainsizer->Fit( this );
1192 mainsizer->SetSizeHints( this );
48fe8374 1193
8b17ba72 1194 Centre( wxBOTH );
ed58dbea 1195
b6fa52db 1196/*
ed58dbea
RR
1197 if (m_fileName.IsEmpty())
1198 m_list->SetFocus();
1199 else
b6fa52db 1200*/
ed58dbea 1201 m_text->SetFocus();
8b17ba72
RR
1202}
1203
cae5359f
RR
1204wxFileDialog::~wxFileDialog()
1205{
eaf40b23
VS
1206 if (wxConfig::Get(FALSE))
1207 {
5e673a6a
VS
1208 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
1209 s_lastViewStyle);
1210 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
1211 s_lastShowHidden);
eaf40b23 1212 }
cae5359f
RR
1213}
1214
1215void wxFileDialog::OnChoice( wxCommandEvent &event )
1216{
13111b2a
VZ
1217 int index = (int)event.GetInt();
1218 wxString *str = (wxString*) m_choice->GetClientData( index );
cae5359f 1219 m_list->SetWild( *str );
13111b2a 1220 m_filterIndex = index;
655cf310
VS
1221 if ( str -> Left( 2 ) == wxT("*.") )
1222 {
1223 m_filterExtension = str -> Mid( 1 );
1224 if (m_filterExtension == ".*") m_filterExtension = wxEmptyString;
1225 }
1226 else
1227 m_filterExtension = wxEmptyString;
cae5359f
RR
1228}
1229
bf9e3e73
RR
1230void wxFileDialog::OnCheck( wxCommandEvent &event )
1231{
eaf40b23 1232 m_list->ShowHidden( (s_lastShowHidden = event.GetInt() != 0) );
bf9e3e73
RR
1233}
1234
e1811a01 1235void wxFileDialog::OnActivated( wxListEvent &event )
8b17ba72 1236{
e1811a01 1237 HandleAction( event.m_item.m_text );
8b17ba72
RR
1238}
1239
cae5359f
RR
1240void wxFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
1241{
1242 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
1243 cevent.SetEventObject( this );
1244 GetEventHandler()->ProcessEvent( cevent );
1245}
1246
8b17ba72
RR
1247void wxFileDialog::OnSelected( wxListEvent &event )
1248{
e1811a01 1249 wxString filename( event.m_item.m_text );
223d09f6 1250 if (filename == wxT("..")) return;
479cd5de 1251
e1811a01
RR
1252 wxString dir;
1253 m_list->GetDir( dir );
5e673a6a
VS
1254 if (!IsTopMostDir(dir))
1255 dir += wxFILE_SEP_PATH;
e1811a01
RR
1256 dir += filename;
1257 if (wxDirExists(dir)) return;
479cd5de 1258
e1811a01 1259 m_text->SetValue( filename );
8b17ba72
RR
1260}
1261
e1811a01 1262void wxFileDialog::HandleAction( const wxString &fn )
8b17ba72 1263{
e1811a01 1264 wxString filename( fn );
8b17ba72
RR
1265 wxString dir;
1266 m_list->GetDir( dir );
1267 if (filename.IsEmpty()) return;
223d09f6 1268 if (filename == wxT(".")) return;
c8c0e54c 1269
223d09f6 1270 if (filename == wxT(".."))
0b855868
RR
1271 {
1272 m_list->GoToParentDir();
1273 m_list->SetFocus();
9c884972
RR
1274 m_list->GetDir( dir );
1275 m_static->SetLabel( dir );
c8c0e54c 1276 return;
0b855868
RR
1277 }
1278
75ec8bd4 1279#ifdef __UNIX__
223d09f6 1280 if (filename == wxT("~"))
ed58dbea
RR
1281 {
1282 m_list->GoToHomeDir();
1283 m_list->SetFocus();
1284 m_list->GetDir( dir );
1285 m_static->SetLabel( dir );
c8c0e54c 1286 return;
ed58dbea 1287 }
c8c0e54c 1288
f6bcfd97 1289 if (filename[0u] == wxT('~'))
ed58dbea
RR
1290 {
1291 filename.Remove( 0, 1 );
c8c0e54c 1292 wxString tmp( wxGetUserHome() );
223d09f6 1293 tmp += wxT('/');
c8c0e54c
VZ
1294 tmp += filename;
1295 filename = tmp;
ed58dbea 1296 }
75ec8bd4 1297#endif // __UNIX__
ed58dbea 1298
223d09f6
KB
1299 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
1300 (filename.Find(wxT('?')) != wxNOT_FOUND))
cae5359f 1301 {
75ec8bd4 1302 if (filename.Find(wxFILE_SEP_PATH) != wxNOT_FOUND)
c8c0e54c
VZ
1303 {
1304 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
1305 return;
1306 }
1307 m_list->SetWild( filename );
1308 return;
cae5359f
RR
1309 }
1310
5e673a6a
VS
1311 if (!IsTopMostDir(dir))
1312 dir += wxFILE_SEP_PATH;
1313 if (!wxIsAbsolutePath(filename))
ed58dbea
RR
1314 {
1315 dir += filename;
1316 filename = dir;
1317 }
c8c0e54c 1318
8b17ba72
RR
1319 if (wxDirExists(filename))
1320 {
1321 m_list->GoToDir( filename );
9c884972
RR
1322 m_list->GetDir( dir );
1323 m_static->SetLabel( dir );
c8c0e54c 1324 return;
8b17ba72 1325 }
c8c0e54c 1326
655cf310 1327
8b17ba72
RR
1328 if ( (m_dialogStyle & wxSAVE) && (m_dialogStyle & wxOVERWRITE_PROMPT) )
1329 {
655cf310 1330 if (filename.Find( wxT('.') ) == wxNOT_FOUND ||
75ec8bd4 1331 filename.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH ) != wxNOT_FOUND)
655cf310 1332 filename << m_filterExtension;
8b17ba72
RR
1333 if (wxFileExists( filename ))
1334 {
1335 wxString msg;
1336 msg.Printf( _("File '%s' already exists, do you really want to "
1337 "overwrite it?"), filename.c_str() );
1338
1339 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1340 return;
1341 }
1342 }
655cf310 1343 else if ( m_dialogStyle & wxOPEN )
8b17ba72
RR
1344 {
1345 if ( !wxFileExists( filename ) )
655cf310 1346 if (filename.Find( wxT('.') ) == wxNOT_FOUND ||
75ec8bd4 1347 filename.AfterLast( wxT('.') ).Find( wxFILE_SEP_PATH ) != wxNOT_FOUND)
655cf310
VS
1348 filename << m_filterExtension;
1349
1350 if ( m_dialogStyle & wxFILE_MUST_EXIST )
8b17ba72 1351 {
655cf310
VS
1352 if ( !wxFileExists( filename ) )
1353 {
1354 wxMessageBox(_("Please choose an existing file."), _("Error"), wxOK | wxICON_ERROR );
1355 return;
1356 }
8b17ba72
RR
1357 }
1358 }
1359
1360 SetPath( filename );
479cd5de 1361
3f6638b8 1362 // change to the directory where the user went if asked
fda09b3f 1363 if ( m_dialogStyle & wxCHANGE_DIR )
3f6638b8
VZ
1364 {
1365 wxString cwd;
1366 wxSplitPath(filename, &cwd, NULL, NULL);
1367
1368 if ( cwd != wxGetWorkingDirectory() )
1369 {
1370 wxSetWorkingDirectory(cwd);
1371 }
1372 }
1373
9b7e522a
RR
1374 wxCommandEvent event;
1375 wxDialog::OnOK(event);
e1811a01
RR
1376}
1377
5e0201ea 1378void wxFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
e1811a01
RR
1379{
1380 HandleAction( m_text->GetValue() );
8b17ba72
RR
1381}
1382
1383void wxFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
1384{
1385 m_list->ChangeToListMode();
eaf40b23 1386 s_lastViewStyle = wxLC_LIST;
0b855868 1387 m_list->SetFocus();
8b17ba72
RR
1388}
1389
1390void wxFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
1391{
1392 m_list->ChangeToReportMode();
eaf40b23 1393 s_lastViewStyle = wxLC_REPORT;
0b855868 1394 m_list->SetFocus();
8b17ba72
RR
1395}
1396
1397void wxFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
1398{
1399 m_list->GoToParentDir();
0b855868 1400 m_list->SetFocus();
9c884972
RR
1401 wxString dir;
1402 m_list->GetDir( dir );
1403 m_static->SetLabel( dir );
8b17ba72
RR
1404}
1405
1406void wxFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
1407{
1408 m_list->GoToHomeDir();
0b855868 1409 m_list->SetFocus();
9c884972
RR
1410 wxString dir;
1411 m_list->GetDir( dir );
1412 m_static->SetLabel( dir );
7a82dabc 1413
d7fa7eaa 1414 m_text->SetFocus();
0b855868
RR
1415}
1416
1417void wxFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
1418{
1419 m_list->MakeDir();
8b17ba72
RR
1420}
1421
1422void wxFileDialog::SetPath( const wxString& path )
1423{
1424 // not only set the full path but also update filename and dir
1425 m_path = path;
1426 if ( !!path )
1427 {
1428 wxString ext;
1429 wxSplitPath(path, &m_dir, &m_fileName, &ext);
c8c0e54c
VZ
1430 if (!ext.IsEmpty())
1431 {
223d09f6 1432 m_fileName += wxT(".");
8b17ba72 1433 m_fileName += ext;
c8c0e54c 1434 }
8b17ba72
RR
1435 }
1436}
c8c0e54c 1437
7941ba11
RR
1438void wxFileDialog::GetPaths( wxArrayString& paths ) const
1439{
1440 paths.Empty();
28c9c76e
RR
1441 if (m_list->GetSelectedItemCount() == 0)
1442 {
1443 paths.Add( GetPath() );
1444 return;
1445 }
479cd5de 1446
7941ba11
RR
1447 paths.Alloc( m_list->GetSelectedItemCount() );
1448
1449 wxString dir;
1450 m_list->GetDir( dir );
5e673a6a 1451#ifdef __UNIX__
7a82dabc 1452 if (dir != wxT("/"))
5e673a6a
VS
1453#endif
1454 dir += wxFILE_SEP_PATH;
7941ba11
RR
1455
1456 wxListItem item;
1457 item.m_mask = wxLIST_MASK_TEXT;
1458
1459 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
92da8bde 1460 while ( item.m_itemId != -1 )
68df5777 1461 {
7941ba11
RR
1462 m_list->GetItem( item );
1463 paths.Add( dir + item.m_text );
68df5777 1464 item.m_itemId = m_list->GetNextItem( item.m_itemId,
7941ba11
RR
1465 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1466 }
1467}
1468
1469void wxFileDialog::GetFilenames(wxArrayString& files) const
1470{
1471 files.Empty();
28c9c76e
RR
1472 if (m_list->GetSelectedItemCount() == 0)
1473 {
1474 files.Add( GetFilename() );
1475 return;
1476 }
7941ba11
RR
1477 files.Alloc( m_list->GetSelectedItemCount() );
1478
1479 wxListItem item;
1480 item.m_mask = wxLIST_MASK_TEXT;
1481
1482 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
92da8bde 1483 while ( item.m_itemId != -1 )
68df5777 1484 {
7941ba11
RR
1485 m_list->GetItem( item );
1486 files.Add( item.m_text );
68df5777 1487 item.m_itemId = m_list->GetNextItem( item.m_itemId,
7941ba11
RR
1488 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1489 }
1490}
1491
655cf310
VS
1492
1493
8b17ba72
RR
1494// ----------------------------------------------------------------------------
1495// global functions
1496// ----------------------------------------------------------------------------
1497
1498wxString
1499wxFileSelectorEx(const wxChar *message,
1500 const wxChar *default_path,
1501 const wxChar *default_filename,
5e0201ea 1502 int *WXUNUSED(indexDefaultExtension),
8b17ba72
RR
1503 const wxChar *wildcard,
1504 int flags,
1505 wxWindow *parent,
1506 int x, int y)
1507{
1508 // TODO: implement this somehow
223d09f6 1509 return wxFileSelector(message, default_path, default_filename, wxT(""),
8b17ba72
RR
1510 wildcard, flags, parent, x, y);
1511}
1512
1513wxString wxFileSelector( const wxChar *title,
1514 const wxChar *defaultDir, const wxChar *defaultFileName,
1515 const wxChar *defaultExtension, const wxChar *filter, int flags,
1516 wxWindow *parent, int x, int y )
1517{
1518 wxString filter2;
1519 if ( defaultExtension && !filter )
223d09f6 1520 filter2 = wxString(wxT("*.")) + wxString(defaultExtension) ;
8b17ba72
RR
1521 else if ( filter )
1522 filter2 = filter;
1523
1524 wxString defaultDirString;
1525 if (defaultDir)
1526 defaultDirString = defaultDir;
1527
1528 wxString defaultFilenameString;
1529 if (defaultFileName)
1530 defaultFilenameString = defaultFileName;
1531
1532 wxFileDialog fileDialog( parent, title, defaultDirString, defaultFilenameString, filter2, flags, wxPoint(x, y) );
1533
1534 if ( fileDialog.ShowModal() == wxID_OK )
1535 {
1536 return fileDialog.GetPath();
1537 }
1538 else
1539 {
1540 return wxEmptyString;
1541 }
1542}
1543
3f6638b8 1544wxString wxLoadFileSelector( const wxChar *what, const wxChar *ext, const wxChar *default_name, wxWindow *parent )
8b17ba72 1545{
3f6638b8 1546 wxString prompt = wxString::Format(_("Load %s file"), what);
8b17ba72 1547
3f6638b8
VZ
1548 if (*ext == wxT('.'))
1549 ext++;
8b17ba72 1550
3f6638b8 1551 wxString wild = wxString::Format(_T("*.%s"), ext);
8b17ba72 1552
3f6638b8
VZ
1553 return wxFileSelector(prompt, (const wxChar *) NULL, default_name,
1554 ext, wild, 0, parent);
8b17ba72
RR
1555}
1556
1557wxString wxSaveFileSelector(const wxChar *what, const wxChar *extension, const wxChar *default_name,
1558 wxWindow *parent )
1559{
1560 wxChar *ext = (wxChar *)extension;
1561
3f6638b8
VZ
1562 wxString prompt = wxString::Format(_("Save %s file"), what);
1563
1564 if (*ext == wxT('.'))
1565 ext++;
8b17ba72 1566
3f6638b8 1567 wxString wild = wxString::Format(_T("*.%s"), ext);
8b17ba72 1568
3f6638b8
VZ
1569 return wxFileSelector(prompt, (const wxChar *) NULL, default_name,
1570 ext, wild, 0, parent);
8b17ba72
RR
1571}
1572
655cf310
VS
1573
1574
1575
1576
1577
1578// A module to allow icons table cleanup
1579
1580class wxFileDialogGenericModule: public wxModule
1581{
1582DECLARE_DYNAMIC_CLASS(wxFileDialogGenericModule)
1583public:
1584 wxFileDialogGenericModule() {}
1585 bool OnInit() { return TRUE; }
1586 void OnExit() { if (g_IconsTable) {delete g_IconsTable; g_IconsTable = NULL;} }
1587};
1588
1589IMPLEMENT_DYNAMIC_CLASS(wxFileDialogGenericModule, wxModule)
1e6feb95
VZ
1590
1591#endif // wxUSE_FILEDLG