/*=========================================================================
 * CPAWButo.cpp :	Implementation of Window Button for the PopUpToolBar.
 * This is a part of the PCA project.
 *
 * Version			1.0
 * Creation date	26/06/96
 * Revision date
 *
 *
 * (c) Ubi Studios 1996
 *=======================================================================*/

#include "stdafx.h"

#ifdef ACTIVE_EDITOR
#include "itf/CPAPopTB.hpp"
#include "itf/CPAWButo.hpp"
#include "itf/CPARes.h"
#include "itf/frmbsmn.hpp"
/////////////////////////////////////////////////////////////////////////////
// CPA_WndButton

CPA_WndButton::CPA_WndButton()
{
}

CPA_WndButton::~CPA_WndButton()
{
}

BEGIN_MESSAGE_MAP(CPA_WndButton, CWnd)
	//{{AFX_MSG_MAP(CPA_WndButton)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPA_WndButton message handlers

int CPA_WndButton::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	return 0;
}

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

int CPA_WndButton::CreateToolBarCtrl()
{
	long i;
	TBBUTTON a_stTBBut[C_MAX_BUTTON_PER_FAMILY];

	m_oToolBarCtrl.AddBitmap(m_lNbButtonsTotal,m_lIdResource);

	for (i = 0 ; i < m_lNbButtons ; i++ )
	{
		if ( m_a_stButtons[i].lId != NULL)
		{
			a_stTBBut[i].fsState = (m_a_stButtons[i].bPermit)?TBSTATE_ENABLED:TBSTATE_INDETERMINATE;// button state
			a_stTBBut[i].fsStyle = TBSTYLE_BUTTON;		// button style--see below
			a_stTBBut[i].dwData = NULL;			// application-defined value
			a_stTBBut[i].iString = NULL;		// zero-based index of button label string
			if ( m_a_stButtons[i].lId == ID_SEPARATOR )
			{
				a_stTBBut[i].iBitmap = 0 ;
				a_stTBBut[i].idCommand = 0 ;
				a_stTBBut[i].fsStyle = TBSTYLE_SEP ;
			}
			else
			{
				a_stTBBut[i].iBitmap = m_a_stButtons[i].lIndexBitmap;
				a_stTBBut[i].idCommand = m_a_stButtons[i].lId ;
			}
		}
	}
	m_oToolBarCtrl.AddButtons(m_lNbButtons, a_stTBBut );
	m_oToolBarCtrl.AutoSize();
	return 0;
}
///////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
// Create the toolbar in the parent FrameWnd.
int CPA_WndButton::CreateToolBar()
{
	CRect stRect;

	if (!m_oToolBarCtrl.Create(CCS_NODIVIDER|WS_CHILD|WS_VISIBLE,CRect(0,0,0,0),
		this,m_lIdResource))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;
	}
	m_p_oPopUpTBParent->GetItemRect(0,&stRect);
	m_oToolBarCtrl.SetButtonSize(CSize(stRect.right-stRect.left-7, stRect.bottom-stRect.top-6));
	m_oToolBarCtrl.SetBitmapSize(CSize(stRect.right-stRect.left-7, stRect.bottom-stRect.top-6));
	CreateToolBarCtrl();
	return 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////

