/*
 *  mdc_item_handle.cpp
 *  mdcanvas
 *
 *  Created by Alfredo Kojima on 07/Mar/9.
 *  Copyright 2007 MySQL AB. All rights reserved.
 *
 */

#include "stdafx.h"

#include "mdc_item_handle.h"
#include "mdc_interaction_layer.h"

using namespace mdc;

ItemHandle::ItemHandle(InteractionLayer *ilayer, CanvasItem *item, const Point &point)
: _item(item), _layer(ilayer), _pos(point), _highlighted(false), _draggable(true)
{
}


ItemHandle::~ItemHandle()
{
  _layer->remove_handle(this);
}


void ItemHandle::move(const Point &point)
{
  Point delta= _pos;

  _layer->set_needs_repaint(get_bounds());

  _pos= point;

  delta.x= _pos.x - delta.x;
  delta.y= _pos.y - delta.y;

  _layer->set_needs_repaint(get_bounds());
}


void ItemHandle::set_highlighted(bool flag)
{
  _highlighted= flag;
  _layer->set_needs_repaint(get_bounds());
}


void ItemHandle::set_draggable(bool flag)
{
  _draggable= flag;
}


void ItemHandle::set_color(const Color &color)
{
  _color= color;
}


void ItemHandle::repaint(CairoCtx *cr)
{
  Rect r= get_bounds();
  
  if (_draggable)
  {
    cr->set_color(_color);
    cr->set_line_width(1);
    cr->rectangle(r);
    cr->fill_preserve();

    if (_highlighted)
      cr->set_color(Color(0, 1, 1, 1));
    else
      cr->set_color(Color(0, 0, 0, 1));
    cr->stroke();
  }
  else
  {
    if (_highlighted)
      cr->set_color(Color(0, 0, 0, 1.0));
    else
      cr->set_color(Color(0, 0, 0, 1));
    cr->set_line_width(1);
    cr->rectangle(r);
    cr->fill_preserve();
    cr->set_color(Color::Black());
    cr->stroke();
  }
}