/*
-----------------------------------------------------------------------------
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
-----------------------------------------------------------------------------
*/

#include "scolEmbeddedWebNavigatorLifeSpan.h"
#include "scolEmbeddedWebNavigator.h"
#include "embeddedWebNavigatorPopupInformations.h"

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnPopup, WEB_NAVIGATOR_LIFESPAN_ON_POPUP_CB)
int FireWebNavigatorReflexiveOnPopup(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnPopup\n");
#endif

  Scol::EmbeddedWebNavigator::PopupInformations* popupInformations = (Scol::EmbeddedWebNavigator::PopupInformations*) wParam;
  int res = 0;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)popupInformations->parentWebNavigator, WEB_NAVIGATOR_LIFESPAN_ON_POPUP_CB))
  {
    MMpush(m, ITOM(popupInformations->x));
    MMpush(m, ITOM(popupInformations->y));
    MMpush(m, ITOM(popupInformations->height));
    MMpush(m, ITOM(popupInformations->width));
    Mpushstrbloc(m, (char*)(popupInformations->url.c_str()));
    res = OBJcallreflex(m, 5);
  }

  delete(popupInformations);
  return res;
}

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(RunModal, WEB_NAVIGATOR_LIFESPAN_RUN_MODAL_CB)
int FireWebNavigatorReflexiveRunModal(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveRunModal\n");
#endif

  int res = 0;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, wParam, WEB_NAVIGATOR_LIFESPAN_RUN_MODAL_CB))
    res = OBJcallreflex(m, 0);

  return res;
}

// Nb of Scol functions or types
#define WebNavigatorLifeSpanScolFunctionsNb 2

/**
 *	Scol function names
 */
char* WebNavigatorLifeSpanScolFunctionsNames[WebNavigatorLifeSpanScolFunctionsNb] =
{
  "WebNavigatorCbOnPopup",
  "WebNavigatorCbRunModal"
};

/**
 *	Pointers to C functions that manipulate the VM for each scol function previously defined
 */
int (*WebNavigatorLifeSpanScolFunctionsPtr[WebNavigatorLifeSpanScolFunctionsNb])(mmachine m)=
{
  WebNavigatorCbOnPopup,
  WebNavigatorCbRunModal
};

/**
 *	Nb of arguments of each scol function
 */
int WebNavigatorLifeSpanScolFunctionsNbArgs[WebNavigatorLifeSpanScolFunctionsNb]=
{
  3,                                          // WebNavigatorCbOnPopup
  3                                           // WebNavigatorCbRunModal
};

/**
 *	Prototypes of the scol functions
 */
char* WebNavigatorLifeSpanScolFunctionsType[WebNavigatorLifeSpanScolFunctionsNb]=
{
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 I I I I S] u1 u0] ObjWebNavigator", // WebNavigatorCbOnPopup
  "fun [ObjWebNavigator fun [ObjWebNavigator u0] u1 u0] ObjWebNavigator",           // WebNavigatorCbRunModal
};

int LoadWebNavigatorLifeSpan(mmachine m)
{
  return PKhardpak(m, "WebNavigatorLifeSpan", WebNavigatorLifeSpanScolFunctionsNb, WebNavigatorLifeSpanScolFunctionsNames, WebNavigatorLifeSpanScolFunctionsPtr, WebNavigatorLifeSpanScolFunctionsNbArgs, WebNavigatorLifeSpanScolFunctionsType);
}
