/*=========================================================================
 * CPAPopTB :	Implementation of 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 "acp_base.h"
#include "geo.h"
#include "gli.h"
#include "itf/CPAPopTB.hpp"
#include "itf/DEVMulti.hpp"
#include "itf/DEVViewp.hpp"
#include "itf/CPAProj.hpp"

BOOL g_bMouseLocked = FALSE;

/////////////////////////////////////////////////////////////////////////////
// CPA_PopUpToolBar

CPA_PopUpToolBar::CPA_PopUpToolBar()
{
	m_bMousePickedButton = FALSE;
	m_lNbButtonsTotal = 0;
	m_lNbFamilies = 0;
	m_bCurrentButtonAlone = FALSE;
	m_bDefined = FALSE;
	m_bJustPressed = FALSE;
	m_bAllreadyCreated = FALSE;
}

CPA_PopUpToolBar::~CPA_PopUpToolBar()
{
}



// copy constructor : the toolbar is built and defines, but not created
CPA_PopUpToolBar::CPA_PopUpToolBar(CPA_PopUpToolBar &r_src)
{
	long k;
	
	m_bMousePickedButton = FALSE;
	m_bCurrentButtonAlone = FALSE;
	m_bJustPressed = FALSE;
	
	m_lNbButtonsTotal = r_src.m_lNbButtonsTotal;
	m_lIdResource = r_src.m_lIdResource;
	m_lIdEndFamily = r_src.m_lIdEndFamily;
	m_lButtonSizeX = r_src.m_lButtonSizeX;
	m_lButtonSizeY = r_src.m_lButtonSizeY;
	m_lIdCurrentButton = r_src.m_lIdCurrentButton;
	m_lNbFamilies = r_src.m_lNbFamilies;
	
	for (k=0; k<m_lNbFamilies; k++)
	{
		m_a_lFamiliesIndex[k] = r_src.m_a_lFamiliesIndex[k];
		m_a_lInitialStates[k] = r_src.m_a_lInitialStates[k];
	}
	
	for (k = 0; k < m_lNbButtonsTotal; k++)
	{
		m_a_stButtons[k] = r_src.m_a_stButtons[k];
		m_a_bInitialPermissions[k] = r_src.m_a_bInitialPermissions[k];

		// set initial permissions
		m_a_stButtons[k].bPermit = m_a_bInitialPermissions[k];
	}

	for (k=0; k<m_lNbFamilies; k++)
	{
		// copy toolbar state
//		m_a_lButtonsInTBIndex[k] = r_src.m_a_lButtonsInTBIndex[k];

		// set toolbar state to initial values
		m_a_lButtonsInTBIndex[k] = GetIndex(m_a_lInitialStates[k]);
	}
	
	m_bDefined = TRUE;
	m_bAllreadyCreated = r_src.m_bAllreadyCreated;
}







BEGIN_MESSAGE_MAP(CPA_PopUpToolBar, CToolBarCtrl)
	//{{AFX_MSG_MAP(CPA_PopUpToolBar)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()



/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// gets the Id of the initial state of each family
long CPA_PopUpToolBar::GetInitialStates(long a_lStates[])
{
	long i;

	for (i = 0; i < m_lNbFamilies; i++)
		a_lStates[i] = m_a_lInitialStates[i];

	return m_lNbFamilies;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// gets the initial permission of all butttons
long CPA_PopUpToolBar::GetInitialPermissions(BOOL a_bPermissions[])
{
	long i;

	for (i = 0; i < m_lNbButtonsTotal; i++)
		a_bPermissions[i] = m_a_bInitialPermissions[i];

	return m_lNbButtonsTotal;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// gets the Id of all the buttons visible in the toolbar
long CPA_PopUpToolBar::GetStates(long a_lStates[])
{
	long i;

	for (i = 0; i < m_lNbFamilies; i++)
		a_lStates[i] = m_a_stButtons[m_a_lButtonsInTBIndex[i]].lId;

	return m_lNbFamilies;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
long CPA_PopUpToolBar::GetIndex(long lIdButton)
{
	for (int i=0;i<m_lNbButtonsTotal;i++)
	{
		if (lIdButton == m_a_stButtons[i].lId)
			return i;
	}
	return -1;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the visibility of the given button
void CPA_PopUpToolBar::SetVisibility(long lId, BOOL bVisible)
{
	long lIndex;

	lIndex = GetIndex(lId);
	ASSERT(lIndex != -1);

	m_a_stButtons[lIndex].bVisible = bVisible;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the visibility of the given buttons
void CPA_PopUpToolBar::SetVisibility(long a_lIdButtons[], BOOL a_bVisible[],long lNbButtons)
{
	long i;

	for (i=0;i<lNbButtons;i++)
	{
		SetPermission(a_lIdButtons[i],a_bVisible[i]);
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// put the given button in the toolbar, at the appropriate
// position (acording to its family)
void CPA_PopUpToolBar::SetButton(long lId)
{
	TBBUTTON TBBut;
	long lFamily,lIndex;

	if (lId == m_lIdEndFamily)
		return;
	lIndex = GetIndex(lId);
	ASSERT(lIndex != -1);

	if (IsStandAlone(lId) == FALSE)
		ASSERT(m_a_stButtons[lIndex].bPermit == TRUE);

	if (m_a_stButtons[lIndex].bIsAMode == FALSE)
		return;

	lFamily = GetFamily(lId);
	ASSERT(lFamily != -1);

	GetButton(lFamily, &TBBut);
	// Which bitmap ?

	TBBut.iBitmap = m_a_stButtons[lIndex].lIndexBitmap;
	TBBut.idCommand = lId;
	InsertButton(lFamily, &TBBut);
	DeleteButton(lFamily+1);
	m_a_lButtonsInTBIndex[lFamily] = lIndex;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// put the given buttons in the toolbar, at the appropriate
// position (acording to its family)
void CPA_PopUpToolBar::SetButtons(long pId[])
{
	UINT i;
	for (i = 0; i < (unsigned int) m_lNbFamilies; i++)
		SetButton(pId[i]);
    RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW | RDW_ERASE);
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// ask if the given button exists in this toolbar
BOOL CPA_PopUpToolBar::IsPresent(long lIdButton)
{
	for (int i=0;i<m_lNbButtonsTotal;i++)
	{
		if (lIdButton == m_a_stButtons[i].lId)
			return TRUE;
	}
	return FALSE;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// ask if the given button is on the screen
BOOL CPA_PopUpToolBar::IsOnScreen(long lIdButton)
{
	for (int i=0;i<m_lNbFamilies;i++)
	{
		if (lIdButton == m_a_stButtons[m_a_lButtonsInTBIndex[i]].lId)
			return TRUE;
	}
	return FALSE;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// ask is the given button is permitted
BOOL CPA_PopUpToolBar::IsPermitted(long lId)
{
	long lIndex;

	lIndex = GetIndex(lId);
	ASSERT(lIndex != -1);

	return (m_a_stButtons[lIndex].bPermit);
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// ask is the given button is alone in its family
BOOL CPA_PopUpToolBar::IsStandAlone(long lId)
{
	long lFamily;

	lFamily = GetFamily(lId);
	return (GetNbButtonsTotalInFamily(lFamily) == 1);
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// ask if the given button is a mode button
BOOL CPA_PopUpToolBar::IsAMode(long lId)
{
	long lIndex;

	lIndex = GetIndex(lId);
	ASSERT(lIndex != -1);

	return (m_a_stButtons[lIndex].bIsAMode);
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the permission of a button
void CPA_PopUpToolBar::SetPermission(long lId, BOOL bPermit)
{
	long lIndex;

	if (lId == m_lIdEndFamily)
		return;

	lIndex = GetIndex(lId);
	ASSERT(lIndex != -1);

	m_a_stButtons[lIndex].bPermit = bPermit;

	if (IsStandAlone(lId))
	{
		// stand-alone button, we must disable it in the toolbar
		EnableButton(lId,bPermit);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the permission of several buttons
void CPA_PopUpToolBar::SetPermissions(long a_lIdButtons[], BOOL a_bPermit[], long lNbButtons)
{
	long i;

	for (i=0;i<lNbButtons;i++)
	{
		SetPermission(a_lIdButtons[i],a_bPermit[i]);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the initial permissions of several buttons
void CPA_PopUpToolBar::SetInitialPermissions(long a_lIdButtons[], BOOL a_bPermit[], long lNbButtons)
{
	long i,lIndex;

	for (i=0;i<lNbButtons;i++)
	{
		lIndex = GetIndex(a_lIdButtons[i]);
		ASSERT(lIndex != -1);

		m_a_bInitialPermissions[lIndex] = a_bPermit[i];
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the initial state of several buttons
void CPA_PopUpToolBar::SetInitialStates(long a_lIdButtons[],long lNbButtons)
{
	long i,lFamily;

	for (i=0;i<lNbButtons;i++)
	{
		lFamily = GetFamily(a_lIdButtons[i]);
		ASSERT(lFamily != -1);

		m_a_lInitialStates[lFamily] = a_lIdButtons[i];
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the initial permissions of one button
void CPA_PopUpToolBar::SetInitialPermission(long lIdButton,BOOL bPermit)
{
	long lIndex;

	lIndex = GetIndex(lIdButton);
	ASSERT(lIndex != -1);

	m_a_bInitialPermissions[lIndex] = bPermit;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the initial state of one button
void CPA_PopUpToolBar::SetInitialState(long lIdButton)
{
	long lFamily;

	lFamily = GetFamily(lIdButton);

	ASSERT(lFamily != -1);

	m_a_lInitialStates[lFamily] = lIdButton;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// sets the permission of all buttons
void CPA_PopUpToolBar::SetPermissions(BOOL a_bPermit[])
{
	long i;

	for (i=0;i<m_lNbButtonsTotal;i++)
	{
		SetPermission(m_a_stButtons[i].lId,a_bPermit[i]);
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// tooltips
BOOL CPA_PopUpToolBar::GetToolTipText(LPTOOLTIPTEXT p_ToolTipText, UINT Id)
{
	long lButtonIndex;

	lButtonIndex = GetIndex(Id);
	if (lButtonIndex == -1) return FALSE;

	strcpy(p_ToolTipText->szText,m_a_stButtons[lButtonIndex].szTipText);
	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// tells the toolbarcontrol how many buttons there are, and draw the visible ones.
long CPA_PopUpToolBar::CreateToolBarCtrl(CWnd *p_WndParent, CPoint org)
{
	TBBUTTON a_stTBBut[C_MAX_POPUPTOOLBAR_FAMILY];
	long iBitmap;
	long i;

	Create(CCS_NODIVIDER | CBRS_TOOLTIPS| TBSTYLE_TOOLTIPS |
			WS_CLIPSIBLINGS | WS_CHILD | WS_VISIBLE | CBRS_SIZE_DYNAMIC,
		   CRect(org.x,org.y,20,50),
		   p_WndParent,
		   m_lIdResource);

	SetButtonSize(CSize(m_lButtonSizeX,m_lButtonSizeY));
	SetBitmapSize(CSize(m_lButtonSizeX,m_lButtonSizeY));

	iBitmap = AddBitmap(m_lNbButtonsTotal, m_lIdResource);

	// sets buttons bitmaps
	for (i = 0; i < m_lNbButtonsTotal; i++)
		m_a_stButtons[i].lIndexBitmap = i + iBitmap;

	if (!m_bAllreadyCreated)
	{
		// sets buttons initial state
		for ( i = 0; i < m_lNbFamilies; i++)
			m_a_lButtonsInTBIndex[i] = GetIndex(m_a_lInitialStates[i]);

		// sets buttons initial permissions
		for (i = 0; i < m_lNbButtonsTotal; i++)
			m_a_stButtons[i].bPermit = m_a_bInitialPermissions[i];
	}
	m_bAllreadyCreated = TRUE;

	// add buttons
	for ( i = 0; i < m_lNbFamilies; i++)
	{
		if (IsStandAlone(m_a_stButtons[m_a_lButtonsInTBIndex[i]].lId))
			ASSERT(m_a_stButtons[m_a_lButtonsInTBIndex[i]].bPermit);

		ASSERT(IsAMode(m_a_stButtons[m_a_lButtonsInTBIndex[i]].lId));

		a_stTBBut[i].fsState = (m_a_stButtons[m_a_lButtonsInTBIndex[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
		a_stTBBut[i].iBitmap = m_a_stButtons[m_a_lButtonsInTBIndex[i]].lIndexBitmap;
		a_stTBBut[i].idCommand = m_a_lInitialStates[i];
	}
	AddButtons( m_lNbFamilies, a_stTBBut );

	RECT	stButtonRect;
    GetItemRect(0,&stButtonRect);
	MoveWindow
	(
		org.x, org.y, 
		(stButtonRect.right - stButtonRect.left + 1) * m_lNbFamilies, 
		(stButtonRect.bottom - stButtonRect.top + 2)
	);
	return 0;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// returns the family number of the given button;
// return -1 if button couldn't be found
// the family number is also the zero-based position of the button in the toolbar
long CPA_PopUpToolBar::GetFamily(long lIdButton)
{
	long i,lfamily;

	ASSERT (lIdButton != m_lIdEndFamily);

	lfamily = 0;
	for (i=0; i<m_lNbButtonsTotal;i++)
	{
		if (m_a_stButtons[i].lId == lIdButton)
			return lfamily;
		if (m_a_stButtons[i].lId == m_lIdEndFamily)
			lfamily++;
	}
	return -1;
}



/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// return the total number of buttons in the given family
// (not included the EndFamily)
long CPA_PopUpToolBar::GetNbButtonsTotalInFamily(long lFamily)
{
	long lNbButtons,lFamilyIndex;

	ASSERT( (lFamily >=0) && (lFamily < m_lNbFamilies));

	lNbButtons = 0;
	lFamilyIndex = m_a_lFamiliesIndex[lFamily];

	while(m_a_stButtons[lFamilyIndex++].lId != m_lIdEndFamily)
		lNbButtons++;

	ASSERT(lNbButtons != 0);
	return lNbButtons;
}



/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// choose, in the given family, an available button that is different
// from the given one.
long CPA_PopUpToolBar::GetOtherAvailableButtonInFamily(long lIdButton,
													   BOOL a_bPermissions[],
													   long lFamily)
{
	UINT i;
	int k;

	// get teh family of the button
	if (lFamily == -1)
	{
		lFamily = GetFamily(lIdButton);
	}

	ASSERT(lFamily != -1);


	// let's see if the initial button of the family suits
	if (a_bPermissions == NULL)
	{
		if ((m_a_lInitialStates[lFamily] != lIdButton) &&
			(m_a_stButtons[GetIndex(m_a_lInitialStates[lFamily])].bPermit) &&
			(m_a_stButtons[GetIndex(m_a_lInitialStates[lFamily])].bIsAMode))

			return m_a_lInitialStates[lFamily];
	}
	else
	{
		if ((m_a_lInitialStates[lFamily] != lIdButton) &&
			(a_bPermissions[GetIndex(m_a_lInitialStates[lFamily])]) &&
			(m_a_stButtons[GetIndex(m_a_lInitialStates[lFamily])].bIsAMode))

			return m_a_lInitialStates[lFamily];
	}

	// get the index of the family
	k = m_a_lFamiliesIndex[lFamily];
	// k is the index of the first button of the family

	for ( i = k; i < (unsigned int) m_lNbButtonsTotal; i++) // scan all buttons of the family
	{
		if (m_a_stButtons[i].lId == m_lIdEndFamily) return -1; // couldn't find a button

		if (a_bPermissions == NULL)
		{
			if ((m_a_stButtons[i].lId != lIdButton) &&
				(m_a_stButtons[i].bPermit) &&
				(m_a_stButtons[i].bIsAMode))

				return m_a_stButtons[i].lId;
		}
		else
		{
			if ((m_a_stButtons[i].lId != lIdButton) &&
				(a_bPermissions[i]) &&
				(m_a_stButtons[i].bIsAMode))

				return m_a_stButtons[i].lId;
		}

	}
	return -1;
}


/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
// fill in the toolbar buttons with all datas
int CPA_PopUpToolBar::Define(tdstButtonDef a_stButtonDef[],
							 long lNbButtons,
							 long lIdResource,
							 long lIdEndFamily,
							 long lSizeX,
							 long lSizeY)
{
	long k;

	ASSERT((lNbButtons > 0) && (lNbButtons <= C_MAX_POPUPTOOLBAR_BUTTONS));

	m_lNbButtonsTotal = lNbButtons;
	m_lIdResource = lIdResource;
	m_lIdEndFamily = lIdEndFamily;
	m_lButtonSizeX = lSizeX;
	m_lButtonSizeY = lSizeY;

	// fills in the button array, and
	//	Count the number of Families
	// a family of buttons is registered if the last button has
	// the m_lIdEndFamily ID !!!!

	m_lNbFamilies = 0;
	m_a_lFamiliesIndex[0] = 0; // the first family begins at button 0;

	for (k = 0; k < m_lNbButtonsTotal; k++)
	{
		m_a_stButtons[k].lId = a_stButtonDef[k].lId;
		m_a_stButtons[k].bPermit = (a_stButtonDef[k].lId != m_lIdEndFamily);
		m_a_bInitialPermissions[k] = (a_stButtonDef[k].lId != m_lIdEndFamily);
		m_a_stButtons[k].bVisible = TRUE;
		m_a_stButtons[k].bIsAMode = a_stButtonDef[k].bIsAMode;
		strcpy(m_a_stButtons[k].szTipText,a_stButtonDef[k].szTip);

		if (m_a_stButtons[k].lId == m_lIdEndFamily)
		{
			ASSERT(m_lNbFamilies < C_MAX_POPUPTOOLBAR_FAMILY-1);
			// separator => one more family
			m_lNbFamilies++;
			m_a_lFamiliesIndex[m_lNbFamilies] = k+1;
		}
	}

	// init : now, sets which buttons are to be displayed : the first one of
	// each family

	for (k=0; k<m_lNbFamilies; k++)
	{
		m_a_lInitialStates[k] = m_a_stButtons[m_a_lFamiliesIndex[k]].lId;
	}
	m_bDefined = TRUE;

	return 0;
}


void CPA_PopUpToolBar::InitAllStatesArray(long aa_lSaveStates[][C_MAX_POPUPTOOLBAR_FAMILY])
{
	long i,k;
	// fills in the save-states array
	for (i=0;i<MAX_DEVICE;i++)
	{
		for (k=0;k<m_lNbFamilies;k++)
		{
			aa_lSaveStates[i][k] = m_a_lInitialStates[k];
		}
	}
}

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

void CPA_PopUpToolBar::OnButton(UINT lId, UINT lFlags, long lIndexInTB)
{
	CRect stViewportRect, stButtonRect;
	long lx, ly, lIndBeginFamily;
	tdstButton a_stButtonsFamily[C_MAX_BUTTON_PER_FAMILY];
	long lNbButtonsFamily;
	long lDx;


	// Where is the new toolbar ?
	GetWindowRect(&stViewportRect);
    GetItemRect(0,&stButtonRect);
	lDx = (stViewportRect.right-stViewportRect.left)-m_lNbFamilies*stButtonRect.right;
	stViewportRect.top += stButtonRect.bottom;
	stViewportRect.left += stButtonRect.right*lIndexInTB-(lDx / 2);
	lx = stViewportRect.left;
	ly = stViewportRect.top;

	// Make the toolbar array of Id.
	lIndBeginFamily = m_a_lFamiliesIndex[lIndexInTB];
	lNbButtonsFamily = 0;
	while (m_a_stButtons[lIndBeginFamily].lId != m_lIdEndFamily)
	{
		if (m_a_stButtons[lIndBeginFamily].bVisible == TRUE)
		{
			a_stButtonsFamily[lNbButtonsFamily++] = m_a_stButtons[lIndBeginFamily];
		}
		lIndBeginFamily++;
	}

	// Create the ToolBar.
	ASSERT (lNbButtonsFamily > 1) ;
	m_oWndButton.CreateWndButton(this,
								 lx,
								 ly,
								 a_stButtonsFamily,
								 lNbButtonsFamily,
								 lIndexInTB,
								 m_lNbButtonsTotal,
								 m_lIdResource);
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// CPA_PopUpToolBar message handlers
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
void CPA_PopUpToolBar::OnLButtonDown(UINT nFlags, CPoint oPoint)
{
	int i;
	BOOL b;

	CToolBarCtrl::OnLButtonDown(nFlags, oPoint);

	m_lIdCurrentButton = -1;
	for (i = 0; i < m_lNbFamilies; i++)
	{
		b = IsButtonPressed(m_a_stButtons[m_a_lButtonsInTBIndex[i]].lId);
		if ((b != 0) && (b != -1))
		{
			m_lIdCurrentButton = m_a_stButtons[m_a_lButtonsInTBIndex[i]].lId;
			break;
		}
	}
	// here, i = the index of the pressed button in the toolbar
	if(m_lIdCurrentButton != -1)
	{
		PressButton(m_lIdCurrentButton, TRUE);
		m_bMousePickedButton = TRUE;
		if (GetNbButtonsTotalInFamily(i) == 1)
		{
			// we have only one button in this family...
			m_bCurrentButtonAlone = TRUE;
		}
		else
		{
			m_bCurrentButtonAlone = FALSE;
			OnButton(m_lIdCurrentButton, nFlags, i);
		}
	}
	m_bJustPressed = TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
void CPA_PopUpToolBar::OnLButtonUp(UINT nFlags, CPoint oPoint)
{
	BOOL bButtonPressed;

	if (m_bMousePickedButton)
		bButtonPressed = IsButtonPressed(m_lIdCurrentButton);


	if (m_bMousePickedButton)
	{
		if (m_bCurrentButtonAlone == TRUE) // case with no sub-toolbar
		{
			if (bButtonPressed)
			{
				m_bCurrentButtonAlone = FALSE;
				m_bMousePickedButton = FALSE;
				CToolBarCtrl::OnLButtonUp(nFlags, oPoint);
				return;
			}
		}
		else // case with sub-toolbar
		{
			long lIdChosenButton;

			m_bCurrentButtonAlone = FALSE;
			m_bMousePickedButton = FALSE;
			CToolBarCtrl::OnLButtonUp(nFlags, oPoint);

			PressButton(m_lIdCurrentButton,FALSE);

			lIdChosenButton = m_oWndButton.GetChosenButtonId();
			if (lIdChosenButton != 0)
			{
				SetButton(lIdChosenButton);
                GetParent()->SendMessage(WM_COMMAND, lIdChosenButton);
			}
		}
	}
	else
	{
		CToolBarCtrl::OnLButtonUp(nFlags, oPoint);
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
void CPA_PopUpToolBar::OnMouseMove(UINT nFlags, CPoint oPoint)
{
	CToolBarCtrl::OnMouseMove(nFlags,oPoint); // for tooltips!!!
}

#endif // ACTIVE_EDITOR