
/*
	"Hemi" JavaScript Framework
	Engine for Web Applications 3.1
	Composite file "hemi.comp.js" release: 3.1.1
	Copyright 2002 - 2010. All Rights Reserved.
	Author: Stephen W. Cote
	Email: sw.cote@gmail.com
	Project: http://www.whitefrost.com/Hemi/
	BSD License: http://www.whitefrost.com/Hemi.license.txt
*/
(function(){if(!D.TU(document.attachEvent)){if(!H.isImported("excanvas")){H.include("excanvas","3rdParty/");if(typeof G_vmlCanvasManager!="undefined")G_vmlCanvasManager.init_(document);}}H.include("hemi.event");H.include("hemi.transaction");H.namespace("graphics.canvas",H,{newInstance:
function(oContainer){var n=H.newObject("canvas","3.1.1");n.p={MouseTrackLeft:0,MouseTrackTop:0,MouseTrackDown:0,MouseOffsetX:0,MouseOffsetY:0,DefaultShapeRadius:20,DefaultShapeVerticalSpacing:15,DefaultShapeHorizontalSpacing:15,DefaultShapeGridUnit:(20*2)+(15*2),TransactionName:"canvas",PacketId:0,IERasterMode:0};n.o={container:oContainer,canvas:0,canvas_2d:0,temp_canvas:0,temp_canvas_2d:0,shapes:[],temp_shapes:[],shape_track_map:[],ShapeDecorators:[],CurrentShape:0,MouseDropShape:0};n.sigterm=function(){this.destroy();};n.destroy=function(){if(this.r!=5){this.r=5;this.Clear();var _p=this.o,_e=H.event.removeEventListener;_e(_p.temp_canvas,'mousedown',this._prehandle_canvas_mouse);_e(_p.temp_canvas,'mousemove',this._prehandle_canvas_mouse);_e(_p.temp_canvas,'mouseup',this._prehandle_canvas_mouse);_p.canvas.parentNode.removeChild(_p.canvas);_p.temp_canvas.parentNode.removeChild(_p.temp_canvas);}};n.getContainer=function(){return this.o.container;};n.Initialize=function(){var oC=this.getContainer(),oT,_t=H.transaction.service,oP,_p=this.p,_o=this.o,_e=H.event.addEventListener;if(oC.nodeName.toLowerCase()!="canvas"){oC=document.createElement("canvas");this.getContainer().appendChild(oC);}_o.canvas=oC;if(typeof oC.getContext=="undefined"){if(typeof G_vmlCanvasManager!="undefined"){_p.IERasterMode=1;G_vmlCanvasManager.initElement(oC);}if(typeof oC.getContext=="undefined"){this.serveTransaction("canvas_available",this);H.message.service.S("Browser does not support canvas","200.4");return;}}_o.canvas_2d=oC.getContext("2d");oT=document.createElement("canvas");oT.style.cssText="position:absolute;top:0px;left:0px;";oC.parentNode.appendChild(oT);if(typeof oT.getContext=="undefined"&&typeof G_vmlCanvasManager!="undefined"){G_vmlCanvasManager.initElement(oT);}_o.temp_canvas=oT;_o.temp_canvas_2d=oT.getContext("2d");oC.parentNode.style.position="relative";this.scopeHandler("canvas_mouse",0,0,1);_e(oT,'mousedown',this._prehandle_canvas_mouse);_e(oT,'mousemove',this._prehandle_canvas_mouse);_e(oT,'mouseup',this._prehandle_canvas_mouse);if(this.getContainer().getAttribute("height")){this.Resize(this.getContainer().getAttribute("width"),this.getContainer().getAttribute("height"));}_p.canvas_supported=1;this.joinTransactionPacket();this.serveTransaction("canvas_available",this);};n._handle_canvas_impl_available=function(ts,tp){tp.data.src.SetCanvasComponent(this);};n.AddShapeDecorator=function(o){this.o.ShapeDecorators.push(o);};n.Clear=function(){this.ClearTempCanvas();this.ClearCanvas();};n.ClearTempCanvas=function(){var _p=this.o;_p.temp_shapes=[];_p.temp_canvas_2d.clearRect(0,0,_p.canvas.clientWidth,_p.canvas.clientHeight);};n.ClearCanvas=function(){var _p=this.o;_p.shape_track_map=[];_p.canvas_2d.clearRect(0,0,_p.canvas.clientWidth,_p.canvas.clientHeight);_p.shapes=[];var aText=_p.canvas.parentNode.getElementsByTagName("span");for(var i=aText.length-1;i>=0;i--){_p.canvas.parentNode.removeChild(aText[i]);}};n.Draw=function(oShape,bStroke,bFix){var _p=this.o;if(typeof oShape!="object"||typeof oShape.type!="string")return;switch(oShape.type){case "RoundedRect":if(!oShape.rendered){_p.temp_shapes.push(oShape);oShape.rendered=1;}this.DrawRoundedRect(oShape);break;case "Ellipse":if(!oShape.rendered){_p.temp_shapes.push(oShape);oShape.rendered=1;}this.DrawEllipse(oShape);break;case "Arc":if(!oShape.rendered){_p.temp_shapes.push(oShape);oShape.rendered=1;}this.DrawArc(oShape.x,oShape.y,oShape.radius,oShape.startAngle,oShape.endAngle,bStroke,oShape.fillStyle,oShape.strokeStyle);break;case "Rect":if(!oShape.rendered){_p.temp_shapes.push(oShape);oShape.rendered=1;}this.DrawRect(oShape);break;case "Text":if(!oShape.rendered){_p.temp_shapes.push(oShape);oShape.rendered=1;}var bType=this.DrawText(oShape);if(bType==2)oShape.is_html=1;break;default:alert("Unknown shape:"+oShape.type);}};n.DrawRoundedRect=function(oShape){if(!oShape.type=="RoundedRect")return;var x1=oShape.x,x2=oShape.x+oShape.width,y1=oShape.y,y2=oShape.y+oShape.height,dx,dy,_p=this.o,tc2;tc2=_p.temp_canvas_2d;dx=Math.abs(x2-x1);dy=Math.abs(y2-y1);var dmin=(dx<dy)?dx:dy;var cornersize=(dmin/2>=15)?15:dmin/2;var xdir=(x2>x1)?cornersize:-1*cornersize;var ydir=(y2>y1)?cornersize:-1*cornersize;tc2.beginPath();tc2.fillStyle=oShape.fillStyle;tc2.strokeStyle=oShape.strokeStyle;tc2.moveTo(x1,y1+ydir);tc2.quadraticCurveTo(x1,y1,x1+xdir,y1);tc2.lineTo(x2-xdir,y1);tc2.quadraticCurveTo(x2,y1,x2,y1+ydir);tc2.lineTo(x2,y2-ydir);tc2.quadraticCurveTo(x2,y2,x2-xdir,y2);tc2.lineTo(x1+xdir,y2);tc2.quadraticCurveTo(x1,y2,x1,y2-ydir);tc2.fill();tc2.stroke();tc2.closePath();};n.DrawEllipse=function(oShape){if(!oShape.type=="Ellipse")return;var x1=oShape.x,x2=oShape.x2,y1=oShape.y,y2=oShape.y2,kappa=oShape.kappa,rx,ry,cx,cy,_p=this.o,tc2;tc2=_p.temp_canvas_2d;rx=(x2-x1)/2;ry=(y2-y1)/2;cx=x1+rx;cy=y1+ry;tc2.beginPath();tc2.fillStyle=oShape.fillStyle;tc2.strokeStyle=oShape.strokeStyle;tc2.moveTo(cx,cy-ry);tc2.bezierCurveTo(cx+(kappa*rx),cy-ry,cx+rx,cy-(kappa*ry),cx+rx,cy);tc2.bezierCurveTo(cx+rx,cy+(kappa*ry),cx+(kappa*rx),cy+ry,cx,cy+ry);tc2.bezierCurveTo(cx-(kappa*rx),cy+ry,cx-rx,cy+(kappa*ry),cx-rx,cy);tc2.bezierCurveTo(cx-rx,cy-(kappa*ry),cx-(kappa*rx),cy-ry,cx,cy-ry);tc2.fill();tc2.stroke();tc2.closePath();};n.DrawArc=function(x,y,radius,s,e,bStroke,fill_color,border_color){var _p=this.o,tc2;tc2=_p.temp_canvas_2d;if(!fill_color)fill_color=tc2.fillStyle;if(!border_color)border_color=fill_color;tc2.beginPath();tc2.fillStyle=fill_color;tc2.strokeStyle=border_color;tc2.arc(x,y,radius,s,e,false);tc2.fill();if(bStroke)tc2.stroke();tc2.closePath();};n.DrawRect=function(oShape){var _p=this.o,tc2,x=oShape.x,y=oShape.y,w=oShape.width,h=oShape.height,bStroke=0,fill_color=oShape.fillStyle,border_color=oShape.strokeStyle;tc2=_p[(oShape.rendered?"canvas_2d":"temp_canvas_2d")];if(!fill_color)fill_color=tc2.fillStyle;if(!border_color)border_color=fill_color;tc2.fillStyle=fill_color;tc2.strokeStyle=border_color;tc2[(bStroke?"strokeRect":"fillRect")](x,y,w,h);};n.DrawText=function(oShape){var sText=oShape.text,vX=oShape.x,vY=oShape.y,sColor=oShape.fillStyle,iH=this.o.canvas.clientHeight;if(!sColor)sColor="#000000";if(typeof this.o.canvas_2d.measureText!="undefined"){this.o.temp_canvas_2d.font="10pt Courier";this.o.temp_canvas_2d.fillStyle=sColor;if(vX=="center"){var iW=this.o.canvas.width;var iTW=this.o.canvas_2d.measureText(sText).width;if(iTW<iW)vX=(iW / 2)-(iTW / 2);else vX=0;}this.o.temp_canvas_2d.fillText(sText,vX,vY);return 1;}else{var oT=document.createElement("span");oT.appendChild(document.createTextNode(sText));oT.style.cssText="font:normal 10pt Courier;color:"+sColor+";position:absolute;top:"+vY+"px;left:"+vX+"px;";this.o.canvas.parentNode.appendChild(oT);return 2;}};n.ConnectShapes=function(oShape1,oShape2,sType){var _p=this.o,tc2;tc2=_p.temp_canvas_2d;if(!sType)sType="elbow";if(sType=="line"){tc2.beginPath();tc2.moveTo(oShape1.x,oShape1.y);tc2.lineTo(oShape2.x,oShape2.y);tc2.stroke();tc2.closePath();}else if(sType=="elbow"){tc2.beginPath();tc2.fillStyle="#000000";tc2.strokeStyle="#000000";var iMod=0;if(oShape1.type=="Rect")iMod=parseInt(oShape1.width/2);tc2.moveTo(oShape1.x+iMod,oShape1.y+oShape1.height);tc2.lineTo(oShape1.x+iMod,oShape1.y+oShape1.height+10);tc2.lineTo(oShape2.x+iMod,oShape1.y+oShape1.height+10);tc2.lineTo(oShape2.x+iMod,oShape2.y);tc2.stroke();tc2.closePath();}},n.Rasterize=function(){var _p=this.o;_p.canvas_2d.drawImage(_p.temp_canvas,0,0);this.ClearTempCanvas();};n.Ellipse=function(x,y,x2,y2,fill_color,border_color){var o=this.NewEllipse(x,y,x2,y2,fill_color,border_color);this.Draw(o);return o;};n.Circle=function(x,y,r,fill_color,border_color){var o=this.NewCircle(x,y,r,fill_color,border_color);this.Draw(o);return o;};n.RoundedRect=function(x,y,w,h,fill_color,border_color){var o=this.NewRoundedRect(x,y,w,h,fill_color,border_color);this.Draw(o);return o;};n.Rect=function(x,y,w,h,fill_color,border_color){var o=this.NewRect(x,y,w,h,fill_color,border_color);this.Draw(o);return o;};n.Text=function(sText,x,y,fill_color,stroke_color){var o=this.NewText(sText,x,y,fill_color,stroke_color);this.Draw(o);return o;};n.NewEllipse=function(x,y,x2,y2,sFill,sStroke){var iIndex=this.o.shapes.length;return this.o.shapes[iIndex]=this.Merge(this.NewShape(iIndex,"Ellipse",sFill,sStroke),{x:x,y:y,x2:x2,y2:y2,kappa:4*((Math.sqrt(2)-1)/ 3)});};n.NewCircle=function(x,y,r,sFill,sStroke){var iIndex=this.o.shapes.length;return this.o.shapes[iIndex]=this.Merge(this.NewShape(iIndex,"Arc",sFill,sStroke),{x:x,y:y,radius:r,startAngle:0,endAngle:Math.PI*2});};n.NewText=function(sText,x,y,sFill,sStroke){var iIndex=this.o.shapes.length;return this.o.shapes[iIndex]=this.Merge(this.NewShape(iIndex,"Text",sFill,sStroke),{x:x,y:y,text:sText});};n.NewRoundedRect=function(x,y,w,h,sFill,sStroke){var o=this.NewRect(x,y,w,h,sFill,sStroke);o.type="RoundedRect";return o;};n.NewRect=function(x,y,w,h,sFill,sStroke){var iIndex=this.o.shapes.length;return this.o.shapes[iIndex]=this.Merge(this.NewShape(iIndex,"Rect",sFill,sStroke),{x:x,y:y,height:h,width:w});};n.NewShape=function(iIndex,sType,sFill,sStroke){return{index:iIndex,type:sType,layerIndex:0,fillStyle:sFill,strokeStyle:sStroke,id:null,rendered:0,children:[],parent:0,is_html:0,reference_id:-1,selectable:1};};n.Merge=function(s,t){for(var i in s){if(typeof t[i]=="undefined")t[i]=s[i];}return t;};n.ShapeAt=function(x,y){var _p=this.o;if(typeof _p.shape_track_map[x]=="object"&&typeof _p.shape_track_map[x][y]=="number"){return _p.shapes[_p.shape_track_map[x][y]];}var oS=this.FindShapeAt(x,y);if(oS){if(typeof _p.shape_track_map[x]!="object")_p.shape_track_map[x]=[];_p.shape_track_map[x][y]=oS.index;}return oS;};n.FindShapeAt=function(x,y){var oShape,oMatch=0,_p=this.o;for(var i=0;i<_p.shapes.length;i++){if(!(oShape=_p.shapes[i])||!oShape.selectable)continue;switch(oShape.type){case "Arc":if(x>=oShape.x&&x<=(oShape.x+(oShape.radius*2))&&y>=oShape.y&&y<=(oShape.y+(oShape.radius*2)))oMatch=oShape;break;case "Rect":if(x>=oShape.x&&x<=(oShape.x+oShape.width)&&y>=oShape.y&&y<=(oShape.y+oShape.height))oMatch=oShape;break;default:break;}if(oMatch)break;}return oMatch;};n.handle_canvas_mousemove=function(e){this.o.MouseDropShape=this.ShapeAt(this.p.MouseTrackLeft,this.p.MouseTrackTop);this.dispatch_decorators(e);};n.handle_canvas_mouseup=function(e){var _s=this.p;this.dispatch_decorators(e);_s.MouseTrackDown=0;this.o.CurrentShape=0;this.o.MouseDropShape=0;_s.MouseTrackChoose=0;_s.MouseOffsetX=0;_s.MouseOffsetY=0;_s.MouseTrackLeft=0;_s.MouseTrackTop=0;};n.dispatch_decorators=function(e){var aD=this.o.ShapeDecorators;for(var i=0;i<aD.length;i++){if(typeof aD[i]["handle_canvas_"+e.type]=="function")aD[i]["handle_canvas_"+e.type](this,e);}};n.handle_canvas_mousedown=function(e){var _s=this.p;_s.MouseTrackDown=1;var oShape=this.ShapeAt(_s.MouseTrackLeft,_s.MouseTrackTop);if(oShape){this.o.CurrentShape=oShape;_s.MouseTrackChoose=1;_s.MouseOffsetX=_s.MouseTrackLeft-oShape.x;_s.MouseOffsetY=_s.MouseTrackTop-oShape.y;}this.dispatch_decorators(e);};n._handle_canvas_mouse=function(e){e=H.event.getEvent(e);var sHandler="handle_canvas_"+e.type;this.p.MouseTrackLeft=(typeof e.layerX=="number"?e.layerX:e.offsetX);this.p.MouseTrackTop=(typeof e.layerY=="number"?e.layerY:e.offsetY);if(typeof this[sHandler]=="function")this[sHandler](e);};n.Resize=function(x,y){var _p=this.o;_p.canvas.height=y;_p.canvas.width=x;_p.temp_canvas.height=y;_p.temp_canvas.width=x;};H.event.addScopeBuffer(n);H.registry.service.addObject(n);H.transaction.service.register(n,1);n.r=4;n.Initialize();return n;}});}());
/* End Hemi Framework 3.1 */
