⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iconmanager.cpp

📁 Last change: 2008-02-03 This is the source code of KCeasy。
💻 CPP
字号:
/*
This file is part of KCeasy (http://www.kceasy.com)
Copyright (C) 2002-2004 Markus Kern <mkern@kceasy.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
*/
//---------------------------------------------------------------------------
#pragma hdrstop
#include "IconManager.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)

TIconManager::TIconManager(TImageList* NImageList)
:   ImageList(NImageList)
{
//    ImageList->Overlay(DownloadIndex,DownloadOverlayIndex);
}

int TIconManager::GetIconIndex(const string& File)
{
    int pos = File.rfind('.');
    if(pos == -1)
        return GetIconInfo(File).Index;
    else
        return GetIconInfo(File.substr(pos+1)).Index;
}

int TIconManager::GetIconIndex(const TFileType FileType)
{
    switch(FileType) {
    case FTAudio:       return AudioFileIndex;
    case FTVideo:       return VideoFileIndex;
    case FTImage:       return ImageFileIndex;
    case FTDocument:    return DocumentFileIndex;
    case FTSoftware:    return SoftwareFileIndex;
    case FTTorrent:     return TorrentFileIndex;
    default:            return UnknownFileIndex;
    }
}

const string& TIconManager::GetFileDesc(const string& File)
{
    int pos = File.rfind('.');
    if(pos == -1)
        return GetIconInfo(File).Description;
    else
        return GetIconInfo(File.substr(pos+1)).Description;
}

int TIconManager::GetNetworkIconIndex(const string& Network)
{
    // TODO: work with localization?
    if(Network == "All Networks")
        return AllNetsIndex;
    if(Network == "OpenFT")
        return OpenftIndex;
    if(Network == "FastTrack")
        return KazaaIndex;
    if(Network == "Gnutella")
        return GnutellaIndex;
    if(Network == "Ares")
        return AresIndex;
    if(Network == "OpenNap")
        return NapsterIndex;
    if(Network == "Http")
        return HttpIndex;

#if 0
    return UnknownNetIndex;
#else
    return -1;
#endif
}

int TIconManager::GetFolderIconIndex(bool Open)
{
    if(Open)
        return OpenFolderIndex;
    else
        return ClosedFolderIndex;
}

int TIconManager::GetSearchRealmIconIndex(TSearchRealm Realm)
{
    switch(Realm) {
    case SRAny:      return UnknownFileIndex;
    case SRAudio:    return AudioFileIndex;
    case SRVideo:    return VideoFileIndex;
    case SRImage:    return ImageFileIndex;
    case SRDocument: return DocumentFileIndex;
    case SRSoftware: return SoftwareFileIndex;
    case SRSource:   return UnknownFileIndex;
    case SRUser:     return UnknownFileIndex;
    case SRTorrent:  return TorrentFileIndex;
    default:         return UnknownFileIndex;
    }
}

// private

const TIconManager::TIconInfo& TIconManager::GetIconInfo(const string& Ext)
{
    map<string,TIconInfo>::iterator itr = IconMap.find(Ext);

    if(itr == IconMap.end())
#ifdef GET_ICONS_FROM_SHELL
        itr = AddIconFromShell(Ext);
#else
        itr = AddIconFromReg(Ext);
#endif

    return (*itr).second;
}

#ifndef GET_ICONS_FROM_SHELL

