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