]>
Commit | Line | Data |
---|---|---|
0cf3e587 VZ |
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 ); | |
260020e3 | 227 | m_type |= S_ISLNK(buff.st_mode) ? is_link : 0; |
0cf3e587 VZ |
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 | //----------------------------------------------------------------------------- | |
16a6b53d | 393 | // wxFileListCtrl |
0cf3e587 VZ |
394 | //----------------------------------------------------------------------------- |
395 | ||
260020e3 | 396 | // FIXME: what is this for? It's never read |
0cf3e587 VZ |
397 | static bool ignoreChanges = false; |
398 | ||
16a6b53d | 399 | IMPLEMENT_DYNAMIC_CLASS(wxFileListCtrl,wxListCtrl) |
0cf3e587 | 400 | |
16a6b53d VZ |
401 | BEGIN_EVENT_TABLE(wxFileListCtrl,wxListCtrl) |
402 | EVT_LIST_DELETE_ITEM(wxID_ANY, wxFileListCtrl::OnListDeleteItem) | |
403 | EVT_LIST_DELETE_ALL_ITEMS(wxID_ANY, wxFileListCtrl::OnListDeleteAllItems) | |
404 | EVT_LIST_END_LABEL_EDIT(wxID_ANY, wxFileListCtrl::OnListEndLabelEdit) | |
405 | EVT_LIST_COL_CLICK(wxID_ANY, wxFileListCtrl::OnListColClick) | |
0cf3e587 VZ |
406 | END_EVENT_TABLE() |
407 | ||
408 | ||
16a6b53d | 409 | wxFileListCtrl::wxFileListCtrl() |
0cf3e587 VZ |
410 | { |
411 | m_showHidden = false; | |
260020e3 | 412 | m_sort_forward = true; |
0cf3e587 VZ |
413 | m_sort_field = wxFileData::FileList_Name; |
414 | } | |
415 | ||
16a6b53d | 416 | wxFileListCtrl::wxFileListCtrl(wxWindow *win, |
0cf3e587 VZ |
417 | wxWindowID id, |
418 | const wxString& wild, | |
419 | bool showHidden, | |
420 | const wxPoint& pos, | |
421 | const wxSize& size, | |
422 | long style, | |
423 | const wxValidator &validator, | |
424 | const wxString &name) | |
425 | : wxListCtrl(win, id, pos, size, style, validator, name), | |
426 | m_wild(wild) | |
427 | { | |
428 | wxImageList *imageList = wxTheFileIconsTable->GetSmallImageList(); | |
429 | ||
430 | SetImageList( imageList, wxIMAGE_LIST_SMALL ); | |
431 | ||
432 | m_showHidden = showHidden; | |
433 | ||
260020e3 | 434 | m_sort_forward = true; |
0cf3e587 VZ |
435 | m_sort_field = wxFileData::FileList_Name; |
436 | ||
437 | m_dirName = wxT("*"); | |
438 | ||
439 | if (style & wxLC_REPORT) | |
440 | ChangeToReportMode(); | |
441 | } | |
442 | ||
16a6b53d | 443 | void wxFileListCtrl::ChangeToListMode() |
0cf3e587 VZ |
444 | { |
445 | ClearAll(); | |
446 | SetSingleStyle( wxLC_LIST ); | |
447 | UpdateFiles(); | |
448 | } | |
449 | ||
16a6b53d | 450 | void wxFileListCtrl::ChangeToReportMode() |
0cf3e587 VZ |
451 | { |
452 | ClearAll(); | |
453 | SetSingleStyle( wxLC_REPORT ); | |
454 | ||
455 | // do this since WIN32 does mm/dd/yy UNIX does mm/dd/yyyy | |
456 | // don't hardcode since mm/dd is dd/mm elsewhere | |
457 | int w, h; | |
458 | wxDateTime dt(22, wxDateTime::Dec, 2002, 22, 22, 22); | |
459 | wxString txt = dt.FormatDate() + wxT("22") + dt.Format(wxT("%I:%M:%S %p")); | |
460 | GetTextExtent(txt, &w, &h); | |
461 | ||
462 | InsertColumn( 0, _("Name"), wxLIST_FORMAT_LEFT, w ); | |
463 | InsertColumn( 1, _("Size"), wxLIST_FORMAT_LEFT, w/2 ); | |
464 | InsertColumn( 2, _("Type"), wxLIST_FORMAT_LEFT, w/2 ); | |
465 | InsertColumn( 3, _("Modified"), wxLIST_FORMAT_LEFT, w ); | |
466 | #if defined(__UNIX__) | |
467 | GetTextExtent(wxT("Permissions 2"), &w, &h); | |
468 | InsertColumn( 4, _("Permissions"), wxLIST_FORMAT_LEFT, w ); | |
469 | #elif defined(__WIN32__) | |
470 | GetTextExtent(wxT("Attributes 2"), &w, &h); | |
471 | InsertColumn( 4, _("Attributes"), wxLIST_FORMAT_LEFT, w ); | |
472 | #endif | |
473 | ||
474 | UpdateFiles(); | |
475 | } | |
476 | ||
16a6b53d | 477 | void wxFileListCtrl::ChangeToSmallIconMode() |
0cf3e587 VZ |
478 | { |
479 | ClearAll(); | |
480 | SetSingleStyle( wxLC_SMALL_ICON ); | |
481 | UpdateFiles(); | |
482 | } | |
483 | ||
16a6b53d | 484 | void wxFileListCtrl::ShowHidden( bool show ) |
0cf3e587 VZ |
485 | { |
486 | m_showHidden = show; | |
487 | UpdateFiles(); | |
488 | } | |
489 | ||
16a6b53d | 490 | long wxFileListCtrl::Add( wxFileData *fd, wxListItem &item ) |
0cf3e587 VZ |
491 | { |
492 | long ret = -1; | |
493 | item.m_mask = wxLIST_MASK_TEXT + wxLIST_MASK_DATA + wxLIST_MASK_IMAGE; | |
494 | fd->MakeItem( item ); | |
495 | long my_style = GetWindowStyleFlag(); | |
496 | if (my_style & wxLC_REPORT) | |
497 | { | |
498 | ret = InsertItem( item ); | |
499 | for (int i = 1; i < wxFileData::FileList_Max; i++) | |
500 | SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) ); | |
501 | } | |
502 | else if ((my_style & wxLC_LIST) || (my_style & wxLC_SMALL_ICON)) | |
503 | { | |
504 | ret = InsertItem( item ); | |
505 | } | |
506 | return ret; | |
507 | } | |
508 | ||
16a6b53d | 509 | void wxFileListCtrl::UpdateItem(const wxListItem &item) |
0cf3e587 VZ |
510 | { |
511 | wxFileData *fd = (wxFileData*)GetItemData(item); | |
512 | wxCHECK_RET(fd, wxT("invalid filedata")); | |
513 | ||
514 | fd->ReadData(); | |
515 | ||
516 | SetItemText(item, fd->GetFileName()); | |
517 | SetItemImage(item, fd->GetImageId()); | |
518 | ||
519 | if (GetWindowStyleFlag() & wxLC_REPORT) | |
520 | { | |
521 | for (int i = 1; i < wxFileData::FileList_Max; i++) | |
522 | SetItem( item.m_itemId, i, fd->GetEntry((wxFileData::fileListFieldType)i) ); | |
523 | } | |
524 | } | |
525 | ||
16a6b53d | 526 | void wxFileListCtrl::UpdateFiles() |
0cf3e587 VZ |
527 | { |
528 | // don't do anything before ShowModal() call which sets m_dirName | |
529 | if ( m_dirName == wxT("*") ) | |
530 | return; | |
531 | ||
532 | wxBusyCursor bcur; // this may take a while... | |
533 | ||
534 | DeleteAllItems(); | |
535 | ||
536 | wxListItem item; | |
537 | item.m_itemId = 0; | |
538 | item.m_col = 0; | |
539 | ||
540 | #if (defined(__WINDOWS__) || defined(__DOS__) || defined(__WXMAC__) || defined(__OS2__)) && !defined(__WXWINCE__) | |
541 | if ( IsTopMostDir(m_dirName) ) | |
542 | { | |
543 | wxArrayString names, paths; | |
544 | wxArrayInt icons; | |
e8548d99 | 545 | const size_t count = wxGetAvailableDrives(paths, names, icons); |
0cf3e587 | 546 | |
e8548d99 | 547 | for ( size_t n = 0; n < count; n++ ) |
0cf3e587 | 548 | { |
e8548d99 VZ |
549 | // use paths[n] as the drive name too as our HandleAction() can't |
550 | // deal with the drive names (of the form "System (C:)") currently | |
551 | // as it mistakenly treats them as file names | |
552 | // | |
553 | // it would be preferable to show names, and not paths, in the | |
554 | // dialog just as the native dialog does but for this we must: | |
555 | // a) store the item type as item data and modify HandleAction() | |
556 | // to use it instead of wxDirExists() to check whether the item | |
557 | // is a directory | |
558 | // b) store the drives by their drive letters and not their | |
559 | // descriptions as otherwise it's pretty confusing to the user | |
560 | wxFileData *fd = new wxFileData(paths[n], paths[n], | |
561 | wxFileData::is_drive, icons[n]); | |
0cf3e587 VZ |
562 | if (Add(fd, item) != -1) |
563 | item.m_itemId++; | |
564 | else | |
565 | delete fd; | |
566 | } | |
567 | } | |
568 | else | |
569 | #endif // defined(__DOS__) || defined(__WINDOWS__) | |
570 | { | |
571 | // Real directory... | |
572 | if ( !IsTopMostDir(m_dirName) && !m_dirName.empty() ) | |
573 | { | |
574 | wxString p(wxPathOnly(m_dirName)); | |
575 | #if (defined(__UNIX__) || defined(__WXWINCE__)) && !defined(__OS2__) | |
576 | if (p.empty()) p = wxT("/"); | |
577 | #endif // __UNIX__ | |
578 | wxFileData *fd = new wxFileData(p, wxT(".."), wxFileData::is_dir, wxFileIconsTable::folder); | |
579 | if (Add(fd, item) != -1) | |
580 | item.m_itemId++; | |
581 | else | |
582 | delete fd; | |
583 | } | |
584 | ||
585 | wxString dirname(m_dirName); | |
586 | #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__) | |
587 | if (dirname.length() == 2 && dirname[1u] == wxT(':')) | |
588 | dirname << wxT('\\'); | |
589 | #endif // defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__) | |
590 | ||
591 | if (dirname.empty()) | |
592 | dirname = wxFILE_SEP_PATH; | |
593 | ||
594 | wxLogNull logNull; | |
595 | wxDir dir(dirname); | |
596 | ||
597 | if ( dir.IsOpened() ) | |
598 | { | |
599 | wxString dirPrefix(dirname); | |
600 | if (dirPrefix.Last() != wxFILE_SEP_PATH) | |
601 | dirPrefix += wxFILE_SEP_PATH; | |
602 | ||
603 | int hiddenFlag = m_showHidden ? wxDIR_HIDDEN : 0; | |
604 | ||
605 | bool cont; | |
606 | wxString f; | |
607 | ||
608 | // Get the directories first (not matched against wildcards): | |
609 | cont = dir.GetFirst(&f, wxEmptyString, wxDIR_DIRS | hiddenFlag); | |
610 | while (cont) | |
611 | { | |
612 | wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_dir, wxFileIconsTable::folder); | |
613 | if (Add(fd, item) != -1) | |
614 | item.m_itemId++; | |
615 | else | |
616 | delete fd; | |
617 | ||
618 | cont = dir.GetNext(&f); | |
619 | } | |
620 | ||
621 | // Tokenize the wildcard string, so we can handle more than 1 | |
622 | // search pattern in a wildcard. | |
623 | wxStringTokenizer tokenWild(m_wild, wxT(";")); | |
624 | while ( tokenWild.HasMoreTokens() ) | |
625 | { | |
626 | cont = dir.GetFirst(&f, tokenWild.GetNextToken(), | |
627 | wxDIR_FILES | hiddenFlag); | |
628 | while (cont) | |
629 | { | |
630 | wxFileData *fd = new wxFileData(dirPrefix + f, f, wxFileData::is_file, wxFileIconsTable::file); | |
631 | if (Add(fd, item) != -1) | |
632 | item.m_itemId++; | |
633 | else | |
634 | delete fd; | |
635 | ||
636 | cont = dir.GetNext(&f); | |
637 | } | |
638 | } | |
639 | } | |
640 | } | |
641 | ||
260020e3 | 642 | SortItems(m_sort_field, m_sort_forward); |
0cf3e587 VZ |
643 | } |
644 | ||
16a6b53d | 645 | void wxFileListCtrl::SetWild( const wxString &wild ) |
0cf3e587 VZ |
646 | { |
647 | if (wild.Find(wxT('|')) != wxNOT_FOUND) | |
648 | return; | |
649 | ||
650 | m_wild = wild; | |
651 | UpdateFiles(); | |
652 | } | |
653 | ||
16a6b53d | 654 | void wxFileListCtrl::MakeDir() |
0cf3e587 VZ |
655 | { |
656 | wxString new_name( _("NewName") ); | |
657 | wxString path( m_dirName ); | |
658 | path += wxFILE_SEP_PATH; | |
659 | path += new_name; | |
660 | if (wxFileExists(path)) | |
661 | { | |
662 | // try NewName0, NewName1 etc. | |
663 | int i = 0; | |
664 | do { | |
665 | new_name = _("NewName"); | |
666 | wxString num; | |
667 | num.Printf( wxT("%d"), i ); | |
668 | new_name += num; | |
669 | ||
670 | path = m_dirName; | |
671 | path += wxFILE_SEP_PATH; | |
672 | path += new_name; | |
673 | i++; | |
674 | } while (wxFileExists(path)); | |
675 | } | |
676 | ||
677 | wxLogNull log; | |
678 | if (!wxMkdir(path)) | |
679 | { | |
680 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
681 | dialog.ShowModal(); | |
682 | return; | |
683 | } | |
684 | ||
685 | wxFileData *fd = new wxFileData( path, new_name, wxFileData::is_dir, wxFileIconsTable::folder ); | |
686 | wxListItem item; | |
687 | item.m_itemId = 0; | |
688 | item.m_col = 0; | |
689 | long id = Add( fd, item ); | |
690 | ||
691 | if (id != -1) | |
692 | { | |
260020e3 | 693 | SortItems(m_sort_field, m_sort_forward); |
0cf3e587 VZ |
694 | id = FindItem( 0, wxPtrToUInt(fd) ); |
695 | EnsureVisible( id ); | |
696 | EditLabel( id ); | |
697 | } | |
698 | else | |
699 | delete fd; | |
700 | } | |
701 | ||
16a6b53d | 702 | void wxFileListCtrl::GoToParentDir() |
0cf3e587 VZ |
703 | { |
704 | if (!IsTopMostDir(m_dirName)) | |
705 | { | |
706 | size_t len = m_dirName.length(); | |
707 | if (wxEndsWithPathSeparator(m_dirName)) | |
708 | m_dirName.Remove( len-1, 1 ); | |
709 | wxString fname( wxFileNameFromPath(m_dirName) ); | |
710 | m_dirName = wxPathOnly( m_dirName ); | |
711 | #if defined(__DOS__) || defined(__WINDOWS__) || defined(__OS2__) | |
712 | if (!m_dirName.empty()) | |
713 | { | |
714 | if (m_dirName.Last() == wxT('.')) | |
715 | m_dirName = wxEmptyString; | |
716 | } | |
717 | #elif defined(__UNIX__) | |
718 | if (m_dirName.empty()) | |
719 | m_dirName = wxT("/"); | |
720 | #endif | |
721 | UpdateFiles(); | |
722 | long id = FindItem( 0, fname ); | |
723 | if (id != wxNOT_FOUND) | |
724 | { | |
725 | ignoreChanges = true; | |
726 | SetItemState( id, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
727 | EnsureVisible( id ); | |
728 | ignoreChanges = false; | |
729 | } | |
730 | } | |
731 | } | |
732 | ||
16a6b53d | 733 | void wxFileListCtrl::GoToHomeDir() |
0cf3e587 VZ |
734 | { |
735 | wxString s = wxGetUserHome( wxString() ); | |
736 | GoToDir(s); | |
737 | } | |
738 | ||
16a6b53d | 739 | void wxFileListCtrl::GoToDir( const wxString &dir ) |
0cf3e587 VZ |
740 | { |
741 | if (!wxDirExists(dir)) return; | |
742 | ||
743 | m_dirName = dir; | |
744 | UpdateFiles(); | |
745 | ||
746 | ignoreChanges = true; | |
747 | SetItemState( 0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
748 | ignoreChanges = false; | |
749 | ||
750 | EnsureVisible( 0 ); | |
751 | } | |
752 | ||
16a6b53d | 753 | void wxFileListCtrl::FreeItemData(wxListItem& item) |
0cf3e587 VZ |
754 | { |
755 | if ( item.m_data ) | |
756 | { | |
757 | wxFileData *fd = (wxFileData*)item.m_data; | |
758 | delete fd; | |
759 | ||
760 | item.m_data = 0; | |
761 | } | |
762 | } | |
763 | ||
16a6b53d | 764 | void wxFileListCtrl::OnListDeleteItem( wxListEvent &event ) |
0cf3e587 VZ |
765 | { |
766 | FreeItemData(event.m_item); | |
767 | } | |
768 | ||
16a6b53d | 769 | void wxFileListCtrl::OnListDeleteAllItems( wxListEvent & WXUNUSED(event) ) |
0cf3e587 VZ |
770 | { |
771 | FreeAllItemsData(); | |
772 | } | |
773 | ||
16a6b53d | 774 | void wxFileListCtrl::FreeAllItemsData() |
0cf3e587 VZ |
775 | { |
776 | wxListItem item; | |
777 | item.m_mask = wxLIST_MASK_DATA; | |
778 | ||
779 | item.m_itemId = GetNextItem( -1, wxLIST_NEXT_ALL ); | |
780 | while ( item.m_itemId != -1 ) | |
781 | { | |
782 | GetItem( item ); | |
783 | FreeItemData(item); | |
784 | item.m_itemId = GetNextItem( item.m_itemId, wxLIST_NEXT_ALL ); | |
785 | } | |
786 | } | |
787 | ||
16a6b53d | 788 | void wxFileListCtrl::OnListEndLabelEdit( wxListEvent &event ) |
0cf3e587 VZ |
789 | { |
790 | wxFileData *fd = (wxFileData*)event.m_item.m_data; | |
791 | wxASSERT( fd ); | |
792 | ||
793 | if ((event.GetLabel().empty()) || | |
794 | (event.GetLabel() == wxT(".")) || | |
795 | (event.GetLabel() == wxT("..")) || | |
796 | (event.GetLabel().First( wxFILE_SEP_PATH ) != wxNOT_FOUND)) | |
797 | { | |
798 | wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR ); | |
799 | dialog.ShowModal(); | |
800 | event.Veto(); | |
801 | return; | |
802 | } | |
803 | ||
804 | wxString new_name( wxPathOnly( fd->GetFilePath() ) ); | |
805 | new_name += wxFILE_SEP_PATH; | |
806 | new_name += event.GetLabel(); | |
807 | ||
808 | wxLogNull log; | |
809 | ||
810 | if (wxFileExists(new_name)) | |
811 | { | |
812 | wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR ); | |
813 | dialog.ShowModal(); | |
814 | event.Veto(); | |
815 | } | |
816 | ||
817 | if (wxRenameFile(fd->GetFilePath(),new_name)) | |
818 | { | |
819 | fd->SetNewName( new_name, event.GetLabel() ); | |
820 | ||
821 | ignoreChanges = true; | |
822 | SetItemState( event.GetItem(), wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
823 | ignoreChanges = false; | |
824 | ||
825 | UpdateItem( event.GetItem() ); | |
826 | EnsureVisible( event.GetItem() ); | |
827 | } | |
828 | else | |
829 | { | |
830 | wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR ); | |
831 | dialog.ShowModal(); | |
832 | event.Veto(); | |
833 | } | |
834 | } | |
835 | ||
16a6b53d | 836 | void wxFileListCtrl::OnListColClick( wxListEvent &event ) |
0cf3e587 VZ |
837 | { |
838 | int col = event.GetColumn(); | |
839 | ||
840 | switch (col) | |
841 | { | |
842 | case wxFileData::FileList_Name : | |
843 | case wxFileData::FileList_Size : | |
844 | case wxFileData::FileList_Type : | |
845 | case wxFileData::FileList_Time : break; | |
846 | default : return; | |
847 | } | |
848 | ||
849 | if ((wxFileData::fileListFieldType)col == m_sort_field) | |
260020e3 | 850 | m_sort_forward = !m_sort_forward; |
0cf3e587 VZ |
851 | else |
852 | m_sort_field = (wxFileData::fileListFieldType)col; | |
853 | ||
260020e3 | 854 | SortItems(m_sort_field, m_sort_forward); |
0cf3e587 VZ |
855 | } |
856 | ||
16a6b53d | 857 | void wxFileListCtrl::SortItems(wxFileData::fileListFieldType field, bool forward) |
0cf3e587 VZ |
858 | { |
859 | m_sort_field = field; | |
260020e3 | 860 | m_sort_forward = forward; |
0cf3e587 VZ |
861 | const long sort_dir = forward ? 1 : -1; |
862 | ||
863 | switch (m_sort_field) | |
864 | { | |
865 | case wxFileData::FileList_Size : | |
866 | wxListCtrl::SortItems(wxFileDataSizeCompare, sort_dir); | |
867 | break; | |
868 | ||
869 | case wxFileData::FileList_Type : | |
870 | wxListCtrl::SortItems(wxFileDataTypeCompare, sort_dir); | |
871 | break; | |
872 | ||
873 | case wxFileData::FileList_Time : | |
874 | wxListCtrl::SortItems(wxFileDataTimeCompare, sort_dir); | |
875 | break; | |
876 | ||
877 | case wxFileData::FileList_Name : | |
878 | default : | |
879 | wxListCtrl::SortItems(wxFileDataNameCompare, sort_dir); | |
880 | break; | |
881 | } | |
882 | } | |
883 | ||
16a6b53d | 884 | wxFileListCtrl::~wxFileListCtrl() |
0cf3e587 VZ |
885 | { |
886 | // Normally the data are freed via an EVT_LIST_DELETE_ALL_ITEMS event and | |
16a6b53d VZ |
887 | // wxFileListCtrl::OnListDeleteAllItems. But if the event is generated after |
888 | // the destruction of the wxFileListCtrl we need to free any data here: | |
0cf3e587 VZ |
889 | FreeAllItemsData(); |
890 | } | |
891 | ||
892 | #define ID_CHOICE (wxID_FILECTRL + 1) | |
893 | #define ID_TEXT (wxID_FILECTRL + 2) | |
894 | #define ID_FILELIST_CTRL (wxID_FILECTRL + 3) | |
895 | #define ID_CHECK (wxID_FILECTRL + 4) | |
896 | ||
897 | /////////////////////////////////////////////////////////////////////////////// | |
898 | // wxGenericFileCtrl implementation | |
899 | /////////////////////////////////////////////////////////////////////////////// | |
900 | ||
901 | IMPLEMENT_DYNAMIC_CLASS( wxGenericFileCtrl, wxPanel ) | |
902 | ||
903 | BEGIN_EVENT_TABLE( wxGenericFileCtrl, wxPanel ) | |
904 | EVT_LIST_ITEM_SELECTED( ID_FILELIST_CTRL, wxGenericFileCtrl::OnSelected ) | |
905 | EVT_LIST_ITEM_ACTIVATED( ID_FILELIST_CTRL, wxGenericFileCtrl::OnActivated ) | |
906 | EVT_CHOICE( ID_CHOICE, wxGenericFileCtrl::OnChoiceFilter ) | |
907 | EVT_TEXT_ENTER( ID_TEXT, wxGenericFileCtrl::OnTextEnter ) | |
908 | EVT_TEXT( ID_TEXT, wxGenericFileCtrl::OnTextChange ) | |
909 | EVT_CHECKBOX( ID_CHECK, wxGenericFileCtrl::OnCheck ) | |
910 | END_EVENT_TABLE() | |
911 | ||
912 | bool wxGenericFileCtrl::Create( wxWindow *parent, | |
913 | wxWindowID id, | |
914 | const wxString& defaultDirectory, | |
915 | const wxString& defaultFileName, | |
916 | const wxString& wildCard, | |
917 | long style, | |
918 | const wxPoint& pos, | |
919 | const wxSize& size, | |
920 | const wxString& name ) | |
921 | { | |
922 | this->m_style = style; | |
923 | m_inSelected = false; | |
924 | m_noSelChgEvent = false; | |
925 | ||
926 | // check that the styles are not contradictory | |
927 | wxASSERT_MSG( !( ( m_style & wxFC_SAVE ) && ( m_style & wxFC_OPEN ) ), | |
928 | wxT( "can't specify both wxFC_SAVE and wxFC_OPEN at once" ) ); | |
929 | ||
930 | wxASSERT_MSG( !( ( m_style & wxFC_SAVE ) && ( m_style & wxFC_MULTIPLE ) ), | |
931 | wxT( "wxFC_MULTIPLE can't be used with wxFC_SAVE" ) ); | |
932 | ||
933 | wxPanel::Create( parent, id, pos, size, wxTAB_TRAVERSAL, name ); | |
934 | ||
935 | m_dir = defaultDirectory; | |
936 | ||
937 | m_ignoreChanges = true; | |
938 | ||
939 | if ( ( m_dir.empty() ) || ( m_dir == wxT( "." ) ) ) | |
940 | { | |
941 | m_dir = wxGetCwd(); | |
942 | if ( m_dir.empty() ) | |
943 | m_dir = wxFILE_SEP_PATH; | |
944 | } | |
945 | ||
946 | const size_t len = m_dir.length(); | |
947 | if ( ( len > 1 ) && ( wxEndsWithPathSeparator( m_dir ) ) ) | |
948 | m_dir.Remove( len - 1, 1 ); | |
949 | ||
950 | m_filterExtension = wxEmptyString; | |
951 | ||
952 | // layout | |
953 | ||
954 | const bool is_pda = ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA ); | |
955 | ||
956 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); | |
957 | ||
958 | wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL ); | |
959 | if ( is_pda ) | |
960 | staticsizer->Add( new wxStaticText( this, wxID_ANY, _( "Current directory:" ) ), 0, wxRIGHT, 10 ); | |
961 | m_static = new wxStaticText( this, wxID_ANY, m_dir ); | |
962 | staticsizer->Add( m_static, 1 ); | |
963 | mainsizer->Add( staticsizer, 0, wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM, 10 ); | |
964 | ||
965 | long style2 = wxLC_LIST; | |
966 | if ( !( m_style & wxFC_MULTIPLE ) ) | |
967 | style2 |= wxLC_SINGLE_SEL; | |
968 | ||
969 | #ifdef __WXWINCE__ | |
970 | style2 |= wxSIMPLE_BORDER; | |
971 | #else | |
972 | style2 |= wxSUNKEN_BORDER; | |
973 | #endif | |
974 | ||
16a6b53d VZ |
975 | m_list = new wxFileListCtrl( this, ID_FILELIST_CTRL, |
976 | wxEmptyString, false, | |
977 | wxDefaultPosition, wxSize( 400, 140 ), | |
978 | style2 ); | |
0cf3e587 VZ |
979 | |
980 | m_text = new wxTextCtrl( this, ID_TEXT, wxEmptyString, | |
981 | wxDefaultPosition, wxDefaultSize, | |
982 | wxTE_PROCESS_ENTER ); | |
983 | m_choice = new wxChoice( this, ID_CHOICE ); | |
984 | ||
985 | if ( is_pda ) | |
986 | { | |
987 | // PDAs have a different screen layout | |
988 | mainsizer->Add( m_list, wxSizerFlags( 1 ).Expand().HorzBorder() ); | |
989 | ||
990 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); | |
991 | textsizer->Add( m_text, wxSizerFlags( 1 ).Centre().Border() ); | |
992 | mainsizer->Add( textsizer, wxSizerFlags().Expand() ); | |
993 | ||
994 | m_check = NULL; | |
995 | textsizer->Add( m_choice, wxSizerFlags( 1 ).Centre().Border() ); | |
996 | } | |
997 | else // !is_pda | |
998 | { | |
999 | mainsizer->Add( m_list, wxSizerFlags( 1 ).Expand().DoubleHorzBorder() ); | |
1000 | ||
1001 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); | |
1002 | textsizer->Add( m_text, wxSizerFlags( 1 ).Centre(). | |
1003 | DoubleBorder( wxLEFT | wxRIGHT | wxTOP ) ); | |
1004 | mainsizer->Add( textsizer, wxSizerFlags().Expand() ); | |
1005 | ||
1006 | wxSizerFlags flagsCentre; | |
1007 | flagsCentre.Centre().DoubleBorder(); | |
1008 | ||
1009 | wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL ); | |
1010 | choicesizer->Add( m_choice, wxSizerFlags( flagsCentre ).Proportion( 1 ) ); | |
1011 | ||
1012 | if ( !( m_style & wxFC_NOSHOWHIDDEN ) ) | |
1013 | { | |
1014 | m_check = new wxCheckBox( this, ID_CHECK, _( "Show &hidden files" ) ); | |
1015 | choicesizer->Add( m_check, flagsCentre ); | |
1016 | } | |
1017 | ||
1018 | mainsizer->Add( choicesizer, wxSizerFlags().Expand() ); | |
1019 | } | |
1020 | ||
1021 | SetWildcard( wildCard ); | |
1022 | ||
1023 | SetAutoLayout( true ); | |
1024 | SetSizer( mainsizer ); | |
1025 | ||
1026 | if ( !is_pda ) | |
1027 | { | |
1028 | mainsizer->Fit( this ); | |
1029 | } | |
1030 | ||
1031 | m_list->GoToDir( m_dir ); | |
1032 | UpdateControls(); | |
1033 | m_text->SetValue( m_fileName ); | |
1034 | ||
1035 | m_ignoreChanges = false; | |
1036 | ||
1037 | // must be after m_ignoreChanges = false | |
1038 | SetFilename( defaultFileName ); | |
1039 | ||
1040 | return true; | |
1041 | } | |
1042 | ||
1043 | wxString wxGenericFileCtrl::GetPath() const | |
1044 | { | |
1045 | return DoGetFilename( true ); | |
1046 | } | |
1047 | ||
1048 | wxString wxGenericFileCtrl::GetFilename() const | |
1049 | { | |
1050 | return DoGetFilename( false ); | |
1051 | } | |
1052 | ||
1053 | wxString wxGenericFileCtrl::DoGetFilename( const bool fullPath ) const | |
1054 | { | |
1055 | wxASSERT_MSG( ( m_style & wxFC_MULTIPLE ) == 0, | |
1056 | wxT( "With controls that has wxFC_MULTIPLE style " ) | |
1057 | wxT( "use GetFilenames/GetPaths to get all filenames/paths selected" ) ); | |
1058 | ||
1059 | const wxString value = m_text->GetValue(); | |
1060 | ||
1061 | if ( !value.empty() ) | |
1062 | return value; | |
1063 | return fullPath ? ( GetProperFileListDir() + value ) : value; | |
1064 | } | |
1065 | ||
1066 | void wxGenericFileCtrl::DoGetFilenames( wxArrayString& filenames, const bool fullPath ) const | |
1067 | { | |
1068 | filenames.Empty(); | |
1069 | ||
1070 | const wxString dir = GetProperFileListDir(); | |
1071 | const wxString value = m_text->GetValue(); | |
1072 | ||
1073 | if ( !value.empty() ) | |
1074 | { | |
1075 | if ( fullPath ) | |
1076 | filenames.Add( dir + value ); | |
1077 | else | |
1078 | filenames.Add( value ); | |
1079 | return; | |
1080 | } | |
1081 | ||
1082 | if ( m_list->GetSelectedItemCount() == 0 ) | |
1083 | { | |
1084 | return; | |
1085 | } | |
1086 | ||
1087 | filenames.Alloc( m_list->GetSelectedItemCount() ); | |
1088 | ||
1089 | wxListItem item; | |
1090 | item.m_mask = wxLIST_MASK_TEXT; | |
1091 | ||
1092 | item.m_itemId = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1093 | while ( item.m_itemId != -1 ) | |
1094 | { | |
1095 | m_list->GetItem( item ); | |
1096 | ||
1097 | if ( fullPath ) | |
1098 | filenames.Add( dir + item.m_text ); | |
1099 | else | |
1100 | filenames.Add( item.m_text ); | |
1101 | ||
1102 | item.m_itemId = m_list->GetNextItem( item.m_itemId, | |
1103 | wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | bool wxGenericFileCtrl::SetDirectory( const wxString& dir ) | |
1108 | { | |
1109 | m_ignoreChanges = true; | |
1110 | m_list->GoToDir( dir ); | |
1111 | UpdateControls(); | |
1112 | m_ignoreChanges = false; | |
1113 | ||
1114 | return wxFileName( dir ).SameAs( m_list->GetDir() ); | |
1115 | } | |
1116 | ||
1117 | wxString wxGenericFileCtrl::GetDirectory() const | |
1118 | { | |
1119 | return m_list->GetDir(); | |
1120 | } | |
1121 | ||
1122 | bool wxGenericFileCtrl::SetFilename( const wxString& name ) | |
1123 | { | |
1124 | const long item = m_list->FindItem( -1, name ); | |
1125 | ||
1126 | if ( item == -1 ) // file not found either because it doesn't exist or the | |
1127 | // current filter doesn't show it. | |
1128 | return false; | |
1129 | ||
1130 | m_noSelChgEvent = true; | |
1131 | ||
1132 | // Deselect selected items | |
1133 | { | |
1134 | const int numSelectedItems = m_list->GetSelectedItemCount(); | |
1135 | ||
1136 | if ( numSelectedItems > 0 ) | |
1137 | { | |
1138 | long itemIndex = -1; | |
1139 | ||
1140 | for ( ;; ) | |
1141 | { | |
1142 | itemIndex = m_list->GetNextItem( itemIndex, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1143 | if ( itemIndex == -1 ) | |
1144 | break; | |
1145 | ||
1146 | m_list->SetItemState( itemIndex, 0, wxLIST_STATE_SELECTED ); | |
1147 | } | |
1148 | } | |
1149 | } | |
1150 | ||
1151 | m_list->SetItemState( item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
1152 | m_list->EnsureVisible( item ); | |
1153 | ||
1154 | m_noSelChgEvent = false; | |
1155 | ||
1156 | return true; | |
1157 | } | |
1158 | ||
1159 | void wxGenericFileCtrl::DoSetFilterIndex( int filterindex ) | |
1160 | { | |
1161 | const wxString& str = (wx_static_cast(wxStringClientData *, | |
1162 | m_choice->GetClientObject( filterindex ))) | |
1163 | ->GetData(); | |
1164 | m_list->SetWild( str ); | |
1165 | m_filterIndex = filterindex; | |
1166 | if ( str.Left( 2 ) == wxT( "*." ) ) | |
1167 | { | |
1168 | m_filterExtension = str.Mid( 1 ); | |
1169 | if ( m_filterExtension == _T( ".*" ) ) | |
1170 | m_filterExtension.clear(); | |
1171 | } | |
1172 | else | |
1173 | { | |
1174 | m_filterExtension.clear(); | |
1175 | } | |
1176 | } | |
1177 | ||
1178 | void wxGenericFileCtrl::SetWildcard( const wxString& wildCard ) | |
1179 | { | |
1180 | if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr ) | |
1181 | { | |
1182 | m_wildCard = wxString::Format( _( "All files (%s)|%s" ), | |
1183 | wxFileSelectorDefaultWildcardStr, | |
1184 | wxFileSelectorDefaultWildcardStr ); | |
1185 | } | |
1186 | else | |
1187 | m_wildCard = wildCard; | |
1188 | ||
1189 | wxArrayString wildDescriptions, wildFilters; | |
1190 | const size_t count = wxParseCommonDialogsFilter( m_wildCard, | |
1191 | wildDescriptions, | |
1192 | wildFilters ); | |
1193 | wxCHECK_RET( count, wxT( "wxFileDialog: bad wildcard string" ) ); | |
1194 | ||
1195 | m_choice->Clear(); | |
1196 | ||
1197 | for ( size_t n = 0; n < count; n++ ) | |
1198 | { | |
1199 | m_choice->Append(wildDescriptions[n], new wxStringClientData(wildFilters[n])); | |
1200 | } | |
1201 | ||
1202 | SetFilterIndex( 0 ); | |
1203 | } | |
1204 | ||
1205 | void wxGenericFileCtrl::SetFilterIndex( int filterindex ) | |
1206 | { | |
1207 | m_choice->SetSelection( filterindex ); | |
1208 | ||
1209 | DoSetFilterIndex( filterindex ); | |
1210 | } | |
1211 | ||
1212 | void wxGenericFileCtrl::OnChoiceFilter( wxCommandEvent &event ) | |
1213 | { | |
1214 | DoSetFilterIndex( ( int )event.GetInt() ); | |
1215 | } | |
1216 | ||
1217 | void wxGenericFileCtrl::OnCheck( wxCommandEvent &event ) | |
1218 | { | |
1219 | m_list->ShowHidden( event.GetInt() != 0 ); | |
1220 | } | |
1221 | ||
1222 | void wxGenericFileCtrl::OnActivated( wxListEvent &event ) | |
1223 | { | |
1224 | HandleAction( event.m_item.m_text ); | |
1225 | } | |
1226 | ||
1227 | void wxGenericFileCtrl::OnTextEnter( wxCommandEvent &WXUNUSED( event ) ) | |
1228 | { | |
1229 | HandleAction( m_text->GetValue() ); | |
1230 | } | |
1231 | ||
1232 | void wxGenericFileCtrl::OnTextChange( wxCommandEvent &WXUNUSED( event ) ) | |
1233 | { | |
1234 | if ( !m_ignoreChanges ) | |
1235 | { | |
1236 | // Clear selections. Otherwise when the user types in a value they may | |
1237 | // not get the file whose name they typed. | |
1238 | if ( m_list->GetSelectedItemCount() > 0 ) | |
1239 | { | |
1240 | long item = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, | |
1241 | wxLIST_STATE_SELECTED ); | |
1242 | while ( item != -1 ) | |
1243 | { | |
1244 | m_list->SetItemState( item, 0, wxLIST_STATE_SELECTED ); | |
1245 | item = m_list->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1246 | } | |
1247 | } | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | void wxGenericFileCtrl::OnSelected( wxListEvent &event ) | |
1252 | { | |
1253 | if ( m_ignoreChanges ) | |
1254 | return; | |
1255 | ||
1256 | if ( m_inSelected ) | |
1257 | return; | |
1258 | ||
1259 | m_inSelected = true; | |
1260 | const wxString filename( event.m_item.m_text ); | |
1261 | ||
1262 | #ifdef __WXWINCE__ | |
1263 | // No double-click on most WinCE devices, so do action immediately. | |
1264 | HandleAction( filename ); | |
1265 | #else | |
1266 | if ( filename == wxT( ".." ) ) | |
1267 | { | |
1268 | m_inSelected = false; | |
1269 | return; | |
1270 | } | |
1271 | ||
1272 | wxString dir = m_list->GetDir(); | |
1273 | if ( !IsTopMostDir( dir ) ) | |
1274 | dir += wxFILE_SEP_PATH; | |
1275 | dir += filename; | |
1276 | if ( wxDirExists( dir ) ) | |
1277 | { | |
1278 | m_inSelected = false; | |
1279 | ||
1280 | return; | |
1281 | } | |
1282 | ||
1283 | ||
1284 | m_ignoreChanges = true; | |
1285 | m_text->SetValue( filename ); | |
1286 | ||
1287 | if ( m_list->GetSelectedItemCount() > 1 ) | |
1288 | { | |
1289 | m_text->Clear(); | |
1290 | } | |
1291 | ||
1292 | if ( !m_noSelChgEvent ) | |
1293 | GenerateSelectionChangedEvent( this, this ); | |
1294 | ||
1295 | m_ignoreChanges = false; | |
1296 | #endif | |
1297 | m_inSelected = false; | |
1298 | } | |
1299 | ||
1300 | void wxGenericFileCtrl::HandleAction( const wxString &fn ) | |
1301 | { | |
1302 | if ( m_ignoreChanges ) | |
1303 | return; | |
1304 | ||
1305 | wxString filename( fn ); | |
1306 | if ( filename.empty() ) | |
1307 | { | |
1308 | return; | |
1309 | } | |
1310 | if ( filename == wxT( "." ) ) return; | |
1311 | ||
1312 | wxString dir = m_list->GetDir(); | |
1313 | ||
1314 | // "some/place/" means they want to chdir not try to load "place" | |
1315 | const bool want_dir = filename.Last() == wxFILE_SEP_PATH; | |
1316 | if ( want_dir ) | |
1317 | filename = filename.RemoveLast(); | |
1318 | ||
1319 | if ( filename == wxT( ".." ) ) | |
1320 | { | |
1321 | m_ignoreChanges = true; | |
1322 | m_list->GoToParentDir(); | |
1323 | ||
1324 | GenerateFolderChangedEvent( this, this ); | |
1325 | ||
1326 | UpdateControls(); | |
1327 | m_ignoreChanges = false; | |
1328 | return; | |
1329 | } | |
1330 | ||
1331 | #ifdef __UNIX__ | |
1332 | if ( filename == wxT( "~" ) ) | |
1333 | { | |
1334 | m_ignoreChanges = true; | |
1335 | m_list->GoToHomeDir(); | |
1336 | ||
1337 | GenerateFolderChangedEvent( this, this ); | |
1338 | ||
1339 | UpdateControls(); | |
1340 | m_ignoreChanges = false; | |
1341 | return; | |
1342 | } | |
1343 | ||
1344 | if ( filename.BeforeFirst( wxT( '/' ) ) == wxT( "~" ) ) | |
1345 | { | |
1346 | filename = wxString( wxGetUserHome() ) + filename.Remove( 0, 1 ); | |
1347 | } | |
1348 | #endif // __UNIX__ | |
1349 | ||
1350 | if ( !( m_style & wxFC_SAVE ) ) | |
1351 | { | |
1352 | if ( ( filename.Find( wxT( '*' ) ) != wxNOT_FOUND ) || | |
1353 | ( filename.Find( wxT( '?' ) ) != wxNOT_FOUND ) ) | |
1354 | { | |
1355 | if ( filename.Find( wxFILE_SEP_PATH ) != wxNOT_FOUND ) | |
1356 | { | |
1357 | wxMessageBox( _( "Illegal file specification." ), | |
1358 | _( "Error" ), wxOK | wxICON_ERROR, this ); | |
1359 | return; | |
1360 | } | |
1361 | m_list->SetWild( filename ); | |
1362 | return; | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | if ( !IsTopMostDir( dir ) ) | |
1367 | dir += wxFILE_SEP_PATH; | |
1368 | if ( !wxIsAbsolutePath( filename ) ) | |
1369 | { | |
1370 | dir += filename; | |
1371 | filename = dir; | |
1372 | } | |
1373 | ||
1374 | if ( wxDirExists( filename ) ) | |
1375 | { | |
1376 | m_ignoreChanges = true; | |
1377 | m_list->GoToDir( filename ); | |
1378 | UpdateControls(); | |
1379 | ||
1380 | GenerateFolderChangedEvent( this, this ); | |
1381 | ||
1382 | m_ignoreChanges = false; | |
1383 | return; | |
1384 | } | |
1385 | ||
1386 | // they really wanted a dir, but it doesn't exist | |
1387 | if ( want_dir ) | |
1388 | { | |
1389 | wxMessageBox( _( "Directory doesn't exist." ), _( "Error" ), | |
1390 | wxOK | wxICON_ERROR, this ); | |
1391 | return; | |
1392 | } | |
1393 | ||
1394 | // append the default extension to the filename if it doesn't have any | |
1395 | // | |
1396 | // VZ: the logic of testing for !wxFileExists() only for the open file | |
1397 | // dialog is not entirely clear to me, why don't we allow saving to a | |
1398 | // file without extension as well? | |
1399 | if ( !( m_style & wxFC_OPEN ) || !wxFileExists( filename ) ) | |
1400 | { | |
1401 | filename = wxFileDialogBase::AppendExtension( filename, m_filterExtension ); | |
1402 | GenerateFileActivatedEvent( this, this, wxFileName( filename ).GetFullName() ); | |
1403 | return; | |
1404 | } | |
1405 | ||
1406 | GenerateFileActivatedEvent( this, this ); | |
1407 | } | |
1408 | ||
1409 | bool wxGenericFileCtrl::SetPath( const wxString& path ) | |
1410 | { | |
1411 | if ( !wxFileName::FileExists( ( path ) ) ) | |
1412 | return false; | |
1413 | ||
1414 | wxString ext; | |
1415 | wxSplitPath( path, &m_dir, &m_fileName, &ext ); | |
1416 | if ( !ext.empty() ) | |
1417 | { | |
1418 | m_fileName += wxT( "." ); | |
1419 | m_fileName += ext; | |
1420 | } | |
1421 | ||
1422 | SetDirectory( m_dir ); | |
1423 | SetFilename( m_fileName ); | |
1424 | ||
1425 | return true; | |
1426 | } | |
1427 | ||
1428 | void wxGenericFileCtrl::GetPaths( wxArrayString& paths ) const | |
1429 | { | |
1430 | DoGetFilenames( paths, true ); | |
1431 | } | |
1432 | ||
1433 | void wxGenericFileCtrl::GetFilenames( wxArrayString& files ) const | |
1434 | { | |
1435 | DoGetFilenames( files, false ); | |
1436 | } | |
1437 | ||
1438 | void wxGenericFileCtrl::UpdateControls() | |
1439 | { | |
1440 | const wxString dir = m_list->GetDir(); | |
1441 | m_static->SetLabel( dir ); | |
1442 | } | |
1443 | ||
1444 | void wxGenericFileCtrl::GoToParentDir() | |
1445 | { | |
1446 | m_list->GoToParentDir(); | |
1447 | UpdateControls(); | |
1448 | } | |
1449 | ||
1450 | void wxGenericFileCtrl::GoToHomeDir() | |
1451 | { | |
1452 | m_list->GoToHomeDir(); | |
1453 | UpdateControls(); | |
1454 | } | |
1455 | ||
1456 | wxString wxGenericFileCtrl::GetProperFileListDir() const | |
1457 | { | |
1458 | wxString dir = m_list->GetDir(); | |
1459 | #ifdef __UNIX__ | |
1460 | if ( dir != wxT( "/" ) ) | |
1461 | #elif defined(__WXWINCE__) | |
1462 | if ( dir != wxT( "/" ) && dir != wxT( "\\" ) ) | |
1463 | #endif | |
1464 | dir += wxFILE_SEP_PATH; | |
1465 | ||
1466 | return dir; | |
1467 | } | |
1468 | ||
1469 | #endif // wxUSE_FILECTRL |