win32textpeer.cpp

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

CPP
1,148
字号
		throw RuntimeException( "Unable to get ITextDocument interface! Can't create Text Peer implementation!" );	}	textDocument_->AddRef();	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->Release();}void Win32TextPeer::initHostDefaultStyle( Font* font ){	String name = font->getName();	name.copy( host_->charFmt_.szFaceName, name.size() );	Color* color = font->getColor();	host_->charFmt_.crTextColor = color->getColorRef32();	host_->charFmt_.yHeight = font->getPointSize();	if ( font->getBold() ) {		host_->charFmt_.dwEffects |= CFE_BOLD;	}	else {		host_->charFmt_.dwEffects &= ~CFE_BOLD;	}	if ( font->getUnderlined() ) {		host_->charFmt_.dwEffects |= CFE_ITALIC;	}	else {		host_->charFmt_.dwEffects &= ~CFE_ITALIC;	}	if ( font->getStrikeOut() ) {		host_->charFmt_.dwEffects |= CFE_STRIKEOUT;	}	else {		host_->charFmt_.dwEffects &= ~CFE_STRIKEOUT;	}	if ( font->getUnderlined() ) {		host_->charFmt_.dwEffects &= ~CFE_UNDERLINE;	}	else {		host_->charFmt_.dwEffects |= CFE_UNDERLINE;	}}OSHandleID Win32TextPeer::getTextObjectHandle(){	OSHandleID result ;	IUnknown* unk = (IUnknown*)textDocument_;	result = (OSHandleID) unk;	return result;}//storagevoid Win32TextPeer::insertText( unsigned int start, const String& text ){	ITextRange* range;	textDocument_->Range( start, start, &range );	if ( NULL != range ) {		BSTR str = SysAllocStringLen( text.c_str(), text.length() );		HRESULT hr = range->SetText( str );		SysFreeString( str );		range->Release();	}}void Win32TextPeer::deleteText( unsigned int start, unsigned int length ){	ITextRange* range;	textDocument_->Range( start, start+length, &range );	if ( NULL != range ) {		HRESULT hr = range->Delete( tomCharacter, 0, NULL );		range->Release();	}}unsigned int Win32TextPeer::getTextLength(){	unsigned int result = 0;	ITextRange* range;	textDocument_->Range( 0, 0, &range );	if ( NULL != range ) {		long len = 0;		range->GetStoryLength( &len );		result = len;		range->Release();	}	return result;}String Win32TextPeer::getText( unsigned int start, unsigned int length ){	String result;	ITextRange* range;	textDocument_->Range( start, start+length, &range );	if ( NULL != range ) {		BSTR str = SysAllocString( NULL );		range->GetText( &str );		result.assign( str, SysStringLen(str) );		SysFreeString( str );		range->Release();	}	return result;}void Win32TextPeer::paint( GraphicsContext* context, const Rect& paintRect ){	HDC dc = (HDC)context->getPeer()->getContextID();	RECTL rect;	rect.left = paintRect.left_ + margins_.left_;	rect.top = paintRect.top_ + margins_.top_;	rect.right = paintRect.right_ - margins_.right_;	rect.bottom = paintRect.bottom_ - margins_.bottom_;	textSvcs_->TxDraw(						DVASPECT_CONTENT,  		// Draw Aspect						0,						// Lindex						NULL,					// Info for drawing optimization						NULL,					// target device information						dc,				// Draw device HDC						0,			 	   	// Target device HDC						(RECTL*) &rect,			// Bounding client rectangle						NULL,					// Clipping rectangle for metafiles						NULL,			// Update rectangle						NULL, 	   				// Call back function						NULL,					// Call back parameter						TXTVIEW_INACTIVE);		// What view of the object could be TXTVIEW_ACTIVE}void Win32TextPeer::setRightMargin( const double & rightMargin ){	margins_.right_ = rightMargin;}void Win32TextPeer::setLeftMargin( const double & leftMargin ){	margins_.left_ = leftMargin;}void Win32TextPeer::setTopMargin( const double & topMargin ){	margins_.top_ = topMargin;}void Win32TextPeer::setBottomMargin( const double & bottomMargin ){	margins_.bottom_ = bottomMargin;}double Win32TextPeer::getLeftMargin(){	return margins_.left_;}double Win32TextPeer::getRightMargin(){	return margins_.right_;}double Win32TextPeer::getTopMargin(){	return margins_.top_;}double Win32TextPeer::getBottomMargin(){	return margins_.bottom_;}unsigned long Win32TextPeer::getLineCount(){	unsigned long result = 0;	ITextRange* range = NULL;	textDocument_->Range( 0, 0, &range );	if ( NULL != range ) {		long len = 0;		range->GetStoryLength( &len );		range->SetEnd ( len );		HRESULT hr = range->MoveStart( tomLine, 1, NULL );		while ( S_OK == hr ) {			result ++;			hr = range->MoveStart( tomLine, 1, NULL );		}		range->Release();	}	return result;}Rect Win32TextPeer::getContentBoundsForWidth(const double& width){	Rect result;	long w;	long h;	w = width;	h = 1;	SIZEL sz;	sz.cx = w;	sz.cy = h;	HDC dc = GetDC( GetDesktopWindow() );	HRESULT hr = textSvcs_->TxGetNaturalSize( DVASPECT_CONTENT, dc, NULL, NULL,								TXTNS_FITTOCONTENT, &sz, &w, &h );	ReleaseDC( GetDesktopWindow(), dc );	result.bottom_ = result.top_ + h;	result.right_ = result.left_ + w;	return result;}void Win32TextPeer::getStyle( unsigned int start, unsigned int length, Dictionary& styles, Color& color ){	String result;	ITextRange* range = NULL;	textDocument_->Range( start, start+length, &range );	if ( NULL != range ) {		ITextFont* font = NULL;		ITextPara* para = NULL;		range->GetFont( &font );		range->GetPara( &para );		if ( (NULL != font) && (NULL != para) ) {			BSTR str = SysAllocString( NULL );			styles[ Text::fsFontName ] = font->GetName( &str );			SysFreeString( str );			long val;			font->GetForeColor( &val );			color.setColorRef32( val );			styles[ Text::fsColor ] = (Object*)&color; // color cannot be a temporary			float size = 0;			font->GetSize( &size );			styles[ Text::fsPointSize ] = (double)size;			font->GetBold( &val );			styles[ Text::fsBold ] = ( tomTrue == val ) ? true : false;			font->GetItalic( &val );			styles[ Text::fsItalic ] = ( tomTrue == val ) ? true : false;			font->GetStrikeThrough( &val );			styles[ Text::fsStrikeout ] = ( tomTrue == val ) ? true : false;			Text::UnderlineTypes underline = Text::utNone;			font->GetUnderline( &val );			switch ( (int)val ) {				case tomNone : {					underline = Text::utNone;				}				break;				case tomSingle : {					underline = Text::utSingle;				}				break;				case tomDouble : {					underline = Text::utDouble;				}				break;				case tomDotted : {					underline = Text::utDotted;				}				break;			}			styles[ Text::fsUnderlined ] = underline;			font->Release();		}		if ( NULL != para ) {			Text::ParagraphAlignment alignment = Text::paLeft;			long val = 0;			para->GetAlignment( &val );			switch ( (int)val ) {				case tomAlignLeft : {					alignment = Text::paLeft;				}				break;				case tomAlignCenter : {					alignment = Text::paCenter;				}				break;				case tomAlignRight : {					alignment = Text::paRight;				}				break;				case tomAlignJustify : {					alignment = Text::paJustified;				}				break;			}			styles[ Text::psAlignment ] = alignment;			para->Release();		}		range->Release();	}}void Win32TextPeer::setStyle( unsigned int start, unsigned int length, Dictionary& styles ){	String result;	ITextRange* range = NULL;	textDocument_->Range( start, start+length, &range );	if ( NULL != range ) {		ITextFont* font = NULL;		ITextPara* para = NULL;		range->GetFont( &font );		range->GetPara( &para );		Dictionary::Enumerator* items = styles.getEnumerator();		if ( (NULL != font) && (NULL != para) ) {			while ( items->hasMoreElements() ) {				Dictionary::pair style = items->nextElement();				if ( style.first == Text::fsFontName ) {					String s = style.second;					BSTR name = SysAllocStringLen( s.c_str(), s.length() );					font->SetName( name );					SysFreeString( name );				}				else if ( style.first == Text::fsColor ) {					Color* color = (Color*)(Object*)style.second;					VCF_ASSERT( NULL != color );					font->SetForeColor( color->getColorRef32() );				}				else if ( style.first == Text::fsPointSize ) {					double val = style.second;					font->SetSize( val );				}				else if ( style.first == Text::fsBold ) {					long val = ((bool)style.second) ? tomTrue : tomFalse;					font->SetBold( val );				}				else if ( style.first == Text::fsItalic ) {					long val = ((bool)style.second) ? tomTrue : tomFalse;					font->SetItalic( val );				}				else if ( style.first == Text::fsStrikeout ) {					long val = ((bool)style.second) ? tomTrue : tomFalse;					font->SetStrikeThrough( val );				}				else if ( style.first == Text::fsUnderlined ) {					long val = tomNone;					switch ( (int)style.second ) {						case Text::utNone : {							val = tomNone;						}						break;						case Text::utSingle : {							val = tomSingle;						}						break;						case Text::utDouble : {							val = tomDouble;						}						break;						case Text::utDotted : {							val = tomDotted;						}						break;					}					font->SetUnderline( val );				}				else if ( style.first == Text::psAlignment ) {					int alignment = style.second;					long val = 0;					switch ( alignment ) {						case Text::paLeft : {							val = tomAlignLeft;						}						break;						case Text::paCenter : {							val = tomAlignCenter;						}						break;						case Text::paRight : {							val = tomAlignRight;						}						break;						case Text::paJustified : {							val = tomAlignJustify;						}						break;					}					para->SetAlignment( val );				}			}			para->Release();			font->Release();		}		range->Release();	}}void Win32TextPeer::setDefaultStyle( Dictionary&  styles ){	if ( NULL == host_ ) {		return;	}	Dictionary::Enumerator* items = styles.getEnumerator();	while ( items->hasMoreElements() ) {		Dictionary::pair style = items->nextElement();		if ( style.first == Text::fsFontName ) {			String s = style.second;			s.copy( host_->charFmt_.szFaceName, s.size() );		}		else if ( style.first == Text::fsColor ) {			Color* color = (Color*)(Object*)style.second;			VCF_ASSERT( NULL != color );			host_->charFmt_.crTextColor = color->getColorRef32();		}		else if ( style.first == Text::fsPointSize ) {			double val = style.second;			host_->charFmt_.yHeight = val;		}		else if ( style.first == Text::fsBold ) {			if ( (bool)style.second ) {				host_->charFmt_.dwEffects |= CFE_BOLD;			}			else {				host_->charFmt_.dwEffects &= ~CFE_BOLD;			}		}		else if ( style.first == Text::fsItalic ) {			if ( (bool)style.second ) {				host_->charFmt_.dwEffects |= CFE_ITALIC;			}			else {				host_->charFmt_.dwEffects &= ~CFE_ITALIC;			}		}		else if ( style.first == Text::fsStrikeout ) {			if ( (bool)style.second ) {				host_->charFmt_.dwEffects |= CFE_STRIKEOUT;			}			else {				host_->charFmt_.dwEffects &= ~CFE_STRIKEOUT;			}		}		else if ( style.first == Text::fsUnderlined ) {			int underline = style.second;			if ( Text::utNone == underline ) {				host_->charFmt_.dwEffects &= ~CFE_UNDERLINE;			}			else {				host_->charFmt_.dwEffects |= CFE_UNDERLINE;			}		}		else if ( style.first == Text::psAlignment ) {			int alignment = style.second;			switch ( alignment ) {				case Text::paLeft : {					host_->paraFmt_.wAlignment = PFA_LEFT;				}				break;				case Text::paCenter : {					host_->paraFmt_.wAlignment = PFA_CENTER;				}				break;				case Text::paRight : {					host_->paraFmt_.wAlignment = PFA_RIGHT;				}				break;				case Text::paJustified : {					//not supported!					//host_->paraFmt_.wAlignment = PFA_RIGHT;				}				break;			}		}	}}/***CVS Log info*$Log$*Revision 1.3  2006/04/07 02:35:26  ddiego*initial checkin of merge from 0.6.9 dev branch.**Revision 1.2.2.1  2005/10/07 19:31:53  ddiego*merged patch 1315995 and 1315991 into dev repos.**Revision 1.2  2005/07/09 23:14:58  ddiego*merging in changes from devmain-0-6-7 branch.**Revision 1.1.2.12  2005/07/05 03:34:35  marcelloptr*minor bug fixed**Revision 1.1.2.11  2005/06/26 01:31:20  marcelloptr*improvements to the Color class. The default, when packing the components into a single integer, is now cpsARGB instead than cpsABGR.**Revision 1.1.2.10  2005/06/25 19:50:49  marcelloptr*forgotten MP mark**Revision 1.1.2.9  2005/06/09 07:18:25  marcelloptr*simpler and more useful use of Color class with ctor and getters/setters**Revision 1.1.2.6  2005/06/07 17:31:29  marcelloptr*added missed getStyle() function. Fixed underline text that couldn't be removed once introduced.**/

⌨️ 快捷键说明

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