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