 ﻿#NoEnv
+#SingleInstance off
+#NoTrayIcon
 SendMode Input
 SetWorkingDir %A_ScriptDir%
 
-;// examples from https://autohotkey.com/boards/viewtopic.php?f=7&t=4588
-
-;activex gui - test  joedf - 2014/07/04
-#SingleInstance, off
-OnExit,OnExit
-
+;Some parts from Lexikos' Installer.ahk
+
+OnExit,GuiClose
+
+MYAPP_NAME:="Webapp.ahk"
 MYAPP_PROTOCOL:="myapp"
 
 HTML_page =

<html>
	<head>
		<style>
			body{font-family:sans-serif;background-color:#dde4ec;}
			#title{font-size:36px;}
			#corner{font-size:10px;position:absolute;top:8px;right:8px;}

		<div id="corner">Welcome!</div>
		<p>The standard Lorem Ipsum passage, used since the 1500s</p>
		<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
		<textarea rows="4" cols="70" id="MyTextBox">1234567890-=\ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$^&*()_+|~</textarea>
		<p id="footer">
			<a href="%MYAPP_PROTOCOL%://msgbox/hello">Click me for a MsgBox</a>&nbsp;-&nbsp;
			<a href="NOPE://msgbox/hello">Click me for nothing</a>&nbsp;-&nbsp;
			<a href="%MYAPP_PROTOCOL%://soundplay/ding">Click me for a ding sound!</a><br>
			<a href="#" onclick="AHK('RunButton4_function')">Link using onlick instead</a>
			<span style="display:block;height:14px;">&nbsp;</span>
			<input type="button" id="MyButton1" value="Show Content in AHK MsgBox">
			<input type="button" id="MyButton2" value="Change Content with AHK">
			<input type="button" id="MyButton3" value="Greetings from AHK">
			<input type="button" id="MyButton4" value="AHK MsgBox using function" onclick="AHK('RunButton4_function')">
		</p>
	</body>
</html>
)

Gui Margin, 0, 0
Gui +LastFound

OnMessage(0x100, "gui_KeyDown", 2)
Gui Add, ActiveX, vwb w640 h480 hwndhwb, Shell.Explorer
wb.silent := true ;Surpress JS Error boxes
Display(wb,HTML_page)
ComObjConnect(wb, wb_events)

DisableClickSounds()

w := wb.Document.parentWindow
w.AHK := Func("JS_AHK")

;Wait for IE to load the page, before we connect the event handlers
while wb.readystate != 4 or wb.busy
	sleep 10

;Use DOM access just like javascript!
MyButton1 := wb.document.getElementById("MyButton1")
MyButton2 := wb.document.getElementById("MyButton2")
MyButton3 := wb.document.getElementById("MyButton3")
ComObjConnect(MyButton1, "MyButton1_") ;connect button events
ComObjConnect(MyButton2, "MyButton2_")
ComObjConnect(MyButton3, "MyButton3_")

Gui Show, w640 h480, %MYAPP_NAME%
return


GuiEscape:
	MsgBox 0x34, Webapp.ahk, Are you sure you want to quit?
	IfMsgBox No
		return
GuiClose:
	Gui Destroy
	FileDelete,%A_Temp%\*.DELETEME.html ;clean tmp file
	ExitApp
return

class wb_events
{
	;for more events and other, see http://msdn.microsoft.com/en-us/library/aa752085
	
	NavigateComplete2(wb) { ;blocked all navigation, we want our own stuff happening
		wb.Stop()
	}
	DownloadComplete(wb, NewURL) {
		wb.Stop()
	}
	DocumentComplete(wb, NewURL) {
		wb.Stop()
	}
	
	BeforeNavigate2(wb, NewURL)
