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