Source code

Revision control

Line Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
<?xml version="1.0"?>
<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->


<!DOCTYPE bindings [
<!ENTITY % treeDTD SYSTEM "chrome://global/locale/tree.dtd">
%treeDTD;
]>

<bindings id="treeBindings"
   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="tree" extends="chrome://global/content/bindings/general.xml#basecontrol">
    <content hidevscroll="true" hidehscroll="true" clickthrough="never">
      <children includes="treecols"/>
      <xul:stack class="tree-stack" flex="1">
        <xul:treerows class="tree-rows" flex="1" xbl:inherits="hidevscroll">
          <children/>
        </xul:treerows>
        <xul:textbox anonid="input" class="tree-input" left="0" top="0" hidden="true"/>
      </xul:stack>
      <xul:hbox xbl:inherits="collapsed=hidehscroll">
        <xul:scrollbar orient="horizontal" flex="1" increment="16" style="position:relative; z-index:2147483647;"
          oncontextmenu="event.stopPropagation(); event.preventDefault();"
          onclick="event.stopPropagation(); event.preventDefault();"
          ondblclick="event.stopPropagation();"
          oncommand="event.stopPropagation();"/>
        <xul:scrollcorner xbl:inherits="collapsed=hidevscroll"
          oncontextmenu="event.stopPropagation(); event.preventDefault();"
          onclick="event.stopPropagation(); event.preventDefault();"
          ondblclick="event.stopPropagation();"
          oncommand="event.stopPropagation();"/>
      </xul:hbox>
    </content>

    <implementation implements="nsIDOMXULMultiSelectControlElement">
      <property name="body"
                onget="return this.treeBody;"/>

      <property name="editable"
                onget="return this.getAttribute('editable') == 'true';"
                onset="if (val) this.setAttribute('editable', 'true');
                       else this.removeAttribute('editable'); return val;"/>

      <!-- ///////////////// nsIDOMXULSelectControlElement ///////////////// -->

      <!-- ///////////////// nsIDOMXULMultiSelectControlElement ///////////////// -->

      <property name="selType"
                onget="return this.getAttribute('seltype')"
                onset="this.setAttribute('seltype', val); return val;"/>

      <property name="currentIndex"
                onget="return this.view ? this.view.selection.currentIndex: - 1;"
                onset="if (this.view) return this.view.selection.currentIndex = val; return val;"/>

      <field name="pageUpOrDownMovesSelection">
        !/Mac/.test(navigator.platform)
      </field>
      <property name="keepCurrentInView"
                onget="return (this.getAttribute('keepcurrentinview') == 'true');"
                onset="if (val) this.setAttribute('keepcurrentinview', 'true');
                       else this.removeAttribute('keepcurrentinview'); return val;"/>

      <property name="enableColumnDrag"
                onget="return this.hasAttribute('enableColumnDrag');"
                onset="if (val) this.setAttribute('enableColumnDrag', 'true');
                       else this.removeAttribute('enableColumnDrag'); return val;"/>

      <field name="_inputField">null</field>

      <property name="inputField" readonly="true">
        <getter><![CDATA[
          if (!this._inputField)
            this._inputField = document.getAnonymousElementByAttribute(this, "anonid", "input");
          return this._inputField;
        ]]></getter>
      </property>

     <property name="disableKeyNavigation"
                onget="return this.hasAttribute('disableKeyNavigation');"
                onset="if (val) this.setAttribute('disableKeyNavigation', 'true');
                       else this.removeAttribute('disableKeyNavigation'); return val;"/>

      <field name="_editingRow">-1</field>
      <field name="_editingColumn">null</field>

      <property name="editingRow" readonly="true"
                onget="return this._editingRow;"/>
      <property name="editingColumn" readonly="true"
                onget="return this._editingColumn;"/>

      <property name="_selectDelay"
                onset="this.setAttribute('_selectDelay', val);"
                onget="return this.getAttribute('_selectDelay') || 50;"/>
      <field name="_columnsDirty">true</field>
      <field name="_lastKeyTime">0</field>
      <field name="_incrementalString">""</field>

      <field name="_touchY">-1</field>

      <method name="_ensureColumnOrder">
        <body><![CDATA[
          if (!this._columnsDirty)
            return;

          if (this.columns) {
            // update the ordinal position of each column to assure that it is
            // an odd number and 2 positions above its next sibling
            var cols = [];
            var i;
            for (var col = this.columns.getFirstColumn(); col; col = col.getNext())
              cols.push(col.element);
            for (i = 0; i < cols.length; ++i)
              cols[i].setAttribute("ordinal", (i * 2) + 1);

            // update the ordinal positions of splitters to even numbers, so that
            // they are in between columns
            var splitters = this.getElementsByTagName("splitter");
            for (i = 0; i < splitters.length; ++i)
              splitters[i].setAttribute("ordinal", (i + 1) * 2);
          }
          this._columnsDirty = false;
        ]]></body>
      </method>

      <method name="_reorderColumn">
        <parameter name="aColMove"/>
        <parameter name="aColBefore"/>
        <parameter name="aBefore"/>
        <body><![CDATA[
          this._ensureColumnOrder();

          var i;
          var cols = [];
          var col = this.columns.getColumnFor(aColBefore);
          if (parseInt(aColBefore.ordinal) < parseInt(aColMove.ordinal)) {
            if (aBefore)
              cols.push(aColBefore);
            for (col = col.getNext(); col.element != aColMove;
                 col = col.getNext())
              cols.push(col.element);

            aColMove.ordinal = cols[0].ordinal;
            for (i = 0; i < cols.length; ++i)
              cols[i].ordinal = parseInt(cols[i].ordinal) + 2;
          } else if (aColBefore.ordinal != aColMove.ordinal) {
            if (!aBefore)
              cols.push(aColBefore);
            for (col = col.getPrevious(); col.element != aColMove;
                 col = col.getPrevious())
              cols.push(col.element);

            aColMove.ordinal = cols[0].ordinal;
            for (i = 0; i < cols.length; ++i)
              cols[i].ordinal = parseInt(cols[i].ordinal) - 2;
          }
        ]]></body>
      </method>

      <method name="_getColumnAtX">
        <parameter name="aX"/>
        <parameter name="aThresh"/>
        <parameter name="aPos"/>
        <body><![CDATA[
          var isRTL = document.defaultView.getComputedStyle(this)
                              .direction == "rtl";

          if (aPos)
            aPos.value = isRTL ? "after" : "before";

          var columns = [];
          var col = this.columns.getFirstColumn();
          while (col) {
            columns.push(col);
            col = col.getNext();
          }
          if (isRTL)
            columns.reverse();
          var currentX = this.boxObject.x;
          var adjustedX = aX + this.horizontalPosition;
          for (var i = 0; i < columns.length; ++i) {
            col = columns[i];
            var cw = col.element.boxObject.width;
            if (cw > 0) {
              currentX += cw;
              if (currentX - (cw * aThresh) > adjustedX)
                return col.element;
            }
          }

          if (aPos)
            aPos.value = isRTL ? "before" : "after";
          return columns.pop().element;
        ]]></body>
      </method>

      <method name="changeOpenState">
        <parameter name="row"/>
        <!-- Optional parameter openState == true or false to set.
             No openState param == toggle -->
        <parameter name="openState"/>
        <body><![CDATA[
          if (row < 0 || !this.view.isContainer(row)) {
            return false;
          }

          if (this.view.isContainerOpen(row) != openState) {
            this.view.toggleOpenState(row);
            if (row == this.currentIndex) {
              // Only fire event when current row is expanded or collapsed
              // because that's all the assistive technology really cares about.
              var event = document.createEvent("Events");
              event.initEvent("OpenStateChange", true, true);
              this.dispatchEvent(event);
            }
            return true;
          }
          return false;
        ]]></body>
      </method>

      <method name="_keyNavigate">
        <parameter name="event"/>
        <body><![CDATA[
          var key = String.fromCharCode(event.charCode).toLowerCase();
          if (event.timeStamp - this._lastKeyTime > 1000)
            this._incrementalString = key;
          else
            this._incrementalString += key;
          this._lastKeyTime = event.timeStamp;

          var length = this._incrementalString.length;
          var incrementalString = this._incrementalString;
          var charIndex = 1;
          while (charIndex < length && incrementalString[charIndex] == incrementalString[charIndex - 1])
            charIndex++;
          // If all letters in incremental string are same, just try to match the first one
          if (charIndex == length) {
            length = 1;
            incrementalString = incrementalString.substring(0, length);
          }

          var keyCol = this.columns.getKeyColumn();
          var rowCount = this.view.rowCount;
          var start = 1;

          var c = this.currentIndex;
          if (length > 1) {
            start = 0;
            if (c < 0)
              c = 0;
          }

          for (var i = 0; i < rowCount; i++) {
            var l = (i + start + c) % rowCount;
            var cellText = this.view.getCellText(l, keyCol);
            cellText = cellText.substring(0, length).toLowerCase();
            if (cellText == incrementalString)
              return l;
          }
          return -1;
        ]]></body>
      </method>

      <method name="startEditing">
        <parameter name="row"/>
        <parameter name="column"/>
        <body>
          <![CDATA[
            if (!this.editable)
              return false;
            if (row < 0 || row >= this.view.rowCount || !column)
              return false;
            if (column.type != window.TreeColumn.TYPE_TEXT &&
                column.type != window.TreeColumn.TYPE_PASSWORD)
              return false;
            if (column.cycler || !this.view.isEditable(row, column))
              return false;

            // Beyond this point, we are going to edit the cell.
            if (this._editingColumn)
              this.stopEditing();

            var input = this.inputField;

            this.ensureCellIsVisible(row, column);

            // Get the coordinates of the text inside the cell.
            var textRect = this.getCoordsForCellItem(row, column, "text");

            // Get the coordinates of the cell itself.
            var cellRect = this.getCoordsForCellItem(row, column, "cell");

            // Calculate the top offset of the textbox.
            var style = window.getComputedStyle(input);
            var topadj = parseInt(style.borderTopWidth) + parseInt(style.paddingTop);
            input.top = textRect.y - topadj;

            // The leftside of the textbox is aligned to the left side of the text
            // in LTR mode, and left side of the cell in RTL mode.
            var left, widthdiff;
            if (style.direction == "rtl") {
              left = cellRect.x;
              widthdiff = cellRect.x - textRect.x;
            } else {
              left = textRect.x;
              widthdiff = textRect.x - cellRect.x;
            }

            input.left = left;
            input.height = textRect.height + topadj +
                           parseInt(style.borderBottomWidth) +
                           parseInt(style.paddingBottom);
            input.width = cellRect.width - widthdiff;
            input.hidden = false;

            input.value = this.view.getCellText(row, column);

            input.select();
            input.inputField.focus();

            this._editingRow = row;
            this._editingColumn = column;
            this.setAttribute("editing", "true");

            this.invalidateCell(row, column);
            return true;
          ]]>
        </body>
      </method>

      <method name="stopEditing">
        <parameter name="accept"/>
        <body>
          <![CDATA[
            if (!this._editingColumn)
              return;

            var input = this.inputField;
            var editingRow = this._editingRow;
            var editingColumn = this._editingColumn;
            this._editingRow = -1;
            this._editingColumn = null;

            if (accept) {
              var value = input.value;
              this.view.setCellText(editingRow, editingColumn, value);
            }
            input.hidden = true;
            input.value = "";
            this.removeAttribute("editing");
          ]]>
        </body>
      </method>

      <method name="_moveByOffset">
        <parameter name="offset"/>
        <parameter name="edge"/>
        <parameter name="event"/>
        <body>
          <![CDATA[
            event.preventDefault();

            if (this.view.rowCount == 0)
              return;

            if (event.getModifierState("Accel") && this.view.selection.single) {
              this.scrollByLines(offset);
              return;
            }

            var c = this.currentIndex + offset;
            if (offset > 0 ? c > edge : c < edge) {
              if (this.view.selection.isSelected(edge) && this.view.selection.count <= 1)
                return;
              c = edge;
            }

            if (!event.getModifierState("Accel"))
              this.view.selection.timedSelect(c, this._selectDelay);
            else // Ctrl+Up/Down moves the anchor without selecting
              this.currentIndex = c;
            this.ensureRowIsVisible(c);
          ]]>
        </body>
      </method>

      <method name="_moveByOffsetShift">
        <parameter name="offset"/>
        <parameter name="edge"/>
        <parameter name="event"/>
        <body>
          <![CDATA[
            event.preventDefault();

            if (this.view.rowCount == 0)
              return;

            if (this.view.selection.single) {
              this.scrollByLines(offset);
              return;
            }

            if (this.view.rowCount == 1 && !this.view.selection.isSelected(0)) {
              this.view.selection.timedSelect(0, this._selectDelay);
              return;
            }

            var c = this.currentIndex;
            if (c == -1)
                c = 0;

            if (c == edge) {
              if (this.view.selection.isSelected(c))
                return;
            }

            // Extend the selection from the existing pivot, if any
            this.view.selection.rangedSelect(-1, c + offset,
                                             event.getModifierState("Accel"));
            this.ensureRowIsVisible(c + offset);

          ]]>
        </body>
      </method>

      <method name="_moveByPage">
        <parameter name="offset"/>
        <parameter name="edge"/>
        <parameter name="event"/>
        <body>
          <![CDATA[
            event.preventDefault();

            if (this.view.rowCount == 0)
              return;

            if (this.pageUpOrDownMovesSelection == event.getModifierState("Accel")) {
               this.scrollByPages(offset);
               return;
            }

            if (this.view.rowCount == 1 && !this.view.selection.isSelected(0)) {
              this.view.selection.timedSelect(0, this._selectDelay);
              return;
            }

            var c = this.currentIndex;
            if (c == -1)
              return;

            if (c == edge && this.view.selection.isSelected(c)) {
              this.ensureRowIsVisible(c);
              return;
            }
            var i = this.getFirstVisibleRow();
            var p = this.getPageLength();

            if (offset > 0) {
              i += p - 1;
              if (c >= i) {
                 i = c + p;
                 this.ensureRowIsVisible(i > edge ? edge : i);
              }
              i = i > edge ? edge : i;

            } else if (c <= i) {
               i = c <= p ? 0 : c - p;
               this.ensureRowIsVisible(i);
            }
            this.view.selection.timedSelect(i, this._selectDelay);
          ]]>
        </body>
      </method>

      <method name="_moveByPageShift">
        <parameter name="offset"/>
        <parameter name="edge"/>
        <parameter name="event"/>
        <body>
          <![CDATA[
            event.preventDefault();

            if (this.view.rowCount == 0)
              return;

            if (this.view.rowCount == 1 && !this.view.selection.isSelected(0) &&
                !(this.pageUpOrDownMovesSelection == event.getModifierState("Accel"))) {
              this.view.selection.timedSelect(0, this._selectDelay);
              return;
            }

            if (this.view.selection.single)
              return;

            var c = this.currentIndex;
            if (c == -1)
              return;
            if (c == edge && this.view.selection.isSelected(c)) {
              this.ensureRowIsVisible(edge);
              return;
            }
            var i = this.getFirstVisibleRow();
            var p = this.getPageLength();

            if (offset > 0) {
              i += p - 1;
              if (c >= i) {
                 i = c + p;
                 this.ensureRowIsVisible(i > edge ? edge : i);
              }
              // Extend the selection from the existing pivot, if any
              this.view.selection.rangedSelect(-1, i > edge ? edge : i, event.getModifierState("Accel"));

            } else {

              if (c <= i) {
                 i = c <= p ? 0 : c - p;
                 this.ensureRowIsVisible(i);
              }
              // Extend the selection from the existing pivot, if any
              this.view.selection.rangedSelect(-1, i, event.getModifierState("Accel"));
            }

          ]]>
        </body>
      </method>

      <method name="_moveToEdge">
        <parameter name="edge"/>
        <parameter name="event"/>
        <body>
          <![CDATA[
            event.preventDefault();

            if (this.view.rowCount == 0)
              return;

            if (this.view.selection.isSelected(edge) && this.view.selection.count == 1) {
              this.currentIndex = edge;
              return;
            }

            // Normal behaviour is to select the first/last row
            if (!event.getModifierState("Accel"))
              this.view.selection.timedSelect(edge, this._selectDelay);

            // In a multiselect tree Ctrl+Home/End moves the anchor
            else if (!this.view.selection.single)
              this.currentIndex = edge;

            this.ensureRowIsVisible(edge);
          ]]>
        </body>
      </method>

      <method name="_moveToEdgeShift">
        <parameter name="edge"/>
        <parameter name="event"/>
        <body>
          <![CDATA[
            event.preventDefault();

            if (this.view.rowCount == 0)
              return;

            if (this.view.rowCount == 1 && !this.view.selection.isSelected(0)) {
              this.view.selection.timedSelect(0, this._selectDelay);
              return;
            }

            if (this.view.selection.single ||
                (this.view.selection.isSelected(edge)) && this.view.selection.isSelected(this.currentIndex))
              return;

            // Extend the selection from the existing pivot, if any.
            // -1 doesn't work here, so using currentIndex instead
            this.view.selection.rangedSelect(this.currentIndex, edge, event.getModifierState("Accel"));

            this.ensureRowIsVisible(edge);
          ]]>
        </body>
      </method>
      <method name="_handleEnter">
        <parameter name="event"/>
        <body><![CDATA[
          if (this._editingColumn) {
            this.stopEditing(true);
            this.focus();
            return true;
          }

          return this.changeOpenState(this.currentIndex);
        ]]></body>
      </method>
    </implementation>

    <handlers>
      <handler event="touchstart">
        <![CDATA[
          function isScrollbarElement(target) {
            return (target.localName == "thumb" || target.localName == "slider")
                && target.namespaceURI == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
          }
          if (event.touches.length > 1 || isScrollbarElement(event.touches[0].target)) {
            // Multiple touch points detected, abort. In particular this aborts
            // the panning gesture when the user puts a second finger down after
            // already panning with one finger. Aborting at this point prevents
            // the pan gesture from being resumed until all fingers are lifted
            // (as opposed to when the user is back down to one finger).
            // Additionally, if the user lands on the scrollbar don't use this
            // code for scrolling, instead allow gecko to handle scrollbar
            // interaction normally.
            this._touchY = -1;
          } else {
            this._touchY = event.touches[0].screenY;
          }
        ]]>
      </handler>
      <handler event="touchmove">
        <![CDATA[
          if (event.touches.length == 1 &&
              this._touchY >= 0) {
            var deltaY = this._touchY - event.touches[0].screenY;
            var lines = Math.trunc(deltaY / this.rowHeight);
            if (Math.abs(lines) > 0) {
              this.scrollByLines(lines);
              deltaY -= lines * this.rowHeight;
              this._touchY = event.touches[0].screenY + deltaY;
            }
            event.preventDefault();
          }
        ]]>
      </handler>
      <handler event="touchend">
        <![CDATA[
          this._touchY = -1;
        ]]>
      </handler>
      <handler event="MozMousePixelScroll">
        <![CDATA[
          if (!(this.getAttribute("allowunderflowscroll") == "true" &&
                this.getAttribute("hidevscroll") == "true"))
            event.preventDefault();
        ]]>
      </handler>
      <handler event="DOMMouseScroll">
        <![CDATA[
          if (!(this.getAttribute("allowunderflowscroll") == "true" &&
                this.getAttribute("hidevscroll") == "true"))
            event.preventDefault();

          if (this._editingColumn)
            return;
          if (event.axis == event.HORIZONTAL_AXIS)
            return;

          var rows = event.detail;
          if (rows == UIEvent.SCROLL_PAGE_UP)
            this.scrollByPages(-1);
          else if (rows == UIEvent.SCROLL_PAGE_DOWN)
            this.scrollByPages(1);
          else
            this.scrollByLines(rows);
        ]]>
      </handler>
      <handler event="MozSwipeGesture" preventdefault="true">
        <![CDATA[
          // Figure out which row to show
          let targetRow = 0;

          // Only handle swipe gestures up and down
          switch (event.direction) {
            case event.DIRECTION_DOWN:
              targetRow = this.view.rowCount - 1;
              // Fall through for actual action
            case event.DIRECTION_UP:
              this.ensureRowIsVisible(targetRow);
              break;
          }
        ]]>
      </handler>
      <handler event="select" phase="target"
               action="if (event.originalTarget == this) this.stopEditing(true);"/>
      <handler event="focus">
        <![CDATA[
          this.focused = true;
          if (this.currentIndex == -1 && this.view.rowCount > 0) {
            this.currentIndex = this.getFirstVisibleRow();
          }
        ]]>
      </handler>
      <handler event="blur" action="this.focused = false;"/>
      <handler event="blur" phase="capturing"
               action="if (event.originalTarget == this.inputField.inputField) this.stopEditing(true);"/>
      <handler event="keydown" keycode="VK_RETURN">
        if (this._handleEnter(event)) {
          event.stopPropagation();
          event.preventDefault();
        }
      </handler>
      <handler event="keydown" keycode="VK_ESCAPE">
        <![CDATA[
          if (this._editingColumn) {
            this.stopEditing(false);
            this.focus();
            event.stopPropagation();
            event.preventDefault();
          }
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_LEFT">
        <![CDATA[
         if (this._editingColumn)
           return;

         var row = this.currentIndex;
         if (row < 0)
           return;

         if (this.changeOpenState(this.currentIndex, false)) {
           event.preventDefault();
           return;
         }
         var parentIndex = this.view.getParentIndex(this.currentIndex);
         if (parentIndex >= 0) {
           this.view.selection.select(parentIndex);
           this.ensureRowIsVisible(parentIndex);
           event.preventDefault();
         }
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_RIGHT">
        <![CDATA[
         if (this._editingColumn)
           return;

          var row = this.currentIndex;
          if (row < 0)
            return;

          if (this.changeOpenState(row, true)) {
            event.preventDefault();
            return;
          }
          var c = row + 1;
          var view = this.view;
          if (c < view.rowCount &&
              view.getParentIndex(c) == row) {
            // If already opened, select the first child.
            // The getParentIndex test above ensures that the children
            // are already populated and ready.
            this.view.selection.timedSelect(c, this._selectDelay);
            this.ensureRowIsVisible(c);
            event.preventDefault();
          }
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_UP" modifiers="accel any">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveByOffset(-1, 0, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_DOWN" modifiers="accel any">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveByOffset(1, this.view.rowCount - 1, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_UP" modifiers="accel any, shift">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveByOffsetShift(-1, 0, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_DOWN" modifiers="accel any, shift">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveByOffsetShift(1, this.view.rowCount - 1, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_PAGE_UP" modifiers="accel any">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveByPage(-1, 0, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_PAGE_DOWN" modifiers="accel any">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveByPage(1, this.view.rowCount - 1, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_PAGE_UP" modifiers="accel any, shift">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveByPageShift(-1, 0, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_PAGE_DOWN" modifiers="accel any, shift">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveByPageShift(1, this.view.rowCount - 1, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_HOME" modifiers="accel any">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveToEdge(0, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_END" modifiers="accel any">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveToEdge(this.view.rowCount - 1, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_HOME" modifiers="accel any, shift">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveToEdgeShift(0, event);
        ]]>
      </handler>
      <handler event="keydown" keycode="VK_END" modifiers="accel any, shift">
        <![CDATA[
          if (this._editingColumn)
            return;
          this._moveToEdgeShift(this.view.rowCount - 1, event);
        ]]>
      </handler>
      <handler event="keypress">
        <![CDATA[
         if (this._editingColumn)
           return;

         if (event.charCode == " ".charCodeAt(0)) {
           var c = this.currentIndex;
           if (!this.view.selection.isSelected(c) ||
               (!this.view.selection.single && event.getModifierState("Accel"))) {
             this.view.selection.toggleSelect(c);
             event.preventDefault();
           }
         } else if (!this.disableKeyNavigation && event.charCode > 0 &&
                    !event.altKey && !event.getModifierState("Accel") &&
                    !event.metaKey && !event.ctrlKey) {
           var l = this._keyNavigate(event);
           if (l >= 0) {
             this.view.selection.timedSelect(l, this._selectDelay);
             this.ensureRowIsVisible(l);
           }
           event.preventDefault();
         }
         ]]>
      </handler>
    </handlers>
  </binding>

  <binding id="treerows" extends="chrome://global/content/bindings/general.xml#basecontrol">
    <content>
      <xul:hbox flex="1" class="tree-bodybox">
        <children/>
      </xul:hbox>
      <xul:scrollbar height="0" minwidth="0" minheight="0" orient="vertical" xbl:inherits="collapsed=hidevscroll" style="position:relative; z-index:2147483647;"
        oncontextmenu="event.stopPropagation(); event.preventDefault();"
        onclick="event.stopPropagation(); event.preventDefault();"
        ondblclick="event.stopPropagation();"
        oncommand="event.stopPropagation();"/>
    </content>
    <handlers>
      <handler event="underflow">
        <![CDATA[
          // Scrollport event orientation
          // 0: vertical
          // 1: horizontal
          // 2: both (not used)
          var tree = document.getBindingParent(this);
          if (event.detail == 1)
            tree.setAttribute("hidehscroll", "true");
          else if (event.detail == 0)
            tree.setAttribute("hidevscroll", "true");
          event.stopPropagation();
        ]]>
      </handler>
      <handler event="overflow">
        <![CDATA[
          var tree = document.getBindingParent(this);
          if (event.detail == 1)
            tree.removeAttribute("hidehscroll");
          else if (event.detail == 0)
            tree.removeAttribute("hidevscroll");
          event.stopPropagation();
        ]]>
      </handler>
    </handlers>
  </binding>

  <binding id="columnpicker" display="xul:button"
           extends="chrome://global/content/bindings/general.xml#basecontrol">
    <content>
      <xul:image class="tree-columnpicker-icon"/>
      <xul:menupopup anonid="popup">
        <xul:menuseparator anonid="menuseparator"/>
        <xul:menuitem anonid="menuitem" label="&restoreColumnOrder.label;"/>
      </xul:menupopup>
    </content>

    <implementation>
      <method name="buildPopup">
        <parameter name="aPopup"/>
        <body>
          <![CDATA[
            // We no longer cache the picker content, remove the old content.
            while (aPopup.childNodes.length > 2)
              aPopup.firstChild.remove();

            var refChild = aPopup.firstChild;

            var tree = this.parentNode.parentNode;
            for (var currCol = tree.columns.getFirstColumn(); currCol;
                 currCol = currCol.getNext()) {
              // Construct an entry for each column in the row, unless
              // it is not being shown.
              var currElement = currCol.element;
              if (!currElement.hasAttribute("ignoreincolumnpicker")) {
                var popupChild = document.createElement("menuitem");
                popupChild.setAttribute("type", "checkbox");
                var columnName = currElement.getAttribute("display") ||
                                 currElement.getAttribute("label");
                popupChild.setAttribute("label", columnName);
                popupChild.setAttribute("colindex", currCol.index);
                if (currElement.getAttribute("hidden") != "true")
                  popupChild.setAttribute("checked", "true");
                if (currCol.primary)
                  popupChild.setAttribute("disabled", "true");
                aPopup.insertBefore(popupChild, refChild);
              }
            }

            var hidden = !tree.enableColumnDrag;
            const anonids = ["menuseparator", "menuitem"];
            for (var i = 0; i < anonids.length; i++) {
              var element = document.getAnonymousElementByAttribute(this, "anonid", anonids[i]);
              element.hidden = hidden;
            }
          ]]>
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="command">
        <![CDATA[
          if (event.originalTarget == this) {
            var popup = document.getAnonymousElementByAttribute(this, "anonid", "popup");
            this.buildPopup(popup);
            popup.openPopup(this, "after_end");
          } else {
            var tree = this.parentNode.parentNode;
            tree.stopEditing(true);
            var menuitem = document.getAnonymousElementByAttribute(this, "anonid", "menuitem");
            if (event.originalTarget == menuitem) {
              tree.columns.restoreNaturalOrder();
              this.removeAttribute("ordinal");
              tree._ensureColumnOrder();
            } else {
              var colindex = event.originalTarget.getAttribute("colindex");
              var column = tree.columns[colindex];
              if (column) {
                var element = column.element;
                if (element.getAttribute("hidden") == "true")
                  element.setAttribute("hidden", "false");
                else
                  element.setAttribute("hidden", "true");
              }
            }
          }
        ]]>
      </handler>
    </handlers>
  </binding>
</bindings>