/*
-----------------------------------------------------------------------------
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 "scolEmbeddedWebNavigatorFind.h"
#include "scolEmbeddedWebNavigator.h"
#include "embeddedWebNavigatorFindInformations.h"

WEB_NAVIGATOR_GENERATE_SCOL_ADD_CB_FUNCTION(OnFindResult, WEB_NAVIGATOR_FIND_ON_RESULT_CB)
int FireWebNavigatorReflexiveOnFindResult(mmachine m, ScolWindowHandle hwnd, unsigned int msg, UINT wParam, LONG lParam, int* ret)
{
#ifdef SCOL_DEBUG
  MMechostr(MSKDEBUG, "FireWebNavigatorReflexiveOnFindResult\n");
#endif

  int res = 0;
  Scol::EmbeddedWebNavigator::FindInformations* findInformations = (Scol::EmbeddedWebNavigator::FindInformations*)wParam;
  if(!OBJbeginreflex(m, OBJWEBNAVIGATORSCOL, (int)findInformations->parentWebNavigator, WEB_NAVIGATOR_FIND_ON_RESULT_CB))
  {
    MMpush(m, ITOM(findInformations->identifier));
    MMpush(m, ITOM(findInformations->count));
    // Create the 'selectionRect' return tuple
    int scolselectionRect = MMmalloc(m, 4, TYPETAB);
    if(scolselectionRect==NIL)
      return MERRMEM;
    // Store the selectionRect
    MMstore(m, scolselectionRect, 3, ITOM(findInformations->selectionRect.x));
    MMstore(m, scolselectionRect, 2, ITOM(findInformations->selectionRect.y));
    MMstore(m, scolselectionRect, 1, ITOM(findInformations->selectionRect.width));
    MMstore(m, scolselectionRect, 0, ITOM(findInformations->selectionRect.height));
    MMpush(m, PTOM(scolselectionRect));
    MMpush(m, ITOM(findInformations->activeMatchOrdinal));
    if(findInformations->finalUpdate)
      MMpush(m, ITOM(1));
    else
      MMpush(m, ITOM(0));
    res = OBJcallreflex(m, 5);
  }

  delete(findInformations);
  return res;
}

// Nb of Scol functions or types
#define WebNavigatorFindScolFunctionsNb 1

/**
 *	Scol function names
 */
char* WebNavigatorFindScolFunctionsNames[WebNavigatorFindScolFunctionsNb] =
{
  "WebNavigatorCbOnFindResult"
};

/**
 *	Pointers to C functions that manipulate the VM for each scol function previously defined
 */
int (*WebNavigatorFindScolFunctionsPtr[WebNavigatorFindScolFunctionsNb])(mmachine m)=
{
  WebNavigatorCbOnFindResult
};

/**
 *	Nb of arguments of each scol function
 */
int WebNavigatorFindScolFunctionsNbArgs[WebNavigatorFindScolFunctionsNb]=
{
  3                                                                      // WebNavigatorCbOnFindResult
};

/**
 *	Prototypes of the scol functions
 */
char* WebNavigatorFindScolFunctionsType[WebNavigatorFindScolFunctionsNb]=
{
  "fun [ObjWebNavigator fun [ObjWebNavigator u0 I I [I I I I] I I] u1 u0] ObjWebNavigator" // WebNavigatorCbOnFindResult
};

int LoadWebNavigatorFind(mmachine m)
{
  return PKhardpak(m, "WebNavigatorFind", WebNavigatorFindScolFunctionsNb, WebNavigatorFindScolFunctionsNames, WebNavigatorFindScolFunctionsPtr, WebNavigatorFindScolFunctionsNbArgs, WebNavigatorFindScolFunctionsType);
}
