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