+IMPLEMENT_DYNAMIC_CLASS(wxFileIconsTableModule, wxModule)
+
+class wxFileIconEntry : public wxObject
+{
+public:
+ wxFileIconEntry(int i) { iconid = i; }
+
+ int iconid;
+};
+
+wxFileIconsTable::wxFileIconsTable()
+{
+ m_HashTable = NULL;
+ m_smallImageList = NULL;
+}
+
+wxFileIconsTable::~wxFileIconsTable()
+{
+ if (m_HashTable)
+ {
+ WX_CLEAR_HASH_TABLE(*m_HashTable);
+ delete m_HashTable;
+ }
+ if (m_smallImageList) delete m_smallImageList;
+}
+
+// delayed initialization - wait until first use (wxArtProv not created yet)
+void wxFileIconsTable::Create()
+{
+ wxCHECK_RET(!m_smallImageList && !m_HashTable, wxT("creating icons twice"));
+ m_HashTable = new wxHashTable(wxKEY_STRING);
+ m_smallImageList = new wxImageList(16, 16);
+
+ // folder:
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+ // folder_open
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FOLDER_OPEN,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+ // computer
+#ifdef __WXGTK20__
+ // GTK24 uses this icon in the file open dialog
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+#else
+ m_smallImageList->Add(wxIcon(file_icons_tbl_computer_xpm));
+#endif
+ // drive
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_HARDDISK,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+ // cdrom
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_CDROM,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+ // floppy
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_FLOPPY,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+ // removeable
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_REMOVABLE,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+ // file
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_NORMAL_FILE,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+ // executable
+ if (GetIconID(wxEmptyString, wxT("application/x-executable")) == file)
+ {
+ m_smallImageList->Add(wxArtProvider::GetBitmap(wxART_EXECUTABLE_FILE,
+ wxART_CMN_DIALOG,
+ wxSize(16, 16)));
+ delete m_HashTable->Get(wxT("exe"));
+ m_HashTable->Delete(wxT("exe"));
+ m_HashTable->Put(wxT("exe"), new wxFileIconEntry(executable));
+ }
+ /* else put into list by GetIconID
+ (KDE defines application/x-executable for *.exe and has nice icon)
+ */
+}
+
+wxImageList *wxFileIconsTable::GetSmallImageList()
+{
+ if (!m_smallImageList)
+ Create();
+
+ return m_smallImageList;
+}
+
+#if wxUSE_MIMETYPE && wxUSE_IMAGE && (!defined(__WINDOWS__) || wxUSE_WXDIB)
+// VS: we don't need this function w/o wxMimeTypesManager because we'll only have
+// one icon and we won't resize it
+
+static wxBitmap CreateAntialiasedBitmap(const wxImage& img)
+{
+ const unsigned int size = 16;
+
+ wxImage smallimg (size, size);
+ unsigned char *p1, *p2, *ps;
+ unsigned char mr = img.GetMaskRed(),
+ mg = img.GetMaskGreen(),
+ mb = img.GetMaskBlue();
+
+ unsigned x, y;
+ unsigned sr, sg, sb, smask;
+
+ p1 = img.GetData(), p2 = img.GetData() + 3 * size*2, ps = smallimg.GetData();
+ smallimg.SetMaskColour(mr, mr, mr);
+
+ for (y = 0; y < size; y++)
+ {
+ for (x = 0; x < size; x++)
+ {
+ sr = sg = sb = smask = 0;
+ if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
+ sr += p1[0], sg += p1[1], sb += p1[2];
+ else smask++;
+ p1 += 3;
+ if (p1[0] != mr || p1[1] != mg || p1[2] != mb)
+ sr += p1[0], sg += p1[1], sb += p1[2];
+ else smask++;
+ p1 += 3;
+ if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
+ sr += p2[0], sg += p2[1], sb += p2[2];
+ else smask++;
+ p2 += 3;
+ if (p2[0] != mr || p2[1] != mg || p2[2] != mb)
+ sr += p2[0], sg += p2[1], sb += p2[2];
+ else smask++;
+ p2 += 3;
+
+ if (smask > 2)
+ ps[0] = ps[1] = ps[2] = mr;
+ else
+ {
+ ps[0] = (unsigned char)(sr >> 2);
+ ps[1] = (unsigned char)(sg >> 2);
+ ps[2] = (unsigned char)(sb >> 2);
+ }
+ ps += 3;
+ }
+ p1 += size*2 * 3, p2 += size*2 * 3;
+ }
+
+ return wxBitmap(smallimg);
+}
+
+// This function is currently not unused anymore
+#if 0
+// finds empty borders and return non-empty area of image:
+static wxImage CutEmptyBorders(const wxImage& img)
+{
+ unsigned char mr = img.GetMaskRed(),
+ mg = img.GetMaskGreen(),
+ mb = img.GetMaskBlue();
+ unsigned char *dt = img.GetData(), *dttmp;
+ unsigned w = img.GetWidth(), h = img.GetHeight();
+
+ unsigned top, bottom, left, right, i;
+ bool empt;
+
+#define MK_DTTMP(x,y) dttmp = dt + ((x + y * w) * 3)
+#define NOEMPTY_PIX(empt) if (dttmp[0] != mr || dttmp[1] != mg || dttmp[2] != mb) {empt = false; break;}
+
+ for (empt = true, top = 0; empt && top < h; top++)
+ {
+ MK_DTTMP(0, top);
+ for (i = 0; i < w; i++, dttmp+=3)
+ NOEMPTY_PIX(empt)
+ }
+ for (empt = true, bottom = h-1; empt && bottom > top; bottom--)
+ {
+ MK_DTTMP(0, bottom);
+ for (i = 0; i < w; i++, dttmp+=3)
+ NOEMPTY_PIX(empt)
+ }
+ for (empt = true, left = 0; empt && left < w; left++)
+ {
+ MK_DTTMP(left, 0);
+ for (i = 0; i < h; i++, dttmp+=3*w)
+ NOEMPTY_PIX(empt)
+ }
+ for (empt = true, right = w-1; empt && right > left; right--)
+ {
+ MK_DTTMP(right, 0);
+ for (i = 0; i < h; i++, dttmp+=3*w)
+ NOEMPTY_PIX(empt)
+ }
+ top--, left--, bottom++, right++;
+
+ return img.GetSubImage(wxRect(left, top, right - left + 1, bottom - top + 1));
+}
+#endif // #if 0
+
+#endif // wxUSE_MIMETYPE
+
+int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime)
+{
+ if (!m_smallImageList)
+ Create();
+
+#if wxUSE_MIMETYPE
+ if (!extension.empty())
+ {
+ wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
+ if (entry) return (entry -> iconid);
+ }
+
+ wxFileType *ft = (mime.empty()) ?
+ wxTheMimeTypesManager -> GetFileTypeFromExtension(extension) :
+ wxTheMimeTypesManager -> GetFileTypeFromMimeType(mime);
+
+ wxIconLocation iconLoc;
+ wxIcon ic;
+
+ {
+ wxLogNull logNull;
+ if ( ft && ft->GetIcon(&iconLoc) )
+ {
+ ic = wxIcon( iconLoc );
+ }
+ }
+
+ delete ft;
+
+ if ( !ic.IsOk() )
+ {
+ int newid = file;
+ m_HashTable->Put(extension, new wxFileIconEntry(newid));
+ return newid;
+ }
+
+ wxBitmap bmp;
+ bmp.CopyFromIcon(ic);
+
+ if ( !bmp.IsOk() )
+ {
+ int newid = file;
+ m_HashTable->Put(extension, new wxFileIconEntry(newid));
+ return newid;
+ }
+
+ const unsigned int size = 16;
+
+ int treeid = m_smallImageList->GetImageCount();
+ if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
+ {
+ m_smallImageList->Add(bmp);
+ }
+#if wxUSE_IMAGE && (!defined(__WINDOWS__) || wxUSE_WXDIB)
+ else
+ {
+ wxImage img = bmp.ConvertToImage();
+
+ if ((img.GetWidth() != size*2) || (img.GetHeight() != size*2))
+// m_smallImageList->Add(CreateAntialiasedBitmap(CutEmptyBorders(img).Rescale(size*2, size*2)));
+ m_smallImageList->Add(CreateAntialiasedBitmap(img.Rescale(size*2, size*2)));
+ else
+ m_smallImageList->Add(CreateAntialiasedBitmap(img));
+ }
+#endif // wxUSE_IMAGE
+
+ m_HashTable->Put(extension, new wxFileIconEntry(treeid));
+ return treeid;
+
+#else // !wxUSE_MIMETYPE
+
+ wxUnusedVar(mime);
+ if (extension == wxT("exe"))
+ return executable;
+ else
+ return file;
+#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
+}
+
+#endif // wxUSE_DIRDLG || wxUSE_FILEDLG