/** * @file XTPResourceManager.h * * @copyright * (c) 1998-2025 Codejock Software, All Rights Reserved. * * This source file is the property of Codejock Software and must not be * redistributed by any means without the explicit written permission of * Codejock Software. * * The use of this source code is governed by the terms and conditions specified * in the Toolkit Pro license agreement. Codejock Software grants you, as a * single software developer, the limited right to use this software on one * computer only. * * Contact Information: * support@codejock.com * http://www.codejock.com * */ /** @cond */ #if !defined(__XTPRESOURCEMANAGER_H__) # define __XTPRESOURCEMANAGER_H__ /** @endcond */ # if _MSC_VER > 1000 # pragma once # endif // _MSC_VER > 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" /** * @brief * Standalone structure used to manage Language Info. */ struct XTP_RESOURCEMANAGER_LANGINFO { WORD wLanguage; /**< Language */ UINT nCodePage; /**< CodePage */ LPCTSTR lpszEncoding; /**< Encoding */ LPCTSTR lpszCaption; /**< Caption */ LPCTSTR lpszLang; /**< Lang */ LPCTSTR lpszSubLang; /**< SubLang */ BYTE nFontCharSet; /**< FontCharSet */ }; /** @cond */ # define XTP_CW2CA(lp) ((LPCSTR)CXTPResourceManager::CXTPW2A((LPCWSTR)(lp))) # define XTP_CA2CW(lp) ((LPCWSTR)CXTPResourceManager::CXTPA2W((LPCSTR)(lp))) # define XTP_CW2CAN(lp, n) ((LPCSTR)CXTPResourceManager::CXTPW2A((LPCWSTR)(lp), n)) # define XTP_CA2CWN(lp, n) ((LPCWSTR)CXTPResourceManager::CXTPA2W((LPCSTR)(lp), n)) # ifdef _UNICODE # define XTP_CT2CW(lp) ((LPCWSTR)(LPCTSTR)(lp)) # define XTP_CW2CT(lp) ((LPCTSTR)(LPCWSTR)(lp)) # define XTP_CT2CA(lp) XTP_CW2CA(lp) # define XTP_CA2CT(lp) XTP_CA2CW(lp) # define XTP_CT2CAN(lp, n) XTP_CW2CAN(lp, n) # define XTP_CA2CTN(lp, n) XTP_CA2CWN(lp, n) # else # define XTP_CT2CW(lp) XTP_CA2CW(lp) # define XTP_CW2CT(lp) XTP_CW2CA(lp) # define XTP_CT2CWN(lp, n) XTP_CA2CWN(lp, n) # define XTP_CW2CTN(lp, n) XTP_CW2CAN(lp, n) # define XTP_CT2CA(lp) ((LPCSTR)(LPCTSTR)(lp)) # define XTP_CA2CT(lp) ((LPCSTR)(LPCTSTR)(lp)) # endif /** @endcond */ /** * @brief * Standalone class used to managed toolkit resources. */ class _XTP_EXT_CLASS CXTPResourceManager : public CNoTrackObject { public: /** * @brief * CManageState is used internally by the toolkit to switch module * states. Specifically, the class enables the toolkit to read its * own .dll resources, and not that of the host application (or .dll). * Future versions of the class may handle other such details */ class _XTP_EXT_CLASS CManageState { public: /** * @brief * Constructs a CManageState object */ CManageState(); /** * @brief * Destroys a CManageState object, handles cleanup and deallocation. */ ~CManageState(); public: /** * @brief * Call this member function to UNDO managed state */ void Undo(); /** * @brief * Call this member function to REDO managed state */ void Redo(); protected: bool m_bSet; /**< true to set */ HMODULE m_hModOldResource; /**< Handle to previous resource. */ }; public: /** * @brief * Constructs a CXTPResourceManager object. */ CXTPResourceManager(); /** * @brief * Destroys a CXTPResourceManagerXML object, handles cleanup and de- * allocation. */ ~CXTPResourceManager(); public: /** * @brief * Call this member to set resource file to be used (dll or xml) * @param strResourceFile Resource file to be used. * @details * Call Close method in ExitInstance of your application to free resources. * @see * Close, SetResourceHandle */ void SetResourceFile(const CString& strResourceFile); /** * @brief * Call this member to set the resource handle for the calling module. * @param hModRes Handle to modules resource. */ void SetResourceHandle(HMODULE hModRes); /** * @brief * Call this member function to return the library resource handle. * @return * Returns the resource handle defined for the library. */ HMODULE GetResourceHandle() const; /** * @brief * The FindResource function tries to locate the resource in the library * resources first. If not found it continues to search for the resource * by walking the resource chain until a handle to the module containing * the resource if found. * @param hModule A reference to a HMODULE reference that receives a handle * to the module whose executable file contains the resource. * @param lpName Specifies the name of the resource. * @param lpType Specifies the type of the resource. * @return * If the function succeeds, the return value is a handle to the specified * resource's information block. To obtain a handle to the resource, pass * this handle to the LoadResource function. If the function fails, the * return value is NULL. To get extended error information, call GetLastError. */ HRSRC FindResource(HMODULE& hModule, LPCTSTR lpName, LPCTSTR lpType); public: /** * @brief * Reads a Windows string resource, identified by nID, into an existing CString object * @param pString Output string pointer * @param nIDResource A Windows string resource ID. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadString(CString* pString, UINT nIDResource); /** * @brief * Reads a Windows string resource, identified by nID. * @param nIDResource A Windows string resource ID. * @param pbError Pointer to a BOOL variable to report was resource load * successful or not. (Optional). * @return * Loaded string or empty string if such string ID does not find in resource. */ virtual CString LoadString(UINT nIDResource, BOOL* pbError = NULL); /** * @brief * Reads a Windows menu resource, identified by nID, into an existing CMenu object * @param lpMenu Output menu pointer * @param nIDResource A Windows menu resource ID. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadMenu(CMenu* lpMenu, UINT nIDResource); /** * @brief * Call this member function to load the toolbar specified by CToolBar and nIDResource. * @param pToolBar Result CToolBar pointer * @param nIDResource Resource ID of the toolbar to be loaded. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadToolBar(CToolBar* pToolBar, UINT nIDResource); /** * @brief * Reads a Windows bitmap resource, identified by nID, into an existing CBitmap object * @param pBitmap Result CBitmap pointer * @param nIDResource A Windows bitmap resource ID. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadBitmap(CBitmap* pBitmap, UINT nIDResource); /** * @brief * Reads a Windows bitmap resource, identified by nID, into an existing CBitmap object * @param pBitmap Result CBitmap pointer * @param lpszResource A Windows bitmap resource name. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadBitmap(CBitmap* pBitmap, LPCTSTR lpszResource); /** * @brief * Reads a Windows cursor resource, identified by nID, into an existing cursor object * @param nIDResource A Windows cursor resource ID. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual HCURSOR LoadCursor(UINT nIDResource); /** * @brief * Reads a Windows dialog resource, identified by pszResource * @param nIDResource A Windows dialog resource ID. * @return * LPCDLGTEMPLATE pointer if resource load was successful; otherwise 0. */ virtual LPCDLGTEMPLATE LoadDialogTemplate(UINT nIDResource); /** * @brief * Reads a Windows dialog template identified by pszResource into the memory. * @param pszTemplate A Windows dialog resource ID. * @return * HGLOBAL handle if resource load was successful; otherwise 0. * @details * Can be useful to load custom templates for common dialogs. * To access dialog template data use LockResource() API function. * @see * LoadDialogTemplate() */ virtual HGLOBAL LoadDialogTemplate2(LPCTSTR pszTemplate); /** * @brief * Reads a Windows HTML resource, identified by nIDResource, * into an existing CString object. * @param pText Output string pointer * @param nIDResource A Windows bitmap resource ID. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadHTML(CString* pText, UINT nIDResource); /** * @brief * Reads a Windows HTML resource, identified by pszResource, * into an existing CString object. * @param pText Output string pointer * @param pszResource A string specifying resource name. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadHTML(CString* pText, LPCTSTR pszResource); /** * @brief * The LoadIcon function loads an icon by locating its resource handle. * @param lpszID [in] Specifies the name of the icon to load. * @param iconSize [in] Specifies the width and height in pixels of the icon. * @return * If the function succeeds, the return value is the handle of the newly * loaded icon. If the function fails, the return value is NULL. To get * extended error information, call GetLastError. */ virtual HICON LoadIcon(LPCTSTR lpszID, CSize iconSize); /** * @brief * The LoadIcon function loads an icon by locating its resource handle. * @param nID [in] Specifies the resource id of the icon to load. * @param iconSize [in] Specifies the width and height in pixels of the icon. * @return * If the function succeeds, the return value is the handle of the newly * loaded icon. If the function fails, the return value is NULL. To get * extended error information, call GetLastError. */ virtual HICON LoadIcon(int nID, CSize iconSize); /** * @brief * Reads a Windows RT_XAML resource, identified by pszResource, * into an existing CString object. * @param xaml Output string * @param nIDResource Resource identifier. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadXAML(CString& xaml, UINT nIDResource); /** * @brief * Reads a Windows RT_SVG resource, identified by pszResource, * into an existing CString object. * @param svg Output string * @param nIDResource Resource identifier. * @return * Nonzero if resource load was successful; otherwise 0. */ virtual BOOL LoadSVG(CString& svg, UINT nIDResource); /** * @brief * The CreateIconFromResource function creates a new icon from the specified * modules icon resource. * @param hModule [in] Handle to the module whose executable file contains the * icon resource. * @param lpszID [in] Specifies the name of the icon to load. * @param iconSize [in] Specifies the width and height in pixels of the icon. * @return * If the function succeeds, the return value is a handle to the icon or * cursor. If the function fails, the return value is NULL. To get extended * error information, call GetLastError. */ virtual HICON CreateIconFromResource(HMODULE hModule, LPCTSTR lpszID, CSize iconSize); public: /** * @brief * Displays a message box on the screen * @param nIDPrompt A unique ID used to reference a string in the string table. * @param nType The style of the message box. * @return * See AfxMessageBox return values list */ int ShowMessageBox(UINT nIDPrompt, UINT nType = MB_OK); /** * @brief * Displays a message box on the screen * @param lpszText Message string to display. * @param nType The style of the message box. * @return * See AfxMessageBox return values list */ int ShowMessageBox(LPCTSTR lpszText, UINT nType = MB_OK); /** * @brief * This method free used resource. Call it in ExitInstance of your application if you use * external resource file. * @see * SetResourceFile */ virtual void Close(); public: /** * @brief * Call this method to set application defined Resource manager * @param pResourceManager Application defined Resource manager * @return * TRUE if successful; otherwise 0. */ BOOL SetResourceManager(CXTPResourceManager* pResourceManager); /** * @brief * Get current resource language ID. * @details * Value for default resource is (LANG_ENGLISH, SUBLANG_ENGLISH_US) * @return * Current resource language ID. */ virtual WORD GetResourcesLangID(); /** * @brief * Get current LCID. * @return * Current resource LCID. */ LCID GetActiveLCID(); /** * @brief * Call this static method to determine what language has resources * @param hResModule Resource instance * @return * Returns the language identifier value for the specified module as described * in https://docs.microsoft.com/en-us/windows/win32/intl/language-identifiers. */ static WORD AFX_CDECL GetResourceLanguage(HMODULE hResModule); /** * @brief * Checks if the resource name is an INT-resource. * @param lpName Resource name pointer. * @param bTryParse If TRUE, the function will try to parse a string * representation of the INT-resource, e.g.: '#123'. * @return * TRUE if the resource name is an INT-resource. */ static BOOL AFX_CDECL IsIntResource(LPCTSTR lpName, BOOL bTryParse = FALSE); /** * @brief * Makes a resource identifier from a provided INT-resource name. * @param lpName Resource name pointer. Can be either an INT-resource or a pointer * string representation of an INT-resource, e.g.: '#123'. * @return * A resource identifier value, or 0 if the INT-resource name is invalid. */ static WORD AFX_CDECL MakeIntResource(LPCTSTR lpName); public: void SetResourceLanguage(WORD wLanguage); int LoadLocaleString(HINSTANCE hInstance, UINT nID, CString& rString) const; int GetMenuLocaleString(CMenu* pMenu, UINT nIDItem, CString& rString, UINT nFlags) const; static XTP_RESOURCEMANAGER_LANGINFO* AFX_CDECL GetLanguageInfo(UINT nLangId); XTP_RESOURCEMANAGER_LANGINFO* GetLanguageInfo() const; static void AFX_CDECL AssertValid(BOOL bValid); public: /** * @brief * Call this member to use the default font charset defined in resource files. * @param bResourceCharset TRUE to use the font charset defined in * translated .dll and .xml files. */ void SetResourceFontCharset(BOOL bResourceCharset = TRUE); /** * @brief * Call this member to get the default charset for a LOGFONT structure. * * @return The default charset for a LOGFONT structure. */ BYTE GetFontCharset() const; /** * @brief * Call this member to set the default font charset. * @param nFontCharset Default font charset that will be used in the whole library. */ void SetFontCharset(BYTE nFontCharset); public: class _XTP_EXT_CLASS CXTPW2A { public: CXTPW2A(LPCWSTR lpwstr, int nLen = -1, UINT nCodePage = 0); ~CXTPW2A(); operator LPCSTR() const; int GetLength() const; protected: LPSTR m_lpsrt; int m_nLen; }; class _XTP_EXT_CLASS CXTPA2W { public: CXTPA2W(LPCSTR lpstr, int nLen = -1, UINT nCodePage = 0); ~CXTPA2W(); operator LPCWSTR() const; int GetLength() const; protected: LPWSTR m_lpwsrt; int m_nLen; }; protected: /** * @brief * This method checks the resource identifier of loaded resources. */ virtual void UpdateResourcesLangID(); /** * @brief * This method is called to notify the developer that resource load failed. * @return * TRUE if loading has failed, otherwise FALSE. */ BOOL OnLoadingFailed(); /** @cond */ _XTP_DEPRECATED_IN_FAVOR(OnLoadingFailed) BOOL LoadFailed() { return OnLoadingFailed(); } /** @endcond */ public: static CProcessLocal m_managerInstance; /**< Manager instance. */ protected: BOOL m_bValid; /**< TRUE if Resource manager is valid. */ HMODULE m_hResourceFile; /**< Resource file handle. */ XTP_RESOURCEMANAGER_LANGINFO* m_pLanguageInfo; /**< Resource language. */ static HMODULE m_hModXTResource; /**< Handle to toolkit resource. */ BOOL m_bResourceCharset; BYTE m_nSystemCharset; /** @cond */ static BOOL CALLBACK EnumResTypeProc(HMODULE hModule, LPTSTR lpszType, LONG_PTR lParam); static BOOL CALLBACK EnumResNameProc(HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam); static BOOL CALLBACK EnumResLangProc(HMODULE hModule, LPCTSTR lpszType, LPCTSTR lpszName, WORD wIDLanguage, LONG_PTR lParam); /** @endcond */ }; /** * @brief * Call this member to safely return an instance handle of the * current application. * @return * An HINSTANCE to the current instance of the application if * available. Otherwise returns a handle to the file used to * create the calling process (.exe file). */ AFX_INLINE HMODULE XTPGetInstanceHandle() { return (afxCurrentInstanceHandle == NULL) ? ::GetModuleHandle(NULL) : afxCurrentInstanceHandle; } # ifndef _XTP_EXCLUDE_XML class CXTPPropExchangeXMLNode; class CXTPPropExchange; /** * @brief * Class used to managed toolkit resources from an XML file. */ class _XTP_EXT_CLASS CXTPResourceManagerXML : public CXTPResourceManager { private: struct MENUITEMTEMPLATEINFO; struct MENUTEMPLATEINFO; struct DLGTEMPLATEINFO; struct DLGITEMTEMPLATEINFO; struct DLGTEMPLATEEX; struct DLGITEMTEMPLATEEX; public: /** * @brief * Constructs a CXTPResourceManagerXML object. * @param lpszFileName .XML file path. */ CXTPResourceManagerXML(LPCTSTR lpszFileName); /** * @brief * Constructs a CXTPResourceManagerXML object. * @param pResourceRoot Base XML Node. */ CXTPResourceManagerXML(CXTPPropExchangeXMLNode* pResourceRoot); /** * @brief * Destroys a CXTPResourceManagerXML object, handles cleanup and deallocation. */ ~CXTPResourceManagerXML(); public: using CXTPResourceManager::LoadString; /** * @brief * Reads a Windows string resource, specified by nID, into an * existing CString object. * @param pString Output string pointer. * @param nIDResource A Windows string resource ID. * @return * TRUE if successful, otherwise FALSE. */ virtual BOOL LoadString(CString* pString, UINT nIDResource); /** * @brief * Reads a Windows menu resource, specified by nID, into an * existing CMenu object. * @param lpMenu Output menu pointer. * @param nIDResource A Windows menu resource ID. * @return * TRUE if successful, otherwise FALSE. */ virtual BOOL LoadMenu(CMenu* lpMenu, UINT nIDResource); /** * @brief * Reads a Windows dialog resource, specified by pszResource. * @param nIDResource A Windows dialog resource ID. * @return * LPCDLGTEMPLATE pointer if resource load was successful, otherwise 0. */ virtual LPCDLGTEMPLATE LoadDialogTemplate(UINT nIDResource); /** * @brief * This method frees used resources. Call it within ExitInstance in your * application if you use an external resource file. * @see * SetResourceFile */ virtual void Close(); protected: /** * @brief * This method checks the resource identifier of loaded resources. */ virtual void UpdateResourcesLangID(); private: BOOL CreateMenu(CMenu& menu, CXTPPropExchange* pPX); void AddMenuItems(CXTPPropExchange* pPX, MENUTEMPLATEINFO* pItems); LPDLGTEMPLATE CreateDialogTemplate(CXTPPropExchange* pPX); LPDLGTEMPLATE CreateDialogTemplate(DLGTEMPLATEINFO& dlgTemplate); private: CXTPPropExchangeXMLNode* m_pResourceRoot; CMap m_mapDialogs; }; # endif /** * @brief * Call this function to access CXTPResourceManager members. * Since this class is designed as a single instance object, you can * only access version info through this method. You cannot * directly instantiate an object of type CXTPResourceManager. * * Example: *
 * XTPResourceManager()->LoadString(&m_strNoFieldsAvailable,
 * XTP_IDS_GRID_NOFIELDSAVAILABLE);
 * 
