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