win32textpeer.cpp

来自「这是VCF框架的代码」· C++ 代码 · 共 1,148 行 · 第 1/2 页

CPP
1,148
字号
//Win32TextPeer.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/ApplicationKit/ApplicationKit.h"#include "vcf/ApplicationKit/ApplicationKitPrivate.h"#include "vcf/ApplicationKit/Win32TextPeer.h"#include "vcf/FoundationKit/Dictionary.h"#include <Richedit.h>#include <imm.h>#include "thirdparty/win32/Microsoft/TOM.h"#include <Richole.h>#define DEFINE_GUIDXXX(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \        EXTERN_C const GUID CDECL name \                = { l, w1, w2, { b1, b2,  b3,  b4,  b5,  b6,  b7,  b8 } }DEFINE_GUIDXXX(IID_ITextDocument,0x8CC497C0,0xA1DF,0x11CE,0x80,0x98,                0x00,0xAA,0x00,0x47,0xBE,0x5D);const IID IID_ITextServices = { // 8d33f740-cf58-11ce-a89d-00aa006cadc5    0x8d33f740,    0xcf58,    0x11ce,    {0xa8, 0x9d, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}  };const IID IID_ITextHost = { // c5bdd8d0-d26e-11ce-a89e-00aa006cadc5    0xc5bdd8d0,    0xd26e,    0x11ce,    {0xa8, 0x9e, 0x00, 0xaa, 0x00, 0x6c, 0xad, 0xc5}  };#include "thirdparty/win32/Microsoft/textserv.h"#if defined(VCF_MINGW)#define CFM_ALL (CFM_EFFECTS | CFM_SIZE | CFM_FACE | CFM_OFFSET | CFM_CHARSET)  #define	PFM_ALL (PFM_STARTINDENT | PFM_RIGHTINDENT | PFM_OFFSET	| \				 PFM_ALIGNMENT   | PFM_TABSTOPS    | PFM_NUMBERING | \				 PFM_OFFSETINDENT| PFM_RTLPARA)#define CFM_SMALLCAPS		0x0040			/* (*)	*/#define	CFM_ALLCAPS			0x0080			/* Displayed by 3.0	*/#define	CFM_HIDDEN			0x0100			/* Hidden by 3.0 */#define	CFM_OUTLINE			0x0200			/* (*)	*/#define	CFM_SHADOW			0x0400			/* (*)	*/#define	CFM_EMBOSS			0x0800			/* (*)	*/#define	CFM_IMPRINT			0x1000			/* (*)	*/#define CFM_DISABLED		0x2000#define	CFM_REVISED			0x4000#define CFM_BACKCOLOR		0x04000000#define CFM_LCID			0x02000000#define	CFM_UNDERLINETYPE	0x00800000		/* Many displayed by 3.0 */#define	CFM_WEIGHT			0x00400000#define CFM_SPACING			0x00200000		/* Displayed by 3.0	*/#define CFM_KERNING			0x00100000		/* (*)	*/#define CFM_STYLE			0x00080000		/* (*)	*/#define CFM_ANIMATION		0x00040000		/* (*)	*/#define CFM_REVAUTHOR		0x00008000#define CFE_SUBSCRIPT		0x00010000		/* Superscript and subscript are */#define CFE_SUPERSCRIPT		0x00020000		/*  mutually exclusive			 */#define CFM_SUBSCRIPT		CFE_SUBSCRIPT | CFE_SUPERSCRIPT#define CFM_SUPERSCRIPT		CFM_SUBSCRIPT#define	CFM_EFFECTS2 (CFM_EFFECTS | CFM_DISABLED | CFM_SMALLCAPS | CFM_ALLCAPS \					| CFM_HIDDEN  | CFM_OUTLINE | CFM_SHADOW | CFM_EMBOSS \					| CFM_IMPRINT | CFM_DISABLED | CFM_REVISED \					| CFM_SUBSCRIPT | CFM_SUPERSCRIPT | CFM_BACKCOLOR)#define CFM_ALL2	 (CFM_ALL | CFM_EFFECTS2 | CFM_BACKCOLOR | CFM_LCID \					| CFM_UNDERLINETYPE | CFM_WEIGHT | CFM_REVAUTHOR \					| CFM_SPACING | CFM_KERNING | CFM_STYLE | CFM_ANIMATION)#define	CFE_SMALLCAPS		CFM_SMALLCAPS#define	CFE_ALLCAPS			CFM_ALLCAPS#define	CFE_HIDDEN			CFM_HIDDEN#define	CFE_OUTLINE			CFM_OUTLINE#define	CFE_SHADOW			CFM_SHADOW#define	CFE_EMBOSS			CFM_EMBOSS#define	CFE_IMPRINT			CFM_IMPRINT#define	CFE_DISABLED		CFM_DISABLED#define	CFE_REVISED			CFM_REVISED  /* NOTE: CFE_AUTOCOLOR and CFE_AUTOBACKCOLOR correspond to CFM_COLOR and   CFM_BACKCOLOR, respectively, which control them */#define CFE_AUTOBACKCOLOR	CFM_BACKCOLOR/* CHARFORMAT effects */#define CFE_BOLD		0x0001#define CFE_ITALIC		0x0002#define CFE_UNDERLINE	0x0004#define CFE_STRIKEOUT	0x0008#define CFE_PROTECTED	0x0010#define CFE_LINK		0x0020#define CFE_AUTOCOLOR	0x40000000		/* NOTE: this corresponds to */  #endif  static PCreateTextServices CreateTextServicesFunc;using namespace VCF;class TextHost : public ITextHost {public:	HWND hwnd_;	PARAFORMAT paraFmt_;	CHARFORMATW charFmt_;	bool wrapLines_;	bool multiLines_;	TextHost(bool wrapLines, bool multiLines ) {		wrapLines_ = wrapLines;		multiLines_ = multiLines ;		memset(&paraFmt_,0,sizeof(paraFmt_)) ;		paraFmt_.cbSize = sizeof(paraFmt_);		paraFmt_.dwMask = PFM_ALL;		paraFmt_.wAlignment = PFA_LEFT;		Font font;		memset(&charFmt_,0,sizeof(charFmt_)) ;		charFmt_.cbSize = sizeof(charFmt_);		charFmt_.yHeight = font.getPointSize();		charFmt_.yOffset = 0;		charFmt_.crTextColor = font.getColor()->getColorRef32();		charFmt_.dwEffects = CFM_EFFECTS | CFE_AUTOBACKCOLOR;		charFmt_.dwEffects &= ~(CFE_PROTECTED | CFE_LINK | CFE_AUTOCOLOR);		charFmt_.dwEffects &= ~CFE_BOLD;		charFmt_.dwEffects &= ~CFE_ITALIC;		charFmt_.dwEffects &= ~CFE_UNDERLINE;		charFmt_.dwEffects &= ~CFE_STRIKEOUT;		if ( font.getBold() ) {			charFmt_.dwEffects |= CFE_BOLD;		}		if ( font.getItalic() ) {			charFmt_.dwEffects |= CFE_ITALIC;		}		if ( font.getStrikeOut() ) {			charFmt_.dwEffects |= CFE_STRIKEOUT;		}		if ( font.getUnderlined() ) {			charFmt_.dwEffects |= CFE_UNDERLINE;		}		charFmt_.dwMask = CFM_ALL | CFM_BACKCOLOR | CFM_STYLE;		charFmt_.bCharSet = DEFAULT_CHARSET;		charFmt_.bPitchAndFamily = FF_DONTCARE;		String fntName = font.getName();		fntName.copy( charFmt_.szFaceName, fntName.size() );	}	STDMETHODIMP QueryInterface( REFIID iid, void ** ppvObject ) {		String uuidStr;		if ( iid == IID_ITextHost ) {			*ppvObject = (void*) (ITextHost*)this;		}		else if ( iid == IID_IUnknown ) {			*ppvObject = (void*) (IUnknown*)this;		}		else {			*ppvObject = NULL;			return E_NOINTERFACE;		}		return S_OK;	}	STDMETHODIMP_(ULONG) AddRef() {		return 0;	}	STDMETHODIMP_(ULONG) Release() {		return 0;	}	//@cmember Get the DC for the host	HDC TxGetDC() {		return GetDC( hwnd_ );	}	//@cmember Release the DC gotten from the host	INT TxReleaseDC(HDC hdc) {		return ReleaseDC( hwnd_, hdc );	}	//@cmember Show the scroll bar	BOOL TxShowScrollBar(INT fnBar, BOOL fShow) {		return FALSE;	}	//@cmember Enable the scroll bar	BOOL TxEnableScrollBar (INT fuSBFlags, INT fuArrowflags) {		return FALSE;	}	//@cmember Set the scroll range	BOOL TxSetScrollRange(							INT fnBar,							LONG nMinPos,							INT nMaxPos,							BOOL fRedraw) {		return FALSE;	}	//@cmember Set the scroll position	BOOL TxSetScrollPos (INT fnBar, INT nPos, BOOL fRedraw) {		return FALSE;	}	//@cmember InvalidateRect	void TxInvalidateRect(LPCRECT prc, BOOL fMode) {		InvalidateRect( hwnd_, prc, fMode );	}	//@cmember Send a WM_PAINT to the window	void TxViewChange(BOOL fUpdate) {	}	//@cmember Create the caret	BOOL TxCreateCaret(HBITMAP hbmp, INT xWidth, INT yHeight) {		StringUtils::trace( "TxCreateCaret\n" );		return ::CreateCaret( hwnd_, hbmp, xWidth, yHeight );		//return FALSE;	}	//@cmember Show the caret	BOOL TxShowCaret(BOOL fShow) {		StringUtils::trace( "TxShowCaret\n" );		if ( fShow ) {			::ShowCaret( hwnd_ );		}		else {			::HideCaret( hwnd_ );		}		return FALSE;	}	//@cmember Set the caret position	BOOL TxSetCaretPos(INT x, INT y) {		StringUtils::trace( "TxSetCaretPos\n" );		return ::SetCaretPos( x, y );		//return FALSE;	}	//@cmember Create a timer with the specified timeout	BOOL TxSetTimer(UINT idTimer, UINT uTimeout) {		StringUtils::trace( "TxSetTimer\n" );		return SetTimer( hwnd_, idTimer, uTimeout, NULL );	}	//@cmember Destroy a timer	void TxKillTimer(UINT idTimer) {		StringUtils::trace( "KillTimer\n" );		KillTimer( hwnd_, idTimer );	}	//@cmember Scroll the content of the specified window's client area	void TxScrollWindowEx (							INT dx,							INT dy,							LPCRECT lprcScroll,							LPCRECT lprcClip,							HRGN hrgnUpdate,							LPRECT lprcUpdate,							UINT fuScroll) {	}	//@cmember Get mouse capture	void TxSetCapture(BOOL fCapture) {		StringUtils::trace( "TxSetCapture\n" );		if ( fCapture ) {			SetCapture( hwnd_ );		}		else {			ReleaseCapture();		}	}	//@cmember Set the focus to the text window	void TxSetFocus() {		StringUtils::trace( "TxSetFocus\n" );		SetFocus( hwnd_ );	}	//@cmember Establish a new cursor shape	void TxSetCursor(HCURSOR hcur, BOOL fText) {		StringUtils::trace( "TxSetCursor\n" );		SetCursor( hcur );	}	//@cmember Converts screen coordinates of a specified point to the client coordinates	BOOL TxScreenToClient (LPPOINT lppt) {		return ScreenToClient( hwnd_, lppt );	}	//@cmember Converts the client coordinates of a specified point to screen coordinates	BOOL TxClientToScreen (LPPOINT lppt) {		return ClientToScreen( hwnd_, lppt );	}	//@cmember Request host to activate text services	HRESULT TxActivate( LONG * plOldState ) {		return S_OK;	}	//@cmember Request host to deactivate text services   	HRESULT TxDeactivate( LONG lNewState ){		return S_OK;	}	//@cmember Retrieves the coordinates of a window's client area	HRESULT TxGetClientRect(LPRECT prc){		GetClientRect( hwnd_, prc );		return S_OK;	}	//@cmember Get the view rectangle relative to the inset	HRESULT TxGetViewInset(LPRECT prc){		prc->left = 0;		prc->right = 0;		prc->top = 100;		prc->bottom = 55;		return S_OK;	}	//@cmember Get the default character format for the text	HRESULT TxGetCharFormat(const CHARFORMATW **ppCF ){		*ppCF = &charFmt_;		return S_OK;	}	//@cmember Get the default paragraph format for the text	HRESULT TxGetParaFormat(const PARAFORMAT **ppPF){		*ppPF = &paraFmt_;		return S_OK;	}	//@cmember Get the background color for the window	COLORREF TxGetSysColor(int nIndex){		return GetSysColor( nIndex );	}	//@cmember Get the background (either opaque or transparent)	HRESULT TxGetBackStyle(TXTBACKSTYLE *pstyle) {		*pstyle = TXTBACK_TRANSPARENT;		return S_OK;	}	//@cmember Get the maximum length for the text	HRESULT TxGetMaxLength(DWORD *plength){		*plength = INFINITE;		return S_OK;	}	//@cmember Get the bits representing requested scroll bars for the window	HRESULT TxGetScrollBars(DWORD *pdwScrollBar){		pdwScrollBar = 0;		return S_OK;	}	//@cmember Get the character to display for password input	HRESULT TxGetPasswordChar(TCHAR *pch){		return S_FALSE;	}	//@cmember Get the accelerator character	HRESULT TxGetAcceleratorPos(LONG *pcp){		*pcp = -1;		return S_OK;	}	//@cmember Get the native size    HRESULT TxGetExtent(LPSIZEL lpExtent){		return E_NOTIMPL;	}	//@cmember Notify host that default character format has changed	HRESULT OnTxCharFormatChange (const CHARFORMATW * pcf){		memcpy(&charFmt_, pcf, pcf->cbSize);		return S_OK;	}	//@cmember Notify host that default paragraph format has changed	HRESULT OnTxParaFormatChange (const PARAFORMAT * ppf){		memcpy(&paraFmt_, ppf, ppf->cbSize);		return S_OK;	}	//@cmember Bulk access to bit properties	HRESULT TxGetPropertyBits(DWORD dwMask, DWORD *pdwBits){		DWORD bits = *pdwBits;		bits = TXTBIT_RICHTEXT | TXTBIT_USECURRENTBKG;		if ( wrapLines_ ) {			bits |= TXTBIT_WORDWRAP;		}		if ( multiLines_ ) {			bits |= TXTBIT_MULTILINE;		}		*pdwBits = bits;		return S_OK;	}	//@cmember Notify host of events	HRESULT TxNotify(DWORD iNotify, void *pv){		return S_OK;	}	// Far East Methods for getting the Input Context//#ifdef WIN95_IME	virtual HIMC TxImmGetContext(){		return NULL;	}	void TxImmReleaseContext( HIMC himc ) {	}//#endif	//@cmember Returns HIMETRIC size of the control bar.	HRESULT TxGetSelectionBarWidth (LONG *lSelBarWidth) {		*lSelBarWidth = 0;		return S_OK;	}};Win32TextPeer::Win32TextPeer( const bool& autoWordWrap, const bool& multiLined ):	host_(NULL),	textSvcs_(NULL),	textDocument_(NULL){	static bool initializedWin32TextPeer = false;	if ( !initializedWin32TextPeer ) {		//load up the Riched20.dll		HINSTANCE riched20 = LoadLibraryA( "Riched20.dll" );		if ( NULL == riched20 ) {			throw RuntimeException( "Riched20.dll not found on this system! Can't create Text Peer implementation!" );		}		CreateTextServicesFunc = (PCreateTextServices) GetProcAddress( riched20, "CreateTextServices" );		if ( NULL == CreateTextServicesFunc ) {			throw RuntimeException( "Riched20.dll found but apparently it does not export the CreateTextServices function. Can't create Text Peer implementation!" );		}	}	initializedWin32TextPeer = true;	host_ = new TextHost(autoWordWrap,multiLined);	IUnknown* unk = NULL;	CreateTextServicesFunc( NULL, host_, &unk );	unk->QueryInterface( IID_ITextServices, (void**)&textSvcs_ );	if ( NULL == textSvcs_ ) {		throw RuntimeException( "Unable to get ITextServices interface! Can't create Text Peer implementation!" );	}	textSvcs_->AddRef();	unk->QueryInterface( IID_ITextDocument, (void**)&textDocument_ );	if ( NULL == textDocument_ ) {		throw RuntimeException( "Unable to get ITextDocument interface! Can't create Text Peer implementation!" );	}	textDocument_->AddRef();	unk->Release();}Win32TextPeer::Win32TextPeer():	host_(NULL),	textSvcs_(NULL),	textDocument_(NULL){}Win32TextPeer::~Win32TextPeer(){	if ( NULL != textSvcs_ ) {		textSvcs_->Release();	}	if ( NULL != textDocument_ ) {		textDocument_->Release();	}	delete host_;}void Win32TextPeer::initFromRichEdit( HWND hwnd ){	//extract the TOM interfaces!	IUnknown* unk = NULL;	::SendMessage( hwnd, EM_GETOLEINTERFACE, 0, (LPARAM)&unk );	if ( NULL == unk ) {		throw RuntimeException( "Unable to retrieve IUnknown interface from edit control." );	}	unk->QueryInterface( IID_ITextDocument, (void**)&textDocument_ );	if ( NULL == textDocument_ ) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?