/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com

Copyright (c) 2011 I-maginer

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt
-----------------------------------------------------------------------------
*/

#ifndef _SCOL_EMBEDDED_WEB_NAVIGATOR_RENDER_HANDLER_
#define _SCOL_EMBEDDED_WEB_NAVIGATOR_RENDER_HANDLER_

#include "embeddedWebNavigatorHandler.h"

namespace Scol
{
  namespace EmbeddedWebNavigator
  {

class WebNavigatorRenderHandler : public CefRenderHandler,
                                  public WebNavigatorHandler
{
public:
protected:
private:
  bool isOffscreen;
  unsigned char* view_buffer;
  int view_buffer_size;
  unsigned char* popup_buffer;
  int popup_buffer_size;
  unsigned int view_buffer_width;
  unsigned int view_buffer_height;
  CefRect popup_rect;
  CefRefPtr<WebNavigatorCriticalSection> offscreenBufferCriticalSection;

public:
  /*!
  Constructor.
  */
  WebNavigatorRenderHandler(CefRefPtr<WebNavigatorClient>& parentWebNavigatorClientInstance, const ScolWindowHandle& scolMainWindow, bool offscreen, CefRefPtr<WebNavigatorCriticalSection>& offscreenBufferCriticalSectionInstance);

  /*!
  Destructor.
  */
  ~WebNavigatorRenderHandler();

  /*!
  */
  unsigned char* GetOffscreenBuffer();

  /*!
  */
  unsigned int GetOffscreenBufferWidth();

  /*!
  */
  unsigned int GetOffscreenBufferHeight();

  /*!
  @copydoc CefRenderHandler::GetViewRect
  */
  virtual bool GetViewRect(CefRefPtr<CefBrowser> browser, CefRect& rect);

  /*!
  @copydoc CefRenderHandler::GetScreenRect
  */
  virtual bool GetScreenRect(CefRefPtr<CefBrowser> browser, CefRect& rect);

  /*!
  @copydoc CefRenderHandler::GetScreenPoint
  */
  virtual bool GetScreenPoint(CefRefPtr<CefBrowser> browser, int viewX, int viewY, int& screenX, int& screenY);

  /*!
  @copydoc CefRenderHandler::OnPopupShow
  */
  virtual void OnPopupShow(CefRefPtr<CefBrowser> browser, bool show);

  /*!
  @copydoc CefRenderHandler::OnPopupSize
  */
  virtual void OnPopupSize(CefRefPtr<CefBrowser> browser, const CefRect& rect);

  /*!
  @copydoc CefRenderHandler::OnPaint
  */
  virtual void OnPaint(CefRefPtr<CefBrowser> browser, PaintElementType type, const RectList& dirtyRects, const void* buffer);

  /*!
  @copydoc CefRenderHandler::OnCursorChange
  */
  virtual void OnCursorChange(CefRefPtr<CefBrowser> browser, CefCursorHandle cursor);
protected:
  //Include the default reference counting implementation.
  IMPLEMENT_REFCOUNTING(WebNavigatorRenderHandler);

  // Include the default locking implementation.
  IMPLEMENT_LOCKING(WebNavigatorRenderHandler);
private:
  /*!
  Forbidden constructor.
  */
  WebNavigatorRenderHandler();

  /*!
  Set the contents of the RGB buffer.
  */
  void SetRGB(CefRefPtr<CefBrowser> browser, const void* src, const RectList& dirtyRects, bool view);

  /*!
  Size the RGB buffer.
  */
  void SetBufferSize(int width, int height, bool view);

  /*!
  Convert from BGRA to RGB format.
  */
  void ConvertToRGBA(const unsigned char* src, unsigned char* dst, const RectList& dirtyRects, int renderSizeWidth, int renderSizeHeight);
};

  }
}

#endif
