]>
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 VZ |
37 | |
38 | #ifdef __WXMSW__ | |
39 | #include "wx/msw/wrapwin.h" | |
40 | #endif | |
41 | ||
42 | #if defined(__WXWINCE__) | |
43 | #define IsTopMostDir(dir) (dir == wxT("\\") || dir == wxT("/")) | |
44 | #elif (defined(__DOS__) || defined(__WINDOWS__) || defined (__OS2__)) | |
45 | #define IsTopMostDir(dir) (dir.empty()) | |
46 | #else | |
47 | #define IsTopMostDir(dir) (dir == wxT("/")) | |
48 | #endif | |
49 | ||
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // private functions | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | static | |
b18e2046 | 56 | int wxCALLBACK wxFileDataNameCompare( long data1, long 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 | |
b18e2046 | 74 | int wxCALLBACK wxFileDataSizeCompare(long data1, long 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 | |
b18e2046 | 96 | int wxCALLBACK wxFileDataTypeCompare(long data1, long 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 | |
b18e2046 | 118 | int wxCALLBACK wxFileDataTimeCompare(long data1, long 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") ); |
0cf3e587 VZ |
380 | if ( dg.Ok() ) |
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; | |
680 | long id = Add( fd, item ); | |
681 | ||
682 | if (id != -1) | |
683 | { | |
260020e3 | 684 | SortItems(m_sort_field, m_sort_forward); |
0cf3e587 VZ |
685 | id = FindItem( 0, wxPtrToUInt(fd) ); |
686 | EnsureVisible( id ); | |
687 | EditLabel( id ); | |
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 | ||
886 | IMPLEMENT_DYNAMIC_CLASS( wxGenericFileCtrl, wxPanel ) | |
887 | ||
888 | BEGIN_EVENT_TABLE( wxGenericFileCtrl, wxPanel ) | |
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 | ||
919 | wxPanel::Create( parent, id, pos, size, wxTAB_TRAVERSAL, name ); | |
920 | ||
921 | m_dir = defaultDirectory; | |
922 | ||
923 | m_ignoreChanges = true; | |
924 | ||
925 | if ( ( m_dir.empty() ) || ( m_dir == wxT( "." ) ) ) | |
926 | { | |
927 | m_dir = wxGetCwd(); | |
928 | if ( m_dir.empty() ) | |
929 | m_dir = wxFILE_SEP_PATH; | |
930 | } | |
931 | ||
932 | const size_t len = m_dir.length(); | |
933 | if ( ( len > 1 ) && ( wxEndsWithPathSeparator( m_dir ) ) ) | |
934 | m_dir.Remove( len - 1, 1 ); | |
935 | ||
936 | m_filterExtension = wxEmptyString; | |
937 | ||
938 | // layout | |
939 | ||
940 | const bool is_pda = ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA ); | |
941 | ||
942 | wxBoxSizer *mainsizer = new wxBoxSizer( wxVERTICAL ); | |
943 | ||
944 | wxBoxSizer *staticsizer = new wxBoxSizer( wxHORIZONTAL ); | |
945 | if ( is_pda ) | |
8ce68f7f VZ |
946 | staticsizer->Add( new wxStaticText( this, wxID_ANY, _( "Current directory:" ) ), |
947 | wxSizerFlags().DoubleBorder(wxRIGHT) ); | |
0cf3e587 VZ |
948 | m_static = new wxStaticText( this, wxID_ANY, m_dir ); |
949 | staticsizer->Add( m_static, 1 ); | |
8ce68f7f | 950 | mainsizer->Add( staticsizer, wxSizerFlags().Expand().Border()); |
0cf3e587 VZ |
951 | |
952 | long style2 = wxLC_LIST; | |
953 | if ( !( m_style & wxFC_MULTIPLE ) ) | |
954 | style2 |= wxLC_SINGLE_SEL; | |
955 | ||
956 | #ifdef __WXWINCE__ | |
957 | style2 |= wxSIMPLE_BORDER; | |
958 | #else | |
959 | style2 |= wxSUNKEN_BORDER; | |
960 | #endif | |
961 | ||
16a6b53d VZ |
962 | m_list = new wxFileListCtrl( this, ID_FILELIST_CTRL, |
963 | wxEmptyString, false, | |
964 | wxDefaultPosition, wxSize( 400, 140 ), | |
965 | style2 ); | |
0cf3e587 VZ |
966 | |
967 | m_text = new wxTextCtrl( this, ID_TEXT, wxEmptyString, | |
968 | wxDefaultPosition, wxDefaultSize, | |
969 | wxTE_PROCESS_ENTER ); | |
970 | m_choice = new wxChoice( this, ID_CHOICE ); | |
971 | ||
972 | if ( is_pda ) | |
973 | { | |
974 | // PDAs have a different screen layout | |
975 | mainsizer->Add( m_list, wxSizerFlags( 1 ).Expand().HorzBorder() ); | |
976 | ||
977 | wxBoxSizer *textsizer = new wxBoxSizer( wxHORIZONTAL ); | |
978 | textsizer->Add( m_text, wxSizerFlags( 1 ).Centre().Border() ); | |
8ce68f7f | 979 | textsizer->Add( m_choice, wxSizerFlags( 1 ).Centre().Border() ); |
0cf3e587 VZ |
980 | mainsizer->Add( textsizer, wxSizerFlags().Expand() ); |
981 | ||
0cf3e587 VZ |
982 | } |
983 | else // !is_pda | |
984 | { | |
8ce68f7f VZ |
985 | mainsizer->Add( m_list, wxSizerFlags( 1 ).Expand().Border() ); |
986 | mainsizer->Add( m_text, wxSizerFlags().Expand().Border() ); | |
0cf3e587 VZ |
987 | |
988 | wxBoxSizer *choicesizer = new wxBoxSizer( wxHORIZONTAL ); | |
8ce68f7f | 989 | choicesizer->Add( m_choice, wxSizerFlags( 1 ).Centre() ); |
0cf3e587 VZ |
990 | |
991 | if ( !( m_style & wxFC_NOSHOWHIDDEN ) ) | |
8ce68f7f | 992 | { |
0cf3e587 | 993 | m_check = new wxCheckBox( this, ID_CHECK, _( "Show &hidden files" ) ); |
8ce68f7f VZ |
994 | choicesizer->Add( m_check, wxSizerFlags().Centre().DoubleBorder(wxLEFT) ); |
995 | } | |
0cf3e587 | 996 | |
8ce68f7f | 997 | mainsizer->Add( choicesizer, wxSizerFlags().Expand().Border() ); |
0cf3e587 VZ |
998 | } |
999 | ||
1000 | SetWildcard( wildCard ); | |
1001 | ||
1002 | SetAutoLayout( true ); | |
1003 | SetSizer( mainsizer ); | |
1004 | ||
1005 | if ( !is_pda ) | |
1006 | { | |
1007 | mainsizer->Fit( this ); | |
1008 | } | |
1009 | ||
1010 | m_list->GoToDir( m_dir ); | |
1011 | UpdateControls(); | |
1012 | m_text->SetValue( m_fileName ); | |
1013 | ||
1014 | m_ignoreChanges = false; | |
1015 | ||
1016 | // must be after m_ignoreChanges = false | |
1017 | SetFilename( defaultFileName ); | |
1018 | ||
1019 | return true; | |
1020 | } | |
1021 | ||
398eebb1 VZ |
1022 | // NB: there is an unfortunate mismatch between wxFileName and wxFileDialog |
1023 | // method names but our GetDirectory() does correspond to wxFileName:: | |
1024 | // GetPath() while our GetPath() is wxFileName::GetFullPath() | |
0cf3e587 VZ |
1025 | wxString wxGenericFileCtrl::GetPath() const |
1026 | { | |
398eebb1 VZ |
1027 | wxASSERT_MSG ( !(m_style & wxFC_MULTIPLE), "use GetPaths() instead" ); |
1028 | ||
1029 | return DoGetFileName().GetFullPath(); | |
0cf3e587 VZ |
1030 | } |
1031 | ||
1032 | wxString wxGenericFileCtrl::GetFilename() const | |
1033 | { | |
398eebb1 VZ |
1034 | wxASSERT_MSG ( !(m_style & wxFC_MULTIPLE), "use GetFilenames() instead" ); |
1035 | ||
1036 | return DoGetFileName().GetFullName(); | |
0cf3e587 VZ |
1037 | } |
1038 | ||
398eebb1 | 1039 | wxString wxGenericFileCtrl::GetDirectory() const |
0cf3e587 | 1040 | { |
398eebb1 VZ |
1041 | // don't check for wxFC_MULTIPLE here, this one is probably safe to call in |
1042 | // any case as it can be always taken to mean "current directory" | |
1043 | return DoGetFileName().GetPath(); | |
1044 | } | |
0cf3e587 | 1045 | |
398eebb1 VZ |
1046 | wxFileName wxGenericFileCtrl::DoGetFileName() const |
1047 | { | |
1048 | wxFileName fn; | |
0cf3e587 | 1049 | |
398eebb1 VZ |
1050 | wxString value = m_text->GetValue(); |
1051 | if ( value.empty() ) | |
1052 | { | |
1053 | // nothing in the text control, get the selected file from the list | |
1054 | wxListItem item; | |
1055 | item.m_itemId = m_list->GetNextItem(-1, wxLIST_NEXT_ALL, | |
1056 | wxLIST_STATE_SELECTED); | |
1057 | m_list->GetItem(item); | |
1058 | ||
1059 | fn.Assign(m_list->GetDir(), item.m_text); | |
1060 | } | |
1061 | else // user entered the value | |
1062 | { | |
1063 | // the path can be either absolute or relative | |
1064 | fn.Assign(value); | |
1065 | if ( fn.IsRelative() ) | |
1066 | fn.MakeAbsolute(m_list->GetDir()); | |
1067 | } | |
1068 | ||
1069 | return fn; | |
0cf3e587 VZ |
1070 | } |
1071 | ||
d42b8535 VZ |
1072 | // helper used in DoGetFilenames() and needed because Borland can't compile |
1073 | // operator?: inline | |
1074 | static inline wxString GetFileNameOrPath(const wxFileName& fn, bool fullPath) | |
1075 | { | |
1076 | return fullPath ? fn.GetFullPath() : fn.GetFullName(); | |
1077 | } | |
1078 | ||
1079 | void | |
1080 | wxGenericFileCtrl::DoGetFilenames(wxArrayString& filenames, bool fullPath) const | |
0cf3e587 | 1081 | { |
7d42f4d6 | 1082 | filenames.clear(); |
0cf3e587 | 1083 | |
398eebb1 | 1084 | const wxString dir = m_list->GetDir(); |
0cf3e587 | 1085 | |
7d42f4d6 | 1086 | const wxString value = m_text->GetValue(); |
0cf3e587 VZ |
1087 | if ( !value.empty() ) |
1088 | { | |
7d42f4d6 VZ |
1089 | wxFileName fn(value); |
1090 | if ( fn.IsRelative() ) | |
1091 | fn.MakeAbsolute(dir); | |
1092 | ||
d42b8535 | 1093 | filenames.push_back(GetFileNameOrPath(fn, fullPath)); |
0cf3e587 VZ |
1094 | return; |
1095 | } | |
1096 | ||
7d42f4d6 VZ |
1097 | const int numSel = m_list->GetSelectedItemCount(); |
1098 | if ( !numSel ) | |
0cf3e587 | 1099 | return; |
0cf3e587 | 1100 | |
7d42f4d6 | 1101 | filenames.reserve(numSel); |
0cf3e587 VZ |
1102 | |
1103 | wxListItem item; | |
1104 | item.m_mask = wxLIST_MASK_TEXT; | |
7d42f4d6 VZ |
1105 | item.m_itemId = -1; |
1106 | for ( ;; ) | |
0cf3e587 | 1107 | { |
7d42f4d6 VZ |
1108 | item.m_itemId = m_list->GetNextItem(item.m_itemId, wxLIST_NEXT_ALL, |
1109 | wxLIST_STATE_SELECTED); | |
0cf3e587 | 1110 | |
7d42f4d6 VZ |
1111 | if ( item.m_itemId == -1 ) |
1112 | break; | |
1113 | ||
1114 | m_list->GetItem(item); | |
0cf3e587 | 1115 | |
7d42f4d6 | 1116 | const wxFileName fn(dir, item.m_text); |
d42b8535 | 1117 | filenames.push_back(GetFileNameOrPath(fn, fullPath)); |
0cf3e587 VZ |
1118 | } |
1119 | } | |
1120 | ||
1121 | bool wxGenericFileCtrl::SetDirectory( const wxString& dir ) | |
1122 | { | |
1123 | m_ignoreChanges = true; | |
1124 | m_list->GoToDir( dir ); | |
1125 | UpdateControls(); | |
1126 | m_ignoreChanges = false; | |
1127 | ||
1128 | return wxFileName( dir ).SameAs( m_list->GetDir() ); | |
1129 | } | |
1130 | ||
0cf3e587 VZ |
1131 | bool wxGenericFileCtrl::SetFilename( const wxString& name ) |
1132 | { | |
1133 | const long item = m_list->FindItem( -1, name ); | |
1134 | ||
1135 | if ( item == -1 ) // file not found either because it doesn't exist or the | |
1136 | // current filter doesn't show it. | |
1137 | return false; | |
1138 | ||
1139 | m_noSelChgEvent = true; | |
1140 | ||
1141 | // Deselect selected items | |
1142 | { | |
1143 | const int numSelectedItems = m_list->GetSelectedItemCount(); | |
1144 | ||
1145 | if ( numSelectedItems > 0 ) | |
1146 | { | |
1147 | long itemIndex = -1; | |
1148 | ||
1149 | for ( ;; ) | |
1150 | { | |
1151 | itemIndex = m_list->GetNextItem( itemIndex, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1152 | if ( itemIndex == -1 ) | |
1153 | break; | |
1154 | ||
1155 | m_list->SetItemState( itemIndex, 0, wxLIST_STATE_SELECTED ); | |
1156 | } | |
1157 | } | |
1158 | } | |
1159 | ||
1160 | m_list->SetItemState( item, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED ); | |
1161 | m_list->EnsureVisible( item ); | |
1162 | ||
1163 | m_noSelChgEvent = false; | |
1164 | ||
1165 | return true; | |
1166 | } | |
1167 | ||
1168 | void wxGenericFileCtrl::DoSetFilterIndex( int filterindex ) | |
1169 | { | |
a85ad1db VZ |
1170 | wxClientData *pcd = m_choice->GetClientObject( filterindex ); |
1171 | if ( !pcd ) | |
1172 | return; | |
1173 | ||
5c33522f | 1174 | const wxString& str = ((static_cast<wxStringClientData *>(pcd))->GetData()); |
0cf3e587 VZ |
1175 | m_list->SetWild( str ); |
1176 | m_filterIndex = filterindex; | |
1177 | if ( str.Left( 2 ) == wxT( "*." ) ) | |
1178 | { | |
1179 | m_filterExtension = str.Mid( 1 ); | |
9a83f860 | 1180 | if ( m_filterExtension == wxT( ".*" ) ) |
0cf3e587 VZ |
1181 | m_filterExtension.clear(); |
1182 | } | |
1183 | else | |
1184 | { | |
1185 | m_filterExtension.clear(); | |
1186 | } | |
6305f044 VZ |
1187 | |
1188 | GenerateFilterChangedEvent( this, this ); | |
0cf3e587 VZ |
1189 | } |
1190 | ||
1191 | void wxGenericFileCtrl::SetWildcard( const wxString& wildCard ) | |
1192 | { | |
1193 | if ( wildCard.empty() || wildCard == wxFileSelectorDefaultWildcardStr ) | |
1194 | { | |
1195 | m_wildCard = wxString::Format( _( "All files (%s)|%s" ), | |
1196 | wxFileSelectorDefaultWildcardStr, | |
1197 | wxFileSelectorDefaultWildcardStr ); | |
1198 | } | |
1199 | else | |
1200 | m_wildCard = wildCard; | |
1201 | ||
1202 | wxArrayString wildDescriptions, wildFilters; | |
1203 | const size_t count = wxParseCommonDialogsFilter( m_wildCard, | |
1204 | wildDescriptions, | |
1205 | wildFilters ); | |
1206 | wxCHECK_RET( count, wxT( "wxFileDialog: bad wildcard string" ) ); | |
1207 | ||
1208 | m_choice->Clear(); | |
1209 | ||
1210 | for ( size_t n = 0; n < count; n++ ) | |
1211 | { | |
1212 | m_choice->Append(wildDescriptions[n], new wxStringClientData(wildFilters[n])); | |
1213 | } | |
1214 | ||
1215 | SetFilterIndex( 0 ); | |
1216 | } | |
1217 | ||
1218 | void wxGenericFileCtrl::SetFilterIndex( int filterindex ) | |
1219 | { | |
1220 | m_choice->SetSelection( filterindex ); | |
1221 | ||
1222 | DoSetFilterIndex( filterindex ); | |
1223 | } | |
1224 | ||
1225 | void wxGenericFileCtrl::OnChoiceFilter( wxCommandEvent &event ) | |
1226 | { | |
1227 | DoSetFilterIndex( ( int )event.GetInt() ); | |
1228 | } | |
1229 | ||
1230 | void wxGenericFileCtrl::OnCheck( wxCommandEvent &event ) | |
1231 | { | |
1232 | m_list->ShowHidden( event.GetInt() != 0 ); | |
1233 | } | |
1234 | ||
1235 | void wxGenericFileCtrl::OnActivated( wxListEvent &event ) | |
1236 | { | |
1237 | HandleAction( event.m_item.m_text ); | |
1238 | } | |
1239 | ||
1240 | void wxGenericFileCtrl::OnTextEnter( wxCommandEvent &WXUNUSED( event ) ) | |
1241 | { | |
1242 | HandleAction( m_text->GetValue() ); | |
1243 | } | |
1244 | ||
1245 | void wxGenericFileCtrl::OnTextChange( wxCommandEvent &WXUNUSED( event ) ) | |
1246 | { | |
1247 | if ( !m_ignoreChanges ) | |
1248 | { | |
1249 | // Clear selections. Otherwise when the user types in a value they may | |
1250 | // not get the file whose name they typed. | |
1251 | if ( m_list->GetSelectedItemCount() > 0 ) | |
1252 | { | |
1253 | long item = m_list->GetNextItem( -1, wxLIST_NEXT_ALL, | |
1254 | wxLIST_STATE_SELECTED ); | |
1255 | while ( item != -1 ) | |
1256 | { | |
1257 | m_list->SetItemState( item, 0, wxLIST_STATE_SELECTED ); | |
1258 | item = m_list->GetNextItem( item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED ); | |
1259 | } | |
1260 | } | |
1261 | } | |
1262 | } | |
1263 | ||
1264 | void wxGenericFileCtrl::OnSelected( wxListEvent &event ) | |
1265 | { | |
1266 | if ( m_ignoreChanges ) | |
1267 | return; | |
1268 | ||
1269 | if ( m_inSelected ) | |
1270 | return; | |
1271 | ||
1272 | m_inSelected = true; | |
1273 | const wxString filename( event.m_item.m_text ); | |
1274 | ||
1275 | #ifdef __WXWINCE__ | |
1276 | // No double-click on most WinCE devices, so do action immediately. | |
1277 | HandleAction( filename ); | |
1278 | #else | |
1279 | if ( filename == wxT( ".." ) ) | |
1280 | { | |
1281 | m_inSelected = false; | |
1282 | return; | |
1283 | } | |
1284 | ||
1285 | wxString dir = m_list->GetDir(); | |
1286 | if ( !IsTopMostDir( dir ) ) | |
1287 | dir += wxFILE_SEP_PATH; | |
1288 | dir += filename; | |
1289 | if ( wxDirExists( dir ) ) | |
1290 | { | |
1291 | m_inSelected = false; | |
1292 | ||
1293 | return; | |
1294 | } | |
bd365871 | 1295 | |
0cf3e587 VZ |
1296 | |
1297 | m_ignoreChanges = true; | |
1298 | m_text->SetValue( filename ); | |
1299 | ||
1300 | if ( m_list->GetSelectedItemCount() > 1 ) | |
1301 | { | |
1302 | m_text->Clear(); | |
1303 | } | |
1304 | ||
1305 | if ( !m_noSelChgEvent ) | |
1306 | GenerateSelectionChangedEvent( this, this ); | |
1307 | ||
1308 | m_ignoreChanges = false; | |
1309 | #endif | |
1310 | m_inSelected = false; | |
1311 | } | |
1312 | ||
1313 | void wxGenericFileCtrl::HandleAction( const wxString &fn ) | |
1314 | { | |
1315 | if ( m_ignoreChanges ) | |
1316 | return; | |
1317 | ||
1318 | wxString filename( fn ); | |
1319 | if ( filename.empty() ) | |
1320 | { | |
1321 | return; | |
1322 | } | |
1323 | if ( filename == wxT( "." ) ) return; | |
1324 | ||
1325 | wxString dir = m_list->GetDir(); | |
1326 | ||
1327 | // "some/place/" means they want to chdir not try to load "place" | |
1328 | const bool want_dir = filename.Last() == wxFILE_SEP_PATH; | |
1329 | if ( want_dir ) | |
1330 | filename = filename.RemoveLast(); | |
1331 | ||
1332 | if ( filename == wxT( ".." ) ) | |
1333 | { | |
1334 | m_ignoreChanges = true; | |
1335 | m_list->GoToParentDir(); | |
1336 | ||
1337 | GenerateFolderChangedEvent( this, this ); | |
1338 | ||
1339 | UpdateControls(); | |
1340 | m_ignoreChanges = false; | |
1341 | return; | |
1342 | } | |
1343 | ||
1344 | #ifdef __UNIX__ | |
1345 | if ( filename == wxT( "~" ) ) | |
1346 | { | |
1347 | m_ignoreChanges = true; | |
1348 | m_list->GoToHomeDir(); | |
1349 | ||
1350 | GenerateFolderChangedEvent( this, this ); | |
1351 | ||
1352 | UpdateControls(); | |
1353 | m_ignoreChanges = false; | |
1354 | return; | |
1355 | } | |
1356 | ||
1357 | if ( filename.BeforeFirst( wxT( '/' ) ) == wxT( "~" ) ) | |
1358 | { | |
1359 | filename = wxString( wxGetUserHome() ) + filename.Remove( 0, 1 ); | |
1360 | } | |
1361 | #endif // __UNIX__ | |
1362 | ||
1363 | if ( !( m_style & wxFC_SAVE ) ) | |
1364 | { | |
1365 | if ( ( filename.Find( wxT( '*' ) ) != wxNOT_FOUND ) || | |
1366 | ( filename.Find( wxT( '?' ) ) != wxNOT_FOUND ) ) | |
1367 | { | |
1368 | if ( filename.Find( wxFILE_SEP_PATH ) != wxNOT_FOUND ) | |
1369 | { | |
1370 | wxMessageBox( _( "Illegal file specification." ), | |
1371 | _( "Error" ), wxOK | wxICON_ERROR, this ); | |
1372 | return; | |
1373 | } | |
1374 | m_list->SetWild( filename ); | |
1375 | return; | |
1376 | } | |
1377 | } | |
1378 | ||
1379 | if ( !IsTopMostDir( dir ) ) | |
1380 | dir += wxFILE_SEP_PATH; | |
1381 | if ( !wxIsAbsolutePath( filename ) ) | |
1382 | { | |
1383 | dir += filename; | |
1384 | filename = dir; | |
1385 | } | |
1386 | ||
1387 | if ( wxDirExists( filename ) ) | |
1388 | { | |
1389 | m_ignoreChanges = true; | |
1390 | m_list->GoToDir( filename ); | |
1391 | UpdateControls(); | |
1392 | ||
1393 | GenerateFolderChangedEvent( this, this ); | |
1394 | ||
1395 | m_ignoreChanges = false; | |
1396 | return; | |
1397 | } | |
1398 | ||
1399 | // they really wanted a dir, but it doesn't exist | |
1400 | if ( want_dir ) | |
1401 | { | |
1402 | wxMessageBox( _( "Directory doesn't exist." ), _( "Error" ), | |
1403 | wxOK | wxICON_ERROR, this ); | |
1404 | return; | |
1405 | } | |
1406 | ||
1407 | // append the default extension to the filename if it doesn't have any | |
1408 | // | |
1409 | // VZ: the logic of testing for !wxFileExists() only for the open file | |
1410 | // dialog is not entirely clear to me, why don't we allow saving to a | |
1411 | // file without extension as well? | |
1412 | if ( !( m_style & wxFC_OPEN ) || !wxFileExists( filename ) ) | |
1413 | { | |
1414 | filename = wxFileDialogBase::AppendExtension( filename, m_filterExtension ); | |
1415 | GenerateFileActivatedEvent( this, this, wxFileName( filename ).GetFullName() ); | |
1416 | return; | |
1417 | } | |
1418 | ||
1419 | GenerateFileActivatedEvent( this, this ); | |
1420 | } | |
1421 | ||
1422 | bool wxGenericFileCtrl::SetPath( const wxString& path ) | |
1423 | { | |
1424 | if ( !wxFileName::FileExists( ( path ) ) ) | |
1425 | return false; | |
1426 | ||
1427 | wxString ext; | |
bd365871 | 1428 | wxFileName::SplitPath( path, &m_dir, &m_fileName, &ext ); |
0cf3e587 VZ |
1429 | if ( !ext.empty() ) |
1430 | { | |
1431 | m_fileName += wxT( "." ); | |
1432 | m_fileName += ext; | |
1433 | } | |
1434 | ||
1435 | SetDirectory( m_dir ); | |
1436 | SetFilename( m_fileName ); | |
1437 | ||
1438 | return true; | |
1439 | } | |
1440 | ||
1441 | void wxGenericFileCtrl::GetPaths( wxArrayString& paths ) const | |
1442 | { | |
1443 | DoGetFilenames( paths, true ); | |
1444 | } | |
1445 | ||
1446 | void wxGenericFileCtrl::GetFilenames( wxArrayString& files ) const | |
1447 | { | |
1448 | DoGetFilenames( files, false ); | |
1449 | } | |
1450 | ||
1451 | void wxGenericFileCtrl::UpdateControls() | |
1452 | { | |
1453 | const wxString dir = m_list->GetDir(); | |
1454 | m_static->SetLabel( dir ); | |
1455 | } | |
1456 | ||
1457 | void wxGenericFileCtrl::GoToParentDir() | |
1458 | { | |
1459 | m_list->GoToParentDir(); | |
1460 | UpdateControls(); | |
1461 | } | |
1462 | ||
1463 | void wxGenericFileCtrl::GoToHomeDir() | |
1464 | { | |
1465 | m_list->GoToHomeDir(); | |
1466 | UpdateControls(); | |
1467 | } | |
1468 | ||
0cf3e587 | 1469 | #endif // wxUSE_FILECTRL |