<?xml version="1.0"?>

# -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
# 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 the Rich ScrollView.
# 
# The Initial Developer of the Original Code is Ben Goodger. 
# Portions created by the Initial Developer are Copyright (C) 2003-2004
# the Initial Developer. All Rights Reserved.
# 
# Contributor(s):
#   Ben Goodger <ben@bengoodger.com>
# 
# 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="richViewBindings"
          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="richview-item">
    <implementation>
      <method name="fireEvent">
        <parameter name="aEventType"/>
        <body>
        <![CDATA[
          var e = document.createEvent("Events");
          e.initEvent(this.eventPrefix + aEventType, false, true);
          
          this.dispatchEvent(e);
        ]]>
        </body>
      </method>
      
      <property name="disabled" onget="return this.getAttribute('disabled') == 'true'"
                                onset="this.setAttribute('disabled', val); return val;"/>
    </implementation>  
  </binding>

  <binding id="richview">
    <implementation>
      <method name="fireEvent">
        <parameter name="aEventType"/>
        <body>
        <![CDATA[
          var e = document.createEvent("Events");
          var eventType = "richview-" + aEventType;
          e.initEvent(eventType, false, true);
          this.dispatchEvent(e);
          
          var handler = this.getAttribute("onrichview-" + aEventType);
          if (handler != "") {
            var fn = new Function("event", handler);
            fn(e);
          }
          document.commandDispatcher.updateCommands(eventType);
        ]]>
        </body>
      </method>

      <field name="_selected">null</field>
      <property name="selected">
        <setter>
        <![CDATA[
          if (this._selected)
            this._selected.removeAttribute("selected");
          this._selected = val;
          if (this._selected)
            this._selected.setAttribute("selected", "true");
          
          this.fireEvent("select");
        ]]>
        </setter>
        <getter>
        <![CDATA[
          return this._selected;
        ]]>
        </getter>
      </property>
      
      <method name="selectForwardInternal">
        <parameter name="aElement"/>
        <body>
        <![CDATA[
          for (var temp = aElement; temp; temp = temp.nextSibling) {
            if ("fireEvent" in temp) {
              this.selected = temp;
              break;
            }
          }
        ]]>
        </body>
      </method>
      
      <method name="selectBackwardInternal">
        <parameter name="aElement"/>
        <body>
        <![CDATA[
          for (var temp = aElement; temp; temp = temp.previousSibling) {
            if ("fireEvent" in temp) {
              this.selected = temp;
              break;
            }
          }
        ]]>
        </body>
      </method>
      
      <method name="selectionForward">
        <parameter name="aEvent"/>
        <body>
        <![CDATA[
          if (this.selected)
            this.selectForwardInternal(this.selected.nextSibling);
          else {
            if (this.hasChildNodes())
              this.selectForwardInternal(this.firstChild);
          }
        ]]>
        </body>
      </method>

      <method name="selectionBackward">
        <parameter name="aEvent"/>
        <body>
        <![CDATA[
          if (this.selected)
            this.selectBackwardInternal(this.selected.previousSibling);
          else {
            if (this.hasChildNodes())
              this.selectBackwardInternal(this.lastChild);
          }
        ]]>
        </body>
      </method>
      
      <property name="children">
        <getter>
        <![CDATA[
          var childNodes = [];
          for (var i = 0; i < this.childNodes.length; ++i) {
            if ("fireEvent" in this.childNodes[i])
              childNodes.push(this.childNodes[i]);
          }
          return childNodes;
        ]]>
        </getter>
      </property>
    </implementation>
    <handlers>
      <handler event="click">
      <![CDATA[
        if (event.ctrlKey) {
          this.selected = null;
          return;
        }
        
        if (event.target == this)
          this.selected = null
        else
          this.selected = event.target;
      ]]>
      </handler>
      <handler event="dblclick" action="if (this.selected &amp;&amp; event.button == 0) this.selected.fireEvent('open');"/>
      <handler event="keypress" keycode="vk_enter" action="if (this.selected) this.selected.fireEvent('open');"/>
      <handler event="keypress" keycode="vk_return" action="if (this.selected) this.selected.fireEvent('open');"/>
      <handler event="keypress" keycode="vk_up" action="this.selectionBackward(event);"/>
      <handler event="keypress" keycode="vk_left" action="this.selectionBackward(event);"/>
      <handler event="keypress" keycode="vk_down" action="this.selectionForward(event);"/>
      <handler event="keypress" keycode="vk_right" action="this.selectionForward(event);"/>
      <handler event="keypress" keycode="vk_up" modifiers="meta" action="this.selected = null"/>
      <handler event="keypress" keycode="vk_down" modifiers="meta" action="this.selected = null"/>
      <handler event="keypress" keycode="vk_left" modifiers="meta" action="this.selected = null"/>
      <handler event="keypress" keycode="vk_right" modifiers="meta" action="this.selected = null"/>
      <handler event="keypress" keycode="vk_delete" action="if (this.selected) this.selected.fireEvent('remove');"/>
    </handlers>
  </binding>
  
</bindings>

