void hdDragTrackerTool::mouseDrag(hdMouseEvent &event)
{
	hdAbstractTool::mouseDrag(event);

	if(event.LeftIsDown())
	{
		int x = event.GetPosition().x, y = event.GetPosition().y;

		//Hack to avoid a weird bug that ocurrs when use double click very fast over figure and drag a same time, lastX
		//and lastY values becomes big negatives numbers, if this happens, then reset it to click point
		if(lastX < 0)
			lastX = x;
		if(lastY < 0)
			lastY = y;

		hasMovedValue = (abs (x - anchorX) > 4 || abs (y - anchorX) > 4);

		if (hasMoved())
		{
			hdIFigure *tmp = NULL;
			hdIteratorBase *iterator = event.getView()->getDrawing()->selectionFigures();
			while(iterator->HasNext())
			{
				tmp = (hdIFigure *)iterator->Next();
				tmp->moveBy(event.getView()->getIdx(), x - lastX, y - lastY);
				event.getView()->notifyChanged();
			}
			delete iterator;
		}
		setLastCoords (x, y);
	}
}
void hdSelectionTool::mouseDown(hdMouseEvent &event)
{
	hdITool::mouseDown(event);

	hdDrawingView *view = event.getView();
	int x = event.GetPosition().x, y = event.GetPosition().y;

	hdIHandle *handle = view->findHandle(view->getIdx(), x, y);
	if(handle)
	{
		setDelegateTool(view, new hdHandleTrackerTool(view, handle));
	}
	else
	{
		hdIFigure *figure = view->getDrawing()->findFigure(view->getIdx(), x, y);
		if(figure)
		{
			view->getDrawing()->bringToFront(figure);
			setDelegateTool(event.getView(), figure->CreateFigureTool(view, new hdDragTrackerTool(view, figure)));
		}
		else
		{
			setDelegateTool(event.getView(),  new hdCanvasMenuTool(view, new hdSelectAreaTool(view)) );
		}
	}

	hdITool *delegateTool = getDelegateTool();
	if (delegateTool)
		delegateTool->mouseDown(event);
}
void hdSelectAreaTool::mouseUp(hdMouseEvent &event)
{
	hdAbstractTool::mouseUp(event);
	hdGeometry g;
	//hack-fix for bug when selecting figures from right to left
	if(event.LeftUp())
	{
		if( selectionRect.width < 0 )
		{
			int tmp;
			tmp = selectionRect.width;
			selectionRect.x += tmp;
			selectionRect.width = g.ddabs(tmp);

		}
		if( selectionRect.height < 0 )
		{
			int tmp;
			tmp = selectionRect.height;
			selectionRect.y += tmp;
			selectionRect.height = g.ddabs(tmp);
		}
		//end hack-fix
		drawSelectionRect(event.getView());
		selectFiguresOnRect(event.ShiftDown(), event.getView());
		event.getView()->disableSelRectDraw();
	}
}
void hdCreationTool::mouseDown(hdMouseEvent &event)
{
	hdAbstractTool::mouseDown(event);
	if(event.LeftDown())
	{
		event.getView()->getDrawing()->add(figurePrototype);
		int x = event.GetPosition().x, y = event.GetPosition().y;
		figurePrototype->moveTo(event.getView()->getIdx(), x, y);
		event.getView()->getDrawing()->clearSelection();
		event.getView()->getDrawing()->addToSelection(figurePrototype);
	}
}
void hdSelectAreaTool::mouseDrag(hdMouseEvent &event)
{
	hdAbstractTool::mouseDrag(event);
	if(event.LeftIsDown())
	{
		drawSelectionRect(event.getView());
		int x = event.GetPosition().x, y = event.GetPosition().y;
		selectionRect.x = anchorX;
		selectionRect.y = anchorY;
		selectionRect.SetBottomRight(wxPoint(x, y));
		drawSelectionRect(event.getView());
		event.getView()->ScrollToMakeVisible(event.GetPosition());
	}
}
void hdDragCreationTool::mouseDrag(hdMouseEvent &event)
{
	if(event.LeftIsDown())
	{
		figurePrototype->displayBox().SetPosition(event.getView()->getIdx(), event.GetPosition());
	}
}
void hdSelectAreaTool::mouseDown(hdMouseEvent &event)
{
	hdAbstractTool::mouseDown(event);
	if(!event.ShiftDown())
	{
		event.getView()->getDrawing()->clearSelection();
	}
	if(event.LeftDown())
	{
		int x = event.GetPosition().x, y = event.GetPosition().y;
		selectionRect.x = x;
		selectionRect.y = y;
		selectionRect.width = 0;
		selectionRect.height = 0;
		drawSelectionRect(event.getView());
	}
}
void hdCanvasMenuTool::mouseDown(hdMouseEvent &event)
{
	//Linux hack for bug
	int x = event.GetPosition().x, y = event.GetPosition().y;
	setAnchorCoords(x, y);

	if(event.RightDown())
	{
		wxMenu menu;
		event.getView()->setCanvasMenuTool(this);
		createViewMenu(event.getView(), menu);
		event.getView()->connectPopUpMenu(menu);
		hdPoint p = event.GetPosition();
		event.getView()->CalcScrolledPosition(p.x, p.y, &p.x, &p.y);
		event.getView()->PopupMenu(&menu, p);
		return;
	}

	defaultTool->mouseDown(event);
}
void hdMenuTool::mouseDown(hdMouseEvent &event)
{
    //Linux hack for bug
    int x = event.GetPosition().x, y = event.GetPosition().y;
    setAnchorCoords(x, y);

    if(menuFigure->menuEnabled() && event.RightDown())
    {
        wxMenu menu;
        event.getView()->setMenuToolFigure(menuFigure);
        menuFigure->createMenu(menu);
        event.getView()->connectPopUpMenu(menu);
        hdPoint p = event.GetPosition();
        event.getView()->CalcScrolledPosition(p.x, p.y, &p.x, &p.y);
        event.getView()->PopupMenu(&menu, p);
        return;
    }

    getDefaultTool()->mouseDown(event);
}
void hdDragTrackerTool::mouseDown(hdMouseEvent &event)
{
	hdAbstractTool::mouseDown(event);

	if(event.LeftDown())
	{
		int x = event.GetPosition().x, y = event.GetPosition().y;

		setLastCoords(x, y);

		if(event.m_shiftDown)
		{
			event.getView()->getDrawing()->toggleSelection(anchorFigure);
		}
		else if(!event.getView()->getDrawing()->isFigureSelected(anchorFigure))
		{
			event.getView()->getDrawing()->clearSelection();
			event.getView()->getDrawing()->addToSelection(anchorFigure);
		}
	}
}
void hdSelectionTool::mouseMove(hdMouseEvent &event)
{
	hdAbstractTool::mouseMove(event);
	hdDrawingView *view = event.getView();
	int x = event.GetPosition().x, y = event.GetPosition().y;
	hdIHandle *handle = view->findHandle(view->getIdx(), x, y);

	if(handle)
	{
		view->SetCursor(handle->createCursor());
	}
	else
	{
		hdIFigure *figure = view->getDrawing()->findFigure(view->getIdx(), x, y);
		if(figure)
		{
			view->SetCursor(wxCursor(wxCURSOR_HAND));
		}
		else
		{
			view->SetCursor(wxCursor(wxCURSOR_ARROW));
		}
	}
}
void hdHandleTrackerTool::mouseDrag(hdMouseEvent &event)
{
	anchorHandle->invokeStep(event, event.getView());
}
void hdHandleTrackerTool::mouseUp(hdMouseEvent &event)
{
	anchorHandle->invokeEnd(event, event.getView());
}
void hdHandleTrackerTool::mouseDown(hdMouseEvent &event)
{
	hdAbstractTool::mouseDown(event);
	anchorHandle->invokeStart(event, event.getView());
}
void hdSimpleTextTool::mouseDown(hdMouseEvent &event)
{
	setAnchorCoords(event.GetPosition().x, event.GetPosition().y);

	// Right click to get the contextual menu
	if(txtFigure->menuEnabled() && event.RightDown())
	{
		wxMenu menu;
		event.getView()->setSimpleTextToolFigure(txtFigure, true);
		txtFigure->createMenu(menu);
		event.getView()->connectPopUpMenu(menu);
		hdPoint p = event.GetPosition();
		event.getView()->CalcScrolledPosition(p.x, p.y, &p.x, &p.y);
		event.getView()->PopupMenu(&menu, p);
		return;
	}

	// Double click to rename an object
	if(event.LeftDClick())
	{
		if(withoutDialog)
		{
			event.getView()->setSimpleTextToolFigure(txtFigure);
			showEdit = true;
			event.getView()->getSimpleTextToolEdit()->ChangeValue(txtFigure->getText()); //Same as SetValue but don't generated wxEVT_COMMAND_TEXT_UPDATED event
			calculateSizeEntry(event.getView());
			event.getView()->getSimpleTextToolEdit()->SetFocus();
			event.getView()->getSimpleTextToolEdit()->Show();
			event.getView()->getOkTxt()->Show();
			event.getView()->getCancelTxt()->Show();
		}
		else
		{
			callDialog(event.getView());
		}
		return;
	}
	getDefaultTool()->mouseDown(event);
}
void hdCreationTool::mouseUp(hdMouseEvent &event)
{
	hdAbstractTool::mouseUp(event);
	event.getView()->setTool(new hdSelectionTool(event.getView()));
}