/** * @file XTPShortcutManager.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(__XTPSHORTCUTMANAGER_H__) # define __XTPSHORTCUTMANAGER_H__ /** @endcond */ # if _MSC_VER >= 1000 # pragma once # endif // _MSC_VER >= 1000 # include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" class CXTPPropExchange; class CXTPCommandBars; class CXTPShortcutManager; struct XTP_SHORTCUTMANAGER_ACCEL { struct SHORTCUTACCEL { BYTE fVirt; /**< Specifies the accelerator behavior. */ WORD key; /**< Specifies the accelerator key. */ }; int cmd; /**< Specifies the accelerator identifier. */ SHORTCUTACCEL key[2]; /**< Double key combination. */ }; /** * @brief * CXTPShortcutManagerAccel represents an accelerator key used * in an accelerator table. */ class _XTP_EXT_CLASS CXTPShortcutManagerAccel : public CXTPCmdTarget , public XTP_SHORTCUTMANAGER_ACCEL { public: /** * @brief * Constructs a CXTPShortcutManagerAccel object. */ CXTPShortcutManagerAccel(); CXTPShortcutManagerAccel(ACCEL* pAccel); CXTPShortcutManagerAccel(const XTP_SHORTCUTMANAGER_ACCEL& accel); CXTPShortcutManagerAccel(const CXTPShortcutManagerAccel& accel); public: const CXTPShortcutManagerAccel& operator=(const CXTPShortcutManagerAccel& accel); # ifdef _XTP_COMMANDBARS_ACTIVEX /** @cond */ protected: DECLARE_DISPATCH_MAP() DECLARE_OLETYPELIB_EX(CXTPShortcutManagerAccel); DECLARE_INTERFACE_MAP(); BSTR OleGetShortcutText(); void OleSetShortcutText(LPCTSTR lpszShortcut); /** @endcond */ # endif public: CXTPShortcutManager* m_pManager; }; /** * @brief * CXTPShortcutManagerAccelTable represents an accelerator table for * the CXTPShortcutManager class. */ class _XTP_EXT_CLASS CXTPShortcutManagerAccelTable { public: /** * @brief * Constructs a CXTPShortcutManagerAccelTable object. * @param pManager Shortcut manager pointer */ CXTPShortcutManagerAccelTable(CXTPShortcutManager* pManager); /** * @brief * Destroys a CXTPShortcutManagerAccelTable object, handles * cleanup and deallocation. */ ~CXTPShortcutManagerAccelTable(); public: /** * @brief * Determines the count of accels in the table. * @return * Number of accels in the table. */ int GetCount() const; /** * @brief * Retrieves an accel by its index. * @param nIndex Index of the accel to retrieve. * @return * Pointer to a CXTPShortcutManagerAccel object. */ CXTPShortcutManagerAccel* GetAt(int nIndex) const; /** * @brief * Adds a new accelerator to the table. * @param accel Pointer to the accel to add. */ void Add(const XTP_SHORTCUTMANAGER_ACCEL& accel); /** * @brief * Call this method to create an array of all the elements in the table. * @return * Pointer to a XTP_SHORTCUTMANAGER_ACCEL array. */ XTP_SHORTCUTMANAGER_ACCEL* CopyAccels() const; public: /** * @brief * Call this method to copy accels. * @param hAccelTable Windows HACCEL structure. */ void CopyAccelTable(HACCEL hAccelTable); /** * @brief * Call this method to copy accels * @param pAccelTable Another accel table to copy from. */ void CopyAccelTable(CXTPShortcutManagerAccelTable* pAccelTable); /** * @brief * Call this method to copy accels. * @param lpAccel Array of ACCEL structure. * @param nSize Count of elements in lpAccel. */ void CopyAccelTable(LPACCEL lpAccel, int nSize); public: /** * @brief * Removes all elements in the table. */ void RemoveAll(); /** * @brief * Call this method to remove an accel by its index. * @param nIndex Index of the accel to remove. */ void RemoveAt(int nIndex); protected: CArray m_arrAccels; /**< Array of accels. */ CXTPShortcutManager* m_pManager; /**< Parent manager. */ }; /** * @brief * CXTPShortcutManager is a standalone class used to manipulate accelerators of a frame. */ class _XTP_EXT_CLASS CXTPShortcutManager : public CXTPCmdTarget { public: /** @cond */ class _XTP_EXT_CLASS CKeyNameText { public: CKeyNameText(); CString Translate(UINT nKey); void SetAt(UINT uiVirtKey, LPCTSTR lpszKeyNameText); WORD Parse(LPCTSTR lpszKey); protected: CMap m_mapVirtualKeys; }; class _XTP_EXT_CLASS CKeyHelper { public: /** * @brief * Constructs a CKeyHelper object. */ CKeyHelper(const CXTPShortcutManagerAccel* lpAccel, CXTPShortcutManager* pManager = NULL); /** * @brief * Destroys a CKeyHelper object, handles cleanup and deallocation */ virtual ~CKeyHelper(); public: void Format(CString& str) const; CString Format(const CXTPShortcutManagerAccel::SHORTCUTACCEL* pAccel) const; int Priority(); static BOOL AFX_CDECL EqualAccels(const CXTPShortcutManagerAccel* pFirst, const CXTPShortcutManagerAccel* pSecond) { return ((pFirst->key[0].fVirt | FNOINVERT) == (pSecond->key[0].fVirt | FNOINVERT)) && (pFirst->key[0].key == pSecond->key[0].key) && ((pFirst->key[1].fVirt | FNOINVERT) == (pSecond->key[1].fVirt | FNOINVERT)) && (pFirst->key[1].key == pSecond->key[1].key); } CXTPShortcutManager* GetShortcutManager() const { return m_pManager; } /** * @brief * The GetKeyNameText function retrieves a string that represents the name of a key. * @param uiVirtKey virtual-key of accelerator. */ static CString AFX_CDECL GetLocalKeyNameText(UINT uiVirtKey); protected: void AddVirtKeyStr(CString& str, UINT uiVirtKey, BOOL bLast = FALSE) const; protected: const CXTPShortcutManagerAccel* m_lpAccel; CXTPShortcutManager* m_pManager; public: BOOL m_bAllowLocaleKey; }; # ifdef _XTP_INCLUDE_CONTROLS class _XTP_EXT_CLASS CKeyAssign : public CXTPEdit # else class _XTP_EXT_CLASS CKeyAssign : public CEdit # endif { public: /** * @brief * Constructs a CKeyAssign object. */ CKeyAssign(CXTPShortcutManager* pManager = NULL); /** * @brief * Destroys a CKeyAssign object, handles cleanup and deallocation */ virtual ~CKeyAssign(); public: BOOL IsKeyDefined() const; CXTPShortcutManagerAccel* GetAccel(); void SetAccel(CXTPShortcutManagerAccel* pAccel); void ResetKey(); protected: void SetAccelFlag(int nKey, BYTE bFlag, BOOL bOn); virtual BOOL PreTranslateMessage(MSG* pMsg); BOOL TranslateDoubleKeyShortcutsMessage(MSG* pMsg); BOOL TranslateSingleKeyShortcutsMessage(MSG* pMsg); public: BOOL m_bExtendedOnly; BOOL m_bAllowDoubleKeyShortcuts; protected: int m_nKeyDefined; CXTPShortcutManagerAccel m_accel; CKeyHelper m_keyHelper; }; /** @endcond */ /** * @brief * Constructs a CXTPShortcutManager object. * @param pCommandBars Owner CommandBars object. */ CXTPShortcutManager(CXTPCommandBars* pCommandBars); /** * @brief * Destroys a CXTPShortcutManager object, handles cleanup and deallocation. */ ~CXTPShortcutManager(); public: /** * @brief * Retrieves the shortcut text for a specified command. * @param nCmd Command with the shortcut text to retrieve. * @param strShortcut Shortcut text reference. * @return * TRUE if the shortcut was found. */ virtual BOOL FindDefaultAccelerator(int nCmd, CString& strShortcut); /** * @brief * Retrieves the default shortcut for a specified command. * @param nCmd Command with the accel to retrieve. * @param strShortcut Shortcut text reference. * @return * TRUE if the command was found. */ BOOL FindDefaultFrameAccelerator(int nCmd, CString& strShortcut); /** * @brief * This method is called to retrieve a text presentation of ACCEL. * You can override this method. * @param lpAccel Accelerator to format. * @param pPriority Returns the priority of the accelerator to be used to determine * what accelerator is used as the default. * @return Retrieves a text presentation of ACCEL */ virtual CString Format(CXTPShortcutManagerAccel* lpAccel, int* pPriority); /** * @brief * This function retrieves a CString object that represents the name of a key. * Override this member function to provide additional functionality. * @param uiVirtKey Virtual-key of accelerator. * @return * A CString object that represents the name of a key. */ virtual CString GetKeyNameText(UINT uiVirtKey); /** * @brief * The OnPreviewEditKey function is called to determine if a * user-defined shortcut is allowed. * @param pAccel User pressed key. * @return * TRUE if the key is allowed. */ virtual BOOL OnPreviewEditKey(CXTPShortcutManagerAccel* pAccel); /** * @brief * This method is called before the user adds a new shortcut. * @param pAccel New shortcut to add. * @return TRUE if handling has been cancelled, FALSE otherwise. * @see * OnBeforeRemove */ virtual BOOL OnBeforeAdd(CXTPShortcutManagerAccel* pAccel); /** * @brief * This method is called before the user removes a shortcut. * @param pAccel Shortcut to be removed. * @return TRUE if handling has been cancelled, FALSE otherwise. * @see * OnBeforeAdd */ virtual BOOL OnBeforeRemove(CXTPShortcutManagerAccel* pAccel); public: /** * @brief * Call this member to localize the name text of a shortcut key. * @param uiVirtKey Virtual-key of accelerator. * @param lpszKeyNameText New text that will be associated with the virtual-key. * * Example: *
pCommandBars->GetShortcutManager()->SetKeyNameText(VK_CONTROL, _T("Strg"));
*/ void SetKeyNameText(UINT uiVirtKey, LPCTSTR lpszKeyNameText); /** * @brief * Call this member to use system localized shortcuts. * @param bSystemKey TRUE to allow the system to localize shortcuts instead * of using a predefined shortcuts table. * * Example: *
pCommandBars->GetShortcutManager()->UseSystemKeyNameText(TRUE);
*/ void UseSystemKeyNameText(BOOL bSystemKey); /** * @brief * This method is called to disable/enable shortcuts usage. * @param bDisable TRUE to disable shortcuts, FALSE to enable shortcuts. */ virtual void DisableShortcuts(BOOL bDisable); /** * @brief * The TranslateAccelerator function processes accelerator keys for menu commands. * @param lpMsg Pointer to an MSG structure that contains message information. * @return * Nonzero if the function is successful. FALSE if the function fails. */ virtual BOOL TranslateAccelerator(LPMSG lpMsg); public: /** * @brief * Call this function to save the accelerators to the registry * or .INI file. * @param lpszProfileName Pointer to a null-terminated string that * specifies the name of a section in the * initialization file or a key in the Windows * registry where state information is stored. */ void SaveShortcuts(LPCTSTR lpszProfileName); /** * @brief * Call this function to retrieve accelerators from the registry * or .INI file. * @param lpszProfileName Pointer to a null-terminated string that * specifies the name of a section in the * initialization file or a key in the Windows * registry where state information is stored. */ void LoadShortcuts(LPCTSTR lpszProfileName); /** * @brief * Either reads this object from or writes this object to an archive. * @param pPX A CXTPPropExchange object to serialize to or from. */ virtual void DoPropExchange(CXTPPropExchange* pPX); /** * @brief * Call this member to create original accelerators. * @param nIDResource Resource identifier of Accelerator table. */ void SetAccelerators(UINT nIDResource); /** * @brief * Call this member to set the accelerator table. * @param hAccelTable New accelerators to be set. */ void SetDefaultAccelerator(HACCEL hAccelTable); /** * @brief * Call this member to get the accelerator table. * @return * Accelerator table handle. */ virtual CXTPShortcutManagerAccelTable* GetDefaultAccelerator() const; /** * @brief * Call this member to get original accelerators. * Accelerator table handle. * @return Returns original accelerators */ CXTPShortcutManagerAccelTable* GetOriginalAccelerator() const; /** * @brief * Call this member to reset shortcuts. */ virtual void Reset(); public: /** @cond */ void CreateOriginalAccelTable(); static TCHAR AFX_CDECL ToUpper(TCHAR vkTCHAR); static BOOL AFX_CDECL CompareAccelKey(TCHAR chAccel, UINT wParam); static UINT AFX_CDECL AccelKeyToVirtualKey(TCHAR chAccel); static int AFX_CDECL FindAccelPos(LPCTSTR lpszString); void UpdateAcellTable(LPACCEL lpAccel, int nSize); void UpdateAcellTable(XTP_SHORTCUTMANAGER_ACCEL* lpAccel, int nSize); void SerializeShortcuts(CArchive& ar); void AddShortcut(long fVirt, long key, long cmd); void AddShortcut(long cmd, LPCTSTR lpszKey); BOOL GetShortcut(int ID, CXTPShortcutManagerAccel* pAccel); BOOL ParseShortcut(CString strShortcut, CXTPShortcutManagerAccel* accel); private: BOOL ParseShortcut(CString strShortcutKey, BYTE& fVirt, WORD& key) const; BOOL ParseShortcutVirtKey(CString& strShortcutKey, int nAccel) const; BOOL IsAccelMessage(CXTPShortcutManagerAccel::SHORTCUTACCEL& accel, int nKeyState, LPMSG lpMsg) const; int GetAccelKeyState() const; /** @endcond */ # ifdef _XTP_COMMANDBARS_ACTIVEX /** @cond */ protected: DECLARE_DISPATCH_MAP() DECLARE_OLETYPELIB_EX(CXTPShortcutManager); DECLARE_CONNECTION_EX(CXTPShortcutManager) DECLARE_INTERFACE_MAP(); DECLARE_ENUM_VARIANT(CXTPShortcutManager); afx_msg void OleAdd(long fVirt, long key, long cmd); afx_msg void OleAddShortcut(long cmd, LPCTSTR lpszShortcut); afx_msg BOOL OleGetItemCount(); afx_msg LPDISPATCH OleGetItem(long Index); afx_msg void OleDeleteAll(); afx_msg void OleDelete(long nID); afx_msg BOOL OleGetEnabled(); afx_msg void OleSetEnahbled(BOOL Eanbled); afx_msg BOOL OleGetEnableCustomShortcutText(); afx_msg void OleSetEnableCustomShortcutText(BOOL Eanbled); public: afx_msg BOOL OleBeforeTranslate(long cmd); LPDISPATCH OleFindShortcut(long nCmd); enum { dispidAdd = 1L, }; /** @endcond */ # endif public: BOOL m_bAllowEscapeShortcut; /**< TRUE to allow escape to be used as a shortcut. */ BOOL m_bUseSystemKeyNameText; /**< TRUE to use localized shortcuts. */ BOOL m_bAllowDoubleKeyShortcuts; /**< TRUE to allow double key shortcuts(ex: Ctrl+K, Ctrl+V). */ int m_nDisableShortcuts; /**< Number of shortcuts that have been disabled. */ BOOL m_bDisableOnCapture; /**< Specifies whether the commandbars will process keybindings while another control is capturing input. */ BOOL m_nEnableCustomShortcutText; /**< TRUE to allow adding custom text to be displayed as a shortcut. */ protected: CXTPShortcutManagerAccelTable* m_pAccelTable; /**< Accelerator table. */ CXTPShortcutManagerAccelTable* m_pOriginalAccelTable; /**< Original accelerator table. */ CXTPCommandBars* m_pCommandBars; /**< Parent CommandBars pointer. */ CKeyNameText* m_pKeyNameText; /**< KeyNameText map. */ friend class CKeyHelper; }; AFX_INLINE CXTPShortcutManagerAccelTable* CXTPShortcutManager::GetDefaultAccelerator() const { return m_pAccelTable; } AFX_INLINE CXTPShortcutManagerAccelTable* CXTPShortcutManager::GetOriginalAccelerator() const { return m_pOriginalAccelTable; } AFX_INLINE void CXTPShortcutManager::UseSystemKeyNameText(BOOL bSystemKey) { m_bUseSystemKeyNameText = bSystemKey; } /** @cond */ # include "Common/Base/Diagnostic/XTPEnableNoisyWarnings.h" #endif //#if !defined(__XTPSHORTCUTMANAGER_H__) /** @endcond */