]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirctrlg.cpp
Further refine of #15226: wxRichTextCtrl: Implement setting properties with undo...
[wxWidgets.git] / src / generic / dirctrlg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/dirctrlg.cpp
3 // Purpose: wxGenericDirCtrl
4 // Author: Harm van der Heijden, Robert Roebling, Julian Smart
5 // Modified by:
6 // Created: 12/12/98
7 // Copyright: (c) Harm van der Heijden, Robert Roebling and Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #if wxUSE_DIRDLG || wxUSE_FILEDLG
19
20 #include "wx/generic/dirctrlg.h"
21
22 #ifndef WX_PRECOMP
23 #include "wx/hash.h"
24 #include "wx/intl.h"
25 #include "wx/log.h"
26 #include "wx/utils.h"
27 #include "wx/button.h"
28 #include "wx/icon.h"
29 #include "wx/settings.h"
30 #include "wx/msgdlg.h"
31 #include "wx/choice.h"
32 #include "wx/textctrl.h"
33 #include "wx/layout.h"
34 #include "wx/sizer.h"
35 #include "wx/textdlg.h"
36 #include "wx/gdicmn.h"
37 #include "wx/image.h"
38 #include "wx/module.h"
39 #endif
40
41 #include "wx/filename.h"
42 #include "wx/filefn.h"
43 #include "wx/imaglist.h"
44 #include "wx/tokenzr.h"
45 #include "wx/dir.h"
46 #include "wx/artprov.h"
47 #include "wx/mimetype.h"
48
49 #if wxUSE_STATLINE
50 #include "wx/statline.h"
51 #endif
52
53 #if defined(__WXMAC__)
54 #include "wx/osx/private.h" // includes mac headers
55 #endif
56
57 #ifdef __WINDOWS__
58 #include <windows.h>
59 #include "wx/msw/winundef.h"
60 #include "wx/volume.h"
61
62 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
63 // older releases don't, but it should be verified and the checks modified
64 // accordingly.
65 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
66 #if !defined(__WXWINCE__)
67 #include <direct.h>
68 #endif
69 #include <stdlib.h>
70 #include <ctype.h>
71 #endif
72
73 #endif // __WINDOWS__
74
75 #if defined(__OS2__) || defined(__DOS__)
76 #ifdef __OS2__
77 #define INCL_BASE
78 #include <os2.h>
79 #ifndef __EMX__
80 #include <direct.h>
81 #endif
82 #include <stdlib.h>
83 #include <ctype.h>
84 #endif
85 #endif // __OS2__
86
87 #if defined(__WXMAC__)
88 // #include "MoreFilesX.h"
89 #endif
90
91 #ifdef __BORLANDC__
92 #include "dos.h"
93 #endif
94
95 extern WXDLLEXPORT_DATA(const char) wxFileSelectorDefaultWildcardStr[];
96
97 // If compiled under Windows, this macro can cause problems
98 #ifdef GetFirstChild
99 #undef GetFirstChild
100 #endif
101
102 bool wxIsDriveAvailable(const wxString& dirName);
103
104 // ----------------------------------------------------------------------------
105 // events
106 // ----------------------------------------------------------------------------
107
108 wxDEFINE_EVENT( wxEVT_DIRCTRL_SELECTIONCHANGED, wxTreeEvent );
109 wxDEFINE_EVENT( wxEVT_DIRCTRL_FILEACTIVATED, wxTreeEvent );
110
111 // ----------------------------------------------------------------------------
112 // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
113 // ----------------------------------------------------------------------------
114
115 size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
116 {
117 #ifdef wxHAS_FILESYSTEM_VOLUMES
118
119 #ifdef __WXWINCE__
120 // No logical drives; return "\"
121 paths.Add(wxT("\\"));
122 names.Add(wxT("\\"));
123 icon_ids.Add(wxFileIconsTable::computer);
124 #elif defined(__WIN32__) && wxUSE_FSVOLUME
125 // TODO: this code (using wxFSVolumeBase) should be used for all platforms
126 // but unfortunately wxFSVolumeBase is not implemented everywhere
127 const wxArrayString as = wxFSVolumeBase::GetVolumes();
128
129 for (size_t i = 0; i < as.GetCount(); i++)
130 {
131 wxString path = as[i];
132 wxFSVolume vol(path);
133 int imageId;
134 switch (vol.GetKind())
135 {
136 case wxFS_VOL_FLOPPY:
137 if ( (path == wxT("a:\\")) || (path == wxT("b:\\")) )
138 imageId = wxFileIconsTable::floppy;
139 else
140 imageId = wxFileIconsTable::removeable;
141 break;
142 case wxFS_VOL_DVDROM:
143 case wxFS_VOL_CDROM:
144 imageId = wxFileIconsTable::cdrom;
145 break;
146 case wxFS_VOL_NETWORK:
147 if (path[0] == wxT('\\'))
148 continue; // skip "\\computer\folder"
149 imageId = wxFileIconsTable::drive;
150 break;
151 case wxFS_VOL_DISK:
152 case wxFS_VOL_OTHER:
153 default:
154 imageId = wxFileIconsTable::drive;
155 break;
156 }
157 paths.Add(path);
158 names.Add(vol.GetDisplayName());
159 icon_ids.Add(imageId);
160 }
161 #elif defined(__OS2__)
162 APIRET rc;
163 ULONG ulDriveNum = 0;
164 ULONG ulDriveMap = 0;
165 rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap);
166 if ( rc == 0)
167 {
168 size_t i = 0;
169 while (i < 26)
170 {
171 if (ulDriveMap & ( 1 << i ))
172 {
173 const wxString path = wxFileName::GetVolumeString(
174 'A' + i, wxPATH_GET_SEPARATOR);
175 const wxString name = wxFileName::GetVolumeString(
176 'A' + i, wxPATH_NO_SEPARATOR);
177
178 // Note: If _filesys is unsupported by some compilers,
179 // we can always replace it by DosQueryFSAttach
180 char filesysname[20];
181 #ifdef __WATCOMC__
182 ULONG cbBuffer = sizeof(filesysname);
183 PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)filesysname;
184 APIRET rc = ::DosQueryFSAttach(name.fn_str(),0,FSAIL_QUERYNAME,pfsqBuffer,&cbBuffer);
185 if (rc != NO_ERROR)
186 {
187 filesysname[0] = '\0';
188 }
189 #else
190 _filesys(name.fn_str(), filesysname, sizeof(filesysname));
191 #endif
192 /* FAT, LAN, HPFS, CDFS, NFS */
193 int imageId;
194 if (path == wxT("A:\\") || path == wxT("B:\\"))
195 imageId = wxFileIconsTable::floppy;
196 else if (!strcmp(filesysname, "CDFS"))
197 imageId = wxFileIconsTable::cdrom;
198 else if (!strcmp(filesysname, "LAN") ||
199 !strcmp(filesysname, "NFS"))
200 imageId = wxFileIconsTable::drive;
201 else
202 imageId = wxFileIconsTable::drive;
203 paths.Add(path);
204 names.Add(name);
205 icon_ids.Add(imageId);
206 }
207 i ++;
208 }
209 }
210 #else // !__WIN32__, !__OS2__
211 /* If we can switch to the drive, it exists. */
212 for ( char drive = 'A'; drive <= 'Z'; drive++ )
213 {
214 const wxString
215 path = wxFileName::GetVolumeString(drive, wxPATH_GET_SEPARATOR);
216
217 if (wxIsDriveAvailable(path))
218 {
219 paths.Add(path);
220 names.Add(wxFileName::GetVolumeString(drive, wxPATH_NO_SEPARATOR));
221 icon_ids.Add(drive <= 2 ? wxFileIconsTable::floppy
222 : wxFileIconsTable::drive);
223 }
224 }
225 #endif // __WIN32__/!__WIN32__
226
227 #elif defined(__WXMAC__) && wxOSX_USE_COCOA_OR_CARBON
228
229 ItemCount volumeIndex = 1;
230 OSErr err = noErr ;
231
232 while( noErr == err )
233 {
234 HFSUniStr255 volumeName ;
235 FSRef fsRef ;
236 FSVolumeInfo volumeInfo ;
237 err = FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoFlags , &volumeInfo , &volumeName, &fsRef);
238 if( noErr == err )
239 {
240 wxString path = wxMacFSRefToPath( &fsRef ) ;
241 wxString name = wxMacHFSUniStrToString( &volumeName ) ;
242
243 if ( (volumeInfo.flags & kFSVolFlagSoftwareLockedMask) || (volumeInfo.flags & kFSVolFlagHardwareLockedMask) )
244 {
245 icon_ids.Add(wxFileIconsTable::cdrom);
246 }
247 else
248 {
249 icon_ids.Add(wxFileIconsTable::drive);
250 }
251 // todo other removable
252
253 paths.Add(path);
254 names.Add(name);
255 volumeIndex++ ;
256 }
257 }
258
259 #elif defined(__UNIX__)
260 paths.Add(wxT("/"));
261 names.Add(wxT("/"));
262 icon_ids.Add(wxFileIconsTable::computer);
263 #else
264 #error "Unsupported platform in wxGenericDirCtrl!"
265 #endif
266 wxASSERT_MSG( (paths.GetCount() == names.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
267 wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives."));
268 return paths.GetCount();
269 }
270
271 // ----------------------------------------------------------------------------
272 // wxIsDriveAvailable
273 // ----------------------------------------------------------------------------
274
275 #if defined(__DOS__)
276
277 bool wxIsDriveAvailable(const wxString& dirName)
278 {
279 // FIXME: this method leads to hang up under Watcom for some reason
280 #ifdef __WATCOMC__
281 wxUnusedVar(dirName);
282 #else
283 if ( dirName.length() == 3 && dirName[1u] == wxT(':') )
284 {
285 wxString dirNameLower(dirName.Lower());
286 // VS: always return true for removable media, since Win95 doesn't
287 // like it when MS-DOS app accesses empty floppy drive
288 return (dirNameLower[0u] == wxT('a') ||
289 dirNameLower[0u] == wxT('b') ||
290 wxDirExists(dirNameLower));
291 }
292 else
293 #endif
294 return true;
295 }
296
297 #elif defined(__WINDOWS__) || defined(__OS2__)
298
299 int setdrive(int WXUNUSED_IN_WINCE(drive))
300 {
301 #ifdef __WXWINCE__
302 return 0;
303 #elif defined(__GNUWIN32__) && \
304 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
305 return _chdrive(drive);
306 #else
307 wxChar newdrive[4];
308
309 if (drive < 1 || drive > 31)
310 return -1;
311 newdrive[0] = (wxChar)(wxT('A') + drive - 1);
312 newdrive[1] = wxT(':');
313 #ifdef __OS2__
314 newdrive[2] = wxT('\\');
315 newdrive[3] = wxT('\0');
316 #else
317 newdrive[2] = wxT('\0');
318 #endif
319 #if defined(__WINDOWS__)
320 if (::SetCurrentDirectory(newdrive))
321 #else
322 // VA doesn't know what LPSTR is and has its own set
323 if (!DosSetCurrentDir((PSZ)newdrive))
324 #endif
325 return 0;
326 else
327 return -1;
328 #endif // !GNUWIN32
329 }
330
331 bool wxIsDriveAvailable(const wxString& WXUNUSED_IN_WINCE(dirName))
332 {
333 #ifdef __WXWINCE__
334 return false;
335 #else
336 #ifdef __WIN32__
337 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
338 #endif
339 bool success = true;
340
341 // Check if this is a root directory and if so,
342 // whether the drive is available.
343 if (dirName.length() == 3 && dirName[(size_t)1] == wxT(':'))
344 {
345 wxString dirNameLower(dirName.Lower());
346 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
347 success = wxDirExists(dirNameLower);
348 #else
349 #if defined(__OS2__)
350 // Avoid changing to drive since no media may be inserted.
351 if (dirNameLower[(size_t)0] == 'a' || dirNameLower[(size_t)0] == 'b')
352 return success;
353 #endif
354 int currentDrive = _getdrive();
355 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
356 int err = setdrive( thisDrive ) ;
357 setdrive( currentDrive );
358
359 if (err == -1)
360 {
361 success = false;
362 }
363 #endif
364 }
365 #ifdef __WIN32__
366 (void) SetErrorMode(errorMode);
367 #endif
368
369 return success;
370 #endif
371 }
372 #endif // __WINDOWS__ || __OS2__
373
374 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
375
376
377
378 #if wxUSE_DIRDLG
379
380 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
381 // and sort regardless of case.
382 static int wxCMPFUNC_CONV wxDirCtrlStringCompareFunction(const wxString& strFirst, const wxString& strSecond)
383 {
384 return strFirst.CmpNoCase(strSecond);
385 }
386
387 //-----------------------------------------------------------------------------
388 // wxDirItemData
389 //-----------------------------------------------------------------------------
390
391 wxDirItemData::wxDirItemData(const wxString& path, const wxString& name,
392 bool isDir)
393 {
394 m_path = path;
395 m_name = name;
396 /* Insert logic to detect hidden files here
397 * In UnixLand we just check whether the first char is a dot
398 * For FileNameFromPath read LastDirNameInThisPath ;-) */
399 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
400 m_isHidden = false;
401 m_isExpanded = false;
402 m_isDir = isDir;
403 }
404
405 void wxDirItemData::SetNewDirName(const wxString& path)
406 {
407 m_path = path;
408 m_name = wxFileNameFromPath(path);
409 }
410
411 bool wxDirItemData::HasSubDirs() const
412 {
413 if (m_path.empty())
414 return false;
415
416 wxDir dir;
417 {
418 wxLogNull nolog;
419 if ( !dir.Open(m_path) )
420 return false;
421 }
422
423 return dir.HasSubDirs();
424 }
425
426 bool wxDirItemData::HasFiles(const wxString& WXUNUSED(spec)) const
427 {
428 if (m_path.empty())
429 return false;
430
431 wxDir dir;
432 {
433 wxLogNull nolog;
434 if ( !dir.Open(m_path) )
435 return false;
436 }
437
438 return dir.HasFiles();
439 }
440
441 //-----------------------------------------------------------------------------
442 // wxGenericDirCtrl
443 //-----------------------------------------------------------------------------
444
445 BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
446 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL, wxGenericDirCtrl::OnExpandItem)
447 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL, wxGenericDirCtrl::OnCollapseItem)
448 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnBeginEditItem)
449 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnEndEditItem)
450 EVT_TREE_SEL_CHANGED (wxID_TREECTRL, wxGenericDirCtrl::OnTreeSelChange)
451 EVT_TREE_ITEM_ACTIVATED (wxID_TREECTRL, wxGenericDirCtrl::OnItemActivated)
452 EVT_SIZE (wxGenericDirCtrl::OnSize)
453 END_EVENT_TABLE()
454
455 wxGenericDirCtrl::wxGenericDirCtrl(void)
456 {
457 Init();
458 }
459
460 void wxGenericDirCtrl::ExpandRoot()
461 {
462 ExpandDir(m_rootId); // automatically expand first level
463
464 // Expand and select the default path
465 if (!m_defaultPath.empty())
466 {
467 ExpandPath(m_defaultPath);
468 }
469 #ifdef __UNIX__
470 else
471 {
472 // On Unix, there's only one node under the (hidden) root node. It
473 // represents the / path, so the user would always have to expand it;
474 // let's do it ourselves
475 ExpandPath( wxT("/") );
476 }
477 #endif
478 }
479
480 bool wxGenericDirCtrl::Create(wxWindow *parent,
481 const wxWindowID treeid,
482 const wxString& dir,
483 const wxPoint& pos,
484 const wxSize& size,
485 long style,
486 const wxString& filter,
487 int defaultFilter,
488 const wxString& name)
489 {
490 if (!wxControl::Create(parent, treeid, pos, size, style, wxDefaultValidator, name))
491 return false;
492
493 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
494 SetForegroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_WINDOWTEXT));
495
496 Init();
497
498 long treeStyle = wxTR_HAS_BUTTONS;
499
500 // On Windows CE, if you hide the root, you get a crash when
501 // attempting to access data for children of the root item.
502 #ifndef __WXWINCE__
503 treeStyle |= wxTR_HIDE_ROOT;
504 #endif
505
506 #ifdef __WXGTK20__
507 treeStyle |= wxTR_NO_LINES;
508 #endif
509
510 if (style & wxDIRCTRL_EDIT_LABELS)
511 treeStyle |= wxTR_EDIT_LABELS;
512
513 if (style & wxDIRCTRL_MULTIPLE)
514 treeStyle |= wxTR_MULTIPLE;
515
516 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
517 treeStyle |= wxNO_BORDER;
518
519 m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL,
520 wxPoint(0,0), GetClientSize(), treeStyle);
521
522 if (!filter.empty() && (style & wxDIRCTRL_SHOW_FILTERS))
523 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL);
524
525 m_defaultPath = dir;
526 m_filter = filter;
527
528 if (m_filter.empty())
529 m_filter = wxFileSelectorDefaultWildcardStr;
530
531 SetFilterIndex(defaultFilter);
532
533 if (m_filterListCtrl)
534 m_filterListCtrl->FillFilterList(filter, defaultFilter);
535
536 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
537
538 m_showHidden = false;
539 wxDirItemData* rootData = new wxDirItemData(wxEmptyString, wxEmptyString, true);
540
541 wxString rootName;
542
543 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
544 rootName = _("Computer");
545 #else
546 rootName = _("Sections");
547 #endif
548
549 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
550 m_treeCtrl->SetItemHasChildren(m_rootId);
551
552 ExpandRoot();
553
554 SetInitialSize(size);
555 DoResize();
556
557 return true;
558 }
559
560 wxGenericDirCtrl::~wxGenericDirCtrl()
561 {
562 }
563
564 void wxGenericDirCtrl::Init()
565 {
566 m_showHidden = false;
567 m_currentFilter = 0;
568 m_currentFilterStr = wxEmptyString; // Default: any file
569 m_treeCtrl = NULL;
570 m_filterListCtrl = NULL;
571 }
572
573 wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID treeid, const wxPoint& pos, const wxSize& size, long treeStyle)
574 {
575 return new wxTreeCtrl(parent, treeid, pos, size, treeStyle);
576 }
577
578 void wxGenericDirCtrl::ShowHidden( bool show )
579 {
580 if ( m_showHidden == show )
581 return;
582
583 m_showHidden = show;
584
585 if ( HasFlag(wxDIRCTRL_MULTIPLE) )
586 {
587 wxArrayString paths;
588 GetPaths(paths);
589 ReCreateTree();
590 for ( unsigned n = 0; n < paths.size(); n++ )
591 {
592 ExpandPath(paths[n]);
593 }
594 }
595 else
596 {
597 wxString path = GetPath();
598 ReCreateTree();
599 SetPath(path);
600 }
601 }
602
603 const wxTreeItemId
604 wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
605 {
606 wxDirItemData *dir_item = new wxDirItemData(path,name,true);
607
608 wxTreeItemId treeid = AppendItem( m_rootId, name, imageId, -1, dir_item);
609
610 m_treeCtrl->SetItemHasChildren(treeid);
611
612 return treeid;
613 }
614
615 void wxGenericDirCtrl::SetupSections()
616 {
617 wxArrayString paths, names;
618 wxArrayInt icons;
619
620 size_t n, count = wxGetAvailableDrives(paths, names, icons);
621
622 #ifdef __WXGTK20__
623 wxString home = wxGetHomeDir();
624 AddSection( home, _("Home directory"), 1);
625 home += wxT("/Desktop");
626 AddSection( home, _("Desktop"), 1);
627 #endif
628
629 for (n = 0; n < count; n++)
630 AddSection(paths[n], names[n], icons[n]);
631 }
632
633 void wxGenericDirCtrl::SetFocus()
634 {
635 // we don't need focus ourselves, give it to the tree so that the user
636 // could navigate it
637 if (m_treeCtrl)
638 m_treeCtrl->SetFocus();
639 }
640
641 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
642 {
643 // don't rename the main entry "Sections"
644 if (event.GetItem() == m_rootId)
645 {
646 event.Veto();
647 return;
648 }
649
650 // don't rename the individual sections
651 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
652 {
653 event.Veto();
654 return;
655 }
656 }
657
658 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
659 {
660 if (event.IsEditCancelled())
661 return;
662
663 if ((event.GetLabel().empty()) ||
664 (event.GetLabel() == wxT(".")) ||
665 (event.GetLabel() == wxT("..")) ||
666 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
667 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
668 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
669 {
670 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
671 dialog.ShowModal();
672 event.Veto();
673 return;
674 }
675
676 wxTreeItemId treeid = event.GetItem();
677 wxDirItemData *data = GetItemData( treeid );
678 wxASSERT( data );
679
680 wxString new_name( wxPathOnly( data->m_path ) );
681 new_name += wxString(wxFILE_SEP_PATH);
682 new_name += event.GetLabel();
683
684 wxLogNull log;
685
686 if (wxFileExists(new_name))
687 {
688 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
689 dialog.ShowModal();
690 event.Veto();
691 }
692
693 if (wxRenameFile(data->m_path,new_name))
694 {
695 data->SetNewDirName( new_name );
696 }
697 else
698 {
699 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
700 dialog.ShowModal();
701 event.Veto();
702 }
703 }
704
705 void wxGenericDirCtrl::OnTreeSelChange(wxTreeEvent &event)
706 {
707 wxTreeEvent changedEvent(wxEVT_DIRCTRL_SELECTIONCHANGED, GetId());
708
709 changedEvent.SetEventObject(this);
710 changedEvent.SetItem(event.GetItem());
711 changedEvent.SetClientObject(m_treeCtrl->GetItemData(event.GetItem()));
712
713 if (GetEventHandler()->SafelyProcessEvent(changedEvent) && !changedEvent.IsAllowed())
714 event.Veto();
715 else
716 event.Skip();
717 }
718
719 void wxGenericDirCtrl::OnItemActivated(wxTreeEvent &event)
720 {
721 wxTreeItemId treeid = event.GetItem();
722 const wxDirItemData *data = GetItemData(treeid);
723
724 if (data->m_isDir)
725 {
726 // is dir
727 event.Skip();
728 }
729 else
730 {
731 // is file
732 wxTreeEvent changedEvent(wxEVT_DIRCTRL_FILEACTIVATED, GetId());
733
734 changedEvent.SetEventObject(this);
735 changedEvent.SetItem(treeid);
736 changedEvent.SetClientObject(m_treeCtrl->GetItemData(treeid));
737
738 if (GetEventHandler()->SafelyProcessEvent(changedEvent) && !changedEvent.IsAllowed())
739 event.Veto();
740 else
741 event.Skip();
742 }
743 }
744
745 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
746 {
747 wxTreeItemId parentId = event.GetItem();
748
749 // VS: this is needed because the event handler is called from wxTreeCtrl
750 // ctor when wxTR_HIDE_ROOT was specified
751
752 if (!m_rootId.IsOk())
753 m_rootId = m_treeCtrl->GetRootItem();
754
755 ExpandDir(parentId);
756 }
757
758 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
759 {
760 CollapseDir(event.GetItem());
761 }
762
763 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
764 {
765 wxTreeItemId child;
766
767 wxDirItemData *data = GetItemData(parentId);
768 if (!data->m_isExpanded)
769 return;
770
771 data->m_isExpanded = false;
772
773 m_treeCtrl->Freeze();
774 if (parentId != m_treeCtrl->GetRootItem())
775 m_treeCtrl->CollapseAndReset(parentId);
776 m_treeCtrl->DeleteChildren(parentId);
777 m_treeCtrl->Thaw();
778 }
779
780 void wxGenericDirCtrl::PopulateNode(wxTreeItemId parentId)
781 {
782 wxDirItemData *data = GetItemData(parentId);
783
784 if (data->m_isExpanded)
785 return;
786
787 data->m_isExpanded = true;
788
789 if (parentId == m_treeCtrl->GetRootItem())
790 {
791 SetupSections();
792 return;
793 }
794
795 wxASSERT(data);
796
797 wxString search,path,filename;
798
799 wxString dirName(data->m_path);
800
801 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
802 // Check if this is a root directory and if so,
803 // whether the drive is available.
804 if (!wxIsDriveAvailable(dirName))
805 {
806 data->m_isExpanded = false;
807 //wxMessageBox(wxT("Sorry, this drive is not available."));
808 return;
809 }
810 #endif
811
812 // This may take a longish time. Go to busy cursor
813 wxBusyCursor busy;
814
815 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
816 if (dirName.Last() == ':')
817 dirName += wxString(wxFILE_SEP_PATH);
818 #endif
819
820 wxArrayString dirs;
821 wxArrayString filenames;
822
823 wxDir d;
824 wxString eachFilename;
825
826 wxLogNull log;
827 d.Open(dirName);
828
829 if (d.IsOpened())
830 {
831 int style = wxDIR_DIRS;
832 if (m_showHidden) style |= wxDIR_HIDDEN;
833 if (d.GetFirst(& eachFilename, wxEmptyString, style))
834 {
835 do
836 {
837 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
838 {
839 dirs.Add(eachFilename);
840 }
841 }
842 while (d.GetNext(&eachFilename));
843 }
844 }
845 dirs.Sort(wxDirCtrlStringCompareFunction);
846
847 // Now do the filenames -- but only if we're allowed to
848 if (!HasFlag(wxDIRCTRL_DIR_ONLY))
849 {
850 d.Open(dirName);
851
852 if (d.IsOpened())
853 {
854 int style = wxDIR_FILES;
855 if (m_showHidden) style |= wxDIR_HIDDEN;
856 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
857 wxStringTokenizer strTok;
858 wxString curFilter;
859 strTok.SetString(m_currentFilterStr,wxT(";"));
860 while(strTok.HasMoreTokens())
861 {
862 curFilter = strTok.GetNextToken();
863 if (d.GetFirst(& eachFilename, curFilter, style))
864 {
865 do
866 {
867 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
868 {
869 filenames.Add(eachFilename);
870 }
871 }
872 while (d.GetNext(& eachFilename));
873 }
874 }
875 }
876 filenames.Sort(wxDirCtrlStringCompareFunction);
877 }
878
879 // Now we really know whether we have any children so tell the tree control
880 // about it.
881 m_treeCtrl->SetItemHasChildren(parentId, !dirs.empty() || !filenames.empty());
882
883 // Add the sorted dirs
884 size_t i;
885 for (i = 0; i < dirs.GetCount(); i++)
886 {
887 eachFilename = dirs[i];
888 path = dirName;
889 if (!wxEndsWithPathSeparator(path))
890 path += wxString(wxFILE_SEP_PATH);
891 path += eachFilename;
892
893 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true);
894 wxTreeItemId treeid = AppendItem( parentId, eachFilename,
895 wxFileIconsTable::folder, -1, dir_item);
896 m_treeCtrl->SetItemImage( treeid, wxFileIconsTable::folder_open,
897 wxTreeItemIcon_Expanded );
898
899 // assume that it does have children by default as it can take a long
900 // time to really check for this (think remote drives...)
901 //
902 // and if we're wrong, we'll correct the icon later if
903 // the user really tries to open this item
904 m_treeCtrl->SetItemHasChildren(treeid);
905 }
906
907 // Add the sorted filenames
908 if (!HasFlag(wxDIRCTRL_DIR_ONLY))
909 {
910 for (i = 0; i < filenames.GetCount(); i++)
911 {
912 eachFilename = filenames[i];
913 path = dirName;
914 if (!wxEndsWithPathSeparator(path))
915 path += wxString(wxFILE_SEP_PATH);
916 path += eachFilename;
917 //path = dirName + wxString(wxT("/")) + eachFilename;
918 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false);
919 int image_id = wxFileIconsTable::file;
920 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
921 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
922 (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item);
923 }
924 }
925 }
926
927 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
928 {
929 // ExpandDir() will not actually expand the tree node, just populate it
930 PopulateNode(parentId);
931 }
932
933 void wxGenericDirCtrl::ReCreateTree()
934 {
935 CollapseDir(m_treeCtrl->GetRootItem());
936 ExpandRoot();
937 }
938
939 void wxGenericDirCtrl::CollapseTree()
940 {
941 wxTreeItemIdValue cookie;
942 wxTreeItemId child = m_treeCtrl->GetFirstChild(m_rootId, cookie);
943 while (child.IsOk())
944 {
945 CollapseDir(child);
946 child = m_treeCtrl->GetNextChild(m_rootId, cookie);
947 }
948 }
949
950 // Find the child that matches the first part of 'path'.
951 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
952 // then the child for /usr is returned.
953 wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
954 {
955 wxString path2(path);
956
957 // Make sure all separators are as per the current platform
958 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
959 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
960
961 // Append a separator to foil bogus substring matching
962 path2 += wxString(wxFILE_SEP_PATH);
963
964 // In MSW or PM, case is not significant
965 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
966 path2.MakeLower();
967 #endif
968
969 wxTreeItemIdValue cookie;
970 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
971 while (childId.IsOk())
972 {
973 wxDirItemData* data = GetItemData(childId);
974
975 if (data && !data->m_path.empty())
976 {
977 wxString childPath(data->m_path);
978 if (!wxEndsWithPathSeparator(childPath))
979 childPath += wxString(wxFILE_SEP_PATH);
980
981 // In MSW and PM, case is not significant
982 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
983 childPath.MakeLower();
984 #endif
985
986 if (childPath.length() <= path2.length())
987 {
988 wxString path3 = path2.Mid(0, childPath.length());
989 if (childPath == path3)
990 {
991 if (path3.length() == path2.length())
992 done = true;
993 else
994 done = false;
995 return childId;
996 }
997 }
998 }
999
1000 childId = m_treeCtrl->GetNextChild(parentId, cookie);
1001 }
1002 wxTreeItemId invalid;
1003 return invalid;
1004 }
1005
1006 // Try to expand as much of the given path as possible,
1007 // and select the given tree item.
1008 bool wxGenericDirCtrl::ExpandPath(const wxString& path)
1009 {
1010 bool done = false;
1011 wxTreeItemId treeid = FindChild(m_rootId, path, done);
1012 wxTreeItemId lastId = treeid; // The last non-zero treeid
1013 while (treeid.IsOk() && !done)
1014 {
1015 ExpandDir(treeid);
1016
1017 treeid = FindChild(treeid, path, done);
1018 if (treeid.IsOk())
1019 lastId = treeid;
1020 }
1021 if (!lastId.IsOk())
1022 return false;
1023
1024 wxDirItemData *data = GetItemData(lastId);
1025 if (data->m_isDir)
1026 {
1027 m_treeCtrl->Expand(lastId);
1028 }
1029 if (HasFlag(wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
1030 {
1031 // Find the first file in this directory
1032 wxTreeItemIdValue cookie;
1033 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
1034 bool selectedChild = false;
1035 while (childId.IsOk())
1036 {
1037 data = GetItemData(childId);
1038
1039 if (data && data->m_path != wxEmptyString && !data->m_isDir)
1040 {
1041 m_treeCtrl->SelectItem(childId);
1042 m_treeCtrl->EnsureVisible(childId);
1043 selectedChild = true;
1044 break;
1045 }
1046 childId = m_treeCtrl->GetNextChild(lastId, cookie);
1047 }
1048 if (!selectedChild)
1049 {
1050 m_treeCtrl->SelectItem(lastId);
1051 m_treeCtrl->EnsureVisible(lastId);
1052 }
1053 }
1054 else
1055 {
1056 m_treeCtrl->SelectItem(lastId);
1057 m_treeCtrl->EnsureVisible(lastId);
1058 }
1059
1060 return true;
1061 }
1062
1063
1064 bool wxGenericDirCtrl::CollapsePath(const wxString& path)
1065 {
1066 bool done = false;
1067 wxTreeItemId treeid = FindChild(m_rootId, path, done);
1068 wxTreeItemId lastId = treeid; // The last non-zero treeid
1069
1070 while ( treeid.IsOk() && !done )
1071 {
1072 CollapseDir(treeid);
1073
1074 treeid = FindChild(treeid, path, done);
1075
1076 if ( treeid.IsOk() )
1077 lastId = treeid;
1078 }
1079
1080 if ( !lastId.IsOk() )
1081 return false;
1082
1083 m_treeCtrl->SelectItem(lastId);
1084 m_treeCtrl->EnsureVisible(lastId);
1085
1086 return true;
1087 }
1088
1089 wxDirItemData* wxGenericDirCtrl::GetItemData(wxTreeItemId itemId)
1090 {
1091 return static_cast<wxDirItemData*>(m_treeCtrl->GetItemData(itemId));
1092 }
1093
1094 wxString wxGenericDirCtrl::GetPath(wxTreeItemId itemId) const
1095 {
1096 const wxDirItemData*
1097 data = static_cast<wxDirItemData*>(m_treeCtrl->GetItemData(itemId));
1098
1099 return data->m_path;
1100 }
1101
1102 wxString wxGenericDirCtrl::GetPath() const
1103 {
1104 // Allow calling GetPath() in multiple selection from OnSelFilter
1105 if (m_treeCtrl->HasFlag(wxTR_MULTIPLE))
1106 {
1107 wxArrayTreeItemIds items;
1108 m_treeCtrl->GetSelections(items);
1109 if (items.size() > 0)
1110 {
1111 // return first string only
1112 wxTreeItemId treeid = items[0];
1113 return GetPath(treeid);
1114 }
1115
1116 return wxEmptyString;
1117 }
1118
1119 wxTreeItemId treeid = m_treeCtrl->GetSelection();
1120 if (treeid)
1121 {
1122 return GetPath(treeid);
1123 }
1124 else
1125 return wxEmptyString;
1126 }
1127
1128 void wxGenericDirCtrl::GetPaths(wxArrayString& paths) const
1129 {
1130 paths.clear();
1131
1132 wxArrayTreeItemIds items;
1133 m_treeCtrl->GetSelections(items);
1134 for ( unsigned n = 0; n < items.size(); n++ )
1135 {
1136 wxTreeItemId treeid = items[n];
1137 paths.push_back(GetPath(treeid));
1138 }
1139 }
1140
1141 wxString wxGenericDirCtrl::GetFilePath() const
1142 {
1143 wxTreeItemId treeid = m_treeCtrl->GetSelection();
1144 if (treeid)
1145 {
1146 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(treeid);
1147 if (data->m_isDir)
1148 return wxEmptyString;
1149 else
1150 return data->m_path;
1151 }
1152 else
1153 return wxEmptyString;
1154 }
1155
1156 void wxGenericDirCtrl::GetFilePaths(wxArrayString& paths) const
1157 {
1158 paths.clear();
1159
1160 wxArrayTreeItemIds items;
1161 m_treeCtrl->GetSelections(items);
1162 for ( unsigned n = 0; n < items.size(); n++ )
1163 {
1164 wxTreeItemId treeid = items[n];
1165 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(treeid);
1166 if ( !data->m_isDir )
1167 paths.Add(data->m_path);
1168 }
1169 }
1170
1171 void wxGenericDirCtrl::SetPath(const wxString& path)
1172 {
1173 m_defaultPath = path;
1174 if (m_rootId)
1175 ExpandPath(path);
1176 }
1177
1178 void wxGenericDirCtrl::SelectPath(const wxString& path, bool select)
1179 {
1180 bool done = false;
1181 wxTreeItemId treeid = FindChild(m_rootId, path, done);
1182 wxTreeItemId lastId = treeid; // The last non-zero treeid
1183 while ( treeid.IsOk() && !done )
1184 {
1185 treeid = FindChild(treeid, path, done);
1186 if ( treeid.IsOk() )
1187 lastId = treeid;
1188 }
1189 if ( !lastId.IsOk() )
1190 return;
1191
1192 if ( done )
1193 {
1194 m_treeCtrl->SelectItem(treeid, select);
1195 }
1196 }
1197
1198 void wxGenericDirCtrl::SelectPaths(const wxArrayString& paths)
1199 {
1200 if ( HasFlag(wxDIRCTRL_MULTIPLE) )
1201 {
1202 UnselectAll();
1203 for ( unsigned n = 0; n < paths.size(); n++ )
1204 {
1205 SelectPath(paths[n]);
1206 }
1207 }
1208 }
1209
1210 void wxGenericDirCtrl::UnselectAll()
1211 {
1212 m_treeCtrl->UnselectAll();
1213 }
1214
1215 // Not used
1216 #if 0
1217 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId treeid, int dirFlags, wxArrayString& filenames)
1218 {
1219 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(treeid);
1220
1221 // This may take a longish time. Go to busy cursor
1222 wxBusyCursor busy;
1223
1224 wxASSERT(data);
1225
1226 wxString search,path,filename;
1227
1228 wxString dirName(data->m_path);
1229
1230 #if defined(__WINDOWS__) || defined(__OS2__)
1231 if (dirName.Last() == ':')
1232 dirName += wxString(wxFILE_SEP_PATH);
1233 #endif
1234
1235 wxDir d;
1236 wxString eachFilename;
1237
1238 wxLogNull log;
1239 d.Open(dirName);
1240
1241 if (d.IsOpened())
1242 {
1243 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1244 {
1245 do
1246 {
1247 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1248 {
1249 filenames.Add(eachFilename);
1250 }
1251 }
1252 while (d.GetNext(& eachFilename)) ;
1253 }
1254 }
1255 }
1256 #endif
1257
1258 void wxGenericDirCtrl::SetFilterIndex(int n)
1259 {
1260 m_currentFilter = n;
1261
1262 wxString f, d;
1263 if (ExtractWildcard(m_filter, n, f, d))
1264 m_currentFilterStr = f;
1265 else
1266 #ifdef __UNIX__
1267 m_currentFilterStr = wxT("*");
1268 #else
1269 m_currentFilterStr = wxT("*.*");
1270 #endif
1271 }
1272
1273 void wxGenericDirCtrl::SetFilter(const wxString& filter)
1274 {
1275 m_filter = filter;
1276
1277 if (!filter.empty() && !m_filterListCtrl && HasFlag(wxDIRCTRL_SHOW_FILTERS))
1278 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL);
1279 else if (filter.empty() && m_filterListCtrl)
1280 {
1281 m_filterListCtrl->Destroy();
1282 m_filterListCtrl = NULL;
1283 }
1284
1285 wxString f, d;
1286 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1287 m_currentFilterStr = f;
1288 else
1289 #ifdef __UNIX__
1290 m_currentFilterStr = wxT("*");
1291 #else
1292 m_currentFilterStr = wxT("*.*");
1293 #endif
1294 // current filter index is meaningless after filter change, set it to zero
1295 SetFilterIndex(0);
1296 if (m_filterListCtrl)
1297 m_filterListCtrl->FillFilterList(m_filter, 0);
1298 }
1299
1300 // Extract description and actual filter from overall filter string
1301 bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1302 {
1303 wxArrayString filters, descriptions;
1304 int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters);
1305 if (count > 0 && n < count)
1306 {
1307 filter = filters[n];
1308 description = descriptions[n];
1309 return true;
1310 }
1311
1312 return false;
1313 }
1314
1315
1316 void wxGenericDirCtrl::DoResize()
1317 {
1318 wxSize sz = GetClientSize();
1319 int verticalSpacing = 3;
1320 if (m_treeCtrl)
1321 {
1322 wxSize filterSz ;
1323 if (m_filterListCtrl)
1324 {
1325 filterSz = m_filterListCtrl->GetSize();
1326 sz.y -= (filterSz.y + verticalSpacing);
1327 }
1328 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1329 if (m_filterListCtrl)
1330 {
1331 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1332 // Don't know why, but this needs refreshing after a resize (wxMSW)
1333 m_filterListCtrl->Refresh();
1334 }
1335 }
1336 }
1337
1338
1339 void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
1340 {
1341 DoResize();
1342 }
1343
1344 wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
1345 const wxString & text,
1346 int image, int selectedImage,
1347 wxTreeItemData * data)
1348 {
1349 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1350
1351 wxASSERT (treeCtrl);
1352
1353 if (treeCtrl)
1354 {
1355 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1356 }
1357 else
1358 {
1359 return wxTreeItemId();
1360 }
1361 }
1362
1363
1364 //-----------------------------------------------------------------------------
1365 // wxDirFilterListCtrl
1366 //-----------------------------------------------------------------------------
1367
1368 IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1369
1370 BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1371 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
1372 END_EVENT_TABLE()
1373
1374 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent,
1375 const wxWindowID treeid,
1376 const wxPoint& pos,
1377 const wxSize& size,
1378 long style)
1379 {
1380 m_dirCtrl = parent;
1381
1382 // by default our border style is determined by the style of our parent
1383 if ( !(style & wxBORDER_MASK) )
1384 {
1385 style |= parent->HasFlag(wxDIRCTRL_3D_INTERNAL) ? wxBORDER_SUNKEN
1386 : wxBORDER_NONE;
1387 }
1388
1389 return wxChoice::Create(parent, treeid, pos, size, 0, NULL, style);
1390 }
1391
1392 void wxDirFilterListCtrl::Init()
1393 {
1394 m_dirCtrl = NULL;
1395 }
1396
1397 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
1398 {
1399 int sel = GetSelection();
1400
1401 if (m_dirCtrl->HasFlag(wxDIRCTRL_MULTIPLE))
1402 {
1403 wxArrayString paths;
1404 m_dirCtrl->GetPaths(paths);
1405
1406 m_dirCtrl->SetFilterIndex(sel);
1407
1408 // If the filter has changed, the view is out of date, so
1409 // collapse the tree.
1410 m_dirCtrl->ReCreateTree();
1411
1412 // Expand and select the previously selected paths
1413 for (unsigned int i = 0; i < paths.GetCount(); i++)
1414 {
1415 m_dirCtrl->ExpandPath(paths.Item(i));
1416 }
1417 }
1418 else
1419 {
1420 wxString currentPath = m_dirCtrl->GetPath();
1421
1422 m_dirCtrl->SetFilterIndex(sel);
1423 m_dirCtrl->ReCreateTree();
1424
1425 // Try to restore the selection, or at least the directory
1426 m_dirCtrl->ExpandPath(currentPath);
1427 }
1428 }
1429
1430 void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1431 {
1432 Clear();
1433 wxArrayString descriptions, filters;
1434 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
1435
1436 if (n > 0 && defaultFilter < (int) n)
1437 {
1438 for (size_t i = 0; i < n; i++)
1439 Append(descriptions[i]);
1440 SetSelection(defaultFilter);
1441 }
1442 }
1443 #endif // wxUSE_DIRDLG
1444
1445 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1446
1447 // ----------------------------------------------------------------------------
1448 // wxFileIconsTable icons
1449 // ----------------------------------------------------------------------------
1450
1451 #ifndef __WXGTK20__
1452 /* Computer (c) Julian Smart */
1453 static const char* const file_icons_tbl_computer_xpm[] = {
1454 /* columns rows colors chars-per-pixel */
1455 "16 16 42 1",
1456 "r c #4E7FD0",
1457 "$ c #7198D9",
1458 "; c #DCE6F6",
1459 "q c #FFFFFF",
1460 "u c #4A7CCE",
1461 "# c #779DDB",
1462 "w c #95B2E3",
1463 "y c #7FA2DD",
1464 "f c #3263B4",
1465 "= c #EAF0FA",
1466 "< c #B1C7EB",
1467 "% c #6992D7",
1468 "9 c #D9E4F5",
1469 "o c #9BB7E5",
1470 "6 c #F7F9FD",
1471 ", c #BED0EE",
1472 "3 c #F0F5FC",
1473 "1 c #A8C0E8",
1474 " c None",
1475 "0 c #FDFEFF",
1476 "4 c #C4D5F0",
1477 "@ c #81A4DD",
1478 "e c #4377CD",
1479 "- c #E2EAF8",
1480 "i c #9FB9E5",
1481 "> c #CCDAF2",
1482 "+ c #89A9DF",
1483 "s c #5584D1",
1484 "t c #5D89D3",
1485 ": c #D2DFF4",
1486 "5 c #FAFCFE",
1487 "2 c #F5F8FD",
1488 "8 c #DFE8F7",
1489 "& c #5E8AD4",
1490 "X c #638ED5",
1491 "a c #CEDCF2",
1492 "p c #90AFE2",
1493 "d c #2F5DA9",
1494 "* c #5282D0",
1495 "7 c #E5EDF9",
1496 ". c #A2BCE6",
1497 "O c #8CACE0",
1498 /* pixels */
1499 " ",
1500 " .XXXXXXXXXXX ",
1501 " oXO++@#$%&*X ",
1502 " oX=-;:>,<1%X ",
1503 " oX23=-;:4,$X ",
1504 " oX5633789:@X ",
1505 " oX05623=78+X ",
1506 " oXqq05623=OX ",
1507 " oX,,,,,<<<$X ",
1508 " wXXXXXXXXXXe ",
1509 " XrtX%$$y@+O,, ",
1510 " uyiiiiiiiii@< ",
1511 " ouiiiiiiiiiip<a",
1512 " rustX%$$y@+Ow,,",
1513 " dfffffffffffffd",
1514 " "
1515 };
1516 #endif // !GTK+ 2
1517
1518 // ----------------------------------------------------------------------------
1519 // wxFileIconsTable & friends
1520 // ----------------------------------------------------------------------------
1521
1522 // global instance of a wxFileIconsTable
1523 wxFileIconsTable* wxTheFileIconsTable = NULL;
1524
1525 // A module to allow icons table cleanup
1526
1527 class wxFileIconsTableModule: public wxModule
1528 {
1529 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1530 public:
1531 wxFileIconsTableModule() {}
1532 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
1533 void OnExit()
1534 {
1535 wxDELETE(wxTheFileIconsTable);
1536 }
1537 };
1538
1539 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1540
1541 class wxFileIconEntry : public wxObject
1542 {
1543 public:
1544 wxFileIconEntry(int i) { iconid = i; }
1545
1546 int iconid;
1547 };
1548
1549 wxFileIconsTable::wxFileIconsTable()
1550 {
1551 m_HashTable = NULL;
1552 m_smallImageList = NULL;
1553 }
1554
1555 wxFileIconsTable::~wxFileIconsTable()
1556 {
1557 if (m_HashTable)
1558 {
1559 WX_CLEAR_HASH_TABLE(*m_HashTable);
1560 delete m_HashTable;
1561 }
1562 if (m_smallImageList) delete m_smallImageList;
1563 }
1564
1565 // delayed initialization - wait until first use (wxArtProv not created yet)
1566 void wxFileIconsTable::Create()
1567 {
1568 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
1569 m_HashTable = new wxHashTable(wxKEY_STRING);
1570 m_smallImageList = new wxImageList(16, 16);
1571
1572 // folder:
1573 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1574 wxART_CMN_DIALOG,
1575 wxSize(16, 16)));
1576 // folder_open
1577 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1578 wxART_CMN_DIALOG,
1579 wxSize(16, 16)));
1580 // computer
1581 #ifdef __WXGTK20__
1582 // GTK24 uses this icon in the file open dialog
1583 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1584 wxART_CMN_DIALOG,
1585 wxSize(16, 16)));
1586 #else
1587 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
1588 #endif
1589 // drive
1590 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1591 wxART_CMN_DIALOG,
1592 wxSize(16, 16)));
1593 // cdrom
1594 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1595 wxART_CMN_DIALOG,
1596 wxSize(16, 16)));
1597 // floppy
1598 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1599 wxART_CMN_DIALOG,
1600 wxSize(16, 16)));
1601 // removeable
1602 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1603 wxART_CMN_DIALOG,
1604 wxSize(16, 16)));
1605 // file
1606 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1607 wxART_CMN_DIALOG,
1608 wxSize(16, 16)));
1609 // executable
1610 if (GetIconID(wxEmptyString, wxT("application/x-executable")) == file)
1611 {
1612 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1613 wxART_CMN_DIALOG,
1614 wxSize(16, 16)));
1615 delete m_HashTable->Get(wxT("exe"));
1616 m_HashTable->Delete(wxT("exe"));
1617 m_HashTable->Put(wxT("exe"), new wxFileIconEntry(executable));
1618 }
1619 /* else put into list by GetIconID
1620 (KDE defines application/x-executable for *.exe and has nice icon)
1621 */
1622 }
1623
1624 wxImageList *wxFileIconsTable::GetSmallImageList()
1625 {
1626 if (!m_smallImageList)
1627 Create();
1628
1629 return m_smallImageList;
1630 }
1631
1632 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WINDOWS__) || wxUSE_WXDIB)
1633 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1634 // one icon and we won't resize it
1635
1636 static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1637 {
1638 const unsigned int size = 16;
1639
1640 wxImage smallimg (size, size);
1641 unsigned char *p1, *p2, *ps;
1642 unsigned char mr = img.GetMaskRed(),
1643 mg = img.GetMaskGreen(),
1644 mb = img.GetMaskBlue();
1645
1646 unsigned x, y;
1647 unsigned sr, sg, sb, smask;
1648
1649 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
1650 smallimg.SetMaskColour(mr, mr, mr);
1651
1652 for (y = 0; y < size; y++)
1653 {
1654 for (x = 0; x < size; x++)
1655 {
1656 sr = sg = sb = smask = 0;
1657 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1658 sr += p1[0], sg += p1[1], sb += p1[2];
1659 else smask++;
1660 p1 += 3;
1661 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1662 sr += p1[0], sg += p1[1], sb += p1[2];
1663 else smask++;
1664 p1 += 3;
1665 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1666 sr += p2[0], sg += p2[1], sb += p2[2];
1667 else smask++;
1668 p2 += 3;
1669 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1670 sr += p2[0], sg += p2[1], sb += p2[2];
1671 else smask++;
1672 p2 += 3;
1673
1674 if (smask > 2)
1675 ps[0] = ps[1] = ps[2] = mr;
1676 else
1677 {
1678 ps[0] = (unsigned char)(sr >> 2);
1679 ps[1] = (unsigned char)(sg >> 2);
1680 ps[2] = (unsigned char)(sb >> 2);
1681 }
1682 ps += 3;
1683 }
1684 p1 += size*2 * 3, p2 += size*2 * 3;
1685 }
1686
1687 return wxBitmap(smallimg);
1688 }
1689
1690 // This function is currently not unused anymore
1691 #if 0
1692 // finds empty borders and return non-empty area of image:
1693 static wxImage CutEmptyBorders(const wxImage& img)
1694 {
1695 unsigned char mr = img.GetMaskRed(),
1696 mg = img.GetMaskGreen(),
1697 mb = img.GetMaskBlue();
1698 unsigned char *dt = img.GetData(), *dttmp;
1699 unsigned w = img.GetWidth(), h = img.GetHeight();
1700
1701 unsigned top, bottom, left, right, i;
1702 bool empt;
1703
1704 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1705 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1706
1707 for (empt = true, top = 0; empt && top < h; top++)
1708 {
1709 MK_DTTMP(0, top);
1710 for (i = 0; i < w; i++, dttmp+=3)
1711 NOEMPTY_PIX(empt)
1712 }
1713 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
1714 {
1715 MK_DTTMP(0, bottom);
1716 for (i = 0; i < w; i++, dttmp+=3)
1717 NOEMPTY_PIX(empt)
1718 }
1719 for (empt = true, left = 0; empt && left < w; left++)
1720 {
1721 MK_DTTMP(left, 0);
1722 for (i = 0; i < h; i++, dttmp+=3*w)
1723 NOEMPTY_PIX(empt)
1724 }
1725 for (empt = true, right = w-1; empt && right > left; right--)
1726 {
1727 MK_DTTMP(right, 0);
1728 for (i = 0; i < h; i++, dttmp+=3*w)
1729 NOEMPTY_PIX(empt)
1730 }
1731 top--, left--, bottom++, right++;
1732
1733 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1734 }
1735 #endif // #if 0
1736
1737 #endif // wxUSE_MIMETYPE
1738
1739 int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1740 {
1741 if (!m_smallImageList)
1742 Create();
1743
1744 #if wxUSE_MIMETYPE
1745 if (!extension.empty())
1746 {
1747 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1748 if (entry) return (entry -> iconid);
1749 }
1750
1751 wxFileType *ft = (mime.empty()) ?
1752 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1753 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
1754
1755 wxIconLocation iconLoc;
1756 wxIcon ic;
1757
1758 {
1759 wxLogNull logNull;
1760 if ( ft && ft->GetIcon(&iconLoc) )
1761 {
1762 ic = wxIcon( iconLoc );
1763 }
1764 }
1765
1766 delete ft;
1767
1768 if ( !ic.IsOk() )
1769 {
1770 int newid = file;
1771 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1772 return newid;
1773 }
1774
1775 wxBitmap bmp;
1776 bmp.CopyFromIcon(ic);
1777
1778 if ( !bmp.IsOk() )
1779 {
1780 int newid = file;
1781 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1782 return newid;
1783 }
1784
1785 const unsigned int size = 16;
1786
1787 int treeid = m_smallImageList->GetImageCount();
1788 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1789 {
1790 m_smallImageList->Add(bmp);
1791 }
1792 #if wxUSE_IMAGE && (!defined(__WINDOWS__) || wxUSE_WXDIB)
1793 else
1794 {
1795 wxImage img = bmp.ConvertToImage();
1796
1797 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1798 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1799 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
1800 else
1801 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1802 }
1803 #endif // wxUSE_IMAGE
1804
1805 m_HashTable->Put(extension, new wxFileIconEntry(treeid));
1806 return treeid;
1807
1808 #else // !wxUSE_MIMETYPE
1809
1810 wxUnusedVar(mime);
1811 if (extension == wxT("exe"))
1812 return executable;
1813 else
1814 return file;
1815 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1816 }
1817
1818 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG