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