void EventSheetEditor::DeselectEvents()
{
    SelectedEventVector  SelectedEvents;
    CreateEventSelectionVector(SelectedEvents,(*m_pEventList));

    for(int a=0; a < SelectedEvents.size(); a ++)
    {
        SelectedEvents[a]->m_select(this) = false;
        InvalidateRect(SelectedEvents[a]->m_rect(this));
    }
}
void EventSheetEditor::OnBookmark()
{
	CreateUndo();

	// Toggle bookmark status for all events
	SelectedEventVector SelectedEvents;
	CreateEventSelectionVector(SelectedEvents, (*m_pEventList));

	for(int a = 0; a < SelectedEvents.size(); a ++)
	{
		SelectedEvents[a]->m_bBookmarked = !SelectedEvents[a]->m_bBookmarked;
		InvalidateRect(SelectedEvents[a]->m_rect(this));
	}
}
void EventSheetEditor::CreateEventSelectionVector(SelectedEventVector& pSelectedEventList,EventVector& EventList)
{
    for(int a = 0 ; a < EventList.size() ; a ++)
    {
        if(EventList[a]->m_select(this))
            pSelectedEventList.push_back(EventList[a]);

        CreateEventSelectionVector(pSelectedEventList, EventList[a]->m_EventList);
    }
}
void EventSheetEditor::OnToolbarPaste()
{
    if(m_pInlineScintilla.GetSafeHwnd())
    {
        m_pInlineScintilla.Paste();
        return;
    }

    CString csF = m_pDDMgr->AvailableDataType(NULL);

    if (m_pDDMgr->OkToPaste())
    {
        CreateUndo();
        if (csF == "Construct Events") m_Drag.dragtype = EVENT;
        if (csF == "Construct Conditions") m_Drag.dragtype = CONDITION;
        if (csF == "Construct Actions") m_Drag.dragtype = ACTION;

        switch (m_Drag.dragtype)
        {
        case EVENT:
        {
            m_Drag.dragtype = -1;

            CEditorEvent* tmpEvent = m_FocusEvent;


            EventVector* tempList = m_pEventList;
            CEditorDragEvents Events;
            int index = -1;
            if(tmpEvent)
            {
                tempList = FindEventListEventExistsIn(tmpEvent,&(*m_pEventList),index);
            }
            if(tempList == 0 )
                return ; // eeppp...the event doesn't have a parent!?!?!?!?

            Events.m_pEventListPart		= tempList;
            Events.index				= index;
            Events.GoingUp				= false;
            Events.pEd = (EventSheetEditor*)this;

            DeselectActions();
            DeselectConditions();
            DeselectEvents();

            m_pDDMgr->DoDrop(&Events,
                             NULL,
                             "Construct Events");
        }
        break;

        case CONDITION:
        {
            m_Drag.dragtype = -1;

            // Get selected events and add condition to them
            SelectedEventVector SelectedEvents;
            CreateEventSelectionVector(SelectedEvents, (*m_pEventList));

            if(SelectedEvents.size() == 0)
            {
                CEditorCondition* tmpCondition = m_FocusCondition;
                CEditorEvent* tmpEvent = 0;
                int index = 0;
                tmpEvent = FindEventConditionExistsIn(tmpCondition,m_pEventList,index);
                if(!tmpEvent)
                {
                    tmpEvent = m_FocusEvent;
                }

                CEditorDragConditions Conditions;
                Conditions.m_pConditions		= &tmpEvent->m_Conditions;
                Conditions.index				= index;
                Conditions.GoingUp				= false;
                Conditions.pEd = (EventSheetEditor*)this;

                DeselectActions();
                DeselectConditions();
                DeselectEvents();

                if(tmpEvent)
                {
                    m_pDDMgr->DoDrop(&Conditions,
                                     DROPEFFECT_NONE,
                                     "Construct Conditions");
                }
            }
            else
            {
                for(int a = 0; a < SelectedEvents.size(); a ++)
                {
                    CEditorDragConditions Conditions;
                    Conditions.m_pConditions		= &SelectedEvents[a]->m_Conditions;
                    Conditions.index				= 0;
                    Conditions.GoingUp				= false;
                    Conditions.pEd = (EventSheetEditor*)this;

                    m_pDDMgr->DoDrop(&Conditions,
                                     DROPEFFECT_NONE,
                                     "Construct Conditions");

                    InvalidateRect(SelectedEvents[a]->m_rect(this));
                }
            }
        }
        break;

        case ACTION:
        {
            m_Drag.dragtype = -1;

            // Get selected events and add condition to them
            SelectedEventVector SelectedEvents;
            CreateEventSelectionVector(SelectedEvents,(*m_pEventList));

            if(SelectedEvents.size() == 0)
            {
                CEditorAction* tmpAction = m_FocusAction;
                CEditorEvent* tmpEvent = 0;
                int index = 0;
                tmpEvent = FindEventActionExistsIn(tmpAction,m_pEventList,index);

                if(!tmpEvent)
                {
                    tmpEvent = m_FocusEvent;
                }

                CEditorDragActions Actions;
                Actions.m_pActions		= &tmpEvent->m_Actions;
                Actions.index				= index;
                Actions.GoingUp				= false;
                Actions.pEd = (EventSheetEditor*)this;

                DeselectActions();
                DeselectConditions();
                DeselectEvents();
                if(tmpEvent)
                {
                    m_pDDMgr->DoDrop(&Actions,
                                     DROPEFFECT_NONE,
                                     "Construct Actions");
                }
            }
            else
            {
                for(int a = 0; a < SelectedEvents.size(); a ++)
                {
                    CEditorDragActions Actions;
                    Actions.m_pActions			= &SelectedEvents[a]->m_Actions;
                    Actions.index				= 0;
                    Actions.GoingUp				= false;
                    Actions.pEd = (EventSheetEditor*)this;

                    m_pDDMgr->DoDrop(&Actions,
                                     DROPEFFECT_NONE,
                                     "Construct Actions");

                    InvalidateRect(SelectedEvents[a]->m_rect(this));
                }
            }
        }
        break;
        }
    }

    Invalidate();
}
void EventSheetEditor::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(!m_pHeader)
		return;
	m_ObjectHeaderTooltip.ShowAt(point, *this);

	m_Mouse = point;
	m_pHeader->OnMouseMove(point);
	if(m_pHeader->m_isDrag)
		Invalidate();

	else
	{
		// Is is possible we might be dragging.
		if(!m_Drag.IsDragging && m_leftDown)
		{
			if(	( (point.x - m_Drag.StoredPos.x)*(point.x - m_Drag.StoredPos.x)
				+(point.y - m_Drag.StoredPos.y)*(point.y - m_Drag.StoredPos.y)) >  8)
			{
				switch (m_Drag.dragtype)
				{
					case EVENT:
					{
						SelectedEventVector  m_SelectedEvents;
						CreateEventSelectionVectorLimited(m_SelectedEvents,(*m_pEventList));
						CEditorDragEvents* Events = new CEditorDragEvents;

						Events->pEd = this;
						Events->m_pSelectedEvents = &m_SelectedEvents;

						DROPEFFECT de = DROPEFFECT_COPY;
						m_pDDMgr->PrepareDrop(DO_DRAGDROP,"Construct Events",Events,&de);				
					
						// wait till the drop is done...then....
						m_Drag.dragtype = -1;
						m_Drag.IsDragging = false;
						Invalidate();
					}
					break;

					case CONDITION:
					{
						SelectedConditionVector m_SelectedConditions;
						CreateConditionSelectionVector(m_SelectedConditions,(*m_pEventList));
						CEditorDragConditions* Conditions = new CEditorDragConditions;

						Conditions->pEd = this;
						Conditions->m_pSelectedConditions = &m_SelectedConditions;

						DROPEFFECT de = DROPEFFECT_COPY;
						m_pDDMgr->PrepareDrop(DO_DRAGDROP,"Construct Conditions",Conditions,&de);				
					
						// wait till the drop is done..then...
						m_Drag.dragtype = -1;
						m_Drag.IsDragging = false;
						Invalidate();
						m_SelectedConditions.clear();
					
					}
					break;

					case ACTION:
					{
						SelectedActionVector m_SelectedActions;
						CreateActionSelectionVector(m_SelectedActions,(*m_pEventList));
						CEditorDragActions* Actions = new CEditorDragActions;
						
						Actions->pEd = this;
						Actions->m_pSelectedActions = &m_SelectedActions;

						DROPEFFECT de = DROPEFFECT_COPY;
						m_pDDMgr->PrepareDrop(DO_DRAGDROP,"Construct Actions",Actions,&de);				
					
						// wait till the drop is done..then...
						m_Drag.dragtype = -1;
						m_Drag.IsDragging = false;
						Invalidate();
						m_SelectedActions.clear();
					}
					break;	

					case OBJECT:
					{
						CEditorDragObject* Objects = new CEditorDragObject;
						Objects->pEd = (EventSheetEditor*)this;
						Objects->ObjectIdentifier = m_pHeader->GetObjectAtX(m_Drag.StoredPos.x)->oid;

						DROPEFFECT de = DROPEFFECT_COPY;
						m_pDDMgr->PrepareDrop(DO_DRAGDROP,"Construct Object",Objects,&de);				
					
						// wait till the drop is done..then...
						m_Drag.dragtype = -1;
						m_Drag.IsDragging = false;
						Invalidate();
					}
					break;	

					default:
					break;
				}
			}
		}
		else
		{
			//Tooltips
			CEditorEvent* pMyEvent = EventAt(point);

			if(pMyEvent)
			{
				SelectedActionVector actions;
				CEditorAction* pAction = pMyEvent->actionAt(point, actions, this);
				CEditorCondition* pCondition = pMyEvent->conditionAt(point, this);
				if(pMyEvent->m_displayActionType(this) < 2)	//only grid and chrono should show the tooltip
				{	
					if(pAction)
					{
						
						CPoint pt = (actions[0])->m_rect(this).TopLeft();
						pt.x += 16;
						pt.y = (actions[0])->m_rect(this).bottom-4;
						if(!m_InsertBefore.IsWindowVisible())
							m_ActionTooltip.ShowAt(pt,actions,*this);		
					}
					else
					{
						m_ActionTooltip.Hide();
					}

				}
				else
				{
						


				}
			}
			else
				m_ActionTooltip.Hide();
			
		}
	}

	SelectedConditionVector SelectedConditions;
	CreateConditionSelectionVector(SelectedConditions,(*m_pEventList));

	for(int c = 0; c < SelectedConditions.size(); c++)
		InvalidateRect(SelectedConditions[c]->m_rect(this));

	SelectedActionVector SelectedActions;
	CreateActionSelectionVector(SelectedActions,(*m_pEventList));

	for(int a = 0; a < SelectedActions.size(); a++)
		InvalidateRect(SelectedActions[a]->m_rect(this));

	SelectedEventVector SelectedEvents;
	CreateEventSelectionVector(SelectedEvents,(*m_pEventList));

	for(int e = 0; e < SelectedEvents.size(); e++)
		InvalidateRect(SelectedEvents[e]->m_rect(this));

	////////////////////////
	// Change to a hand cursor if overlapping "new action"
	bool useHandCursor = false;
	static CEditorEvent* pLastEvent = NULL;

	if (pLastEvent)
	{
		pLastEvent->mouseOverNewAction = false;
		pLastEvent->mouseOverFooter = false;

	}

	if (!(point.x >= m_pHeader->m_Split - 2 && point.x <= m_pHeader->m_Split + 5))
	{
		CEditorEvent* pEvent = this->EventAt(point);
		if(pEvent)
		{
			pEvent->mouseOverNewAction = false;
			pEvent->mouseOverFooter = false;

			if(pEvent->canAddActions())
			{
				if(pEvent->m_gridRec(this).PtInRect(point))
				{
					useHandCursor = true;
					pEvent->mouseOverNewAction = true;
				}
			}
			if(pEvent->m_type == 2 && pEvent->m_open(this)) // Group
			{
				CRect Footer = pEvent->m_rect(this);
				Footer.top = Footer.bottom - 17;
				Footer.left += EVENT_INDENT * 2;


				if(Footer.PtInRect(point))
				{
					useHandCursor = true;
					pEvent->mouseOverFooter = true;
				}
			}


			pLastEvent = pEvent;

			this->Invalidate();
		}
	}
	////////////////////////
	// Change to a hand cursor if overlapping "new event to group"


	static HCURSOR handCursor = LoadCursor(NULL, IDC_HAND);
	static HCURSOR arrowCursor = LoadCursor(NULL, IDC_ARROW);

	if (useHandCursor)
		SetCursor(handCursor);
	//else
	//	SetCursor(arrowCursor);
	////////////////////////

	int objid = 0;
	CPoint pt = point;   // copy!	
	CRect myrect;
	GetWindowRect(&myrect);


	CRect rect;
	rect.top = pt.y;
	rect.left = pt.x;
	rect.right = pt.x + 20;
	rect.bottom = pt.y + 20;

	CScrollView::OnMouseMove(nFlags, point);
}
void EventSheetEditor::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	switch (nChar)
	{
		case VK_DELETE:
			OnToolbarDelete();
		break;

		// Bookmarking hotkeys
		case 0x42:
		{
			if (m_CtrlDown)
			{
				// Toggle bookmark status for all events
				SelectedEventVector SelectedEvents;
				CreateEventSelectionVector(SelectedEvents, (*m_pEventList));

				for(int a = 0; a < SelectedEvents.size(); a ++)
				{
					SelectedEvents[a]->m_bBookmarked = !SelectedEvents[a]->m_bBookmarked;
					InvalidateRect(SelectedEvents[a]->m_rect(this));
				}
			}
		}
		break;

		case VK_CONTROL:
			m_CtrlDown = true;
		break;

		case VK_SHIFT:
			m_ShiftDown = true;
		break;

		case VK_NEXT:
			OnNextBookmark();
		break;

		case VK_PRIOR:
			OnPreviousBookmark();
		break;

		case 'E':
			if (!m_CtrlDown)
				OnAddEvent();
		break;

		case 'S':
			if (!m_CtrlDown)
				OnAddSubEvent();
		break;

		case 'A':
			if (!m_CtrlDown)
				OnAddAction();
		break;

		case 'C':
			if (!m_CtrlDown)
				OnAddCondition();
		break;
	}

	CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}