/* 
 * © 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 "badge_figure.h"
#include "figure_common.h"

BadgeFigure::BadgeFigure(mdc::Layer *layer)
: mdc::Figure(layer)
{
  _font= parse_font_spec("Helvetica Bold 11");
  _xpadding= 8;
  _ypadding= 3;
  _line_width= 2;
  _pen_color= mdc::Color(0.9, 0.9, 0.9);
  _text_color= mdc::Color(1, 1, 1);
  
  set_cache_toplevel_contents(false);
  
  _gradient= 0;
}


BadgeFigure::~BadgeFigure()
{
  updater_connection.disconnect();
  
  cairo_pattern_destroy(_gradient);
}


void BadgeFigure::set_badge_id(const std::string &bid)
{
  _badge_id= bid;
}


void BadgeFigure::set_text(const std::string &text)
{
  _text= text;
  set_needs_relayout();
}


void BadgeFigure::set_gradient_from_color(const mdc::Color &color)
{
  mdc::HSVColor hsv(color);
  
  hsv.v/= 1.4;
  
  set_fill_color(mdc::Color(hsv));
  set_fill_color2(color);
  set_needs_render();
}


void BadgeFigure::set_fill_color2(const mdc::Color &color)
{
  _fill_color2= color;
  if (_gradient)
    cairo_pattern_destroy(_gradient);
  _gradient= 0;
}


void BadgeFigure::set_text_color(const mdc::Color &color)
{
  _text_color= color;
}



mdc::Size BadgeFigure::calc_min_size()
{
  mdc::Size size;
  cairo_text_extents_t extents;

  get_view()->cairoctx()->get_text_extents(_font, _text.c_str(), extents);
  
  size.width= extents.x_advance;
  size.height= extents.height;
  _text_size= size;
//  size.width+= 2*_xpadding;
//  size.height+= 2*_ypadding;
  
  return size;
}


void BadgeFigure::draw_contents(mdc::CairoCtx *cr)
{
  if (!_gradient)
  {
    _gradient= cairo_pattern_create_linear(0.0, 0.0, 0.0, get_size().height);
    cairo_pattern_add_color_stop_rgba(_gradient, 1, _fill_color.r, _fill_color.g, _fill_color.b, _fill_color.a);
    cairo_pattern_add_color_stop_rgba(_gradient, 0, _fill_color2.r, _fill_color2.g, _fill_color2.b, _fill_color2.a);    
  }
  
  cr->save();
  
  stroke_rounded_rectangle(cr, get_bounds(), mdc::CAll, 4.0, 0.0);
  cairo_set_source(cr->get_cr(), _gradient);
  cr->fill_preserve();
  cr->set_line_width(_line_width);
  cr->set_color(_pen_color);
  cr->stroke();

  cr->move_to(get_position().x + _xpadding, get_position().y + (get_size().height + _text_size.height)/2);
  cr->set_color(_text_color);
  cr->show_text(_text);
  
  cr->restore();
}

