<?xml version="1.0"?>

<!-- ***** 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 BlueGriffon.
   -
   - The Initial Developer of the Original Code is
   - Disruptive Innovations SARL.
   - Portions created by the Initial Developer are Copyright (C) 2010
   - the Initial Developer. All Rights Reserved.
   -
   - Contributor(s):
   -   Daniel Glazman (daniel.glazman@disruptive-innovations.com), Original Author
   -
   - 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 LGPL or the GPL. 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="rotatorBindings"
          xmlns="http://www.mozilla.org/xbl"
          xmlns:html="http://www.w3.org/1999/xhtml"
          xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
          xmlns:xbl="http://www.mozilla.org/xbl">

  <binding id="rotator">
    <resources>
      <stylesheet src="rotator.css"/>
    </resources>

    <content>
      <xul:hbox anonid="outerRotator">
        <xul:hbox anonid="innerRotator" align="center">
          <xul:spacer flex="1"/>
          <xul:rotateGrippy/>
        </xul:hbox>
      </xul:hbox>
      <xul:textbox anonid="textbox" size="4" type="number" min="-1000" max="1000"
                   value="0"
                   onchange="this.parentNode.reflectPosition(this)"
                   oninput="this.parentNode.reflectPosition(this)"/>
      <xul:label value="degrees"/>
    </content>

    <implementation>
      <constructor>
        <![CDATA[
          if (this.getAttribute("value"))
            this.value = this.getAttribute("value");
        ]]>
      </constructor>

      <property name="value">
        <getter>
        <![CDATA[
          return this.getChild("textbox").value;
        ]]>
        </getter>
        <setter>
        <![CDATA[
          this.getChild("textbox").value = val;
          this.reflectPosition(this.getChild("textbox"));
        ]]>
        </setter>
      </property>

      <method name="getChild">
        <parameter name="aChildName"/>
        <body><![CDATA[
          return document.getAnonymousElementByAttribute(this, "anonid", aChildName);
        ]]></body>
      </method>
      <method name="reflectPosition">
        <parameter name="aElt"/>
        <body><![CDATA[
          var angle = -parseInt(aElt.value);
          this.getChild("innerRotator").style.MozTransform = "rotate(" + angle + "deg)";
          this.callCallback(angle);
        ]]></body>
      </method>

      <method name="callCallback">
        <parameter name="aAngle"/>
        <body><![CDATA[
          if (this.getAttribute("onchange")) {
            try {
              var fn = new Function("angle", this.getAttribute("onchange"));
              fn.call(window, aAngle);
            }
            catch(e) {}
          }
        ]]></body>
      </method>
    </implementation>
  </binding>

  <binding id="rotateGrippy">
    <resources>
      <stylesheet src="rotator.css"/>
    </resources>

    <implementation>
      <field name="mStartX">0</field>
      <field name="mStartY">0</field>
      <field name="mRotating">false</field>
      <field name="mAngle">0</field>
    </implementation>

    <handlers>
      <handler event="mousedown">
        <![CDATA[
          this.mStartX = event.screenX;
          this.mStartY = event.screenY;
          this.mRotating = true;
          this.setAttribute("rotating", "true");
          this.setCapture(true);
        ]]>
      </handler>

      <handler event="mousemove">
        <![CDATA[
          if (!this.mRotating)
            return;
          var x = event.screenX;
          var y = event.screenY;
          with (Math) {
            this.mAngle = floor(atan2(y - this.mStartY, x - this.mStartX) * 180 / PI);
          }
          this.parentNode.style.MozTransform = "rotate(" + this.mAngle + "deg)";
          this.parentNode.parentNode.parentNode.getChild("textbox").value = -this.mAngle;
          this.parentNode.parentNode.parentNode.callCallback(this.mAngle);
        ]]>
      </handler>

      <handler event="mouseup">
        <![CDATA[
          if (!this.mRotating)
            return;
          this.releaseCapture();
          this.removeAttribute("rotating");
          this.mRotating = false;
        ]]>
      </handler>
    </handlers>

  </binding>
  
</bindings>

