#ifndef _DrawWidget_H_
#define _DrawWidget_H_

#include <Fl/Fl.H>
#include <Fl/Fl_Widget.H>

typedef unsigned int uint;

/** 
    A widget for drawing into.  This derives a subclass from
    Fl_Widget.  Coordinate transformation functions are defined which
    invert the Y axis so the positive Y axis goes up instead of
    down. The drawing environment can be at a different scale from the
    display.
 */ 

class DrawWidget : public Fl_Widget {

protected:
  /// Minimum x value of drawing area
  float draw_min_x;
  /// Minimum y value of drawing area
  float draw_min_y;
  /// Maximum x value of drawing area
  float draw_max_x;
  /// Maximum y value of drawing area
  float draw_max_y;

  /// Width of the drawing area (calculated from draw_min_x draw_max_x)
  float draw_width;
  /// Heigth of the drawing area (calculated from draw_min_y draw_max_y)
  float draw_height;

  /// Global width of the widget
  float widget_width;
  /// Global height of the widget
  float widget_height;

  /// Override the draw method
  virtual void draw()=0;
  /// Override the handle method
  virtual int handle(int e);

  /// Helper function for handle()
  virtual void getClick(float & x, float & y);

public:
  /** Constructor.
      @par x The upper left corner of the widget
      @par y The upper left corner of the widget
      @par w The width of the widget
      @par h The height of the widget
      @par min_x The minimum x coordinate of the drawing area
      @par min_y The minimum y coordinate of the drawing area
      @par max_x The maximum x coordinate of the drawing area
      @par max_y The maximum y coordinate of the drawing area
   */
  DrawWidget(int x, 
	     int y, 
	     int w, 
	     int h,
	     float min_x, 
	     float min_y, 
	     float max_x, 
	     float max_y,
	     const char * s=0);

  /** Constructor.
      @par x The upper left corner of the widget
      @par y The upper left corner of the widget
      @par w The width of the widget
      @par h The height of the widget
   */
  DrawWidget(int x, 
	     int y, 
	     int w, 
	     int h,
	     const char * s=0);

  /** Set the drawing area size 
      @par min_x The minimum x coordinate of the drawing area
      @par min_y The minimum y coordinate of the drawing area
      @par max_x The maximum x coordinate of the drawing area
      @par max_y The maximum y coordinate of the drawing area
   */
  virtual void setDrawingBounds(float min_x, 
				float min_y, 
				float max_x, 
				float max_y);
  
  /// Convert a pixel value into an X coordinate
  virtual float uToX(int u) const;

  /// Convert a pixel value into a Y coordinate.  Also flips the Y value
  virtual float vToY(int v) const;

  /// Convert a Y coordinate into a pixel value
  virtual int xToU(float x) const;

  /// Convert a Y coordinate into a pixel value.  Also flips the Y value
  virtual int yToV(float y) const;

  /// Convert a width in X coordinates into a width in pixels
  virtual int widthToU(float w) const;

  /// Convert a height in Y coordinates into a height in pixels
  virtual int heightToV(float h) const;
  
  // I still need widthToX and heightToY to go the other direction...
};

#endif
