<?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) 2006
   - 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 ***** -->

<!DOCTYPE bindings [
  <!ENTITY % structurebarDTD SYSTEM "chrome://bluegriffon/locale/structurebar.dtd" >
  %structurebarDTD;
]>

<bindings id="structurebarBindings"
          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="structurebar" extends="chrome://global/content/bindings/scrollbox.xml#arrowscrollbox">


    <implementation>

      <field name="mLastNode">null</field>
      <field name="mOneElementSelected">false</field>

      <method name="selectionChanged">
        <parameter name="aArguments"/>
        <parameter name="aNode"/>
        <parameter name="aOneElementSelected"/>
        <body>
        <![CDATA[
          this.mOneElementSelected = aOneElementSelected;

          var node = aNode;
          while (node && node.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul") // XUL
            node = node.parentNode;
          while (node && node.parentNode &&
                 node.namespaceURI == "http://www.w3.org/2000/svg" &&
                 node.parentNode.namespaceURI == "http://www.w3.org/2000/svg") // SVG container only
            node = node.parentNode;
          if (node != aNode) {
            EditorUtils.getCurrentEditor().selectElement(node);
            return;
          }

          var toolbarbutton;
          if (this.mLastNode == node)
          {
            // avoid rebuilding the whole structure toolbar if possible
            toolbarbutton = this.lastChild;
            while (node && toolbarbutton &&
                   node == toolbarbutton.getUserData("node") &&
                   node.nodeName.toLowerCase() != "html")
            {
              node = node.parentNode;
              toolbarbutton = toolbarbutton.previousSibling;
            }
            // so we rebuild only from toolbarbutton up to the root
            if (aOneElementSelected)
              toolbarbutton.setAttribute("checked", "true");
            else
              toolbarbutton.removeAttribute("checked");
          }
          else
          {
            // the deepest node is not the one the toolbar is currently showing,
            // so let's clear all
            this.mLastNode = node;
            toolbarbutton = this.lastChild;
          }

          while (toolbarbutton)
          {
            var tmp = toolbarbutton.previousSibling;
            if (toolbarbutton.localName == "toolbarbutton")
              this.removeChild(toolbarbutton);
            toolbarbutton = tmp;
          }

          var prefs = GetPrefs();
          var showIDs = true;
          var showClasses = true;
          var showLang = false;
          var showRole = true;
          try {
            showIDs = prefs.getBoolPref("bluegriffon.structurebar.id.show");
          } catch(e) {}
          try {
            showClasses = prefs.getBoolPref("bluegriffon.structurebar.class.show");
          } catch(e) {}
          try {
            showLang = prefs.getBoolPref("bluegriffon.structurebar.lang.show");
          } catch(e) {}
          try {
            showRole = prefs.getBoolPref("bluegriffon.structurebar.role.show");
          } catch(e) {}
          while (node.nodeName.toLowerCase() != "html")
          {
            var newLabel = document.createElement("toolbarbutton");

            var isBlueGriffonNS = (node.namespaceURI == "http://disruptive-innovations.com/zoo/bluegriffon");

            var text = (isBlueGriffonNS ? "" : "<")
                       + node.nodeName.toLowerCase();
            if (!isBlueGriffonNS && showIDs && node.id)
              text += " #" + node.id;
            if (!isBlueGriffonNS && showClasses && node.hasAttribute("class"))
              text += " ." + node.getAttribute("class").replace( / /g, ".");
            if (!isBlueGriffonNS && showLang && node.hasAttribute("lang"))
              text += " :lang(" + node.getAttribute("lang") + ")";
            if (!isBlueGriffonNS && showRole) {
              if (node.hasAttribute("role"))
                text += " :role(" + node.getAttribute("role") + ")";
              else {
                var lookForEpubType = Services.prefs.getBoolPref("bluegriffon.aria.epub-type") &&
                                      EditorUtils.isXHTMLDocument() &&
                                      node.hasAttributeNS("http://www.idpf.org/2007/ops", "type");
                if (lookForEpubType)
                  text += " :role(" + node.getAttributeNS("http://www.idpf.org/2007/ops", "type") + ")";
              }
            }
            text += (isBlueGriffonNS ? "" : ">")

            newLabel.setAttribute("label",     text);
            if (aOneElementSelected && aNode == node)
              newLabel.setAttribute("checked", "true");

            newLabel.setUserData("node", node, null);
            newLabel.setAttribute("value", node.nodeName.toLowerCase());
            newLabel.setAttribute("context", "structureBarContextMenu");
            newLabel.setAttribute("oncommand", "this.parentNode.selectNode(this)");

            this.insertBefore(newLabel, this.firstChild);

            node = node.parentNode;
          }

          // make sure the deepest element is visible;
          // we always have a lastChild here
          var lastButton = this.lastChild.previousSibling;
          if (lastButton)
            this.ensureElementIsVisible(lastButton);
        ]]>
        </body>
      </method>

      <method name="init">
        <body>
        <![CDATA[
          NotifierUtils.addNotifierCallback("selection_strict", this.selectionChanged, this);
        ]]>
        </body>
      </method>

      <method name="shutdown">
        <body>
        <![CDATA[
          NotifierUtils.removeNotifierCallback("selection_strict", this.selectionChanged, this);
        ]]>
        </body>
      </method>

      <method name="selectNode">
        <parameter name="aNode"/>
        <body>
        <![CDATA[
          var editor = EditorUtils.getCurrentEditor();
          var node   = aNode.getUserData("node");
          if (editor && node)
            editor.selectElement(node);
        ]]>
        </body>
      </method>

    </implementation>

    <handlers>
      <handler event="command">
        <![CDATA[
          var elt = event.target;
          if (elt.localName == "toolbarbutton")
            this.selectNode(elt);
        ]]>
      </handler>
    </handlers>

  </binding>

</bindings>