* @return A global resource manager object pointer. */ _XTP_EXT_CLASS CXTPResourceManager* AFX_CDECL XTPResourceManager(); /** @cond */ AFX_INLINE WORD CXTPResourceManager::GetResourcesLangID() { return m_pLanguageInfo->wLanguage; } AFX_INLINE LCID CXTPResourceManager::GetActiveLCID() { return MAKELCID(GetResourcesLangID(), SORT_DEFAULT); } AFX_INLINE XTP_RESOURCEMANAGER_LANGINFO* CXTPResourceManager::GetLanguageInfo() const { return m_pLanguageInfo; } AFX_INLINE CString CXTPResourceManager::LoadString(UINT nIDResource, BOOL* pbError) { CString str; BOOL bRes = LoadString(&str, nIDResource); _ASSERTE(bRes); if (pbError) *pbError = !bRes; return str; } AFX_INLINE void CXTPResourceManager::SetResourceFontCharset(BOOL bResourceCharset) { m_bResourceCharset = bResourceCharset; } AFX_INLINE void CXTPResourceManager::SetFontCharset(BYTE nFontCharset) { m_bResourceCharset = FALSE; m_nSystemCharset = nFontCharset; } /** @endcond */ /** * @brief * Obtains a default resource type name depending on the file extension * specified by the full path to the resource. * @param lpszPath Full path to the resource. * @return * A pointer to a string or integer resource that indicates a default resource * type for the resource specified, or NULL if no association is found. */ _XTP_EXT_CLASS LPCTSTR AFX_CDECL XTPGetDefaultResourceTypeFromPath(LPCTSTR lpszPath); /** * @brief * Tries to find a resource type name in a provided module by looking up * through all resource directories for a default culture. * The first matching resource type is then returned. * @param hModule Module handle in which resource lookup should be done. * @param lpszName Resource name or INT-resource value to find type name for. * @return * Resource type name for the first match for the name provided, * or an empty string if no resource type is found. */ _XTP_EXT_CLASS CString AFX_CDECL XTPFindResourceType(HMODULE hModule, LPCTSTR lpszName); # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" /** @cond */ #endif // !defined(__XTPRESOURCEMANAGER_H__) /** @endcond */