// src/simulation_widget.hh - 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 "config.h"

#include "cairo_widget.hh"
#include "cairo_elements.hh"
// #include "backend/string_utils.hh"
#include "gettext.h"


#include <glibmm/ustring.h>
#include <gtkmm/box.h>
#include <gtkmm/button.h>
#include <gtkmm/buttonbox.h>
#include <gtkmm/fixed.h>
#include <gtkmm/main.h>
#include <gtkmm/window.h>

#include <cassert>
#include <iostream>
#include <sstream>

#include <iostream>

// ------------------------------------------------------
//
// TestWidget class
//
// ------------------------------------------------------

namespace sgpem
{
  class TestWidget : public CairoWidget
  {
  public:
    TestWidget();
    virtual ~TestWidget();

  protected:
    virtual bool on_button_press_event(GdkEventButton* event);

    void draw_widget(cairo_t* ctx);
    void change_scaling_mode();

    virtual void calc_drawing_size(cairo_t* ctx, size_t& width, size_t& height);
    // void calc_size(const History& history, size_t& width, size_t& height) const;
  private:
    int _desired_w;
    int _desired_h;
  };

}

using namespace sgpem;

TestWidget::TestWidget()
    : Glib::ObjectBase("sgpem_TestWidget"), CairoWidget(),
    _desired_w(200), _desired_h(300)
{
}


TestWidget::~TestWidget()
{
}

bool
TestWidget::on_button_press_event(GdkEventButton* event)
{
  std::cout << " on_button_press_event " << std::endl;
  change_scaling_mode();
  // Not here. Yet.
  return true;
}

void
TestWidget::draw_widget(cairo_t* ctx)
{
  // show line
  cairo_set_source_rgb(ctx, 0, 0, 0);
  cairo_move_to(ctx, 0, 0);
  cairo_line_to(ctx, _desired_w, _desired_w);
  cairo_stroke(ctx);
  
  // NOTE: just to try
  CairoElements ce(ctx);

  Rectangle area( 10, 10, _desired_w-10, _desired_h/2 );
  ce.draw_container(area);
  Color red(1, 0, 0 );
  Point center1(_desired_w/4, _desired_h/4*3 );
  ce.draw_3dsphere(center1, _desired_w/10, red);
  
  Color yellow(1, 1, 0);
  Point center2(_desired_w/4*2, _desired_h/4*3);
  ce.draw_3dsphere(center2, _desired_w/10, yellow);

  Color green(0, 1, 0 );
  Point center3(_desired_w/4*3, _desired_h/4*3 );
  ce.draw_3dsphere(center3, _desired_w/10, green);

  // show text
  Glib::ustring msg;
  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, 10);
  cairo_show_text(ctx,msg.c_str());
  msg = "clic to change mode...";
  cairo_move_to(ctx, 10, 30);
  cairo_show_text(ctx,msg.c_str());
  



}


void
TestWidget::calc_drawing_size(cairo_t* ctx, size_t& width, size_t& height)
{
  // FIXME: write me
  // some magic here!
  width  = _desired_w;
  height = _desired_h;
}

void
TestWidget::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();
  // resize_redraw();
}


// ------------------------------------------------------
//
// MainWindow class
//
// ------------------------------------------------------
class MainWindow : public Gtk::Window
{
public:
  MainWindow();
  virtual ~MainWindow();

protected:
  TestWidget _test_widget;
};


MainWindow::MainWindow()
{
  // This just sets the title of our new window.
  set_title("Cairo Widget Test: \"Do you like semaphores?\"");

  add(_test_widget);
  _test_widget.show();
}

MainWindow::~MainWindow()
{
}



// ------------------------------------------------------
//
// main()
//
// ------------------------------------------------------
int
main(int argc, char** argv)
{
  int ret = 0;


  Gtk::Main kit(argc, argv);
  
  // Gtk::Window win;
  MainWindow win;
  
  win.set_border_width(10);
  win.resize (200, 200);

  Gtk::Main::run(win);

  return ret;
}


