6#include "res.mgr/SResProvider.h"
16const TCHAR KTypeBitmap[] = _T(
"BITMAP");
17const TCHAR KTypeCursor[] = _T(
"CURSOR");
18const TCHAR KTypeIcon[] = _T(
"ICON");
19const TCHAR KTypeHtml[] = _T(
"HTML");
25 GETRENDERFACTORY->CreateBitmap(&pImg);
42 GETRENDERFACTORY->GetImgDecoderFactory()->CreateImgX(&pImgX);
55SResProviderPE::SResProviderPE()
61SResProviderPE::~SResProviderPE()
65 FreeLibrary(m_hResInst);
69BOOL SResProviderPE::Init(WPARAM wParam, LPARAM lParam)
73 m_hResInst = (HINSTANCE)wParam;
79 LPCTSTR pszPath = (LPCTSTR)wParam;
80 m_hResInst = LoadLibrary(pszPath);
88HBITMAP SResProviderPE::LoadBitmap(LPCTSTR pszResName)
90 return ::LoadBitmap(m_hResInst, pszResName);
93HICON SResProviderPE::LoadIcon(LPCTSTR pszResName,
int cx ,
int cy )
95 return (HICON)::LoadImage(m_hResInst, pszResName, IMAGE_ICON, cx, cy, LR_DEFAULTCOLOR);
98HCURSOR SResProviderPE::LoadCursor(LPCTSTR pszResName)
100 HRSRC hRes = ::FindResource(m_hResInst, pszResName, RT_ANICURSOR);
103 DWORD dwSize = SizeofResource(m_hResInst, hRes);
104 HGLOBAL hGlob = LoadResource(m_hResInst, hRes);
105 LPBYTE pBytes = (LPBYTE)LockResource(hGlob);
106 return (HCURSOR)CreateIconFromResource(pBytes, dwSize, FALSE, 0x00030000);
110 return ::LoadCursor(m_hResInst, pszResName);
114IBitmapS *SResProviderPE::LoadImage(LPCTSTR strType, LPCTSTR pszResName)
116 if (!HasResource(strType, pszResName))
118 size_t szImgBuf = GetRawBufferSize(strType, pszResName);
121 LPVOID pBuf = GetRawBufferPtr(strType, pszResName);
125IImgX *SResProviderPE::LoadImgX(LPCTSTR strType, LPCTSTR pszResName)
127 if (!HasResource(strType, pszResName))
129 size_t szImgBuf = GetRawBufferSize(strType, pszResName);
132 LPVOID pBuf = GetRawBufferPtr(strType, pszResName);
136size_t SResProviderPE::GetRawBufferSize(LPCTSTR strType, LPCTSTR pszResName)
138 HRSRC hRsrc = MyFindResource(strType, pszResName);
143 return ::SizeofResource(m_hResInst, hRsrc);
146BOOL SResProviderPE::GetRawBuffer(LPCTSTR strType, LPCTSTR pszResName, LPVOID pBuf,
size_t size)
149 HRSRC hRsrc = MyFindResource(strType, pszResName);
154 size_t dwSize = ::SizeofResource(m_hResInst, hRsrc);
160 SetLastError(ERROR_INSUFFICIENT_BUFFER);
163 HGLOBAL hGlobal = ::LoadResource(m_hResInst, hRsrc);
167 LPVOID pBuffer = ::LockResource(hGlobal);
171 memcpy(pBuf, pBuffer, dwSize);
173 ::FreeResource(hGlobal);
178LPVOID SResProviderPE::GetRawBufferPtr(LPCTSTR strType, LPCTSTR pszResName)
181 HRSRC hRsrc = MyFindResource(strType, pszResName);
186 size_t dwSize = ::SizeofResource(m_hResInst, hRsrc);
190 HGLOBAL hGlobal = ::LoadResource(m_hResInst, hRsrc);
194 LPVOID pBuffer = ::LockResource(hGlobal);
196 ::FreeResource(hGlobal);
201BOOL SResProviderPE::HasResource(LPCTSTR strType, LPCTSTR pszResName)
205 return MyFindResource(strType, pszResName) != NULL;
208HRSRC SResProviderPE::MyFindResource(LPCTSTR strType, LPCTSTR pszResName)
210 if (_tcsicmp(strType, KTypeBitmap) == 0)
212 else if (_tcsicmp(strType, KTypeIcon) == 0)
213 strType = RT_GROUP_ICON;
214 else if (_tcsicmp(strType, KTypeCursor) == 0)
215 strType = RT_GROUP_CURSOR;
216 else if (_tcsicmp(strType, KTypeHtml) == 0)
219 HRSRC hRet = ::FindResource(m_hResInst, pszResName, strType);
220 if (!hRet && strType == RT_GROUP_CURSOR)
221 hRet = ::FindResource(m_hResInst, pszResName, RT_ANICURSOR);
231static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam)
233 EnumResParam *enumParam = (EnumResParam *)lParam;
234 return enumParam->fun(lpszType, lpszName, enumParam->lParam);
237static BOOL CALLBACK EnumResTypeProc(HMODULE hModule, LPTSTR lpszType, LONG_PTR lParam)
239 return EnumResourceNames(hModule, lpszType, EnumResNameProc, lParam);
242void SResProviderPE::EnumResource(EnumResCallback funEnumCB, LPARAM lp)
244 EnumResParam param = { funEnumCB, lp };
245 EnumResourceTypes(m_hResInst, EnumResTypeProc, (LONG_PTR)¶m);
250 EnumFileCallback fun;
253static BOOL CALLBACK EnumResFileProc(HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam)
255 EnumFileParam *enumParam = (EnumFileParam *)lParam;
256 SStringT strPath = SStringT().Format(_T(
"%s:%s"), lpszType, lpszName);
257 return enumParam->fun(strPath.c_str(), enumParam->lParam);
260static BOOL CALLBACK EnumResTypeProc2(HMODULE hModule, LPTSTR lpszType, LONG_PTR lParam)
262 return EnumResourceNames(hModule, lpszType, EnumResFileProc, lParam);
264void SResProviderPE::EnumFile(THIS_ EnumFileCallback funEnumCB, LPARAM lp)
266 EnumFileParam param = { funEnumCB, lp };
267 EnumResourceTypes(m_hResInst, EnumResTypeProc2, (LONG_PTR)¶m);
276 return (HBITMAP)
::LoadImage(NULL, strPath, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION | LR_LOADFROMFILE);
281 return (HICON)
::LoadImage(NULL, strPath, IMAGE_ICON, cx, cy, LR_LOADFROMFILE);
286 return (HCURSOR)
::LoadImage(NULL, strPath, IMAGE_CURSOR, 0, 0, LR_LOADFROMFILE);
292 GETRENDERFACTORY->CreateBitmap(&pImg);
306 GETRENDERFACTORY->GetImgDecoderFactory()->CreateImgX(&pImgX);
323 return file_length(strPath);
328 FILE *f = _tfopen(strPath, _T(
"rb"));
332 size_t len = _filelength(_fileno(f));
334 fseek(f, 0, SEEK_END);
335 size_t len = ftell(f);
336 fseek(f, 0, SEEK_SET);
340 SetLastError(ERROR_INSUFFICIENT_BUFFER);
344 BOOL bRet = (len == fread(pBuf, 1, len, f));
362 SStringT strRet = m_strPath + _T(PATH_SLASH) + pszResName;
363 DWORD dwAttr = GetFileAttributes(strRet);
364 if (dwAttr == INVALID_FILE_ATTRIBUTES || (dwAttr & FILE_ATTRIBUTE_ARCHIVE) == 0)
368 SResID resID(strType, pszResName);
369 SMap<SResID, SStringT>::CPair *p = m_mapFiles.Lookup(resID);
373 SStringT strRet = m_strPath + _T(PATH_SLASH) + p->m_value;
379 SStringT strPath =
GetRes(KTypeBitmap, pszResName);
380 if (strPath.IsEmpty())
387 SStringT strPath =
GetRes(KTypeIcon, pszResName);
388 if (strPath.IsEmpty())
395 SStringT strPath =
GetRes(KTypeCursor, pszResName);
396 if (strPath.IsEmpty())
403 SStringT strPath =
GetRes(strType, pszResName);
404 if (strPath.IsEmpty())
411 SStringT strPath =
GetRes(strType, pszResName);
412 if (strPath.IsEmpty())
419 SStringT strPath =
GetRes(strType, pszResName);
420 if (strPath.IsEmpty())
427 SStringT strPath =
GetRes(strType, pszResName);
428 if (strPath.IsEmpty())
435 LPCTSTR pszPath = (LPCTSTR)wParam;
437 SStringT strPathIndex = pszPath;
438 strPathIndex += _T(PATH_SLASH);
439 strPathIndex += UIRES_INDEX;
442 SStringT strFileName;
443 if (!xmlDoc.
load_file(strPathIndex, xml_parse_default, enc_auto))
452 SStringT strType = S_CW2T(xmlType.
name());
459 strFile.ReplaceChar(_T(
'\\'), _T(
'/'));
464 m_mapFiles[id] = strFile;
470 TCHAR szFullPath[1025];
471 GetFullPathName(pszPath, 1024, szFullPath, NULL);
472 m_strPath = szFullPath;
481 SStringT resPath =
GetRes(strType, pszResName);
482 return !resPath.IsEmpty();
487 SPOSITION pos = m_mapFiles.GetStartPosition();
490 SResID id = m_mapFiles.GetNextKey(pos);
491 if (!funEnumCB(
id.szName,
id.szType, lp))
507 strFilter = m_strPath + _T(
"\\") + pszPath + _T(
"\\*.*");
509 strFilter = m_strPath + _T(
"\\*.*");
510 HANDLE hFind = FindFirstFile(strFilter.c_str(), &wfd);
511 if (hFind != INVALID_HANDLE_VALUE)
517 strPath = wfd.cFileName;
519 strPath = SStringT().Format(_T(
"%s\\%s"), pszPath, wfd.cFileName);
520 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
522 if (_tcscmp(wfd.cFileName, _T(
".")) == 0 || _tcscmp(wfd.cFileName, _T(
"..")) == 0)
524 _EnumFile(strPath.c_str(), funEnumCB, lp);
526 else if (wfd.dwFileAttributes & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_READONLY))
528 if (!funEnumCB(strPath.c_str(), lp))
531 }
while (FindNextFile(hFind, &wfd));
536 struct dirent *entry;
540 strFilter = m_strPath + _T(
"/") + pszPath;
542 strFilter = m_strPath;
544 dir = opendir(pszPath);
550 while ((entry = readdir(dir)) != NULL)
554 strPath = entry->d_name;
556 strPath = SStringT().Format(_T(
"%s\\%s"), pszPath, entry->d_name);
557 if (entry->d_type & DT_DIR)
559 if (_tcscmp(entry->d_name, _T(
".")) == 0 || _tcscmp(entry->d_name, _T(
"..")) == 0)
561 _EnumFile(strPath.c_str(), funEnumCB, lp);
563 else if (entry->d_type & DT_REG)
565 if (!funEnumCB(strPath.c_str(), lp))
static HBITMAP LoadBitmap(LPCTSTR pszFileName)
Loads a bitmap from a file.
static size_t GetRawBufferSize(LPCTSTR pszFileName)
Retrieves the size of the raw buffer for a file.
static BOOL GetRawBuffer(LPCTSTR pszFileName, LPVOID pBuf, size_t size)
Retrieves the raw buffer for a file.
static IBitmapS * LoadImage(LPCTSTR pszFileName)
Loads an image from a file as an IBitmapS object.
static IImgX * LoadImgX(LPCTSTR pszFileName)
Loads an image from a file as an IImgX object.
static HICON LoadIcon(LPCTSTR pszFileName, int cx=0, int cy=0)
Loads an icon from a file.
static HCURSOR LoadCursor(LPCTSTR pszFileName)
Loads a cursor from a file.
static IImgX * LoadImgX(LPVOID pBuf, size_t size)
Loads an image from a memory buffer as an IImgX object.
static IBitmapS * LoadImage(LPVOID pBuf, size_t size)
Loads an image from a memory buffer.
HBITMAP LoadBitmap(LPCTSTR pszResName) OVERRIDE
Loads a bitmap resource.
BOOL HasResource(LPCTSTR pszType, LPCTSTR pszResName) OVERRIDE
Checks if a resource exists.
HICON LoadIcon(LPCTSTR pszResName, int cx, int cy) OVERRIDE
Loads an icon resource.
SResProviderFiles()
Constructor.
IBitmapS * LoadImage(LPCTSTR pszType, LPCTSTR pszResName) OVERRIDE
Loads an image resource as an IBitmapS object.
void EnumFile(EnumFileCallback funEnumCB, LPARAM lp) OVERRIDE
Enumerates files in the resource provider.
BOOL GetRawBuffer(LPCTSTR pszType, LPCTSTR pszResName, LPVOID pBuf, size_t size) OVERRIDE
Retrieves the raw buffer for a resource.
IImgX * LoadImgX(LPCTSTR pszType, LPCTSTR pszResName) OVERRIDE
Loads an image resource as an IImgX object.
HCURSOR LoadCursor(LPCTSTR pszResName) OVERRIDE
Loads a cursor resource.
SStringT GetRes(LPCTSTR strType, LPCTSTR pszResName)
Retrieves the resource path for a given type and name.
void _EnumFile(LPCTSTR pszPath, EnumFileCallback funEnumCB, LPARAM lp)
Enumerates files in a specified path.
size_t GetRawBufferSize(LPCTSTR pszType, LPCTSTR pszResName) OVERRIDE
Retrieves the size of the raw buffer for a resource.
void EnumResource(EnumResCallback funEnumCB, LPARAM lp) OVERRIDE
Enumerates resources of a specific type.
BOOL Init(WPARAM wParam, LPARAM lParam) OVERRIDE
Initializes the resource provider.
const wchar_t * value() const
Gets the attribute value.
Implementation of IXmlDoc.
SXmlNode root() const
Retrieves the root node of the document.
bool load_file(const char *path, unsigned int options=xml_parse_default, XmlEncoding encoding=enc_auto)
Loads the document from a file (ANSI version).
Class representing an XML node.
SXmlNode next_sibling() const
Gets the next sibling node in the children list of the parent node.
SXmlNode first_child() const
Gets the first child node of the node.
const wchar_t * name() const
Gets the name of the node.
SXmlAttr attribute(const wchar_t *name, bool bCaseSensitive=false) const
Gets the attribute with the specified name.
SXmlNode child(const wchar_t *name, bool bCaseSensitive=false) const
Gets the child node, attribute, or next/previous sibling with the specified name.
HRESULT LoadFromMemory(LPBYTE pBuf, size_t szLen) PURE
Loads the bitmap from memory.
HRESULT LoadFromFile(LPCTSTR pszFileName) PURE
Loads the bitmap from a file.
long Release() PURE
Decrements the reference count for the object.
Interface for image data.
int LoadFromFileA(LPCSTR pszFileName) PURE
Load image data from a file (ANSI encoding).
int LoadFromMemory(void *pBuf, size_t bufLen) PURE
Load image data from a memory buffer.
int LoadFromFileW(LPCWSTR pszFileName) PURE
Load image data from a file (Unicode encoding).
long Release() PURE
Decrement the reference count.