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