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