/*
作者 lujjjh 
http://www.ecranesoft.com/bbs/showtopic-9084.aspx
*/ 
import win.ui;
import win.ui.menu
import win.ole;
import web.form;
/*DSG{{*/
var winform = win.form(parent=...; right=587;bottom=514;text="AAuto Explorer(可编程浏览器)";border="resizable";maxmize=1 )
winform.add( 
tab={ dr=1;dl=1;bottom=481;right=552;left=8;dt=1;top=51;font=LOGFONT( name="宋体";h=-12 );text="tab";z=1;db=1;cp=1;edge=1;cls="tab" };
btnClose={ dr=1;bottom=132;text="关闭";left=557;dt=1;top=71;z=10;right=582;cls="button" };
btnforward={ dr=1;bottom=275;text="前进";left=557;dt=1;top=214;z=6;right=582;cls="button" };
btnrefresh3={ dr=1;bottom=487;text="硬刷新";left=557;dt=1;top=426;z=9;right=582;cls="button" };
static={ dl=1;bottom=46;right=52;left=12;dt=1;top=30;font=LOGFONT( name="宋体";h=-12 );z=2;text="地址：";transparent=1;cls="static" };
edit_url={ dr=1;dl=1;bottom=46;right=432;left=52;dt=1;top=23;font=LOGFONT( name="宋体";h=-12 );z=3;text="";tabstop=1;edge=1;cls="edit" };
btnGo={ dr=1;bottom=50;text="Go";left=448;dt=1;top=24;z=4;right=531;cls="button" };
btnrefresh={ dr=1;bottom=420;text="刷新";left=557;dt=1;top=359;z=8;right=582;cls="button" };
btnback={ dr=1;bottom=351;text="后退";left=557;dt=1;top=290;z=7;right=582;cls="button" };
bthome={ dr=1;bottom=205;text="主页";left=557;dt=1;top=144;z=5;right=582;cls="button" }
)
/*}}*/

winform.edit_url.wndproc = function(hwnd,message,wparam,lparam){ 
	if (  message == 0x101/*_WM_KEYUP*/ ) { 
		if ( wparam == 0xD/*_VK_RETURN*/ ) {
			addWebPage( winform.edit_url.text );
		}
	} 
}//endproc
 
 
//创建状态栏
import win.ui.statusbar;
var stat = win.ui.statusbar( winform );
 
//当网址发生改变
onUrlChange = function () { 
	if(winform.tab.form ){
		winform.edit_url.text = winform.tab.form.web.location; 
		winform.btnback.disabled = winform.tab.form.web.btnback
		winform.btnforward.disabled = winform.tab.form.web.btnforward
	}
	else {
		winform.edit_url.text = "";
	}
	
}
 

import web.form.util;
//启用网页对选项卡的支持，例如鼠标中键打开等等 
web.form.util.enableTab();
 
import string.mbcs //中文增强函数库
addWebPage = function ( url ) {

	//创建web窗体
	var form = winform.tab.add( text = "Loading...";scroll=true ); 
	var wb = web.form( form , , 0x40000000/*_DLCTL_SILENT*/ | 0x10/*_DLCTL_DLIMAGES*/| 0x20/*_DLCTL_VIDEOS*/  );
 	 
	//获得索引位置
	var sel = #winform.tab.items ;
	winform.tab.selIndex = sel;
	
	//web窗体事件
	wb.TitleChange = function(text){
		var str = string.mbcs( text ) 
		winform.tab.setItemText(sel,str.limit(15/*_MAX_WEBTITLE*/)  ) 
	} 
	//新窗口事件
	wb.NewWindow3=function(ppDisp, Cancel,dwFlags,bstrUrlContext, bstrUrl ) {
		wb.NewWindow2 = null;
	 	/*弹出新窗口以前触发 */
		if(dwFlags & 0x10000/*_NWMF_FORCEWINDOW*/ )
			return;//如果用户选择在新窗口打开，不处理
			
		//否则,在新的选项卡打开
		return addWebPage( "about:blank" ).application;
	} 
	wb.NewWindow2 = function ( ppDisp, Cancel ) {  
		return addWebPage( "about:blank" ).application;
	}
	wb.NavigateComplete2 = function (pDisp, url) {
		onUrlChange();  
		//wb.refresh()
	} 
	wb.CommandStateChange=function(cmd,enable) {
	 	/*当命令的激活状态改变时触发。它表明何时激活或关闭Back和Forward菜单项或按钮 */ 
	 	select(cmd) {
	 	
	 		case 0x2/*_CSC_NAVIGATEBACK*/ {
	 			winform.btnback.disabled = !enable
	 			wb.btnback = !enable 
	 			
	 		}
	 		case 0x1/*_CSC_NAVIGATEFORWARD*/ {
	 			winform.btnforward.disabled = !enable
	 			wb.btnforward = !enable
	 		}  
	 	}
	 } 
	wb.StatusTextChange = function ( text ) {
		if ( winform.tab.form == wb._form )
			stat.setText( text );
	}
	wb.ProgressChange = function ( progress, progressMax ) {
		if ( progressMax && winform.tab.form == wb._form )
			stat.setText( "正在载入..." + (progress / progressMax * 100) + "%" );
	} 
	
	//打开网址
	wb.go( url );   
	
	return wb;
}

