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