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