<commit_before>#include "StdAfx.h"

#include "RhoSignature.h"
#include "common/RhodesApp.h"
#include "common/RhoTime.h"
#include "common/RhoFilePath.h"
#include "common/app_build_capabilities.h"

#if defined(_WIN32_WCE)&& !defined( OS_PLATFORM_MOTCE )
#include <msinkaut.h>
#include <msinkaut_i.c>
#endif

#if defined(_WIN32_WCE)
#include <imaging.h>
#endif

#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
extern void *rho_wmimpl_createSignatureInstance(HINSTANCE hInst, HWND hWnd, RECT rcWnd, int bgColor, int penColor, int penWidth);
extern void rho_wmimpl_deleteSignature(void *inkImpl);
extern LRESULT rho_wmimpl_signatureSigProcCall(void *inkImpl, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
extern void rho_wmimpl_signatureClearScreenCall(void *inkImpl);
#endif

extern "C" HWND getMainWnd();

namespace rho 
{
using namespace common;

CRhoSignatureWindow* CRhoSignature::m_pSigWindow = 0;
CRhoSignature::CRhoSignature(void)
{
}

CRhoSignature::~CRhoSignature(void)
{
}

/*static*/ void CRhoSignature::takeSignature(CParams* params)
{
    CModalParams& oSigParams = *((CModalParams*)params);
    CRhoSignatureWindow oSigWnd(params);
    int retVal = oSigWnd.DoModal(getMainWnd());
    oSigWnd.m_hWnd = 0;

    String strStatus = retVal != IDOK ? "" : ((oSigWnd.getLatestHResult() != S_OK && oSigWnd.getLatestHResult() != S_FALSE ? "Error" : ""));
    CFilePath oFilePath(oSigParams.getFilePath());

    RHODESAPP().callSignatureCallback( oSigParams.m_callback_url, oFilePath.getBaseName(), strStatus, retVal != IDOK );
}

/*static*/ void CRhoSignature::showSignature(CParams* params)
{
    CNonModalParams& oSigParams = *((CNonModalParams*)params);
    if (oSigParams.m_bVisible)
    {
        m_pSigWindow = new CRhoSignatureWindow(params);
        m_pSigWindow->Create(getMainWnd());
        m_pSigWindow->ShowWindow(SW_SHOW);

    }else
        hideSignature();
}

/*static*/ void CRhoSignature::saveSignature(CParams* params)
{
    CModalParams& oSigParams = *((CModalParams*)params);

    if (m_pSigWindow)
    {
        m_pSigWindow->saveImage();

        String strStatus = m_pSigWindow->getLatestHResult() != S_OK && m_pSigWindow->getLatestHResult() != S_FALSE ? "Error" : "";
        CFilePath oFilePath(m_pSigWindow->getParams().getFilePath());

        RHODESAPP().callSignatureCallback( oSigParams.m_callback_url, oFilePath.getBaseName(), strStatus, false );
    }else
        RHODESAPP().callSignatureCallback( oSigParams.m_callback_url, "", "Error", false );
}

/*static*/ void CRhoSignature::clearSignature(int )
{
    if (m_pSigWindow)
        m_pSigWindow->clearImage();
}

/*static*/ void CRhoSignature::hideSignature()
{
    if (m_pSigWindow)
    {
        m_pSigWindow->DestroyWindow();
        m_pSigWindow->m_hWnd = 0;
        delete m_pSigWindow;
        m_pSigWindow = 0;
    }
}

LRESULT CRhoSignatureWindow::OnDestroyDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER

    if (m_pInkImpl != NULL )
    {
        rho_wmimpl_deleteSignature(m_pInkImpl);
    }
#else

#if defined(_WIN32_WCE) && !defined( OS_PLATFORM_MOTCE )
	if (m_pInkImpl != NULL) 
    {
        ((IInkOverlay*)m_pInkImpl)->Release();
	}
#endif

#endif

    if ( m_hWndCommandBar )
        ::DestroyWindow(m_hWndCommandBar);

    m_hWndCommandBar = 0;

	m_pInkImpl = NULL;
	return FALSE;
}

LRESULT CRhoSignatureWindow::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
    if ( getParams().hasBorder() )
        SetWindowLong(GWL_STYLE,(long)WS_BORDER);

    CRect rcWnd = getParams().getWndRect();
    MoveWindow(rcWnd);

    if (getParams().m_eType == CRhoSignature::esModal)
    {

    #if defined(_WIN32_WCE) && !defined( OS_PLATFORM_MOTCE )
	    SetWindowText(_T("Take signature"));

	    SHINITDLGINFO shidi = { SHIDIM_FLAGS, m_hWnd, SHIDIF_SIZEDLGFULLSCREEN };
	    RHO_ASSERT(SHInitDialog(&shidi));

	    SHMENUBARINFO mbi = { sizeof(mbi), 0 };
	    mbi.hwndParent = m_hWnd;
	    mbi.nToolBarId = IDR_GETURL_MENUBAR;
	    mbi.hInstRes = _AtlBaseModule.GetResourceInstance();
	    RHO_ASSERT(SHCreateMenuBar(&mbi));
    #elif defined( OS_PLATFORM_MOTCE )
        m_hWndCommandBar = CommandBar_Create(_AtlBaseModule.GetResourceInstance(), m_hWnd, 1);
        TBBUTTON oBtn = {0};
        oBtn.iBitmap = -1;
        oBtn.idCommand = IDM_CLEAR;
        oBtn.fsState = TBSTATE_ENABLED;
        oBtn.iString = (int)L"Clear";

        CommandBar_InsertButton(m_hWndCommandBar, 0, &oBtn);
        CommandBar_AddAdornments(m_hWndCommandBar, CMDBAR_OK, 0 );

        CommandBar_Show(m_hWndCommandBar, TRUE);
    #endif

    }else
    {

    }

    #ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
        //CSignature* pRESig = new CSignature(_AtlBaseModule.GetResourceInstance(), getMainWnd(), 0, 0);
        //pRESig->RhoInitialise(m_hWnd, rcWnd, getParams().getBgColor(), getParams().getPenColor(), getParams().getPenWidth());
        m_pInkImpl = rho_wmimpl_createSignatureInstance(_AtlBaseModule.GetResourceInstance(), m_hWnd, (RECT)rcWnd, 
                getParams().getBgColor(), getParams().getPenColor(), getParams().getPenWidth());
    #elif defined(_WIN32_WCE) && !defined( OS_PLATFORM_MOTCE ) 
	    HRESULT hr = S_OK;
	    HRESULT co_init_result = CoInitializeEx(NULL, 0); //COINIT_APARTMENTTHREADED
	    if ( (co_init_result == S_OK) || (co_init_result == S_FALSE)  ) 
        {
            IInkOverlay* pInkOverlay = 0;
		    hr = ::CoCreateInstance(CLSID_InkOverlay,
								    NULL,
								    CLSCTX_INPROC_SERVER,
								    IID_IInkOverlay,
								    (void **)&pInkOverlay);
		    if (pInkOverlay != NULL) 
            {
                m_pInkImpl = pInkOverlay;

                CComPtr<IInkDrawingAttributes> spIInkDrawAttrs = NULL;
                hr = pInkOverlay->get_DefaultDrawingAttributes(&spIInkDrawAttrs);
                if (SUCCEEDED(hr))
                {
                    spIInkDrawAttrs->put_Color(getParams().getPenColor());      
                    //HIMETRIC = (PIXEL * 2540) / 96
                    spIInkDrawAttrs->put_Width( ((float)getParams().getPenWidth()*2540)/96 );

                    // Set the new drawing attributes
                    pInkOverlay->putref_DefaultDrawingAttributes(spIInkDrawAttrs);
                }

                // Attach the inkoverlay object to the window and enable it to start collecting ink
			    pInkOverlay->put_hWnd((long)m_hWnd);
			    hr = pInkOverlay->put_Enabled(VARIANT_TRUE);
		    }
		    else {
			    RAWLOG_ERROR("ERROR: Can not get Ink Overlay in Signature Capture !");
		    }
	    }
	    else {
		    RAWLOG_ERROR("ERROR: Can not Signature CoInitialize !");
	    }
    #endif //!APP_BUILD_CAPABILITY_WEBKIT_BROWSER

	return FALSE;
}

