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