Code Search for Developers
 
 
  

editbox.cpp from NeoEngineNG at Krugle


Show editbox.cpp syntax highlighted

/***************************************************************************
           editbox.cpp  -  Textbox with borders and input processing
                             -------------------
    begin                : Sun Mar 2 2003
    copyright            : (C) 2003 by Reality Rift Studios
    email                : mattias@realityrift.com
 ***************************************************************************

 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at 
 http://www.mozilla.org/MPL/

 Software distributed under the License is distributed on an "AS IS" basis,
 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 for the specific language governing rights and limitations under the
 License.

 The Original Code is the NeoEngine, NeoWTK, editbox.cpp

 The Initial Developer of the Original Code is Mattias Jansson.
 Portions created by Mattias Jansson are Copyright (C) 2003
 Reality Rift Studios. All Rights Reserved.

 ***************************************************************************/

#include "editbox.h"
#include "textarea.h"
#include "msg.h"

using namespace NeoEngine;


namespace NeoWTK
{


EditBox::EditBox( Object *pkParent, EditBox *pkObject, bool bCreateAreas ) :
	BorderArea( pkParent, !pkObject ? new TextArea( 0 ) : 0, pkObject, bCreateAreas )
{
	if( bCreateAreas )
		GetArea( CENTER )->SetName( "Center" );

	m_pkTextArea       = bCreateAreas ? dynamic_cast< TextArea* >( GetArea( BorderArea::CENTER ) ) : 0;
	m_pkCursor         = bCreateAreas ? GetObject( "Cursor" ) : 0;

	m_bInputFocus      = false;
	m_fCursorBlinkTime = 0.0f;
}


EditBox::~EditBox()
{
}


bool EditBox::Render( Frustum *pkFrustum, bool bForce )
{
	return BorderArea::Render( pkFrustum, bForce );
}


unsigned int EditBox::ProcessMsg( Msg *pkMsg )
{
	if( pkMsg->m_eID == Msg::MOUSE_UP )
	{
		Coord kMouse( pkMsg->m_uiData[2], pkMsg->m_uiData[3] );

		if( !pkMsg->m_uiData[1] && ( HitTest( kMouse ) != OUTSIDE ) )
		{
			//Grab focus
			Msg *pkNewMsg = new Msg( Msg::KEYBOARD_FOCUS, this, 0 );

			GetRoot()->ProcessMsg( pkNewMsg );
            
			delete pkNewMsg;
		}
	}
	else if( pkMsg->m_eID == Msg::KEYBOARD_FOCUS )
	{
		if( pkMsg->m_pkSender->GetID() != GetID() )
		{
			//Lost focus
			if( m_pkCursor )
				m_pkCursor->Deactivate();

			m_bInputFocus      = false;
		}
		else
		{
			//Gained focus
			if( m_pkCursor )
				m_pkCursor->Activate();

			m_bInputFocus      = true;
			m_fCursorBlinkTime = 0.0f;
		}
	}
	else if( ( pkMsg->m_eID == Msg::KEYBOARD_DOWN ) && m_bInputFocus )
	{
		if( pkMsg->m_uiData[1] )
			m_pkTextArea->m_strText += (char)pkMsg->m_uiData[1];
		else if( ( pkMsg->m_uiData[0] == KC_BACKSPACE ) && m_pkTextArea->m_strText.size() )
			m_pkTextArea->m_strText.resize( m_pkTextArea->m_strText.size() - 1 );

		if( m_pkCursor )
		{
			Coord kPos( m_pkTextArea->GetTextPadding() );

			int iTextSize = m_pkTextArea->GetFont()->GetStringLength( m_pkTextArea->m_strText );

			if( iTextSize )
				++iTextSize;

			kPos.x += iTextSize;

			m_pkCursor->SetPosition( kPos );
			m_pkCursor->Activate();

			m_fCursorBlinkTime = 0.0f;
		}
	}

	return BorderArea::ProcessMsg( pkMsg );
}


Object *EditBox::Duplicate( Object *pkParent, Object *pkObject )
{
	if( !pkObject )
		pkObject = new EditBox( pkParent, this );
	else
	{
		BorderArea::Duplicate( pkParent, pkObject );

		EditBox *pkEdit = dynamic_cast< EditBox* >( pkObject );

		pkEdit->m_pkTextArea = dynamic_cast< TextArea* >( pkEdit->GetArea( BorderArea::CENTER ) );
		pkEdit->m_pkCursor   = pkEdit->GetObject( "Cursor" );
	}

	return pkObject;
}


void EditBox::Update( float fDeltaTime )
{
	if( m_bInputFocus )
	{
		m_fCursorBlinkTime += fDeltaTime;

		if( m_fCursorBlinkTime > 0.6f )
		{
			if( m_pkCursor )
				m_pkCursor->Toggle();

			m_fCursorBlinkTime -= 0.6f;
		}
	}
}





#ifdef HAVE_NEOCHUNKIO


int EditBoxChunk::ParseData( unsigned int uiFlags, FileManager *pkFileManager )
{
	//Chunk *pkChunk = 0;
	int      iRet    = 0;

	if( !m_pkObject )
		m_pkObject = new EditBox( 0, 0, false );

	EditBox *pkEditBox = dynamic_cast< EditBox* >( m_pkObject );

	if( ( iRet = BorderAreaChunk::ParseData( uiFlags, pkFileManager ) ) <= 0 )
		return iRet;

	pkEditBox->m_pkTextArea = dynamic_cast< TextArea* >( pkEditBox->GetArea( BorderArea::CENTER ) );
	pkEditBox->m_pkCursor   = pkEditBox->GetObject( "Cursor" );

	if( pkEditBox->m_pkCursor )
	{
		pkEditBox->m_pkCursor->SetPosition( pkEditBox->m_pkTextArea->GetTextPadding() );
		pkEditBox->m_pkCursor->Deactivate();
	}

	return 1;
}


#endif


}; /*! namespace NeoWTK */





See more files for this project here

NeoEngineNG

NeoenEngine NG (Next Generation) is the evolution of neoengine one,it\'s a different development from NeoEngine2, it\'s a direct inherits from NeoEngine one.\n

Project homepage: http://sourceforge.net/projects/neoengineng
Programming language(s): C,C++
License: other

  Makefile.am
  SConscript
  area.cpp
  area.h
  attribute.cpp
  attribute.h
  base.cpp
  base.h
  borderarea.cpp
  borderarea.h
  button.cpp
  button.h
  chunktype.h
  coord.cpp
  coord.h
  core.cpp
  core.h
  dll.cpp
  editbox.cpp
  editbox.h
  msg.cpp
  msg.h
  msghook.cpp
  msghook.h
  neowtk-static.dev
  neowtk.cbp
  neowtk.dev
  neowtk.dsp
  neowtk.layout
  neowtk.vcproj
  object.cpp
  object.h
  textarea.cpp
  textarea.h
  widgetlib.cpp
  widgetlib.h