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