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