/* 
 * © 2007-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
#include "stdafx.h"

#include "layer_figure.h"
#include "figure_common.h"
#include <grts/structs.model.h>

#ifdef _WIN32
// stupid VC++ workaround
#undef max
#undef min
#define max _cpp_max
#define min _cpp_min
#endif

using namespace wbfig;

#define MIN_LAYER_TITLE_WIDTH 120.0
#define MIN_LAYER_SIZE 10.0



LayerAreaGroup::LayerAreaGroup(mdc::Layer *layer, FigureEventHub *hub, model_Object *represented_object)
: mdc::AreaGroup(layer), _hub(hub), _represented_object(represented_object)
{
  set_cache_toplevel_contents(false);

  set_auto_sizing(false);
//  set_background(_layer_back);
  set_draggable(true);

  _extents_invalid= true;

  _font= layer->get_view()->get_default_font();
  _font.weight= mdc::WBold;

  _title_fore= mdc::Color::Black();
  _title_back= mdc::Color(0.85, 0.85, 0.85);

  _min_size.width= MIN_LAYER_SIZE;
  _min_size.height= MIN_LAYER_SIZE;

  _drag_selects_contents= true;
  
  _resizing= false;
}


void LayerAreaGroup::set_title(const std::string &title)
{
  _title= title;
  _extents_invalid= true;
  set_needs_repaint();
}


void LayerAreaGroup::set_font(const mdc::FontSpec &font)
{
  _font= font;
  _extents_invalid= true;
  set_needs_repaint();
}


static void get_bounding_area(mdc::CanvasItem *item, mdc::Point *maxpos)
{
  mdc::Rect bounds= item->get_bounds();

  maxpos->x= std::max(maxpos->x, bounds.xmax());
  maxpos->y= std::max(maxpos->y, bounds.ymax());
}


void LayerAreaGroup::move_item(mdc::CanvasItem *item, const mdc::Point &pos)
{
  item->move_to(pos);
}


bool LayerAreaGroup::on_drag_handle(mdc::ItemHandle *handle, const mdc::Point &pos, bool dragging)
{
  //mdc::Point offset= pos - get_view()->snap_to_grid(handle->get_position());
  mdc::Point npos= get_position();
  mdc::Size nsize= get_size();

  if (!_resizing)
  {
    mdc::Point maxpos;

    _initial_bounds= get_root_bounds();
    _resizing= true;

    foreach(sigc::bind<mdc::Point*>(sigc::ptr_fun(get_bounding_area), &maxpos));

    _min_size.width= std::max(maxpos.x, MIN_LAYER_SIZE);
    _min_size.height= std::max(maxpos.y, MIN_LAYER_SIZE);
    _min_size_invalid= false;
  }

  bool flag= mdc::AreaGroup::on_drag_handle(handle, get_view()->snap_to_grid(pos), dragging);

  if (!dragging)
  {
    _resizing= false;
    _min_size.width= MIN_LAYER_SIZE;
    _min_size.height= MIN_LAYER_SIZE;
    _min_size_invalid= false;

    _resize_signal.emit(_initial_bounds);
  }

  return flag;
}


bool LayerAreaGroup::on_button_press(mdc::CanvasItem *target, const mdc::Point &point, mdc::MouseButton button, mdc::EventState state)
{
  if (mdc::bounds_contain_point(get_title_bounds(), point.x, point.y))
  {
    _drag_selects_contents= false;
  }

  if (!_hub || !_hub->figure_button_release(_represented_object, target, point, button, state))
    return super::on_button_press(target, point, button, state);
  
  return false;
}


bool LayerAreaGroup::on_button_release(mdc::CanvasItem *target, const mdc::Point &point, mdc::MouseButton button, mdc::EventState state)
{
  bool ret= false;
  
  if (!_hub || !_hub->figure_button_release(_represented_object, target, point, button, state))
    ret= super::on_button_release(target, point, button, state);

  _drag_selects_contents= true;
  
  return ret;
}


bool LayerAreaGroup::on_click(mdc::CanvasItem *target, const mdc::Point &point, mdc::MouseButton button, mdc::EventState state)
{
  if (!_hub || !_hub->figure_click(_represented_object, target, point, button, state))
    return mdc::AreaGroup::on_click(target, point, button, state);

  return false;
}


bool LayerAreaGroup::on_enter(mdc::CanvasItem *target, const mdc::Point &point)
{
  if (!_hub || !_hub->figure_enter(_represented_object, target, point))
    return super::on_enter(target, point);
  return false;
}


bool LayerAreaGroup::on_leave(mdc::CanvasItem *target, const mdc::Point &point)
{
  if (!_hub || !_hub->figure_leave(_represented_object, target, point))
    return super::on_leave(target, point);
  return false;
}


void LayerAreaGroup::render(mdc::CairoCtx *cr)
{
  mdc::AreaGroup::render(cr);

  if (_extents_invalid)
  {
    cr->get_text_extents(_font, _title, _extents);
    _extents_invalid= false;
  }

  double padding= 5;

  mdc::Size size= get_title_bounds().size;

  cr->save();
  cr->translate(get_position());
  cr->set_color(_title_back);
  cr->new_path();
  cr->move_to(mdc::Point(0, 0));
  cr->line_to(mdc::Point(size.width, 0));
  cr->line_to(mdc::Point(size.width, size.height-5));
  cr->line_to(mdc::Point(size.width-5, size.height));
  cr->line_to(mdc::Point(0, size.height));
  cr->close_path();
  cr->fill();

  cr->set_color(_title_fore);
  cr->move_to(mdc::Point(padding+_extents.x_bearing, padding-_extents.y_bearing));
  cr->set_font(_font);
  cr->show_text(_title);
  
  cr->restore();
}


void LayerAreaGroup::render_gl(mdc::CairoCtx *cr)
{
  mdc::AreaGroup::render_gl(cr);

  if (_extents_invalid)
  {
    cr->get_text_extents(_font, _title, _extents);
    _extents_invalid= false;
  }

  double padding= 5;

  mdc::Size size= get_title_bounds().size;

  gl_setcolor(_title_back);

  glPushMatrix();
  glTranslated(get_position().x, get_position().y, 0);
  glBegin(GL_POLYGON);
  glVertex2f(0.0, 0.0);
  glVertex2d(size.width, 0);
  glVertex2d(size.width, size.height - 5);
  glVertex2d(size.width - 5, size.height);
  glVertex2d(0, size.height);
  glEnd();
  glPopMatrix();

  cr->set_color(_title_fore);
  cr->move_to(get_position() + mdc::Point(padding+_extents.x_bearing, padding-_extents.y_bearing));
  cr->set_font(_font);
  cr->show_text(_title);
}


mdc::Rect LayerAreaGroup::get_title_bounds() const
{
  mdc::Rect rect;
  double padding= 5;
  
  double width= _extents.width + 10;
  if (width < MIN_LAYER_TITLE_WIDTH)
    width= MIN_LAYER_TITLE_WIDTH;
  
  mdc::Size size= get_size();

  if (width > size.width - _extents.height - 2*padding)
    width= size.width - _extents.height - 2*padding;

  rect.size.width= width + 2*padding;
  rect.size.height= _extents.height + 2*padding;

  return rect;
}