LRESULT CRhoSignatureWindow::OnClearCommand(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    clearImage();
    return FALSE;
}

LRESULT CRhoSignatureWindow::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    return rho_wmimpl_signatureSigProcCall(m_pInkImpl, m_hWnd, uMsg, wParam, lParam);
#else
    bHandled = FALSE;
#endif
    return TRUE;
}

LRESULT CRhoSignatureWindow::OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    return rho_wmimpl_signatureSigProcCall(m_pInkImpl, m_hWnd, uMsg, wParam, lParam);
#else
    bHandled = FALSE;
#endif
    return TRUE;
}

LRESULT CRhoSignatureWindow::OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    return rho_wmimpl_signatureSigProcCall(m_pInkImpl, m_hWnd, uMsg, wParam, lParam);
#else
    bHandled = FALSE;
#endif
    return TRUE;
}

LRESULT CRhoSignatureWindow::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    return rho_wmimpl_signatureSigProcCall(m_pInkImpl, m_hWnd, uMsg, wParam, lParam);
#else
    CPaintDC oPaintDC(m_hWnd);
    CRect rcClient;
    GetClientRect(rcClient);
    oPaintDC.FillSolidRect(rcClient, getParams().getBgColor());
    bHandled = TRUE;
#endif
    return TRUE;
}

void CRhoSignatureWindow::saveImage()
{
	HBITMAP hBitmap = getScreenBitmap();
    m_hResult = saveBitmapToFile( hBitmap, convertToStringW(getParams().getFilePath()).c_str(), convertToStringW(getParams().getFileFormat()).c_str() );
	DeleteObject(hBitmap);
}

void CRhoSignatureWindow::clearImage()
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
	if (m_pInkImpl != NULL) 
    {<commit_msg>wm: fix signature on ce5<commit_after>#include "StdAfx.h"

#include "RhoSignature.h"
#include "common/RhodesApp.h"
#include "common/RhoTime.h"
#include "common/RhoFilePath.h"
#include "common/app_build_capabilities.h"

#if defined(_WIN32_WCE)&& !defined( OS_PLATFORM_MOTCE )
#include <msinkaut.h>
#include <msinkaut_i.c>
#endif

#if defined(_WIN32_WCE)
#include <imaging.h>
#endif

#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
extern void *rho_wmimpl_createSignatureInstance(HINSTANCE hInst, HWND hWnd, RECT rcWnd, int bgColor, int penColor, int penWidth);
extern void rho_wmimpl_deleteSignature(void *inkImpl);
extern LRESULT rho_wmimpl_signatureSigProcCall(void *inkImpl, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
extern void rho_wmimpl_signatureClearScreenCall(void *inkImpl);
extern BOOL rho_wmimpl_saveSignature(void *inkImpl, LPCTSTR szFilePathName);
#endif

extern "C" HWND getMainWnd();

namespace rho 
{
using namespace common;

CRhoSignatureWindow* CRhoSignature::m_pSigWindow = 0;
CRhoSignature::CRhoSignature(void)
{
}

CRhoSignature::~CRhoSignature(void)
{
}

/*static*/ void CRhoSignature::takeSignature(CParams* params)
{
    CModalParams& oSigParams = *((CModalParams*)params);
    CRhoSignatureWindow oSigWnd(params);
    int retVal = oSigWnd.DoModal(getMainWnd());
    oSigWnd.m_hWnd = 0;

    String strStatus = retVal != IDOK ? "" : ((oSigWnd.getLatestHResult() != S_OK && oSigWnd.getLatestHResult() != S_FALSE ? "Error" : ""));
    CFilePath oFilePath(oSigParams.getFilePath());

    RHODESAPP().callSignatureCallback( oSigParams.m_callback_url, oFilePath.getBaseName(), strStatus, retVal != IDOK );
}

/*static*/ void CRhoSignature::showSignature(CParams* params)
{
    CNonModalParams& oSigParams = *((CNonModalParams*)params);
    if (oSigParams.m_bVisible)
    {
        m_pSigWindow = new CRhoSignatureWindow(params);
        m_pSigWindow->Create(getMainWnd());
        m_pSigWindow->ShowWindow(SW_SHOW);

    }else
        hideSignature();
}

/*static*/ void CRhoSignature::saveSignature(CParams* params)
{
    CModalParams& oSigParams = *((CModalParams*)params);

    if (m_pSigWindow)
    {
        m_pSigWindow->saveImage();

        String strStatus = m_pSigWindow->getLatestHResult() != S_OK && m_pSigWindow->getLatestHResult() != S_FALSE ? "Error" : "";
        CFilePath oFilePath(m_pSigWindow->getParams().getFilePath());

        RHODESAPP().callSignatureCallback( oSigParams.m_callback_url, oFilePath.getBaseName(), strStatus, false );
    }else
        RHODESAPP().callSignatureCallback( oSigParams.m_callback_url, "", "Error", false );
}

/*static*/ void CRhoSignature::clearSignature(int )
{
    if (m_pSigWindow)
        m_pSigWindow->clearImage();
}

/*static*/ void CRhoSignature::hideSignature()
{
    if (m_pSigWindow)
    {
        m_pSigWindow->DestroyWindow();
        m_pSigWindow->m_hWnd = 0;
        delete m_pSigWindow;
        m_pSigWindow = 0;
    }
}

LRESULT CRhoSignatureWindow::OnDestroyDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER

    if (m_pInkImpl != NULL )
    {
        rho_wmimpl_deleteSignature(m_pInkImpl);
    }
#else

#if defined(_WIN32_WCE) && !defined( OS_PLATFORM_MOTCE )
	if (m_pInkImpl != NULL) 
    {
        ((IInkOverlay*)m_pInkImpl)->Release();
	}
#endif

#endif

    if ( m_hWndCommandBar )
        ::DestroyWindow(m_hWndCommandBar);

    m_hWndCommandBar = 0;

	m_pInkImpl = NULL;
	return FALSE;
}

LRESULT CRhoSignatureWindow::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
    if ( getParams().hasBorder() )
        SetWindowLong(GWL_STYLE,(long)WS_BORDER);

    CRect rcWnd = getParams().getWndRect();
    MoveWindow(rcWnd);

    if (getParams().m_eType == CRhoSignature::esModal)
    {

    #if defined(_WIN32_WCE) && !defined( OS_PLATFORM_MOTCE )
	    SetWindowText(_T("Take signature"));

	    SHINITDLGINFO shidi = { SHIDIM_FLAGS, m_hWnd, SHIDIF_SIZEDLGFULLSCREEN };
	    RHO_ASSERT(SHInitDialog(&shidi));

	    SHMENUBARINFO mbi = { sizeof(mbi), 0 };
	    mbi.hwndParent = m_hWnd;
	    mbi.nToolBarId = IDR_GETURL_MENUBAR;
	    mbi.hInstRes = _AtlBaseModule.GetResourceInstance();
	    RHO_ASSERT(SHCreateMenuBar(&mbi));
    #elif defined( OS_PLATFORM_MOTCE )
        m_hWndCommandBar = CommandBar_Create(_AtlBaseModule.GetResourceInstance(), m_hWnd, 1);
        TBBUTTON oBtn = {0};
        oBtn.iBitmap = -1;
        oBtn.idCommand = IDM_CLEAR;
        oBtn.fsState = TBSTATE_ENABLED;
        oBtn.iString = (int)L"Clear";

        CommandBar_InsertButton(m_hWndCommandBar, 0, &oBtn);
        CommandBar_AddAdornments(m_hWndCommandBar, CMDBAR_OK, 0 );

        CommandBar_Show(m_hWndCommandBar, TRUE);
    #endif

    }else
    {

    }

    #ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
        //CSignature* pRESig = new CSignature(_AtlBaseModule.GetResourceInstance(), getMainWnd(), 0, 0);
        //pRESig->RhoInitialise(m_hWnd, rcWnd, getParams().getBgColor(), getParams().getPenColor(), getParams().getPenWidth());
        m_pInkImpl = rho_wmimpl_createSignatureInstance(_AtlBaseModule.GetResourceInstance(), m_hWnd, (RECT)rcWnd, 
                getParams().getBgColor(), getParams().getPenColor(), getParams().getPenWidth());
    #elif defined(_WIN32_WCE) && !defined( OS_PLATFORM_MOTCE ) 
	    HRESULT hr = S_OK;
	    HRESULT co_init_result = CoInitializeEx(NULL, 0); //COINIT_APARTMENTTHREADED
	    if ( (co_init_result == S_OK) || (co_init_result == S_FALSE)  ) 
        {
            IInkOverlay* pInkOverlay = 0;
		    hr = ::CoCreateInstance(CLSID_InkOverlay,
								    NULL,
								    CLSCTX_INPROC_SERVER,
								    IID_IInkOverlay,
								    (void **)&pInkOverlay);
		    if (pInkOverlay != NULL) 
            {
                m_pInkImpl = pInkOverlay;

                CComPtr<IInkDrawingAttributes> spIInkDrawAttrs = NULL;
                hr = pInkOverlay->get_DefaultDrawingAttributes(&spIInkDrawAttrs);
                if (SUCCEEDED(hr))
                {
                    spIInkDrawAttrs->put_Color(getParams().getPenColor());      
                    //HIMETRIC = (PIXEL * 2540) / 96
                    spIInkDrawAttrs->put_Width( ((float)getParams().getPenWidth()*2540)/96 );

                    // Set the new drawing attributes
                    pInkOverlay->putref_DefaultDrawingAttributes(spIInkDrawAttrs);
                }

                // Attach the inkoverlay object to the window and enable it to start collecting ink
			    pInkOverlay->put_hWnd((long)m_hWnd);
			    hr = pInkOverlay->put_Enabled(VARIANT_TRUE);
		    }
		    else {
			    RAWLOG_ERROR("ERROR: Can not get Ink Overlay in Signature Capture !");
		    }
	    }
	    else {
		    RAWLOG_ERROR("ERROR: Can not Signature CoInitialize !");
	    }
    #endif //!APP_BUILD_CAPABILITY_WEBKIT_BROWSER

	return FALSE;
}

LRESULT CRhoSignatureWindow::OnClearCommand(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
    clearImage();
    return FALSE;
}

LRESULT CRhoSignatureWindow::OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    return rho_wmimpl_signatureSigProcCall(m_pInkImpl, m_hWnd, uMsg, wParam, lParam);
#else
    bHandled = FALSE;
#endif
    return TRUE;
}

