[wxGenericDirCtrl] use of item-icons? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

[wxGenericDirCtrl] use of item-icons?

Post by MoonKid »

How dows wxGenericDirCtrl decide which image is used for an item (file)?

Some of the files display in my ctrl got the system-specified (WinXP) icons and others (for example image files like bmp, gif, jpg) got other icons that are not specified by my system.
Can I change this behavior? Can I say the ctrl should only use system-icons or should use only own icons?

What is about the directory icons? Is it possible to say that should be system-icons?
xskater11x
Earned a small fee
Earned a small fee
Posts: 22
Joined: Sat May 20, 2006 2:45 am

Post by xskater11x »

wxGenericDirCtrl gets its icons using the GetIconID function (part of wxFileIconsTable class in dirctrlg.h) in dirctrlg.cpp:

the headerfile can be found in includes/wx/generic/dirctrlg.h
the cpp file is in the src folder somewher,e i jsut did a search for it.

Code: Select all


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 -> id);
    }

    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.Ok() )
    {
        int newid = file;
        m_HashTable->Put(extension, new wxFileIconEntry(newid));
        return newid;
    }

    wxBitmap bmp;
    bmp.CopyFromIcon(ic);

    if ( !bmp.Ok() )
    {
        int newid = file;
        m_HashTable->Put(extension, new wxFileIconEntry(newid));
        return newid;
    }

    const unsigned int size = 16;

    int id = m_smallImageList->GetImageCount();
    if ((bmp.GetWidth() == (int) size) && (bmp.GetHeight() == (int) size))
    {
        m_smallImageList->Add(bmp);
    }
#if wxUSE_IMAGE && (!defined(__WXMSW__) || 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(id));
    return id;

#else // !wxUSE_MIMETYPE

    wxUnusedVar(mime);
    if (extension == wxT("exe"))
        return executable;
    else
        return file;
#endif // wxUSE_MIMETYPE/!wxUSE_MIMETYPE
}
MoonKid
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 543
Joined: Wed Apr 05, 2006 9:39 am
Contact:

Post by MoonKid »

xskater11x wrote:

Code: Select all

#if wxUSE_MIMETYPE
    if (!extension.empty())
    {
        wxFileIconEntry *entry = (wxFileIconEntry*) m_HashTable->Get(extension);
        if (entry) return (entry -> id);
    }
Ok, but why does this code sometimes work and sometimes not?
Mr.shi
Earned a small fee
Earned a small fee
Posts: 13
Joined: Fri Jul 13, 2007 3:05 am

Post by Mr.shi »

#include <wx/dirctrl.h>

SetImageList( wxTheFileIconsTable->GetSmallImageList() );

int image_id = wxFileIconsTable::file;
if (fileName.Find(wxT('.')) != wxNOT_FOUND)
image_id = wxTheFileIconsTable->GetIconID(fileName.AfterLast(wxT('.')));
Post Reply