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