// 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/WPaintDevice>
#include <Wt/WObject>

namespace Wt {

class DomElement;
class WTransform;

/*! \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.
 *
 * <i>Note: the current implementation has only limited support for
 * text, which is rendered in an overlayed DIV. As a consequence text
 * is not subject to rotation and scaling components of the current
 * transformation. This will be fixed in the future (some way, some
 * how!).</i>
 *
 * \ingroup painting
 */
class WT_API WCanvasPaintDevice : public WPaintDevice, public WObject
{
public:
  /*! \brief Create a canvas paint device.
   */
  WCanvasPaintDevice(const WLength& width, const WLength& height,
		     WObject *parent = 0);

  virtual void setChanged(int 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, int flags,
			const WString& text);
  virtual void init();
  virtual void done();

  void render(const std::string& canvasId, DomElement* text);

private:
  int changeFlags_;

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

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

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

}

#endif // WCANVAS_PAINT_DEVICE_H_