void CPA_WndButton::CreateWndButton(CPA_PopUpToolBar    *p_oPopUpTB,
									long                lWndPosX,
									long                lWndPosY,
									tdstButton          a_stButtons[],
									long                lNbButtons,
									long                lFamily,
									long				lNbButtonsTotal,
									long				lIdResource)
{
	CRect	stRect;
	CWnd	*p_oWnd = p_oPopUpTB->GetParent()->GetParent();
	CPoint	pt(lWndPosX, lWndPosY);

	p_oWnd->ScreenToClient(&pt);
	Create( NULL, "toto",WS_BORDER|WS_VISIBLE|WS_CHILD,
		CRect(pt.x,pt.y,0,0), p_oWnd, lIdResource);

	g_bMouseLocked = TRUE;

	m_lNbButtonsTotal = lNbButtonsTotal;
	m_lIdResource = lIdResource;
	m_lFamily = lFamily;
	m_lNbButtons = lNbButtons;
	m_p_oPopUpTBParent = p_oPopUpTB;
	m_lIdChosenButton = 0;
	memcpy(m_a_stButtons, a_stButtons,m_lNbButtons * sizeof(tdstButton));
	CreateToolBar();
	m_oToolBarCtrl.GetItemRect(0,&stRect);
	SetWindowPos(&wndTopMost,0,0,m_lNbButtons*stRect.right,stRect.bottom,SWP_NOMOVE|SWP_NOZORDER|SWP_SHOWWINDOW);
	m_p_oGrabCapture();
	SetFocus();
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
void CPA_WndButton::OnSize(UINT nType, int cx, int cy)
{
	CWnd::OnSize(nType, cx, cy);

	if (m_oToolBarCtrl.m_hWnd != NULL)
	  m_oToolBarCtrl.AutoSize();
}

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
void CPA_WndButton::OnLButtonUp( UINT nflags, CPoint oPoint )
{
	CWnd::OnLButtonUp(nflags,oPoint);

	int		i;
	long	lParam;

	if(m_p_oPopUpTBParent->m_bJustPressed)
	{
		m_p_oPopUpTBParent->m_bJustPressed = FALSE;
		if(m_p_oPopUpTBParent->m_bCurrentButtonAlone)
		{
			m_p_oPopUpTBParent->PressButton(m_p_oPopUpTBParent->m_lIdCurrentButton, FALSE);
			m_p_oPopUpTBParent->GetParent()->SendMessage(WM_COMMAND, m_p_oPopUpTBParent->m_lIdCurrentButton);
			DestroyWindow();
			g_bMouseLocked = FALSE;
		}
		return;
	}

	for(i = 0; i < m_oToolBarCtrl.GetButtonCount(); i++)
	{
		if(m_oToolBarCtrl.IsButtonPressed(m_a_stButtons[i].lId))
		{
			m_lIdChosenButton = m_a_stButtons[i].lId;
			break;
		}
	}
	((FRMBaseMenu*)(m_p_oPopUpTBParent->GetParent()->GetParent()))->UpdateStatus(NULL,
																		  		 C_STATUSPANE_INFOS,
																				 C_STATUS_NORMAL);
	m_p_oSurrenderCapture();
	// tells popuptoolbar about buttonup...
	lParam = (oPoint.x + (oPoint.y << 16));
	m_p_oPopUpTBParent->SendMessage(WM_LBUTTONUP,nflags,lParam);
	DestroyWindow();
	g_bMouseLocked = FALSE;
}

//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
void CPA_WndButton::OnMouseMove( UINT nflags, CPoint oPoint )
{
	CRect	rect;
	int		i;


	// first, release the buttons if necessary
	for(i = 0; i < m_lNbButtons; i++)
	{
		m_oToolBarCtrl.GetItemRect(i, &rect);

		// if the mouse is not on the button and if the button is pressed -> release it
		if (m_oToolBarCtrl.IsButtonPressed(m_a_stButtons[i].lId) && !rect.PtInRect(oPoint))
		{
			m_oToolBarCtrl.PressButton( m_a_stButtons[i].lId, FALSE);
			((FRMBaseMenu*)(m_p_oPopUpTBParent->GetParent()->GetParent()))->UpdateStatus(NULL,
																						 C_STATUSPANE_INFOS,
																						 C_STATUS_NORMAL);
		}
	}
	
	//then, press the the good button if necessary
	for(i = 0; i < m_lNbButtons; i++)
	{
		m_oToolBarCtrl.GetItemRect(i, &rect);
		
		// if the mouse is on the button and if the button is not pressed -> press it
		if (rect.PtInRect(oPoint) && !m_oToolBarCtrl.IsButtonPressed(m_a_stButtons[i].lId))
		{
			if (m_oToolBarCtrl.IsButtonEnabled(m_a_stButtons[i].lId))
			{
				m_oToolBarCtrl.PressButton(m_a_stButtons[i].lId, TRUE);
				((FRMBaseMenu*)(m_p_oPopUpTBParent->GetParent()->GetParent()))->UpdateStatus(m_a_stButtons[i].szTipText,
					C_STATUSPANE_INFOS,
					C_STATUS_NORMAL);
			}
		}
	}
}


#endif // ACTIVE_EDITOR
