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

//开启自动完成 
import win.util.autoComplete;
win.util.autoComplete(winform.editUrl,6/*_SHACF_URLALL*/)

winform.editUrl.wndproc = function(hwnd,message,wParam,lParam){ 
	if (  message == 0x101/*_WM_KEYUP*/ ) { 
		if ( wParam == 0xD/*_VK_RETURN*/ ) {
			addWebPage( winform.editUrl.text );
		}
	} 
}//endproc

 import web.form; 

//创建状态栏
import win.ui.statusbar;
var stat = win.ui.statusbar( winform );
 
//当网址发生改变
onUrlChange = function () { 
	if(winform.tab.form ){
		winform.editUrl.text = winform.tab.form.web.location; 
		winform.btnback.disabled = winform.tab.form.web.btnback
		winform.btnforward.disabled = winform.tab.form.web.btnforward
	}
	else {
		winform.editUrl.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窗体事件 
	var preTitle;
	wb.TitleChange = function(title){ 
		if(preTitle==title)
			return; 
			
		preTitle = title
		var str = string.mbcs( title )  
		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.aau.cn" );

var wb = addWebPage( ..io.fullpath("/") );
wb.document.currentViewMode = 1
import winex;
import win.ui.ctrl.listview;
listview = win.ui.ctrl.listview();
listview.hwnd =  winex.findEx(wb.hwndEmbedding, ,"SysListView32") 
var str = listview.getItemText(1) 


/*控制网页按钮部份{{*/ 
	winform.btnGo.oncommand = function(id,event){ 
		addWebPage( winform.editUrl.text ); 
	}//endproc
	winform.btnClose.oncommand = function(id,event){
		 if(!winform.tab.selIndex)
		 	return;
		 	
		 winform.tab.remove(winform.tab.selIndex)  
		 onUrlChange();
	}//endproc
	winform.btnrefresh3.oncommand = function(id,event){
		if(!winform.tab.selIndex)
		 	return;
		 	
		winform.tab.form.web.refresh3()
	}//endproc
	winform.btnrefresh.oncommand = function(id,event){
		if(!winform.tab.selIndex)
		 	return;
		 	
		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){
		if(!winform.tab.form)
			addWebPage( "about:blank");
		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){ 
			if(!winform.tab.selIndex)
				return ; 
			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(!winform.favmenu[dir]){
						tpath = io.splitpath(dir) 
						winform.favmenu[dir ] = win.ui.popmenu(winform);
						winform.favmenu[tpath.dir].add(tpath.file,winform.favmenu[ 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(20),
							function(id){ 
								if( winform.tab.form )
									winform.tab.form.web.go(tlist.URL) 
								else {
									addWebPage( tlist.URL )
								}
								
							}
						) 
					} 
				} 
		  	);
				
	var menu = winform.menu;
	if(!menu){
		menu = win.ui.menu(winform);//创建主菜单
		menu.add('收藏夹',winform.favmenu) 
		winform.popDrive = win.ui.popmenu(winform);
		import sys.volume;
		var tab = sys.volume.getLogicalDrives() 
		var openDrive = function(id){ 
			winform.tab.form.web.go( winform.popDrive.getString(id,0) );
		}  
		for(i=1;#tab;1){
			winform.popDrive.add(tab[i] + "\",openDrive)  
		}
		menu.add('本地文件',winform.popDrive) 
	}
	else {
		menu.remove( 1 );
		menu.insert(1,"收藏夹",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();
