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