#include once "window9.bi"

Dim shared As IWebBrowser2Vtbl Ptr g_IWebBrowser

Function WebGadget(ByVal Gadget As long, _
ByVal x As Long, ByVal y As Long, _
byval w As Long, byval h As Long, _
byref URL As USTRING, _
ByVal Style As Integer, _
ByVal ExStyle As Integer) As Integer Ptr Export
	Dim i As Integer
	Dim pIWebBrowser As Integer Ptr
	Dim AtlAxWinInit As Function As Boolean
	Dim AtlAxGetControl As Function (ByVal hWin As HWND,ByRef pp As Integer ptr) As Integer
	Dim pIUnknown As Integer Ptr
	Dim IUnknown As IUnknownVtbl Ptr
	Var hDll=LoadLibrary("atl.dll")
	If hDll Then
		AtlAxWinInit=Cast(Any ptr,GetProcAddress(hDll,"AtlAxWinInit"))
		AtlAxGetControl=Cast(Any ptr,GetProcAddress(hDll,"AtlAxGetControl"))
		If AtlAxWinInit() Then
			#ifdef UNICODE
				var hWeb=CreateWindowEx(ExStyle,"AtlAxWin",*URL,WS_CHILD Or WS_VISIBLE Or Style, CInt(x),CInt(y),CInt(w),CInt(h), win9GetCurrent,Cast(HMENU,CInt(gadget)),0,0)
			#else	
				var hWeb=CreateWindowEx(ExStyle,"AtlAxWin",URL,WS_CHILD Or WS_VISIBLE Or Style, CInt(x),CInt(y),CInt(w),CInt(h), win9GetCurrent,Cast(HMENU,CInt(gadget)),0,0)
			#EndIf  
			AtlAxGetControl(hWeb,pIUnknown)
			IUnknown=Cast(IUnknownVtbl ptr,*pIUnknown)
			i=IUnknown->AddRef(Cast(IUnknown Ptr,pIUnknown))
			i=IUnknown->QueryInterface(Cast(IUnknown Ptr,pIUnknown),@IID_IWebBrowser2,@pIWebBrowser)
			g_IWebBrowser=Cast(IWebBrowser2Vtbl ptr,*pIWebBrowser)
			i=g_IWebBrowser->AddRef(Cast(IWebBrowser2 Ptr,pIWebBrowser))
			i=IUnknown->Release(Cast(IUnknown Ptr,pIUnknown))
			win9AddNewGadget(gadget,hWeb)
			Return pIWebBrowser
		End If
	End If
End Function

sub WebGadgetNavigate(ByVal pIWebBrowser As Integer Ptr, ByVal URL As WString Ptr)  Export
	If *URL<>"" Then
		g_IWebBrowser->Navigate(Cast(IWebBrowser2 Ptr,pIWebBrowser),Cast(BSTR,URL),NULL,NULL,NULL,NULL)
	EndIf
End sub

Sub WebGadgetGoForward(ByVal pIWebBrowser As Integer Ptr) Export
	g_IWebBrowser->GoForward(Cast(IWebBrowser2 Ptr,pIWebBrowser))
End Sub

Sub WebGadgetGoBack(ByVal pIWebBrowser As Integer Ptr) Export
	g_IWebBrowser->GoBack(Cast(IWebBrowser2 Ptr,pIWebBrowser))
End Sub

Sub WebGadgetRefresh(ByVal pIWebBrowser As Integer Ptr) Export
	g_IWebBrowser->Refresh(Cast(IWebBrowser2 Ptr,pIWebBrowser))
End Sub

Function WebGadgetGetURL(ByVal pIWebBrowser As Integer Ptr) As USTRING Export
	Dim As WString ptr buf
	g_IWebBrowser->get_LocationURL(Cast(IWebBrowser2 Ptr,pIWebBrowser),@buf)
	#ifdef UNICODE
		dim as USTRING sRet = *buf
		return sRet
	#else	
		Return *buf
	#EndIf 
End Function

Function WebGadgetState(ByVal pIWebBrowser As Integer Ptr) As Integer Export
	Dim state As Integer
	g_IWebBrowser->get_Busy(Cast(IWebBrowser2 Ptr,pIWebBrowser),Cast(VARIANT_BOOL Ptr,@state))
	Return state
End Function

Sub WebGadgetStop(ByVal pIWebBrowser As Integer Ptr) Export
	g_IWebBrowser->Stop(Cast(IWebBrowser2 Ptr,pIWebBrowser))
End Sub

Function WebGadgetGetBody(ByVal pIWebBrowser As Integer Ptr, ByVal flag As long) As USTRING Export
	dim text as wstring ptr
	Dim as IHTMLDocument2 ptr htmldoc2
	Dim As IDispatch ptr doc
	Dim as IHTMLElement ptr BODY
	g_IWebBrowser->get_Document(Cast(IWebBrowser2 Ptr,pIWebBrowser),@doc)
	if( doc->lpVtbl->QueryInterface( doc, @IID_IHTMLDocument2, cast( PVOID ptr, @htmldoc2 ) ) = S_OK ) Then
		htmlDoc2->lpVtbl->get_body(htmlDoc2, @BODY)
		If flag=0 Then
			BODY->lpVtbl->get_innerHTML(BODY, @TEXT)
		Else
			BODY->lpVtbl->get_outerHTML(BODY, @TEXT)
		EndIf
		#ifdef UNICODE
			dim as USTRING sRet = *text
			Function=sRet
		#else	
			Function=UTFTOASCII(text)
		#EndIf   
		BODY->lpVtbl->Release( BODY )
		doc->lpVtbl->Release( doc )
		htmlDoc2->lpVtbl->Release( htmlDoc2)
	EndIf
End Function

Sub WebGadgetSetBody(ByVal pIWebBrowser As Integer Ptr, ByVal text As USTRING) Export
	Dim as IHTMLDocument2 ptr htmldoc2
	Dim As IDispatch ptr doc
	Dim as IHTMLElement ptr BODY
	Dim UnicodeText As WString Ptr
	g_IWebBrowser->get_Document(Cast(IWebBrowser2 Ptr,pIWebBrowser),@doc)
	if( doc->lpVtbl->QueryInterface( doc, @IID_IHTMLDocument2, cast( PVOID ptr, @htmldoc2 ) ) = S_OK ) Then
		htmlDoc2->lpVtbl->get_body(htmlDoc2, @BODY)
		#ifdef UNICODE
			UnicodeText = cast(any ptr , *text)
			BODY->lpVtbl->put_innerHTML(BODY, UnicodeText)
			BODY->lpVtbl->Release( BODY )
			doc->lpVtbl->Release( doc )
			htmlDoc2->lpVtbl->Release( htmlDoc2)   	
		#else	
			UnicodeText = ASCIITOUTF(text)
			if UnicodeText then
				BODY->lpVtbl->put_innerHTML(BODY, UnicodeText)
				BODY->lpVtbl->Release( BODY )
				doc->lpVtbl->Release( doc )
				htmlDoc2->lpVtbl->Release( htmlDoc2)
				DeAllocate(UnicodeText)
			endif 	
		#EndIf
	EndIf
End Sub
