]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
Disabling code that causes display glitch on Mac
[wxWidgets.git] / src / generic / dirctrlg.cpp
CommitLineData
51a58d8b
JS
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#ifdef __GNUG__
3fa4bd0e 13#pragma implementation "dirctrlg.h"
51a58d8b
JS
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
1e6feb95 23#if wxUSE_DIRDLG
51a58d8b 24
06cc1fb9
JS
25#include "wx/generic/dirctrlg.h"
26
e624f5ba 27#include "wx/module.h"
51a58d8b 28#include "wx/utils.h"
51a58d8b
JS
29#include "wx/button.h"
30#include "wx/layout.h"
31#include "wx/msgdlg.h"
618a5e38 32#include "wx/textctrl.h"
51a58d8b
JS
33#include "wx/textdlg.h"
34#include "wx/filefn.h"
35#include "wx/cmndata.h"
36#include "wx/gdicmn.h"
37#include "wx/intl.h"
38#include "wx/imaglist.h"
39#include "wx/icon.h"
40#include "wx/log.h"
41#include "wx/sizer.h"
42#include "wx/tokenzr.h"
43#include "wx/dir.h"
3fa4bd0e 44#include "wx/settings.h"
06cc1fb9
JS
45#include "wx/artprov.h"
46#include "wx/hash.h"
47#include "wx/mimetype.h"
48#include "wx/image.h"
49#include "wx/choice.h"
51a58d8b
JS
50
51#if wxUSE_STATLINE
52 #include "wx/statline.h"
53#endif
54
76a5e5d2
SC
55#if defined(__WXMAC__)
56 #include "wx/mac/private.h" // includes mac headers
57#endif
58
51a58d8b 59#ifdef __WXMSW__
2736b3ce 60#include <windows.h>
7328394a 61
c9ecda60
VS
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.
b4da152e 65#if !defined(__GNUWIN32__) || (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
c9ecda60
VS
66 #include <direct.h>
67 #include <stdlib.h>
68 #include <ctype.h>
51a58d8b
JS
69#endif
70
7328394a
JS
71#endif
72
ec1b28a3
DW
73#ifdef __WXPM__
74
75#define INCL_BASE
76#include <os2.h>
2b5f62a0 77#ifndef __EMX__
ec1b28a3 78#include <direct.h>
2b5f62a0 79#endif
ec1b28a3
DW
80#include <stdlib.h>
81#include <ctype.h>
4f5c180e 82extern bool wxIsDriveAvailable(const wxString& dirName);
ec1b28a3
DW
83#endif // __WXPM__
84
f4ac0693 85#if defined(__WXMAC__)
2d4e4f80
GD
86# ifdef __DARWIN__
87# include "MoreFilesX.h"
88# else
89# include "MoreFilesExtras.h"
90# endif
bedaf53e
SC
91#endif
92
267a7108
JS
93#ifdef __BORLANDC__
94#include "dos.h"
95#endif
96
51a58d8b
JS
97// If compiled under Windows, this macro can cause problems
98#ifdef GetFirstChild
99#undef GetFirstChild
100#endif
101
06cc1fb9
JS
102// ----------------------------------------------------------------------------
103// wxGetAvailableDrives, for WINDOWS, DOS, WXPM, MAC, UNIX (returns "/")
104// ----------------------------------------------------------------------------
51a58d8b 105
06cc1fb9
JS
106size_t wxGetAvailableDrives(wxArrayString &paths, wxArrayString &names, wxArrayInt &icon_ids)
107{
108#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b 109
06cc1fb9
JS
110#ifdef __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]);
51a58d8b 119
06cc1fb9
JS
120 int imageId = wxFileIconsTable::drive;
121 int driveType = ::GetDriveType(path);
122 switch (driveType)
123 {
124 case DRIVE_REMOVABLE:
125 if (path == wxT("a:\\") || path == wxT("b:\\"))
126 imageId = wxFileIconsTable::floppy;
127 else
128 imageId = wxFileIconsTable::removeable;
129 break;
130 case DRIVE_CDROM:
131 imageId = wxFileIconsTable::cdrom;
132 break;
133 case DRIVE_REMOTE:
134 case DRIVE_FIXED:
135 default:
136 imageId = wxFileIconsTable::drive;
137 break;
138 }
51a58d8b 139
06cc1fb9
JS
140 paths.Add(path);
141 names.Add(name);
142 icon_ids.Add(imageId);
51a58d8b 143
06cc1fb9
JS
144 while (driveBuffer[i] != wxT('\0'))
145 i ++;
146 i ++;
147 if (driveBuffer[i] == wxT('\0'))
148 break;
149 }
150#else // !__WIN32__
151 int drive;
51a58d8b 152
06cc1fb9
JS
153 /* If we can switch to the drive, it exists. */
154 for( drive = 1; drive <= 26; drive++ )
155 {
156 wxString path, name;
157 path.Printf(wxT("%c:\\"), (char) (drive + 'a' - 1));
158 name.Printf(wxT("%c:"), (char) (drive + 'A' - 1));
51a58d8b 159
06cc1fb9
JS
160 if (wxIsDriveAvailable(path))
161 {
162 paths.Add(path);
163 names.Add(name);
164 icon_ids.Add((drive <= 2) ? wxFileIconsTable::floppy : wxFileIconsTable::drive);
165 }
166 }
167#endif // __WIN32__/!__WIN32__
168
169#elif defined(__WXMAC__)
170#ifdef __DARWIN__
171 FSRef **theVolRefs;
172 ItemCount theVolCount;
173 char thePath[FILENAME_MAX];
174
175 if (FSGetMountedVolumes(&theVolRefs, &theVolCount) == noErr) {
176 ItemCount index;
177 ::HLock( (Handle)theVolRefs ) ;
178 for (index = 0; index < theVolCount; ++index) {
179 // get the POSIX path associated with the FSRef
180 if ( FSRefMakePath(&((*theVolRefs)[index]),
181 (UInt8 *)thePath, sizeof(thePath)) != noErr ) {
182 continue;
183 }
184 // add path separator at end if necessary
185 wxString path( thePath ) ;
186 if (path.Last() != wxFILE_SEP_PATH) {
187 path += wxFILE_SEP_PATH;
188 }
189 // get Mac volume name for display
190 FSVolumeRefNum vRefNum ;
191 HFSUniStr255 volumeName ;
192
193 if ( FSGetVRefNum(&((*theVolRefs)[index]), &vRefNum) != noErr ) {
194 continue;
195 }
196 if ( FSGetVInfo(vRefNum, &volumeName, NULL, NULL) != noErr ) {
197 continue;
198 }
199 // get C string from Unicode HFS name
200 // see: http://developer.apple.com/carbon/tipsandtricks.html
201 CFStringRef cfstr = CFStringCreateWithCharacters( kCFAllocatorDefault,
202 volumeName.unicode,
203 volumeName.length );
204 // Do something with str
205 char *cstr = NewPtr(CFStringGetLength(cfstr) + 1);
206 if (( cstr == NULL ) ||
207 !CFStringGetCString(cfstr, cstr, CFStringGetLength(cfstr) + 1,
208 kCFStringEncodingMacRoman))
209 {
210 CFRelease( cstr );
211 continue;
212 }
213 wxString name( cstr );
214 DisposePtr( cstr );
215 CFRelease( cfstr );
216
217 GetVolParmsInfoBuffer volParmsInfo;
218 UInt32 actualSize;
219 if ( FSGetVolParms(vRefNum, sizeof(volParmsInfo), &volParmsInfo, &actualSize) != noErr ) {
220 continue;
221 }
222
223 paths.Add(path);
224 names.Add(name);
225
226 if ( VolIsEjectable(&volParmsInfo) )
227 icon_ids.Add(wxFileIconsTable::cdrom);
228 else
229 icon_ids.Add(wxFileIconsTable::drive);
230 }
231 ::HUnlock( (Handle)theVolRefs );
232 ::DisposeHandle( (Handle)theVolRefs );
233 }
234#else // !__DARWIN__
235 FSSpec volume;
236 short index = 1;
237 while(1)
238 {
239 short actualCount = 0 ;
240 if (OnLine(&volume, 1, &actualCount, &index ) != noErr || actualCount==0)
241 {
242 break;
243 }
244
245 wxString name = wxMacFSSpec2MacFilename( &volume );
246 paths.Add(name + wxFILE_SEP_PATH);
247 names.Add(name);
248 icon_ids.Add(wxFileIconsTable::drive);
249 }
250#endif // __DARWIN__
251
252#elif defined(__UNIX__)
253 paths.Add(wxT("/"));
254 names.Add(wxT("/"));
255 icon_ids.Add(wxFileIconsTable::computer);
256#else
257 #error "Unsupported platform in wxGenericDirCtrl!"
258#endif
259 return paths.GetCount();
260}
51a58d8b 261
06cc1fb9
JS
262// ----------------------------------------------------------------------------
263// wxIsDriveAvailable
264// ----------------------------------------------------------------------------
748fcded 265
748fcded 266#if defined(__DOS__)
db5333a5 267
37fd1c97 268bool wxIsDriveAvailable(const wxString& dirName)
db5333a5 269{
6157794c
VS
270 // FIXME_MGL - this method leads to hang up under Watcom for some reason
271#ifndef __WATCOMC__
db5333a5
VS
272 if ( dirName.Len() == 3 && dirName[1u] == wxT(':') )
273 {
274 wxString dirNameLower(dirName.Lower());
275 // VS: always return TRUE for removable media, since Win95 doesn't
276 // like it when MS-DOS app accesses empty floppy drive
277 return (dirNameLower[0u] == wxT('a') ||
278 dirNameLower[0u] == wxT('b') ||
279 wxPathExists(dirNameLower));
280 }
281 else
6157794c 282#endif
db5333a5
VS
283 return TRUE;
284}
285
286#elif defined(__WINDOWS__) || defined(__WXPM__)
287
33ac7e6f
KB
288int setdrive(int drive)
289{
c9ecda60
VS
290#if defined(__GNUWIN32__) && \
291 (defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
292 return _chdrive(drive);
293#else
e987a489 294 wxChar newdrive[3];
33ac7e6f
KB
295
296 if (drive < 1 || drive > 31)
297 return -1;
e987a489
VS
298 newdrive[0] = (wxChar)(wxT('A') + drive - 1);
299 newdrive[1] = wxT(':');
300 newdrive[2] = wxT('\0');
50c76ce1 301#if defined(__WXMSW__)
788722ac
JS
302#ifdef __WIN16__
303 if (wxSetWorkingDirectory(newdrive))
304#else
e987a489 305 if (::SetCurrentDirectory(newdrive))
788722ac 306#endif
50c76ce1
DW
307#else
308 // VA doesn't know what LPSTR is and has its own set
309 if (DosSetCurrentDir((PSZ)newdrive))
310#endif
33ac7e6f
KB
311 return 0;
312 else
313 return -1;
c9ecda60 314#endif // !GNUWIN32
33ac7e6f
KB
315}
316
37fd1c97 317bool wxIsDriveAvailable(const wxString& dirName)
ad9cd15c
JS
318{
319#ifdef __WIN32__
33fed835 320 UINT errorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX);
ad9cd15c 321#endif
33fed835 322 bool success = TRUE;
ad9cd15c
JS
323
324 // Check if this is a root directory and if so,
2d4e4f80 325 // whether the drive is available.
33fed835 326 if (dirName.Len() == 3 && dirName[(size_t)1] == wxT(':'))
ad9cd15c 327 {
33fed835 328 wxString dirNameLower(dirName.Lower());
b4da152e 329#if defined(__GNUWIN32__) && !(defined(__MINGW32_MAJOR_VERSION) && __MINGW32_MAJOR_VERSION >= 1)
7328394a
JS
330 success = wxPathExists(dirNameLower);
331#else
ad9cd15c 332 int currentDrive = _getdrive();
33fed835 333 int thisDrive = (int) (dirNameLower[(size_t)0] - 'a' + 1) ;
33ac7e6f
KB
334 int err = setdrive( thisDrive ) ;
335 setdrive( currentDrive );
ad9cd15c
JS
336
337 if (err == -1)
338 {
339 success = FALSE;
340 }
7328394a 341#endif
ad9cd15c
JS
342 }
343#ifdef __WIN32__
33fed835 344 (void) SetErrorMode(errorMode);
ad9cd15c
JS
345#endif
346
33fed835 347 return success;
ad9cd15c 348}
db5333a5 349#endif // __WINDOWS__ || __WXPM__
ad9cd15c 350
57e26a09
JS
351// Function which is called by quick sort. We want to override the default wxArrayString behaviour,
352// and sort regardless of case.
353static int LINKAGEMODE wxDirCtrlStringCompareFunction(const void *first, const void *second)
354{
355 wxString *strFirst = (wxString *)first;
356 wxString *strSecond = (wxString *)second;
574c939e 357
57e26a09
JS
358 return strFirst->CmpNoCase(*strSecond);
359}
360
51a58d8b 361//-----------------------------------------------------------------------------
748fcded 362// wxDirItemData
51a58d8b
JS
363//-----------------------------------------------------------------------------
364
748fcded
VS
365wxDirItemData::wxDirItemData(const wxString& path, const wxString& name,
366 bool isDir)
51a58d8b
JS
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;
51a58d8b
JS
375 m_isExpanded = FALSE;
376 m_isDir = isDir;
377}
378
748fcded 379wxDirItemData::~wxDirItemData()
51a58d8b
JS
380{
381}
382
748fcded 383void wxDirItemData::SetNewDirName(const wxString& path)
51a58d8b
JS
384{
385 m_path = path;
748fcded
VS
386 m_name = wxFileNameFromPath(path);
387}
388
389bool wxDirItemData::HasSubDirs() const
390{
391 if (m_path.IsEmpty())
392 return FALSE;
393
394 wxDir dir;
395 {
396 wxLogNull nolog;
397 if ( !dir.Open(m_path) )
398 return FALSE;
399 }
400
401 return dir.HasSubDirs();
402}
403
574c939e 404bool wxDirItemData::HasFiles(const wxString& WXUNUSED(spec)) const
748fcded
VS
405{
406 if (m_path.IsEmpty())
407 return FALSE;
408
409 wxDir dir;
410 {
411 wxLogNull nolog;
412 if ( !dir.Open(m_path) )
413 return FALSE;
414 }
415
416 return dir.HasFiles();
51a58d8b
JS
417}
418
51a58d8b
JS
419//-----------------------------------------------------------------------------
420// wxGenericDirCtrl
421//-----------------------------------------------------------------------------
422
423IMPLEMENT_DYNAMIC_CLASS(wxGenericDirCtrl, wxControl)
424
425BEGIN_EVENT_TABLE(wxGenericDirCtrl, wxControl)
426 EVT_TREE_ITEM_EXPANDING (-1, wxGenericDirCtrl::OnExpandItem)
427 EVT_TREE_ITEM_COLLAPSED (-1, wxGenericDirCtrl::OnCollapseItem)
428 EVT_TREE_BEGIN_LABEL_EDIT (-1, wxGenericDirCtrl::OnBeginEditItem)
429 EVT_TREE_END_LABEL_EDIT (-1, wxGenericDirCtrl::OnEndEditItem)
430 EVT_SIZE (wxGenericDirCtrl::OnSize)
431END_EVENT_TABLE()
432
433wxGenericDirCtrl::wxGenericDirCtrl(void)
434{
435 Init();
436}
437
438bool wxGenericDirCtrl::Create(wxWindow *parent,
748fcded
VS
439 const wxWindowID id,
440 const wxString& dir,
441 const wxPoint& pos,
442 const wxSize& size,
443 long style,
444 const wxString& filter,
445 int defaultFilter,
446 const wxString& name)
51a58d8b
JS
447{
448 if (!wxControl::Create(parent, id, pos, size, style, wxDefaultValidator, name))
449 return FALSE;
450
db5333a5 451 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE));
51a58d8b
JS
452
453 Init();
454
08887820 455 long treeStyle = wxTR_HAS_BUTTONS | wxTR_HIDE_ROOT;
fd775aae 456
dabd1377 457 if (style & wxDIRCTRL_EDIT_LABELS)
fd775aae
JS
458 treeStyle |= wxTR_EDIT_LABELS;
459
51a58d8b
JS
460 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
461 treeStyle |= wxNO_BORDER;
462
463 long filterStyle = 0;
464 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
465 filterStyle |= wxNO_BORDER;
466
467 m_treeCtrl = new wxTreeCtrl(this, wxID_TREECTRL, pos, size, treeStyle);
468
469 if (!filter.IsEmpty() && (style & wxDIRCTRL_SHOW_FILTERS))
470 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
471
472 m_defaultPath = dir;
473 m_filter = filter;
474
475 SetFilterIndex(defaultFilter);
476
477 if (m_filterListCtrl)
478 m_filterListCtrl->FillFilterList(filter, defaultFilter);
479
06cc1fb9 480 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
51a58d8b
JS
481
482 m_showHidden = FALSE;
748fcded 483 wxDirItemData* rootData = new wxDirItemData(wxT(""), wxT(""), TRUE);
51a58d8b
JS
484
485 wxString rootName;
486
db5333a5 487#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
51a58d8b
JS
488 rootName = _("Computer");
489#else
490 rootName = _("Sections");
491#endif
492
493 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
494 m_treeCtrl->SetItemHasChildren(m_rootId);
08887820 495 ExpandDir(m_rootId); // automatically expand first level
51a58d8b
JS
496
497 // Expand and select the default path
498 if (!m_defaultPath.IsEmpty())
499 ExpandPath(m_defaultPath);
500
501 DoResize();
502
503 return TRUE;
504}
505
506wxGenericDirCtrl::~wxGenericDirCtrl()
507{
51a58d8b
JS
508}
509
510void wxGenericDirCtrl::Init()
511{
512 m_showHidden = FALSE;
51a58d8b
JS
513 m_currentFilter = 0;
514 m_currentFilterStr = wxEmptyString; // Default: any file
515 m_treeCtrl = NULL;
516 m_filterListCtrl = NULL;
517}
518
42dcacf0
RR
519void wxGenericDirCtrl::ShowHidden( bool show )
520{
521 m_showHidden = show;
574c939e 522
3b423cdd 523 wxString path = GetPath();
08887820 524 ReCreateTree();
3b423cdd 525 SetPath(path);
42dcacf0
RR
526}
527
51a58d8b
JS
528void wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
529{
748fcded 530 wxDirItemData *dir_item = new wxDirItemData(path,name,TRUE);
51a58d8b 531
51a58d8b 532 wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, imageId, -1, dir_item);
4f5c180e 533
06cc1fb9
JS
534 m_treeCtrl->SetItemHasChildren(id);
535}
51a58d8b 536
06cc1fb9
JS
537void wxGenericDirCtrl::SetupSections()
538{
539 wxArrayString paths, names;
540 wxArrayInt icons;
2d4e4f80 541
06cc1fb9 542 size_t n, count = wxGetAvailableDrives(paths, names, icons);
4f5c180e 543
06cc1fb9
JS
544 for (n = 0; n < count; n++)
545 {
546 AddSection(paths[n], names[n], icons[n]);
bedaf53e 547 }
51a58d8b
JS
548}
549
550void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
551{
552 // don't rename the main entry "Sections"
553 if (event.GetItem() == m_rootId)
554 {
555 event.Veto();
556 return;
557 }
558
559 // don't rename the individual sections
99006e44 560 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
51a58d8b
JS
561 {
562 event.Veto();
563 return;
564 }
565}
566
567void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
568{
569 if ((event.GetLabel().IsEmpty()) ||
570 (event.GetLabel() == _(".")) ||
571 (event.GetLabel() == _("..")) ||
06cc1fb9
JS
572 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
573 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
574 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
51a58d8b
JS
575 {
576 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
577 dialog.ShowModal();
578 event.Veto();
579 return;
580 }
581
582 wxTreeItemId id = event.GetItem();
748fcded 583 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
51a58d8b
JS
584 wxASSERT( data );
585
586 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 587 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
588 new_name += event.GetLabel();
589
590 wxLogNull log;
591
592 if (wxFileExists(new_name))
593 {
594 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
595 dialog.ShowModal();
596 event.Veto();
597 }
598
599 if (wxRenameFile(data->m_path,new_name))
600 {
601 data->SetNewDirName( new_name );
602 }
603 else
604 {
605 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
606 dialog.ShowModal();
607 event.Veto();
608 }
609}
610
611void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
612{
613 wxTreeItemId parentId = event.GetItem();
614
748fcded
VS
615 // VS: this is needed because the event handler is called from wxTreeCtrl
616 // ctor when wxTR_HIDE_ROOT was specified
617 if (m_rootId == 0)
618 m_rootId = m_treeCtrl->GetRootItem();
619
51a58d8b
JS
620 ExpandDir(parentId);
621}
622
623void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
624{
08887820
VS
625 CollapseDir(event.GetItem());
626}
627
628void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
629{
630 wxTreeItemId child;
51a58d8b 631
08887820 632 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
633 if (!data->m_isExpanded)
634 return;
635
636 data->m_isExpanded = FALSE;
637 long cookie;
638 /* Workaround because DeleteChildren has disapeared (why?) and
639 * CollapseAndReset doesn't work as advertised (deletes parent too) */
08887820 640 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
641 while (child.IsOk())
642 {
643 m_treeCtrl->Delete(child);
644 /* Not GetNextChild below, because the cookie mechanism can't
645 * handle disappearing children! */
08887820 646 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
647 }
648}
649
650void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
651{
748fcded 652 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
653
654 if (data->m_isExpanded)
655 return;
656
657 data->m_isExpanded = TRUE;
658
748fcded 659 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
660 {
661 SetupSections();
662 return;
663 }
664
665 wxASSERT(data);
666
667 wxString search,path,filename;
668
669 wxString dirName(data->m_path);
670
db5333a5 671#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
672 // Check if this is a root directory and if so,
673 // whether the drive is avaiable.
33fed835
MB
674 if (!wxIsDriveAvailable(dirName))
675 {
ad9cd15c 676 data->m_isExpanded = FALSE;
7328394a
JS
677 //wxMessageBox(wxT("Sorry, this drive is not available."));
678 return;
33fed835 679 }
51a58d8b
JS
680#endif
681
682 // This may take a longish time. Go to busy cursor
683 wxBusyCursor busy;
684
db5333a5 685#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
686 if (dirName.Last() == ':')
687 dirName += wxString(wxFILE_SEP_PATH);
688#endif
689
690 wxArrayString dirs;
691 wxArrayString filenames;
692
693 wxDir d;
694 wxString eachFilename;
695
f9c165b1 696 wxLogNull log;
51a58d8b
JS
697 d.Open(dirName);
698
699 if (d.IsOpened())
700 {
42dcacf0
RR
701 int style = wxDIR_DIRS;
702 if (m_showHidden) style |= wxDIR_HIDDEN;
703 if (d.GetFirst(& eachFilename, wxEmptyString, style))
51a58d8b
JS
704 {
705 do
706 {
707 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
708 {
709 dirs.Add(eachFilename);
710 }
711 }
748fcded 712 while (d.GetNext(& eachFilename));
51a58d8b
JS
713 }
714 }
57e26a09 715 dirs.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
51a58d8b
JS
716
717 // Now do the filenames -- but only if we're allowed to
718 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
719 {
f9c165b1
JS
720 wxLogNull log;
721
51a58d8b 722 d.Open(dirName);
ec1b28a3 723
51a58d8b
JS
724 if (d.IsOpened())
725 {
726 if (d.GetFirst(& eachFilename, m_currentFilterStr, wxDIR_FILES))
727 {
728 do
729 {
730 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
731 {
732 filenames.Add(eachFilename);
733 }
734 }
748fcded 735 while (d.GetNext(& eachFilename));
51a58d8b
JS
736 }
737 }
57e26a09 738 filenames.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
51a58d8b
JS
739 }
740
741 // Add the sorted dirs
742 size_t i;
743 for (i = 0; i < dirs.Count(); i++)
744 {
745 wxString eachFilename(dirs[i]);
746 path = dirName;
083f7497 747 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
748 path += wxString(wxFILE_SEP_PATH);
749 path += eachFilename;
750
748fcded 751 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,TRUE);
06cc1fb9
JS
752 wxTreeItemId id = m_treeCtrl->AppendItem( parentId, eachFilename,
753 wxFileIconsTable::folder, -1, dir_item);
754 m_treeCtrl->SetItemImage( id, wxFileIconsTable::folder_open,
755 wxTreeItemIcon_Expanded );
ec1b28a3 756
51a58d8b 757 // Has this got any children? If so, make it expandable.
748fcded
VS
758 // (There are two situations when a dir has children: either it
759 // has subdirectories or it contains files that weren't filtered
760 // out. The latter only applies to dirctrl with files.)
761 if ( dir_item->HasSubDirs() ||
762 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
763 dir_item->HasFiles(m_currentFilterStr)) )
51a58d8b 764 {
748fcded 765 m_treeCtrl->SetItemHasChildren(id);
51a58d8b 766 }
51a58d8b
JS
767 }
768
769 // Add the sorted filenames
770 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
771 {
772 for (i = 0; i < filenames.Count(); i++)
773 {
774 wxString eachFilename(filenames[i]);
775 path = dirName;
083f7497 776 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
777 path += wxString(wxFILE_SEP_PATH);
778 path += eachFilename;
779 //path = dirName + wxString(wxT("/")) + eachFilename;
748fcded 780 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,FALSE);
06cc1fb9
JS
781 int image_id = wxFileIconsTable::file;
782 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
783 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
784 (void)m_treeCtrl->AppendItem( parentId, eachFilename, image_id, -1, dir_item);
51a58d8b
JS
785 }
786 }
787}
788
08887820
VS
789void wxGenericDirCtrl::ReCreateTree()
790{
791 CollapseDir(m_treeCtrl->GetRootItem());
792 ExpandDir(m_treeCtrl->GetRootItem());
793}
794
51a58d8b
JS
795// Find the child that matches the first part of 'path'.
796// E.g. if a child path is "/usr" and 'path' is "/usr/include"
797// then the child for /usr is returned.
798wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
799{
800 wxString path2(path);
ec1b28a3 801
51a58d8b
JS
802 // Make sure all separators are as per the current platform
803 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
804 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 805
51a58d8b
JS
806 // Append a separator to foil bogus substring matching
807 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
808
809 // In MSW or PM, case is not significant
db5333a5 810#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
811 path2.MakeLower();
812#endif
ec1b28a3 813
51a58d8b
JS
814 long cookie;
815 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 816 while (childId.IsOk())
51a58d8b 817 {
748fcded 818 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 819
748fcded 820 if (data && !data->m_path.IsEmpty())
51a58d8b
JS
821 {
822 wxString childPath(data->m_path);
083f7497 823 if (!wxEndsWithPathSeparator(childPath))
51a58d8b 824 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
825
826 // In MSW and PM, case is not significant
db5333a5 827#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
828 childPath.MakeLower();
829#endif
ec1b28a3 830
51a58d8b
JS
831 if (childPath.Len() <= path2.Len())
832 {
833 wxString path3 = path2.Mid(0, childPath.Len());
834 if (childPath == path3)
835 {
836 if (path3.Len() == path2.Len())
837 done = TRUE;
838 else
839 done = FALSE;
840 return childId;
841 }
842 }
843 }
ec1b28a3 844
748fcded 845 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 846 }
3fa4bd0e
VS
847 wxTreeItemId invalid;
848 return invalid;
51a58d8b
JS
849}
850
851// Try to expand as much of the given path as possible,
852// and select the given tree item.
853bool wxGenericDirCtrl::ExpandPath(const wxString& path)
854{
855 bool done = FALSE;
856 wxTreeItemId id = FindChild(m_rootId, path, done);
857 wxTreeItemId lastId = id; // The last non-zero id
237387ad 858 while (id.IsOk() && !done)
51a58d8b
JS
859 {
860 ExpandDir(id);
861
862 id = FindChild(id, path, done);
53ccf1c0 863 if (id.IsOk())
51a58d8b
JS
864 lastId = id;
865 }
53ccf1c0 866 if (lastId.IsOk())
51a58d8b 867 {
748fcded 868 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
51a58d8b
JS
869 if (data->m_isDir)
870 {
871 m_treeCtrl->Expand(lastId);
872 }
873 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
874 {
875 // Find the first file in this directory
876 long cookie;
877 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
878 bool selectedChild = FALSE;
53ccf1c0 879 while (childId.IsOk())
51a58d8b 880 {
748fcded 881 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 882
8dd8f875 883 if (data && data->m_path != wxT("") && !data->m_isDir)
51a58d8b
JS
884 {
885 m_treeCtrl->SelectItem(childId);
886 m_treeCtrl->EnsureVisible(childId);
887 selectedChild = TRUE;
888 break;
889 }
890 childId = m_treeCtrl->GetNextChild(lastId, cookie);
891 }
892 if (!selectedChild)
893 {
894 m_treeCtrl->SelectItem(lastId);
895 m_treeCtrl->EnsureVisible(lastId);
896 }
897 }
898 else
899 {
900 m_treeCtrl->SelectItem(lastId);
901 m_treeCtrl->EnsureVisible(lastId);
902 }
903
904 return TRUE;
905 }
906 else
907 return FALSE;
908}
909
910wxString wxGenericDirCtrl::GetPath() const
911{
912 wxTreeItemId id = m_treeCtrl->GetSelection();
913 if (id)
914 {
748fcded 915 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
916 return data->m_path;
917 }
918 else
919 return wxEmptyString;
920}
921
922wxString wxGenericDirCtrl::GetFilePath() const
923{
924 wxTreeItemId id = m_treeCtrl->GetSelection();
925 if (id)
926 {
748fcded 927 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
928 if (data->m_isDir)
929 return wxEmptyString;
930 else
931 return data->m_path;
932 }
933 else
934 return wxEmptyString;
935}
936
937void wxGenericDirCtrl::SetPath(const wxString& path)
938{
939 m_defaultPath = path;
940 if (m_rootId)
941 ExpandPath(path);
942}
943
944// Not used
945#if 0
946void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
947{
748fcded 948 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
51a58d8b
JS
949
950 // This may take a longish time. Go to busy cursor
951 wxBusyCursor busy;
952
953 wxASSERT(data);
954
955 wxString search,path,filename;
956
957 wxString dirName(data->m_path);
958
ec1b28a3 959#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
960 if (dirName.Last() == ':')
961 dirName += wxString(wxFILE_SEP_PATH);
962#endif
963
964 wxDir d;
965 wxString eachFilename;
966
f9c165b1 967 wxLogNull log;
51a58d8b
JS
968 d.Open(dirName);
969
970 if (d.IsOpened())
971 {
972 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
973 {
974 do
975 {
976 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
977 {
978 filenames.Add(eachFilename);
979 }
980 }
981 while (d.GetNext(& eachFilename)) ;
982 }
983 }
984}
985#endif
986
987void wxGenericDirCtrl::SetFilterIndex(int n)
988{
989 m_currentFilter = n;
990
991 wxString f, d;
992 if (ExtractWildcard(m_filter, n, f, d))
993 m_currentFilterStr = f;
994 else
995 m_currentFilterStr = wxT("*.*");
996}
997
998void wxGenericDirCtrl::SetFilter(const wxString& filter)
999{
1000 m_filter = filter;
1001
1002 wxString f, d;
1003 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1004 m_currentFilterStr = f;
1005 else
1006 m_currentFilterStr = wxT("*.*");
1007}
1008
1009// Extract description and actual filter from overall filter string
1010bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1011{
1012 wxArrayString filters, descriptions;
1013 int count = ParseFilter(filterStr, filters, descriptions);
1014 if (count > 0 && n < count)
1015 {
1016 filter = filters[n];
1017 description = descriptions[n];
1018 return TRUE;
1019 }
5716a1ab
VZ
1020
1021 return FALSE;
51a58d8b
JS
1022}
1023
1024// Parses the global filter, returning the number of filters.
1025// Returns 0 if none or if there's a problem.
1026// filterStr is in the form:
1027//
1028// "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1029
1030int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1031{
1032 wxString str(filterStr);
1033
1034 wxString description, filter;
1035 int pos;
1036 bool finished = FALSE;
1037 do
1038 {
1039 pos = str.Find(wxT('|'));
1040 if (pos == -1)
1041 return 0; // Problem
1042 description = str.Left(pos);
1043 str = str.Mid(pos+1);
1044 pos = str.Find(wxT('|'));
1045 if (pos == -1)
1046 {
1047 filter = str;
1048 finished = TRUE;
1049 }
1050 else
1051 {
1052 filter = str.Left(pos);
1053 str = str.Mid(pos+1);
1054 }
1055 descriptions.Add(description);
1056 filters.Add(filter);
1057 }
1058 while (!finished) ;
1059
1060 return filters.Count();
1061}
1062
1063void wxGenericDirCtrl::DoResize()
1064{
1065 wxSize sz = GetClientSize();
1066 int verticalSpacing = 3;
1067 if (m_treeCtrl)
1068 {
1069 wxSize filterSz ;
1070 if (m_filterListCtrl)
1071 {
232f35cb
JS
1072#ifdef __WXMSW__
1073 // For some reason, this is required in order for the
1074 // correct control height to always be returned, rather
1075 // than the drop-down list height which is sometimes returned.
1076 wxSize oldSize = m_filterListCtrl->GetSize();
1077 m_filterListCtrl->SetSize(-1, -1, oldSize.x+10, -1, wxSIZE_USE_EXISTING);
1078 m_filterListCtrl->SetSize(-1, -1, oldSize.x, -1, wxSIZE_USE_EXISTING);
1079#endif
51a58d8b
JS
1080 filterSz = m_filterListCtrl->GetSize();
1081 sz.y -= (filterSz.y + verticalSpacing);
1082 }
1083 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1084 if (m_filterListCtrl)
1085 {
1086 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1087 // Don't know why, but this needs refreshing after a resize (wxMSW)
1088 m_filterListCtrl->Refresh();
1089 }
1090 }
1091}
1092
1093
33ac7e6f 1094void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
51a58d8b
JS
1095{
1096 DoResize();
1097}
1098
1099//-----------------------------------------------------------------------------
1100// wxDirFilterListCtrl
1101//-----------------------------------------------------------------------------
1102
1103IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1104
1105BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1106 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter)
1107END_EVENT_TABLE()
1108
1109bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1110 const wxPoint& pos,
1111 const wxSize& size,
1112 long style)
1113{
1114 m_dirCtrl = parent;
1115 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1116}
1117
1118void wxDirFilterListCtrl::Init()
1119{
1120 m_dirCtrl = NULL;
1121}
1122
33ac7e6f 1123void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
51a58d8b
JS
1124{
1125 int sel = GetSelection();
1126
1127 wxString currentPath = m_dirCtrl->GetPath();
ec1b28a3 1128
51a58d8b
JS
1129 m_dirCtrl->SetFilterIndex(sel);
1130
1131 // If the filter has changed, the view is out of date, so
1132 // collapse the tree.
08887820 1133 m_dirCtrl->ReCreateTree();
51a58d8b
JS
1134
1135 // Try to restore the selection, or at least the directory
1136 m_dirCtrl->ExpandPath(currentPath);
1137}
1138
1139void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1140{
1141 Clear();
1142 wxArrayString descriptions, filters;
1143 size_t n = (size_t) m_dirCtrl->ParseFilter(filter, filters, descriptions);
1144
1145 if (n > 0 && defaultFilter < (int) n)
1146 {
1147 size_t i = 0;
1148 for (i = 0; i < n; i++)
1149 Append(descriptions[i]);
1150 SetSelection(defaultFilter);
1151 }
1152}
1153
06cc1fb9
JS
1154// ----------------------------------------------------------------------------
1155// wxFileIconsTable icons
1156// ----------------------------------------------------------------------------
1157
1158/* Open folder */
1159static const char * file_icons_tbl_folder_open_xpm[] = {
1160/* width height ncolors chars_per_pixel */
1161"16 16 6 1",
1162/* colors */
1163" s None c None",
1164". c #000000",
1165"+ c #c0c0c0",
1166"@ c #808080",
1167"# c #ffff00",
1168"$ c #ffffff",
1169/* pixels */
1170" ",
1171" @@@@@ ",
1172" @$$$$$@ ",
1173" @$#+#+#$@@@@@@ ",
1174" @$+#+#+$$$$$$@.",
1175" @$#+#+#+#+#+#@.",
1176"@@@@@@@@@@@@@#@.",
1177"@$$$$$$$$$$@@+@.",
1178"@$#+#+#+#+##.@@.",
1179" @$#+#+#+#+#+.@.",
1180" @$+#+#+#+#+#.@.",
1181" @$+#+#+#+##@..",
1182" @@@@@@@@@@@@@.",
1183" .............",
1184" ",
1185" "};
1186
1187/* Computer */
1188static const char * file_icons_tbl_computer_xpm[] = {
1189"16 16 7 1",
1190" s None c None",
1191". c #808080",
1192"X c #c0c0c0",
1193"o c Black",
1194"O c Gray100",
1195"+ c #008080",
1196"@ c Blue",
1197" ........... ",
1198" .XXXXXXXXXX.o",
1199" .OOOOOOOOO..o",
1200" .OoooooooX..o",
1201" .Oo+...@+X..o",
1202" .Oo+XXX.+X..o",
1203" .Oo+....+X..o",
1204" .Oo++++++X..o",
1205" .OXXXXXXXX.oo",
1206" ..........o.o",
1207" ...........Xo",
1208" .XXXXXXXXXX.o",
1209" .o.o.o.o.o...o",
1210" .oXoXoXoXoXo.o ",
1211".XOXXXXXXXXX.o ",
1212"............o "};
1213
1214/* Drive */
1215static const char * file_icons_tbl_drive_xpm[] = {
1216"16 16 7 1",
1217" s None c None",
1218". c #808080",
1219"X c #c0c0c0",
1220"o c Black",
1221"O c Gray100",
1222"+ c Green",
1223"@ c #008000",
1224" ",
1225" ",
1226" ",
1227" ",
1228" ............. ",
1229" .XXXXXXXXXXXX.o",
1230".OOOOOOOOOOOO..o",
1231".XXXXXXXXX+@X..o",
1232".XXXXXXXXXXXX..o",
1233".X..........X..o",
1234".XOOOOOOOOOOX..o",
1235"..............o ",
1236" ooooooooooooo ",
1237" ",
1238" ",
1239" "};
1240
1241/* CD-ROM */
1242static const char *file_icons_tbl_cdrom_xpm[] = {
1243"16 16 10 1",
1244" s None c None",
1245". c #808080",
1246"X c #c0c0c0",
1247"o c Yellow",
1248"O c Blue",
1249"+ c Black",
1250"@ c Gray100",
1251"# c #008080",
1252"$ c Green",
1253"% c #008000",
1254" ... ",
1255" ..XoX.. ",
1256" .O.XoXXX+ ",
1257" ...O.oXXXX+ ",
1258" .O..X.XXXX+ ",
1259" ....X.+..XXX+",
1260" .XXX.+@+.XXX+",
1261" .X@XX.+.X@@X+",
1262" .....X...#XX@+ ",
1263".@@@...XXo.O@X+ ",
1264".@XXX..XXoXOO+ ",
1265".@++++..XoX+++ ",
1266".@$%@@XX+++X.+ ",
1267".............+ ",
1268" ++++++++++++ ",
1269" "};
1270
1271/* Floppy */
1272static const char * file_icons_tbl_floppy_xpm[] = {
1273"16 16 7 1",
1274" s None c None",
1275". c #808080",
1276"X c Gray100",
1277"o c #c0c0c0",
1278"O c Black",
1279"+ c Cyan",
1280"@ c Red",
1281" ......X",
1282" .ooooooO",
1283" .+++++OO",
1284" .++++++O",
1285" .++++++O",
1286" .ooooooO",
1287" .......o....oO",
1288" .oooooo.o.O.XoO",
1289".XXXXXXXXOOOOOO ",
1290".ooooooooo@o..O ",
1291".ooo....oooo..O ",
1292".o..OOOO...o..O ",
1293".oooXXXXoooo..O ",
1294".............O ",
1295" OOOOOOOOOOOO ",
1296" "};
1297
1298/* Removeable */
1299static const char * file_icons_tbl_removeable_xpm[] = {
1300"16 16 7 1",
1301" s None c None",
1302". c #808080",
1303"X c #c0c0c0",
1304"o c Black",
1305"O c Gray100",
1306"+ c Red",
1307"@ c #800000",
1308" ",
1309" ",
1310" ",
1311" ............. ",
1312" .XXXXXXXXXXXX.o",
1313".OOOOOOOOOOOO..o",
1314".OXXXXXXXXXXX..o",
1315".O+@.oooooo.X..o",
1316".OXXOooooooOX..o",
1317".OXXXOOOOOOXX..o",
1318".OXXXXXXXXXXX..o",
1319".O............o ",
1320" ooooooooooooo ",
1321" ",
1322" ",
1323" "};
1324
1325// ----------------------------------------------------------------------------
1326// wxFileIconsTable & friends
1327// ----------------------------------------------------------------------------
1328
1329// global instance of a wxFileIconsTable
1330wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1331
1332// A module to allow icons table cleanup
1333
1334class wxFileIconsTableModule: public wxModule
1335{
1336DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1337public:
1338 wxFileIconsTableModule() {}
1339 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return TRUE; }
1340 void OnExit()
1341 {
1342 if (wxTheFileIconsTable)
1343 {
1344 delete wxTheFileIconsTable;
1345 wxTheFileIconsTable = NULL;
1346 }
1347 }
1348};
1349
1350IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1351
1352class wxFileIconEntry : public wxObject
1353{
1354public:
1355 wxFileIconEntry(int i) { id = i; }
1356
1357 int id;
1358};
1359
1360wxFileIconsTable::wxFileIconsTable()
1361{
1362 m_HashTable = NULL;
1363 m_smallImageList = NULL;
1364}
1365
1366wxFileIconsTable::~wxFileIconsTable()
1367{
1368 if (m_HashTable) delete m_HashTable;
1369 if (m_smallImageList) delete m_smallImageList;
1370}
1371
1372// delayed initialization - wait until first use (wxArtProv not created yet)
1373void wxFileIconsTable::Create()
1374{
1375 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
1376printf("creating \n"); fflush(stdout);
1377 m_HashTable = new wxHashTable(wxKEY_STRING);
1378 m_smallImageList = new wxImageList(16, 16);
1379
1380 m_HashTable->DeleteContents(TRUE);
1381 // folder:
1382 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER, wxART_CMN_DIALOG));
1383 // folder_open
1384 m_smallImageList->Add(wxIcon(file_icons_tbl_folder_open_xpm));
1385 // computer
1386 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
1387 // drive
1388 m_smallImageList->Add(wxIcon(file_icons_tbl_drive_xpm));
1389 // cdrom
1390 m_smallImageList->Add(wxIcon(file_icons_tbl_cdrom_xpm));
1391 // floppy
1392 m_smallImageList->Add(wxIcon(file_icons_tbl_floppy_xpm));
1393 // removeable
1394 m_smallImageList->Add(wxIcon(file_icons_tbl_removeable_xpm));
1395 // file
1396 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_CMN_DIALOG));
1397 // executable
1398 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1399 {
1400 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE, wxART_CMN_DIALOG));
1401 m_HashTable->Delete(_T("exe"));
1402 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1403 }
1404 /* else put into list by GetIconID
1405 (KDE defines application/x-executable for *.exe and has nice icon)
1406 */
1407}
1408
1409wxImageList *wxFileIconsTable::GetSmallImageList()
1410{
1411 if (!m_smallImageList)
1412 Create();
1413
1414 return m_smallImageList;
1415}
1416
1417#if wxUSE_MIMETYPE
1418// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1419// one icon and we won't resize it
1420
1421static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1422{
1423 wxImage smallimg (16, 16);
1424 unsigned char *p1, *p2, *ps;
1425 unsigned char mr = img.GetMaskRed(),
1426 mg = img.GetMaskGreen(),
1427 mb = img.GetMaskBlue();
1428
1429 unsigned x, y;
1430 unsigned sr, sg, sb, smask;
1431
1432 p1 = img.GetData(), p2 = img.GetData() + 3 * 32, ps = smallimg.GetData();
1433 smallimg.SetMaskColour(mr, mr, mr);
1434
1435 for (y = 0; y < 16; y++)
1436 {
1437 for (x = 0; x < 16; x++)
1438 {
1439 sr = sg = sb = smask = 0;
1440 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1441 sr += p1[0], sg += p1[1], sb += p1[2];
1442 else smask++;
1443 p1 += 3;
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 (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1449 sr += p2[0], sg += p2[1], sb += p2[2];
1450 else smask++;
1451 p2 += 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
1457 if (smask > 2)
1458 ps[0] = ps[1] = ps[2] = mr;
1459 else
1460 ps[0] = sr >> 2, ps[1] = sg >> 2, ps[2] = sb >> 2;
1461 ps += 3;
1462 }
1463 p1 += 32 * 3, p2 += 32 * 3;
1464 }
1465
1466 return wxBitmap(smallimg);
1467}
1468
1469// finds empty borders and return non-empty area of image:
1470static wxImage CutEmptyBorders(const wxImage& img)
1471{
1472 unsigned char mr = img.GetMaskRed(),
1473 mg = img.GetMaskGreen(),
1474 mb = img.GetMaskBlue();
1475 unsigned char *dt = img.GetData(), *dttmp;
1476 unsigned w = img.GetWidth(), h = img.GetHeight();
1477
1478 unsigned top, bottom, left, right, i;
1479 bool empt;
1480
1481#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1482#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
1483
1484 for (empt = TRUE, top = 0; empt && top < h; top++)
1485 {
1486 MK_DTTMP(0, top);
1487 for (i = 0; i < w; i++, dttmp+=3)
1488 NOEMPTY_PIX(empt)
1489 }
1490 for (empt = TRUE, bottom = h-1; empt && bottom > top; bottom--)
1491 {
1492 MK_DTTMP(0, bottom);
1493 for (i = 0; i < w; i++, dttmp+=3)
1494 NOEMPTY_PIX(empt)
1495 }
1496 for (empt = TRUE, left = 0; empt && left < w; left++)
1497 {
1498 MK_DTTMP(left, 0);
1499 for (i = 0; i < h; i++, dttmp+=3*w)
1500 NOEMPTY_PIX(empt)
1501 }
1502 for (empt = TRUE, right = w-1; empt && right > left; right--)
1503 {
1504 MK_DTTMP(right, 0);
1505 for (i = 0; i < h; i++, dttmp+=3*w)
1506 NOEMPTY_PIX(empt)
1507 }
1508 top--, left--, bottom++, right++;
1509
1510 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1511}
1512#endif // wxUSE_MIMETYPE
1513
1514int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1515{
1516 if (!m_smallImageList)
1517 Create();
1518
1519#if wxUSE_MIMETYPE
1520 if (!extension.IsEmpty())
1521 {
1522 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1523 if (entry) return (entry -> id);
1524 }
1525
1526 wxFileType *ft = (mime.IsEmpty()) ?
1527 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1528 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
1529 wxIcon ic;
1530 if (ft == NULL || (!ft->GetIcon(&ic)) || (!ic.Ok()))
1531 {
1532 int newid = file;
1533 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1534 return newid;
1535 }
1536
1537 wxBitmap tmpBmp;
1538 tmpBmp.CopyFromIcon(ic);
1539 wxImage img = tmpBmp.ConvertToImage();
1540
1541 delete ft;
1542
1543 int id = m_smallImageList->GetImageCount();
1544 if (img.GetWidth() == 16 && img.GetHeight() == 16)
1545 m_smallImageList->Add(wxBitmap(img));
1546 else
1547 {
1548 if (img.GetWidth() != 32 || img.GetHeight() != 32)
1549 m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(32, 32)));
1550 else
1551 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1552 }
1553 m_HashTable->Put(extension, new wxFileIconEntry(id));
1554 return id;
1555
1556#else // !wxUSE_MIMETYPE
1557
1558 if (extension == wxT("exe"))
1559 return executable;
1560 else
1561 return file;
1562#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1563}
1564
1e6feb95 1565#endif // wxUSE_DIRDLG