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