addWebPage( "http://www.ecranesoft.com" );
addWebPage( "http://www.baidu.com" );


/*控制网页按钮部份{{*/ 
	winform.btnGo.oncommand = function(id,event){ 
		addWebPage( winform.edit_url.text ); 
	}//endproc
	winform.btnClose.oncommand = function(id,event){
		 winform.tab.remove(winform.tab.selIndex)  
		 onUrlChange();
	}//endproc
	winform.btnrefresh3.oncommand = function(id,event){
		winform.tab.form.web.refresh3()
	}//endproc
	winform.btnrefresh.oncommand = function(id,event){
		winform.tab.form.web.refresh()
	}//endproc
	winform.btnback.oncommand = function(id,event){
		winform.tab.form.web.goback()
	}//endproc
	winform.btnforward.oncommand = function(id,event){
		winform.tab.form.web.goforward();	
	}//endproc
	winform.bthome.oncommand = function(id,event){
		winform.tab.form.web.gohome()
	}//endproc
/*}}*/


/*选项卡的右键的菜单{{*/ 
	//创建弹出菜单,当右键点击选项卡时,弹出关闭窗口的菜单
	winform.popmenu = win.ui.popmenu(winform);
	winform.popmenu.add(
		'刷新',
		function(id){
			winform.tab.form.web.refresh3()
		}
	);
	winform.popmenu.add(
		'关闭',
		function(id){ 
			winform.tab.remove( winform.tab.selIndex )  
			onUrlChange();
		}
	) 
	winform.popmenu.add();
	winform.popmenu.add(
		'全部关闭',
		function(id){ 
			winform.tab.removeAll();
			onUrlChange();
		}
	)
/*}}*/

import fsys;
import string.list;
import winex;
 
//加载收藏夹到菜单
reloadFavorites = function(){  
	var fav = fsys.getSpecial(0x0006 /*_CSIDL_FAVORITES*/ ) 
	fav = fsys.path.addBackslash(fav)
	winform.favmenu = win.ui.popmenu(winform);//创建弹出菜单
	winform.favmenu[ fav ] = winform.favmenu 
	winform.favmenu[ fav ].add( 
		"添加到收藏夹", 
		function(id){ 
			winex.enum( 
				function(hwnd,depth){
					::SendMessageInt(hwnd,0x111/*_WM_COMMAND*/,::MAKELONG(2261/*_IDM_ADDFAVORITES*/,1),hwnd )
				} 
				,winform.tab.form.web.hwndEmbedding 
			)
			reloadFavorites(); 
			
		}
	)
	fsys.enum(  fav, 
				"*.url", 
				function(dir,filename){ 
					dir = fsys.path.addBackslash(dir)
					
					if(filename){ 
						var tlist = string.list(dir+filename) 
						var tpath =  io.splitpath(filename)
						var lab = string.mbcs( tpath.name );
						winform.favmenu[dir].add(
							lab.limit(8),
							function(id){ 
								if( winform.tab.form )
									winform.tab.form.web.go(tlist.URL) 
								else {
									addWebPage( tlist.URL )
								}
								
							}
						) 
					}
					else{ 
						tpath = io.splitpath(dir) 
						winform.favmenu[dir ] = win.ui.popmenu(winform);
						winform.favmenu[tpath.dir].add(tpath.file,winform.favmenu[ dir ]  )
					}
				} 
		  	);
				
	var menu = win.ui.menu(winform);//创建主菜单
	menu.add('收藏夹',winform.favmenu) 
	menu.redraw()
}
reloadFavorites();

/*消息处理函数{{*/
	//当用户切换选项卡,触发此函数
	winform.tab.onnotify = function(id,code,ptr){
		if ( code == 0xFFFFFDD9/*_TCN_SELCHANGE*/ ) {
			onUrlChange();
		}
	}
	
 
	//tab控件消息回调函数
	winform.tab.wndproc = function(hwnd,message,wparam,lparam){
		if ( message == 0x207/*_WM_MBUTTONDOWN*/ or message = 0x203/*_WM_LBUTTONDBLCLK*/) { 
			//鼠标中键点击选项卡或双击,关闭窗口
			var i = winform.tab.fromPoint( ::LOWORD(lparam), ::HIWORD(lparam) ); 
			if ( i ) {
				winform.tab.remove(i)  
				onUrlChange();
			} 
		}
		else if (message = 0x204/*_WM_RBUTTONDOWN*/){
			//鼠标右键点击选项卡,弹出菜单
			var rc = ::RECT()
			rc.left,rc.top = ::LOWORD(lparam), ::HIWORD(lparam)
			::ClientToScreen(winform.tab.hwnd,rc);
			winform.popmenu.popup(rc.left,rc.top,true);//弹出菜单
		}
		
	}
/*}}*/

winform.show(0x3/*_SW_MAXIMIZE*/) 
win.loopMessage();
