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