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