<?xml version="1.0" encoding="UTF-8"?>

<!-- ***** BEGIN LICENSE BLOCK *****
   - Version: MPL 1.1/GPL 2.0/LGPL 2.1
   -
   - The contents of this file are subject to the Mozilla Public License Version
   - 1.1 (the "License"); you may not use this file except in compliance with
   - the License. You may obtain a copy of the License at
   - http://www.mozilla.org/MPL/
   - 
   - Software distributed under the License is distributed on an "AS IS" basis,
   - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
   - for the specific language governing rights and limitations under the
   - License.
   -
   - The Original Code is Status-4-Evar.
   -
   - The Initial Developer of the Original Code is 
   - Matthew Turnbull <sparky@bluefang-logic.com>.
   -
   - Portions created by the Initial Developer are Copyright (C) 2011
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -
   - Alternatively, the contents of this file may be used under the terms of
   - either the GNU General Public License Version 2 or later (the "GPL"), or
   - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
   - in which case the provisions of the GPL or the LGPL are applicable instead
   - of those above. If you wish to allow use of your version of this file only
   - under the terms of either the GPL or the LGPL, and not to allow others to
   - use your version of this file under the terms of the MPL, indicate your
   - decision by deleting the provisions above and replace them with the notice
   - and other provisions required by the GPL or the LGPL. If you do not delete
   - the provisions above, a recipient may use your version of this file under
   - the terms of any one of the MPL, the GPL or the LGPL.
   - 
   - ***** END LICENSE BLOCK ***** -->

<bindings id="status4evar-bindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
          xmlns:xbl="http://www.mozilla.org/xbl">

	<binding id="statuspanel" display="xul:hbox" extends="chrome://browser/content/tabbrowser.xml#statuspanel">
		<implementation>
			<!--                          -->
			<!-- Inverted mirror handling -->
			<!--                          -->

			<field name="_invertMirror"><![CDATA[
				false
			]]></field>

			<property name="invertMirror">
				<setter><![CDATA[
					this._invertMirror = val;
					this.mirror = this._isMirrored;
					return val;
				]]></setter>
				<getter><![CDATA[
					return this._invertMirror;
				]]></getter>
			</property>

			<!--                       -->
			<!-- Mouse mirror handling -->
			<!--                       -->

			<field name="_mouseMirror"><![CDATA[
				true
			]]></field>

			<field name="_mouseMirrorListen"><![CDATA[
				false
			]]></field>

			<property name="mouseMirror">
				<setter><![CDATA[
					this._mouseMirror = val;
					this.setupMouseMirror(this.value);
					return val;
				]]></setter>
				<getter><![CDATA[
					return this._mouseMirror;
				]]></getter>
			</property>

			<method name="setupMouseMirror">
				<parameter name="val"/>
				<body><![CDATA[
					if(val && this._mouseMirror)
					{
						this._calcMouseTargetRect();
						if(!this._mouseMirrorListen)
						{
							MousePosTracker.addListener(this);
							this._mouseMirrorListen = true;
						}
					}
					else
					{
						this.mirror = false;
						if(this._mouseMirrorListen)
						{
							MousePosTracker.removeListener(this);
							this._mouseMirrorListen = false;
						}
					}
				]]></body>
			</method>

			<method name="_calcMouseTargetRect">
				<body><![CDATA[
					let alignRight = false;
					let isRTL = (getComputedStyle(document.documentElement).direction == "rtl");
					if((this._invertMirror && !isRTL) || (!this._invertMirror && isRTL))
					{
						alignRight = true;
					}

					let rect = this.getBoundingClientRect();
					this._mouseTargetRect =
					{
						top:    rect.top,
						bottom: rect.bottom,
						left:   ((alignRight) ? window.innerWidth - rect.width : 0),
						right:  ((alignRight) ? window.innerWidth : rect.width)
					};
				]]></body>
			</method>

			<method name="onMouseEnter">
				<body><![CDATA[
					this.mirror = true;
				]]></body>
			</method>

			<method name="onMouseLeave">
				<body><![CDATA[
					this.mirror = false;
				]]></body>
			</method>

			<!--                 -->
			<!-- Mirror handling -->
			<!--                 -->

			<field name="_isMirrored"><![CDATA[
				false
			]]></field>

			<property name="mirror">
				<setter><![CDATA[
					this._isMirrored = val;
					if(this._invertMirror)
					{
						val = !val;
					}

					this.setBooleanAttr("mirror", val);
				]]></setter>
				<getter><![CDATA[
					return this._isMirrored;
				]]></getter>
			</property>

			<method name="_mirror">
				<body><![CDATA[
					this.mirror = !this._isMirrored;
				]]></body>
			</method>

			<!--                -->
			<!-- Value handling -->
			<!--                -->

			<property name="label">
				<setter><![CDATA[
					window.caligon.status4evar.statusService.setStatusText(val);
					return undefined;
				]]></setter>
				<getter><![CDATA[
					return window.caligon.status4evar.statusService.getStatusText();
				]]></getter>
			</property>

			<property name="value">
				<setter><![CDATA[
					this.setValue(val);
					this.setupMouseMirror(val);
					return val;
				]]></setter>
				<getter><![CDATA[
					return this.getAttribute("label");
				]]></getter>
			</property>

			<method name="setValue">
				<parameter name="val"/>
				<body><![CDATA[
					if(this.getAttribute("type") == "network" && this.getAttribute("previoustype") == "network")
					{
						this.style.minWidth = getComputedStyle(this).width;
					}
					else
					{
						this.style.minWidth = "";
					}

					this.setAttribute("label", val);
					this.setBooleanAttr("inactive", !val);
				]]></body>
			</method>

			<!--         -->
			<!-- Helpers -->
			<!--         -->

			<method name="setBooleanAttr">
				<parameter name="name"/>
				<parameter name="val"/>
				<body><![CDATA[
					if(val)
					{
						this.setAttribute(name, "true");
					}
					else
					{
						this.removeAttribute(name);
					}
				]]></body>
			</method>
		</implementation>
	</binding>

	<binding id="statuspanel-11" display="xul:hbox" extends="chrome://status4evar/content/tabbrowser.xml#statuspanel">
		<implementation>
			<field name="findMirror"><![CDATA[
				true
			]]></field>

			<property name="value">
				<setter><![CDATA[
					if(!val)
					{
						this.mirror = (this.findMirror && window.gFindBarInitialized && !window.gFindBar.hidden);
					}

					this.setValue(val);
					return val;
				]]></setter>
				<getter><![CDATA[
					return this.getAttribute("label");
				]]></getter>
			</property>

			<method name="setupMouseMirror">
				<parameter name="val"/>
				<body><![CDATA[
					this.setBooleanAttr("nomouse", !this._mouseMirror);
				]]></body>
			</method>

			<method name="handleEvent">
				<parameter name="event"/>
				<body><![CDATA[
					if(this.findMirror && event.type == "findbaropen" && this.value)
					{
						this.mirror = true;
					}
				]]></body>
			</method>
		</implementation>
	</binding>
</bindings>

