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