//网页表单神捕
//Author: lujjjh
import win.ui;
import mouse;
import winex;
import win.cur;
import win.imageList;
import inet.url;
import web.access;
import win.util.tray;
/*DSG{{*/
var winform = win.form(parent=...; bottom=401;text="网页表单神捕";border="resizable";right=460 )
winform.add( 
tab={ dr=1;dl=1;bottom=391;text="tab";left=8;dt=1;top=92;right=454;z=10;db=1;tabstop=1;edge=1;cls="tab" };
staticId={ dl=1;bgcolor=16777215;bottom=47;align="right";right=96;left=46;dt=1;top=27;z=5;color=0;text="Id:";border=1;cls="static" };
staticAction={ dl=1;bgcolor=16777215;bottom=66;align="right";right=96;left=46;dt=1;top=46;z=7;color=0;text="Action:";border=1;cls="static" };
editMethod={ dr=1;dl=1;bgcolor=16777215;color=0;right=453;readonly=1;dt=1;left=95;cls="edit";border=1;bottom=85;text="";top=65;tabstop=1;z=8 };
editName={ dr=1;dl=1;bgcolor=16777215;color=0;right=453;readonly=1;dt=1;left=95;cls="edit";border=1;bottom=28;text="";top=8;tabstop=1;z=2 };
editAction={ dr=1;dl=1;bgcolor=16777215;color=0;right=453;readonly=1;dt=1;left=95;cls="edit";border=1;bottom=66;text="";top=46;tabstop=1;z=6 };
staticName={ dl=1;bgcolor=16777215;bottom=28;align="right";right=96;left=46;dt=1;top=8;z=3;color=0;text="Name:";border=1;cls="static" };
picturebox={ dl=1;bgcolor=16777215;notify=1;right=40;left=8;dt=1;top=8;z=1;bottom=40;text="picturebox";cls="picturebox" };
staticMethod={ dl=1;bgcolor=16777215;bottom=85;align="right";right=96;left=46;dt=1;top=65;z=9;color=0;text="Method:";border=1;cls="static" };
editId={ dr=1;dl=1;bgcolor=16777215;color=0;right=453;readonly=1;dt=1;left=95;cls="edit";border=1;bottom=47;text="";top=27;tabstop=1;z=4 }
)
/*}}*/

/*CURDATA{{*/
var oCur = com.picture.load(string.load("/res/spy.cur"));
var hCursor = topointer(oCur.CopyHandle());
var gifCur = string.load("/res/spy.gif")
var gifBlank = string.load("/res/spyBlank.gif");
var imlData = string.load("/res/iml.gif");
/*}}*/

/*PREPARE{{*/
var pageElements = winform.tab.loadcode($"\.code\pageElements.aau");
var pageCode = winform.tab.loadcode($"\.code\pageCode.aau");
winform.tab.loadcode($"\.code\pageAbout.aau");

for (k,v in winform) {
	if (type(v) == type.table) {
		select (v.cls) {
			case "edit" {
				v.wndproc = function (hwnd, message, wParam, lParam) {
					select (message) {
						case 0x7/*_WM_SETFOCUS*/
							v.bgcolor = 15335423;
						case 0x8/*_WM_KILLFOCUS*/
							v.bgcolor = 16777215;
					}
				}
			}
		}
	}
}
winform.picturebox.image = gifCur;

// imagelist
var imlElements = win.imageList(16, 16);
imlElements.add(imlData, 0xff00ff);

// listview
::SendMessage(pageElements.lvElements.hwnd, 0x1003/*_LVM_SETIMAGELIST*/, topointer(0x02/*_LVSIL_STATE*/), imlElements.handle);

pageElements.lvElements.insertColumn("", 30, 1);
pageElements.lvElements.insertColumn("Name", 80, 2);
pageElements.lvElements.insertColumn("Id", 80, 3);
pageElements.lvElements.insertColumn("Value", 100, 4);
/*}}*/

 
CreatePen := Gdi32.api("CreatePen", "pointer(int nStyle, int nWidth, int nColor)");
Rectangle := Gdi32.api("Rectangle", "int(pointer hDC, int nLeft, int nTop, int nRight, int nBottom)");
SetROP2 := Gdi32.api("SetROP2", "int(pointer hDC, int nMode)");
var flashRect = function(rc){
		var hDC = ::GetWindowDC(0);
		var screenCx,screenCy = win.getScreen()

		rc.right =  math.min(rc.right,screenCx) ;
		rc.bottom =  math.min(rc.bottom,screenCy) 
		if (rc.left < 0)
			rc.left = 0;
		if (rc.top < 0)
			rc.top = 0; 
		
		var hPen = CreatePen(0x0/*_PS_SOLID*/, 3, 0);
		var hOldPen = ::SelectObject(hDC, hPen);
		var oldROP2 = SetROP2(hDC, 0xA/*_R2_NOTXORPEN*/);
		Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
		sleep(100) ;
		Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom);
		SetROP2(hDC, oldROP2);
		::SelectObject(hDC, hOldPen);
		::DeleteObject(hPen);
		::ReleaseDC(0, hDC);
}


