;------------------------------------------------------------------------------
; http://www.autohotkey.com/forum/topic19225.html
; IE.ahk Standard Library
; by Sean
;------------------------------------------------------------------------------

IE_Init()
{
	COM_AtlAxWinInit()
}

IE_Term()
{
	COM_AtlAxWinTerm()
}

IE_Add(hWnd, x, y, w, h)
{
	Return	COM_AtlAxGetControl(COM_AtlAxCreateContainer(hWnd, x, y, w, h, "Shell.Explorer"))
}

IE_Move(pwb, l, t, w, h)
{
	WinMove, % "ahk_id " . COM_AtlAxGetContainer(pwb), , l, t, w, h
}

IE_LoadURL(pwb, u)
{
	pUrl := COM_SysAllocString(u)
	VarSetCapacity(var, 8 * 2, 0)
	DllCall(NumGet(NumGet(1*pwb)+44), "Uint", pwb, "Uint", pUrl, "Uint", &var, "Uint", &var, "Uint", &var, "Uint", &var)
	COM_SysFreeString(pUrl)
}

IE_LoadHTML(pwb, h)
{
	pUrl := COM_SysAllocString("about:" . h)
	VarSetCapacity(var, 8 * 2, 0)
	DllCall(NumGet(NumGet(1*pwb)+44), "Uint", pwb, "Uint", pUrl, "Uint", &var, "Uint", &var, "Uint", &var, "Uint", &var)
	COM_SysFreeString(pUrl)
}

IE_GoBack(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+28), "Uint", pwb)
}

IE_GoForward(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+32), "Uint", pwb)
}

IE_GoHome(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+36), "Uint", pwb)
}

IE_GoSearch(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+40), "Uint", pwb)
}

IE_Refresh(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+48), "Uint", pwb)
}

IE_Stop(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+56), "Uint", pwb)
}

IE_Document(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+72), "Uint", pwb, "UintP", pdoc)
	Return	pdoc
}

IE_GetTitle(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+116), "Uint", pwb, "UintP", pTitle)
	COM_Unicode2Ansi(pTitle, sTitle)
	COM_SysFreeString(pTitle)
	Return	sTitle
}

IE_GetUrl(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+120), "Uint", pwb, "UintP", pUrl)
	COM_Unicode2Ansi(pUrl, sUrl)
	COM_SysFreeString(pUrl)
	Return	sUrl
}

IE_Busy(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+124), "Uint", pwb, "shortP", bBusy)
	Return	-bBusy
}

IE_Quit(pwb)				; iexplore.exe only
{
	DllCall(NumGet(NumGet(1*pwb)+128), "Uint", pwb)
}

IE_hWnd(pwb)				; iexplore.exe only
{
	DllCall(NumGet(NumGet(1*pwb)+148), "Uint", pwb, "UintP", hIE)
	Return	hIE
}

IE_FullName(pwb)			; iexplore.exe only
{
	DllCall(NumGet(NumGet(1*pwb)+152), "Uint", pwb, "UintP", pFile)
	COM_Unicode2Ansi(pFile, sFile)
	COM_SysFreeString(pFile)
	Return	sFile
}

IE_GetStatusText(pwb)			; iexplore.exe only
{
	DllCall(NumGet(NumGet(1*pwb)+176), "Uint", pwb, "UintP", pText)
	COM_Unicode2Ansi(pText, sText)
	COM_SysFreeString(pText)
	Return	sText
}

IE_SetStatusText(pwb, sText = "")	; iexplore.exe only
{
	pText := COM_SysAllocString(sText)
	DllCall(NumGet(NumGet(1*pwb)+180), "Uint", pwb, "Uint", pText)
	COM_SysFreeString(pText)
}

IE_ReadyState(pwb)
{
/*
	READYSTATE_UNINITIALIZED = 0      ; Default initialization state.
	READYSTATE_LOADING       = 1      ; Object is currently loading its properties.
	READYSTATE_LOADED        = 2      ; Object has been initialized.
	READYSTATE_INTERACTIVE   = 3      ; Object is interactive, but not all of its data is available.
	READYSTATE_COMPLETE      = 4      ; Object has received all of its data.
*/
	DllCall(NumGet(NumGet(1*pwb)+224), "Uint", pwb, "intP", nReady)
	Return	nReady
}

IE_Open(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 1, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_New(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 2, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_Save(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 3, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_SaveAs(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 4, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_Print(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 6, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_PrintPreview(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 7, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_PageSetup(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 8, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_Properties(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 10, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_Cut(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 11, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_Copy(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 12, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_Paste(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 13, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_SelectAll(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 17, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_Find(pwb)
{
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 32, "Uint", 0, "Uint", 0, "Uint", 0)
}

IE_DoFontSize(pwb, s)
{
/*
	s = 4	; Largest
	s = 3	; Larger
	s = 2	; Medium
	s = 1	; Smaller
	s = 0	; Smallest
*/
	VarSetCapacity(var, 8 * 2, 0)
	NumPut(3, var, 0)
	NumPut(s, var, 8)
	DllCall(NumGet(NumGet(1*pwb)+216), "Uint", pwb, "Uint", 19, "Uint", 2, "Uint", &var, "Uint", &var)
}

IE_InternetOptions(pwb)
{
	CGID_MSHTML(pwb, 2135)
}

IE_ViewSource(pwb)
{
	CGID_MSHTML(pwb, 2139)
}

IE_AddToFavorites(pwb)
{
	CGID_MSHTML(pwb, 2261)
}

IE_MakeDesktopShortcut(pwb)
{
	CGID_MSHTML(pwb, 2266)
}

IE_SendEMail(pwb)
{
	CGID_MSHTML(pwb, 2288)
}

CGID_MSHTML(pwb, nCmd, nOpt = 0)
{
	pct := COM_QueryInterface(pwb, IID_IOleCommandTarget:="{B722BCCB-4E68-101B-A2BC-00AA00404770}")
	DllCall(NumGet(NumGet(1*pct)+16), "Uint", pct, "Uint", COM_GUID4String(CGID_MSHTML,"{DE4BA900-59CA-11CF-9592-444553540000}"), "Uint", nCmd, "Uint", nOpt, "Uint", 0, "Uint", 0)
	COM_Release(pct)
}
