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