// src/resources_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 3 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, see http://www.gnu.org/licenses/.


#ifndef RESOURCES_WIDGET_HH
#define RESOURCES_WIDGET_HH 1

namespace sgpem
{
  class Resource;
}

#include <gtkmm/box.h> // Needed for a bug in gtkmm 2.22
#include <gtkmm/treeview.h>
#include <gtkmm/liststore.h>
#include <gtkmm/menu.h>
#include <gtkmm/uimanager.h>
#include <gtkmm/actiongroup.h>
#include <gtkmm/dialog.h>
#include <gtkmm/builder.h>

#include "sgpemv2/history_observer.hh"

namespace sgpem
{
  /// \brief A ResourcesWidget displays the resources during the simulation 
  /// and lets the user modify them.
  ///
  /// A ResourceWidget is a Gtk::Treewidget customized with the aim of
  /// representing resources and their inner queues.
  /// As an editor, the widget allows the user to select a resource and
  /// modify it pressing the left mouse button over it.
  ///
  /// A ResourcesWidget is also a HistoryObserver, which means that it is
  /// automatically updated at every notified update of the History.
  class ResourcesWidget : public Gtk::TreeView, public HistoryObserver
  {
  public:
    /** \brief Constructor required by gtkbuilder
     */
    ResourcesWidget(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& ui);

    /// \brief Standard virtual destructor.
    ///
    virtual ~ResourcesWidget();

    /// \brief updates the widget with the new content of the notifying History
    ///
    /// \param history the notifying history.
    virtual void update(const History& history);
    
    /// \brief Manages the mouse button press event.
    /// 
    /// This method pops up a menu and offers the user the tools to modify or add a resource.
    /// \param event the event to manage.
    bool on_button_press_event(GdkEventButton* event);

  private:
    /// \brief a CellRenderer which allows rhe rendering of
    /// marked-up text.
    ///
    /// This class allows the customization of the text displayed
    /// inside the lines of the TreeView.
    class CellRendererTextMarkup : public Gtk::CellRendererText
    {
      public:
        /// \brief Customize this to render the text.
        Glib::PropertyProxy_Base _property_renderable();
    };

    /// \brief Returns the current selection via the in/out parameter selection.
    /// \param selection the parameter where the current selection will be set
    /// \return \c true if the selection is valid, \c false otherwise
    bool get_selected_key(unsigned int& selection);
    
    /// \brief Returns the selected Resource.
    ///
    /// Returns the selected Resource.
    /// \return a pointer to the selected Resource
    Resource* get_selected_resource();

    /// \brief Manages the user choice of adding a resoure.
    ///
    /// Calls add_edit_resource.
    /// Displays a popup dialog which asks for details of the new resource.
    void _on_add_resource();

    /// \brief Manages the user choice of editing a resoure.
    ///
    /// Calls add_edit_resource.
    /// Displays a popup dialog which asks for details of the edited resource.
    void _on_edit_resource();

    /// \brief Manages the user choice of adding or editing a resoure.
    ///
    /// Displays a popup dialog which asks for details of the new or edited resource.
    void add_edit_resource(bool adding);

    /// \brief Manages the user choice of removing a resoure.
    ///
    /// Calls add_edit_resource.
    /// Displays a popup dialog which asks for details of the new resource.    
    void _on_remove_resource();
    
    /// \brief Updates the cell text with proper rendering.
    /// 
    /// \param cr the cell renderer used to render
    /// \param it an iterator over the underlying model
    void _on_cell_name_data(Gtk::CellRenderer* cr, 
                            const Gtk::TreeModel::iterator& it);
    
    Glib::RefPtr<Gtk::ListStore> _model;
    Gtk::TreeModelColumnRecord _columns;
    Gtk::TreeModelColumn<unsigned int> _key_column;
    Gtk::TreeModelColumn<Glib::ustring> _main_column;
    Gtk::TreeModelColumn<void*> _handles_column;
    
    Glib::RefPtr<Gtk::UIManager> _UIManager;
    Glib::RefPtr<Gtk::Builder>   _add_resource_dialog_ui;
    Gtk::Dialog* _add_resource_dialog;

    CellRendererTextMarkup _cell_renderer;
  };

} //~ namespace sgpem


#endif //~ RESOURCES_WIDGET_HH
