// This may look like C code, but it's really -*- C++ -*-
/*
 * Copyright (C) 2014 Emweb bvba, Kessel-Lo, Belgium.
 *
 * See the LICENSE file for terms of use.
 */
#ifndef WMEASURE_PAINT_DEVICE_H_
#define WMEASURE_PAINT_DEVICE_H_

#include <Wt/WPaintDevice>
#include <Wt/WRectF>

namespace Wt {

/*! \brief WMeasurePaintDevice Wt/WMeasurePaintDevice Wt/WMeasurePaintDevice
 *
 * This implements a (pseudo)-paintdevice which measures the bounding
 * rect of whatever is being painted on it, using fontmetrics from the
 * underlying device.
 *
 * The only output of the device is the computation of a bounding rect
 * which is returned by boundingRect().
 */
class WMeasurePaintDevice : public WPaintDevice
{
public:
  /*! \brief Creates a paint device to measure for the underlying device.
   */
  WMeasurePaintDevice(WPaintDevice *paintDevice);

  /*! \brief Returns the bounding rectangle of everything painted so far.
   *
   * The bounding rect is returned in device coordinates (i.e. after all
   * transformations applied).
   */
  WRectF boundingRect() const { return bounds_; }

  virtual WFlags<FeatureFlag> features() const;
  virtual void setChanged(WFlags<ChangeFlag> flags);
  virtual void drawArc(const WRectF& rect, double startAngle, double spanAngle);
  virtual void drawImage(const WRectF& rect, const std::string& imgUri,
			 int imgWidth, int imgHeight, const WRectF& sourceRect);
  virtual void drawLine(double x1, double y1, double x2, double y2);
  virtual void drawPath(const WPainterPath& path);
  virtual void drawText(const WRectF& rect, 
			WFlags<AlignmentFlag> alignmentFlags,
			TextFlag textFlag,
			const WString& text);
  virtual WTextItem measureText(const WString& text, double maxWidth = -1,
				bool wordWrap = false);
  virtual WFontMetrics fontMetrics();
  virtual void init();
  virtual void done();
  virtual bool paintActive() const { return painter_ != 0; }

  virtual WLength width() const;
  virtual WLength height() const;

protected:
  virtual WPainter *painter() const { return painter_; }
  virtual void setPainter(WPainter *painter) { painter_ = painter; }

private:
  WPainter *painter_;
  WPaintDevice *device_;

  WRectF bounds_;

  void expandBounds(const WRectF& bounds);
};

}

#endif // WMEASURE_PAINT_DEVICE_H_
