/** * @file XTPCustomizePageKeyboard.cpp * * @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 * */ #include "stdafx.h" #include "Common/XTPVC80Helpers.h" #include "Common/XTPTypeId.h" #include "Common/XTPCasting.h" #include "Common/XTPFramework.h" #include "Common/XTPSystemHelpers.h" #include "Common/XTPSynchro.h" #include "Common/XTPApplication.h" #include "Common/XTPSingleton.h" #include "Common/XTPGdiObjects.h" #include "Common/XTPResourceManager.h" #include "Common/XTPDrawHelpers.h" #include "Common/XTPHookManager.h" #include "Common/XTPColorManager.h" #include "Common/XTPResourceImage.h" #include "Common/XTPMaskEditT.h" #include "Common/ScrollBar/XTPScrollInfo.h" #include "Common/ScrollBar/XTPScrollBase.h" #include "Common/ScrollBar/XTPScrollBarCtrl.h" #include "Common/ScrollBar/XTPScrollable.h" #include "Controls/Resize/XTPResizeRect.h" #include "Controls/Resize/XTPResizePoint.h" #include "Controls/Resize/XTPResize.h" #include "Controls/Resize/XTPResizeDialog.h" #include "Controls/Util/XTPControlTheme.h" #include "Controls/Button/XTPButton.h" #include "Controls/Edit/XTPEdit.h" #include "Controls/Combo/XTPComboBox.h" #include "Controls/ListBox/XTPListBox.h" #include "Controls/Scroll/XTPScrollBar.h" #include "CommandBars/Resource.h" #include "CommandBars/XTPCommandBarsDefines.h" #include "CommandBars/XTPShortcutManager.h" #include "CommandBars/XTPPaintManager.h" #include "CommandBars/XTPControls.h" #include "CommandBars/XTPControl.h" #include "CommandBars/XTPControlEdit.h" #include "CommandBars/XTPControlButton.h" #include "CommandBars/XTPControlPopup.h" #include "CommandBars/XTPCommandBar.h" #include "CommandBars/XTPCommandBars.h" #include "CommandBars/XTPPopupBar.h" #include "CommandBars/XTPToolBar.h" #include "CommandBars/XTPMenuBar.h" #include "CommandBars/XTPMenuTitleBar.h" #include "Controls/Dialog/XTPDialogBase.h" #include "Controls/Dialog/XTPPropertyPage.h" #include "Controls/Dialog/XTPPropertySheet.h" #include "CommandBars/Customize/XTPCustomizeSheet.h" #include "CommandBars/Customize/XTPCustomizePageCommands.h" #include "CommandBars/Customize/XTPCustomizePageKeyboard.h" #include "Common/Base/Diagnostic/XTPDisableNoisyWarnings.h" #ifdef _DEBUG # define new DEBUG_NEW # undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CXTPCustomizeKeyboardPage property page CXTPCustomizeKeyboardPage::CXTPCustomizeKeyboardPage(CXTPCustomizeSheet* pSheet) : CXTPPropertyPage(CXTPCustomizeKeyboardPage::IDD) , m_editShortcutKey(pSheet->GetCommandBars()->GetShortcutManager()) { //{{AFX_DATA_INIT(CXTPCustomizeKeyboardPage) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT m_pSheet = pSheet; m_strDesc = _T(""); m_iCategory = 0; _ASSERTE(pSheet->GetCommandBars()->GetShortcutManager()->GetOriginalAccelerator()); } CXTPCustomizeKeyboardPage::~CXTPCustomizeKeyboardPage() { for (int i = 0; i < m_arrCategories.GetSize(); i++) { delete m_arrCategories[i]; } } void CXTPCustomizeKeyboardPage::DoDataExchange(CDataExchange* pDX) { CXTPPropertyPage::DoDataExchange(pDX); //{{AFX_DATA_MAP(CXTPCustomizeKeyboardPage) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP DDX_Control(pDX, XTP_IDC_BTN_ASSIGN, m_btnAssign); DDX_Control(pDX, XTP_IDC_BTN_REMOVE, m_btnRemove); DDX_Control(pDX, XTP_IDC_BTN_ACCEL_RESET, m_btnResetAll); DDX_Control(pDX, XTP_IDC_EDIT_SHORTCUTKEY, m_editShortcutKey); #ifdef _XTP_INCLUDE_CONTROLS DDX_Control(pDX, XTP_IDC_LBOX_KEYS_EX, m_lboxKeys); DDX_Control(pDX, XTP_IDC_LBOX_ACCEL_COMMANDS_EX, m_lboxCommands); #else DDX_Control(pDX, XTP_IDC_LBOX_KEYS, m_lboxKeys); DDX_Control(pDX, XTP_IDC_LBOX_ACCEL_COMMANDS, m_lboxCommands); #endif DDX_Control(pDX, XTP_IDC_COMBO_CATEGORY, m_comboCategory); DDX_Text(pDX, XTP_IDC_TXT_DESC_OVERVIEW, m_strDesc); DDX_CBIndex(pDX, XTP_IDC_COMBO_CATEGORY, m_iCategory); } #include "Common/Base/Diagnostic/XTPBeginAfxMap.h" BEGIN_MESSAGE_MAP(CXTPCustomizeKeyboardPage, CXTPPropertyPage) //{{AFX_MSG_MAP(CXTPCustomizeKeyboardPage) // NOTE: the ClassWizard will add message map macros here //}}AFX_MSG_MAP ON_CBN_SELCHANGE(XTP_IDC_COMBO_CATEGORY, OnSelchangeCategory) #ifdef _XTP_INCLUDE_CONTROLS ON_LBN_SELCHANGE(XTP_IDC_LBOX_ACCEL_COMMANDS_EX, OnSelchangeCommands) ON_LBN_SELCHANGE(XTP_IDC_LBOX_KEYS_EX, OnSelchangeCurKeys) #else ON_LBN_SELCHANGE(XTP_IDC_LBOX_ACCEL_COMMANDS, OnSelchangeCommands) ON_LBN_SELCHANGE(XTP_IDC_LBOX_KEYS, OnSelchangeCurKeys) #endif ON_BN_CLICKED(XTP_IDC_BTN_ASSIGN, OnAssign) ON_BN_CLICKED(XTP_IDC_BTN_ACCEL_RESET, OnResetAll) ON_BN_CLICKED(XTP_IDC_BTN_REMOVE, OnRemove) ON_EN_CHANGE(XTP_IDC_EDIT_SHORTCUTKEY, OnChangeShortcutKey) ON_WM_CTLCOLOR() END_MESSAGE_MAP() #include "Common/Base/Diagnostic/XTPEndAfxMap.h" XTP_COMMANDBARS_CATEGORYINFO* CXTPCustomizeKeyboardPage::FindCategory(LPCTSTR strCategory) const { for (int i = 0; i < m_arrCategories.GetSize(); i++) { if (m_arrCategories[i]->strCategory.Compare(strCategory) == 0) return m_arrCategories[i]; } return NULL; } XTP_COMMANDBARS_CATEGORYINFO* CXTPCustomizeKeyboardPage::GetCategoryInfo(int nIndex) const { if (nIndex >= 0 && nIndex < m_arrCategories.GetSize()) return m_arrCategories[nIndex]; return NULL; } CXTPControls* CXTPCustomizeKeyboardPage::InsertCategory(LPCTSTR strCategory, int nIndex) { XTP_COMMANDBARS_CATEGORYINFO* pInfo = FindCategory(strCategory); if (!pInfo) { pInfo = new XTP_COMMANDBARS_CATEGORYINFO(strCategory, m_pSheet->GetCommandBars()); m_arrCategories.InsertAt(nIndex == -1 ? m_arrCategories.GetSize() : nIndex, pInfo); } return pInfo->pControls; } BOOL CXTPCustomizeKeyboardPage::AddCategory(LPCTSTR strCategory, CMenu* pMenu, BOOL bListSubItems) { CXTPControls* pCategoryControls = InsertCategory(strCategory); int nCount = pMenu->GetMenuItemCount(); for (int i = 0; i < nCount; i++) { if (pMenu->GetMenuItemID(i) > 0) { CXTPControlPopup* pControl = DYNAMIC_DOWNCAST(CXTPControlPopup, pCategoryControls->AddMenuItem(pMenu, i)); if (pControl && bListSubItems) { CString strSubCategory = pControl->GetCaption() + _T(" | "); CXTPControls* pControls = pControl->GetCommandBar()->GetControls(); for (int j = 0; j < pControls->GetCount(); j++) { CXTPControl* pSubControl = pCategoryControls->AddClone(pControls->GetAt(j)); pSubControl->SetCaption(strSubCategory + pSubControl->GetCaption()); } } } } return TRUE; } BOOL CXTPCustomizeKeyboardPage::AddCategories(UINT nIDResource, BOOL bListSubItems) { CMenu menu; if (!XTPResourceManager()->LoadMenu(&menu, nIDResource)) return FALSE; int nCount = menu.GetMenuItemCount(); for (int i = 0; i < nCount; i++) { CString strCategory; if (XTPResourceManager()->GetMenuLocaleString(&menu, XTPToUInt(i), strCategory, MF_BYPOSITION) > 0) { CMenu* pMenu = menu.GetSubMenu(i); if (pMenu) { CXTPPaintManager::StripMnemonics(strCategory); int iPos = strCategory.Find(_T('\t')); if (iPos > 0) { strCategory.ReleaseBuffer(iPos); } if (!AddCategory(strCategory, pMenu, bListSubItems)) return FALSE; } } } return TRUE; } BOOL CXTPCustomizeKeyboardPage::AddCategories(CXTPControls* pControls) { for (int i = 0; i < pControls->GetCount(); i++) { CXTPControl* pControl = pControls->GetAt(i); CString strCategory = pControl->GetCategory(); if (!strCategory.IsEmpty() && pControl->GetID() > 0 && pControl->GetType() == xtpControlButton) { CXTPControls* pCategoryControls = InsertCategory(strCategory); pCategoryControls->AddClone(pControl); } } return TRUE; } ///////////////////////////////////////////////////////////////////////////// // CXTPCustomizeKeyboardPage message handlers BOOL CXTPCustomizeKeyboardPage::OnInitDialog() { CXTPPropertyPage::OnInitDialog(); // ModifyStyle(0, WS_CLIPCHILDREN); for (int i = 0; i < m_arrCategories.GetSize(); i++) { int nIndex = m_comboCategory.AddString(m_arrCategories[i]->strCategory); m_comboCategory.SetItemDataPtr(nIndex, m_arrCategories[i]->pControls); if (i == 0) m_comboCategory.SetCurSel(0); } CXTPCommandBars* pCommandBars = m_pSheet->GetCommandBars(); if (pCommandBars->IsLayoutRTL()) { m_lboxCommands.ModifyStyleEx(0, WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR | WS_EX_RTLREADING); m_comboCategory.ModifyStyleEx(0, WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR | WS_EX_RTLREADING); } m_lboxCommands.SetItemHeight(0, XTPToUInt(::GetSystemMetrics(SM_CYMENU))); OnSelchangeCategory(); // UpdateData(FALSE); SetResize(XTP_IDC_COMBO_CATEGORY, XTP_ANCHOR_TOPLEFT, XTP_ANCHOR_TOPCENTER); SetResize(XTP_IDC_TXT_ASSIGNMENTS, XTP_ANCHOR_TOPCENTER, XTP_ANCHOR_TOPRIGHT); SetResize(XTP_IDC_TXT_NEWKEY, XTP_ANCHOR_BOTTOMCENTER, XTP_ANCHOR_BOTTOMRIGHT); SetResize(XTP_IDC_EDIT_SHORTCUTKEY, XTP_ANCHOR_BOTTOMCENTER, XTP_ANCHOR_BOTTOMRIGHT); SetResize(XTP_IDC_BTN_ASSIGN, XTP_ANCHOR_TOPRIGHT, XTP_ANCHOR_TOPRIGHT); SetResize(XTP_IDC_BTN_ACCEL_RESET, XTP_ANCHOR_TOPRIGHT, XTP_ANCHOR_TOPRIGHT); SetResize(XTP_IDC_BTN_REMOVE, XTP_ANCHOR_TOPRIGHT, XTP_ANCHOR_TOPRIGHT); SetResize(XTP_IDC_TXT_DESC_TITLE, XTP_ANCHOR_BOTTOMLEFT, XTP_ANCHOR_BOTTOMRIGHT); SetResize(XTP_IDC_TXT_DESC_OVERVIEW, XTP_ANCHOR_BOTTOMLEFT, XTP_ANCHOR_BOTTOMRIGHT); #ifdef _XTP_INCLUDE_CONTROLS SetResize(XTP_IDC_LBOX_ACCEL_COMMANDS_EX, XTP_ANCHOR_TOPLEFT, XTP_ANCHOR_BOTTOMCENTER); SetResize(XTP_IDC_LBOX_KEYS_EX, XTP_ANCHOR_TOPCENTER, XTP_ANCHOR_BOTTOMRIGHT); #else SetResize(XTP_IDC_LBOX_ACCEL_COMMANDS, XTP_ANCHOR_TOPLEFT, XTP_ANCHOR_BOTTOMCENTER); SetResize(XTP_IDC_LBOX_KEYS, XTP_ANCHOR_TOPCENTER, XTP_ANCHOR_BOTTOMRIGHT); #endif #ifdef _XTP_INCLUDE_CONTROLS XTPControlTheme controlTheme = m_pSheet->GetTheme(); m_btnAssign.SetTheme(controlTheme); m_btnRemove.SetTheme(controlTheme); m_btnResetAll.SetTheme(controlTheme); m_lboxKeys.SetTheme(controlTheme); m_lboxCommands.SetTheme(controlTheme); m_comboCategory.SetTheme(controlTheme); m_editShortcutKey.SetTheme(controlTheme); m_clrText = XTPIniColor(_T("CommandBars.Toolbar"), _T("ButtonText"), GetXtremeColor(COLOR_WINDOWTEXT)); XTP_SAFE_CALL1(GetDlgItem(XTP_IDC_LBOX_ACCEL_COMMANDS), ShowWindow(SW_HIDE)); XTP_SAFE_CALL1(GetDlgItem(XTP_IDC_LBOX_KEYS), ShowWindow(SW_HIDE)); #else XTP_SAFE_CALL1(GetDlgItem(XTP_IDC_LBOX_ACCEL_COMMANDS_EX), ShowWindow(SW_HIDE)); XTP_SAFE_CALL1(GetDlgItem(XTP_IDC_LBOX_KEYS_EX), ShowWindow(SW_HIDE)); #endif return FALSE; } void CXTPCustomizeKeyboardPage::ReloadList() { int iIndex = m_comboCategory.GetCurSel(); if (iIndex == LB_ERR) return; CXTPControls* pControls = (CXTPControls*)m_comboCategory.GetItemDataPtr(iIndex); if (pControls == NULL) return; m_lboxCommands.ResetContent(); int iItem; for (iItem = 0; iItem < pControls->GetCount(); ++iItem) { CXTPControl* pControl = pControls->GetAt(iItem); int nID = pControl->GetID(); if (nID != 0 && pControl->GetType() != xtpControlPopup && pControl->GetType() != xtpControlButtonPopup) { if ((nID < ID_FILE_MRU_FIRST) || (nID > ID_FILE_MRU_LAST)) { CString str = pControl->GetCaption(); CXTPPaintManager::StripMnemonics(str); iIndex = m_lboxCommands.AddString(str); m_lboxCommands.SetItemDataPtr(iIndex, pControl); } } } if (m_lboxCommands.GetCount()) m_lboxCommands.SetCurSel(0); } void CXTPCustomizeKeyboardPage::OnChangeShortcutKey() { EnableControls(); } void CXTPCustomizeKeyboardPage::OnSelchangeCurKeys() { EnableControls(); } void CXTPCustomizeKeyboardPage::OnSelchangeCategory() { UpdateData(); ReloadList(); OnSelchangeCommands(); EnableControls(); } CXTPShortcutManagerAccelTable* CXTPCustomizeKeyboardPage::GetFrameAccelerator() const { return m_pSheet->GetCommandBars()->GetShortcutManager()->GetDefaultAccelerator(); } void CXTPCustomizeKeyboardPage::OnSelchangeCommands() { _ASSERTE(m_pSheet); if (!m_pSheet) return; m_lboxKeys.ResetContent(); int iIndex = m_lboxCommands.GetCurSel(); if (iIndex == -1) { return; } CXTPCommandBars* pCommandBars = m_pSheet->GetCommandBars(); CXTPControl* pControl = (CXTPControl*)m_lboxCommands.GetItemDataPtr(iIndex); _ASSERTE(pControl); if (!pControl) return; CXTPShortcutManagerAccelTable* pAccelTable = GetFrameAccelerator(); if (pAccelTable) { for (int i = 0; i < pAccelTable->GetCount(); i++) { CXTPShortcutManagerAccel* accel = pAccelTable->GetAt(i); if (accel->cmd == pControl->GetID()) { CString str = pCommandBars->GetShortcutManager()->Format(accel, NULL); int nIndex = m_lboxKeys.AddString(str); m_lboxKeys.SetItemData(nIndex, XTPToDWORD(i)); } } } m_strDesc = pControl->GetDescription(); UpdateData(FALSE); EnableControls(); } void CXTPCustomizeKeyboardPage::UpdateAcellTable(LPACCEL lpAccel, int nSize) { m_pSheet->GetCommandBars()->GetShortcutManager()->UpdateAcellTable(lpAccel, nSize); } void CXTPCustomizeKeyboardPage::OnAssign() { int iIndex = m_lboxCommands.GetCurSel(); if (iIndex == -1) return; CXTPControl* pControl = (CXTPControl*)m_lboxCommands.GetItemDataPtr(iIndex); if (!pControl) return; CXTPShortcutManagerAccel* pAccel = m_editShortcutKey.GetAccel(); _ASSERTE(pAccel); if (!pAccel) return; pAccel->cmd = pControl->GetID(); if (m_pSheet->GetCommandBars()->GetShortcutManager()->OnBeforeAdd(pAccel)) return; CXTPShortcutManagerAccelTable* pAccelTable = GetFrameAccelerator(); for (int i = 0; i < pAccelTable->GetCount(); i++) { CXTPShortcutManagerAccel* accel = pAccelTable->GetAt(i); if (CXTPShortcutManager::CKeyHelper::EqualAccels(accel, pAccel)) { if (XTPResourceManager()->ShowMessageBox(XTP_IDS_CONFIRM_REASSIGN, MB_ICONWARNING | MB_YESNO) != IDYES) { return; } pAccelTable->RemoveAt(i); break; } } pAccelTable->Add(*pAccel); OnSelchangeCommands(); m_editShortcutKey.ResetKey(); EnableControls(); } void CXTPCustomizeKeyboardPage::EnableControls() { CString strText; m_editShortcutKey.GetWindowText(strText); CWnd* pWnd = GetDlgItem(XTP_IDC_BTN_ASSIGN); if (pWnd) { pWnd->EnableWindow(!strText.IsEmpty() && m_editShortcutKey.IsKeyDefined()); } int iSel = m_lboxKeys.GetCurSel(); pWnd = GetDlgItem(XTP_IDC_BTN_REMOVE); if (pWnd) { pWnd->EnableWindow(iSel != LB_ERR); } } void CXTPCustomizeKeyboardPage::OnResetAll() { CXTPShortcutManager* pShortcutManager = m_pSheet->GetCommandBars()->GetShortcutManager(); CString strPrompt; CXTPResourceManager::AssertValid( XTPResourceManager()->LoadString(&strPrompt, XTP_IDS_CONFIRM_KEYBOARD)); if (m_pSheet->MessageBox(strPrompt, MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2) == IDYES) { pShortcutManager->Reset(); OnSelchangeCommands(); EnableControls(); } } void CXTPCustomizeKeyboardPage::OnRemove() { int iIndex = m_lboxKeys.GetCurSel(); if (iIndex == LB_ERR) return; CXTPShortcutManagerAccelTable* pAccelTable = GetFrameAccelerator(); if (!pAccelTable) return; int j = (int)m_lboxKeys.GetItemData(iIndex); _ASSERTE(j >= 0 && j < pAccelTable->GetCount()); if (j >= 0 && j < pAccelTable->GetCount()) { if (m_pSheet->GetCommandBars()->GetShortcutManager()->OnBeforeRemove(pAccelTable->GetAt(j)) == FALSE) { pAccelTable->RemoveAt(j); } } OnSelchangeCommands(); EnableControls(); } BOOL CXTPCustomizeKeyboardPage::OnSetActive() { UpdateData(FALSE); ReloadList(); EnableControls(); return CXTPPropertyPage::OnSetActive(); } HBRUSH CXTPCustomizeKeyboardPage::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hBrush = CXTPPropertyPage::OnCtlColor(pDC, pWnd, nCtlColor); #ifdef _XTP_INCLUDE_CONTROLS if (nCtlColor == CTLCOLOR_STATIC) { if (m_pSheet->GetTheme() != xtpControlThemeDefault) ::SetTextColor(pDC->GetSafeHdc(), m_clrText); } #endif return hBrush; }