map<string,TIconManager::TIconInfo>::iterator TIconManager::AddIconFromReg(const string& Ext)
{
    HKEY hKey1, hKey2;
    char buf[MAX_PATH], *p;
    DWORD Size;
    HICON hIcon;
    map<string,TIconInfo>::iterator itr;

    TIconInfo IconInfo;
    itr = IconMap.insert(map<string,TIconInfo>::value_type(Ext,IconInfo)).first;
    (*itr).second.Index = UnknownFileIndex; // default for file unknown
    (*itr).second.Description = "Unknown";

    buf[0] = '.';
    strcpy(buf+1,Ext.c_str());

    // open extension key and get class key name
    if((::RegOpenKeyEx(HKEY_CLASSES_ROOT,buf,0,KEY_QUERY_VALUE,&hKey1)) != ERROR_SUCCESS)
        return itr;
    Size = MAX_PATH;
    if((::RegQueryValueEx(hKey1,NULL,0,NULL,buf,&Size)) != ERROR_SUCCESS) {
        RegCloseKey(hKey1);
        return itr;
    }
    RegCloseKey(hKey1);
    // open class key
    if((::RegOpenKeyEx(HKEY_CLASSES_ROOT,buf,0,KEY_QUERY_VALUE,&hKey1)) != ERROR_SUCCESS)
        return itr;
    if((::RegOpenKeyEx(hKey1,"DefaultIcon",0,KEY_QUERY_VALUE,&hKey2)) != ERROR_SUCCESS) {
        RegCloseKey(hKey1);
        return itr;
    }
    // get icon location string
    Size = MAX_PATH;
    if((::RegQueryValueEx(hKey2,NULL,0,NULL,buf,&Size)) != ERROR_SUCCESS) {
        RegCloseKey(hKey1);
        return itr;
    }
    if((p = strrchr(buf,',')) == NULL) {
        RegCloseKey(hKey1);
        RegCloseKey(hKey2);
        return itr;
    }
    *p = 0;
    int ResIndex = atoi(p+1);
    // load icon
    if(::ExtractIconEx(buf,ResIndex,NULL,&hIcon,1) == NULL || hIcon == NULL) {
        RegCloseKey(hKey1);
        RegCloseKey(hKey2);
        return itr;
    }
    TIcon* Icon = new TIcon();
    Icon->ReleaseHandle();
    Icon->Handle = hIcon;
    (*itr).second.Index = ImageList->AddIcon(Icon);
    if((*itr).second.Index == -1)
        (*itr).second.Index = UnknownFileIndex;
    delete Icon;

    // get file type
    Size = MAX_PATH;
    if((::RegQueryValueEx(hKey1,NULL,0,NULL,buf,&Size)) == ERROR_SUCCESS) {
        (*itr).second.Description = buf;
    }
    RegCloseKey(hKey1);
    RegCloseKey(hKey2);
    return itr;
}

#else

map<string,TIconManager::TIconInfo>::iterator TIconManager::AddIconFromShell(const string& Ext)
{
    map<string,TIconInfo>::iterator itr;

    TIconInfo IconInfo;
    itr = IconMap.insert(map<string,TIconInfo>::value_type(Ext,IconInfo)).first;
    (*itr).second.Index = UnknownFileIndex; // default for file unknown
    (*itr).second.Description = "Unknown";
/*
    char TmpDir[MAX_PATH];
    if(!GetTempPath(MAX_PATH,TmpDir))
        return itr;

    // create temp file with specified extension
    string Path = string(TmpDir) + SanitizePath(string("\\KCeasyTmp.") + Ext);
    HANDLE hFile = CreateFile(Path.c_str(),GENERIC_WRITE,0,NULL,CREATE_NEW,0,NULL);
    if(hFile != INVALID_HANDLE_VALUE)
        CloseHandle(hFile);

    // get icon
    SHFILEINFO ShFileInfo;
    SHGetFileInfo(Path.c_str(),0,&ShFileInfo,sizeof(SHFILEINFO),SHGFI_ICON	| SHGFI_SMALLICON | SHGFI_TYPENAME);
*/
    // get icon
    SHFILEINFO ShFileInfo;
    string Path("."); Path += Ext;
    SHGetFileInfo(Path.c_str(),FILE_ATTRIBUTE_NORMAL,&ShFileInfo,sizeof(SHFILEINFO),SHGFI_ICON	| SHGFI_SMALLICON | SHGFI_TYPENAME | SHGFI_USEFILEATTRIBUTES);

    if(ShFileInfo.hIcon) {
        TIcon* Icon = new TIcon();
        Icon->ReleaseHandle();
        Icon->Handle = ShFileInfo.hIcon;
        (*itr).second.Index = ImageList->AddIcon(Icon);
        if((*itr).second.Index == -1)
            (*itr).second.Index = UnknownFileIndex;
        delete Icon;
    }

    if(ShFileInfo.szTypeName[0])
        (*itr).second.Description = ShFileInfo.szTypeName;
/*
    // delete file
    if(hFile != INVALID_HANDLE_VALUE)
        DeleteFile(Path.c_str());
*/
    return itr;
}

#endif // !GET_ICONS_FROM_SHELL

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -