// Text.cpp: implementation of the CText class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "Text.h"
#include "DrawView.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CEdit CText::edit;
CText::CText( CDrawView & view ) : CDrawBase( view )
{
this->draw.tag = TEXT;
this->text = "";
if( ! this->edit.GetSafeHwnd() )
{
this->edit.Create( WS_BORDER | WS_CHILD | ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL , CRect( 0 , 0 , 1 , 1 ) , &this->view , 0 );
WNDPROC old = ( WNDPROC )::SetWindowLong( this->edit.GetSafeHwnd() , GWL_WNDPROC , ( long )CText::WindowProc );
::SetWindowLong( this->edit.GetSafeHwnd() , GWL_USERDATA , ( long )old );
}
}
CText::~CText()
{
if( this->edit.GetSafeHwnd() && this->edit.IsWindowVisible() )
this->edit.ShowWindow( SW_HIDE );
}
LRESULT CALLBACK CText::WindowProc(HWND hwnd,UINT message, WPARAM wParam, LPARAM lParam)
{
WNDPROC old = ( WNDPROC )::GetWindowLong( hwnd , GWL_USERDATA );
if( ! old ) return NULL;
CWnd * pWnd = CWnd::FromHandle( hwnd );
LRESULT result = ::CallWindowProc( old , hwnd , message , wParam , lParam );
//键盘输入
if( message == WM_CHAR )
{
CClientDC dc( pWnd );
CString text;
pWnd->GetWindowText( text );
CRect rc , rcClient;
dc.DrawText( text , &rc , DT_CALCRECT );
pWnd->GetClientRect( &rcClient );
if( rc.Width( ) > rcClient.Width( ) || rc.Height( ) > rcClient.Height( ) )
pWnd->SetWindowPos( NULL , 0 , 0 , rc.Width( ) + 8 , rc.Height( ) + 8 , SWP_NOMOVE );
}
return result;
}
void CText::OnLButtonDown( UINT nFlags, CPoint point )
{
point -= this->view.GetScrollPosition( );
this->edit.GetWindowText( this->text );
if( this->text.IsEmpty( ) )
{
this->edit.SetWindowPos( NULL , point.x - 2 , point.y , 150 , 20 , SWP_SHOWWINDOW );
this->edit.SetFocus( );
}
}
void CText::OnMouseMove( UINT nFlags, CPoint point )
{
}
void CText::OnLButtonUp( UINT nFlags, CPoint point )
{
this->edit.GetWindowText( this->text );
if( ! text.IsEmpty( ) )
{
CRect rc;
this->edit.GetWindowRect( &rc );
this->view.ScreenToClient( &rc );
rc.OffsetRect( this->view.GetScrollPosition( ) );
this->draw.start = rc.TopLeft();
this->draw.end = rc.BottomRight();
this->edit.SetWindowText( "" );
this->edit.ShowWindow( SW_HIDE );
}
}
void CText::OnDraw( CDC * pDC )
{
CDrawBase::OnDraw( pDC );
pDC->SetBkMode( TRANSPARENT );
pDC->SetTextColor( this->draw.pen.lopnColor );
pDC->DrawText( this->text , CRect( this->draw.start , this->draw.end ) , DT_LEFT );
}
CDrawBase * CText::Clone( void )
{
CText * obj = new CText( this->view );
memcpy( &obj->draw , &this->draw , sizeof( DRAW ) );
return obj;
}
HCURSOR CText::GetCursor( void )
{
static HICON hIcon = ::AfxGetApp()->LoadStandardCursor( IDC_IBEAM );
return hIcon;
}
char * CText::Encode( int & size , bool erase )
{
char * buf = CDrawBase::Encode( size , erase );
if( erase || this->IsSelect() ) return buf;
char * buffer = new char[ size + this->text.GetLength() + 1 ];
memcpy( buffer , buf , size );
strcpy( buffer + size , this->text );
size += this->text.GetLength() + 1;
delete []buf;
return buffer;
}
bool CText::Decode( char * buffer , int size )
{
if( CDrawBase::Decode( buffer , size ) )
{
if( size > this->draw_id.GetLength() + 1 + sizeof( DRAW ) )
this->text = buffer + this->draw_id.GetLength() + 1 + sizeof( DRAW );
return true;
}
return false;
}
bool CText::Select( CRect & rc )
{
CRect compare;
rc.NormalizeRect();
CRect r( this->draw.start , this->draw.end );
r.NormalizeRect();
return this->draw_select = compare.IntersectRect( &rc , &r );
}