void AutomationEvent::mouseUp (const MouseEvent& e)
{
    if (owner)
    {
        SelectedItemSet<MidiGridItem*> selection = owner->getLassoSelection ();

        if (e.mods.isLeftButtonDown())
        {
			for (int i = 0; i < selection.getNumSelected (); i++)
			{
				MidiGridItem* component = selection.getSelectedItem (i);
				component->endDragging (e);
				owner->repaint();
			}

            repaint ();
        }
		else if (e.mods.isRightButtonDown())
        {
            for (int i = 0; i < selection.getNumSelected (); i++)
            {
				AutomationEvent* component = dynamic_cast<AutomationEvent*>(selection.getSelectedItem (i));
                if (component && component != this)
                    owner->removeNote (component, true);
            }

            owner->removeNote (this, false);
            delete (this);
        }
    }
}
void PianoGridNote::mouseUp (const MouseEvent& e)
{
    if (owner)
    {
        SelectedItemSet<MidiGridItem*> selection = owner->getLassoSelection ();

        if (e.mods.isLeftButtonDown())
        {
            if (isResizing)
            {
                for (int i = 0; i < selection.getNumSelected (); i++)
                {
					PianoGridNote* component = dynamic_cast<PianoGridNote*>(selection.getSelectedItem (i));
					if (component)
						component->endResizing (e);
                }
            }
            else if (isDragging)
            {
                for (int i = 0; i < selection.getNumSelected (); i++)
                {
                    MidiGridItem* component = selection.getSelectedItem (i);
                    component->endDragging (e);
                }
            }

            repaint ();
        }
        else if (e.mods.isMiddleButtonDown())
        {
            if (isEditingVelocity)
            {
                for (int i = 0; i < selection.getNumSelected (); i++)
                {
					PianoGridNote* component = dynamic_cast<PianoGridNote*>(selection.getSelectedItem (i));
					if (component)
						component->endVelocity (component == this ? e : e.getEventRelativeTo (component));
                }
            }
        }
        else if (e.mods.isRightButtonDown())
        {
            for (int i = 0; i < selection.getNumSelected (); i++)
            {
				PianoGridNote* component = dynamic_cast<PianoGridNote*>(selection.getSelectedItem (i));
                if (component && component != this)
                    owner->removeNote (component, true);
            }

            owner->removeNote (this, false);
            delete (this);
        }
    }
}
void ObjController::removeSelectedObjects(ObjectsHolder& holder)
{
    SelectedItemSet <SelectableObject*> temp;
    temp = sObjects;

    if (temp.getNumSelected() > 0)
    {
        sObjects.deselectAll();
        sObjects.changed(true);
        // first remove all selected links
        for (int i = temp.getNumSelected(); --i >= 0;)
        {
            if(LinkComponent* lc = dynamic_cast<LinkComponent*>(temp.getSelectedItem(i)))
            {
                temp.deselect(lc);
                removeLink(lc, true, &holder);
            }
        }
        // then objects and remaining links connected to the objects
        for (int i = temp.getNumSelected(); --i >= 0;)
        {
            if(AudioOutConnector* aoc = dynamic_cast<AudioOutConnector*>(temp.getSelectedItem(i)))
            {
                temp.deselect(aoc);
                removeAudioConnection(aoc, true, &holder);
                continue;
            }
        }
        for (int i = temp.getNumSelected(); --i >= 0;)
        {
            if(ObjectComponent* oc = dynamic_cast<ObjectComponent*>(temp.getSelectedItem(i)))
            {
                removeObject(oc, true, &holder);
            }
            else if(CommentComponent* cc = dynamic_cast<CommentComponent*>(temp.getSelectedItem(i)))
            {
                removeComment(cc, true, &holder);
            }
//            LinkComponent* lc = ObjectsHelper::getLink(temp.getSelectedItem(i));
//            if(lc != nullptr)
//            {
//                removeLink(lc, true, holder);
//                continue;
//            }

        }
    }
}
void PianoGridNote::mouseDown (const MouseEvent& e)
{
   if (! owner) return;

   SelectedItemSet<MidiGridItem*> selection = owner->getLassoSelection ();
   if (! selection.isSelected (this))
   {
      owner->selectNote (this, !e.mods.isShiftDown());
      selection = owner->getLassoSelection();
   }

    if (e.mods.isLeftButtonDown())
    {
        if (e.x >= getWidth() - 2)
        {
            for (int i = 0; i < selection.getNumSelected (); i++)
            {
                PianoGridNote* component = dynamic_cast<PianoGridNote*>(selection.getSelectedItem (i));
				if (component)
					component->startResizing (e);
            }
        }
        else
        {
            for (int i = 0; i < selection.getNumSelected (); i++)
            {
                MidiGridItem* component = selection.getSelectedItem (i);
                component->startDragging (component == this ? e : e.getEventRelativeTo (component));
            }
        }

        DBG ("Note: " + String (note) + " " + String (beat) + " " + String (length));
    }
    else if (e.mods.isMiddleButtonDown())
    {
        for (int i = 0; i < selection.getNumSelected (); i++)
        {
            PianoGridNote* component = dynamic_cast<PianoGridNote*>(selection.getSelectedItem (i));
			if (component)
				component->startVelocity (e);
        }
    }
}
void PianoGridNote::mouseDrag (const MouseEvent& e)
{
    if (! owner) return;

    SelectedItemSet<MidiGridItem*> selection = owner->getLassoSelection ();

    if (e.mods.isLeftButtonDown())
    {
        if (isResizing)
        {
            for (int i = 0; i < selection.getNumSelected (); i++)
            {
            PianoGridNote* component = dynamic_cast<PianoGridNote*>(selection.getSelectedItem (i));
			if (component)
                component->continueResizing (e);
            }
        }
        else if (isDragging)
        {
            for (int i = 0; i < selection.getNumSelected (); i++)
            {
                MidiGridItem* component = selection.getSelectedItem (i);
                component->continueDragging (component == this ? e : e.getEventRelativeTo (component));
            }
        }
    }
    else if (e.mods.isMiddleButtonDown())
    {
        if (isEditingVelocity)
        {
            for (int i = 0; i < selection.getNumSelected (); i++)
            {
				PianoGridNote* component = dynamic_cast<PianoGridNote*>(selection.getSelectedItem (i));
				if (component)
					component->continueVelocity (e);
            }
        }
    }
}
void AutomationEvent::mouseDown (const MouseEvent& e)
{
    if (! owner) return;

    SelectedItemSet<MidiGridItem*> selection = owner->getLassoSelection ();
    if (! selection.isSelected (this))
        owner->selectNote (this, true);

    if (e.mods.isLeftButtonDown())
    {
		for (int i = 0; i < selection.getNumSelected (); i++)
		{
			MidiGridItem* component = selection.getSelectedItem (i);
			component->startDragging (component == this ? e : e.getEventRelativeTo (component));
			owner->repaint();
		}
    }
}