Click here to Skip to main content
15,886,199 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 705K   26.8K   263  
Syntax coloring, multi-level undo/redo editor control.
/*
 *	tooltip.c
 *
 *	(C) Copyright 2005 Jan van den Baard.
 *	    All Rights Reserved.
 */

#include "defs.h"

/*
 *	Compute the tip position. It will always be
 *	two pixels below the line on which the mouse
 *	cursor is located.
 */
DWORD TipPositionByMouse( LPCLASSDATA lpcd )
{
	/*
	 *	Obtain the mouse position.
	 */
	POINT pt;
	GetCursorPos( &pt );

	/*
	 *	Convert to client coordinates.
	 */
	ScreenToClient( lpcd->hWnd, &pt );

	/*
	 *	Now adjust the y position to be 2 pixels
	 *	below the bottom of the line on which the 
	 *	mouse cursor is located.
	 */
	pt.y = ((( pt.y / Parser->szCharSize.cy ) + 1 ) * Parser->szCharSize.cy ) + 2;

	/*
	 *	Convert the coordinates back to
	 *	screen coordinates and return the
	 *	result.
	 */
	ClientToScreen( lpcd->hWnd, &pt );
	return ( MAKELONG( pt.x, pt.y ));
}

/*
 *	Static tooltip buffer. The code below will
 *	make sure that the tooltip will never grow
 *	beyond the size of this buffer.
 */
TCHAR szTipBuffer[ 1024 ];

void SetTooltipText( LPCLASSDATA lpcd, LPCTSTR pszText )
{
	/*
	 *	Show tips? Is the tooltip opened?
	 */
	if ( Parser->bShowHyperTips && lpcd->bTipShowing )
	{
		TOOLINFO ti = { 0 };

		/*
		 *	Truncate the tip string if it
		 *	grows beyond 1024 characters.
		 */
		int nLength = _tcslen( pszText );
		if ( nLength < 1024 ) _tcscpy( szTipBuffer, pszText );
		else _tcsncpy( szTipBuffer, pszText, 1024 );

		/*
		 *	Setup the tooltip.
		 */
		ti.cbSize   = sizeof( ti );
		ti.hwnd     = lpcd->hWnd;
		ti.uId      = ( UINT_PTR )( HWND )lpcd->hWnd;
		ti.hinst    = GetModuleHandle( NULL );
		ti.lpszText = szTipBuffer;

		/*
		 *	Get the current tip text.
		 */
		SendMessage( lpcd->hTooltip, TTM_GETTEXT, 0, ( LPARAM )&ti );

		/*
		 *	Did it realy change?
		 */
		if ( _tcscmp( szTipBuffer, pszText ) != 0 )
		{
			/*
			 *	Set the new text.
			 */
			ti.lpszText = ( LPTSTR )pszText;

			/*
			 *	Update and position the tooltip.
			 */
			SendMessage( lpcd->hTooltip, TTM_UPDATETIPTEXT, 0, ( LPARAM )&ti );
			SendMessage( lpcd->hTooltip, TTM_TRACKPOSITION, 0, TipPositionByMouse( lpcd ));
		}
	}
}

void ShowTooltip( LPCLASSDATA lpcd, LPCTSTR pszText )
{
	TOOLINFO ti = { 0 };

	/*
	 *	Show tips?
	 */
	if ( Parser->bShowHyperTips )
	{
		/*
		 *	Truncate the tooltip string if it
		 *	grows larger than 1024 characters.
		 */
		int nLength = _tcslen( pszText );
		if ( nLength < 1024 ) _tcscpy( szTipBuffer, pszText );
		else _tcsncpy( szTipBuffer, pszText, 1024 );

		/*
		 *	Setup the tooltip.
		 */
		ti.cbSize   = sizeof( ti );
		ti.hwnd     = lpcd->hWnd;
		ti.uId      = ( UINT_PTR )( HWND )lpcd->hWnd;
		ti.hinst    = GetModuleHandle( NULL );
		ti.lpszText = szTipBuffer;

		/*
		 *	Update and show the tooltip.
		 */
		SendMessage( lpcd->hTooltip, TTM_UPDATETIPTEXT, 0, ( LPARAM )&ti );
		SendMessage( lpcd->hTooltip, TTM_TRACKPOSITION, 0, TipPositionByMouse( lpcd ));
		SendMessage( lpcd->hTooltip, TTM_TRACKACTIVATE, TRUE, ( LPARAM )&ti );

		/*
		 *	Tooltip visible.
		 */
		lpcd->bTipShowing = TRUE;
	}
}

void HideTooltip( LPCLASSDATA lpcd )
{
	/*
	 *	Is the tooltip visible?
	 */
	if ( lpcd->bTipShowing )
	{
		/*
		 *	Setup the TOOLINFO structure.
		 */
		TOOLINFO ti = { 0 };
		ti.cbSize   = sizeof( ti );
		ti.hwnd     = lpcd->hWnd;
		ti.uId	    = ( UINT_PTR )( HWND )lpcd->hWnd;

		/*
		 *	Hide the tooltip.
		 */
		SendMessage( lpcd->hTooltip, TTM_TRACKACTIVATE, FALSE, ( LPARAM )&ti );
		lpcd->bTipShowing = FALSE;
	}
}

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