// ---------------------------------------------------------------------------
// (c) 2003 Geotalk, Hans Martin Mohn
// ---------------------------------------------------------------------------
//
// Generic tool class.
//
//    Create children of this class to override the various event handlers.
//
// Dependencies:
//
//    None
//

function gtTool()
{
  // Events
  this.evtSelect = new gtEventHandler();

  // Member variables
  this.m_context = null;  // The context (document, canvas, selection, etc) to operate upon.
} // end gtTool Constructor

var GTTOOL = gtTool.prototype;

// Methods
GTTOOL.buttonDown     = function(evt)       { return true; };
GTTOOL.mouseMove      = function(evt)       { return true; };
GTTOOL.buttonUp       = function(evt)       { return true; };
GTTOOL.enter          = function(evt)       { return true; };
GTTOOL.leave          = function(evt)       { return true; };
GTTOOL.click          = function(evt)       { return true; };
GTTOOL.keyDown        = function(evt)       { return true; };
GTTOOL.menuItemClick  = function(itemName)  {};
GTTOOL.deselect       = function()          {}; // Called when this tool is deselected
GTTOOL.select         = function(context)   { this.m_context = context; }; // called when this tool is selected
GTTOOL.update         = function()          {}; // Called after the window has been reloaded/drawn

// Check the given selection to see whether this tool knows how to handle it.
// Can be used to disable the UI elements activating this tool
gtTool.prototype.canHandleSelection = function (selection) { return false; };


function gtBeToolHandler(element)
{
  this.m_tool = null;

  // ----------------------------------------------------------------------------
  // Mapping of mouse and keyboard events to the current tool
  // ----------------------------------------------------------------------------
  element.gtButtonDown  = function(evt)       { if (this.m_tool) return this.m_tool.buttonDown   (evt); };
  element.gtMouseMove   = function(evt)       { if (this.m_tool) return this.m_tool.mouseMove    (evt); };
  element.gtButtonUp    = function(evt)       { if (this.m_tool) return this.m_tool.buttonUp     (evt); };
  element.gtEnter       = function(evt)       { if (this.m_tool) return this.m_tool.enter        (evt); };
  element.gtLeave       = function(evt)       { if (this.m_tool) return this.m_tool.leave        (evt); };
  element.gtClick       = function(evt)       { if (this.m_tool) return this.m_tool.click        (evt); };
  element.gtKeyDown     = function(evt)       { if (this.m_tool) return this.m_tool.keyDown      (evt); };
  element.menuItemClick = function(itemName)  { if (this.m_tool) return this.m_tool.menuItemClick(itemName); };

  element.selectTool = function(tool)
  {
    var prev = this.m_tool;
    if (this.m_tool)
    {
      this.m_tool.evtSelect.raise(false);
      this.m_tool.deselect();
    }
    this.m_tool = tool;
    if (this.m_tool !== null)
    {
      this.m_tool.select(this);
      this.m_tool.evtSelect.raise(true);
    }
    return prev;
  }; // end selectTool()

} // end function gtBeToolHandler()

///////////////////////////////////////////////////////////////////////////////////////


