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

#include <glibmm/refptr.h>
#include <gtkmm/action.h>
#include <gtkmm/actiongroup.h>
#include <gtkmm/menu.h>
#include <gtkmm/uimanager.h>


using namespace sgpem;
using namespace Gtk;
using namespace Glib;


HoltContainerWindow::HoltContainerWindow(Simulation& simulation)
  : Window(), _holt_widget(simulation), _auto_dispose(true)
{
  // This just sets the title of our new window.
  set_title(_("Holt Graph"));
  set_type_hint(Gdk::WINDOW_TYPE_HINT_UTILITY);

  add(_holt_widget);
  _holt_widget.set_scaling_mode(CairoWidget::scaling_min);
  _holt_widget.show();
  _holt_widget.set_show_threads(true);
  //set_keep_above();
}

HoltContainerWindow::~HoltContainerWindow()
{
}

void  HoltContainerWindow::on_size_request(Gtk::Requisition* /* requisition */ )
{
  set_disposition();
}

HoltWidget& HoltContainerWindow::get_holt_widget()
{
  return _holt_widget;
}








bool 
HoltContainerWindow::on_button_press_event(GdkEventButton* event)
{
  if((event->type & Gdk::BUTTON_PRESS) == Gdk::BUTTON_PRESS && (event->button == 3))
  {
    Glib::ustring ui_info = 
    "<ui>"
    "  <popup name='PopupMenu'>";

    HoltWidget::arrange_mode  arrange = _holt_widget.get_arrange_mode();
    CairoWidget::scaling_mode scaling = _holt_widget.get_scaling_mode();
    
    RefPtr<ActionGroup> action_group = Gtk::ActionGroup::create();

    if(_auto_dispose || arrange!=HoltWidget::arrange_vertical)
    {
      action_group->add( Gtk::Action::create("DisposeVertical", "Dispose _vertical"),
      sigc::mem_fun(*this, &HoltContainerWindow::_on_dispose_vertical) );
      ui_info += "    <menuitem action='DisposeVertical'/>";
    }
    
    if(_auto_dispose || arrange!=HoltWidget::arrange_horizontal)
    {
      action_group->add( Gtk::Action::create("DisposeHorizontal", "Dispose _horizontal"),
      sigc::mem_fun(*this, &HoltContainerWindow::_on_dispose_horizontal) );
      ui_info += "    <menuitem action='DisposeHorizontal'/>";
    }
    
    if(_auto_dispose || arrange!=HoltWidget::arrange_circular)
    {
      action_group->add( Gtk::Action::create("DisposeCircular", "Dispose _circular"),
      sigc::mem_fun(*this, &HoltContainerWindow::_on_dispose_circular) );
      ui_info += "    <menuitem action='DisposeCircular'/>";
    }
    
    if(!_auto_dispose)
    {
      action_group->add( Gtk::Action::create("DisposeAuto", "_Auto dispose"),
      sigc::mem_fun(*this, &HoltContainerWindow::_on_dispose_auto) );
      ui_info += "    <menuitem action='DisposeAuto'/>";
    }

    /* disabled scaling options
    if(scaling==CairoWidget::scaling_min)
    {
      action_group->add( Gtk::Action::create("ScalingNone", "Auto _resize window"),
      sigc::mem_fun(*this, &HoltContainerWindow::_on_no_scaling) );
      ui_info +=    "    <separator/>"
      "    <menuitem action='ScalingNone'/>";
    }
    else 
    {
      // scaling==scaling_none
      action_group->add( Gtk::Action::create("ScalingFit", "_Fit in window"),
      sigc::mem_fun(*this, &HoltContainerWindow::_on_fit_scaling) );
      ui_info +=    "    <separator/>"
      "    <menuitem action='ScalingFit'/>";
    }
    */
    
    RefPtr<UIManager> UIManager = Gtk::UIManager::create();
    UIManager->insert_action_group(action_group);

    ui_info += 
    "  </popup>"
    "</ui>";

    UIManager->add_ui_from_string(ui_info);
    Gtk::Menu* menu = dynamic_cast<Gtk::Menu*>(UIManager->get_widget("/PopupMenu"));

    menu->popup(event->button, event->time);
    return true; //It has been handled.
  }
  else
    return false;
}

void
HoltContainerWindow::_on_dispose_vertical()
{
  _auto_dispose = false;
  _holt_widget.set_arrange_mode(HoltWidget::arrange_vertical);
  _holt_widget.resize_redraw();
}

void
HoltContainerWindow::_on_dispose_circular()
{
  _auto_dispose = false;
  _holt_widget.set_arrange_mode(HoltWidget::arrange_circular);
  _holt_widget.resize_redraw();
}

void
HoltContainerWindow::_on_dispose_horizontal()
{
  _auto_dispose = false;
  _holt_widget.set_arrange_mode(HoltWidget::arrange_horizontal);
  _holt_widget.resize_redraw();
}

void
HoltContainerWindow::_on_dispose_auto()
{
  _auto_dispose = true;
  set_disposition();
  _holt_widget.resize_redraw();
}

void
HoltContainerWindow::_on_no_scaling()
{
  _holt_widget.set_scaling_mode(CairoWidget::scaling_none);
  _holt_widget.resize_redraw();
}

void
HoltContainerWindow::_on_fit_scaling()
{
  _holt_widget.set_scaling_mode(CairoWidget::scaling_min);
  _holt_widget.resize_redraw();
}

  void  HoltContainerWindow::set_disposition()
  {
    if(_auto_dispose){
      int height = get_height();
      int width  = get_width();
      // if height/width >=5/3
      if(height * 3 >= width * 5)
      {
        _holt_widget.set_arrange_mode(HoltWidget::arrange_vertical);
      }
      else if( width * 3 >= height * 5)
      {
        _holt_widget.set_arrange_mode(HoltWidget::arrange_horizontal);
      }
      else
      {
        _holt_widget.set_arrange_mode(HoltWidget::arrange_circular);
      }
    }
  }
