Click here to Skip to main content
15,896,207 members
Articles / Desktop Programming / MFC

Brainchild, A syntax coloring edit control

Rate me:
Please Sign up or sign in to vote.
4.85/5 (64 votes)
16 Jun 2005CPOL5 min read 711.4K   26.8K   263  
Syntax coloring, multi-level undo/redo editor control.
//
//	page.cpp
//
//	(C) Copyright 2000-2002 by Jan van den Baard.
//	    All Rights Reserved.
//

#include "bcc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// Constructor.
Page::Page() 
{
	// Setup defaults.
	m_pParser = NULL;
}

// Destructor.
Page::~Page()
{
}

// Re-act on an edit control event.
void Page::HandleEditControl( UINT nNotifyCode, UINT nCtrlID, LPTSTR *pStrPtr, BOOL bChange /* = TRUE */ )
{
	// Was the focus lost?
	if ( nNotifyCode == EN_KILLFOCUS )
	{
		// Get the contents of the control.
		LPTSTR pszText = CopyEditContents( nCtrlID );
		if ( pszText )
		{
			// Release current contents.
			if ( *pStrPtr ) ::FreePooled( pParserPool, *pStrPtr );

			// Check if it has valid contents.
			for ( size_t i = 0; i <= _tcslen( pszText ); i++ )
			{
				// If we read the end it were all blanks.
				if ( pszText[ i ] == _T( '\0' ))
				{
					// Free the memory.
					::FreePooled( pParserPool, pszText );

					// Clear edit control contents.
					::SetWindowText( GetDlgItemHandle( nCtrlID ), _T( "" ));

					// Clear the pointer.
					*pStrPtr = NULL;
					return;
				}

				// Is it a white space?
				if ( ! _istspace( pszText[ i ] )) break;
			}

			// Setup new contents.
			*pStrPtr = pszText;
		}
		else
			// Error...
			MessageBox( ClsString( MAKEINTRESOURCE( IDS_NO_MEMORY )), ClsGetApp()->GetAppTitle(), MB_ICONERROR | MB_OK );
	}
	else if ( bChange == TRUE && nNotifyCode == EN_CHANGE && ( ::GetFocus() == GetDlgItemHandle( nCtrlID )))
		// Changed...
		if ( m_pParser ) pSettings->Changed( m_pParser );
}
	
// Copy the contents of an edit control.
LPTSTR Page::CopyEditContents( UINT nCtrlID )
{
	// Get the edit control.
	HWND hEdit = GetDlgItemHandle( nCtrlID );

	// First we see how many bytes we need for the new buffer.
	int nLen = ::GetWindowTextLength( hEdit );
	
	// Allocate buffer.
	LPTSTR pszBuffer = ( LPTSTR )::AllocPooled( pParserPool, nLen + 1 );
	if ( pszBuffer )
	{
		// Get contents.
		::GetWindowText( hEdit, pszBuffer, nLen + 1 );
		return pszBuffer;
	}
	return NULL;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
I have been programming for a hobby since 1985. I have started programming on the C= 64. After that I migrated to the C= Amiga which I traded in for a PC back in 1997 I believe. Back in 2000 I decided to lose a hobby and start developing software for a living.

Currently I am working mainly in developing software for building security and access control systems.

Comments and Discussions