]> git.saurik.com Git - wxWidgets.git/blob - src/generic/dirctrlg.cpp
set depth to 32 when creating pixbuf from wxImage with alpha; correct a few comments
[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 #endif
41
42 #include "wx/module.h"
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
62 // FIXME - Mingw32 1.0 has both _getdrive() and _chdrive(). For now, let's assume
63 // older releases don't, but it should be verified and the checks modified
64 // accordingly.
65 #if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
66 #if !defined(__WXWINCE__)
67 #include <direct.h>
68 #endif
69 #include <stdlib.h>
70 #include <ctype.h>
71 #endif
72
73 #endif
74
75 #if defined(__OS2__) || defined(__DOS__)
76 #ifdef __OS2__
77 #define INCL_BASE
78 #include <os2.h>
79 #ifndef __EMX__
80 #include <direct.h>
81 #endif
82 #include <stdlib.h>
83 #include <ctype.h>
84 #endif
85 extern bool wxIsDriveAvailable(const wxString& dirName);
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 // ----------------------------------------------------------------------------
102 // wxGetAvailableDrives, for WINDOWS, DOS, OS2, MAC, UNIX (returns "/")
103 // ----------------------------------------------------------------------------
104
105 size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
106 {
107 #if defined(__WINDOWS__) || defined(__DOS__) || defined(__OS2__)
108
109 #ifdef __WXWINCE__
110 // No logical drives; return "\"
111 paths.Add(wxT("\\"));
112 names.Add(wxT("\\"));
113 icon_ids.Add(wxFileIconsTable::computer);
114 #elif defined(__WIN32__)
115 wxChar driveBuffer[256];
116 size_t n = (size_t) GetLogicalDriveStrings(255, driveBuffer);
117 size_t i = 0;
118 while (i < n)
119 {
120 wxString path, name;
121 path.Printf(wxT("%c:\\"), driveBuffer[i]);
122 name.Printf(wxT("%c:"), driveBuffer[i]);
123
124 // Do not use GetVolumeInformation to further decorate the
125 // name, since it can cause severe delays on network drives.
126
127 int imageId;
128 int driveType = ::GetDriveType(path);
129 switch (driveType)
130 {
131 case DRIVE_REMOVABLE:
132 if (path == wxT("a:\\") || path == wxT("b:\\"))
133 imageId = wxFileIconsTable::floppy;
134 else
135 imageId = wxFileIconsTable::removeable;
136 break;
137 case DRIVE_CDROM:
138 imageId = wxFileIconsTable::cdrom;
139 break;
140 case DRIVE_REMOTE:
141 case DRIVE_FIXED:
142 default:
143 imageId = wxFileIconsTable::drive;
144 break;
145 }
146
147 paths.Add(path);
148 names.Add(name);
149 icon_ids.Add(imageId);
150
151 while (driveBuffer[i] != wxT('\0'))
152 i ++;
153 i ++;
154 if (driveBuffer[i] == wxT('\0'))
155 break;
156 }
157 #elif defined(__OS2__)
158 APIRET rc;
159 ULONG ulDriveNum = 0;
160 ULONG ulDriveMap = 0;
161 rc = ::DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap);
162 if ( rc == 0)
163 {
164 size_t i = 0;
165 while (i < 26)
166 {
167 if (ulDriveMap & ( 1 << i ))
168 {
169 wxString path, name;
170 path.Printf(wxT("%c:\\"), 'A' + i);
171 name.Printf(wxT("%c:"), 'A' + i);
172
173 // Note: If _filesys is unsupported by some compilers,
174 // we can always replace it by DosQueryFSAttach
175 char filesysname[20];
176 #ifdef __WATCOMC__
177 ULONG cbBuffer = sizeof(filesysname);
178 PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2)filesysname;
179 APIRET rc = ::DosQueryFSAttach(name.fn_str(),0,FSAIL_QUERYNAME,pfsqBuffer,&cbBuffer);
180 if (rc != NO_ERROR)
181 {
182 filesysname[0] = '\0';
183 }
184 #else
185 _filesys(name.fn_str(), filesysname, sizeof(filesysname));
186 #endif
187 /* FAT, LAN, HPFS, CDFS, NFS */
188 int imageId;
189 if (path == wxT("A:\\") || path == wxT("B:\\"))
190 imageId = wxFileIconsTable::floppy;
191 else if (!strcmp(filesysname, "CDFS"))
192 imageId = wxFileIconsTable::cdrom;
193 else if (!strcmp(filesysname, "LAN") ||
194 !strcmp(filesysname, "NFS"))
195 imageId = wxFileIconsTable::drive;
196 else
197 imageId = wxFileIconsTable::drive;
198 paths.Add(path);
199 names.Add(name);
200 icon_ids.Add(imageId);
201 }
202 i ++;
203 }
204 }
205 #else // !__WIN32__, !__OS2__
206 int drive;
207
208 /* If we can switch to the drive, it exists. */
209 for( drive = 1; drive <= 26; drive++ )
210 {
211 wxString path, name;
212 path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1));
213 name.Printf(wxT("%c:"), (char) (drive + 'A' - 1));
214
215 if (wxIsDriveAvailable(path))
216 {
217 paths.Add(path);
218 names.Add(name);
219 icon_ids.Add((drive <= 2) ? wxFileIconsTable::floppy : wxFileIconsTable::drive);
220 }
221 }
222 #endif // __WIN32__/!__WIN32__
223
224 #elif defined(__WXMAC__)
225
226 ItemCount volumeIndex = 1;
227 OSErr err = noErr ;
228
229 while( noErr == err )
230 {
231 HFSUniStr255 volumeName ;
232 FSRef fsRef ;
233 FSVolumeInfo volumeInfo ;
234 err = FSGetVolumeInfo(0, volumeIndex, NULL, kFSVolInfoFlags , &volumeInfo , &volumeName, &fsRef);
235 if( noErr == err )
236 {
237 wxString path = wxMacFSRefToPath( &fsRef ) ;
238 wxString name = wxMacHFSUniStrToString( &volumeName ) ;
239
240 if ( (volumeInfo.flags & kFSVolFlagSoftwareLockedMask) || (volumeInfo.flags & kFSVolFlagHardwareLockedMask) )
241 {
242 icon_ids.Add(wxFileIconsTable::cdrom);
243 }
244 else
245 {
246 icon_ids.Add(wxFileIconsTable::drive);
247 }
248 // todo other removable
249
250 paths.Add(path);
251 names.Add(name);
252 volumeIndex++ ;
253 }
254 }
255
256 #elif defined(__UNIX__)
257 paths.Add(wxT("/"));
258 names.Add(wxT("/"));
259 icon_ids.Add(wxFileIconsTable::computer);
260 #else
261 #error "Unsupported platform in wxGenericDirCtrl!"
262 #endif
263 wxASSERT_MSG( (paths.GetCount() == names.GetCount()), wxT("The number of paths and their human readable names should be equal in number."));
264 wxASSERT_MSG( (paths.GetCount() == icon_ids.GetCount()), wxT("Wrong number of icons for available drives."));
265 return paths.GetCount();
266 }
267
268 // ----------------------------------------------------------------------------
269 // wxIsDriveAvailable
270 // ----------------------------------------------------------------------------
271
272 #if defined(__DOS__)
273
274 bool wxIsDriveAvailable(const wxString& dirName)
275 {
276 // FIXME_MGL - this method leads to hang up under Watcom for some reason
277 #ifdef __WATCOMC__
278 wxUnusedVar(dirName);
279 #else
280 if ( dirName.length() == 3 && dirName[1u] == wxT(':') )
281 {
282 wxString dirNameLower(dirName.Lower());
283 // VS: always return true for removable media, since Win95 doesn't
284 // like it when MS-DOS app accesses empty floppy drive
285 return (dirNameLower[0u] == wxT('a') ||
286 dirNameLower[0u] == wxT('b') ||
287 wxDirExists(dirNameLower));
288 }
289 else
290 #endif
291 return true;
292 }
293
294 #elif defined(__WINDOWS__) || defined(__OS2__)
295
296 int setdrive(int WXUNUSED_IN_WINCE(drive))
297 {
298 #ifdef __WXWINCE__
299 return 0;
300 #elif defined(__GNUWIN32__) && \
301 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
302 return _chdrive(drive);
303 #else
304 wxChar newdrive[4];
305
306 if (drive < 1 || drive > 31)
307 return -1;
308 newdrive[0] = (wxChar)(wxT('A') + drive - 1);
309 newdrive[1] = wxT(':');
310 #ifdef __OS2__
311 newdrive[2] = wxT('\\');
312 newdrive[3] = wxT('\0');
313 #else
314 newdrive[2] = wxT('\0');
315 #endif
316 #if defined(__WXMSW__)
317 if (::SetCurrentDirectory(newdrive))
318 #else
319 // VA doesn't know what LPSTR is and has its own set
320 if (!DosSetCurrentDir((PSZ)newdrive))
321 #endif
322 return 0;
323 else
324 return -1;
325 #endif // !GNUWIN32
326 }
327
328 bool wxIsDriveAvailable(const wxString& WXUNUSED_IN_WINCE(dirName))
329 {
330 #ifdef __WXWINCE__
331 return false;
332 #else
333 #ifdef __WIN32__
334 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
335 #endif
336 bool success = true;
337
338 // Check if this is a root directory and if so,
339 // whether the drive is available.
340 if (dirName.length() == 3 && dirName[(size_t)1] == wxT(':'))
341 {
342 wxString dirNameLower(dirName.Lower());
343 #if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
344 success = wxDirExists(dirNameLower);
345 #else
346 #if defined(__OS2__)
347 // Avoid changing to drive since no media may be inserted.
348 if (dirNameLower[(size_t)0] == 'a' || dirNameLower[(size_t)0] == 'b')
349 return success;
350 #endif
351 int currentDrive = _getdrive();
352 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
353 int err = setdrive( thisDrive ) ;
354 setdrive( currentDrive );
355
356 if (err == -1)
357 {
358 success = false;
359 }
360 #endif
361 }
362 #ifdef __WIN32__
363 (void) SetErrorMode(errorMode);
364 #endif
365
366 return success;
367 #endif
368 }
369 #endif // __WINDOWS__ || __OS2__
370
371 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG
372
373
374
375 #if wxUSE_DIRDLG
376
377 // Function which is called by quick sort. We want to override the default wxArrayString behaviour,
378 // and sort regardless of case.
379 static int wxCMPFUNC_CONV wxDirCtrlStringCompareFunction(const wxString& strFirst, const wxString& strSecond)
380 {
381 return strFirst.CmpNoCase(strSecond);
382 }
383
384 //-----------------------------------------------------------------------------
385 // wxDirItemData
386 //-----------------------------------------------------------------------------
387
388 wxDirItemData::wxDirItemData(const wxString& path, const wxString& name,
389 bool isDir)
390 {
391 m_path = path;
392 m_name = name;
393 /* Insert logic to detect hidden files here
394 * In UnixLand we just check whether the first char is a dot
395 * For FileNameFromPath read LastDirNameInThisPath ;-) */
396 // m_isHidden = (bool)(wxFileNameFromPath(*m_path)[0] == '.');
397 m_isHidden = false;
398 m_isExpanded = false;
399 m_isDir = isDir;
400 }
401
402 void wxDirItemData::SetNewDirName(const wxString& path)
403 {
404 m_path = path;
405 m_name = wxFileNameFromPath(path);
406 }
407
408 bool wxDirItemData::HasSubDirs() const
409 {
410 if (m_path.empty())
411 return false;
412
413 wxDir dir;
414 {
415 wxLogNull nolog;
416 if ( !dir.Open(m_path) )
417 return false;
418 }
419
420 return dir.HasSubDirs();
421 }
422
423 bool wxDirItemData::HasFiles(const wxString& WXUNUSED(spec)) const
424 {
425 if (m_path.empty())
426 return false;
427
428 wxDir dir;
429 {
430 wxLogNull nolog;
431 if ( !dir.Open(m_path) )
432 return false;
433 }
434
435 return dir.HasFiles();
436 }
437
438 //-----------------------------------------------------------------------------
439 // wxGenericDirCtrl
440 //-----------------------------------------------------------------------------
441
442
443 #if wxUSE_EXTENDED_RTTI
444 WX_DEFINE_FLAGS( wxGenericDirCtrlStyle )
445
446 wxBEGIN_FLAGS( wxGenericDirCtrlStyle )
447 // new style border flags, we put them first to
448 // use them for streaming out
449 wxFLAGS_MEMBER(wxBORDER_SIMPLE)
450 wxFLAGS_MEMBER(wxBORDER_SUNKEN)
451 wxFLAGS_MEMBER(wxBORDER_DOUBLE)
452 wxFLAGS_MEMBER(wxBORDER_RAISED)
453 wxFLAGS_MEMBER(wxBORDER_STATIC)
454 wxFLAGS_MEMBER(wxBORDER_NONE)
455
456 // old style border flags
457 wxFLAGS_MEMBER(wxSIMPLE_BORDER)
458 wxFLAGS_MEMBER(wxSUNKEN_BORDER)
459 wxFLAGS_MEMBER(wxDOUBLE_BORDER)
460 wxFLAGS_MEMBER(wxRAISED_BORDER)
461 wxFLAGS_MEMBER(wxSTATIC_BORDER)
462 wxFLAGS_MEMBER(wxBORDER)
463
464 // standard window styles
465 wxFLAGS_MEMBER(wxTAB_TRAVERSAL)
466 wxFLAGS_MEMBER(wxCLIP_CHILDREN)
467 wxFLAGS_MEMBER(wxTRANSPARENT_WINDOW)
468 wxFLAGS_MEMBER(wxWANTS_CHARS)
469 wxFLAGS_MEMBER(wxFULL_REPAINT_ON_RESIZE)
470 wxFLAGS_MEMBER(wxALWAYS_SHOW_SB )
471 wxFLAGS_MEMBER(wxVSCROLL)
472 wxFLAGS_MEMBER(wxHSCROLL)
473
474 wxFLAGS_MEMBER(wxDIRCTRL_DIR_ONLY)
475 wxFLAGS_MEMBER(wxDIRCTRL_3D_INTERNAL)
476 wxFLAGS_MEMBER(wxDIRCTRL_SELECT_FIRST)
477 wxFLAGS_MEMBER(wxDIRCTRL_SHOW_FILTERS)
478
479 wxEND_FLAGS( wxGenericDirCtrlStyle )
480
481 IMPLEMENT_DYNAMIC_CLASS_XTI(wxGenericDirCtrl, wxControl,"wx/dirctrl.h")
482
483 wxBEGIN_PROPERTIES_TABLE(wxGenericDirCtrl)
484 wxHIDE_PROPERTY( Children )
485 wxPROPERTY( DefaultPath , wxString , SetDefaultPath , GetDefaultPath , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group"))
486 wxPROPERTY( Filter , wxString , SetFilter , GetFilter , EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
487 wxPROPERTY( DefaultFilter , int , SetFilterIndex, GetFilterIndex, EMPTY_MACROVALUE , 0 /*flags*/ , wxT("Helpstring") , wxT("group") )
488 wxPROPERTY_FLAGS( WindowStyle, wxGenericDirCtrlStyle, long, SetWindowStyleFlag, GetWindowStyleFlag, EMPTY_MACROVALUE , 0, wxT("Helpstring"), wxT("group") )
489 wxEND_PROPERTIES_TABLE()
490
491 wxBEGIN_HANDLERS_TABLE(wxGenericDirCtrl)
492 wxEND_HANDLERS_TABLE()
493
494 wxCONSTRUCTOR_8( wxGenericDirCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , DefaultPath ,
495 wxPoint , Position , wxSize , Size , long , WindowStyle , wxString , Filter , int , DefaultFilter )
496 #else
497 IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
498 #endif
499
500 BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
501 EVT_TREE_ITEM_EXPANDING (wxID_TREECTRL, wxGenericDirCtrl::OnExpandItem)
502 EVT_TREE_ITEM_COLLAPSED (wxID_TREECTRL, wxGenericDirCtrl::OnCollapseItem)
503 EVT_TREE_BEGIN_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnBeginEditItem)
504 EVT_TREE_END_LABEL_EDIT (wxID_TREECTRL, wxGenericDirCtrl::OnEndEditItem)
505 EVT_SIZE (wxGenericDirCtrl::OnSize)
506 END_EVENT_TABLE()
507
508 wxGenericDirCtrl::wxGenericDirCtrl(void)
509 {
510 Init();
511 }
512
513 void wxGenericDirCtrl::ExpandRoot()
514 {
515 ExpandDir(m_rootId); // automatically expand first level
516
517 // Expand and select the default path
518 if (!m_defaultPath.empty())
519 {
520 ExpandPath(m_defaultPath);
521 }
522 #ifdef __UNIX__
523 else
524 {
525 // On Unix, there's only one node under the (hidden) root node. It
526 // represents the / path, so the user would always have to expand it;
527 // let's do it ourselves
528 ExpandPath( wxT("/") );
529 }
530 #endif
531 }
532
533 bool wxGenericDirCtrl::Create(wxWindow *parent,
534 const wxWindowID id,
535 const wxString& dir,
536 const wxPoint& pos,
537 const wxSize& size,
538 long style,
539 const wxString& filter,
540 int defaultFilter,
541 const wxString& name)
542 {
543 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
544 return false;
545
546 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
547
548 Init();
549
550 long treeStyle = wxTR_HAS_BUTTONS;
551
552 // On Windows CE, if you hide the root, you get a crash when
553 // attempting to access data for children of the root item.
554 #ifndef __WXWINCE__
555 treeStyle |= wxTR_HIDE_ROOT;
556 #endif
557
558 #ifdef __WXGTK20__
559 treeStyle |= wxTR_NO_LINES;
560 #endif
561
562 if (style & wxDIRCTRL_EDIT_LABELS)
563 treeStyle |= wxTR_EDIT_LABELS;
564
565 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
566 treeStyle |= wxNO_BORDER;
567 else
568 treeStyle |= wxBORDER_SUNKEN;
569
570 long filterStyle = 0;
571 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
572 filterStyle |= wxNO_BORDER;
573 else
574 filterStyle |= wxBORDER_SUNKEN;
575
576 m_treeCtrl = CreateTreeCtrl(this, wxID_TREECTRL,
577 wxPoint(0,0), GetClientSize(), treeStyle);
578
579 if (!filter.empty() && (style & wxDIRCTRL_SHOW_FILTERS))
580 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
581
582 m_defaultPath = dir;
583 m_filter = filter;
584
585 if (m_filter.empty())
586 #ifdef __UNIX__
587 m_filter = wxT("*");
588 #else
589 m_filter = wxT("*.*");
590 #endif
591
592 SetFilterIndex(defaultFilter);
593
594 if (m_filterListCtrl)
595 m_filterListCtrl->FillFilterList(filter, defaultFilter);
596
597 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
598
599 m_showHidden = false;
600 wxDirItemData* rootData = new wxDirItemData(wxEmptyString, wxEmptyString, true);
601
602 wxString rootName;
603
604 #if defined(__WINDOWS__) || defined(__OS2__) || defined(__DOS__)
605 rootName = _("Computer");
606 #else
607 rootName = _("Sections");
608 #endif
609
610 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
611 m_treeCtrl->SetItemHasChildren(m_rootId);
612
613 ExpandRoot();
614
615 SetBestSize(size);
616 DoResize();
617
618 return true;
619 }
620
621 wxGenericDirCtrl::~wxGenericDirCtrl()
622 {
623 }
624
625 void wxGenericDirCtrl::Init()
626 {
627 m_showHidden = false;
628 m_currentFilter = 0;
629 m_currentFilterStr = wxEmptyString; // Default: any file
630 m_treeCtrl = NULL;
631 m_filterListCtrl = NULL;
632 }
633
634 wxTreeCtrl* wxGenericDirCtrl::CreateTreeCtrl(wxWindow *parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long treeStyle)
635 {
636 return new wxTreeCtrl(parent, id, pos, size, treeStyle);
637 }
638
639 void wxGenericDirCtrl::ShowHidden( bool show )
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 #if WXWIN_COMPATIBILITY_2_4
1212 // Parses the global filter, returning the number of filters.
1213 // Returns 0 if none or if there's a problem.
1214 // filterStr is in the form: "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1215 int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1216 {
1217 return wxParseCommonDialogsFilter(filterStr, descriptions, filters );
1218 }
1219 #endif // WXWIN_COMPATIBILITY_2_4
1220
1221 void wxGenericDirCtrl::DoResize()
1222 {
1223 wxSize sz = GetClientSize();
1224 int verticalSpacing = 3;
1225 if (m_treeCtrl)
1226 {
1227 wxSize filterSz ;
1228 if (m_filterListCtrl)
1229 {
1230 filterSz = m_filterListCtrl->GetSize();
1231 sz.y -= (filterSz.y + verticalSpacing);
1232 }
1233 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1234 if (m_filterListCtrl)
1235 {
1236 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1237 // Don't know why, but this needs refreshing after a resize (wxMSW)
1238 m_filterListCtrl->Refresh();
1239 }
1240 }
1241 }
1242
1243
1244 void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
1245 {
1246 DoResize();
1247 }
1248
1249 wxTreeItemId wxGenericDirCtrl::AppendItem (const wxTreeItemId & parent,
1250 const wxString & text,
1251 int image, int selectedImage,
1252 wxTreeItemData * data)
1253 {
1254 wxTreeCtrl *treeCtrl = GetTreeCtrl ();
1255
1256 wxASSERT (treeCtrl);
1257
1258 if (treeCtrl)
1259 {
1260 return treeCtrl->AppendItem (parent, text, image, selectedImage, data);
1261 }
1262 else
1263 {
1264 return wxTreeItemId();
1265 }
1266 }
1267
1268
1269 //-----------------------------------------------------------------------------
1270 // wxDirFilterListCtrl
1271 //-----------------------------------------------------------------------------
1272
1273 IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1274
1275 BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1276 EVT_CHOICE(wxID_ANY, wxDirFilterListCtrl::OnSelFilter)
1277 END_EVENT_TABLE()
1278
1279 bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1280 const wxPoint& pos,
1281 const wxSize& size,
1282 long style)
1283 {
1284 m_dirCtrl = parent;
1285 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1286 }
1287
1288 void wxDirFilterListCtrl::Init()
1289 {
1290 m_dirCtrl = NULL;
1291 }
1292
1293 void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
1294 {
1295 int sel = GetSelection();
1296
1297 wxString currentPath = m_dirCtrl->GetPath();
1298
1299 m_dirCtrl->SetFilterIndex(sel);
1300
1301 // If the filter has changed, the view is out of date, so
1302 // collapse the tree.
1303 m_dirCtrl->ReCreateTree();
1304
1305 // Try to restore the selection, or at least the directory
1306 m_dirCtrl->ExpandPath(currentPath);
1307 }
1308
1309 void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1310 {
1311 Clear();
1312 wxArrayString descriptions, filters;
1313 size_t n = (size_t) wxParseCommonDialogsFilter(filter, descriptions, filters);
1314
1315 if (n > 0 && defaultFilter < (int) n)
1316 {
1317 for (size_t i = 0; i < n; i++)
1318 Append(descriptions[i]);
1319 SetSelection(defaultFilter);
1320 }
1321 }
1322 #endif // wxUSE_DIRDLG
1323
1324 #if wxUSE_DIRDLG || wxUSE_FILEDLG
1325
1326 // ----------------------------------------------------------------------------
1327 // wxFileIconsTable icons
1328 // ----------------------------------------------------------------------------
1329
1330 #ifndef __WXGTK24__
1331 /* Computer (c) Julian Smart */
1332 static const char * file_icons_tbl_computer_xpm[] = {
1333 /* columns rows colors chars-per-pixel */
1334 "16 16 42 1",
1335 "r c #4E7FD0",
1336 "$ c #7198D9",
1337 "; c #DCE6F6",
1338 "q c #FFFFFF",
1339 "u c #4A7CCE",
1340 "# c #779DDB",
1341 "w c #95B2E3",
1342 "y c #7FA2DD",
1343 "f c #3263B4",
1344 "= c #EAF0FA",
1345 "< c #B1C7EB",
1346 "% c #6992D7",
1347 "9 c #D9E4F5",
1348 "o c #9BB7E5",
1349 "6 c #F7F9FD",
1350 ", c #BED0EE",
1351 "3 c #F0F5FC",
1352 "1 c #A8C0E8",
1353 " c None",
1354 "0 c #FDFEFF",
1355 "4 c #C4D5F0",
1356 "@ c #81A4DD",
1357 "e c #4377CD",
1358 "- c #E2EAF8",
1359 "i c #9FB9E5",
1360 "> c #CCDAF2",
1361 "+ c #89A9DF",
1362 "s c #5584D1",
1363 "t c #5D89D3",
1364 ": c #D2DFF4",
1365 "5 c #FAFCFE",
1366 "2 c #F5F8FD",
1367 "8 c #DFE8F7",
1368 "& c #5E8AD4",
1369 "X c #638ED5",
1370 "a c #CEDCF2",
1371 "p c #90AFE2",
1372 "d c #2F5DA9",
1373 "* c #5282D0",
1374 "7 c #E5EDF9",
1375 ". c #A2BCE6",
1376 "O c #8CACE0",
1377 /* pixels */
1378 " ",
1379 " .XXXXXXXXXXX ",
1380 " oXO++@#$%&*X ",
1381 " oX=-;:>,<1%X ",
1382 " oX23=-;:4,$X ",
1383 " oX5633789:@X ",
1384 " oX05623=78+X ",
1385 " oXqq05623=OX ",
1386 " oX,,,,,<<<$X ",
1387 " wXXXXXXXXXXe ",
1388 " XrtX%$$y@+O,, ",
1389 " uyiiiiiiiii@< ",
1390 " ouiiiiiiiiiip<a",
1391 " rustX%$$y@+Ow,,",
1392 " dfffffffffffffd",
1393 " "
1394 };
1395 #endif // GTK+ < 2.4
1396
1397 // ----------------------------------------------------------------------------
1398 // wxFileIconsTable & friends
1399 // ----------------------------------------------------------------------------
1400
1401 // global instance of a wxFileIconsTable
1402 wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1403
1404 // A module to allow icons table cleanup
1405
1406 class wxFileIconsTableModule: public wxModule
1407 {
1408 DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1409 public:
1410 wxFileIconsTableModule() {}
1411 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return true; }
1412 void OnExit()
1413 {
1414 if (wxTheFileIconsTable)
1415 {
1416 delete wxTheFileIconsTable;
1417 wxTheFileIconsTable = NULL;
1418 }
1419 }
1420 };
1421
1422 IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1423
1424 class wxFileIconEntry : public wxObject
1425 {
1426 public:
1427 wxFileIconEntry(int i) { id = i; }
1428
1429 int id;
1430 };
1431
1432 wxFileIconsTable::wxFileIconsTable()
1433 {
1434 m_HashTable = NULL;
1435 m_smallImageList = NULL;
1436 }
1437
1438 wxFileIconsTable::~wxFileIconsTable()
1439 {
1440 if (m_HashTable)
1441 {
1442 WX_CLEAR_HASH_TABLE(*m_HashTable);
1443 delete m_HashTable;
1444 }
1445 if (m_smallImageList) delete m_smallImageList;
1446 }
1447
1448 // delayed initialization - wait until first use (wxArtProv not created yet)
1449 void wxFileIconsTable::Create()
1450 {
1451 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
1452 m_HashTable = new wxHashTable(wxKEY_STRING);
1453 m_smallImageList = new wxImageList(16, 16);
1454
1455 // folder:
1456 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
1457 wxART_CMN_DIALOG,
1458 wxSize(16, 16)));
1459 // folder_open
1460 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
1461 wxART_CMN_DIALOG,
1462 wxSize(16, 16)));
1463 // computer
1464 #ifdef __WXGTK24__
1465 // GTK24 uses this icon in the file open dialog
1466 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1467 wxART_CMN_DIALOG,
1468 wxSize(16, 16)));
1469 #else
1470 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
1471 #endif
1472 // drive
1473 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
1474 wxART_CMN_DIALOG,
1475 wxSize(16, 16)));
1476 // cdrom
1477 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
1478 wxART_CMN_DIALOG,
1479 wxSize(16, 16)));
1480 // floppy
1481 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
1482 wxART_CMN_DIALOG,
1483 wxSize(16, 16)));
1484 // removeable
1485 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
1486 wxART_CMN_DIALOG,
1487 wxSize(16, 16)));
1488 // file
1489 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
1490 wxART_CMN_DIALOG,
1491 wxSize(16, 16)));
1492 // executable
1493 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1494 {
1495 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
1496 wxART_CMN_DIALOG,
1497 wxSize(16, 16)));
1498 delete m_HashTable->Get(_T("exe"));
1499 m_HashTable->Delete(_T("exe"));
1500 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1501 }
1502 /* else put into list by GetIconID
1503 (KDE defines application/x-executable for *.exe and has nice icon)
1504 */
1505 }
1506
1507 wxImageList *wxFileIconsTable::GetSmallImageList()
1508 {
1509 if (!m_smallImageList)
1510 Create();
1511
1512 return m_smallImageList;
1513 }
1514
1515 #if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1516 // VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1517 // one icon and we won't resize it
1518
1519 static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1520 {
1521 const unsigned int size = 16;
1522
1523 wxImage smallimg (size, size);
1524 unsigned char *p1, *p2, *ps;
1525 unsigned char mr = img.GetMaskRed(),
1526 mg = img.GetMaskGreen(),
1527 mb = img.GetMaskBlue();
1528
1529 unsigned x, y;
1530 unsigned sr, sg, sb, smask;
1531
1532 p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
1533 smallimg.SetMaskColour(mr, mr, mr);
1534
1535 for (y = 0; y < size; y++)
1536 {
1537 for (x = 0; x < size; x++)
1538 {
1539 sr = sg = sb = smask = 0;
1540 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1541 sr += p1[0], sg += p1[1], sb += p1[2];
1542 else smask++;
1543 p1 += 3;
1544 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1545 sr += p1[0], sg += p1[1], sb += p1[2];
1546 else smask++;
1547 p1 += 3;
1548 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1549 sr += p2[0], sg += p2[1], sb += p2[2];
1550 else smask++;
1551 p2 += 3;
1552 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1553 sr += p2[0], sg += p2[1], sb += p2[2];
1554 else smask++;
1555 p2 += 3;
1556
1557 if (smask > 2)
1558 ps[0] = ps[1] = ps[2] = mr;
1559 else
1560 {
1561 ps[0] = (unsigned char)(sr >> 2);
1562 ps[1] = (unsigned char)(sg >> 2);
1563 ps[2] = (unsigned char)(sb >> 2);
1564 }
1565 ps += 3;
1566 }
1567 p1 += size*2 * 3, p2 += size*2 * 3;
1568 }
1569
1570 return wxBitmap(smallimg);
1571 }
1572
1573 // This function is currently not unused anymore
1574 #if 0
1575 // finds empty borders and return non-empty area of image:
1576 static wxImage CutEmptyBorders(const wxImage& img)
1577 {
1578 unsigned char mr = img.GetMaskRed(),
1579 mg = img.GetMaskGreen(),
1580 mb = img.GetMaskBlue();
1581 unsigned char *dt = img.GetData(), *dttmp;
1582 unsigned w = img.GetWidth(), h = img.GetHeight();
1583
1584 unsigned top, bottom, left, right, i;
1585 bool empt;
1586
1587 #define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1588 #define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
1589
1590 for (empt = true, top = 0; empt && top < h; top++)
1591 {
1592 MK_DTTMP(0, top);
1593 for (i = 0; i < w; i++, dttmp+=3)
1594 NOEMPTY_PIX(empt)
1595 }
1596 for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
1597 {
1598 MK_DTTMP(0, bottom);
1599 for (i = 0; i < w; i++, dttmp+=3)
1600 NOEMPTY_PIX(empt)
1601 }
1602 for (empt = true, left = 0; empt && left < w; left++)
1603 {
1604 MK_DTTMP(left, 0);
1605 for (i = 0; i < h; i++, dttmp+=3*w)
1606 NOEMPTY_PIX(empt)
1607 }
1608 for (empt = true, right = w-1; empt && right > left; right--)
1609 {
1610 MK_DTTMP(right, 0);
1611 for (i = 0; i < h; i++, dttmp+=3*w)
1612 NOEMPTY_PIX(empt)
1613 }
1614 top--, left--, bottom++, right++;
1615
1616 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1617 }
1618 #endif // #if 0
1619
1620 #endif // wxUSE_MIMETYPE
1621
1622 int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1623 {
1624 if (!m_smallImageList)
1625 Create();
1626
1627 #if wxUSE_MIMETYPE
1628 if (!extension.empty())
1629 {
1630 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1631 if (entry) return (entry -> id);
1632 }
1633
1634 wxFileType *ft = (mime.empty()) ?
1635 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1636 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
1637
1638 wxIconLocation iconLoc;
1639 wxIcon ic;
1640
1641 {
1642 wxLogNull logNull;
1643 if ( ft && ft->GetIcon(&iconLoc) )
1644 {
1645 ic = wxIcon( iconLoc );
1646 }
1647 }
1648
1649 delete ft;
1650
1651 if ( !ic.Ok() )
1652 {
1653 int newid = file;
1654 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1655 return newid;
1656 }
1657
1658 wxBitmap bmp;
1659 bmp.CopyFromIcon(ic);
1660
1661 if ( !bmp.Ok() )
1662 {
1663 int newid = file;
1664 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1665 return newid;
1666 }
1667
1668 const unsigned int size = 16;
1669
1670 int id = m_smallImageList->GetImageCount();
1671 if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
1672 {
1673 m_smallImageList->Add(bmp);
1674 }
1675 #if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
1676 else
1677 {
1678 wxImage img = bmp.ConvertToImage();
1679
1680 if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
1681 // m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
1682 m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
1683 else
1684 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1685 }
1686 #endif // wxUSE_IMAGE
1687
1688 m_HashTable->Put(extension, new wxFileIconEntry(id));
1689 return id;
1690
1691 #else // !wxUSE_MIMETYPE
1692
1693 wxUnusedVar(mime);
1694 if (extension == wxT("exe"))
1695 return executable;
1696 else
1697 return file;
1698 #endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1699 }
1700
1701 #endif // wxUSE_DIRDLG || wxUSE_FILEDLG