]> git.saurik.com Git - wxWidgets.git/blame - src/generic/filedlgg.cpp
Ensure grid components don't have borders
[wxWidgets.git] / src / generic / filedlgg.cpp
CommitLineData
8b17ba72
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: filedlgg.cpp
23254b13 3// Purpose: wxGenericFileDialog
8b17ba72
RR
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
9cedab37
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
8b17ba72
RR
20#ifdef __GNUG__
21#pragma implementation "filedlgg.h"
22#endif
23
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28#pragma hdrstop
29#endif
30
1e6feb95
VZ
31#if wxUSE_FILEDLG
32
06cc1fb9 33// NOTE : it probably also supports MAC, untested
099d4217 34#if !defined(__UNIX__) && !defined(__DOS__) && !defined(__WIN32__)
23254b13 35#error wxGenericFileDialog currently only supports Unix, win32 and DOS
8b17ba72
RR
36#endif
37
04dbb646
VZ
38#include "wx/checkbox.h"
39#include "wx/textctrl.h"
40#include "wx/choice.h"
41#include "wx/checkbox.h"
42#include "wx/stattext.h"
8b17ba72
RR
43#include "wx/debug.h"
44#include "wx/log.h"
45#include "wx/intl.h"
46#include "wx/msgdlg.h"
47#include "wx/sizer.h"
0b855868 48#include "wx/bmpbuttn.h"
cae5359f 49#include "wx/tokenzr.h"
eaf40b23 50#include "wx/config.h"
48fe8374 51#include "wx/imaglist.h"
5e673a6a 52#include "wx/dir.h"
60d2cc25 53#include "wx/artprov.h"
1265e13d 54#include "wx/file.h" // for wxS_IXXX constants only
23254b13
JS
55#include "wx/filedlg.h" // wxOPEN, wxSAVE...
56#include "wx/generic/filedlgg.h"
06cc1fb9 57#include "wx/generic/dirctrlg.h" // for wxFileIconsTable
8b17ba72 58
e6daf794
RR
59#if wxUSE_TOOLTIPS
60 #include "wx/tooltip.h"
61#endif
62
ac2def68
RR
63#include <sys/types.h>
64#include <sys/stat.h>
4894ae18
VS
65
66#ifdef __UNIX__
67 #include <dirent.h>
68 #include <pwd.h>
69 #ifndef __VMS
70 # include <grp.h>
71 #endif
72#endif
73
74#ifdef __WATCOMC__
75 #include <direct.h>
27df579a 76#endif
4894ae18 77
7a82dabc 78#include <time.h>
099d4217 79#if defined(__UNIX__) || defined(__DOS__)
ac2def68 80#include <unistd.h>
099d4217 81#endif
8b17ba72 82
655cf310 83// ----------------------------------------------------------------------------
06cc1fb9 84// private functions
655cf310
VS
85// ----------------------------------------------------------------------------
86
06cc1fb9
JS
87static
88int wxFileDataNameCompare( long data1, long data2, long data)
655cf310 89{
06cc1fb9
JS
90 wxFileData *fd1 = (wxFileData*)data1;
91 wxFileData *fd2 = (wxFileData*)data2;
92 if (fd1->GetFileName() == wxT("..")) return -data;
93 if (fd2->GetFileName() == wxT("..")) return data;
94 if (fd1->IsDir() && !fd2->IsDir()) return -data;
95 if (fd2->IsDir() && !fd1->IsDir()) return data;
96 return data*wxStrcmp( fd1->GetFileName(), fd2->GetFileName() );
655cf310
VS
97}
98
c8c0e54c 99static
06cc1fb9 100int wxFileDataSizeCompare( long data1, long data2, long data)
23254b13
JS
101{
102 wxFileData *fd1 = (wxFileData*)data1;
103 wxFileData *fd2 = (wxFileData*)data2;
06cc1fb9
JS
104 if (fd1->GetFileName() == wxT("..")) return -data;
105 if (fd2->GetFileName() == wxT("..")) return data;
23254b13
JS
106 if (fd1->IsDir() && !fd2->IsDir()) return -data;
107 if (fd2->IsDir() && !fd1->IsDir()) return data;
06cc1fb9
JS
108 if (fd1->IsLink() && !fd2->IsLink()) return -data;
109 if (fd2->IsLink() && !fd1->IsLink()) return data;
110 return data*(fd1->GetSize() - fd2->GetSize());
23254b13
JS
111}
112
113static
114int wxFileDataTypeCompare( long data1, long data2, long data)
115{
116 wxFileData *fd1 = (wxFileData*)data1;
117 wxFileData *fd2 = (wxFileData*)data2;
06cc1fb9
JS
118 if (fd1->GetFileName() == wxT("..")) return -data;
119 if (fd2->GetFileName() == wxT("..")) return data;
23254b13
JS
120 if (fd1->IsDir() && !fd2->IsDir()) return -data;
121 if (fd2->IsDir() && !fd1->IsDir()) return data;
122 if (fd1->IsLink() && !fd2->IsLink()) return -data;
123 if (fd2->IsLink() && !fd1->IsLink()) return data;
06cc1fb9 124 return data*wxStrcmp( fd1->GetType(), fd2->GetType() );
23254b13
JS
125}
126
127static
128int wxFileDataTimeCompare( long data1, long data2, long data)
129{
130 wxFileData *fd1 = (wxFileData*)data1;
131 wxFileData *fd2 = (wxFileData*)data2;
06cc1fb9
JS
132 if (fd1->GetFileName() == wxT("..")) return -data;
133 if (fd2->GetFileName() == wxT("..")) return data;
23254b13
JS
134 if (fd1->IsDir() && !fd2->IsDir()) return -data;
135 if (fd2->IsDir() && !fd1->IsDir()) return data;
136
06cc1fb9 137 return fd1->GetTime().IsLaterThan(fd2->GetTime()) ? int(data) : -int(data);
c8c0e54c
VZ
138}
139
5e673a6a
VS
140#ifdef __UNIX__
141#define IsTopMostDir(dir) (dir == wxT("/"))
142#endif
143
144#if defined(__DOS__) || defined(__WINDOWS__)
145#define IsTopMostDir(dir) (dir.IsEmpty())
146#endif
147
4f5c180e 148#if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__)
06cc1fb9 149// defined in src/generic/dirctrlg.cpp
37fd1c97
VS
150extern bool wxIsDriveAvailable(const wxString& dirName);
151#endif
152
06cc1fb9
JS
153// defined in src/generic/dirctrlg.cpp
154extern size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids);
155
8b17ba72
RR
156//-----------------------------------------------------------------------------
157// wxFileData
158//-----------------------------------------------------------------------------
159
06cc1fb9 160wxFileData::wxFileData( const wxString &filePath, const wxString &fileName, fileType type, int image_id )
8b17ba72 161{
06cc1fb9
JS
162 m_fileName = fileName;
163 m_filePath = filePath;
23254b13 164 m_type = type;
06cc1fb9 165 m_image = image_id;
7a82dabc 166
06cc1fb9 167 if (IsDrive())
37fd1c97 168 {
23254b13
JS
169 m_size = 0;
170 return;
171 }
172
06cc1fb9
JS
173#if defined(__DOS__) || defined(__WINDOWS__)
174 // c:\.. is a drive don't stat it
175 if ((fileName == wxT("..")) && (filePath.length() <= 5))
23254b13
JS
176 {
177 m_type = is_drive;
37fd1c97
VS
178 m_size = 0;
179 return;
180 }
7f04165e 181#endif // __DOS__ || __WINDOWS__
c8c0e54c 182
8115f9e7 183 wxStructStat buff;
479cd5de 184
4894ae18 185#if defined(__UNIX__) && (!defined( __EMX__ ) && !defined(__VMS))
06cc1fb9 186 lstat( m_filePath.fn_str(), &buff );
23254b13 187 m_type |= S_ISLNK( buff.st_mode ) != 0 ? is_link : 0;
7f04165e 188#else // no lstat()
06cc1fb9 189 wxStat( m_filePath, &buff );
9f2d09aa
RR
190#endif
191
23254b13
JS
192 m_type |= (buff.st_mode & S_IFDIR) != 0 ? is_dir : 0;
193 m_type |= (buff.st_mode & wxS_IXUSR) != 0 ? is_exe : 0;
8b17ba72 194
06cc1fb9
JS
195 // try to get a better icon
196 if (m_image == wxFileIconsTable::file)
197 {
198 if (IsExe())
199 m_image = wxFileIconsTable::executable;
200 else if (m_fileName.Find(wxT('.'), TRUE) != wxNOT_FOUND)
201 m_image = wxTheFileIconsTable->GetIconID(m_fileName.AfterLast(wxT('.')));
202 }
203
8b17ba72
RR
204 m_size = buff.st_size;
205
06cc1fb9 206 m_dateTime = buff.st_mtime;
8b17ba72 207
06cc1fb9
JS
208#if defined(__UNIX__)
209 m_permissions.Printf(_T("%c%c%c%c%c%c%c%c%c"),
7f04165e
VZ
210 buff.st_mode & wxS_IRUSR ? _T('r') : _T('-'),
211 buff.st_mode & wxS_IWUSR ? _T('w') : _T('-'),
06cc1fb9
JS
212 buff.st_mode & wxS_IXUSR ? _T('x') : _T('-'),
213 buff.st_mode & wxS_IRGRP ? _T('r') : _T('-'),
214 buff.st_mode & wxS_IWGRP ? _T('w') : _T('-'),
215 buff.st_mode & wxS_IXGRP ? _T('x') : _T('-'),
216 buff.st_mode & wxS_IROTH ? _T('r') : _T('-'),
217 buff.st_mode & wxS_IWOTH ? _T('w') : _T('-'),
218 buff.st_mode & wxS_IXOTH ? _T('x') : _T('-'));
219#elif defined(__WIN32__)
220 DWORD attribs = GetFileAttributes(filePath);
221 if (attribs != (DWORD)-1)
222 {
223 m_permissions.Printf(_T("%c%c%c%c"),
224 attribs & FILE_ATTRIBUTE_ARCHIVE ? _T('A') : _T(' '),
225 attribs & FILE_ATTRIBUTE_READONLY ? _T('R') : _T(' '),
226 attribs & FILE_ATTRIBUTE_HIDDEN ? _T('H') : _T(' '),
227 attribs & FILE_ATTRIBUTE_SYSTEM ? _T('S') : _T(' '));
228 }
229#endif
8b17ba72
RR
230}
231
06cc1fb9 232wxString wxFileData::GetType() const
8b17ba72 233{
06cc1fb9
JS
234 if (IsDir())
235 return _("<DIR>");
236 else if (IsLink())
237 return _("<LINK>");
238 else if (IsDrive())
239 return _("<DRIVE>");
240 else if (m_fileName.Find(wxT('.'), TRUE) != wxNOT_FOUND)
241 return m_fileName.AfterLast(wxT('.'));
8b17ba72 242
06cc1fb9 243 return wxEmptyString;
8b17ba72
RR
244}
245
06cc1fb9 246wxString wxFileData::GetModificationTime() const
23254b13 247{
06cc1fb9
JS
248 // want time as 01:02 so they line up nicely, no %r in WIN32
249 return m_dateTime.FormatDate() + wxT(" ") + m_dateTime.Format(wxT("%I:%M:%S %p"));
23254b13
JS
250}
251
8b17ba72
RR
252wxString wxFileData::GetHint() const
253{
06cc1fb9 254 wxString s = m_filePath;
2b5f62a0 255 s += wxT(" ");
b8985048 256
23254b13 257 if (IsDir())
b8985048 258 s += _("<DIR>");
23254b13 259 else if (IsLink())
b8985048 260 s += _("<LINK>");
23254b13 261 else if (IsDrive())
b8985048
VZ
262 s += _("<DRIVE>");
263 else // plain file
264 s += wxString::Format( _("%ld bytes"), m_size );
265
266 s += wxT(' ');
267
268 if ( !IsDrive() )
23254b13 269 {
b8985048
VZ
270 s << GetModificationTime()
271 << wxT(" ")
272 << m_permissions;
8b17ba72 273 }
23254b13 274
8b17ba72
RR
275 return s;
276};
277
23254b13 278wxString wxFileData::GetEntry( fileListFieldType num ) const
8b17ba72
RR
279{
280 wxString s;
9cedab37 281 switch ( num )
8b17ba72 282 {
9cedab37 283 case FileList_Name:
06cc1fb9 284 s = m_fileName;
8b17ba72 285 break;
9cedab37 286
06cc1fb9
JS
287 case FileList_Size:
288 if (!IsDir() && !IsLink() && !IsDrive())
9cedab37 289 s.Printf(_T("%ld"), m_size);
8b17ba72 290 break;
9cedab37 291
06cc1fb9
JS
292 case FileList_Type:
293 s = GetType();
8b17ba72 294 break;
8b17ba72 295
9cedab37 296 case FileList_Time:
23254b13 297 if (!IsDrive())
06cc1fb9 298 s = GetModificationTime();
9cedab37 299 break;
8b17ba72 300
06cc1fb9 301#if defined(__UNIX__) || defined(__WIN32__)
9cedab37
VZ
302 case FileList_Perm:
303 s = m_permissions;
304 break;
06cc1fb9 305#endif // defined(__UNIX__) || defined(__WIN32__)
8b17ba72 306
9cedab37
VZ
307 default:
308 wxFAIL_MSG( _T("unexpected field in wxFileData::GetEntry()") );
309 }
8b17ba72 310
9cedab37 311 return s;
8b17ba72
RR
312}
313
06cc1fb9 314void wxFileData::SetNewName( const wxString &filePath, const wxString &fileName )
8b17ba72 315{
06cc1fb9
JS
316 m_fileName = fileName;
317 m_filePath = filePath;
8b17ba72
RR
318}
319
320void wxFileData::MakeItem( wxListItem &item )
321{
06cc1fb9 322 item.m_text = m_fileName;
9b00bb16 323 item.ClearAttributes();
9cedab37
VZ
324 if (IsExe())
325 item.SetTextColour(*wxRED);
326 if (IsDir())
327 item.SetTextColour(*wxBLUE);
48fe8374 328
06cc1fb9 329 item.m_image = m_image;
655cf310 330
8b17ba72
RR
331 if (IsLink())
332 {
9cedab37 333 wxColour *dg = wxTheColourDatabase->FindColour( _T("MEDIUM GREY") );
aaa37c0d 334 item.SetTextColour(*dg);
8b17ba72
RR
335 }
336 item.m_data = (long)this;
337}
c8c0e54c 338
8b17ba72
RR
339//-----------------------------------------------------------------------------
340// wxFileCtrl
341//-----------------------------------------------------------------------------
342
d84afea9 343IMPLEMENT_DYNAMIC_CLASS(wxFileCtrl,wxListCtrl)
8b17ba72
RR
344
345BEGIN_EVENT_TABLE(wxFileCtrl,wxListCtrl)
0b855868
RR
346 EVT_LIST_DELETE_ITEM(-1, wxFileCtrl::OnListDeleteItem)
347 EVT_LIST_END_LABEL_EDIT(-1, wxFileCtrl::OnListEndLabelEdit)
23254b13 348 EVT_LIST_COL_CLICK(-1, wxFileCtrl::OnListColClick)
8b17ba72
RR
349END_EVENT_TABLE()
350
655cf310 351
8b17ba72
RR
352wxFileCtrl::wxFileCtrl()
353{
8b17ba72 354 m_showHidden = FALSE;
23254b13
JS
355 m_sort_foward = 1;
356 m_sort_field = wxFileData::FileList_Name;
8b17ba72
RR
357}
358
9cedab37
VZ
359wxFileCtrl::wxFileCtrl(wxWindow *win,
360 wxWindowID id,
361 const wxString& wild,
362 bool showHidden,
363 const wxPoint& pos,
364 const wxSize& size,
365 long style,
366 const wxValidator &validator,
5e673a6a 367 const wxString &name)
9cedab37
VZ
368 : wxListCtrl(win, id, pos, size, style, validator, name),
369 m_wild(wild)
8b17ba72 370{
06cc1fb9 371 wxImageList *imageList = wxTheFileIconsTable->GetSmallImageList();
655cf310 372
0b855868 373 SetImageList( imageList, wxIMAGE_LIST_SMALL );
c8c0e54c 374
9cedab37 375 m_showHidden = showHidden;
23254b13
JS
376
377 m_sort_foward = 1;
378 m_sort_field = wxFileData::FileList_Name;
379
380 m_dirName = wxT("*");
381
382 if (style & wxLC_REPORT)
383 ChangeToReportMode();
8b17ba72
RR
384}
385
386void wxFileCtrl::ChangeToListMode()
387{
23254b13 388 ClearAll();
8b17ba72 389 SetSingleStyle( wxLC_LIST );
8f4fcc4e 390 UpdateFiles();
8b17ba72
RR
391}
392
393void wxFileCtrl::ChangeToReportMode()
394{
23254b13 395 ClearAll();
8b17ba72 396 SetSingleStyle( wxLC_REPORT );
23254b13 397
06cc1fb9
JS
398 // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy
399 // don't hardcode since mm/dd is dd/mm elsewhere
23254b13 400 int w, h;
06cc1fb9
JS
401 wxDateTime dt(22, wxDateTime::Dec, 2002, 22, 22, 22);
402 wxString txt = dt.FormatDate() + wxT("22") + dt.Format(wxT("%I:%M:%S %p"));
403 GetTextExtent(txt, &w, &h);
404
405 InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, w );
406 InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, w/2 );
407 InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT, w/2 );
408 InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT, w );
409#if defined(__UNIX__)
410 GetTextExtent(wxT("Permissions 2"), &w, &h);
411 InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, w );
412#elif defined(__WIN32__)
413 GetTextExtent(wxT("Attributes 2"), &w, &h);
414 InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT, w );
23254b13
JS
415#endif
416
8f4fcc4e 417 UpdateFiles();
8b17ba72
RR
418}
419
06cc1fb9 420void wxFileCtrl::ChangeToSmallIconMode()
8b17ba72 421{
23254b13 422 ClearAll();
06cc1fb9 423 SetSingleStyle( wxLC_SMALL_ICON );
8f4fcc4e 424 UpdateFiles();
8b17ba72
RR
425}
426
427void wxFileCtrl::ShowHidden( bool show )
428{
429 m_showHidden = show;
8f4fcc4e 430 UpdateFiles();
8b17ba72
RR
431}
432
0b855868
RR
433long wxFileCtrl::Add( wxFileData *fd, wxListItem &item )
434{
435 long ret = -1;
436 item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE;
437 fd->MakeItem( item );
438 long my_style = GetWindowStyleFlag();
439 if (my_style & wxLC_REPORT)
440 {
441 ret = InsertItem( item );
23254b13
JS
442 for (int i = 1; i < wxFileData::FileList_Max; i++)
443 SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) );
0b855868 444 }
06cc1fb9 445 else if ((my_style & wxLC_LIST) || (my_style & wxLC_SMALL_ICON))
0b855868
RR
446 {
447 ret = InsertItem( item );
c8c0e54c 448 }
0b855868
RR
449 return ret;
450}
451
8f4fcc4e 452void wxFileCtrl::UpdateFiles()
c8c0e54c 453{
2b5f62a0 454 // don't do anything before ShowModal() call which sets m_dirName
23254b13 455 if ( m_dirName == wxT("*") )
2b5f62a0
VZ
456 return;
457
37fd1c97 458 wxBusyCursor bcur; // this may take a while...
7a82dabc 459
7a82dabc 460 FreeAllItemsData();
23254b13 461 DeleteAllItems();
7a82dabc 462
8b17ba72
RR
463 wxFileData *fd = (wxFileData *) NULL;
464 wxListItem item;
8b17ba72
RR
465 item.m_itemId = 0;
466 item.m_col = 0;
0b855868 467
06cc1fb9 468#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__WXPM__)
37fd1c97 469 if ( IsTopMostDir(m_dirName) )
37fd1c97 470 {
06cc1fb9
JS
471 wxArrayString names, paths;
472 wxArrayInt icons;
473 size_t n, count = wxGetAvailableDrives(paths, names, icons);
474
475 for (n=0; n<count; n++)
37fd1c97 476 {
06cc1fb9 477 fd = new wxFileData(paths[n], names[n], wxFileData::is_drive, icons[n]);
37fd1c97
VS
478 Add(fd, item);
479 item.m_itemId++;
480 }
481 }
5e673a6a 482 else
06cc1fb9 483#endif // defined(__DOS__) || defined(__WINDOWS__)
8b17ba72 484 {
5e673a6a
VS
485 // Real directory...
486 if ( !IsTopMostDir(m_dirName) )
8b17ba72 487 {
5e673a6a
VS
488 wxString p(wxPathOnly(m_dirName));
489#ifdef __UNIX__
490 if (p.IsEmpty()) p = wxT("/");
06cc1fb9
JS
491#endif // __UNIX__
492 fd = new wxFileData(p, wxT(".."), wxFileData::is_dir, wxFileIconsTable::folder);
5e673a6a 493 Add(fd, item);
8b17ba72
RR
494 item.m_itemId++;
495 }
c8c0e54c 496
5e673a6a
VS
497 wxString dirname(m_dirName);
498#if defined(__DOS__) || defined(__WINDOWS__)
499 if (dirname.length() == 2 && dirname[1u] == wxT(':'))
500 dirname << wxT('\\');
06cc1fb9 501#endif // defined(__DOS__) || defined(__WINDOWS__)
5e673a6a
VS
502 wxDir dir(dirname);
503
504 if ( dir.IsOpened() )
cae5359f 505 {
23254b13
JS
506 wxString dirPrefix(dirname);
507 if (dirPrefix.Last() != wxFILE_SEP_PATH)
508 dirPrefix += wxFILE_SEP_PATH;
509
5e673a6a 510 int hiddenFlag = m_showHidden ? wxDIR_HIDDEN : 0;
7a82dabc 511
5e673a6a
VS
512 bool cont;
513 wxString f;
514
515 // Get the directories first (not matched against wildcards):
516 cont = dir.GetFirst(&f, wxEmptyString, wxDIR_DIRS | hiddenFlag);
517 while (cont)
551adc4b 518 {
06cc1fb9 519 fd = new wxFileData(dirPrefix + f, f, wxFileData::is_dir, wxFileIconsTable::folder);
5e673a6a 520 Add(fd, item);
551adc4b 521 item.m_itemId++;
5e673a6a
VS
522 cont = dir.GetNext(&f);
523 }
524
7a82dabc 525 // Tokenize the wildcard string, so we can handle more than 1
5e673a6a
VS
526 // search pattern in a wildcard.
527 wxStringTokenizer tokenWild(m_wild, wxT(";"));
528 while ( tokenWild.HasMoreTokens() )
529 {
7a82dabc 530 cont = dir.GetFirst(&f, tokenWild.GetNextToken(),
5e673a6a
VS
531 wxDIR_FILES | hiddenFlag);
532 while (cont)
533 {
06cc1fb9 534 fd = new wxFileData(dirPrefix + f, f, wxFileData::is_file, wxFileIconsTable::file);
5e673a6a
VS
535 Add(fd, item);
536 item.m_itemId++;
537 cont = dir.GetNext(&f);
538 }
551adc4b 539 }
cae5359f 540 }
cae5359f 541 }
c8c0e54c 542
06cc1fb9 543 SortItems(m_sort_field, m_sort_foward);
8b17ba72
RR
544}
545
0b855868 546void wxFileCtrl::SetWild( const wxString &wild )
8b17ba72 547{
06cc1fb9
JS
548 if (wild.Find(wxT('|')) != wxNOT_FOUND)
549 return;
550
0b855868 551 m_wild = wild;
8f4fcc4e 552 UpdateFiles();
0b855868
RR
553}
554
555void wxFileCtrl::MakeDir()
556{
5e673a6a 557 wxString new_name( _("NewName") );
0b855868 558 wxString path( m_dirName );
75ec8bd4 559 path += wxFILE_SEP_PATH;
0b855868
RR
560 path += new_name;
561 if (wxFileExists(path))
8b17ba72 562 {
0b855868
RR
563 // try NewName0, NewName1 etc.
564 int i = 0;
c8c0e54c 565 do {
0b855868 566 new_name = _("NewName");
c8c0e54c 567 wxString num;
223d09f6 568 num.Printf( wxT("%d"), i );
c8c0e54c
VZ
569 new_name += num;
570
0b855868 571 path = m_dirName;
75ec8bd4 572 path += wxFILE_SEP_PATH;
0b855868 573 path += new_name;
c8c0e54c
VZ
574 i++;
575 } while (wxFileExists(path));
8b17ba72 576 }
c8c0e54c 577
0b855868 578 wxLogNull log;
c8c0e54c 579 if (!wxMkdir(path))
8b17ba72 580 {
0b855868 581 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 582 dialog.ShowModal();
0b855868 583 return;
8b17ba72 584 }
8b17ba72 585
06cc1fb9 586 wxFileData *fd = new wxFileData( path, new_name, wxFileData::is_dir, wxFileIconsTable::folder );
0b855868
RR
587 wxListItem item;
588 item.m_itemId = 0;
589 item.m_col = 0;
13111b2a 590 long id = Add( fd, item );
c8c0e54c 591
0b855868
RR
592 if (id != -1)
593 {
06cc1fb9 594 SortItems(m_sort_field, m_sort_foward);
c8c0e54c 595 id = FindItem( 0, (long)fd );
0b855868
RR
596 EnsureVisible( id );
597 EditLabel( id );
598 }
8b17ba72
RR
599}
600
601void wxFileCtrl::GoToParentDir()
602{
5e673a6a 603 if (!IsTopMostDir(m_dirName))
8b17ba72 604 {
353f41cb 605 size_t len = m_dirName.Len();
083f7497 606 if (wxEndsWithPathSeparator(m_dirName))
353f41cb 607 m_dirName.Remove( len-1, 1 );
c8c0e54c 608 wxString fname( wxFileNameFromPath(m_dirName) );
0b855868 609 m_dirName = wxPathOnly( m_dirName );
23254b13
JS
610#if defined(__DOS__) || defined(__WINDOWS__)
611 if (!m_dirName.IsEmpty())
612 {
613 if (m_dirName.Last() == wxT('.'))
614 m_dirName = wxT("");
615 }
616#elif defined(__UNIX__)
7a82dabc 617 if (m_dirName.IsEmpty())
5e673a6a
VS
618 m_dirName = wxT("/");
619#endif
8f4fcc4e 620 UpdateFiles();
13111b2a 621 long id = FindItem( 0, fname );
c8c0e54c
VZ
622 if (id != -1)
623 {
624 SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
625 EnsureVisible( id );
626 }
8b17ba72
RR
627 }
628}
629
630void wxFileCtrl::GoToHomeDir()
631{
632 wxString s = wxGetUserHome( wxString() );
5e673a6a 633 GoToDir(s);
8b17ba72
RR
634}
635
636void wxFileCtrl::GoToDir( const wxString &dir )
637{
06cc1fb9
JS
638 if (!wxDirExists(dir)) return;
639
8b17ba72 640 m_dirName = dir;
8f4fcc4e 641 UpdateFiles();
e6daf794
RR
642 SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
643 EnsureVisible( 0 );
8b17ba72
RR
644}
645
7a82dabc 646void wxFileCtrl::FreeItemData(const wxListItem& item)
8b17ba72 647{
7a82dabc 648 wxFileData *fd = (wxFileData*)item.m_data;
8b17ba72
RR
649 delete fd;
650}
651
7a82dabc
VZ
652void wxFileCtrl::OnListDeleteItem( wxListEvent &event )
653{
654 FreeItemData(event.m_item);
655}
656
657void wxFileCtrl::FreeAllItemsData()
12c1b46a
RR
658{
659 wxListItem item;
660 item.m_mask = wxLIST_MASK_DATA;
661
662 item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL );
92da8bde 663 while ( item.m_itemId != -1 )
12c1b46a
RR
664 {
665 GetItem( item );
7a82dabc 666 FreeItemData(item);
12c1b46a
RR
667 item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL );
668 }
669}
670
0b855868 671void wxFileCtrl::OnListEndLabelEdit( wxListEvent &event )
8b17ba72
RR
672{
673 wxFileData *fd = (wxFileData*)event.m_item.m_data;
0b855868 674 wxASSERT( fd );
c8c0e54c 675
0b855868
RR
676 if ((event.GetLabel().IsEmpty()) ||
677 (event.GetLabel() == _(".")) ||
678 (event.GetLabel() == _("..")) ||
75ec8bd4 679 (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND))
8b17ba72 680 {
0b855868 681 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 682 dialog.ShowModal();
0b855868 683 event.Veto();
c8c0e54c 684 return;
8b17ba72 685 }
c8c0e54c 686
06cc1fb9 687 wxString new_name( wxPathOnly( fd->GetFilePath() ) );
75ec8bd4 688 new_name += wxFILE_SEP_PATH;
0b855868 689 new_name += event.GetLabel();
c8c0e54c 690
0b855868 691 wxLogNull log;
c8c0e54c 692
0b855868 693 if (wxFileExists(new_name))
8b17ba72 694 {
0b855868 695 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 696 dialog.ShowModal();
0b855868 697 event.Veto();
8b17ba72 698 }
c8c0e54c 699
06cc1fb9 700 if (wxRenameFile(fd->GetFilePath(),new_name))
8b17ba72 701 {
0b855868 702 fd->SetNewName( new_name, event.GetLabel() );
c8c0e54c 703 SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED );
0b855868 704 EnsureVisible( event.GetItem() );
8b17ba72
RR
705 }
706 else
707 {
0b855868 708 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
c8c0e54c 709 dialog.ShowModal();
0b855868 710 event.Veto();
8b17ba72 711 }
8b17ba72
RR
712}
713
23254b13
JS
714void wxFileCtrl::OnListColClick( wxListEvent &event )
715{
716 int col = event.GetColumn();
717
718 switch (col)
719 {
720 case wxFileData::FileList_Name :
06cc1fb9 721 case wxFileData::FileList_Size :
23254b13 722 case wxFileData::FileList_Type :
23254b13
JS
723 case wxFileData::FileList_Time : break;
724 default : return;
725 }
726
727 if ((wxFileData::fileListFieldType)col == m_sort_field)
06cc1fb9 728 m_sort_foward = !m_sort_foward;
23254b13
JS
729 else
730 m_sort_field = (wxFileData::fileListFieldType)col;
731
06cc1fb9 732 SortItems(m_sort_field, m_sort_foward);
23254b13
JS
733}
734
735void wxFileCtrl::SortItems(wxFileData::fileListFieldType field, bool foward)
736{
737 m_sort_field = field;
06cc1fb9
JS
738 m_sort_foward = foward;
739 long sort_dir = foward ? 1 : -1;
23254b13
JS
740
741 switch (m_sort_field)
742 {
743 case wxFileData::FileList_Name :
744 {
06cc1fb9
JS
745 wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataNameCompare, sort_dir);
746 break;
747 }
748 case wxFileData::FileList_Size :
749 {
750 wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataSizeCompare, sort_dir);
23254b13
JS
751 break;
752 }
753 case wxFileData::FileList_Type :
754 {
06cc1fb9 755 wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataTypeCompare, sort_dir);
23254b13
JS
756 break;
757 }
23254b13
JS
758 case wxFileData::FileList_Time :
759 {
06cc1fb9 760 wxListCtrl::SortItems((wxListCtrlCompare)wxFileDataTimeCompare, sort_dir);
23254b13
JS
761 break;
762 }
763 default : break;
764 }
765}
766
7a82dabc
VZ
767wxFileCtrl::~wxFileCtrl()
768{
769 FreeAllItemsData();
770}
771
8b17ba72 772//-----------------------------------------------------------------------------
23254b13 773// wxGenericFileDialog
8b17ba72
RR
774//-----------------------------------------------------------------------------
775
5e673a6a
VS
776#define ID_LIST_MODE (wxID_FILEDLGG )
777#define ID_REPORT_MODE (wxID_FILEDLGG + 1)
778#define ID_UP_DIR (wxID_FILEDLGG + 5)
779#define ID_PARENT_DIR (wxID_FILEDLGG + 6)
780#define ID_NEW_DIR (wxID_FILEDLGG + 7)
781#define ID_CHOICE (wxID_FILEDLGG + 8)
782#define ID_TEXT (wxID_FILEDLGG + 9)
783#define ID_LIST_CTRL (wxID_FILEDLGG + 10)
784#define ID_ACTIVATED (wxID_FILEDLGG + 11)
785#define ID_CHECK (wxID_FILEDLGG + 12)
8b17ba72 786
23254b13
JS
787IMPLEMENT_DYNAMIC_CLASS(wxGenericFileDialog,wxDialog)
788
789BEGIN_EVENT_TABLE(wxGenericFileDialog,wxDialog)
790 EVT_BUTTON(ID_LIST_MODE, wxGenericFileDialog::OnList)
791 EVT_BUTTON(ID_REPORT_MODE, wxGenericFileDialog::OnReport)
792 EVT_BUTTON(ID_UP_DIR, wxGenericFileDialog::OnUp)
793 EVT_BUTTON(ID_PARENT_DIR, wxGenericFileDialog::OnHome)
794 EVT_BUTTON(ID_NEW_DIR, wxGenericFileDialog::OnNew)
795 EVT_BUTTON(wxID_OK, wxGenericFileDialog::OnListOk)
796 EVT_LIST_ITEM_SELECTED(ID_LIST_CTRL, wxGenericFileDialog::OnSelected)
797 EVT_LIST_ITEM_ACTIVATED(ID_LIST_CTRL, wxGenericFileDialog::OnActivated)
798 EVT_CHOICE(ID_CHOICE,wxGenericFileDialog::OnChoiceFilter)
799 EVT_TEXT_ENTER(ID_TEXT,wxGenericFileDialog::OnTextEnter)
800 EVT_TEXT(ID_TEXT,wxGenericFileDialog::OnTextChange)
801 EVT_CHECKBOX(ID_CHECK,wxGenericFileDialog::OnCheck)
8b17ba72
RR
802END_EVENT_TABLE()
803
23254b13
JS
804long wxGenericFileDialog::ms_lastViewStyle = wxLC_LIST;
805bool wxGenericFileDialog::ms_lastShowHidden = FALSE;
655cf310 806
23254b13 807wxGenericFileDialog::wxGenericFileDialog(wxWindow *parent,
9cedab37
VZ
808 const wxString& message,
809 const wxString& defaultDir,
810 const wxString& defaultFile,
811 const wxString& wildCard,
812 long style,
813 const wxPoint& pos )
814 : wxDialog( parent, -1, message, pos, wxDefaultSize,
815 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER )
8b17ba72 816{
eaf40b23
VS
817 if (wxConfig::Get(FALSE))
818 {
7a82dabc 819 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ViewStyle"),
9cedab37 820 &ms_lastViewStyle);
7a82dabc 821 wxConfig::Get()->Read(wxT("/wxWindows/wxFileDialog/ShowHidden"),
9cedab37 822 &ms_lastShowHidden);
eaf40b23
VS
823 }
824
8b17ba72
RR
825 m_message = message;
826 m_dialogStyle = style;
479cd5de 827
9cedab37
VZ
828 if (m_dialogStyle == 0)
829 m_dialogStyle = wxOPEN;
7941ba11 830 if ((m_dialogStyle & wxMULTIPLE ) && !(m_dialogStyle & wxOPEN))
9b00bb16 831 m_dialogStyle |= wxOPEN;
479cd5de 832
8b17ba72 833 m_dir = defaultDir;
353f41cb 834 if ((m_dir.empty()) || (m_dir == wxT(".")))
ac2def68 835 {
75ec8bd4 836 m_dir = wxGetCwd();
ac2def68 837 }
7a82dabc 838
353f41cb 839 size_t len = m_dir.Len();
083f7497 840 if ((len > 1) && (wxEndsWithPathSeparator(m_dir)))
353f41cb
RR
841 m_dir.Remove( len-1, 1 );
842
843 m_path = m_dir;
75ec8bd4 844 m_path += wxFILE_SEP_PATH;
8b17ba72
RR
845 m_path += defaultFile;
846 m_fileName = defaultFile;
847 m_wildCard = wildCard;
848 m_filterIndex = 0;
655cf310 849 m_filterExtension = wxEmptyString;
c8c0e54c 850
cae5359f 851 // interpret wildcards
c8c0e54c 852
cae5359f
RR
853 if (m_wildCard.IsEmpty())
854 m_wildCard = _("All files (*)|*");
c8c0e54c 855
223d09f6 856 wxStringTokenizer tokens( m_wildCard, wxT("|") );
cae5359f
RR
857 wxString firstWild;
858 wxString firstWildText;
859 if (tokens.CountTokens() == 1)
860 {
861 firstWildText = tokens.GetNextToken();
862 firstWild = firstWildText;
863 }
864 else
865 {
223d09f6 866 wxASSERT_MSG( tokens.CountTokens() % 2 == 0, wxT("Wrong file type descripition") );
cae5359f
RR
867 firstWildText = tokens.GetNextToken();
868 firstWild = tokens.GetNextToken();
869 }
655cf310
VS
870 if ( firstWild.Left( 2 ) == wxT("*.") )
871 m_filterExtension = firstWild.Mid( 1 );
2b5f62a0
VZ
872 if ( m_filterExtension == wxT(".*") )
873 m_filterExtension = wxEmptyString;
cae5359f
RR
874
875 // layout
4f5c180e 876
2b5f62a0 877 bool is_pda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
c8c0e54c 878
8b17ba72 879 wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL );
c8c0e54c 880
8b17ba72 881 wxBoxSizer *buttonsizer = new wxBoxSizer( wxHORIZONTAL );
c8c0e54c 882
e6daf794 883 wxBitmapButton *but;
c8c0e54c 884
7a82dabc 885 but = new wxBitmapButton(this, ID_LIST_MODE,
60d2cc25 886 wxArtProvider::GetBitmap(wxART_LIST_VIEW, wxART_CMN_DIALOG));
e6daf794
RR
887#if wxUSE_TOOLTIPS
888 but->SetToolTip( _("View files as a list view") );
889#endif
890 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 891
60d2cc25
VS
892 but = new wxBitmapButton(this, ID_REPORT_MODE,
893 wxArtProvider::GetBitmap(wxART_REPORT_VIEW, wxART_CMN_DIALOG));
e6daf794
RR
894#if wxUSE_TOOLTIPS
895 but->SetToolTip( _("View files as a detailed view") );
896#endif
897 buttonsizer->Add( but, 0, wxALL, 5 );
c8c0e54c 898
0b855868 899 buttonsizer->Add( 30, 5, 1 );
e6daf794 900
06cc1fb9 901 m_upDirButton = new wxBitmapButton(this, ID_UP_DIR,
60d2cc25 902 wxArtProvider::GetBitmap(wxART_GO_DIR_UP, wxART_CMN_DIALOG));
e6daf794 903#if wxUSE_TOOLTIPS
06cc1fb9 904 m_upDirButton->SetToolTip( _("Go to parent directory") );
e6daf794 905#endif
06cc1fb9 906 buttonsizer->Add( m_upDirButton, 0, wxALL, 5 );
c8c0e54c 907
37fd1c97 908#ifndef __DOS__ // VS: Home directory is meaningless in MS-DOS...
60d2cc25
VS
909 but = new wxBitmapButton(this, ID_PARENT_DIR,
910 wxArtProvider::GetBitmap(wxART_GO_HOME, wxART_CMN_DIALOG));
e6daf794
RR
911#if wxUSE_TOOLTIPS
912 but->SetToolTip( _("Go to home directory") );
913#endif
914 buttonsizer->Add( but, 0, wxALL, 5);
c8c0e54c 915
e6daf794 916 buttonsizer->Add( 20, 20 );
75ec8bd4 917#endif //!__DOS__
c8c0e54c 918
06cc1fb9 919 m_newDirButton = new wxBitmapButton(this, ID_NEW_DIR,
60d2cc25 920 wxArtProvider::GetBitmap(wxART_NEW_DIR, wxART_CMN_DIALOG));
e6daf794 921#if wxUSE_TOOLTIPS
06cc1fb9 922 m_newDirButton->SetToolTip( _("Create new directory") );
e6daf794 923#endif
06cc1fb9 924 buttonsizer->Add( m_newDirButton, 0, wxALL, 5 );
c8c0e54c 925
2b5f62a0 926 if (is_pda)
c15521c6
RR
927 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 0 );
928 else
929 mainsizer->Add( buttonsizer, 0, wxALL | wxEXPAND, 5 );
c8c0e54c 930
9c884972 931 wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL );
2b5f62a0 932 if (is_pda)
c15521c6 933 staticsizer->Add( new wxStaticText( this, -1, _("Current directory:") ), 0, wxRIGHT, 10 );
9c884972
RR
934 m_static = new wxStaticText( this, -1, m_dir );
935 staticsizer->Add( m_static, 1 );
936 mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT|wxRIGHT|wxBOTTOM, 10 );
937
cd6b752b 938 long style2 = ms_lastViewStyle | wxSUNKEN_BORDER;
9cedab37 939 if ( !(m_dialogStyle & wxMULTIPLE) )
cd6b752b 940 style2 |= wxLC_SINGLE_SEL;
9cedab37 941
23254b13 942 m_list = new wxFileCtrl( this, ID_LIST_CTRL,
2b5f62a0 943 firstWild, ms_lastShowHidden,
9cedab37 944 wxDefaultPosition, wxSize(540,200),
cd6b752b 945 style2);
9cedab37 946
2b5f62a0 947 if (is_pda)
c15521c6 948 {
41fecb44
VS
949 // PDAs have a different screen layout
950 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 5 );
951
952 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
953 m_choice = new wxChoice( this, ID_CHOICE );
954 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 5 );
955 mainsizer->Add( choicesizer, 0, wxEXPAND );
956
957 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
958 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
959 textsizer->Add( m_text, 1, wxCENTER | wxALL, 5 );
960 mainsizer->Add( textsizer, 0, wxEXPAND );
961
962 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
9cedab37 963 m_check->SetValue( ms_lastShowHidden );
41fecb44
VS
964 textsizer->Add( m_check, 0, wxCENTER|wxALL, 5 );
965
966 buttonsizer = new wxBoxSizer( wxHORIZONTAL );
967 buttonsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxALL, 5 );
968 buttonsizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 5 );
969 mainsizer->Add( buttonsizer, 0, wxALIGN_RIGHT );
c15521c6
RR
970 }
971 else
972 {
41fecb44
VS
973 mainsizer->Add( m_list, 1, wxEXPAND | wxLEFT|wxRIGHT, 10 );
974
975 wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL );
976 m_text = new wxTextCtrl( this, ID_TEXT, m_fileName, wxDefaultPosition, wxDefaultSize, wxPROCESS_ENTER );
977 textsizer->Add( m_text, 1, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
978 textsizer->Add( new wxButton( this, wxID_OK, _("OK") ), 0, wxCENTER | wxLEFT|wxRIGHT|wxTOP, 10 );
979 mainsizer->Add( textsizer, 0, wxEXPAND );
980
981 wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL );
982 m_choice = new wxChoice( this, ID_CHOICE );
983 choicesizer->Add( m_choice, 1, wxCENTER|wxALL, 10 );
984 m_check = new wxCheckBox( this, ID_CHECK, _("Show hidden files") );
9cedab37 985 m_check->SetValue( ms_lastShowHidden );
41fecb44
VS
986 choicesizer->Add( m_check, 0, wxCENTER|wxALL, 10 );
987 choicesizer->Add( new wxButton( this, wxID_CANCEL, _("Cancel") ), 0, wxCENTER | wxALL, 10 );
988 mainsizer->Add( choicesizer, 0, wxEXPAND );
c15521c6 989 }
24f932d2 990
cae5359f
RR
991 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
992 while (tokens.HasMoreTokens())
993 {
994 firstWildText = tokens.GetNextToken();
995 firstWild = tokens.GetNextToken();
996 m_choice->Append( firstWildText, (void*) new wxString( firstWild ) );
997 }
998 m_choice->SetSelection( 0 );
8b17ba72
RR
999
1000 SetAutoLayout( TRUE );
1001 SetSizer( mainsizer );
c8c0e54c 1002
8b17ba72
RR
1003 mainsizer->Fit( this );
1004 mainsizer->SetSizeHints( this );
48fe8374 1005
8b17ba72 1006 Centre( wxBOTH );
ed58dbea 1007
9cedab37 1008 m_text->SetFocus();
8b17ba72
RR
1009}
1010
23254b13 1011wxGenericFileDialog::~wxGenericFileDialog()
cae5359f 1012{
eaf40b23
VS
1013 if (wxConfig::Get(FALSE))
1014 {
5e673a6a 1015 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ViewStyle"),
9cedab37 1016 ms_lastViewStyle);
5e673a6a 1017 wxConfig::Get()->Write(wxT("/wxWindows/wxFileDialog/ShowHidden"),
9cedab37 1018 ms_lastShowHidden);
eaf40b23 1019 }
c5ef41d3
VZ
1020
1021 const int count = m_choice->GetCount();
1022 for ( int i = 0; i < count; i++ )
1023 {
1024 delete (wxString *)m_choice->GetClientData(i);
1025 }
cae5359f
RR
1026}
1027
23254b13 1028int wxGenericFileDialog::ShowModal()
9cedab37
VZ
1029{
1030 m_list->GoToDir(m_dir);
06cc1fb9 1031 UpdateControls();
2b5f62a0 1032 m_text->SetValue(m_fileName);
9cedab37
VZ
1033
1034 return wxDialog::ShowModal();
1035}
1036
23254b13 1037void wxGenericFileDialog::DoSetFilterIndex(int filterindex)
2b5f62a0
VZ
1038{
1039 wxString *str = (wxString*) m_choice->GetClientData( filterindex );
1040 m_list->SetWild( *str );
1041 m_filterIndex = filterindex;
1042 if ( str->Left(2) == wxT("*.") )
1043 {
787d22ec
JS
1044 m_filterExtension = str->Mid(1);
1045 if (m_filterExtension == _T(".*"))
2b5f62a0
VZ
1046 m_filterExtension.clear();
1047 }
1048 else
1049 {
1050 m_filterExtension.clear();
1051 }
1052}
1053
23254b13 1054void wxGenericFileDialog::SetFilterIndex( int filterindex )
04ea7f93
RR
1055{
1056 m_choice->SetSelection( filterindex );
2b5f62a0
VZ
1057
1058 DoSetFilterIndex(filterindex);
04ea7f93
RR
1059}
1060
23254b13 1061void wxGenericFileDialog::OnChoiceFilter( wxCommandEvent &event )
cae5359f 1062{
2b5f62a0 1063 DoSetFilterIndex((int)event.GetInt());
cae5359f
RR
1064}
1065
23254b13 1066void wxGenericFileDialog::OnCheck( wxCommandEvent &event )
bf9e3e73 1067{
9cedab37 1068 m_list->ShowHidden( (ms_lastShowHidden = event.GetInt() != 0) );
bf9e3e73
RR
1069}
1070
23254b13 1071void wxGenericFileDialog::OnActivated( wxListEvent &event )
8b17ba72 1072{
e1811a01 1073 HandleAction( event.m_item.m_text );
8b17ba72
RR
1074}
1075
23254b13 1076void wxGenericFileDialog::OnTextEnter( wxCommandEvent &WXUNUSED(event) )
cae5359f
RR
1077{
1078 wxCommandEvent cevent(wxEVT_COMMAND_BUTTON_CLICKED, wxID_OK);
1079 cevent.SetEventObject( this );
1080 GetEventHandler()->ProcessEvent( cevent );
1081}
1082
df413171
JS
1083static bool ignoreChanges = FALSE;
1084
23254b13 1085void wxGenericFileDialog::OnTextChange( wxCommandEvent &WXUNUSED(event) )
df413171
JS
1086{
1087 if (!ignoreChanges)
1088 {
1089 // Clear selections. Otherwise when the user types in a value they may
1090 // not get the file whose name they typed.
1091 if (m_list->GetSelectedItemCount() > 0)
1092 {
7f04165e 1093 long item = m_list->GetNextItem(-1, wxLIST_NEXT_ALL,
df413171
JS
1094 wxLIST_STATE_SELECTED);
1095 while ( item != -1 )
7f04165e 1096 {
df413171
JS
1097 m_list->SetItemState(item,0, wxLIST_STATE_SELECTED);
1098 item = m_list->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
7f04165e 1099 }
df413171
JS
1100 }
1101 }
1102}
1103
23254b13 1104void wxGenericFileDialog::OnSelected( wxListEvent &event )
8b17ba72 1105{
e1811a01 1106 wxString filename( event.m_item.m_text );
223d09f6 1107 if (filename == wxT("..")) return;
479cd5de 1108
06cc1fb9 1109 wxString dir = m_list->GetDir();
5e673a6a
VS
1110 if (!IsTopMostDir(dir))
1111 dir += wxFILE_SEP_PATH;
e1811a01
RR
1112 dir += filename;
1113 if (wxDirExists(dir)) return;
479cd5de 1114
df413171 1115 ignoreChanges = TRUE;
e1811a01 1116 m_text->SetValue( filename );
df413171 1117 ignoreChanges = FALSE;
8b17ba72
RR
1118}
1119
23254b13 1120void wxGenericFileDialog::HandleAction( const wxString &fn )
8b17ba72 1121{
e1811a01 1122 wxString filename( fn );
06cc1fb9 1123 wxString dir = m_list->GetDir();
8b17ba72 1124 if (filename.IsEmpty()) return;
223d09f6 1125 if (filename == wxT(".")) return;
c8c0e54c 1126
223d09f6 1127 if (filename == wxT(".."))
0b855868
RR
1128 {
1129 m_list->GoToParentDir();
1130 m_list->SetFocus();
06cc1fb9 1131 UpdateControls();
c8c0e54c 1132 return;
0b855868
RR
1133 }
1134
75ec8bd4 1135#ifdef __UNIX__
223d09f6 1136 if (filename == wxT("~"))
ed58dbea
RR
1137 {
1138 m_list->GoToHomeDir();
1139 m_list->SetFocus();
06cc1fb9 1140 UpdateControls();
c8c0e54c 1141 return;
ed58dbea 1142 }
c8c0e54c 1143
f6bcfd97 1144 if (filename[0u] == wxT('~'))
ed58dbea
RR
1145 {
1146 filename.Remove( 0, 1 );
c8c0e54c 1147 wxString tmp( wxGetUserHome() );
223d09f6 1148 tmp += wxT('/');
c8c0e54c
VZ
1149 tmp += filename;
1150 filename = tmp;
ed58dbea 1151 }
75ec8bd4 1152#endif // __UNIX__
ed58dbea 1153
223d09f6
KB
1154 if ((filename.Find(wxT('*')) != wxNOT_FOUND) ||
1155 (filename.Find(wxT('?')) != wxNOT_FOUND))
cae5359f 1156 {
75ec8bd4 1157 if (filename.Find(wxFILE_SEP_PATH) != wxNOT_FOUND)
c8c0e54c
VZ
1158 {
1159 wxMessageBox(_("Illegal file specification."), _("Error"), wxOK | wxICON_ERROR );
1160 return;
1161 }
1162 m_list->SetWild( filename );
1163 return;
cae5359f
RR
1164 }
1165
5e673a6a
VS
1166 if (!IsTopMostDir(dir))
1167 dir += wxFILE_SEP_PATH;
1168 if (!wxIsAbsolutePath(filename))
ed58dbea
RR
1169 {
1170 dir += filename;
1171 filename = dir;
1172 }
c8c0e54c 1173
8b17ba72
RR
1174 if (wxDirExists(filename))
1175 {
1176 m_list->GoToDir( filename );
06cc1fb9 1177 UpdateControls();
c8c0e54c 1178 return;
8b17ba72 1179 }
c8c0e54c 1180
2b5f62a0
VZ
1181 // append the default extension to the filename if it doesn't have any
1182 //
1183 // VZ: the logic of testing for !wxFileExists() only for the open file
1184 // dialog is not entirely clear to me, why don't we allow saving to a
1185 // file without extension as well?
1186 if ( !(m_dialogStyle & wxOPEN) || !wxFileExists(filename) )
8b17ba72 1187 {
2b5f62a0
VZ
1188 wxString ext;
1189 wxSplitPath(filename, NULL, NULL, &ext);
1190 if ( ext.empty() )
8b17ba72 1191 {
2b5f62a0
VZ
1192 // append the first extension of the filter string
1193 filename += m_filterExtension.BeforeFirst(_T(';'));
8b17ba72
RR
1194 }
1195 }
2b5f62a0
VZ
1196
1197 // check that the file [doesn't] exist if necessary
1198 if ( (m_dialogStyle & wxSAVE) &&
1199 (m_dialogStyle & wxOVERWRITE_PROMPT) &&
1200 wxFileExists( filename ) )
8b17ba72 1201 {
2b5f62a0
VZ
1202 wxString msg;
1203 msg.Printf( _("File '%s' already exists, do you really want to "
1204 "overwrite it?"), filename.c_str() );
655cf310 1205
2b5f62a0
VZ
1206 if (wxMessageBox(msg, _("Confirm"), wxYES_NO) != wxYES)
1207 return;
1208 }
1209 else if ( (m_dialogStyle & wxOPEN) &&
1210 (m_dialogStyle & wxFILE_MUST_EXIST) &&
1211 !wxFileExists(filename) )
1212 {
1213 wxMessageBox(_("Please choose an existing file."), _("Error"),
1214 wxOK | wxICON_ERROR );
8b17ba72
RR
1215 }
1216
1217 SetPath( filename );
479cd5de 1218
3f6638b8 1219 // change to the directory where the user went if asked
fda09b3f 1220 if ( m_dialogStyle & wxCHANGE_DIR )
3f6638b8
VZ
1221 {
1222 wxString cwd;
1223 wxSplitPath(filename, &cwd, NULL, NULL);
1224
1225 if ( cwd != wxGetWorkingDirectory() )
1226 {
1227 wxSetWorkingDirectory(cwd);
1228 }
1229 }
1230
9b7e522a
RR
1231 wxCommandEvent event;
1232 wxDialog::OnOK(event);
e1811a01
RR
1233}
1234
23254b13 1235void wxGenericFileDialog::OnListOk( wxCommandEvent &WXUNUSED(event) )
e1811a01
RR
1236{
1237 HandleAction( m_text->GetValue() );
8b17ba72
RR
1238}
1239
23254b13 1240void wxGenericFileDialog::OnList( wxCommandEvent &WXUNUSED(event) )
8b17ba72
RR
1241{
1242 m_list->ChangeToListMode();
9cedab37 1243 ms_lastViewStyle = wxLC_LIST;
0b855868 1244 m_list->SetFocus();
8b17ba72
RR
1245}
1246
23254b13 1247void wxGenericFileDialog::OnReport( wxCommandEvent &WXUNUSED(event) )
8b17ba72
RR
1248{
1249 m_list->ChangeToReportMode();
9cedab37 1250 ms_lastViewStyle = wxLC_REPORT;
0b855868 1251 m_list->SetFocus();
8b17ba72
RR
1252}
1253
23254b13 1254void wxGenericFileDialog::OnUp( wxCommandEvent &WXUNUSED(event) )
8b17ba72
RR
1255{
1256 m_list->GoToParentDir();
0b855868 1257 m_list->SetFocus();
06cc1fb9 1258 UpdateControls();
8b17ba72
RR
1259}
1260
23254b13 1261void wxGenericFileDialog::OnHome( wxCommandEvent &WXUNUSED(event) )
8b17ba72
RR
1262{
1263 m_list->GoToHomeDir();
0b855868 1264 m_list->SetFocus();
06cc1fb9 1265 UpdateControls();
0b855868
RR
1266}
1267
23254b13 1268void wxGenericFileDialog::OnNew( wxCommandEvent &WXUNUSED(event) )
0b855868
RR
1269{
1270 m_list->MakeDir();
8b17ba72
RR
1271}
1272
23254b13 1273void wxGenericFileDialog::SetPath( const wxString& path )
8b17ba72
RR
1274{
1275 // not only set the full path but also update filename and dir
1276 m_path = path;
9cedab37 1277 if ( !path.empty() )
8b17ba72
RR
1278 {
1279 wxString ext;
1280 wxSplitPath(path, &m_dir, &m_fileName, &ext);
9cedab37 1281 if (!ext.empty())
c8c0e54c 1282 {
223d09f6 1283 m_fileName += wxT(".");
8b17ba72 1284 m_fileName += ext;
c8c0e54c 1285 }
8b17ba72
RR
1286 }
1287}
c8c0e54c 1288
23254b13 1289void wxGenericFileDialog::GetPaths( wxArrayString& paths ) const
7941ba11
RR
1290{
1291 paths.Empty();
28c9c76e
RR
1292 if (m_list->GetSelectedItemCount() == 0)
1293 {
1294 paths.Add( GetPath() );
1295 return;
1296 }
479cd5de 1297
7941ba11
RR
1298 paths.Alloc( m_list->GetSelectedItemCount() );
1299
06cc1fb9 1300 wxString dir = m_list->GetDir();
5e673a6a 1301#ifdef __UNIX__
7a82dabc 1302 if (dir != wxT("/"))
5e673a6a
VS
1303#endif
1304 dir += wxFILE_SEP_PATH;
7941ba11
RR
1305
1306 wxListItem item;
1307 item.m_mask = wxLIST_MASK_TEXT;
1308
1309 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
92da8bde 1310 while ( item.m_itemId != -1 )
68df5777 1311 {
7941ba11
RR
1312 m_list->GetItem( item );
1313 paths.Add( dir + item.m_text );
68df5777 1314 item.m_itemId = m_list->GetNextItem( item.m_itemId,
7941ba11
RR
1315 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1316 }
1317}
1318
23254b13 1319void wxGenericFileDialog::GetFilenames(wxArrayString& files) const
7941ba11
RR
1320{
1321 files.Empty();
28c9c76e
RR
1322 if (m_list->GetSelectedItemCount() == 0)
1323 {
1324 files.Add( GetFilename() );
1325 return;
1326 }
7941ba11
RR
1327 files.Alloc( m_list->GetSelectedItemCount() );
1328
1329 wxListItem item;
1330 item.m_mask = wxLIST_MASK_TEXT;
1331
1332 item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
92da8bde 1333 while ( item.m_itemId != -1 )
68df5777 1334 {
7941ba11
RR
1335 m_list->GetItem( item );
1336 files.Add( item.m_text );
68df5777 1337 item.m_itemId = m_list->GetNextItem( item.m_itemId,
7941ba11
RR
1338 wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED );
1339 }
1340}
1341
06cc1fb9
JS
1342void wxGenericFileDialog::UpdateControls()
1343{
1344 wxString dir = m_list->GetDir();
1345 m_static->SetLabel(dir);
1346
1347 bool enable = !IsTopMostDir(dir);
1348 m_upDirButton->Enable(enable);
1349
1350#if defined(__DOS__) || defined(__WINDOWS__)
1351 m_newDirButton->Enable(enable);
1352#endif // defined(__DOS__) || defined(__WINDOWS__)
1353}
1354
23254b13 1355#ifdef USE_GENERIC_FILEDIALOG
655cf310 1356
23254b13 1357IMPLEMENT_DYNAMIC_CLASS(wxFileDialog, wxGenericFileDialog);
655cf310 1358
8b17ba72
RR
1359// ----------------------------------------------------------------------------
1360// global functions
1361// ----------------------------------------------------------------------------
1362
ce9462d2
VZ
1363// common part of both wxFileSelectorEx() and wxFileSelector()
1364static wxString
1365DoSelectFile(const wxChar *title,
1366 const wxChar *defaultDir,
1367 const wxChar *defaultFileName,
1368 const wxChar *defaultExtension,
1369 int *indexDefaultExtension,
1370 const wxChar *filter,
1371 int flags,
1372 wxWindow *parent,
1373 int x,
1374 int y)
1375{
1376 // the filter may be either given explicitly or created automatically from
1377 // the default extension
1378 wxString filterReal;
1379 if ( filter )
1380 {
1381 // the user has specified the filter explicitly, use it
1382 filterReal = filter;
1383 }
1384 else if ( !wxIsEmpty(defaultExtension) )
1385 {
1386 // create the filter to match the given extension
1387 filterReal << wxT("*.") << defaultExtension;
1388 }
1389
1390 wxFileDialog fileDialog(parent,
1391 title,
1392 defaultDir,
1393 defaultFileName,
1394 filterReal,
1395 flags,
1396 wxPoint(x, y));
1397
1398 wxString path;
1399 if ( fileDialog.ShowModal() == wxID_OK )
1400 {
1401 path = fileDialog.GetPath();
1402 if ( indexDefaultExtension )
1403 {
1404 *indexDefaultExtension = fileDialog.GetFilterIndex();
1405 }
1406 }
1407
1408 return path;
1409}
1410
8b17ba72 1411wxString
ce9462d2
VZ
1412wxFileSelectorEx(const wxChar *title,
1413 const wxChar *defaultDir,
1414 const wxChar *defaultFileName,
1415 int *indexDefaultExtension,
1416 const wxChar *filter,
8b17ba72
RR
1417 int flags,
1418 wxWindow *parent,
ce9462d2
VZ
1419 int x,
1420 int y)
8b17ba72 1421{
ce9462d2
VZ
1422 return DoSelectFile(title,
1423 defaultDir,
1424 defaultFileName,
1425 wxT(""), // def ext determined by index
1426 indexDefaultExtension,
1427 filter,
1428 flags,
1429 parent,
1430 x,
1431 y);
8b17ba72
RR
1432}
1433
ce9462d2
VZ
1434wxString
1435wxFileSelector(const wxChar *title,
1436 const wxChar *defaultDir,
1437 const wxChar *defaultFileName,
1438 const wxChar *defaultExtension,
1439 const wxChar *filter,
1440 int flags,
1441 wxWindow *parent,
1442 int x,
1443 int y)
8b17ba72 1444{
ce9462d2
VZ
1445 return DoSelectFile(title,
1446 defaultDir,
1447 defaultFileName,
1448 defaultExtension,
1449 NULL, // not interested in filter index
1450 filter,
1451 flags,
1452 parent,
1453 x,
1454 y);
8b17ba72
RR
1455}
1456
2645b45a 1457static wxString GetWildcardString(const wxChar *ext)
8b17ba72 1458{
2645b45a
VZ
1459 wxString wild;
1460 if ( ext )
1461 {
1462 if ( *ext == wxT('.') )
1463 ext++;
8b17ba72 1464
2645b45a
VZ
1465 wild << _T("*.") << ext;
1466 }
1467 else // no extension specified
1468 {
1469 wild = wxFileSelectorDefaultWildcardStr;
1470 }
8b17ba72 1471
2645b45a 1472 return wild;
8b17ba72
RR
1473}
1474
2645b45a
VZ
1475wxString wxLoadFileSelector(const wxChar *what,
1476 const wxChar *ext,
1477 const wxChar *nameDef,
1478 wxWindow *parent)
8b17ba72 1479{
2645b45a
VZ
1480 wxString prompt;
1481 if ( what && *what )
1482 prompt = wxString::Format(_("Load %s file"), what);
1483 else
1484 prompt = _("Load file");
8b17ba72 1485
2645b45a
VZ
1486 return wxFileSelector(prompt, NULL, nameDef, ext,
1487 GetWildcardString(ext), 0, parent);
8b17ba72
RR
1488}
1489
2645b45a
VZ
1490wxString wxSaveFileSelector(const wxChar *what,
1491 const wxChar *ext,
1492 const wxChar *nameDef,
1493 wxWindow *parent)
1494{
1495 wxString prompt;
1496 if ( what && *what )
1497 prompt = wxString::Format(_("Save %s file"), what);
1498 else
1499 prompt = _("Save file");
655cf310 1500
2645b45a
VZ
1501 return wxFileSelector(prompt, NULL, nameDef, ext,
1502 GetWildcardString(ext), 0, parent);
1503}
655cf310 1504
23254b13
JS
1505#endif // USE_GENERIC_FILEDIALOG
1506
1e6feb95 1507#endif // wxUSE_FILEDLG
9cedab37 1508