// -*- C++ -*- //

#ifndef GRAPH_DISPLAYER_CANVAS_H
#define GRAPH_DISPLAYER_CANVAS_H

class GraphDisplayerCanvas;

#include "all_include.h"
#include "mapstorage.h"
#include "broken_edge.h"
#include "map_win.h"
#include <libgnomecanvasmm.h>
#include <libgnomecanvasmm/polygon.h>

///This class is the canvas, on which the graph can be drawn.
class GraphDisplayerCanvas : public Gnome::Canvas::CanvasAA
{
  typedef Gnome::Canvas::CanvasAA Parent;

public:
  GraphDisplayerCanvas(MapStorage &, MapWin &);
  virtual ~GraphDisplayerCanvas();

  ///Changes the linewidth attribute according to the given map.
  ///\param mapname is the name of the map which contains the new values
  int changeEdgeWidth (std::string mapname, Graph::Edge new_item=INVALID);

  ///Changes the linecolor attribute according to the given map.
  ///\param mapname is the name of the map which contains the new values
  int changeEdgeColor (std::string mapname, Graph::Edge new_item=INVALID);

  ///Changes the text of line attribute according to the given map.
  ///\param mapname is the name of the map which contains the new values
  int changeEdgeText (std::string mapname, Graph::Edge new_item=INVALID);

  ///Changes the linewidth attribute according to the given map.
  ///\param mapname is the name of the map which contains the new values
  int changeNodeRadius (std::string mapname, Graph::Node new_item=INVALID);

  ///Changes the linecolor attribute according to the given map.
  ///\param mapname is the name of the map which contains the new values
  int changeNodeColor (std::string mapname, Graph::Node new_item=INVALID);

  ///Changes the text of line attribute according to the given map.
  ///\param mapname is the name of the map which contains the new values
  int changeNodeText (std::string mapname, Graph::Node new_item=INVALID);

  ///Callback for 'ViewZoomIn' action.
  virtual void zoomIn();
  ///Callback for 'ViewZoomOut' action.
  virtual void zoomOut();
  ///Callback for 'ViewZoomFit' action.
  virtual void zoomFit();
  ///Callback for 'ViewZoom100' action.
  virtual void zoom100();
  ///Sets the scroll region of the convas to the bounding box of the graph.
  void updateScrollRegion();

  ///This function changes the tool in the graph-editor's hand
  void changeEditorialTool(int);

protected:

  //maximizing, minimizing, restoring window, etc. 
  virtual bool on_expose_event(GdkEventExpose *);

private:

  ///This function is responsible for the correct
  ///reaction of any action happened in the territory
  ///of the canvas
  ///DEPRECATED!!!!
  bool eventHandler(GdkEvent* e, Node n);

  ///actual event handler
  ///
  ///Actual event handler should be stored, to be able to disconnect it and later reconnect it.
  sigc::connection actual_handler;

  ///event handler for the case when move-tool is active
  bool moveEventHandler(GdkEvent*);
  ///event handler for the case when create_node-tool is active
  bool createNodeEventHandler(GdkEvent*);
  ///event handler for the case when create_edge-tool is active
  bool createEdgeEventHandler(GdkEvent*);
  ///event handler for the case when eraser-tool is active
  bool eraserEventHandler(GdkEvent*);
  ///event handler for the case when edge map editor tool is active
  bool edgeMapEditEventHandler(GdkEvent*);
  ///event handler for the case when node map editor tool is active
  bool nodeMapEditEventHandler(GdkEvent*);

  ///event handler for the case when the entry widget is changed
  bool entryWidgetChangeHandler(GdkEvent*);

public:
  ///Moves the text to new place
  void textReposition(xy<double>);
  ///Activates an edge belonging to a BrokenEdge
  ///
  ///After we have activated an edge this way,
  ///the GDC object will know, which edge is under forming
  ///therefore it can redraw the necessarz elementy on the canvas,
  ///for example the text belonging to the \ref BrokenEdge can be
  ///redrawn (\ref textReposition).
  void toggleEdgeActivity(BrokenEdge*, bool);

public:
  ///\return the actual tool in hand
  int getActualTool();

  void drawGraph();
  void clear();

  ///creates a new Nodemap
  int addNewNodeMap(double,std::string);
  ///creates a new Edgemap
  int addNewEdgeMap(double,std::string);

private:
  ///Deletes the given element.
  void deleteItem(NodeIt);
  ///Deletes the given element.
  void deleteItem(EdgeIt);
  ///Deletes the given element.
  void deleteItem(Graph::Edge);

private:

  ///Map of nodes of graph
  Graph::NodeMap<Gnome::Canvas::Ellipse *> nodesmap;

  ///Map of edges of graph
  Graph::EdgeMap<BrokenEdge *> edgesmap;

  ///Map of texts to write on edges
  Graph::EdgeMap<Gnome::Canvas::Text *> edgetextmap;

  ///Map of texts to write on nodes
  Graph::NodeMap<Gnome::Canvas::Text *> nodetextmap;

  ///Group of graphical elements of displayed_graph
  Gnome::Canvas::Group displayed_graph;

  ///Map editor entry
  Gtk::Entry entrywidget;

  ///GnomeCanvas holder for entry
  Gnome::Canvas::Widget * canvasentrywidget;

  ///Here we store the maps that can be displayed through properties.
  MapStorage & mapstorage;

  ///Indicates whether the button of mouse is pressed or not
  int isbutton;

  ///Stores the actual tool in hand
  int actual_tool;

  ///At this location was the mousebutton pressed.
  ///It helps to calculate the distance of dragging.
  double clicked_x, clicked_y;

  ///Remembers which Gnome::Canvas::Item was pressed.
  ///this variable is needed, because
  ///1. we cannot query the item at he cursor as fast as it could not cause a Segmentation Fault
  ///2. we would like to handle only ony item per movement, therefore quering it is not a working solution
  Gnome::Canvas::Item * active_item, * target_item;
  Graph::NodeIt active_node;
  Graph::EdgeIt active_edge;
  Graph::EdgeIt forming_edge;

  std::string nodemap_to_edit, edgemap_to_edit;

  static const int zoom_step = 5;

  ///We need to store mapwin, to be able to ask the appropriate values for properties of new items.
  MapWin & mapwin;

};

#endif //GRAPH_DISPLAYER_CANVAS_H
