// src/ready_queue_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 "ready_queue_widget.hh"

#include <sgpemv2/history.hh>
#include <sgpemv2/environment.hh>
#include <sgpemv2/ready_queue.hh>
#include <sgpemv2/thread.hh>

#include <glibmm/markup.h>
#include <glibmm/ustring.h>

#include <sstream>

using namespace sgpem;

static const Glib::ustring string_start = _("<b>Ready queue: { </b>");
static const Glib::ustring string_end   = _("<b> } at instant </b>");
static const Glib::ustring separator    = " ~ ";

ReadyQueueWidget::ReadyQueueWidget(History& history)
  : Gtk::Label(string_start + string_end + "0"), _h(history)
{
  _h.attach(*this);

  set_use_markup(true);
  set_justify(Gtk::JUSTIFY_LEFT);
  set_padding(5, 3);
}


ReadyQueueWidget::~ReadyQueueWidget()
{
  _h.detach(*this);
}


void 
ReadyQueueWidget::update(const History& changed_history)
{
  std::ostringstream oss;
  oss << string_start;

  const ReadyQueue& rq = changed_history.get_last_environment().get_sorted_queue();
  size_t size = rq.size();

  for(size_t i = 0; i < size; ++i)
    oss << Glib::Markup::escape_text(rq.get_item_at(i).get_name()) << separator;

  unsigned int instant = changed_history.get_front() == 0 ? 0 : changed_history.get_front() - 1;
  oss << string_end << instant;
  
  set_markup(oss.str());
}