//生成AAuto代码的头部
var getCodeHeader = function ( charset ) {
	var template = /*
import inet.http; 
io.open();
var formEncode = inet.url.encode; 

var http = inet.http();
*/
	if (string.lower(charset) == "utf-8")  {
template = /*
import inet.http; 
io.open();
var formEncode = function(v){
	return inet.url.encode( string.fromto(v, 0, 65001) )
}

var http = inet.http();
*/	 
	} 
	return template;
} 
//为每个表单控件生成AAuto代码
var getCodeFormField = function (name, value="") { 
	if (!name) 
		return "";
	else { 
		if(#value && inet.url.encode(value)!=value)
			return string.format("%s=""+formEncode(""%s"") + ""&",name, value ) 
		else
			return string.format( "%s=%s&",name, value )
	} 
} 

// 分析 Form 表映射法,这样快
var typeToImlIndex = {
	password = 2;
	file = 3;
	button = 4;
	submit = 5;
	reset = 6;
	image = 7;
	radio = 8;
	checkbox = 9;
	hidden = 11;
}
var spyWebBrowser;
var listElements = function () {
	::SendMessage(pageElements.lvElements.hwnd, 0x1009/*_LVM_DELETEALLITEMS*/);
	if (spyWebBrowser[["spyElement"]] ) {
		var ele = spyWebBrowser.spyElement ;
		while (ele) {
			if (string.lower(ele.tagName || "") == "form")
				break;
			ele = ele.parentNode;
		}
		if (!ele) { 
			win.util.tray.pop("网页对象不是一个表单","网页表单神捕")
			return;
		}
		
		var postData = "";
		
		// 此时 ele 是 spyWebBrowser.spyElement  对应的 form
		winform.editName.text = ele.name || "";
		winform.editId.text = ele.id || "";
		winform.editAction.text = ele.action || "";
		winform.editMethod.text = string.upper(ele.method || "GET");
		
		var index, obj, i;
		
		// inputs
		var inputs = ele.getElementsByTagName("input");
		for index, obj in com.each(inputs) {
			i = pageElements.lvElements.addItem("", , , typeToImlIndex[string.lower(obj.type)] : 0  );
			pageElements.lvElements.setItemText(obj.name || "", i, 2);
			pageElements.lvElements.setItemText(obj.id || "", i, 3);
			pageElements.lvElements.setItemText(obj.value || "", i, 4);
			if (string.lower(obj.type || "") == "radio" || string.lower(obj.type || "") == "checkbox")
				if (obj.checked == "")
					continue;
			postData += getCodeFormField(obj.name, obj.value  );
		}
		
		// textareas
		var textareas = ele.getElementsByTagName("textarea");
		for index, obj in com.each(textareas) {
			i = pageElements.lvElements.addItem("", , , 1);
			pageElements.lvElements.setItemText(obj.name || "", i, 2);
			pageElements.lvElements.setItemText(obj.id || "", i, 3);
			pageElements.lvElements.setItemText(obj.innerHTML || "", i, 4);
			postData += getCodeFormField(obj.name, obj.innerHTML );
		}
		
		// selects
		var selects = ele.getElementsByTagName("select");
		for index, obj in com.each(selects) {
			i = pageElements.lvElements.addItem("", , , 10);
			pageElements.lvElements.setItemText(obj.name || "", i, 2);
			pageElements.lvElements.setItemText(obj.id || "", i, 3);
			pageElements.lvElements.setItemText(obj.value || "", i, 4);
			postData += getCodeFormField(obj.name, obj.value );
		}
		
		var code = getCodeHeader(spyWebBrowser.document.charset) + 'var html = http.';
		code += winform.editMethod.text == "POST" ? "post" : "get";
		code += '("' + inet.url.joinpath(spyWebBrowser.document.url, winform.editAction.text);
		if (winform.editMethod.text == "POST")
			code += '", "' + postData + '");';
		else
			code += '?' + postData + '");'
		code += '\nio.print(html)\nexecute("pause");\nio.close();';
		pageCode.editCode.text = code;
	}
}

var timeId;
winform.picturebox.wndproc = function(hwnd,message,wParam,lParam){
	select (message) {
		case 0x201/*_WM_LBUTTONDOWN*/ {
			win.cur.setCur(hCursor);
			winform.picturebox.capture = true;
			winform.picturebox.image = gifBlank;
			spyWebBrowser = null, null;
			
			timeId = winform.addtimer(300, function (id) {
				var x, y = mouse.getPos();
				var hwndSpy = winex.fromPoint(x, y);
				if (!hwndSpy) return;
				if( win.getClass(hwndSpy) == "Chrome_RenderWidgetHostHWND"){
					win.util.tray.pop("谷歌浏览器不支持此程序","网页表单神捕")
				}
				if (win.getClass(hwndSpy) != "Internet Explorer_Server") 
					return;
				
				spyWebBrowser = web.access( hwndSpy ) 
				if (!spyWebBrowser) 
					return;
					
				var x_c, y_c = win.toClient(hwndSpy, x, y);
				spyWebBrowser.spyElement = spyWebBrowser.fromPoint(x_c, y_c);
				var e_x,e_y,e_cx,e_cy = spyWebBrowser.getPos( spyWebBrowser.spyElement ,true) 
				flashRect( ::RECT(e_x,e_y,e_x+e_cx+4,e_y+e_cy+4) ) 
			});
		}
		case 0x202/*_WM_LBUTTONUP*/ {
			winform.killtimer(timeId);
			winform.picturebox.capture = false;
			winform.picturebox.image = gifCur;
			
			listElements();
		}
	}
}

winform.show();
win.loopMessage();
oCur = null;
