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