// src/simulation_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 "simulation_widget.hh" #include "cairo_elements.hh" #include "backend/history.hh" #include "backend/simulation.hh" #include "backend/string_utils.hh" #include #ifndef NDEBUG #include #endif using namespace sgpem; SimulationWidget::SimulationWidget() : Glib::ObjectBase("sgpem_SimulationWidget"), CairoWidget(), _n_update(0) { // Register this observer: Simulation::get_instance().get_history().attach(*this); } SimulationWidget::~SimulationWidget() { Simulation::get_instance().get_history().detach(*this); } void SimulationWidget::update(const History& history) { _n_update++; std::cout << " update: " << _n_update << std::endl; // Force redraw redraw(); } void SimulationWidget::draw_widget(cairo_t* ctx) { cairo_set_source_rgb(ctx, 0, 0, 0); cairo_move_to(ctx, 0, 0); cairo_line_to(ctx, 300, 350); cairo_stroke(ctx); cairo_set_source_rgb(ctx, 0, 0, 0); cairo_move_to(ctx, 0, 0); cairo_line_to(ctx, 300, 350); // NOTE: just to try CairoElements ce(ctx); Rectangle area = { 30, 30, 100, 100 }; ce.draw_container(area); Color red = { 1, 0, 0 }; Point center = { 25, 25 }; ce.draw_3dsphere(center, 20, red); Glib::ustring msg("Number of updates: "), number; int_to_string(_n_update, number); msg = msg + number; cairo_move_to(ctx, 10, 105); cairo_show_text(ctx,msg.c_str()); switch(get_scaling_mode()) { case CairoWidget::scaling_none: msg = "scaling_none = without any scaling"; break; case CairoWidget::scaling_to_w: msg = "scaling_to_w = uses width to calculate scale factor"; break; case CairoWidget::scaling_to_h: msg = "scaling_to_h = uses height to calculate scale factor"; break; case CairoWidget::scaling_min: msg = "scaling_min = uses minimum scale factor from width, height"; break; case CairoWidget::scaling_max: msg = "scaling_max = uses maximum scale factor from width, height"; break; case CairoWidget::scaling_all: msg = "scaling_all = scale to stretch into client area"; break; } cairo_move_to(ctx, 10, 115); cairo_show_text(ctx,msg.c_str()); msg = "clic to change mode..."; cairo_move_to(ctx, 10, 125); cairo_show_text(ctx,msg.c_str()); } void SimulationWidget::calc_drawing_size(size_t& width, size_t& height) const { // FIXME: write me // some magic here! width = 150; height = 150; } bool SimulationWidget::on_button_press_event(GdkEventButton* event) { std::cout << " on_button_press_event " << std::endl; change_scaling_mode(); // Not here. Yet. return true; } void SimulationWidget::change_scaling_mode() { std::cout << " change_scaling_mode " << std::endl; scaling_mode scaling = get_scaling_mode(); switch(scaling) { case scaling_none: set_scaling_mode(scaling_to_w); break; case scaling_to_w: case scaling_to_h: case scaling_min: case scaling_max: set_scaling_mode((scaling_mode)(scaling << 1)); break; case scaling_all: set_scaling_mode(scaling_none); break; } redraw(); }