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

#include <Wt/WBrush>
#include <Wt/WFont>
#include <Wt/WObject>
#include <Wt/WPaintDevice>
#include <Wt/WPen>
#include <Wt/WPointF>
#include <Wt/WShadow>
#include <Wt/WTransform>

#include <sstream>

namespace Wt {

class DomElement;
class WTransform;
class ServerSideFontMetrics;

/*! \class WCanvasPaintDevice Wt/WCanvasPaintDevice Wt/WCanvasPaintDevice
 *  \brief A paint device for rendering using the HTML 5 &lt;canvas&gt; element.
 *
 * The %WCanvasPaintDevice is used by WPaintedWidget to render to the
 * browser using the HTML 5 &lt;canvas&gt; element. You usually will
 * not use the device directly, but rather rely on WPaintedWidget to
 * use this device when appropriate.
 *
 * \note Older browsers do not have text support in &lt;canvas&gt;.
 * Text is then rendered in an overlayed DIV and a consequence text is
 * not subject to rotation and scaling components of the current
 * transformation (but does take into account translation). On most
 * browser you can use the WSvgImage or WVmlImage paint devices which
 * do support text natively.
 *
 * \ingroup painting
 */
class WT_API WCanvasPaintDevice : public WObject, public WPaintDevice
{
public:
  /*! \brief Create a canvas paint device.
   */
  WCanvasPaintDevice(const WLength& width, const WLength& height,
		     WObject *parent = 0,
		     bool paintUpdate = false);
  virtual ~WCanvasPaintDevice();

  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; }

  void render(const std::string& canvasId, DomElement* text);
  void renderPaintCommands(std::stringstream& js, const std::string& element);

  virtual WLength width() const { return width_; }
  virtual WLength height() const { return height_; }

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

private:
  enum TextMethod { MozText, Html5Text, DomText };

  WLength     width_, height_;
  WPainter   *painter_;
  WFlags<ChangeFlag> changeFlags_;
  bool        paintUpdate_;

  TextMethod  textMethod_;

  bool        busyWithPath_, currentNoPen_, currentNoBrush_;

  WTransform  currentTransform_;
  WBrush      currentBrush_;
  WPen        currentPen_;
  WShadow     currentShadow_;
  WFont       currentFont_;
  WPointF     pathTranslation_;
  AlignmentFlag currentTextHAlign_, currentTextVAlign_;
  ServerSideFontMetrics *fontMetrics_;

  std::stringstream js_;
  std::vector<DomElement *> textElements_;
  std::vector<std::string> images_;

  void finishPath();
  void renderTransform(std::stringstream& s, const WTransform& t,
		       bool invert = false);
  void renderStateChanges(bool resetPathTranslation);
  void resetPathTranslation();
  void drawPlainPath(std::stringstream& s, const WPainterPath& path);

  int createImage(const std::string& imgUri);

  TextMethod textMethod() const { return textMethod_; }

  friend class WWidgetCanvasPainter;
};

}

#endif // WCANVAS_PAINT_DEVICE_H_
