// src/schedulable_state_widget.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SGPEMv2 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include "gettext.h"

#include "schedulable_state_widget.hh"

#include <sgpemv2/templates/smartp.tcc>

#include <glibmm/ustring.h>

#include <cassert>

using namespace sgpem;


memory::smart_ptr<Gtk::Tooltips>
SchedulableStateWidget::_global_ttips = NULL;


SchedulableStateWidget::SchedulableStateWidget(Schedulable::state st)
  : Glib::ObjectBase("sgpem_SchedulableStateWidget"), CairoWidget(), _color(1, 1, 1)
{
  // Yes, we leak, but this will be up until the end of the app
  if(!_global_ttips) _global_ttips = new Gtk::Tooltips();
  _ttips = _global_ttips;

  set_scaling_mode(scaling_to_w);
  set_state(st);
}

SchedulableStateWidget::~SchedulableStateWidget()
{
  _ttips->unset_tip(*this);
  _ttips = memory::smart_ptr<Gtk::Tooltips>::null;
  if(_global_ttips.alive_refs() == 1)
    _global_ttips = memory::smart_ptr<Gtk::Tooltips>::null;
}


void 
SchedulableStateWidget::set_state(Schedulable::state new_state)
{
  Glib::ustring tip;
  switch(new_state)
    {
    case Schedulable::state_future:
      _color = Color(1, 1, 1); // White
      tip = _("Future");
      break;
    case Schedulable::state_ready:
      _color = Color(1, 1, 0); // Yellow
      tip = _("Ready");
      break;
    case Schedulable::state_running:
      _color = Color(0, 1, 0); // Green
      tip = _("Running");
      break;
    case Schedulable::state_blocked:
      _color = Color(1, 0, 0); // Red
      tip = _("Blocked");
      break;
    case Schedulable::state_terminated:
      _color = Color(0, 0, 0); // Black
      tip = _("Terminated");
      break;
    }

  if(is_realized())
    {
      assert(has_no_window() == false);
      _ttips->set_tip(*this, tip);
    }

  queue_draw();
}

void 
SchedulableStateWidget::draw_widget(cairo_t* ctx)
{
  CairoElements ce(ctx);
  static const Point center(.5f, .5f);
  ce.draw_3dsphere(center, .49f, _color);
}


void 
SchedulableStateWidget::calc_drawing_size(size_t& width, size_t& height) const
{
  width  = 1;
  height = 1;
}


void 
SchedulableStateWidget::calc_widget_size(size_t& width, size_t& height) const
{
  static const size_t min_size = 15; // px

  if(width != height)
    if(width < min_size || height < min_size)
      width = height = min_size;
    else
      width = height = std::min(width, height);
}
