]> git.saurik.com Git - wxWidgets.git/blame - src/generic/dirctrlg.cpp
removed carbon printing code
[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;
27c73976
JS
462 else
463 treeStyle |= wxBORDER_SUNKEN;
51a58d8b
JS
464
465 long filterStyle = 0;
466 if ((style & wxDIRCTRL_3D_INTERNAL) == 0)
467 filterStyle |= wxNO_BORDER;
27c73976
JS
468 else
469 filterStyle |= wxBORDER_SUNKEN;
51a58d8b
JS
470
471 m_treeCtrl = new wxTreeCtrl(this, wxID_TREECTRL, pos, size, treeStyle);
472
473 if (!filter.IsEmpty() && (style & wxDIRCTRL_SHOW_FILTERS))
474 m_filterListCtrl = new wxDirFilterListCtrl(this, wxID_FILTERLISTCTRL, wxDefaultPosition, wxDefaultSize, filterStyle);
475
476 m_defaultPath = dir;
477 m_filter = filter;
478
479 SetFilterIndex(defaultFilter);
480
481 if (m_filterListCtrl)
482 m_filterListCtrl->FillFilterList(filter, defaultFilter);
483
06cc1fb9 484 m_treeCtrl->SetImageList(wxTheFileIconsTable->GetSmallImageList());
51a58d8b
JS
485
486 m_showHidden = FALSE;
748fcded 487 wxDirItemData* rootData = new wxDirItemData(wxT(""), wxT(""), TRUE);
51a58d8b
JS
488
489 wxString rootName;
490
db5333a5 491#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__DOS__)
51a58d8b
JS
492 rootName = _("Computer");
493#else
494 rootName = _("Sections");
495#endif
496
497 m_rootId = m_treeCtrl->AddRoot( rootName, 3, -1, rootData);
498 m_treeCtrl->SetItemHasChildren(m_rootId);
08887820 499 ExpandDir(m_rootId); // automatically expand first level
51a58d8b
JS
500
501 // Expand and select the default path
502 if (!m_defaultPath.IsEmpty())
503 ExpandPath(m_defaultPath);
504
505 DoResize();
506
507 return TRUE;
508}
509
510wxGenericDirCtrl::~wxGenericDirCtrl()
511{
51a58d8b
JS
512}
513
514void wxGenericDirCtrl::Init()
515{
516 m_showHidden = FALSE;
51a58d8b
JS
517 m_currentFilter = 0;
518 m_currentFilterStr = wxEmptyString; // Default: any file
519 m_treeCtrl = NULL;
520 m_filterListCtrl = NULL;
521}
522
42dcacf0
RR
523void wxGenericDirCtrl::ShowHidden( bool show )
524{
525 m_showHidden = show;
574c939e 526
3b423cdd 527 wxString path = GetPath();
08887820 528 ReCreateTree();
3b423cdd 529 SetPath(path);
42dcacf0
RR
530}
531
51a58d8b
JS
532void wxGenericDirCtrl::AddSection(const wxString& path, const wxString& name, int imageId)
533{
748fcded 534 wxDirItemData *dir_item = new wxDirItemData(path,name,TRUE);
51a58d8b 535
51a58d8b 536 wxTreeItemId id = m_treeCtrl->AppendItem( m_rootId, name, imageId, -1, dir_item);
4f5c180e 537
06cc1fb9
JS
538 m_treeCtrl->SetItemHasChildren(id);
539}
51a58d8b 540
06cc1fb9
JS
541void wxGenericDirCtrl::SetupSections()
542{
543 wxArrayString paths, names;
544 wxArrayInt icons;
2d4e4f80 545
06cc1fb9 546 size_t n, count = wxGetAvailableDrives(paths, names, icons);
4f5c180e 547
06cc1fb9
JS
548 for (n = 0; n < count; n++)
549 {
550 AddSection(paths[n], names[n], icons[n]);
bedaf53e 551 }
51a58d8b
JS
552}
553
554void wxGenericDirCtrl::OnBeginEditItem(wxTreeEvent &event)
555{
556 // don't rename the main entry "Sections"
557 if (event.GetItem() == m_rootId)
558 {
559 event.Veto();
560 return;
561 }
562
563 // don't rename the individual sections
99006e44 564 if (m_treeCtrl->GetItemParent( event.GetItem() ) == m_rootId)
51a58d8b
JS
565 {
566 event.Veto();
567 return;
568 }
569}
570
571void wxGenericDirCtrl::OnEndEditItem(wxTreeEvent &event)
572{
573 if ((event.GetLabel().IsEmpty()) ||
574 (event.GetLabel() == _(".")) ||
575 (event.GetLabel() == _("..")) ||
06cc1fb9
JS
576 (event.GetLabel().Find(wxT('/')) != wxNOT_FOUND) ||
577 (event.GetLabel().Find(wxT('\\')) != wxNOT_FOUND) ||
578 (event.GetLabel().Find(wxT('|')) != wxNOT_FOUND))
51a58d8b
JS
579 {
580 wxMessageDialog dialog(this, _("Illegal directory name."), _("Error"), wxOK | wxICON_ERROR );
581 dialog.ShowModal();
582 event.Veto();
583 return;
584 }
585
586 wxTreeItemId id = event.GetItem();
748fcded 587 wxDirItemData *data = (wxDirItemData*)m_treeCtrl->GetItemData( id );
51a58d8b
JS
588 wxASSERT( data );
589
590 wxString new_name( wxPathOnly( data->m_path ) );
ad9cd15c 591 new_name += wxString(wxFILE_SEP_PATH);
51a58d8b
JS
592 new_name += event.GetLabel();
593
594 wxLogNull log;
595
596 if (wxFileExists(new_name))
597 {
598 wxMessageDialog dialog(this, _("File name exists already."), _("Error"), wxOK | wxICON_ERROR );
599 dialog.ShowModal();
600 event.Veto();
601 }
602
603 if (wxRenameFile(data->m_path,new_name))
604 {
605 data->SetNewDirName( new_name );
606 }
607 else
608 {
609 wxMessageDialog dialog(this, _("Operation not permitted."), _("Error"), wxOK | wxICON_ERROR );
610 dialog.ShowModal();
611 event.Veto();
612 }
613}
614
615void wxGenericDirCtrl::OnExpandItem(wxTreeEvent &event)
616{
617 wxTreeItemId parentId = event.GetItem();
618
748fcded
VS
619 // VS: this is needed because the event handler is called from wxTreeCtrl
620 // ctor when wxTR_HIDE_ROOT was specified
4ded51f2
CE
621
622 if (!m_rootId.IsOk())
623
748fcded
VS
624 m_rootId = m_treeCtrl->GetRootItem();
625
51a58d8b
JS
626 ExpandDir(parentId);
627}
628
629void wxGenericDirCtrl::OnCollapseItem(wxTreeEvent &event )
630{
08887820
VS
631 CollapseDir(event.GetItem());
632}
633
634void wxGenericDirCtrl::CollapseDir(wxTreeItemId parentId)
635{
636 wxTreeItemId child;
51a58d8b 637
08887820 638 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
639 if (!data->m_isExpanded)
640 return;
641
642 data->m_isExpanded = FALSE;
643 long cookie;
644 /* Workaround because DeleteChildren has disapeared (why?) and
645 * CollapseAndReset doesn't work as advertised (deletes parent too) */
08887820 646 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
647 while (child.IsOk())
648 {
649 m_treeCtrl->Delete(child);
650 /* Not GetNextChild below, because the cookie mechanism can't
651 * handle disappearing children! */
08887820 652 child = m_treeCtrl->GetFirstChild(parentId, cookie);
51a58d8b
JS
653 }
654}
655
656void wxGenericDirCtrl::ExpandDir(wxTreeItemId parentId)
657{
748fcded 658 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(parentId);
51a58d8b
JS
659
660 if (data->m_isExpanded)
661 return;
662
663 data->m_isExpanded = TRUE;
664
748fcded 665 if (parentId == m_treeCtrl->GetRootItem())
51a58d8b
JS
666 {
667 SetupSections();
668 return;
669 }
670
671 wxASSERT(data);
672
673 wxString search,path,filename;
674
675 wxString dirName(data->m_path);
676
db5333a5 677#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
678 // Check if this is a root directory and if so,
679 // whether the drive is avaiable.
33fed835
MB
680 if (!wxIsDriveAvailable(dirName))
681 {
ad9cd15c 682 data->m_isExpanded = FALSE;
7328394a
JS
683 //wxMessageBox(wxT("Sorry, this drive is not available."));
684 return;
33fed835 685 }
51a58d8b
JS
686#endif
687
688 // This may take a longish time. Go to busy cursor
689 wxBusyCursor busy;
690
db5333a5 691#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
692 if (dirName.Last() == ':')
693 dirName += wxString(wxFILE_SEP_PATH);
694#endif
695
696 wxArrayString dirs;
697 wxArrayString filenames;
698
699 wxDir d;
700 wxString eachFilename;
701
f9c165b1 702 wxLogNull log;
51a58d8b
JS
703 d.Open(dirName);
704
705 if (d.IsOpened())
706 {
42dcacf0
RR
707 int style = wxDIR_DIRS;
708 if (m_showHidden) style |= wxDIR_HIDDEN;
709 if (d.GetFirst(& eachFilename, wxEmptyString, style))
51a58d8b
JS
710 {
711 do
712 {
713 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
714 {
715 dirs.Add(eachFilename);
716 }
717 }
748fcded 718 while (d.GetNext(& eachFilename));
51a58d8b
JS
719 }
720 }
57e26a09 721 dirs.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
51a58d8b
JS
722
723 // Now do the filenames -- but only if we're allowed to
724 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
725 {
f9c165b1
JS
726 wxLogNull log;
727
51a58d8b 728 d.Open(dirName);
ec1b28a3 729
51a58d8b
JS
730 if (d.IsOpened())
731 {
732 if (d.GetFirst(& eachFilename, m_currentFilterStr, wxDIR_FILES))
733 {
734 do
735 {
736 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
737 {
738 filenames.Add(eachFilename);
739 }
740 }
748fcded 741 while (d.GetNext(& eachFilename));
51a58d8b
JS
742 }
743 }
57e26a09 744 filenames.Sort((wxArrayString::CompareFunction) wxDirCtrlStringCompareFunction);
51a58d8b
JS
745 }
746
747 // Add the sorted dirs
748 size_t i;
749 for (i = 0; i < dirs.Count(); i++)
750 {
751 wxString eachFilename(dirs[i]);
752 path = dirName;
083f7497 753 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
754 path += wxString(wxFILE_SEP_PATH);
755 path += eachFilename;
756
748fcded 757 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,TRUE);
06cc1fb9
JS
758 wxTreeItemId id = m_treeCtrl->AppendItem( parentId, eachFilename,
759 wxFileIconsTable::folder, -1, dir_item);
760 m_treeCtrl->SetItemImage( id, wxFileIconsTable::folder_open,
761 wxTreeItemIcon_Expanded );
ec1b28a3 762
51a58d8b 763 // Has this got any children? If so, make it expandable.
748fcded
VS
764 // (There are two situations when a dir has children: either it
765 // has subdirectories or it contains files that weren't filtered
766 // out. The latter only applies to dirctrl with files.)
767 if ( dir_item->HasSubDirs() ||
768 (((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0) &&
769 dir_item->HasFiles(m_currentFilterStr)) )
51a58d8b 770 {
748fcded 771 m_treeCtrl->SetItemHasChildren(id);
51a58d8b 772 }
51a58d8b
JS
773 }
774
775 // Add the sorted filenames
776 if ((GetWindowStyle() & wxDIRCTRL_DIR_ONLY) == 0)
777 {
778 for (i = 0; i < filenames.Count(); i++)
779 {
780 wxString eachFilename(filenames[i]);
781 path = dirName;
083f7497 782 if (!wxEndsWithPathSeparator(path))
51a58d8b
JS
783 path += wxString(wxFILE_SEP_PATH);
784 path += eachFilename;
785 //path = dirName + wxString(wxT("/")) + eachFilename;
748fcded 786 wxDirItemData *dir_item = new wxDirItemData(path,eachFilename,FALSE);
06cc1fb9
JS
787 int image_id = wxFileIconsTable::file;
788 if (eachFilename.Find(wxT('.')) != wxNOT_FOUND)
789 image_id = wxTheFileIconsTable->GetIconID(eachFilename.AfterLast(wxT('.')));
790 (void)m_treeCtrl->AppendItem( parentId, eachFilename, image_id, -1, dir_item);
51a58d8b
JS
791 }
792 }
793}
794
08887820
VS
795void wxGenericDirCtrl::ReCreateTree()
796{
797 CollapseDir(m_treeCtrl->GetRootItem());
798 ExpandDir(m_treeCtrl->GetRootItem());
799}
800
51a58d8b
JS
801// Find the child that matches the first part of 'path'.
802// E.g. if a child path is "/usr" and 'path' is "/usr/include"
803// then the child for /usr is returned.
804wxTreeItemId wxGenericDirCtrl::FindChild(wxTreeItemId parentId, const wxString& path, bool& done)
805{
806 wxString path2(path);
ec1b28a3 807
51a58d8b
JS
808 // Make sure all separators are as per the current platform
809 path2.Replace(wxT("\\"), wxString(wxFILE_SEP_PATH));
810 path2.Replace(wxT("/"), wxString(wxFILE_SEP_PATH));
ec1b28a3 811
51a58d8b
JS
812 // Append a separator to foil bogus substring matching
813 path2 += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
814
815 // In MSW or PM, case is not significant
db5333a5 816#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
817 path2.MakeLower();
818#endif
ec1b28a3 819
51a58d8b
JS
820 long cookie;
821 wxTreeItemId childId = m_treeCtrl->GetFirstChild(parentId, cookie);
53ccf1c0 822 while (childId.IsOk())
51a58d8b 823 {
748fcded 824 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 825
748fcded 826 if (data && !data->m_path.IsEmpty())
51a58d8b
JS
827 {
828 wxString childPath(data->m_path);
083f7497 829 if (!wxEndsWithPathSeparator(childPath))
51a58d8b 830 childPath += wxString(wxFILE_SEP_PATH);
ec1b28a3
DW
831
832 // In MSW and PM, case is not significant
db5333a5 833#if defined(__WINDOWS__) || defined(__DOS__) || defined(__WXPM__)
51a58d8b
JS
834 childPath.MakeLower();
835#endif
ec1b28a3 836
51a58d8b
JS
837 if (childPath.Len() <= path2.Len())
838 {
839 wxString path3 = path2.Mid(0, childPath.Len());
840 if (childPath == path3)
841 {
842 if (path3.Len() == path2.Len())
843 done = TRUE;
844 else
845 done = FALSE;
846 return childId;
847 }
848 }
849 }
ec1b28a3 850
748fcded 851 childId = m_treeCtrl->GetNextChild(parentId, cookie);
51a58d8b 852 }
3fa4bd0e
VS
853 wxTreeItemId invalid;
854 return invalid;
51a58d8b
JS
855}
856
857// Try to expand as much of the given path as possible,
858// and select the given tree item.
859bool wxGenericDirCtrl::ExpandPath(const wxString& path)
860{
861 bool done = FALSE;
862 wxTreeItemId id = FindChild(m_rootId, path, done);
863 wxTreeItemId lastId = id; // The last non-zero id
237387ad 864 while (id.IsOk() && !done)
51a58d8b
JS
865 {
866 ExpandDir(id);
867
868 id = FindChild(id, path, done);
53ccf1c0 869 if (id.IsOk())
51a58d8b
JS
870 lastId = id;
871 }
53ccf1c0 872 if (lastId.IsOk())
51a58d8b 873 {
748fcded 874 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(lastId);
51a58d8b
JS
875 if (data->m_isDir)
876 {
877 m_treeCtrl->Expand(lastId);
878 }
879 if ((GetWindowStyle() & wxDIRCTRL_SELECT_FIRST) && data->m_isDir)
880 {
881 // Find the first file in this directory
882 long cookie;
883 wxTreeItemId childId = m_treeCtrl->GetFirstChild(lastId, cookie);
884 bool selectedChild = FALSE;
53ccf1c0 885 while (childId.IsOk())
51a58d8b 886 {
748fcded 887 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(childId);
ec1b28a3 888
8dd8f875 889 if (data && data->m_path != wxT("") && !data->m_isDir)
51a58d8b
JS
890 {
891 m_treeCtrl->SelectItem(childId);
892 m_treeCtrl->EnsureVisible(childId);
893 selectedChild = TRUE;
894 break;
895 }
896 childId = m_treeCtrl->GetNextChild(lastId, cookie);
897 }
898 if (!selectedChild)
899 {
900 m_treeCtrl->SelectItem(lastId);
901 m_treeCtrl->EnsureVisible(lastId);
902 }
903 }
904 else
905 {
906 m_treeCtrl->SelectItem(lastId);
907 m_treeCtrl->EnsureVisible(lastId);
908 }
909
910 return TRUE;
911 }
912 else
913 return FALSE;
914}
915
916wxString wxGenericDirCtrl::GetPath() const
917{
918 wxTreeItemId id = m_treeCtrl->GetSelection();
919 if (id)
920 {
748fcded 921 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
922 return data->m_path;
923 }
924 else
925 return wxEmptyString;
926}
927
928wxString wxGenericDirCtrl::GetFilePath() const
929{
930 wxTreeItemId id = m_treeCtrl->GetSelection();
931 if (id)
932 {
748fcded 933 wxDirItemData* data = (wxDirItemData*) m_treeCtrl->GetItemData(id);
51a58d8b
JS
934 if (data->m_isDir)
935 return wxEmptyString;
936 else
937 return data->m_path;
938 }
939 else
940 return wxEmptyString;
941}
942
943void wxGenericDirCtrl::SetPath(const wxString& path)
944{
945 m_defaultPath = path;
946 if (m_rootId)
947 ExpandPath(path);
948}
949
950// Not used
951#if 0
952void wxGenericDirCtrl::FindChildFiles(wxTreeItemId id, int dirFlags, wxArrayString& filenames)
953{
748fcded 954 wxDirItemData *data = (wxDirItemData *) m_treeCtrl->GetItemData(id);
51a58d8b
JS
955
956 // This may take a longish time. Go to busy cursor
957 wxBusyCursor busy;
958
959 wxASSERT(data);
960
961 wxString search,path,filename;
962
963 wxString dirName(data->m_path);
964
ec1b28a3 965#if defined(__WXMSW__) || defined(__WXPM__)
51a58d8b
JS
966 if (dirName.Last() == ':')
967 dirName += wxString(wxFILE_SEP_PATH);
968#endif
969
970 wxDir d;
971 wxString eachFilename;
972
f9c165b1 973 wxLogNull log;
51a58d8b
JS
974 d.Open(dirName);
975
976 if (d.IsOpened())
977 {
978 if (d.GetFirst(& eachFilename, m_currentFilterStr, dirFlags))
979 {
980 do
981 {
982 if ((eachFilename != wxT(".")) && (eachFilename != wxT("..")))
983 {
984 filenames.Add(eachFilename);
985 }
986 }
987 while (d.GetNext(& eachFilename)) ;
988 }
989 }
990}
991#endif
992
993void wxGenericDirCtrl::SetFilterIndex(int n)
994{
995 m_currentFilter = n;
996
997 wxString f, d;
998 if (ExtractWildcard(m_filter, n, f, d))
999 m_currentFilterStr = f;
1000 else
1001 m_currentFilterStr = wxT("*.*");
1002}
1003
1004void wxGenericDirCtrl::SetFilter(const wxString& filter)
1005{
1006 m_filter = filter;
1007
1008 wxString f, d;
1009 if (ExtractWildcard(m_filter, m_currentFilter, f, d))
1010 m_currentFilterStr = f;
1011 else
1012 m_currentFilterStr = wxT("*.*");
1013}
1014
1015// Extract description and actual filter from overall filter string
1016bool wxGenericDirCtrl::ExtractWildcard(const wxString& filterStr, int n, wxString& filter, wxString& description)
1017{
1018 wxArrayString filters, descriptions;
1019 int count = ParseFilter(filterStr, filters, descriptions);
1020 if (count > 0 && n < count)
1021 {
1022 filter = filters[n];
1023 description = descriptions[n];
1024 return TRUE;
1025 }
5716a1ab
VZ
1026
1027 return FALSE;
51a58d8b
JS
1028}
1029
1030// Parses the global filter, returning the number of filters.
1031// Returns 0 if none or if there's a problem.
1032// filterStr is in the form:
1033//
1034// "All files (*.*)|*.*|JPEG Files (*.jpeg)|*.jpg"
1035
1036int wxGenericDirCtrl::ParseFilter(const wxString& filterStr, wxArrayString& filters, wxArrayString& descriptions)
1037{
1038 wxString str(filterStr);
1039
1040 wxString description, filter;
1041 int pos;
1042 bool finished = FALSE;
1043 do
1044 {
1045 pos = str.Find(wxT('|'));
1046 if (pos == -1)
1047 return 0; // Problem
1048 description = str.Left(pos);
1049 str = str.Mid(pos+1);
1050 pos = str.Find(wxT('|'));
1051 if (pos == -1)
1052 {
1053 filter = str;
1054 finished = TRUE;
1055 }
1056 else
1057 {
1058 filter = str.Left(pos);
1059 str = str.Mid(pos+1);
1060 }
1061 descriptions.Add(description);
1062 filters.Add(filter);
1063 }
1064 while (!finished) ;
1065
1066 return filters.Count();
1067}
1068
1069void wxGenericDirCtrl::DoResize()
1070{
1071 wxSize sz = GetClientSize();
1072 int verticalSpacing = 3;
1073 if (m_treeCtrl)
1074 {
1075 wxSize filterSz ;
1076 if (m_filterListCtrl)
1077 {
232f35cb
JS
1078#ifdef __WXMSW__
1079 // For some reason, this is required in order for the
1080 // correct control height to always be returned, rather
1081 // than the drop-down list height which is sometimes returned.
1082 wxSize oldSize = m_filterListCtrl->GetSize();
1083 m_filterListCtrl->SetSize(-1, -1, oldSize.x+10, -1, wxSIZE_USE_EXISTING);
1084 m_filterListCtrl->SetSize(-1, -1, oldSize.x, -1, wxSIZE_USE_EXISTING);
1085#endif
51a58d8b
JS
1086 filterSz = m_filterListCtrl->GetSize();
1087 sz.y -= (filterSz.y + verticalSpacing);
1088 }
1089 m_treeCtrl->SetSize(0, 0, sz.x, sz.y);
1090 if (m_filterListCtrl)
1091 {
1092 m_filterListCtrl->SetSize(0, sz.y + verticalSpacing, sz.x, filterSz.y);
1093 // Don't know why, but this needs refreshing after a resize (wxMSW)
1094 m_filterListCtrl->Refresh();
1095 }
1096 }
1097}
1098
1099
33ac7e6f 1100void wxGenericDirCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
51a58d8b
JS
1101{
1102 DoResize();
1103}
1104
1105//-----------------------------------------------------------------------------
1106// wxDirFilterListCtrl
1107//-----------------------------------------------------------------------------
1108
1109IMPLEMENT_CLASS(wxDirFilterListCtrl, wxChoice)
1110
1111BEGIN_EVENT_TABLE(wxDirFilterListCtrl, wxChoice)
1112 EVT_CHOICE(-1, wxDirFilterListCtrl::OnSelFilter)
1113END_EVENT_TABLE()
1114
1115bool wxDirFilterListCtrl::Create(wxGenericDirCtrl* parent, const wxWindowID id,
1116 const wxPoint& pos,
1117 const wxSize& size,
1118 long style)
1119{
1120 m_dirCtrl = parent;
1121 return wxChoice::Create(parent, id, pos, size, 0, NULL, style);
1122}
1123
1124void wxDirFilterListCtrl::Init()
1125{
1126 m_dirCtrl = NULL;
1127}
1128
33ac7e6f 1129void wxDirFilterListCtrl::OnSelFilter(wxCommandEvent& WXUNUSED(event))
51a58d8b
JS
1130{
1131 int sel = GetSelection();
1132
1133 wxString currentPath = m_dirCtrl->GetPath();
ec1b28a3 1134
51a58d8b
JS
1135 m_dirCtrl->SetFilterIndex(sel);
1136
1137 // If the filter has changed, the view is out of date, so
1138 // collapse the tree.
08887820 1139 m_dirCtrl->ReCreateTree();
51a58d8b
JS
1140
1141 // Try to restore the selection, or at least the directory
1142 m_dirCtrl->ExpandPath(currentPath);
1143}
1144
1145void wxDirFilterListCtrl::FillFilterList(const wxString& filter, int defaultFilter)
1146{
1147 Clear();
1148 wxArrayString descriptions, filters;
1149 size_t n = (size_t) m_dirCtrl->ParseFilter(filter, filters, descriptions);
1150
1151 if (n > 0 && defaultFilter < (int) n)
1152 {
1153 size_t i = 0;
1154 for (i = 0; i < n; i++)
1155 Append(descriptions[i]);
1156 SetSelection(defaultFilter);
1157 }
1158}
1159
06cc1fb9
JS
1160// ----------------------------------------------------------------------------
1161// wxFileIconsTable icons
1162// ----------------------------------------------------------------------------
1163
1164/* Open folder */
1165static const char * file_icons_tbl_folder_open_xpm[] = {
1166/* width height ncolors chars_per_pixel */
1167"16 16 6 1",
1168/* colors */
1169" s None c None",
1170". c #000000",
1171"+ c #c0c0c0",
1172"@ c #808080",
1173"# c #ffff00",
1174"$ c #ffffff",
1175/* pixels */
1176" ",
1177" @@@@@ ",
1178" @$$$$$@ ",
1179" @$#+#+#$@@@@@@ ",
1180" @$+#+#+$$$$$$@.",
1181" @$#+#+#+#+#+#@.",
1182"@@@@@@@@@@@@@#@.",
1183"@$$$$$$$$$$@@+@.",
1184"@$#+#+#+#+##.@@.",
1185" @$#+#+#+#+#+.@.",
1186" @$+#+#+#+#+#.@.",
1187" @$+#+#+#+##@..",
1188" @@@@@@@@@@@@@.",
1189" .............",
1190" ",
1191" "};
1192
1193/* Computer */
1194static const char * file_icons_tbl_computer_xpm[] = {
1195"16 16 7 1",
1196" s None c None",
1197". c #808080",
1198"X c #c0c0c0",
1199"o c Black",
1200"O c Gray100",
1201"+ c #008080",
1202"@ c Blue",
1203" ........... ",
1204" .XXXXXXXXXX.o",
1205" .OOOOOOOOO..o",
1206" .OoooooooX..o",
1207" .Oo+...@+X..o",
1208" .Oo+XXX.+X..o",
1209" .Oo+....+X..o",
1210" .Oo++++++X..o",
1211" .OXXXXXXXX.oo",
1212" ..........o.o",
1213" ...........Xo",
1214" .XXXXXXXXXX.o",
1215" .o.o.o.o.o...o",
1216" .oXoXoXoXoXo.o ",
1217".XOXXXXXXXXX.o ",
1218"............o "};
1219
1220/* Drive */
1221static const char * file_icons_tbl_drive_xpm[] = {
1222"16 16 7 1",
1223" s None c None",
1224". c #808080",
1225"X c #c0c0c0",
1226"o c Black",
1227"O c Gray100",
1228"+ c Green",
1229"@ c #008000",
1230" ",
1231" ",
1232" ",
1233" ",
1234" ............. ",
1235" .XXXXXXXXXXXX.o",
1236".OOOOOOOOOOOO..o",
1237".XXXXXXXXX+@X..o",
1238".XXXXXXXXXXXX..o",
1239".X..........X..o",
1240".XOOOOOOOOOOX..o",
1241"..............o ",
1242" ooooooooooooo ",
1243" ",
1244" ",
1245" "};
1246
1247/* CD-ROM */
1248static const char *file_icons_tbl_cdrom_xpm[] = {
1249"16 16 10 1",
1250" s None c None",
1251". c #808080",
1252"X c #c0c0c0",
1253"o c Yellow",
1254"O c Blue",
1255"+ c Black",
1256"@ c Gray100",
1257"# c #008080",
1258"$ c Green",
1259"% c #008000",
1260" ... ",
1261" ..XoX.. ",
1262" .O.XoXXX+ ",
1263" ...O.oXXXX+ ",
1264" .O..X.XXXX+ ",
1265" ....X.+..XXX+",
1266" .XXX.+@+.XXX+",
1267" .X@XX.+.X@@X+",
1268" .....X...#XX@+ ",
1269".@@@...XXo.O@X+ ",
1270".@XXX..XXoXOO+ ",
1271".@++++..XoX+++ ",
1272".@$%@@XX+++X.+ ",
1273".............+ ",
1274" ++++++++++++ ",
1275" "};
1276
1277/* Floppy */
1278static const char * file_icons_tbl_floppy_xpm[] = {
1279"16 16 7 1",
1280" s None c None",
1281". c #808080",
1282"X c Gray100",
1283"o c #c0c0c0",
1284"O c Black",
1285"+ c Cyan",
1286"@ c Red",
1287" ......X",
1288" .ooooooO",
1289" .+++++OO",
1290" .++++++O",
1291" .++++++O",
1292" .ooooooO",
1293" .......o....oO",
1294" .oooooo.o.O.XoO",
1295".XXXXXXXXOOOOOO ",
1296".ooooooooo@o..O ",
1297".ooo....oooo..O ",
1298".o..OOOO...o..O ",
1299".oooXXXXoooo..O ",
1300".............O ",
1301" OOOOOOOOOOOO ",
1302" "};
1303
1304/* Removeable */
1305static const char * file_icons_tbl_removeable_xpm[] = {
1306"16 16 7 1",
1307" s None c None",
1308". c #808080",
1309"X c #c0c0c0",
1310"o c Black",
1311"O c Gray100",
1312"+ c Red",
1313"@ c #800000",
1314" ",
1315" ",
1316" ",
1317" ............. ",
1318" .XXXXXXXXXXXX.o",
1319".OOOOOOOOOOOO..o",
1320".OXXXXXXXXXXX..o",
1321".O+@.oooooo.X..o",
1322".OXXOooooooOX..o",
1323".OXXXOOOOOOXX..o",
1324".OXXXXXXXXXXX..o",
1325".O............o ",
1326" ooooooooooooo ",
1327" ",
1328" ",
1329" "};
1330
1331// ----------------------------------------------------------------------------
1332// wxFileIconsTable & friends
1333// ----------------------------------------------------------------------------
1334
1335// global instance of a wxFileIconsTable
1336wxFileIconsTable* wxTheFileIconsTable = (wxFileIconsTable *)NULL;
1337
1338// A module to allow icons table cleanup
1339
1340class wxFileIconsTableModule: public wxModule
1341{
1342DECLARE_DYNAMIC_CLASS(wxFileIconsTableModule)
1343public:
1344 wxFileIconsTableModule() {}
1345 bool OnInit() { wxTheFileIconsTable = new wxFileIconsTable; return TRUE; }
1346 void OnExit()
1347 {
1348 if (wxTheFileIconsTable)
1349 {
1350 delete wxTheFileIconsTable;
1351 wxTheFileIconsTable = NULL;
1352 }
1353 }
1354};
1355
1356IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
1357
1358class wxFileIconEntry : public wxObject
1359{
1360public:
1361 wxFileIconEntry(int i) { id = i; }
1362
1363 int id;
1364};
1365
1366wxFileIconsTable::wxFileIconsTable()
1367{
1368 m_HashTable = NULL;
1369 m_smallImageList = NULL;
1370}
1371
1372wxFileIconsTable::~wxFileIconsTable()
1373{
1374 if (m_HashTable) delete m_HashTable;
1375 if (m_smallImageList) delete m_smallImageList;
1376}
1377
1378// delayed initialization - wait until first use (wxArtProv not created yet)
1379void wxFileIconsTable::Create()
1380{
1381 wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
06cc1fb9
JS
1382 m_HashTable = new wxHashTable(wxKEY_STRING);
1383 m_smallImageList = new wxImageList(16, 16);
1384
1385 m_HashTable->DeleteContents(TRUE);
1386 // folder:
1387 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER, wxART_CMN_DIALOG));
1388 // folder_open
1389 m_smallImageList->Add(wxIcon(file_icons_tbl_folder_open_xpm));
1390 // computer
1391 m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
1392 // drive
1393 m_smallImageList->Add(wxIcon(file_icons_tbl_drive_xpm));
1394 // cdrom
1395 m_smallImageList->Add(wxIcon(file_icons_tbl_cdrom_xpm));
1396 // floppy
1397 m_smallImageList->Add(wxIcon(file_icons_tbl_floppy_xpm));
1398 // removeable
1399 m_smallImageList->Add(wxIcon(file_icons_tbl_removeable_xpm));
1400 // file
1401 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE, wxART_CMN_DIALOG));
1402 // executable
1403 if (GetIconID(wxEmptyString, _T("application/x-executable")) == file)
1404 {
1405 m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE, wxART_CMN_DIALOG));
1406 m_HashTable->Delete(_T("exe"));
1407 m_HashTable->Put(_T("exe"), new wxFileIconEntry(executable));
1408 }
1409 /* else put into list by GetIconID
1410 (KDE defines application/x-executable for *.exe and has nice icon)
1411 */
1412}
1413
1414wxImageList *wxFileIconsTable::GetSmallImageList()
1415{
1416 if (!m_smallImageList)
1417 Create();
1418
1419 return m_smallImageList;
1420}
1421
1422#if wxUSE_MIMETYPE
1423// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
1424// one icon and we won't resize it
1425
1426static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
1427{
1428 wxImage smallimg (16, 16);
1429 unsigned char *p1, *p2, *ps;
1430 unsigned char mr = img.GetMaskRed(),
1431 mg = img.GetMaskGreen(),
1432 mb = img.GetMaskBlue();
1433
1434 unsigned x, y;
1435 unsigned sr, sg, sb, smask;
1436
1437 p1 = img.GetData(), p2 = img.GetData() + 3 * 32, ps = smallimg.GetData();
1438 smallimg.SetMaskColour(mr, mr, mr);
1439
1440 for (y = 0; y < 16; y++)
1441 {
1442 for (x = 0; x < 16; x++)
1443 {
1444 sr = sg = sb = smask = 0;
1445 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1446 sr += p1[0], sg += p1[1], sb += p1[2];
1447 else smask++;
1448 p1 += 3;
1449 if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
1450 sr += p1[0], sg += p1[1], sb += p1[2];
1451 else smask++;
1452 p1 += 3;
1453 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1454 sr += p2[0], sg += p2[1], sb += p2[2];
1455 else smask++;
1456 p2 += 3;
1457 if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
1458 sr += p2[0], sg += p2[1], sb += p2[2];
1459 else smask++;
1460 p2 += 3;
1461
1462 if (smask > 2)
1463 ps[0] = ps[1] = ps[2] = mr;
1464 else
1465 ps[0] = sr >> 2, ps[1] = sg >> 2, ps[2] = sb >> 2;
1466 ps += 3;
1467 }
1468 p1 += 32 * 3, p2 += 32 * 3;
1469 }
1470
1471 return wxBitmap(smallimg);
1472}
1473
1474// finds empty borders and return non-empty area of image:
1475static wxImage CutEmptyBorders(const wxImage& img)
1476{
1477 unsigned char mr = img.GetMaskRed(),
1478 mg = img.GetMaskGreen(),
1479 mb = img.GetMaskBlue();
1480 unsigned char *dt = img.GetData(), *dttmp;
1481 unsigned w = img.GetWidth(), h = img.GetHeight();
1482
1483 unsigned top, bottom, left, right, i;
1484 bool empt;
1485
1486#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
1487#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = FALSE; break;}
1488
1489 for (empt = TRUE, top = 0; empt && top < h; top++)
1490 {
1491 MK_DTTMP(0, top);
1492 for (i = 0; i < w; i++, dttmp+=3)
1493 NOEMPTY_PIX(empt)
1494 }
1495 for (empt = TRUE, bottom = h-1; empt && bottom > top; bottom--)
1496 {
1497 MK_DTTMP(0, bottom);
1498 for (i = 0; i < w; i++, dttmp+=3)
1499 NOEMPTY_PIX(empt)
1500 }
1501 for (empt = TRUE, left = 0; empt && left < w; left++)
1502 {
1503 MK_DTTMP(left, 0);
1504 for (i = 0; i < h; i++, dttmp+=3*w)
1505 NOEMPTY_PIX(empt)
1506 }
1507 for (empt = TRUE, right = w-1; empt && right > left; right--)
1508 {
1509 MK_DTTMP(right, 0);
1510 for (i = 0; i < h; i++, dttmp+=3*w)
1511 NOEMPTY_PIX(empt)
1512 }
1513 top--, left--, bottom++, right++;
1514
1515 return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
1516}
1517#endif // wxUSE_MIMETYPE
1518
1519int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
1520{
1521 if (!m_smallImageList)
1522 Create();
1523
1524#if wxUSE_MIMETYPE
1525 if (!extension.IsEmpty())
1526 {
1527 wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
1528 if (entry) return (entry -> id);
1529 }
1530
1531 wxFileType *ft = (mime.IsEmpty()) ?
1532 wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
1533 wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
1534 wxIcon ic;
1535 if (ft == NULL || (!ft->GetIcon(&ic)) || (!ic.Ok()))
1536 {
1537 int newid = file;
1538 m_HashTable->Put(extension, new wxFileIconEntry(newid));
1539 return newid;
1540 }
1541
1542 wxBitmap tmpBmp;
1543 tmpBmp.CopyFromIcon(ic);
1544 wxImage img = tmpBmp.ConvertToImage();
1545
1546 delete ft;
1547
1548 int id = m_smallImageList->GetImageCount();
1549 if (img.GetWidth() == 16 && img.GetHeight() == 16)
1550 m_smallImageList->Add(wxBitmap(img));
1551 else
1552 {
1553 if (img.GetWidth() != 32 || img.GetHeight() != 32)
1554 m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(32, 32)));
1555 else
1556 m_smallImageList->Add(CreateAntialiasedBitmap(img));
1557 }
1558 m_HashTable->Put(extension, new wxFileIconEntry(id));
1559 return id;
1560
1561#else // !wxUSE_MIMETYPE
1562
1563 if (extension == wxT("exe"))
1564 return executable;
1565 else
1566 return file;
1567#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
1568}
1569
1e6feb95 1570#endif // wxUSE_DIRDLG