fix compilation problem when wxUSE_FSVOLUME==0 after last commit: declare wxIsDriveAv...
[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 #if WXWIN_COMPATIBILITY_2_4
1209 // Parses the global filter, returning the number of filters.
1210 // Returns 0 if none or if there's a problem.
1211 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1212 int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1213 {
1214 return wxParseCommonDialogsFilter(filterStr, descriptions, filters );
1215 }
1216 #endif // WXWIN_COMPATIBILITY_2_4
1217
1218 void wxGenericDirCtrl::DoResize()
1219 {
1220 wxSize sz = GetClientSize();
1221 int verticalSpacing = 3;
1222 if (m_treeCtrl)
1223 {
1224 wxSize filterSz ;
1225 if (m_filterListCtrl)
1226 {
1227 filterSz = m_filterListCtrl->GetSize();
1228 sz.y -= (filterSz.y + verticalSpacing);
1229 }
1230 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1231 if (m_filterListCtrl)
1232 {
1233 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1234 // Don't know why, but this needs refreshing after a resize (wxMSW)
1235 m_filterListCtrl->Refresh();
1236 }
1237 }
1238 }
1239
1240
1241 void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
1242 {
1243 DoResize();
1244 }
1245
1246 wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
1247 const wxString & text,
1248 int image, int selectedImage,
1249 wxTreeItemData * data)
1250 {
1251 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1252
1253 wxASSERT (treeCtrl);
1254
1255 if (treeCtrl)
1256 {
1257 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1258 }
1259 else
1260 {
1261 return wxTreeItemId();
1262 }
1263 }
1264
1265
1266 //-----------------------------------------------------------------------------
1267 // wxDirFilterListCtrl
1268 //-----------------------------------------------------------------------------
1269
1270 IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1271
1272 BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1273 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
1274 END_EVENT_TABLE()
1275
1276 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1277 const wxPoint& pos,
1278 const wxSize& size,
1279 long style)
1280 {
1281 m_dirCtrl = parent;
1282 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1283 }
1284
1285 void wxDirFilterListCtrl::Init()
1286 {
1287 m_dirCtrl = NULL;
1288 }
1289
1290 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
1291 {
1292 int sel = GetSelection();
1293
1294 wxString currentPath = m_dirCtrl->GetPath();
1295
1296 m_dirCtrl->SetFilterIndex(sel);
1297
1298 // If the filter has changed, the view is out of date, so
1299 // collapse the tree.
1300 m_dirCtrl->ReCreateTree();
1301
1302 // Try to restore the selection, or at least the directory
1303 m_dirCtrl->ExpandPath(currentPath);
1304 }
1305
1306 void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1307 {
1308 Clear();
1309 wxArrayString descriptions, filters;
1310 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
1311
1312 if (n > 0 && defaultFilter < (int) n)
1313 {
1314 for (size_t i = 0; i < n; i++)
1315 Append(descriptions[i]);
1316 SetSelection(defaultFilter);
1317 }
1318 }
1319 #endif // wxUSE_DIRDLG
1320
1321 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1322
1323 // ----------------------------------------------------------------------------
1324 // wxFileIconsTable icons
1325 // ----------------------------------------------------------------------------
1326
1327 #ifndef __WXGTK24__
1328 /* Computer (c) Julian Smart */
1329 static const char * file_icons_tbl_computer_xpm[] = {
1330 /* columns rows colors chars-per-pixel */
1331 "16 16 42 1",
1332 "r c #4E7FD0",
1333 "$ c #7198D9",
1334 "; c #DCE6F6",
1335 "q c #FFFFFF",
1336 "u c #4A7CCE",
1337 "# c #779DDB",
1338 "w c #95B2E3",
1339 "y c #7FA2DD",
1340 "f c #3263B4",
1341 "= c #EAF0FA",
1342 "< c #B1C7EB",
1343 "% c #6992D7",
1344 "9 c #D9E4F5",
1345 "o c #9BB7E5",
1346 "6 c #F7F9FD",
1347 ", c #BED0EE",
1348 "3 c #F0F5FC",
1349 "1 c #A8C0E8",
1350 " c None",
1351 "0 c #FDFEFF",
1352 "4 c #C4D5F0",
1353 "@ c #81A4DD",
1354 "e c #4377CD",
1355 "- c #E2EAF8",
1356 "i c #9FB9E5",
1357 "> c #CCDAF2",
1358 "+ c #89A9DF",
1359 "s c #5584D1",
1360 "t c #5D89D3",
1361 ": c #D2DFF4",
1362 "5 c #FAFCFE",
1363 "2 c #F5F8FD",
1364 "8 c #DFE8F7",
1365 "& c #5E8AD4",
1366 "X c #638ED5",
1367 "a c #CEDCF2",
1368 "p c #90AFE2",
1369 "d c #2F5DA9",
1370 "* c #5282D0",
1371 "7 c #E5EDF9",
1372 ". c #A2BCE6",
1373 "O c #8CACE0",
1374 /* pixels */
1375 " ",
1376 " .XXXXXXXXXXX ",
1377 " oXO++@#$%&*X ",
1378 " oX=-;:>,<1%X ",
1379 " oX23=-;:4,$X ",
1380 " oX5633789:@X ",
1381 " oX05623=78+X ",
1382 " oXqq05623=OX ",
1383 " oX,,,,,<<<$X ",
1384 " wXXXXXXXXXXe ",
1385 " XrtX%$$y@+O,, ",
1386 " uyiiiiiiiii@< ",
1387 " ouiiiiiiiiiip<a",
1388 " rustX%$$y@+Ow,,",
1389 " dfffffffffffffd",
1390 " "
1391 };
1392 #endif // GTK+ < 2.4
1393
1394 // ----------------------------------------------------------------------------
1395 // wxFileIconsTable & friends
1396 // ----------------------------------------------------------------------------
1397
1398 // global instance of a wxFileIconsTable
1399 wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1400
1401 // A module to allow icons table cleanup
1402
1403 class wxFileIconsTableModule: public wxModule
1404 {
1405 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1406 public:
1407 wxFileIconsTableModule() {}
1408 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
1409 void OnExit()
1410 {
1411 if (wxTheFileIconsTable)
1412 {
1413 delete wxTheFileIconsTable;
1414 wxTheFileIconsTable = NULL;
1415 }
1416 }
1417 };
1418
1419 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1420
1421 class wxFileIconEntry : public wxObject
1422 {
1423 public:
1424 wxFileIconEntry(int i) { id = i; }
1425
1426 int id;
1427 };
1428
1429 wxFileIconsTable::wxFileIconsTable()
1430 {
1431 m_HashTable = NULL;
1432 m_smallImageList = NULL;
1433 }
1434
1435 wxFileIconsTable::~wxFileIconsTable()
1436 {
1437 if (m_HashTable)
1438 {
1439 WX_CLEAR_HASH_TABLE(*m_HashTable);
1440 delete m_HashTable;
1441 }
1442 if (m_smallImageList) delete m_smallImageList;
1443 }
1444
1445 // delayed initialization - wait until first use (wxArtProv not created yet)
1446 void wxFileIconsTable::Create()
1447 {
1448 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
1449 m_HashTable = new wxHashTable(wxKEY_STRING);
1450 m_smallImageList = new wxImageList(16, 16);
1451
1452 // folder:
1453 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1454 wxART_CMN_DIALOG,
1455 wxSize(16, 16)));
1456 // folder_open
1457 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1458 wxART_CMN_DIALOG,
1459 wxSize(16, 16)));
1460 // computer
1461 #ifdef __WXGTK24__
1462 // GTK24 uses this icon in the file open dialog
1463 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1464 wxART_CMN_DIALOG,
1465 wxSize(16, 16)));
1466 #else
1467 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
1468 #endif
1469 // drive
1470 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1471 wxART_CMN_DIALOG,
1472 wxSize(16, 16)));
1473 // cdrom
1474 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1475 wxART_CMN_DIALOG,
1476 wxSize(16, 16)));
1477 // floppy
1478 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1479 wxART_CMN_DIALOG,
1480 wxSize(16, 16)));
1481 // removeable
1482 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1483 wxART_CMN_DIALOG,
1484 wxSize(16, 16)));
1485 // file
1486 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1487 wxART_CMN_DIALOG,
1488 wxSize(16, 16)));
1489 // executable
1490 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1491 {
1492 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1493 wxART_CMN_DIALOG,
1494 wxSize(16, 16)));
1495 delete m_HashTable->Get(_T("exe"));
1496 m_HashTable->Delete(_T("exe"));
1497 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1498 }
1499 /* else put into list by GetIconID
1500 (KDE defines application/x-executable for *.exe and has nice icon)
1501 */
1502 }
1503
1504 wxImageList *wxFileIconsTable::GetSmallImageList()
1505 {
1506 if (!m_smallImageList)
1507 Create();
1508
1509 return m_smallImageList;
1510 }
1511
1512 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1513 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1514 // one icon and we won't resize it
1515
1516 static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1517 {
1518 const unsigned int size = 16;
1519
1520 wxImage smallimg (size, size);
1521 unsigned char *p1, *p2, *ps;
1522 unsigned char mr = img.GetMaskRed(),
1523 mg = img.GetMaskGreen(),
1524 mb = img.GetMaskBlue();
1525
1526 unsigned x, y;
1527 unsigned sr, sg, sb, smask;
1528
1529 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
1530 smallimg.SetMaskColour(mr, mr, mr);
1531
1532 for (y = 0; y < size; y++)
1533 {
1534 for (x = 0; x < size; x++)
1535 {
1536 sr = sg = sb = smask = 0;
1537 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1538 sr += p1[0], sg += p1[1], sb += p1[2];
1539 else smask++;
1540 p1 += 3;
1541 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1542 sr += p1[0], sg += p1[1], sb += p1[2];
1543 else smask++;
1544 p1 += 3;
1545 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1546 sr += p2[0], sg += p2[1], sb += p2[2];
1547 else smask++;
1548 p2 += 3;
1549 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1550 sr += p2[0], sg += p2[1], sb += p2[2];
1551 else smask++;
1552 p2 += 3;
1553
1554 if (smask > 2)
1555 ps[0] = ps[1] = ps[2] = mr;
1556 else
1557 {
1558 ps[0] = (unsigned char)(sr >> 2);
1559 ps[1] = (unsigned char)(sg >> 2);
1560 ps[2] = (unsigned char)(sb >> 2);
1561 }
1562 ps += 3;
1563 }
1564 p1 += size*2 * 3, p2 += size*2 * 3;
1565 }
1566
1567 return wxBitmap(smallimg);
1568 }
1569
1570 // This function is currently not unused anymore
1571 #if 0
1572 // finds empty borders and return non-empty area of image:
1573 static wxImage CutEmptyBorders(const wxImage& img)
1574 {
1575 unsigned char mr = img.GetMaskRed(),
1576 mg = img.GetMaskGreen(),
1577 mb = img.GetMaskBlue();
1578 unsigned char *dt = img.GetData(), *dttmp;
1579 unsigned w = img.GetWidth(), h = img.GetHeight();
1580
1581 unsigned top, bottom, left, right, i;
1582 bool empt;
1583
1584 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1585 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1586
1587 for (empt = true, top = 0; empt && top < h; top++)
1588 {
1589 MK_DTTMP(0, top);
1590 for (i = 0; i < w; i++, dttmp+=3)
1591 NOEMPTY_PIX(empt)
1592 }
1593 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
1594 {
1595 MK_DTTMP(0, bottom);
1596 for (i = 0; i < w; i++, dttmp+=3)
1597 NOEMPTY_PIX(empt)
1598 }
1599 for (empt = true, left = 0; empt && left < w; left++)
1600 {
1601 MK_DTTMP(left, 0);
1602 for (i = 0; i < h; i++, dttmp+=3*w)
1603 NOEMPTY_PIX(empt)
1604 }
1605 for (empt = true, right = w-1; empt && right > left; right--)
1606 {
1607 MK_DTTMP(right, 0);
1608 for (i = 0; i < h; i++, dttmp+=3*w)
1609 NOEMPTY_PIX(empt)
1610 }
1611 top--, left--, bottom++, right++;
1612
1613 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1614 }
1615 #endif // #if 0
1616
1617 #endif // wxUSE_MIMETYPE
1618
1619 int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1620 {
1621 if (!m_smallImageList)
1622 Create();
1623
1624 #if wxUSE_MIMETYPE
1625 if (!extension.empty())
1626 {
1627 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1628 if (entry) return (entry -> id);
1629 }
1630
1631 wxFileType *ft = (mime.empty()) ?
1632 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1633 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
1634
1635 wxIconLocation iconLoc;
1636 wxIcon ic;
1637
1638 {
1639 wxLogNull logNull;
1640 if ( ft && ft->GetIcon(&iconLoc) )
1641 {
1642 ic = wxIcon( iconLoc );
1643 }
1644 }
1645
1646 delete ft;
1647
1648 if ( !ic.Ok() )
1649 {
1650 int newid = file;
1651 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1652 return newid;
1653 }
1654
1655 wxBitmap bmp;
1656 bmp.CopyFromIcon(ic);
1657
1658 if ( !bmp.Ok() )
1659 {
1660 int newid = file;
1661 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1662 return newid;
1663 }
1664
1665 const unsigned int size = 16;
1666
1667 int id = m_smallImageList->GetImageCount();
1668 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1669 {
1670 m_smallImageList->Add(bmp);
1671 }
1672 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1673 else
1674 {
1675 wxImage img = bmp.ConvertToImage();
1676
1677 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1678 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1679 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
1680 else
1681 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1682 }
1683 #endif // wxUSE_IMAGE
1684
1685 m_HashTable->Put(extension, new wxFileIconEntry(id));
1686 return id;
1687
1688 #else // !wxUSE_MIMETYPE
1689
1690 wxUnusedVar(mime);
1691 if (extension == wxT("exe"))
1692 return executable;
1693 else
1694 return file;
1695 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1696 }
1697
1698 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG