// WebBrowserEdit.cpp: implementation of the CWebBrowserEdit class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HtmlEditCtrl2.h"
#include "AtlBase.H"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
/////////////////////////////////////////////////////////////////////////////
// CHtmlEditCtrl2
CHtmlEditCtrl2::CHtmlEditCtrl2()
{
m_hResult = E_FAIL;
m_pDispatch = NULL;
m_pHtmlDoc = NULL;
m_pCmdTarg = NULL;
}
CHtmlEditCtrl2::~CHtmlEditCtrl2()
{
if(m_pDispatch)
{
m_pDispatch->Release();
}
if(m_pHtmlDoc)
{
m_pHtmlDoc->Release();
}
if(m_pCmdTarg)
{
m_pCmdTarg->Release();
}
}
/*
IZ: Function to retive all tags that contains images (like <img src="http://www.server.com/image.jpg>") from HTML document
Was not present in orginal CHtmlEditCtrlBase
*/
HRESULT CHtmlEditCtrl2::GetURLsOfAllImages(CStringArray& a_arrImages)
{
if( PrepareInterfaces() == FALSE)
{
return E_FAIL;
}
IHTMLElementCollection* pImages = NULL;
m_hResult = m_pHtmlDoc->get_images(&pImages);
if( SUCCEEDED(m_hResult) && pImages )
{
long lNumberOfItems = 0;
m_hResult = pImages->get_length(&lNumberOfItems);
if( SUCCEEDED(m_hResult) )
{
IDispatch *ppvDisp = NULL;
IHTMLImgElement *ppvElement = NULL;
for (long ix=0; ix<lNumberOfItems; ix++)
{
CComVariant index = ix;
m_hResult = pImages->item( index, index, &ppvDisp );
if( SUCCEEDED(m_hResult) && ppvDisp )
{
m_hResult = ppvDisp->QueryInterface( IID_IHTMLImgElement , (void **) &ppvElement);
ppvDisp->Release();
if ( SUCCEEDED(m_hResult) && ppvElement )
{
BSTR bstrText = NULL;
CString sText;
m_hResult = ppvElement->get_src(&bstrText);
ppvElement->Release();
BSTR2CString(bstrText, sText);
::SysFreeString(bstrText);
a_arrImages.Add( sText );
}
}
}
}
pImages->Release();
}
return m_hResult;
}
/*
IZ: Was not present in orginal CHtmlEditCtrlBase
*/
HRESULT CHtmlEditCtrl2::ReplaceImageURL(const CString& a_sUrlToReplace, const CString& a_sUrlToReplaceWith)
{
if( PrepareInterfaces() == FALSE)
{
return E_FAIL;
}
IHTMLElementCollection* pImages = NULL;
m_hResult = m_pHtmlDoc->get_images(&pImages);
if( SUCCEEDED(m_hResult) && pImages)
{
long lNumberOfItems = 0;
m_hResult = pImages->get_length(&lNumberOfItems);
if( SUCCEEDED(m_hResult) )
{
IDispatch *ppvDisp = NULL;
IHTMLImgElement *ppvElement = NULL;
for (long ix=0; ix<lNumberOfItems; ix++)
{
CComVariant index = ix;
m_hResult = pImages->item( index, index, &ppvDisp );
if( SUCCEEDED(m_hResult) && ppvDisp )
{
m_hResult = ppvDisp->QueryInterface( IID_IHTMLImgElement , (void **) &ppvElement);
ppvDisp->Release();
if ( SUCCEEDED(m_hResult) && ppvElement )
{
BSTR bstrText = NULL;
CString sText;
m_hResult = ppvElement->get_src(&bstrText);
BSTR2CString(bstrText, sText);
SysFreeString(bstrText);
if(a_sUrlToReplace == sText)
{
bstrText = a_sUrlToReplaceWith.AllocSysString();
m_hResult = ppvElement->put_src(bstrText);
SysFreeString(bstrText);
}
ppvElement->Release();
}
}
}
}
pImages->Release();
}
return m_hResult;
}
HRESULT CHtmlEditCtrl2::GetBodyColorParameters(CString& a_sColor, BOOL a_bTextColor)
{
if( PrepareInterfaces() == FALSE)
{
return E_FAIL;
}
IHTMLElement* pElem = NULL;
m_hResult = m_pHtmlDoc->get_body(&pElem);
if( SUCCEEDED(m_hResult) && pElem )
{
IHTMLBodyElement* pBodyElem = NULL;
m_hResult = pElem->QueryInterface( IID_IHTMLBodyElement, (void**)&pBodyElem );
pElem->Release();
if( SUCCEEDED(m_hResult) && pBodyElem )
{
CComVariant vaRet;
if(a_bTextColor)
m_hResult = pBodyElem->get_text( &vaRet );
else
m_hResult = pBodyElem->get_bgColor( &vaRet );
pBodyElem->Release();
if(vaRet.vt & VT_BSTR)
{
BSTR2CString(vaRet.bstrVal, a_sColor);
}
else if(vaRet.vt & VT_I4)
{
ASSERT(0);//IZ TO CHECK
a_sColor.Format(_T("%d"), vaRet.lVal);
}
else
{
ASSERT(0);
}
}
}
return m_hResult;
}
HRESULT CHtmlEditCtrl2::GetBodyBackgroundColor(CString& a_sColor)
{
return GetBodyColorParameters(a_sColor, FALSE);
}
HRESULT CHtmlEditCtrl2::GetBodyTextColor(CString& a_sColor)
{
return GetBodyColorParameters(a_sColor, TRUE);
}
HRESULT CHtmlEditCtrl2::GetBodyBackgroundImage(CString& a_sImage)
{
if( PrepareInterfaces() == FALSE)
{
return E_FAIL;
}
IHTMLElement* pElem = NULL;
m_hResult = m_pHtmlDoc->get_body(&pElem);
if( SUCCEEDED(m_hResult) && pElem )
{
IHTMLBodyElement* pBodyElem = NULL;
m_hResult = pElem->QueryInterface( IID_IHTMLBodyElement, (void**)&pBodyElem );
pElem->Release();
if( SUCCEEDED(m_hResult) && pBodyElem )
{
BSTR bstrBody = NULL;
m_hResult = pBodyElem->get_background( &bstrBody );
pBodyElem->Release();
BSTR2CString(bstrBody, a_sImage);
SysFreeString(bstrBody);
}
}
return m_hResult;
}
HRESULT CHtmlEditCtrl2::GetBodyProperties(CString& a_sTag)
{
if( PrepareInterfaces() == FALSE)
{
return E_FAIL;
}
IHTMLElement* pElem = NULL;
m_hResult = m_pHtmlDoc->get_body(&pElem);
if( SUCCEEDED(m_hResult) && pElem )
{
IHTMLBodyElement* pBodyElem = NULL;
m_hResult = pElem->QueryInterface( IID_IHTMLBodyElement, (void**)&pBodyElem );
pElem->Release();
if( SUCCEEDED(m_hResult) && pBodyElem )
{
BSTR bstrBody = NULL;
m_hResult = pBodyElem->get_bgProperties( &bstrBody );
pBodyElem->Release();
BSTR2CString(bstrBody, a_sTag);
SysFreeString(bstrBody);
}
}
return m_hResult;
}
HRESULT CHtmlEditCtrl2::SetBodyBackgroundImage(const CString& a_sImage)
{
if( PrepareInterfaces() == FALSE)
{
return E_FAIL;
}
IHTMLElement* pElem = NULL;
m_hResult = m_pHtmlDoc->get_body(&pElem);
if( SUCCEEDED(m_hResult) && pElem )
{
IHTMLBodyElement* pBodyElem = NULL;
m_hResult = pElem->QueryInterface( IID_IHTMLBodyElement, (void**)&pBodyElem );
pElem->Release();
if( SUCCEEDED(m_hResult) && pBodyElem )
{
BSTR bstrBody = a_sImage.AllocSysString();
m_hResult = pBodyElem->put_background( bstrBody );
pBodyElem->Release();
::SysFreeString(bstrBody);
}
}
return m_hResult;
}
HRESULT CHtmlEditCtrl2::GetDocumentCharset(CString& a_sCharsetEncoding)
{
if( PrepareInterfaces() == FALSE)
{
return E_FAIL;
}
BSTR bstrCharset = NULL;
m_hResult = m_pHtmlDoc->get_charset(&bstrCharset);
BSTR2CString(bstrCharset, a_sCharsetEncoding);
SysFreeString(bstrCharset);
return m_hResult;
}
HRESULT CHtmlEditCtrl2::SetDocumentCharset(const CString& a_sCharsetEncoding)
{
if( PrepareInterfaces() == FALSE)
{
return E_FAIL;
}
BSTR bstrCharset = a_sCharsetEncoding.AllocSysString();
m_hResult = m_pHtmlDoc->put_charset(bstrCharset);
::SysFreeString(bstrCharset);
return m_hResult;
}
BOOL CHtmlEditCtrl2::PrepareInterfaces()
{
m_hResult = E_FAIL;
if(m_pDispatch == NULL)
{
m_pDispatch = CWebBrowser2::GetDocument();
if(m_pDispatch)
{
m_hResult = m_pDispatch->QueryInterface(IID_IHTMLDocument2, (void**)&m_pHtmlDoc);
m_hResult = m_pDispatch->QueryInterface(IID_IOleCommandTarget, (void**)&m_pCmdTarg);
}
}
return m_pDispatch && m_pHtmlDoc && m_pCmdTarg;
}
BOOL CHtmlEditCtrl2::SetDesignMode(BOOL bMode)
{
if( PrepareInterfaces() == FALSE)
{
return FALSE;
}
m_hResult = m_pHtmlDoc->put_designMode(bMode ? L"On" : L"Off");
return SUCCEEDED( m_hResult );
}
BOOL CHtmlEditCtrl2::IsDesignMode()
{
if(PrepareInterfaces() == FALSE)