/[pcsx2_0.9.7]/branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/generic/dirctrlg.cpp
ViewVC logotype

Contents of /branch/r3113_0.9.7_beta/3rdparty/wxWidgets/src/generic/dirctrlg.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22