' ########################################################################################
' Microsoft Windows
' File: mshtml.inc
' Copyright (c) 2011 Jos? Roca
' Portions Copyright (c) Microsoft Corporation.
' All Rights Reserved.
' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
' EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
' MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
' ########################################################################################
' ########################################################################################
' Library name: MSHTML
' Version: 4.0
' Documentation string: Microsoft HTML Object Library
' Path: C:\WINDOWS\system32\mshtml.tlb
' Library GUID: {3050F1C5-98B5-11CF-BB82-00AA00BDCE0B}
' ########################################################################################
#IF NOT %DEF(PBX_MSHTML_PBI)
#DEFINE PBX_MSHTML_PBI
%EQUATE `MSHTML_INC = 1
#INCLUDE ONCE "Legacy/System/Mshtmlc.inc"
#INCLUDE ONCE "Legacy/COM/OleAuto.inc"
' ========================================================================================
' ProgIDs (Program identifiers)
' ========================================================================================
' CLSID = {3050F819-98B5-11CF-BB82-00AA00BDCE0B}
$PROGID_HtmlDlgSafeHelperHtmlDlgSafeHelper1 = "HtmlDlgSafeHelper.HtmlDlgSafeHelper.1"
' CLSID = {25336920-03F9-11CF-8FD0-00AA00686F13}
$PROGID_HTMLDocument = "htmlfile"
' CLSID = {AE24FDAE-03C6-11D1-8B76-0080C744F389}
$PROGID_ScriptBridgeScriptBridge1 = "ScriptBridge.ScriptBridge.1"
' ========================================================================================
' Version independent ProgIDs
' ========================================================================================
' CLSID = {3050F819-98B5-11CF-BB82-00AA00BDCE0B}
$PROGID_HtmlDlgSafeHelperHtmlDlgSafeHelper = "HtmlDlgSafeHelper.HtmlDlgSafeHelper"
' CLSID = {AE24FDAE-03C6-11D1-8B76-0080C744F389}
$PROGID_ScriptBridgeScriptBridge = "ScriptBridge.ScriptBridge"
' ########################################################################################
' W R A P P E R F U N C T I O N S
' ########################################################################################
' ========================================================================================
' Wrapper function for IHTMLDocument2::write
' - Writes one or more HTML expressions to a document in the specified window.
' * Parameters:
' - pIHTMLDocument2 : Reference to the IHTMLDocument2 interface.
' - bstrScript : Text and HTML tags to write.
' OBJRESULT:
' - %S_OK if successful, or an error value otherwise.
' ========================================================================================
SUB IHTMLDocument2_WriteString ( BYVAL pIHTMLDocument2 AS IHTMLDocument2
 , BYVAL bstrScript AS WSTRING
 )
 LOCAL psarray AS DWORD
 LOCAL ix AS LONG
 LOCAL vScript AS VARIANT
 LOCAL rgsabound AS SAFEARRAYBOUND
 ' Create a safearray of variants of one element
 rgsabound.lLBound = 0 : rgsabound.cElements = 1
 psarray = SafeArrayCreate(%VT_VARIANT, 1, rgsabound)
 ' Fill the safearray with the script
 vScript = bstrScript : ix = 0
 SafeArrayPutElement(psarray, BYVAL VARPTR(ix), BYVAL VARPTR(vScript))
 ' Do the call
 pIHTMLDocument2.write(psarray)
 ' Destroy the safearray
 SafeArrayDestroy psarray
END SUB
' ========================================================================================
' ========================================================================================
' Wrapper function for IHTMLDocument2::writeln
' - Writes one or more HTML expressions, followed by a carriage return, to a document in
' the specified window.
' * Remarks:
' - In HTML, the carriage return is ignored unless it occurs within preformatted text.
' * Parameters:
' - pIHTMLDocument2 : Reference to the IHTMLDocument2 interface.
' - bstrScript : Text and HTML tags to write.
' OBJRESULT:
' - %S_OK if successful, or an error value otherwise.
' ========================================================================================
SUB IHTMLDocument2_WritelnString ( BYVAL pIHTMLDocument2 AS IHTMLDocument2
 , BYVAL bstrScript AS WSTRING
 )
 LOCAL psarray AS DWORD
 LOCAL ix AS LONG
 LOCAL vScript AS VARIANT
 LOCAL rgsabound AS SAFEARRAYBOUND
 ' Create a safearray of variants of one element
 rgsabound.lLBound = 0 : rgsabound.cElements = 1
 psarray = SafeArrayCreate(%VT_VARIANT, 1, rgsabound)
 ' Fill the safearray with the script
 vScript = bstrScript : ix = 0
 SafeArrayPutElement(psarray, BYVAL VARPTR(ix), BYVAL VARPTR(vScript))
 ' Do the call
 pIHTMLDocument2.writeln(psarray)
 ' Destroy the safearray
 SafeArrayDestroy psarray
END SUB
' ========================================================================================
' IHTMLDocument_getElementValueById
' - Retrieves the value of the specified attribute.
' Parameters:
' - pIHTMLDocument2 = Reference to the IHTMLDocument2 interface
' - bstrId = The value of the ID attribute
' Return Value:
' - An string containing the value as defined by the attribute.
' Remarks:
' This method performs a case insensitive property search.
' If two or more attributes have the same name (differing only in uppercase and lowercase
' letters) this function retrieves values only for the last attribute created with this
' name, and ignores all other attributes with the same name.
' When retrieving the CLASS attribute using this method, set the strAttributeName to be
' "className", which is the corresponding Dynamic HTML (DHTML) property.
' This function is used only by events created from HTML Components.
' OBJRESULT:
' - %S_OK if successful, or an error value otherwise.
' ========================================================================================
FUNCTION IHTMLDocument_getElementValueById ( BYVAL pIHTMLDocument2 AS IHTMLDocument2
 , BYVAL bstrId AS WSTRING
 ) AS WSTRING
 LOCAL pIHTMLDocument3 AS IHTMLDocument3 ' // Reference to the IHTMLDocument3 interface
 LOCAL pIHTMLElement AS IHTMLElement ' // Reference to the element
 LOCAL vValue AS VARIANT ' // Value
 ' // Get a reference to the IHTMLDocument3 interface
 pIHTMLDocument3 = pIHTMLDocument2
 IF ISFALSE ISOBJECT(pIHTMLDocument3) THEN EXIT FUNCTION
 ' // Get a reference to the input element
 pIHTMLElement = pIHTMLDocument3.getElementById(bstrId)
 IF ISFALSE ISOBJECT(pIHTMLElement) THEN EXIT FUNCTION
 ' // Get the value
 vValue = pIHTMLElement.getAttribute("value", 0)
 IF OBJRESULT THEN EXIT FUNCTION
 ' // Convert the value to string
 IF VARIANTVT(vValue) = %VT_BOOL THEN
 FUNCTION = LTRIM$(STR$(CINT(VARIANT#(vValue))))
 ELSEIF VARIANTVT(vValue) = %VT_BSTR THEN
 FUNCTION = VARIANT$$(vValue)
 ELSE
 FUNCTION = LTRIM$(STR$(VARIANT#(vValue)))
 END IF
END FUNCTION
' ========================================================================================
' ========================================================================================
' IHTMLDocument_setElementValueById
' - Sets the value of the specified attribute.
' Parameters:
' - pIHTMLDocument2 = Reference to the IHTMLDocument2 interface
' - bstrId = The value of the ID attribute
' - varValue = Variant that specifies the string, number, or Boolean to assign to the
' attribute.
' OBJRESULT:
' - %S_OK if successful, or an error value otherwise.
' ========================================================================================
SUB IHTMLDocument_setElementValueById ( BYVAL pIHTMLDocument2 AS IHTMLDocument2
 , BYVAL bstrId AS WSTRING
 , BYVAL vValue AS VARIANT
 )
 LOCAL pIHTMLDocument3 AS IHTMLDocument3 ' // Reference to the IHTMLDocument3 interface
 LOCAL pIHTMLElement AS IHTMLElement ' // Reference to the element
 ' // Get a reference to the IHTMLDocument3 interface
 pIHTMLDocument3 = pIHTMLDocument2
 IF ISFALSE ISOBJECT(pIHTMLDocument3) THEN EXIT SUB
 ' // Get a reference to the input element
 pIHTMLElement = pIHTMLDocument3.getElementById(bstrId)
 IF ISFALSE ISOBJECT(pIHTMLElement) THEN EXIT SUB
 ' // Set the value
 pIHTMLElement.setAttribute("value", vValue, 0)
END SUB
' ========================================================================================
' ========================================================================================
' IHTMLDocument_getElementInnerHtmlById
' - Retrieves the HTML between the start and end tags of the object.
' Parameters:
' - pIHTMLDocument2 = Reference to the IHTMLDocument2 interface
' - bstrId = The value of the ID attribute
' Return Value:
' - An string containing the HTML text.
' OBJRESULT:
' - %S_OK if successful, or an error value otherwise.
' ========================================================================================
FUNCTION IHTMLDocument_getElementInnerHtmlById ( BYVAL pIHTMLDocument2 AS IHTMLDocument2
 , BYVAL bstrId AS WSTRING
 ) AS WSTRING
 LOCAL pIHTMLDocument3 AS IHTMLDocument3 ' // Reference to the IHTMLDocument3 interface
 LOCAL pIHTMLElement AS IHTMLElement ' // Reference to the element
 LOCAL vValue AS VARIANT ' // Value
 ' // Get a reference to the IHTMLDocument3 interface
 pIHTMLDocument3 = pIHTMLDocument2
 IF ISFALSE ISOBJECT(pIHTMLDocument3) THEN EXIT FUNCTION
 ' // Get a reference to the input element
 pIHTMLElement = pIHTMLDocument3.getElementById(bstrId)
 IF ISFALSE ISOBJECT(pIHTMLElement) THEN EXIT FUNCTION
 ' // Get the inner html text
 FUNCTION = pIHTMLElement.innerHTML
END FUNCTION
' ========================================================================================
' ========================================================================================
' IHTMLDocument_setElementInnerHtmlById
' - Sets the HTML between the start and end tags of the object.
' Parameters:
' - pIHTMLDocument2 = Reference to the IHTMLDocument2 interface
' - bstrId = The value of the ID attribute
' Return Value:
' - An string containing the HTML text.
' OBJRESULT:
' - %S_OK if successful, or an error value otherwise.
' ========================================================================================
SUB IHTMLDocument_setElementInnerHtmlById ( BYVAL pIHTMLDocument2 AS IHTMLDocument2
 , BYVAL bstrId AS WSTRING
 , BYVAL bstrHtml AS WSTRING
 )
 LOCAL pIHTMLDocument3 AS IHTMLDocument3 ' // Reference to the IHTMLDocument3 interface
 LOCAL pIHTMLElement AS IHTMLElement ' // Reference to the element
 LOCAL vValue AS VARIANT ' // Value
 ' // Get a reference to the IHTMLDocument3 interface
 pIHTMLDocument3 = pIHTMLDocument2
 IF ISFALSE ISOBJECT(pIHTMLDocument3) THEN EXIT SUB
 ' // Get a reference to the input element
 pIHTMLElement = pIHTMLDocument3.getElementById(bstrId)
 IF ISFALSE ISOBJECT(pIHTMLElement) THEN EXIT SUB
 ' // Get the inner html text
 pIHTMLElement.innerHTML = bstrHtml
END SUB
' ========================================================================================
' ========================================================================================
' IHTMLDocument2_getActiveElementId
' - Retrieves the ID of the active element (the object that has the focus when the parent
' document has focus).
' * Parameters:
' - pIHTMLDocument2 = Reference to the IHTMLDocument2 interface.
' Return value:
' - ID of the active element.
' OBJRESULT:
' - %S_OK if successful, or an error value otherwise.
' ========================================================================================
FUNCTION IHTMLDocument2_getActiveElementId ( BYVAL pIHTMLDocument2 AS IHTMLDocument2
 ) AS WSTRING
 LOCAL pIHTMLElement AS IHTMLElement ' // Reference to the element
 pIHTMLElement = pIHTMLDocument2.activeElement
 IF ISFALSE ISOBJECT(pIHTMLElement) THEN EXIT FUNCTION
 FUNCTION = pIHTMLElement.id
END FUNCTION
' ========================================================================================
' ========================================================================================
' IHTMLDocument_setElementFocusById
' - Sets the focus in the specified element.
' Parameters:
' - pIHTMLDocument2 = Reference to the IHTMLDocument2 interface.
' - bstrId = Identifier of the element.
' OBJRESULT:
' - %S_OK if successful, or an error value otherwise.
' ========================================================================================
SUB IHTMLDocument_setElementFocusById ( BYVAL pIHTMLDocument2 AS IHTMLDocument2
 , BYVAL bstrId AS WSTRING
 )
 LOCAL pIHTMLDocument3 AS IHTMLDocument3 ' // Reference to the IHTMLDocument3 interface
 LOCAL pIHTMLElement AS IHTMLElement ' // Reference to the element
 LOCAL pIHTMLElement2 AS IHTMLElement2 ' // Reference to the element
 LOCAL vValue AS VARIANT ' // Value
 ' // Get a reference to the IHTMLDocument3 interface
 pIHTMLDocument3 = pIHTMLDocument2
 IF ISFALSE ISOBJECT(pIHTMLDocument3) THEN EXIT SUB
 ' // Get a reference to the input element
 pIHTMLElement = pIHTMLDocument3.getElementById(bstrId)
 IF ISFALSE ISOBJECT(pIHTMLElement) THEN EXIT SUB
 ' // Get a reference to the IHTMLElement2 interface
 pIHTMLElement2 = pIHTMLElement
 IF ISFALSE ISOBJECT(pIHTMLElement2) THEN EXIT SUB
 ' // Set the focus in the element
 pIHTMLElement2.focus
END SUB
' ========================================================================================
#ENDIF
