📄 bcgpkeyboardmanager.cpp
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// This is a part of the BCGControlBar Library
// Copyright (C) 1998-2000 BCGSoft Ltd.
// All rights reserved.
//
// This source code can be used, distributed or modified
// only under terms and conditions
// of the accompanying license agreement.
//*******************************************************************************
// BCGPKeyboardManager.cpp: implementation of the CBCGPKeyboardManager class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "BCGPKeyboardManager.h"
#include "BCGPMultiDocTemplate.h"
#include "BCGPFrameWnd.h"
#include "BCGPMDIFrameWnd.h"
#include "BCGPOleIPFrameWnd.h"
#include "BCGPRegistry.h"
#include "BCGPKeyHelper.h"
#include "BCGPToolBar.h"
#include "RegPath.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define REG_SECTION_FMT _T("%sBCGKeyboard-%d")
#define REG_ENTRY_DATA _T("Accelerators")
CBCGPKeyboardManager* g_pKeyboardManager = NULL;
static const CString strKbProfile = _T("BCGPKeyboardManager");
LPACCEL CBCGPKeyboardManager::m_lpAccel = NULL;
LPACCEL CBCGPKeyboardManager::m_lpAccelDefault = NULL;
int CBCGPKeyboardManager::m_nAccelDefaultSize = 0;
int CBCGPKeyboardManager::m_nAccelSize = 0;
HACCEL CBCGPKeyboardManager::m_hAccelDefaultLast = NULL;
HACCEL CBCGPKeyboardManager::m_hAccelLast = NULL;
// a special struct that will cleanup automatically
class _KBD_TERM
{
public:
~_KBD_TERM()
{
CBCGPKeyboardManager::CleanUp ();
}
};
static const _KBD_TERM kbdTerm;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBCGPKeyboardManager::CBCGPKeyboardManager()
{
ASSERT (g_pKeyboardManager == NULL);
g_pKeyboardManager = this;
}
//******************************************************************
CBCGPKeyboardManager::~CBCGPKeyboardManager()
{
g_pKeyboardManager = NULL;
}
//******************************************************************
BOOL CBCGPKeyboardManager::UpdateAcellTable (CMultiDocTemplate* pTemplate,
LPACCEL lpAccel, int nSize,
CFrameWnd* pDefaultFrame)
{
ASSERT (lpAccel != NULL);
//--------------------------------
// Create a new accelerator table:
//--------------------------------
HACCEL hAccelNew = ::CreateAcceleratorTable(lpAccel, nSize);
if (hAccelNew == NULL)
{
TRACE(_T ("Can't create accelerator table!\n"));
return FALSE;
}
if (!UpdateAcellTable (pTemplate, hAccelNew, pDefaultFrame))
{
::DestroyAcceleratorTable (hAccelNew);
return FALSE;
}
return TRUE;
}
//******************************************************************
BOOL CBCGPKeyboardManager::UpdateAcellTable (CMultiDocTemplate* pTemplate,
HACCEL hAccelNew,
CFrameWnd* pDefaultFrame)
{
ASSERT (hAccelNew != NULL);
//-------------------------------------------------------------
// Find an existing accelerator table associated with template:
//-------------------------------------------------------------
HACCEL hAccelTable = NULL;
if (pTemplate != NULL)
{
ASSERT (pDefaultFrame == NULL);
ASSERT_VALID (pTemplate);
hAccelTable = pTemplate->m_hAccelTable;
ASSERT (hAccelTable != NULL);
pTemplate->m_hAccelTable = hAccelNew;
//--------------------------------------------------
// Walk trougth all template's documents and change
// frame's accelerator tables:
//--------------------------------------------------
for (POSITION pos = pTemplate->GetFirstDocPosition(); pos != NULL;)
{
CDocument* pDoc = pTemplate->GetNextDoc (pos);
ASSERT_VALID (pDoc);
for (POSITION posView = pDoc->GetFirstViewPosition();
posView != NULL;)
{
CView* pView = pDoc->GetNextView (posView);
ASSERT_VALID (pView);
CFrameWnd* pFrame = pView->GetParentFrame ();
ASSERT_VALID (pFrame);
if (pFrame->m_hAccelTable == hAccelTable)
{
pFrame->m_hAccelTable = hAccelNew;
}
}
}
}
else
{
if (pDefaultFrame == NULL)
{
pDefaultFrame = DYNAMIC_DOWNCAST (CFrameWnd, AfxGetMainWnd ());
}
if (pDefaultFrame != NULL)
{
hAccelTable = pDefaultFrame->m_hAccelTable;
pDefaultFrame->m_hAccelTable = hAccelNew;
}
}
if (hAccelTable == NULL)
{
TRACE(_T ("Accelerator table not found!\n"));
return FALSE;
}
::DestroyAcceleratorTable (hAccelTable);
return TRUE;
}
//************************************************************************************************
BOOL CBCGPKeyboardManager::SaveAccelaratorState (LPCTSTR lpszProfileName,
UINT uiResId, HACCEL hAccelTable)
{
ASSERT (hAccelTable != NULL);
CString strSection;
strSection.Format (REG_SECTION_FMT, lpszProfileName, uiResId);
CBCGPRegistrySP regSP;
CBCGPRegistry& reg = regSP.Create (FALSE, FALSE);
int nAccelSize = ::CopyAcceleratorTable (hAccelTable, NULL, 0);
if (nAccelSize == 0)
{
return FALSE;
}
if (!reg.CreateKey (strSection))
{
return FALSE;
}
LPACCEL lpAccel = new ACCEL [nAccelSize];
ASSERT (lpAccel != NULL);
::CopyAcceleratorTable (hAccelTable, lpAccel, nAccelSize);
reg.Write (REG_ENTRY_DATA, (LPBYTE) lpAccel, nAccelSize * sizeof (ACCEL));
delete lpAccel;
return TRUE;
}
//************************************************************************************************
BOOL CBCGPKeyboardManager::LoadAccelaratorState (LPCTSTR lpszProfileName,
UINT uiResId, HACCEL& hAccelTable)
{
ASSERT (hAccelTable == NULL);
CString strSection;
strSection.Format (REG_SECTION_FMT, lpszProfileName, uiResId);
CBCGPRegistrySP regSP;
CBCGPRegistry& reg = regSP.Create (FALSE, FALSE);
if (!reg.Open (strSection))
{
return FALSE;
}
UINT uiSize;
LPACCEL lpAccel;
if (reg.Read (REG_ENTRY_DATA, (LPBYTE*) &lpAccel, &uiSize))
{
int nAccelSize = uiSize / sizeof (ACCEL);
ASSERT (lpAccel != NULL);
for (int i = 0; i < nAccelSize; i ++)
{
if (!CBCGPToolBar::IsCommandPermitted (lpAccel [i].cmd))
{
lpAccel [i].cmd = 0;
}
}
hAccelTable = ::CreateAcceleratorTable(lpAccel, nAccelSize);
}
delete lpAccel;
return hAccelTable != NULL;
}
//************************************************************************************************
BOOL CBCGPKeyboardManager::LoadState (LPCTSTR lpszProfileName, CFrameWnd* pDefaultFrame)
{
CString strProfileName = ::BCGPGetRegPath (strKbProfile, lpszProfileName);
CDocManager* pDocManager = AfxGetApp ()->m_pDocManager;
if (pDocManager != NULL)
{
//---------------------------------------
// Walk all templates in the application:
//---------------------------------------
for (POSITION pos = pDocManager->GetFirstDocTemplatePosition (); pos != NULL;)
{
CBCGPMultiDocTemplate* pTemplate =
(CBCGPMultiDocTemplate*) pDocManager->GetNextDocTemplate (pos);
ASSERT_VALID (pTemplate);
ASSERT_KINDOF (CDocTemplate, pTemplate);
//-----------------------------------------------------
// We are interessing CMultiDocTemplate objects with
// the sahred menu only....
//-----------------------------------------------------
if (!pTemplate->IsKindOf (RUNTIME_CLASS (CMultiDocTemplate)) ||
pTemplate->m_hAccelTable == NULL)
{
continue;
}
UINT uiResId = pTemplate->GetResId ();
ASSERT (uiResId != 0);
HACCEL hAccellTable = NULL;
if (LoadAccelaratorState (strProfileName, uiResId, hAccellTable))
{
UpdateAcellTable (pTemplate, hAccellTable);
}
}
}
//--------------------------------
// Save default accelerator table:
//--------------------------------
if (pDefaultFrame == NULL)
{
pDefaultFrame = DYNAMIC_DOWNCAST (CFrameWnd, AfxGetMainWnd ());
}
if (pDefaultFrame != NULL && pDefaultFrame->m_hAccelTable != NULL)
{
HACCEL hAccelTable = NULL;
if (LoadAccelaratorState (strProfileName, 0, hAccelTable))
{
UpdateAcellTable (NULL, hAccelTable, pDefaultFrame);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -