]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirctrlg.cpp
rebaked after adding new string-related headers
[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 if ( m_showHidden == show )
639 return;
640
641 m_showHidden = show;
642
643 wxString path = GetPath();
644 ReCreateTree();
645 SetPath(path);
646 }
647
648 const wxTreeItemId
649 wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
650 {
651 wxDirItemData *dir_item = new wxDirItemData(path,name,true);
652
653 wxTreeItemId id = AppendItem( m_rootId, name, imageId, -1, dir_item);
654
655 m_treeCtrl->SetItemHasChildren(id);
656
657 return id;
658 }
659
660 void wxGenericDirCtrl::SetupSections()
661 {
662 wxArrayString paths, names;
663 wxArrayInt icons;
664
665 size_t n, count = wxGetAvailableDrives(paths, names, icons);
666
667 #ifdef __WXGTK20__
668 wxString home = wxGetHomeDir();
669 AddSection( home, _("Home directory"), 1);
670 home += wxT("/Desktop");
671 AddSection( home, _("Desktop"), 1);
672 #endif
673
674 for (n = 0; n < count; n++)
675 AddSection(paths[n], names[n], icons[n]);
676 }
677
678 void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
679 {
680 // don't rename the main entry "Sections"
681 if (event.GetItem() == m_rootId)
682 {
683 event.Veto();
684 return;
685 }
686
687 // don't rename the individual sections
688 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
689 {
690 event.Veto();
691 return;
692 }
693 }
694
695 void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
696 {
697 if (event.IsEditCancelled())
698 return;
699
700 if ((event.GetLabel().empty()) ||
701 (event.GetLabel() == wxT(".")) ||
702 (event.GetLabel() == wxT("..")) ||
703 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
704 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
705 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
706 {
707 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
708 dialog.ShowModal();
709 event.Veto();
710 return;
711 }
712
713 wxTreeItemId id = event.GetItem();
714 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
715 wxASSERT( data );
716
717 wxString new_name( wxPathOnly( data->m_path ) );
718 new_name += wxString(wxFILE_SEP_PATH);
719 new_name += event.GetLabel();
720
721 wxLogNull log;
722
723 if (wxFileExists(new_name))
724 {
725 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
726 dialog.ShowModal();
727 event.Veto();
728 }
729
730 if (wxRenameFile(data->m_path,new_name))
731 {
732 data->SetNewDirName( new_name );
733 }
734 else
735 {
736 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
737 dialog.ShowModal();
738 event.Veto();
739 }
740 }
741
742 void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
743 {
744 wxTreeItemId parentId = event.GetItem();
745
746 // VS: this is needed because the event handler is called from wxTreeCtrl
747 // ctor when wxTR_HIDE_ROOT was specified
748
749 if (!m_rootId.IsOk())
750
751 m_rootId = m_treeCtrl->GetRootItem();
752
753 ExpandDir(parentId);
754 }
755
756 void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
757 {
758 CollapseDir(event.GetItem());
759 }
760
761 void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
762 {
763 wxTreeItemId child;
764
765 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
766 if (!data->m_isExpanded)
767 return;
768
769 data->m_isExpanded = false;
770 wxTreeItemIdValue cookie;
771 /* Workaround because DeleteChildren has disapeared (why?) and
772 * CollapseAndReset doesn't work as advertised (deletes parent too) */
773 child = m_treeCtrl->GetFirstChild(parentId, cookie);
774 while (child.IsOk())
775 {
776 m_treeCtrl->Delete(child);
777 /* Not GetNextChild below, because the cookie mechanism can't
778 * handle disappearing children! */
779 child = m_treeCtrl->GetFirstChild(parentId, cookie);
780 }
781 if (parentId != m_treeCtrl->GetRootItem())
782 m_treeCtrl->Collapse(parentId);
783 }
784
785 void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
786 {
787 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
788
789 if (data->m_isExpanded)
790 return;
791
792 data->m_isExpanded = true;
793
794 if (parentId == m_treeCtrl->GetRootItem())
795 {
796 SetupSections();
797 return;
798 }
799
800 wxASSERT(data);
801
802 wxString search,path,filename;
803
804 wxString dirName(data->m_path);
805
806 #if (defined(__WINDOWS__) && !defined(__WXWINCE__)) || defined(__DOS__) || defined(__OS2__)
807 // Check if this is a root directory and if so,
808 // whether the drive is avaiable.
809 if (!wxIsDriveAvailable(dirName))
810 {
811 data->m_isExpanded = false;
812 //wxMessageBox(wxT("Sorry, this drive is not available."));
813 return;
814 }
815 #endif
816
817 // This may take a longish time. Go to busy cursor
818 wxBusyCursor busy;
819
820 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
821 if (dirName.Last() == ':')
822 dirName += wxString(wxFILE_SEP_PATH);
823 #endif
824
825 wxArrayString dirs;
826 wxArrayString filenames;
827
828 wxDir d;
829 wxString eachFilename;
830
831 wxLogNull log;
832 d.Open(dirName);
833
834 if (d.IsOpened())
835 {
836 int style = wxDIR_DIRS;
837 if (m_showHidden) style |= wxDIR_HIDDEN;
838 if (d.GetFirst(& eachFilename, wxEmptyString, style))
839 {
840 do
841 {
842 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
843 {
844 dirs.Add(eachFilename);
845 }
846 }
847 while (d.GetNext(&eachFilename));
848 }
849 }
850 dirs.Sort(wxDirCtrlStringCompareFunction);
851
852 // Now do the filenames -- but only if we're allowed to
853 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
854 {
855 d.Open(dirName);
856
857 if (d.IsOpened())
858 {
859 int style = wxDIR_FILES;
860 if (m_showHidden) style |= wxDIR_HIDDEN;
861 // Process each filter (ex: "JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg")
862 wxStringTokenizer strTok;
863 wxString curFilter;
864 strTok.SetString(m_currentFilterStr,wxT(";"));
865 while(strTok.HasMoreTokens())
866 {
867 curFilter = strTok.GetNextToken();
868 if (d.GetFirst(& eachFilename, curFilter, style))
869 {
870 do
871 {
872 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
873 {
874 filenames.Add(eachFilename);
875 }
876 }
877 while (d.GetNext(& eachFilename));
878 }
879 }
880 }
881 filenames.Sort(wxDirCtrlStringCompareFunction);
882 }
883
884 // Add the sorted dirs
885 size_t i;
886 for (i = 0; i < dirs.Count(); i++)
887 {
888 eachFilename = dirs[i];
889 path = dirName;
890 if (!wxEndsWithPathSeparator(path))
891 path += wxString(wxFILE_SEP_PATH);
892 path += eachFilename;
893
894 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,true);
895 wxTreeItemId id = AppendItem( parentId, eachFilename,
896 wxFileIconsTable::folder, -1, dir_item);
897 m_treeCtrl->SetItemImage( id, wxFileIconsTable::folder_open,
898 wxTreeItemIcon_Expanded );
899
900 // Has this got any children? If so, make it expandable.
901 // (There are two situations when a dir has children: either it
902 // has subdirectories or it contains files that weren't filtered
903 // out. The latter only applies to dirctrl with files.)
904 if ( dir_item->HasSubDirs() ||
905 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
906 dir_item->HasFiles(m_currentFilterStr)) )
907 {
908 m_treeCtrl->SetItemHasChildren(id);
909 }
910 }
911
912 // Add the sorted filenames
913 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
914 {
915 for (i = 0; i < filenames.Count(); i++)
916 {
917 eachFilename = filenames[i];
918 path = dirName;
919 if (!wxEndsWithPathSeparator(path))
920 path += wxString(wxFILE_SEP_PATH);
921 path += eachFilename;
922 //path = dirName + wxString(wxT("/")) + eachFilename;
923 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,false);
924 int image_id = wxFileIconsTable::file;
925 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
926 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
927 (void) AppendItem( parentId, eachFilename, image_id, -1, dir_item);
928 }
929 }
930 }
931
932 void wxGenericDirCtrl::ReCreateTree()
933 {
934 CollapseDir(m_treeCtrl->GetRootItem());
935 ExpandRoot();
936 }
937
938 void wxGenericDirCtrl::CollapseTree()
939 {
940 wxTreeItemIdValue cookie;
941 wxTreeItemId child = m_treeCtrl->GetFirstChild(m_rootId, cookie);
942 while (child.IsOk())
943 {
944 CollapseDir(child);
945 child = m_treeCtrl->GetNextChild(m_rootId, cookie);
946 }
947 }
948
949 // Find the child that matches the first part of 'path'.
950 // E.g. if a child path is "/usr" and 'path' is "/usr/include"
951 // then the child for /usr is returned.
952 wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
953 {
954 wxString path2(path);
955
956 // Make sure all separators are as per the current platform
957 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
958 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
959
960 // Append a separator to foil bogus substring matching
961 path2 += wxString(wxFILE_SEP_PATH);
962
963 // In MSW or PM, case is not significant
964 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
965 path2.MakeLower();
966 #endif
967
968 wxTreeItemIdValue cookie;
969 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
970 while (childId.IsOk())
971 {
972 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
973
974 if (data && !data->m_path.empty())
975 {
976 wxString childPath(data->m_path);
977 if (!wxEndsWithPathSeparator(childPath))
978 childPath += wxString(wxFILE_SEP_PATH);
979
980 // In MSW and PM, case is not significant
981 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
982 childPath.MakeLower();
983 #endif
984
985 if (childPath.length() <= path2.length())
986 {
987 wxString path3 = path2.Mid(0, childPath.length());
988 if (childPath == path3)
989 {
990 if (path3.length() == path2.length())
991 done = true;
992 else
993 done = false;
994 return childId;
995 }
996 }
997 }
998
999 childId = m_treeCtrl->GetNextChild(parentId, cookie);
1000 }
1001 wxTreeItemId invalid;
1002 return invalid;
1003 }
1004
1005 // Try to expand as much of the given path as possible,
1006 // and select the given tree item.
1007 bool wxGenericDirCtrl::ExpandPath(const wxString& path)
1008 {
1009 bool done = false;
1010 wxTreeItemId id = FindChild(m_rootId, path, done);
1011 wxTreeItemId lastId = id; // The last non-zero id
1012 while (id.IsOk() && !done)
1013 {
1014 ExpandDir(id);
1015
1016 id = FindChild(id, path, done);
1017 if (id.IsOk())
1018 lastId = id;
1019 }
1020 if (!lastId.IsOk())
1021 return false;
1022
1023 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
1024 if (data->m_isDir)
1025 {
1026 m_treeCtrl->Expand(lastId);
1027 }
1028 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
1029 {
1030 // Find the first file in this directory
1031 wxTreeItemIdValue cookie;
1032 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
1033 bool selectedChild = false;
1034 while (childId.IsOk())
1035 {
1036 data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
1037
1038 if (data && data->m_path != wxEmptyString && !data->m_isDir)
1039 {
1040 m_treeCtrl->SelectItem(childId);
1041 m_treeCtrl->EnsureVisible(childId);
1042 selectedChild = true;
1043 break;
1044 }
1045 childId = m_treeCtrl->GetNextChild(lastId, cookie);
1046 }
1047 if (!selectedChild)
1048 {
1049 m_treeCtrl->SelectItem(lastId);
1050 m_treeCtrl->EnsureVisible(lastId);
1051 }
1052 }
1053 else
1054 {
1055 m_treeCtrl->SelectItem(lastId);
1056 m_treeCtrl->EnsureVisible(lastId);
1057 }
1058
1059 return true;
1060 }
1061
1062
1063 bool wxGenericDirCtrl::CollapsePath(const wxString& path)
1064 {
1065 bool done = false;
1066 wxTreeItemId id = FindChild(m_rootId, path, done);
1067 wxTreeItemId lastId = id; // The last non-zero id
1068
1069 while ( id.IsOk() && !done )
1070 {
1071 CollapseDir(id);
1072
1073 id = FindChild(id, path, done);
1074
1075 if ( id.IsOk() )
1076 lastId = id;
1077 }
1078
1079 if ( !lastId.IsOk() )
1080 return false;
1081
1082 m_treeCtrl->SelectItem(lastId);
1083 m_treeCtrl->EnsureVisible(lastId);
1084
1085 return true;
1086 }
1087
1088
1089 wxString wxGenericDirCtrl::GetPath() const
1090 {
1091 wxTreeItemId id = m_treeCtrl->GetSelection();
1092 if (id)
1093 {
1094 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
1095 return data->m_path;
1096 }
1097 else
1098 return wxEmptyString;
1099 }
1100
1101 wxString wxGenericDirCtrl::GetFilePath() const
1102 {
1103 wxTreeItemId id = m_treeCtrl->GetSelection();
1104 if (id)
1105 {
1106 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
1107 if (data->m_isDir)
1108 return wxEmptyString;
1109 else
1110 return data->m_path;
1111 }
1112 else
1113 return wxEmptyString;
1114 }
1115
1116 void wxGenericDirCtrl::SetPath(const wxString& path)
1117 {
1118 m_defaultPath = path;
1119 if (m_rootId)
1120 ExpandPath(path);
1121 }
1122
1123 // Not used
1124 #if 0
1125 void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
1126 {
1127 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
1128
1129 // This may take a longish time. Go to busy cursor
1130 wxBusyCursor busy;
1131
1132 wxASSERT(data);
1133
1134 wxString search,path,filename;
1135
1136 wxString dirName(data->m_path);
1137
1138 #if defined(__WXMSW__) || defined(__OS2__)
1139 if (dirName.Last() == ':')
1140 dirName += wxString(wxFILE_SEP_PATH);
1141 #endif
1142
1143 wxDir d;
1144 wxString eachFilename;
1145
1146 wxLogNull log;
1147 d.Open(dirName);
1148
1149 if (d.IsOpened())
1150 {
1151 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
1152 {
1153 do
1154 {
1155 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
1156 {
1157 filenames.Add(eachFilename);
1158 }
1159 }
1160 while (d.GetNext(& eachFilename)) ;
1161 }
1162 }
1163 }
1164 #endif
1165
1166 void wxGenericDirCtrl::SetFilterIndex(int n)
1167 {
1168 m_currentFilter = n;
1169
1170 wxString f, d;
1171 if (ExtractWildcard(m_filter, n, f, d))
1172 m_currentFilterStr = f;
1173 else
1174 #ifdef __UNIX__
1175 m_currentFilterStr = wxT("*");
1176 #else
1177 m_currentFilterStr = wxT("*.*");
1178 #endif
1179 }
1180
1181 void wxGenericDirCtrl::SetFilter(const wxString& filter)
1182 {
1183 m_filter = filter;
1184
1185 wxString f, d;
1186 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1187 m_currentFilterStr = f;
1188 else
1189 #ifdef __UNIX__
1190 m_currentFilterStr = wxT("*");
1191 #else
1192 m_currentFilterStr = wxT("*.*");
1193 #endif
1194 }
1195
1196 // Extract description and actual filter from overall filter string
1197 bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1198 {
1199 wxArrayString filters, descriptions;
1200 int count = wxParseCommonDialogsFilter(filterStr, descriptions, filters);
1201 if (count > 0 && n < count)
1202 {
1203 filter = filters[n];
1204 description = descriptions[n];
1205 return true;
1206 }
1207
1208 return false;
1209 }
1210
1211
1212 void wxGenericDirCtrl::DoResize()
1213 {
1214 wxSize sz = GetClientSize();
1215 int verticalSpacing = 3;
1216 if (m_treeCtrl)
1217 {
1218 wxSize filterSz ;
1219 if (m_filterListCtrl)
1220 {
1221 filterSz = m_filterListCtrl->GetSize();
1222 sz.y -= (filterSz.y + verticalSpacing);
1223 }
1224 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1225 if (m_filterListCtrl)
1226 {
1227 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1228 // Don't know why, but this needs refreshing after a resize (wxMSW)
1229 m_filterListCtrl->Refresh();
1230 }
1231 }
1232 }
1233
1234
1235 void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
1236 {
1237 DoResize();
1238 }
1239
1240 wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
1241 const wxString & text,
1242 int image, int selectedImage,
1243 wxTreeItemData * data)
1244 {
1245 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1246
1247 wxASSERT (treeCtrl);
1248
1249 if (treeCtrl)
1250 {
1251 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1252 }
1253 else
1254 {
1255 return wxTreeItemId();
1256 }
1257 }
1258
1259
1260 //-----------------------------------------------------------------------------
1261 // wxDirFilterListCtrl
1262 //-----------------------------------------------------------------------------
1263
1264 IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1265
1266 BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1267 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
1268 END_EVENT_TABLE()
1269
1270 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1271 const wxPoint& pos,
1272 const wxSize& size,
1273 long style)
1274 {
1275 m_dirCtrl = parent;
1276 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1277 }
1278
1279 void wxDirFilterListCtrl::Init()
1280 {
1281 m_dirCtrl = NULL;
1282 }
1283
1284 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
1285 {
1286 int sel = GetSelection();
1287
1288 wxString currentPath = m_dirCtrl->GetPath();
1289
1290 m_dirCtrl->SetFilterIndex(sel);
1291
1292 // If the filter has changed, the view is out of date, so
1293 // collapse the tree.
1294 m_dirCtrl->ReCreateTree();
1295
1296 // Try to restore the selection, or at least the directory
1297 m_dirCtrl->ExpandPath(currentPath);
1298 }
1299
1300 void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1301 {
1302 Clear();
1303 wxArrayString descriptions, filters;
1304 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
1305
1306 if (n > 0 && defaultFilter < (int) n)
1307 {
1308 for (size_t i = 0; i < n; i++)
1309 Append(descriptions[i]);
1310 SetSelection(defaultFilter);
1311 }
1312 }
1313 #endif // wxUSE_DIRDLG
1314
1315 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1316
1317 // ----------------------------------------------------------------------------
1318 // wxFileIconsTable icons
1319 // ----------------------------------------------------------------------------
1320
1321 #ifndef __WXGTK24__
1322 /* Computer (c) Julian Smart */
1323 static const char * file_icons_tbl_computer_xpm[] = {
1324 /* columns rows colors chars-per-pixel */
1325 "16 16 42 1",
1326 "r c #4E7FD0",
1327 "$ c #7198D9",
1328 "; c #DCE6F6",
1329 "q c #FFFFFF",
1330 "u c #4A7CCE",
1331 "# c #779DDB",
1332 "w c #95B2E3",
1333 "y c #7FA2DD",
1334 "f c #3263B4",
1335 "= c #EAF0FA",
1336 "< c #B1C7EB",
1337 "% c #6992D7",
1338 "9 c #D9E4F5",
1339 "o c #9BB7E5",
1340 "6 c #F7F9FD",
1341 ", c #BED0EE",
1342 "3 c #F0F5FC",
1343 "1 c #A8C0E8",
1344 " c None",
1345 "0 c #FDFEFF",
1346 "4 c #C4D5F0",
1347 "@ c #81A4DD",
1348 "e c #4377CD",
1349 "- c #E2EAF8",
1350 "i c #9FB9E5",
1351 "> c #CCDAF2",
1352 "+ c #89A9DF",
1353 "s c #5584D1",
1354 "t c #5D89D3",
1355 ": c #D2DFF4",
1356 "5 c #FAFCFE",
1357 "2 c #F5F8FD",
1358 "8 c #DFE8F7",
1359 "& c #5E8AD4",
1360 "X c #638ED5",
1361 "a c #CEDCF2",
1362 "p c #90AFE2",
1363 "d c #2F5DA9",
1364 "* c #5282D0",
1365 "7 c #E5EDF9",
1366 ". c #A2BCE6",
1367 "O c #8CACE0",
1368 /* pixels */
1369 " ",
1370 " .XXXXXXXXXXX ",
1371 " oXO++@#$%&*X ",
1372 " oX=-;:>,<1%X ",
1373 " oX23=-;:4,$X ",
1374 " oX5633789:@X ",
1375 " oX05623=78+X ",
1376 " oXqq05623=OX ",
1377 " oX,,,,,<<<$X ",
1378 " wXXXXXXXXXXe ",
1379 " XrtX%$$y@+O,, ",
1380 " uyiiiiiiiii@< ",
1381 " ouiiiiiiiiiip<a",
1382 " rustX%$$y@+Ow,,",
1383 " dfffffffffffffd",
1384 " "
1385 };
1386 #endif // GTK+ < 2.4
1387
1388 // ----------------------------------------------------------------------------
1389 // wxFileIconsTable & friends
1390 // ----------------------------------------------------------------------------
1391
1392 // global instance of a wxFileIconsTable
1393 wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1394
1395 // A module to allow icons table cleanup
1396
1397 class wxFileIconsTableModule: public wxModule
1398 {
1399 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1400 public:
1401 wxFileIconsTableModule() {}
1402 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
1403 void OnExit()
1404 {
1405 if (wxTheFileIconsTable)
1406 {
1407 delete wxTheFileIconsTable;
1408 wxTheFileIconsTable = NULL;
1409 }
1410 }
1411 };
1412
1413 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1414
1415 class wxFileIconEntry : public wxObject
1416 {
1417 public:
1418 wxFileIconEntry(int i) { id = i; }
1419
1420 int id;
1421 };
1422
1423 wxFileIconsTable::wxFileIconsTable()
1424 {
1425 m_HashTable = NULL;
1426 m_smallImageList = NULL;
1427 }
1428
1429 wxFileIconsTable::~wxFileIconsTable()
1430 {
1431 if (m_HashTable)
1432 {
1433 WX_CLEAR_HASH_TABLE(*m_HashTable);
1434 delete m_HashTable;
1435 }
1436 if (m_smallImageList) delete m_smallImageList;
1437 }
1438
1439 // delayed initialization - wait until first use (wxArtProv not created yet)
1440 void wxFileIconsTable::Create()
1441 {
1442 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
1443 m_HashTable = new wxHashTable(wxKEY_STRING);
1444 m_smallImageList = new wxImageList(16, 16);
1445
1446 // folder:
1447 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1448 wxART_CMN_DIALOG,
1449 wxSize(16, 16)));
1450 // folder_open
1451 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1452 wxART_CMN_DIALOG,
1453 wxSize(16, 16)));
1454 // computer
1455 #ifdef __WXGTK24__
1456 // GTK24 uses this icon in the file open dialog
1457 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1458 wxART_CMN_DIALOG,
1459 wxSize(16, 16)));
1460 #else
1461 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
1462 #endif
1463 // drive
1464 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1465 wxART_CMN_DIALOG,
1466 wxSize(16, 16)));
1467 // cdrom
1468 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1469 wxART_CMN_DIALOG,
1470 wxSize(16, 16)));
1471 // floppy
1472 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1473 wxART_CMN_DIALOG,
1474 wxSize(16, 16)));
1475 // removeable
1476 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1477 wxART_CMN_DIALOG,
1478 wxSize(16, 16)));
1479 // file
1480 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1481 wxART_CMN_DIALOG,
1482 wxSize(16, 16)));
1483 // executable
1484 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1485 {
1486 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1487 wxART_CMN_DIALOG,
1488 wxSize(16, 16)));
1489 delete m_HashTable->Get(_T("exe"));
1490 m_HashTable->Delete(_T("exe"));
1491 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1492 }
1493 /* else put into list by GetIconID
1494 (KDE defines application/x-executable for *.exe and has nice icon)
1495 */
1496 }
1497
1498 wxImageList *wxFileIconsTable::GetSmallImageList()
1499 {
1500 if (!m_smallImageList)
1501 Create();
1502
1503 return m_smallImageList;
1504 }
1505
1506 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1507 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1508 // one icon and we won't resize it
1509
1510 static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1511 {
1512 const unsigned int size = 16;
1513
1514 wxImage smallimg (size, size);
1515 unsigned char *p1, *p2, *ps;
1516 unsigned char mr = img.GetMaskRed(),
1517 mg = img.GetMaskGreen(),
1518 mb = img.GetMaskBlue();
1519
1520 unsigned x, y;
1521 unsigned sr, sg, sb, smask;
1522
1523 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
1524 smallimg.SetMaskColour(mr, mr, mr);
1525
1526 for (y = 0; y < size; y++)
1527 {
1528 for (x = 0; x < size; x++)
1529 {
1530 sr = sg = sb = smask = 0;
1531 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1532 sr += p1[0], sg += p1[1], sb += p1[2];
1533 else smask++;
1534 p1 += 3;
1535 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1536 sr += p1[0], sg += p1[1], sb += p1[2];
1537 else smask++;
1538 p1 += 3;
1539 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1540 sr += p2[0], sg += p2[1], sb += p2[2];
1541 else smask++;
1542 p2 += 3;
1543 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1544 sr += p2[0], sg += p2[1], sb += p2[2];
1545 else smask++;
1546 p2 += 3;
1547
1548 if (smask > 2)
1549 ps[0] = ps[1] = ps[2] = mr;
1550 else
1551 {
1552 ps[0] = (unsigned char)(sr >> 2);
1553 ps[1] = (unsigned char)(sg >> 2);
1554 ps[2] = (unsigned char)(sb >> 2);
1555 }
1556 ps += 3;
1557 }
1558 p1 += size*2 * 3, p2 += size*2 * 3;
1559 }
1560
1561 return wxBitmap(smallimg);
1562 }
1563
1564 // This function is currently not unused anymore
1565 #if 0
1566 // finds empty borders and return non-empty area of image:
1567 static wxImage CutEmptyBorders(const wxImage& img)
1568 {
1569 unsigned char mr = img.GetMaskRed(),
1570 mg = img.GetMaskGreen(),
1571 mb = img.GetMaskBlue();
1572 unsigned char *dt = img.GetData(), *dttmp;
1573 unsigned w = img.GetWidth(), h = img.GetHeight();
1574
1575 unsigned top, bottom, left, right, i;
1576 bool empt;
1577
1578 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1579 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1580
1581 for (empt = true, top = 0; empt && top < h; top++)
1582 {
1583 MK_DTTMP(0, top);
1584 for (i = 0; i < w; i++, dttmp+=3)
1585 NOEMPTY_PIX(empt)
1586 }
1587 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
1588 {
1589 MK_DTTMP(0, bottom);
1590 for (i = 0; i < w; i++, dttmp+=3)
1591 NOEMPTY_PIX(empt)
1592 }
1593 for (empt = true, left = 0; empt && left < w; left++)
1594 {
1595 MK_DTTMP(left, 0);
1596 for (i = 0; i < h; i++, dttmp+=3*w)
1597 NOEMPTY_PIX(empt)
1598 }
1599 for (empt = true, right = w-1; empt && right > left; right--)
1600 {
1601 MK_DTTMP(right, 0);
1602 for (i = 0; i < h; i++, dttmp+=3*w)
1603 NOEMPTY_PIX(empt)
1604 }
1605 top--, left--, bottom++, right++;
1606
1607 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1608 }
1609 #endif // #if 0
1610
1611 #endif // wxUSE_MIMETYPE
1612
1613 int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1614 {
1615 if (!m_smallImageList)
1616 Create();
1617
1618 #if wxUSE_MIMETYPE
1619 if (!extension.empty())
1620 {
1621 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1622 if (entry) return (entry -> id);
1623 }
1624
1625 wxFileType *ft = (mime.empty()) ?
1626 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1627 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
1628
1629 wxIconLocation iconLoc;
1630 wxIcon ic;
1631
1632 {
1633 wxLogNull logNull;
1634 if ( ft && ft->GetIcon(&iconLoc) )
1635 {
1636 ic = wxIcon( iconLoc );
1637 }
1638 }
1639
1640 delete ft;
1641
1642 if ( !ic.Ok() )
1643 {
1644 int newid = file;
1645 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1646 return newid;
1647 }
1648
1649 wxBitmap bmp;
1650 bmp.CopyFromIcon(ic);
1651
1652 if ( !bmp.Ok() )
1653 {
1654 int newid = file;
1655 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1656 return newid;
1657 }
1658
1659 const unsigned int size = 16;
1660
1661 int id = m_smallImageList->GetImageCount();
1662 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1663 {
1664 m_smallImageList->Add(bmp);
1665 }
1666 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1667 else
1668 {
1669 wxImage img = bmp.ConvertToImage();
1670
1671 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1672 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1673 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
1674 else
1675 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1676 }
1677 #endif // wxUSE_IMAGE
1678
1679 m_HashTable->Put(extension, new wxFileIconEntry(id));
1680 return id;
1681
1682 #else // !wxUSE_MIMETYPE
1683
1684 wxUnusedVar(mime);
1685 if (extension == wxT("exe"))
1686 return executable;
1687 else
1688 return file;
1689 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1690 }
1691
1692 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG