sgpemv2/src/schedulables_widget.cc

147 lines
3.9 KiB
C++
Raw Normal View History

// src/schedulables_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 "cairo_elements.hh"
#include "schedulables_widget.hh"
#include "backend/history.hh"
#include "backend/simulation.hh"
#include <iostream>
#include <memory>
using namespace sgpem;
SchedulablesWidget::SchedulablesWidget()
: Gtk::DrawingArea(), _h(1), _buf(NULL)
{
// Add other events we want to manage (as mouse clicks)
// when needed
add_events(Gdk::EXPOSURE_MASK);
// Register this observer:
Simulation::get_instance().get_history().attach(*this);
}
SchedulablesWidget::~SchedulablesWidget()
{
Simulation::get_instance().get_history().detach(*this);
}
void
SchedulablesWidget::update(const History& history)
{
// Determine the final height before to start drawing
unsigned int w = get_allocation().get_width();
_h = calc_height(history);
// FIXME TO THE EXPERT: this assertion fails.
// is this important???
assert(is_drawable());
// FIXME : write me using double buffering
// PROBLEM : get_window() seems to return a null pointer!!!
// I read on the web that get_window() returns a null pointer
// if the widget has not been realized
_buf = Gdk::Pixmap::create(get_window(), w, _h);
cairo_t* ctx = gdk_cairo_create(_buf->gobj());
// do the drawing...
draw_widget(ctx);
cairo_scale(ctx, w, _h);
// manually force an expose_event?
cairo_destroy(ctx);
}
void
SchedulablesWidget::draw_widget(cairo_t* ctx)
{
// NOTE: just to try
CairoElements ce(ctx);
Color red = { 1, 0, 0 };
Point center = { 25, 25 };
ce.draw_3dsphere(center, 25, red);
}
unsigned int
SchedulablesWidget::calc_height(const History& history) const
{
// FIXME: write me
return 400;
}
void
SchedulablesWidget::on_size_request(Gtk::Requisition* requisition)
{
// FIXME: take correct measures, consider also using a viewport(?)
*requisition = Gtk::Requisition();
requisition->width = get_allocation().get_width();
requisition->height = _h;
}
bool
SchedulablesWidget::on_expose_event(GdkEventExpose* event)
{
const Gtk::Allocation& allocation = get_allocation();
int width = allocation.get_width();
int height = allocation.get_height();
// Clip to redraw only the smallest possible area
std::auto_ptr<Gdk::Rectangle> clip_area;
if(event)
clip_area = std::auto_ptr<Gdk::Rectangle>(new Gdk::Rectangle(event->area.x, event->area.y,
event->area.width, event->area.height));
else
clip_area = std::auto_ptr<Gdk::Rectangle>(new Gdk::Rectangle(0, 0, width, height));
// Use double buffering or we're CPU-dead
// Copy from the buffer to the screen
if(_buf)
get_window()->draw_drawable(Gdk::GC::create(get_window()), _buf,
clip_area->get_x(), clip_area->get_y(),
clip_area->get_x(), clip_area->get_y(),
clip_area->get_width(), clip_area->get_height());
// calculated dinamically:
set_size_request(width, _h);
return true;
}
bool
SchedulablesWidget::on_button_press_event(GdkEventButton* event)
{
// Not here. Yet.
return false;
}