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