📄 csecurerichedit.cpp
字号:
/*____________________________________________________________________________
Copyright (C) 2002 PGP Corporation
All rights reserved.
$Id: CSecureRichEdit.cpp,v 1.12 2002/11/13 19:34:24 wjb Exp $
____________________________________________________________________________*/
#include "pgpClassesConfig.h"
#include <stdlib.h>
#include "CArray.h"
#include "CString.h"
#include "UMath.h"
#include "UTime.h"
#include "UWinVersion.h"
#include "pgpUnicode.h"
#include "pgpUnicodeWin32.h"
#include "pgputilities.h"
#include "SpaceEdit.h"
#include "CSecureRichEdit.h"
_USING_PGP
// Class CSecureRichEdit member functions
CSecureRichEdit::CSecureRichEdit()
{
mPumping = FALSE;
mCapsOnInit = FALSE;
mHideTyping = TRUE;
mOldCapsWarning = FALSE;
mDestroyed = FALSE;
mUnicodeOS = UWinVersion::IsWinNT4Compatible();
mContentsUTF8.Clear();
}
CSecureRichEdit::~CSecureRichEdit()
{
mContentsUTF8.Clear();
}
VOID
SpaceEditCallback(HWND hwndEdit,void *pUserValue)
{
BOOL bShowCapsWarning;
CSecureRichEdit *csre;
DWORD dwKey;
csre=(CSecureRichEdit *)pUserValue;
csre->SendParentEvent(CSecureRichEdit::kContentsChangedEvent);
bShowCapsWarning=SEGetShowWarning(csre->REhwnd());//REhwnd());
if(((!bShowCapsWarning)&&(csre->mOldCapsWarning))||(bShowCapsWarning))
csre->SendParentEvent(CSecureRichEdit::kSawCapsLockKeyEvent);
csre->mOldCapsWarning=bShowCapsWarning;
dwKey = SEGetSpecialKey (csre->REhwnd());
switch (dwKey)
{
case VK_RETURN :
csre->SendParentEvent(CSecureRichEdit::kSawReturnKeyEvent);
return;
}
}
LRESULT APIENTRY
CSRESubclassProc (
HWND hWnd,
UINT uMsg,
WPARAM wParam,
LPARAM lParam)
{
CSecureRichEdit *csre;
csre=(CSecureRichEdit *)GetProp(hWnd,"CSRECLASS");
switch (uMsg)
{
case WM_SETFOCUS :
{
csre->InstallSecurityHooks();
if(csre->mPumping==FALSE)
{
csre->mPumping=TRUE;
if(csre->mCapsOnInit)
{
CWindow(csre->GetParent()).SendMessage(CSecureRichEdit::WM_SECUREEDIT_EVENT,
reinterpret_cast<WPARAM>(csre->REhwnd()), CSecureRichEdit::kSawCapsLockKeyEvent);
csre->mCapsOnInit=FALSE;
}
}
break;
}
case WM_KILLFOCUS :
{
csre->UninstallSecurityHooks();
break;
}
case WM_DESTROY:
{
csre->Contents();
csre->mDestroyed = TRUE;
SEDestroy(hWnd);
return FALSE;
}
}
if (csre->mUnicodeOS)
{
return CallWindowProcW (csre->mOrigProc,
hWnd, uMsg, wParam, lParam);
}
else
{
return CallWindowProcA (csre->mOrigProc,
hWnd, uMsg, wParam, lParam);
}
}
void
CSecureRichEdit::Subclass(HWND hWnd,PGPContextRef context)
{
mREhwnd=hWnd;
mContext=context;
SetProp(hWnd,"CSRECLASS", this );
SEInit(
context,
hWnd,
TRUE,
SpaceEditCallback,
this);
if (mUnicodeOS)
{
mOrigProc = (WNDPROC)
SetWindowLongW(hWnd,GWL_WNDPROC,(LONG)CSRESubclassProc);
CWindow::SubclassUnicode(hWnd);
}
else
{
mOrigProc = (WNDPROC)
::SetWindowLongA(hWnd,GWL_WNDPROC,(LONG)CSRESubclassProc);
CWindow::Subclass(hWnd);
}
}
void
CSecureRichEdit::ClearContents()
{
mContentsUTF8.Clear();
if(!mDestroyed)
{
SEWipeEditBox (REhwnd());
InvalidateRect(NULL,TRUE);
UpdateWindow();
SendParentEvent(CSecureRichEdit::kContentsChangedEvent);
}
}
PGPBoolean
CSecureRichEdit::SetHideTypingPref(PGPBoolean hideValue)
{
PGPBoolean oldHideValue;
oldHideValue=HidingTyping();
SEChangeHideTyping (REhwnd(),hideValue);
mHideTyping=hideValue;
return oldHideValue;
}
PGPUInt32
CSecureRichEdit::SetMaxSizeContents(PGPUInt32 maxSize)
{
PGPUInt32 oldSize = MaxSize();
ClearContents();
mMaxSize = maxSize;
SESetMaxLength (REhwnd(),mMaxSize);
return oldSize;
}
const CSecureString&
CSecureRichEdit::Contents() const
{
char *sz;
PGPUInt32 i;
int memamt;
// cast away "const-ness"
if(!(((CSecureRichEdit*)this)->mDestroyed))
{
((CSecureRichEdit*)this)->mContentsUTF8.Clear();
memamt=SEGetTextLength(((CSecureRichEdit*)this)->REhwnd())+1;
sz=(char *)PGPNewSecureData(
PGPPeekContextMemoryMgr(((CSecureRichEdit*)this)->mContext),
memamt,
kPGPMemoryMgrFlags_Clear);
if(sz!=NULL)
{
SEGetText(((CSecureRichEdit*)this)->REhwnd(),sz,memamt);
for(i=0;i<strlen(sz);i++)
{
// cast away "const-ness"
((CSecureRichEdit*)this)->
mContentsUTF8.InsertAfter(i-1, sz[i]);
}
memset(sz,0x00,memamt);
PGPFreeData(sz);
}
}
return ((CSecureRichEdit*)this)->mContentsUTF8;
}
/* Length in Unicode (wide) characters,
which is not less then the length in bytes (respective UTF8 string)
*/
PGPUInt32
CSecureRichEdit::ContentsLength() const
{
if(mDestroyed)
return mContentsUTF8.Length();
return SEGetTextLength(mREhwnd);
}
LRESULT
CALLBACK
CSecureRichEdit::SecurityHookProc(INT iCode, WPARAM wParam, LPARAM lParam)
{
return 0;
}
HHOOK g_hookGetMessage=NULL;
LRESULT
CALLBACK
CSecureRichEdit::SecurityGetMessageHookProc(INT iCode, WPARAM wParam, LPARAM lParam)
{
PMSG pMsg = (PMSG)lParam;
// allow run-time defined messages through.
// this hack is required (at least on WinXP) since the OS itself hooks
// edit windows in order to handle keyboard switching properly.
if (pMsg->message > 0xC000)
return CallNextHookEx (g_hookGetMessage, iCode, wParam, lParam);
return 0;
}
void
CSecureRichEdit::InstallSecurityHooks()
{
PGPUInt32 dwPhraseThreadID = GetCurrentThreadId();
// These are just to prevent sniffers from seeing these messages.
mHhookKeyboard = SetWindowsHookEx(WH_KEYBOARD, SecurityHookProc, NULL,
dwPhraseThreadID);
mHhookCBT = SetWindowsHookEx(WH_CBT, SecurityHookProc, NULL,
dwPhraseThreadID);
g_hookGetMessage=mHhookGetMessage = SetWindowsHookEx(WH_GETMESSAGE, SecurityGetMessageHookProc,
NULL, dwPhraseThreadID);
mHhookMsgFilter = SetWindowsHookEx(WH_MSGFILTER, SecurityHookProc, NULL,
dwPhraseThreadID);
}
void
CSecureRichEdit::UninstallSecurityHooks()
{
UnhookWindowsHookEx(mHhookMsgFilter);
UnhookWindowsHookEx(mHhookGetMessage);
UnhookWindowsHookEx(mHhookCBT);
UnhookWindowsHookEx(mHhookKeyboard);
}
PGPUInt32
CSecureRichEdit::SendParentEvent(PGPUInt32 event)
{
if(!mPumping)
{
if(event==CSecureRichEdit::kSawCapsLockKeyEvent)
mCapsOnInit=TRUE;
return TRUE;
}
return CWindow(GetParent()).SendMessage(WM_SECUREEDIT_EVENT,
reinterpret_cast<WPARAM>(mREhwnd), event);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -