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