
//ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
//	FILE	: TipWin.cpp
//	AUTHOR	: Catalin Cocos
//__________________________________________________________________________________________________

#include "stdinc.h"
#include "tipwin.h"

int CTip::nCount = 0; 

CTip::CTip(HWND hwnd)
{
	m_hAppWnd = hwnd;
	if(!nCount++)
	{
		WNDCLASS wcls =
		{ 0, CTip::SWProc, 0, 0, hInstance,
		  NULL,
		  NULL,
		  (HBRUSH)(GetSysColorBrush(COLOR_INFOBK)),
		  NULL,"ToolTipTT"
		};
		RegisterClass(&wcls);
	}
	m_szString = NULL;
	m_bVisible = FALSE;
	m_hwnd = CreateWindowEx(0, "ToolTipTT", "", WS_BORDER|WS_POPUP ,
		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, hwnd, NULL , hInstance, NULL);
	SetProp(m_hwnd, "CT", this);
	ICONMETRICS im = {sizeof(ICONMETRICS)};
	SystemParametersInfo( SPI_GETICONMETRICS, sizeof(ICONMETRICS), &im, 0);
	m_hfont = CreateFontIndirect(&im.lfFont);
	SendMessage( m_hwnd, WM_SETFONT, (WPARAM)m_hfont, FALSE);
}
CTip::~CTip()
{
	if(m_hwnd) DestroyWindow( m_hwnd );
	if(m_szString) delete[] m_szString;
	DeleteObject( m_hfont );
}

void CTip::Show( char* szText, HWND hParentWnd, POINT* pPos)
{
	BOOL ERASE = !(m_bVisible && szText && m_szString && !strcmp(szText,m_szString));
	if(ERASE)
	{
		if(m_szString) delete[]m_szString;
		m_szString = NULL;
		if(szText && *szText)
			m_szString = strcpy( new char[lstrlen(szText) +1], szText);
	}
	if(m_szString)
	{
		m_hParentWnd = hParentWnd;
		SIZE size;
		HDC dc = GetDC( m_hwnd );
		HFONT hOldF = SelectObject( dc, m_hfont);
		GetTextExtentPoint32( dc, m_szString, strlen(m_szString), &size);
		SelectObject( dc, hOldF);
		ReleaseDC( m_hwnd, dc );
		if(pPos) MoveWindow(m_hwnd,pPos->x,pPos->y,size.cx+4,size.cy+4,TRUE); 
		else
		{
			POINT p;
    		GetCursorPos( &p );
		    MoveWindow(m_hwnd,p.x+12,p.y+20,size.cx+4,size.cy+4,TRUE); 
		}
		SetWindowText( m_hwnd, m_szString);
		InvalidateRect( m_hwnd, NULL, ERASE);
        if(!m_bVisible)
		{	
			m_bVisible = TRUE;
			ShowWindow(m_hwnd,SW_SHOWNA);
 		}
	}
	else if(m_bVisible)
	{
		ReleaseCapture();
		ShowWindow(m_hwnd,SW_HIDE);
		m_bVisible = FALSE;
	}
}

void CTip::Test( HWND hParentWnd, LPARAM mousepos)
{
	POINT p = {LOWORD(mousepos), HIWORD(mousepos)};
	RECT r;
	GetClientRect(hParentWnd,&r);
	if(!PtInRect(&r,p) && m_bVisible)
		Show( NULL, hParentWnd);	
}

LONG CALLBACK CTip::SWProc( HWND  hwnd, UINT msg, WPARAM  wp, LPARAM  lp )
{
	CTip* ct = (CTip*)GetProp( hwnd, "CT" );
	if(ct) return ct->WProc( hwnd, msg, wp, lp );
	else return DefWindowProc( hwnd, msg, wp, lp );
}

LONG CALLBACK CTip::WProc( HWND  hwnd, UINT msg, WPARAM  wp, LPARAM  lp )
{
	switch(msg)
	{
	case WM_DESTROY: m_hwnd = NULL; return DefWindowProc( hwnd, msg, wp, lp );
	case WM_LBUTTONUP: case WM_RBUTTONUP:
	case WM_MOUSEMOVE: case WM_LBUTTONDOWN: case WM_RBUTTONDOWN:
		if(m_bVisible)
		{
			POINT p = { LOWORD(lp), HIWORD(lp) };
			MapWindowPoints( m_hwnd, m_hParentWnd, &p, 1 );
			SendMessage(m_hParentWnd, msg, wp, p.x |(p.y<<16));
		}
		break;
	case WM_PAINT:
		{
			SetCapture(hwnd);
			RECT r;
			GetClientRect(hwnd, &r);
			r.top = r.left = 1;
			PAINTSTRUCT ps;
			HDC dc = BeginPaint(hwnd, &ps);
			HFONT hOldF = SelectObject( dc, m_hfont);
			SetTextColor( dc, GetSysColor( COLOR_INFOTEXT ));
			SetBkMode( dc, TRANSPARENT );
			DrawText( dc, m_szString, strlen(m_szString), &r, DT_WORDBREAK|DT_EXPANDTABS);
			SelectObject( dc, hOldF);
			EndPaint(hwnd,&ps);
		}
		break;
	default:
		return DefWindowProc( hwnd, msg, wp, lp );
	}
	return 0;
}