LRESULT CRhoSignatureWindow::OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    return rho_wmimpl_signatureSigProcCall(m_pInkImpl, m_hWnd, uMsg, wParam, lParam);
#else
    bHandled = FALSE;
#endif
    return TRUE;
}

LRESULT CRhoSignatureWindow::OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    return rho_wmimpl_signatureSigProcCall(m_pInkImpl, m_hWnd, uMsg, wParam, lParam);
#else
    bHandled = FALSE;
#endif
    return TRUE;
}

LRESULT CRhoSignatureWindow::OnPaint(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    return rho_wmimpl_signatureSigProcCall(m_pInkImpl, m_hWnd, uMsg, wParam, lParam);
#else
    CPaintDC oPaintDC(m_hWnd);
    CRect rcClient;
    GetClientRect(rcClient);
    oPaintDC.FillSolidRect(rcClient, getParams().getBgColor());
    bHandled = TRUE;
#endif
    return TRUE;
}

void CRhoSignatureWindow::saveImage()
{
    StringW strFilePathW = convertToStringW( getParams().getFilePath() );

#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
    BOOL bRes = rho_wmimpl_saveSignature( m_pInkImpl, strFilePathW.c_str());
    m_hResult = bRes ? S_OK : E_FAIL;
#else
	HBITMAP hBitmap = getScreenBitmap();
    m_hResult = saveBitmapToFile( hBitmap, strFilePathW.c_str(), convertToStringW(getParams().getFileFormat()).c_str() );
	DeleteObject(hBitmap);
#endif
}

void CRhoSignatureWindow::clearImage()
{
#ifdef APP_BUILD_CAPABILITY_WEBKIT_BROWSER
	if (m_pInkImpl != NULL) 
    {
{'fraction_non_alphanumeric': 0.11711711711711711, 'fraction_numerical': 0.00430366672404889, 'mean_word_length': 3.860011154489682, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4884491', 'n_tokens_mistral': 6552, 'n_tokens_neox': 6181, 'n_words': 1057}