DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (341e6b4a9882)

VCS Links

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 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575
<?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 % messengerDTD SYSTEM "chrome://messenger/locale/messenger.dtd" >
  %messengerDTD;
  <!ENTITY % globalDTD SYSTEM "chrome://global/locale/global.dtd">
  %globalDTD;
]>

<bindings id="tabmailBindings"
          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">

  <!-- SeaMonkey's clone of Thunderbird's tab UI mechanism.
    -
    - We expect to be instantiated with the following children:
    - * One "tabpanels" child element whose id must be placed in the
    -   "panelcontainer" attribute on the element we are being bound to. We do
    -   this because it is important to allow overlays to contribute panels.
    -   When we attempted to have the immediate children of the bound element
    -   be propagated through use of the "children" tag, we found that children
    -   contributed by overlays did not propagate.
    - * Any children you want added to the right side of the tab bar.  This is
    -   primarily intended to allow for "open a BLANK tab" buttons, namely
    -   calendar and tasks.  For reasons similar to the tabpanels case, we
    -   expect the instantiating element to provide a child hbox for overlays
    -   to contribute buttons to.
    -
    - From a javascript perspective, there are three types of code that we
    -  expect to interact with:
    - 1) Code that wants to open new tabs.
    - 2) Code that wants to contribute one or more varieties of tabs.
    - 3) Code that wants to monitor to know when the active tab changes.
    -
    - Consumer code should use the following methods:
    - * openTab(aTabModeName, arguments...): Open a tab of the given "mode",
    -   passing the provided arguments.  The tab type author should tell you
    -   the modes they implement and the required/optional arguments.
    - * setTabTitle([aOptionalTabInfo]): Tells us that the title of the current
    -   tab (if no argument is provided) or provided tab needs to be updated.
    -   This will result in a call to the tab mode's logic to update the title.
    -   In the event this is not for the current tab, the caller is responsible
    -   for ensuring that the underlying tab mode is capable of providing a tab
    -   title when it is in the background.
    - * removeCurrentTab(): Close the current tab.
    - * removeTab(aTabElement): Close the tab whose tabmail-tab bound
    -   element is passed in.
    - Changing the currently displayed tab is accomplished by changing
    -  tabmail.tabContainer's selectedIndex or selectedItem property.
    -
    - Tab contributing code should define a tab type object and register it
    -  with us by calling registerTabType.  Each tab type can provide multiple
    -  tab modes.  The rationale behind this organization is that Thunderbird
    -  historically/currently uses a single 3-pane view to display both
    -  three-pane folder browsing and single message browsing across multiple
    -  tabs.  Each tab type has the ability to use a single tab panel for all
    -  of its display needs.  So Thunderbird's "mail" tab type covers both the
    -  "folder" (3-pane folder-based browsing)  and "message" (just a single
    -  message) tab modes, while SeaMonkey integrates both flavours into just
    -  one "3pane" mode.  Likewise, calendar/lightning currently displays
    -  both its calendar and tasks in the same panel.  A tab type can also
    -  create a new tabpanel for each tab as it is created.  In that case, the
    -  tab type should probably only have a single mode unless there are a
    -  number of similar modes that can gain from code sharing.
    - The tab type definition should include the following attributes:
    - * name: The name of the tab-type, mainly to aid in debugging.
    - * panelId or perTabPanel: If using a single tab panel, the id of the
    -     panel must be provided in panelId.  If using one tab panel per tab,
    -     perTabPanel should be either the XUL element name that should be
    -     created for each tab, or a helper function to create and return the
    -     element.
    - * modes: An object whose attributes are mode names (which are
    -     automatically propagated to a 'name' attribute for debugging) and
    -     values are objects with the following attributes...
    - * any of the openTab/closeTab/saveTabState/showTab/onTitleChanged
    -     functions as described on the mode definitions.  These will only be
    -     called if the mode does not provide the functions.  Note that because
    -     the 'this' variable passed to the functions will always reference the
    -     tab type definition (rather than the mode definition), the mode
    -     functions can defer to the tab type functions by calling
    -     this.functionName().  (This should prove convenient.)
    - Mode definition attributes:
    - * type: The "type" attribute to set on the displayed tab for CSS purposes.
    -     Generally, this would be the same as the mode name, but you can do as
    -     you please.
    - * isDefault: This should only be present and should be true for the tab
    -     mode that is the tab displayed automatically on startup.
    - * maxTabs: The maximum number of this mode that can be opened at a time.
    -     If this limit is reached, any additional calls to openTab for this
    -     mode will simply result in the first existing tab of this mode being
    -     displayed.
    - * shouldSwitchTo(arguments...): Optional function. Called when
    -     openTab is called on the top-level tabmail binding. It is used to
    -     decide if the openTab function should switch to an existing tab or
    -     actually open a new tab.
    -     If the openTab function should switch to an existing tab, return the
    -     index of that tab; otherwise return -1.
    - * openTab(aTabInfo, arguments...): Called when a tab of the given mode is
    -     in the process of being opened.  aTabInfo will have its "mode"
    -     attribute set to the mode definition of the tab mode being opened.
    -     You should set the "title" attribute on it, and may set any other
    -     attributes you wish for your own use in subsequent functions.  Note
    -     that 'this' points to the tab type definition, not the mode definition
    -     as you might expect.  This allows you to place common logic code on
    -     the tab type for use by multiple modes and to defer to it.  Any
    -     arguments provided to the caller of tabmail.openTab will be passed to
    -     your function as well.
    - * canCloseTab(aTabInfo): Optional function.
    -     Return true (false) if the tab is (not) allowed to close.
    -     A tab's default permission is stored in aTabInfo.canClose.
    - * closeTab(aTabInfo): Called when aTabInfo is being closed.  The tab need
    -     not be currently displayed.  You are responsible for properly cleaning
    -     up any state you preserved in aTabInfo.
    - * saveTabState(aTabInfo): Called when aTabInfo is being switched away from
    -     so that you can preserve its state on aTabInfo.  This is primarily for
    -     single tab panel implementations; you may not have much state to save
    -     if your tab has its own tab panel.
    - * showTab(aTabInfo): Called when aTabInfo is being displayed and you
    -     should restore its state (if required).
    - * onTitleChanged(aTabInfo): Called when someone calls
    -     tabmail.setTabTitle() to hint that the tab's title needs to be
    -     updated.  This function should update aTabInfo.title if it can.
    - * getBrowser(aTabInfo): This function should return the browser element
    -     for your tab if there is one (return null or don't define this
    -     function otherwise). It is used for some toolkit functions that
    -     require a global "getBrowser" function, e.g. ZoomManager.
    -
    - Mode definition functions for menu/toolbar commands (see nsIController):
    - * supportsCommand(aCommand, aTabInfo): Called when a menu or toolbar needs
    -     to be updated. Return true if you support that command in
    -     isCommandEnabled and doCommand, return false otherwise.
    - * isCommandEnabled(aCommand, aTabInfo): Called when a menu or toolbar
    -     needs to be updated. Return true if the command can be executed at the
    -     current time, false otherwise.
    - * doCommand(aCommand, aTabInfo): Called when a menu or toolbar command is
    -     to be executed. Perform the action appropriate to the command.
    - * onEvent(aEvent, aTabInfo): This can be used to handle different events
    -     on the window.
    -
    - Tab monitoring code is expected to be used for widgets on the screen
    -  outside of the tab box that need to update themselves as the active tab
    -  changes.  This is primarily intended to be used for the ThunderBar; if
    -  you are not the ThunderBar and this sounds appealing to you, please
    -  solicit discussion on your needs on the mozilla.dev.apps.thunderbird
    -  newsgroup.
    - Tab monitoring code (un)registers itself via (un)registerTabMonitor.
    -  The following functions should be provided on the monitor object:
    - * onTabTitleChanged(aTabInfo): Called when the tab's title changes.
    - * onTabSwitched(aTabInfo, aOldTabInfo): Called when a new tab is made
    -     active.  If this is the first tab ever, aOldTabInfo will be null,
    -     otherwise aOldTabInfo will be the previously active tab.
    -->
  <binding id="tabmail"
           extends="chrome://navigator/content/tabbrowser.xml#tabbrowser">
    <resources>
      <stylesheet src="chrome://navigator/skin/tabbrowser.css"/>
    </resources>
    <content>
      <xul:tabbox anonid="tabbox"
                  flex="1"
                  eventnode="document"
                  xbl:inherits="handleCtrlPageUpDown"
                  onselect="if (event.target.localName == 'tabs' &amp;&amp;
                                'updateCurrentTab' in this.parentNode)
                              this.parentNode.updateCurrentTab();">
        <xul:hbox class="tab-drop-indicator-bar" collapsed="true">
          <xul:hbox class="tab-drop-indicator" mousethrough="always"/>
        </xul:hbox>
        <xul:hbox class="tabbrowser-strip tabmail-strip"
                  tooltip="_child"
                  context="_child"
                  anonid="strip"
                  ondragstart="nsDragAndDrop.startDrag(event, this.parentNode.parentNode); event.stopPropagation();"
                  ondragover="nsDragAndDrop.dragOver(event, this.parentNode.parentNode); event.stopPropagation();"
                  ondrop="nsDragAndDrop.drop(event, this.parentNode.parentNode); event.stopPropagation();"
                  ondragexit="nsDragAndDrop.dragExit(event, this.parentNode.parentNode); event.stopPropagation();">
          <xul:tooltip onpopupshowing="var tabmail = this.parentNode.parentNode.parentNode;
                                       return tabmail.FillTabmailTooltip(document, event);"/>
          <xul:menupopup anonid="tabContextMenu"
                         onpopupshowing="return document.getBindingParent(this)
                                                        .onTabContextMenuShowing();">
            <xul:menuitem label="&closeTabCmd.label;"
                          accesskey="&closeTabCmd.accesskey;"
                          oncommand="var tabmail = document.getBindingParent(this);
                                     tabmail.removeTab(tabmail.mContextTab);"/>
          </xul:menupopup>
          <xul:tabs class="tabbrowser-tabs tabmail-tabs"
                    flex="1"
                    anonid="tabcontainer"
                    setfocus="false"
                    onclick="this.parentNode.parentNode.parentNode.onTabClick(event);">
            <xul:tab selected="true"
                     validate="never"
                     type="3pane"
                     maxwidth="250"
                     width="0"
                     minwidth="100"
                     flex="100"
                     class="tabbrowser-tab tabmail-tab icon-holder"
                     crop="end"/>
          </xul:tabs>
          <children/>
        </xul:hbox>
        <!-- Remember, user of this binding, you need to provide tabpanels!  -->
        <children includes="tabpanels"/>
      </xul:tabbox>
    </content>

    <implementation implements="nsIController, nsIObserver">
      <constructor>
        <![CDATA[
          window.controllers.insertControllerAt(0, this);
          const kAutoHide = "browser.tabs.autoHide";
          this.mAutoHide = this.mPrefs.getBoolPref(kAutoHide);
          this.mPrefs.addObserver(kAutoHide, this, false);
        ]]>
      </constructor>

      <destructor>
        <![CDATA[
          this.mPrefs.removeObserver("browser.tabs.autoHide", this);
          window.controllers.removeController(this);
        ]]>
      </destructor>

      <field name="mPrefs">
        Components.classes["@mozilla.org/preferences-service;1"]
                  .getService(Components.interfaces.nsIPrefBranch);
      </field>

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

      <field name="tabTypes" readonly="true">
        new Object()
      </field>

      <field name="tabModes" readonly="true">
        new Object()
      </field>

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

      <field name="tabInfo" readonly="true">
        new Array()
      </field>

      <field name="tabStrip" readonly="true">
        document.getAnonymousElementByAttribute(this, "anonid", "strip");
      </field>

      <field name="tabContainer" readonly="true">
        document.getAnonymousElementByAttribute(this, "anonid", "tabcontainer");
      </field>

      <field name="panelContainer" readonly="true">
        document.getElementById(this.getAttribute("panelcontainer"));
      </field>

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

      <!-- _mAutoHide/mAutoHide reflect the current autoHide pref value -->
      <field name="_mAutoHide">false</field>
      <property name="mAutoHide" onget="return this._mAutoHide;">
        <setter>
          <![CDATA[
            if (val != this._mAutoHide)
            {
              if (this.tabContainer.childNodes.length == 1)
                this.mStripVisible = !val;
              this._mAutoHide = val;
            }
            return val;
          ]]>
        </setter>
      </property>

      <!-- mStripVisible reflects the actual XUL autoHide state -->
      <property name="mStripVisible"
                onget="return !this.tabStrip.collapsed;"
                onset="return this.tabStrip.collapsed = !val;"/>

      <method name="registerTabType">
        <parameter name="aTabType"/>
        <body>
          <![CDATA[
            if (aTabType.name in this.tabTypes)
              return;
            this.tabTypes[aTabType.name] = aTabType;
            for (let [modeName, modeDetails] in Iterator(aTabType.modes))
            {
              modeDetails.name = modeName;
              modeDetails.tabType = aTabType;
              modeDetails.tabs = [];
              this.tabModes[modeName] = modeDetails;
              if (modeDetails.isDefault)
                this.defaultTabMode = modeDetails;
            }
            aTabType.panel = document.getElementById(aTabType.panelId);
          ]]>
        </body>
      </method>

      <field name="tabMonitors" readonly="true">
        new Array()
      </field>

      <method name="registerTabMonitor">
        <parameter name="aTabMonitor"/>
        <body>
          <![CDATA[
            if (this.tabMonitors.indexOf(aTabMonitor) == -1)
              this.tabMonitors.push(aTabMonitor);
          ]]>
        </body>
      </method>

      <method name="unregisterTabMonitor">
        <parameter name="aTabMonitor"/>
        <body>
          <![CDATA[
            if (this.tabMonitors.indexOf(aTabMonitor) >= 0)
              this.tabMonitors.splice(this.tabMonitors.indexOf(aTabMonitor), 1);
          ]]>
        </body>
      </method>

      <method name="openFirstTab">
        <body>
          <![CDATA[
            // From the moment of creation, our XBL binding already has a
            // visible tab. We need to create a tab information structure for
            // this tab. In the process we also generate a synthetic "tab title
            // changed" event to ensure we have an accurate title.
            // Note: for mail tabs, the title gets only set later when the
            // folder or message is loaded, as we don't have a gDBView yet!
            // We assume the tab contents will set themselves up correctly.
            if (!this.tabInfo.length)
            {
              let firstTabInfo = {mode: this.defaultTabMode, canClose: true};
              let firstTabNode = this.tabContainer.firstChild;
              firstTabInfo.mode.tabs.push(firstTabInfo);
              this.tabInfo[0] = this.currentTabInfo = firstTabInfo;
              this.setTabTitle(firstTabInfo);
              if (this.tabMonitors.length)
              {
                for (let [i, tabMonitor] in Iterator(this.tabMonitors))
                  tabMonitor.onTabSwitched(firstTabInfo, null);
              }
            }
          ]]>
        </body>
      </method>

      <method name="openTab">
        <parameter name="aTabModeName"/>
        <!-- parameter name="aMoreParameters..."/-->
        <body>
          <![CDATA[
            if (!aTabModeName)
              aTabModeName = this.currentTabInfo.mode.type;

            let tabMode = this.tabModes[aTabModeName];
            // if we are already at our limit for this mode, show an existing one
            if (tabMode.tabs.length == tabMode.maxTabs)
            {
              // show the first tab of this mode
              this.tabContainer.selectedIndex = this.tabInfo.indexOf(tabMode.tabs[0]);
              return;
            }

            // If the mode wants us to, we should switch to an existing tab
            // rather than open a new one.
            let shouldSwitchToFunc = tabMode.shouldSwitchTo ||
                                     tabMode.tabType.shouldSwitchTo;

            if (shouldSwitchToFunc)
            {
              let args = Array.prototype.slice.call(arguments, 1);
              let tabIndex = shouldSwitchToFunc.apply(tabMode.tabType, args);
              if (tabIndex >= 0)
              {
                this.selectTabByIndex(tabIndex);
                return;
              }
            }

            // we need to save the state before it gets corrupted
            this.saveCurrentTabState();

            let tabInfo = {mode: tabMode, canClose: true};
            tabMode.tabs.push(tabInfo);

            let t = document.createElementNS(
              "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
              "tab");
            t.setAttribute("crop", "end");
            t.maxWidth = this.tabContainer.mTabMaxWidth;
            t.minWidth = this.tabContainer.mTabMinWidth;
            t.width = 0;
            t.setAttribute("flex", "100");
            t.setAttribute("validate", "never");
            t.className = "tabbrowser-tab tabmail-tab icon-holder";
            // for styling purposes, apply the type to the tab
            // (this attribute may be overwritten by mode functions)
            t.setAttribute("type", tabInfo.mode.type);
            this.tabContainer.appendChild(t);
            if (!this.mStripVisible)
            {
              this.mStripVisible = true;
              this.tabContainer.adjustTabstrip();
            }

            let oldPanel = this.panelContainer.selectedPanel;

            // Open new tabs in the background?
            let switchToNewTab = !this.mPrefs.getBoolPref("browser.tabs.loadInBackground");
            tabInfo.switchToNewTab = switchToNewTab;

            // the order of the following statements is important
            let oldTabInfo = this.currentTabInfo;
            this.tabInfo[this.tabContainer.childNodes.length - 1] = tabInfo;

            if (switchToNewTab)
            {
              this.currentTabInfo = tabInfo;
              // this has a side effect of calling updateCurrentTab, but our
              // setting currentTabInfo above will cause it to take no action.
              this.tabContainer.selectedIndex = this.tabContainer.childNodes.length - 1;
            }
            // make sure we are on the right panel
            let selectedPanel;
            if (tabInfo.mode.tabType.perTabPanel)
            {
              // should we create the element for them, or will they do it?
              if (typeof(tabInfo.mode.tabType.perTabPanel) == "string")
              {
                tabInfo.panel = document.createElementNS(
                  "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
                  tabInfo.mode.tabType.perTabPanel);
              }
              else
              {
                tabInfo.panel = tabInfo.mode.tabType.perTabPanel(tabInfo);
              }
              this.panelContainer.appendChild(tabInfo.panel);
              selectedPanel = tabInfo.panel;
            }
            else
            {
              selectedPanel = tabInfo.mode.tabType.panel;
            }
            if (switchToNewTab)
              this.panelContainer.selectedPanel = selectedPanel;

            oldPanel.removeAttribute("selected");
            this.panelContainer.selectedPanel.setAttribute("selected", "true");

            let tabOpenFunc = tabInfo.mode.openTab ||
                              tabInfo.mode.tabType.openTab;
            let args = [tabInfo].concat(Array.prototype.slice.call(arguments, 1));
            if (tabOpenFunc)
              tabOpenFunc.apply(tabInfo.mode.tabType, args);

            if (!switchToNewTab)
            {
              // if the new tab isn't made current,
              // its title won't change automatically
              this.setTabTitle(tabInfo);
            }

            if (this.tabMonitors.length)
            {
              if (switchToNewTab)
                for (let [i, tabMonitor] in Iterator(this.tabMonitors))
                  tabMonitor.onTabSwitched(tabInfo, oldTabInfo);
            }

            t.setAttribute("label", tabInfo.title);
            if (switchToNewTab)
            {
              let docTitle = tabInfo.title;
              if (!/Mac/.test(navigator.platform))
                docTitle += " - " + gBrandBundle.getString("brandFullName");
              document.title = docTitle;

              // Update the toolbar status - we don't need to do menus as they
              // do themselves when we open them.
              UpdateMailToolbar("tabmail");
            }
          ]]>
        </body>
      </method>

      <method name="selectTabByMode">
        <parameter name="aTabModeName"/>
        <body>
          <![CDATA[
            let tabMode = this.tabModes[aTabModeName];
            if (tabMode.tabs.length)
            {
              let desiredTab = tabMode.tabs[0];
              this.tabContainer.selectedIndex = this.tabInfo.indexOf(desiredTab);
            }
          ]]>
        </body>
      </method>

      <method name="selectTabByIndex">
        <parameter name="aIndex"/>
        <body>
          <![CDATA[
            // count backwards for aIndex < 0
            if (aIndex < 0)
              aIndex += this.tabInfo.length;
            if (aIndex >= 0 &&
                aIndex < this.tabInfo.length &&
                aIndex != this.tabContainer.selectedIndex)
            {
              this.tabContainer.selectedIndex = aIndex;
            }

            if (aEvent)
            {
              aEvent.preventDefault();
              aEvent.stopPropagation();
            }
          ]]>
        </body>
      </method>

      <method name="closeTabs">
        <body>
          <![CDATA[
            for (let i = 0; i < this.tabInfo.length; i++)
            {
              let tabInfo = this.tabInfo[i];
              let tabCloseFunc = tabInfo.mode.closeTab ||
                                 tabInfo.mode.tabType.closeTab;
              if (tabCloseFunc)
                tabCloseFunc.call(tabInfo.mode.tabType, tabInfo);
            }
          ]]>
        </body>
      </method>

      <method name="removeTab">
        <parameter name="aTabNode"/>
        <!-- parameter name="aMoreParameters..."/-->
        <body>
          <![CDATA[
            // Find and locate the tab in our list.
            let iTab, numTabs = this.tabContainer.childNodes.length;
            for (iTab = 0; iTab < numTabs; iTab++)
              if (this.tabContainer.childNodes[iTab] == aTabNode)
                break;
            let tabInfo = this.tabInfo[iTab];

            // ask the tab type implementation if we're allowed to close the tab
            let canClose = tabInfo.canClose;
            let canCloseFunc = tabInfo.mode.canCloseTab ||
                               tabInfo.mode.tabType.canCloseTab;
            if (canCloseFunc)
              canClose = canCloseFunc.call(tabInfo.mode.tabType, tabInfo);
            if (!canClose)
              return;

            let closeFunc = tabInfo.mode.closeTab ||
                            tabInfo.mode.tabType.closeTab;
            if (closeFunc)
              closeFunc.call(tabInfo.mode.tabType, tabInfo);

            this.tabInfo.splice(iTab, 1);
            tabInfo.mode.tabs.splice(tabInfo.mode.tabs.indexOf(tabInfo), 1);
            aTabNode.remove();
            --numTabs;
            if (this.tabContainer.selectedIndex == -1)
              this.tabContainer.selectedIndex = (iTab == numTabs) ? iTab - 1 : iTab;
            if (this.currentTabInfo == tabInfo)
              this.updateCurrentTab();

            if (tabInfo.panel)
            {
              tabInfo.panel.remove();
              delete tabInfo.panel;
            }
            if (numTabs == 1 && this.mAutoHide)
              this.mStripVisible = false;
          ]]>
        </body>
      </method>

      <method name="removeCurrentTab">
        <body>
          <![CDATA[
            this.removeTab(this.tabContainer.selectedItem);
          ]]>
        </body>
      </method>

      <!--  UpdateCurrentTab - called in response to changing the current tab -->
      <method name="updateCurrentTab">
        <body>
          <![CDATA[
            if (this.currentTabInfo != this.tabInfo[this.tabContainer.selectedIndex])
            {
              if (this.currentTabInfo)
                this.saveCurrentTabState();
              let oldTabInfo = this.currentTabInfo;
              let oldPanel = this.panelContainer.selectedPanel;
              let tabInfo = this.currentTabInfo = this.tabInfo[this.tabContainer.selectedIndex];
              this.panelContainer.selectedPanel = tabInfo.panel ||
                                                  tabInfo.mode.tabType.panel;

              // Update the selected attribute on the current and old tab panel.
              oldPanel.removeAttribute("selected");
              this.panelContainer.selectedPanel.setAttribute("selected", "true");

              let showTabFunc = tabInfo.mode.showTab ||
                                tabInfo.mode.tabType.showTab;
              if (showTabFunc)
                showTabFunc.call(tabInfo.mode.tabType, tabInfo);
              if (this.tabMonitors.length)
              {
                for (let [i, tabMonitor] in Iterator(this.tabMonitors))
                  tabMonitor.onTabSwitched(tabInfo, oldTabInfo);
              }

              let docTitle = tabInfo.title;
              if (!/Mac/.test(navigator.platform))
                docTitle += " - " + gBrandBundle.getString("brandFullName");
              document.title = docTitle;

              // Update the toolbar status - we don't need to do menus as they
              // do themselves when we open them.
              UpdateMailToolbar("tabmail");
            }
          ]]>
        </body>
      </method>

      <method name="saveTabState">
        <parameter name="aTabInfo"/>
        <body>
          <![CDATA[
            if (!aTabInfo)
              return;
            let saveTabFunc = aTabInfo.mode.saveTabState ||
                              aTabInfo.mode.tabType.saveTabState;
            if (saveTabFunc)
              saveTabFunc.call(aTabInfo.mode.tabType, aTabInfo);
          ]]>
        </body>
      </method>

      <method name="saveCurrentTabState">
        <body>
          <![CDATA[
            if (!this.currentTabInfo)
              this.currentTabInfo = this.tabInfo[0];
            // save the old tab state before we change the current tab
            this.saveTabState(this.currentTabInfo);
          ]]>
        </body>
      </method>

      <method name="onTabClick">
        <parameter name="event"/>
        <body>
          <![CDATA[
            // a middle mouse button click on a tab is a short cut for closing a tab
            if (event.button != 1 || event.target.localName != "tab")
              return;
            this.removeTab(event.target);
            event.stopPropagation();
          ]]>
        </body>
      </method>

      <method name="setTabTitle">
        <parameter name="aTabInfo"/>
        <body>
          <![CDATA[
            // First find the tab and its index.
            let tabInfo;
            let index;
            if (aTabInfo)
            {
              tabInfo = aTabInfo;
              for (index = 0; index < this.tabInfo.length; ++index)
              {
                if (tabInfo == this.tabInfo[index])
                  break;
              }
            }
            else
            {
              index = this.tabContainer.selectedIndex;
              tabInfo = this.tabInfo[index];
            }

            if (tabInfo)
            {
              let tabNode = this.tabContainer.childNodes[index];
              let titleChangeFunc = tabInfo.mode.onTitleChanged ||
                                    tabInfo.mode.tabType.onTitleChanged;
              if (titleChangeFunc)
                titleChangeFunc.call(tabInfo.mode.tabType, tabInfo, tabNode);
              if (this.tabMonitors.length)
              {
                for (let [i, tabMonitor] in Iterator(this.tabMonitors))
                  tabMonitor.onTabTitleChanged(tabInfo);
              }
              tabNode.setAttribute("label", tabInfo.title);

              // Update the window title if we're the displayed tab.
              if (index == this.tabContainer.selectedIndex)
              {
                let docTitle = tabInfo.title;
                if (!/Mac/.test(navigator.platform))
                  docTitle += " - " + gBrandBundle.getString("brandFullName");
                document.title = docTitle;

                // Update the toolbar status - we don't need to do menus as they
                // do themselves when we open them.
                UpdateMailToolbar("tabmail");
              }
            }
          ]]>
        </body>
      </method>

      <method name="FillTabmailTooltip">
        <parameter name="aDocument"/>
        <parameter name="aEvent"/>
        <body>
          <![CDATA[
            aEvent.stopPropagation();
            let tn = aDocument.tooltipNode;
            if (tn.localName != "tab")
              return false; // Not a tab, so cancel the tooltip.
            if (tn.hasAttribute("label"))
            {
              aEvent.target.setAttribute("label", tn.getAttribute("label"));
              return true;
            }
            return false;
          ]]>
        </body>
      </method>

      <method name="onTabContextMenuShowing">
        <body>
          <![CDATA[
            // The user might right-click on a non-tab area of the tab strip.
            this.mContextTab = document.popupNode;
            return this.mContextTab.localName == "tab";
          ]]>
        </body>
      </method>

      <!-- getBrowserForSelectedTab is required as some toolkit functions
           require a getBrowser() function. -->
      <method name="getBrowserForSelectedTab">
        <body>
          <![CDATA[
            if (!this.currentTabInfo)
              this.currentTabInfo = this.tabInfo[0];
            let tabInfo = this.currentTabInfo;
            let browserFunc = tabInfo.mode.getBrowser ||
                              tabInfo.mode.tabType.getBrowser;
            if (!browserFunc)
              return null;
            return browserFunc.call(tabInfo.mode.tabType, tabInfo);
          ]]>
        </body>
      </method>

      <!-- nsIObserver implementation -->

      <method name="observe">
        <parameter name="aSubject"/>
        <parameter name="aTopic"/>
        <parameter name="aData"/>
        <body>
          <![CDATA[
            const kAutoHide = "browser.tabs.autoHide";
            if (aTopic == "nsPref:changed" && aData == kAutoHide)
              this.mAutoHide = this.mPrefs.getBoolPref(kAutoHide);
          ]]>
        </body>
      </method>

      <!-- nsIController implementation -->

      <method name="supportsCommand">
        <parameter name="aCommand"/>
        <body>
          <![CDATA[
            // return early on startup when we haven't got a tab loaded yet
            let tabInfo = this.currentTabInfo;
            if (!tabInfo)
              return false;

            let supportsCommandFunc = tabInfo.mode.supportsCommand ||
                                      tabInfo.mode.tabType.supportsCommand;
            if (!supportsCommandFunc)
              return false;
            return supportsCommandFunc.call(tabInfo.mode.tabType,
                                            aCommand,
                                            tabInfo);
          ]]>
        </body>
      </method>

      <method name="isCommandEnabled">
        <parameter name="aCommand"/>
        <body>
          <![CDATA[
            // return early on startup when we haven't got a tab loaded yet
            let tabInfo = this.currentTabInfo;
            if (!tabInfo)
              return false;

            let isCommandEnabledFunc = tabInfo.mode.isCommandEnabled ||
                                       tabInfo.mode.tabType.isCommandEnabled;
            if (!isCommandEnabledFunc)
              return false;
            return isCommandEnabledFunc.call(tabInfo.mode.tabType,
                                             aCommand,
                                             tabInfo);
          ]]>
        </body>
      </method>

      <method name="doCommand">
        <parameter name="aCommand"/>
        <body>
          <![CDATA[
            // return early on startup when we haven't got a tab loaded yet
            let tabInfo = this.currentTabInfo;
            if (!tabInfo)
              return;

            let doCommandFunc = tabInfo.mode.doCommand ||
                                tabInfo.mode.tabType.doCommand;
            if (!doCommandFunc)
              return;
            doCommandFunc.call(tabInfo.mode.tabType,
                               aCommand,
                               tabInfo);
           ]]>
        </body>
      </method>

      <method name="onEvent">
        <parameter name="aEvent"/>
        <body>
          <![CDATA[
            // return early on startup when we haven't got a tab loaded yet
            let tabInfo = this.currentTabInfo;
            if (!tabInfo)
              return;

            let onEventFunc = tabInfo.mode.onEvent ||
                              tabInfo.mode.tabType.onEvent;
            if (!onEventFunc)
              return;

            onEventFunc.call(tabInfo.mode.tabType, aCommand, tabInfo);
          ]]>
        </body>
      </method>
    </implementation>
  </binding>

  <binding id="tabmail-tab"
           display="xul:box"
           extends="chrome://global/content/bindings/tabbox.xml#tab">
    <content closetabtext="&tabmailClose.label;">
      <xul:hbox class="tab-middle box-inherit"
                xbl:inherits="align,dir,pack,orient,selected"
                flex="1">
        <xul:image class="tab-icon tab-icon-image" xbl:inherits="validate,src=image"/>
        <xul:label class="tab-text"
                   xbl:inherits="value=label,accesskey,crop,disabled"
                   flex="1"/>
      </xul:hbox>
      <xul:toolbarbutton anonid="close-button"
                         tooltiptext="&tabmailClose.tooltip;"
                         tabindex="-1"
                         class="tabs-closebutton tab-close-button"/>
    </content>

    <implementation>
      <field name="mCorrespondingMenuitem">null</field>
    </implementation>
  </binding>

  <binding id="tabmail-arrowscrollbox"
           extends="chrome://global/content/bindings/scrollbox.xml#arrowscrollbox-clicktoscroll">
    <content>
      <xul:toolbarbutton class="scrollbutton-up tab-scrollbutton-up"
                         collapsed="true"
                         xbl:inherits="orient"
                         anonid="scrollbutton-up"
                         onmousedown="_startScroll(-1);"
                         onmouseup="_stopScroll();"
                         onmouseout="_stopScroll();"/>
      <xul:scrollbox xbl:inherits="orient,align,pack,dir"
                     flex="1"
                     anonid="scrollbox">
        <children/>
      </xul:scrollbox>
      <xul:stack align="center" pack="end" class="scrollbutton-down-stack">
        <xul:hbox flex="1"
                  class="scrollbutton-down-box"
                  collapsed="true"
                  anonid="down-box"/>
        <xul:hbox flex="1"
                  class="scrollbutton-down-box-animate"
                  collapsed="true"
                  anonid="down-box-animate"/>
        <xul:toolbarbutton class="scrollbutton-down tab-scrollbutton-down"
                           collapsed="true"
                           xbl:inherits="orient"
                           anonid="scrollbutton-down"
                           onmousedown="_startScroll(1);"
                           onmouseup="_stopScroll();"
                           onmouseout="_stopScroll();"/>
      </xul:stack>
    </content>

    <implementation>
      <field name="_scrollButtonDownBox">
        document.getAnonymousElementByAttribute(this, "anonid", "down-box");
      </field>
      <field name="_scrollButtonDownBoxAnimate">
        document.getAnonymousElementByAttribute(this, "anonid", "down-box-animate");
      </field>
    </implementation>

    <handlers>
      <handler event="underflow" phase="target">
        <![CDATA[
          // Ignore vertical events.
          if (event.detail == 0)
            return;
          this._scrollButtonDownBox.collapsed = true;
          this._scrollButtonDownBoxAnimate.collapsed = true;
        ]]>
      </handler>

      <handler event="overflow" phase="target">
        <![CDATA[
          // Ignore vertical events.
          if (event.detail == 0)
            return;
          this._scrollButtonDownBox.collapsed = false;
          this._scrollButtonDownBoxAnimate.collapsed = false;
        ]]>
      </handler>

      <handler event="UpdatedScrollButtonsDisabledState">
        <![CDATA[
          // filter underflow events which were dispatched on nested scrollboxes
          if (event.target != this)
            return;

          // fix for bug #352353
          // unlike the scrollup button on the tab strip (which is a
          // simple toolbarbutton) the scrolldown button is
          // a more complicated stack of boxes and a toolbarbutton
          // so that we can animate when a tab is opened offscreen.
          // in order to style the box with the actual background image
          // we need to manually set the disable state to match the
          // disable state of the toolbarbutton.
          this._scrollButtonDownBox
              .setAttribute("disabled", this._scrollButtonDown.disabled);
        ]]>
      </handler>
    </handlers>
  </binding>

  <binding id="tabmail-tabs"
           extends="chrome://global/content/bindings/tabbox.xml#tabs">
    <content>
      <xul:stack flex="1" class="tabs-stack">
        <xul:vbox>
          <xul:spacer flex="1"/>
          <xul:hbox class="tabs-bottom" align="center"/>
        </xul:vbox>
        <xul:stack>
          <xul:spacer class="tabs-left tabs-right"/>
          <xul:hbox>
            <xul:hbox class="tabs-newbutton-box"
                      pack="start"
                      anonid="tabstrip-newbutton">
              <xul:toolbarbutton class="new-button tabs-newbutton"
                                 tooltiptext="&tabmailNewButton.tooltip;"/>
            </xul:hbox>
            <xul:arrowscrollbox anonid="arrowscrollbox"
                                class="tabbrowser-arrowscrollbox tabmail-arrowscrollbox"
                                flex="1"
                                xbl:inherits="smoothscroll"
                                orient="horizontal"
                                style="min-width: 1px;">
              <children includes="tab"/>
            </xul:arrowscrollbox>
            <children/>
            <xul:hbox class="tabs-closebutton-box"
                      align="center"
                      pack="end"
                      anonid="tabstrip-closebutton">
              <xul:toolbarbutton class="close-button tabs-closebutton"
                                 tooltiptext="&tabmailCloseButton.tooltip;"/>
            </xul:hbox>
            <xul:stack align="center" pack="end" class="tabs-alltabs-stack">
              <xul:hbox flex="1" class="tabs-alltabs-box" anonid="alltabs-box"/>
              <xul:hbox flex="1"
                        class="tabs-alltabs-box-animate"
                        anonid="alltabs-box-animate"/>
              <xul:toolbarbutton class="tabs-alltabs-button"
                                 type="menu"
                                 anonid="alltabs-button"
                                 tooltipstring="&tabmailAllTabs.tooltip;">
                <xul:menupopup class="tabs-alltabs-popup"
                               anonid="alltabs-popup"
                               position="after_end"/>
              </xul:toolbarbutton>
            </xul:stack>
          </xul:hbox>
        </xul:stack>
      </xul:stack>
    </content>

    <implementation implements="nsITimerCallback, nsIDOMEventListener, nsIObserver">
      <constructor>
        <![CDATA[
          this.mTabMinWidth  = this.mPrefs.getIntPref ("browser.tabs.tabMinWidth");
          this.mTabMaxWidth  = this.mPrefs.getIntPref ("browser.tabs.tabMaxWidth");
          this.mTabClipWidth = this.mPrefs.getIntPref ("browser.tabs.tabClipWidth");
          this.mCloseButtons = this.mPrefs.getIntPref ("browser.tabs.closeButtons");
          this.firstChild.minWidth = this.mTabMinWidth;
          this.firstChild.maxWidth = this.mTabMaxWidth;
          this.adjustTabstrip();
          this.mPrefs.addObserver("browser.tabs.", this, false);
          window.addEventListener("resize", this, false);

          // Listen to overflow/underflow events on the tabstrip,
          // we cannot put these as xbl handlers on the entire binding because
          // they would also get called for the all-tabs popup scrollbox.
          // Also, we can't rely on event.target because these are all
          // anonymous nodes.
          this.mTabstrip.addEventListener("overflow",  this, false);
          this.mTabstrip.addEventListener("underflow", this, false);
        ]]>
      </constructor>

      <destructor>
        <![CDATA[
          this.mPrefs.removeObserver("browser.tabs.", this);

          // Release timer to avoid reference cycles.
          if (this._animateTimer)
          {
            this._animateTimer.cancel();
            this._animateTimer = null;
          }
          this.mTabstrip.removeEventListener("overflow", this, false);
          this.mTabstrip.removeEventListener("underflow", this, false);
        ]]>
      </destructor>

      <field name="mPrefs">
        Components.classes["@mozilla.org/preferences-service;1"]
                  .getService(Components.interfaces.nsIPrefBranch);
      </field>

      <field name="mTabstripWidth">0</field>

      <field name="mTabstrip">
        document.getAnonymousElementByAttribute(this, "anonid", "arrowscrollbox");
      </field>

      <field name="mTabstripClosebutton">
        document.getAnonymousElementByAttribute(this, "anonid", "tabstrip-closebutton");
      </field>

      <field name="mTabMinWidth">100</field>
      <field name="mTabMaxWidth">250</field>
      <field name="mTabClipWidth">140</field>
      <field name="mCloseButtons">3</field>
      <method name="adjustTabstrip">
        <body>
          <![CDATA[
            // modes for tabstrip
            // 0 - activetab  = close button on active tab only
            // 1 - alltabs    = close buttons on all tabs
            // 2 - noclose    = no close buttons at all
            // 3 - closeatend = close button at the end of the tabstrip
            switch (this.mCloseButtons)
            {
              case 0:
                this.setAttribute("closebuttons", "activetab");
                break;
              case 1:
                let width = this.firstChild.boxObject.width;
                // 0 width is an invalid value and indicates
                // an item without display, so ignore.
                if (width > this.mTabClipWidth || width == 0)
                  this.setAttribute("closebuttons", "alltabs");
                else
                  this.setAttribute("closebuttons", "activetab");
                break;
              case 2:
                this.setAttribute("closebuttons", "noclose");
                break;
              case 3:
                this.setAttribute("closebuttons", "closeatend");
                break;
            }
            this.mTabstripClosebutton.collapsed = this.mCloseButtons != 3;
          ]]>
        </body>
      </method>

      <method name="_handleTabSelect">
        <body>
          <![CDATA[
            this.mTabstrip.ensureElementIsVisible(this.selectedItem);
          ]]>
        </body>
      </method>

      <method name="handleEvent">
        <parameter name="aEvent"/>
        <body>
          <![CDATA[
            switch (aEvent.type)
            {
              case "overflow":
                this.setAttribute("overflow", "true");
                this.mTabstrip.scrollBoxObject
                              .ensureElementIsVisible(this.selectedItem);
                break;
              case "underflow":
                this.removeAttribute("overflow");
                break;
              case "resize":
                let width = this.mTabstrip.boxObject.width;
                if (width != this.mTabstripWidth)
                {
                  this.adjustTabstrip();
                  // XXX without this line the tab bar won't budge
                  this.mTabstrip.scrollByPixels(1);
                  this._handleTabSelect();
                  this.mTabstripWidth = width;
                }
                break;
            }
          ]]>
        </body>
      </method>

      <field name="mAllTabsPopup">
        document.getAnonymousElementByAttribute(this, "anonid", "alltabs-popup");
      </field>

      <field name="mAllTabsBoxAnimate">
        document.getAnonymousElementByAttribute(this, "anonid", "alltabs-box-animate");
      </field>

      <field name="mDownBoxAnimate">
        this.mTabstrip._scrollButtonDownBoxAnimate;
      </field>

      <field name="mAllTabsButton">
        document.getAnonymousElementByAttribute(this, "anonid", "alltabs-button");
      </field>

      <field name="_animateTimer">null</field>
      <field name="_animateStep">-1</field>
      <field name="_animateDelay">25</field>
      <field name="_animatePercents">
       [1.00, 0.85, 0.80, 0.75, 0.71, 0.68, 0.65, 0.62, 0.59, 0.57,
        0.54, 0.52, 0.50, 0.47, 0.45, 0.44, 0.42, 0.40, 0.38, 0.37,
        0.35, 0.34, 0.32, 0.31, 0.30, 0.29, 0.28, 0.27, 0.26, 0.25,
        0.24, 0.23, 0.23, 0.22, 0.22, 0.21, 0.21, 0.21, 0.20, 0.20,
        0.20, 0.20, 0.20, 0.20, 0.20, 0.20, 0.19, 0.19, 0.19, 0.18,
        0.18, 0.17, 0.17, 0.16, 0.15, 0.14, 0.13, 0.11, 0.09, 0.06]
      </field>

      <method name="_stopAnimation">
        <body>
          <![CDATA[
            if (this._animateStep != -1)
            {
              if (this._animateTimer)
                this._animateTimer.cancel();

              this._animateStep = -1;
              this.mAllTabsBoxAnimate.style.opacity = 0.0;
              this.mDownBoxAnimate.style.opacity = 0.0;
            }
          ]]>
        </body>
      </method>

      <method name="_notifyBackgroundTab">
        <parameter name="aTabNode"/>
        <body>
          <![CDATA[
            let tsbo = this.mTabstrip.scrollBoxObject;
            let tsboStart = tsbo.screenX;
            let tsboEnd = tsboStart + tsbo.width;
            let ctbo = aTabNode.boxObject;
            let ctboStart = ctbo.screenX;
            let ctboEnd = ctboStart + ctbo.width;

            // only start the flash timer if the new tab (which was loaded in
            // the background) is not completely visible
            if (tsboStart > ctboStart || ctboEnd > tsboEnd)
            {
              this._animateStep = 0;

              if (!this._animateTimer)

                this._animateTimer =
                  Components.classes["@mozilla.org/timer;1"]
                            .createInstance(Components.interfaces.nsITimer);
              else
                 this._animateTimer.cancel();

              this._animateTimer.initWithCallback(this,
                           this._animateDelay,
                           Components.interfaces.nsITimer.TYPE_REPEATING_SLACK);
            }
          ]]>
        </body>
      </method>

      <method name="notify">
        <parameter name="aTimer"/>
        <body>
          <![CDATA[
            if (!document)
              aTimer.cancel();

            let percent = this._animatePercents[this._animateStep];
            this.mAllTabsBoxAnimate.style.opacity = percent;
            this.mDownBoxAnimate.style.opacity = percent;

            if (this._animateStep < (this._animatePercents.length - 1))
              this._animateStep++;
            else
              this._stopAnimation();
          ]]>
        </body>
      </method>

      <!-- nsIObserver implementation -->

      <method name="observe">
        <parameter name="aSubject"/>
        <parameter name="aTopic"/>
        <parameter name="aData"/>
        <body>
          <![CDATA[
            const kCloseButtons = "browser.tabs.closeButtons";
            if (aTopic == "nsPref:changed" && aData == kCloseButtons)
            {
              this.mCloseButtons = this.mPrefs.getIntPref(kCloseButtons);
              this.adjustTabstrip();
            }
          ]]>
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="TabSelect" action="this._handleTabSelect();"/>

      <handler event="mouseover">
        <![CDATA[
          if (event.originalTarget == this.mAllTabsButton)
          {
            this.mAllTabsButton
                .setAttribute("tooltiptext",
                              this.mAllTabsButton.getAttribute("tooltipstring"));
          }
          else
          {
            this.mAllTabsButton.removeAttribute("tooltiptext");
          }
        ]]>
      </handler>
    </handlers>
  </binding>

  <!-- alltabs-popup binding
       This binding relies on the structure of the tabbrowser binding.
       Therefore it should only be used as a child of the tabs element.
       This binding is exposed as a pseudo-public-API so themes can customize
       the tabbar appearance without having to be scriptable
       (see globalBindings.xml in osx for example).
  -->
  <binding id="tabmail-alltabs-popup"
           extends="chrome://global/content/bindings/popup.xml#popup">
    <implementation implements="nsIDOMEventListener">
      <method name="_tabOnTabClose">
        <parameter name="aEvent"/>
        <body>
          <![CDATA[
            let menuItem = aEvent.target.mCorrespondingMenuitem;
            if (menuItem)
              menuItem.remove();
          ]]>
        </body>
      </method>

      <method name="handleEvent">
        <parameter name="aEvent"/>
        <body>
          <![CDATA[
            switch (aEvent.type)
            {
              case "TabClose":
                this._tabOnTabClose(aEvent);
                break;
              case "TabOpen":
                this._createTabMenuItem(aEvent.originalTarget);
                break;
              case "scroll":
                this._updateTabsVisibilityStatus();
                break;
            }
          ]]>
        </body>
      </method>

      <method name="_updateTabsVisibilityStatus">
        <body>
          <![CDATA[
            let tabContainer = document.getBindingParent(this);
            let tabstripBO = tabContainer.mTabstrip.scrollBoxObject;

            for (let i = 0; i < this.childNodes.length; i++)
            {
              let curTabBO = this.childNodes[i].tab.boxObject;
              if (curTabBO.screenX >= tabstripBO.screenX &&
                  curTabBO.screenX + curTabBO.width <= tabstripBO.screenX + tabstripBO.width)
                this.childNodes[i].removeAttribute("tabIsScrolled");
              else
                this.childNodes[i].setAttribute("tabIsScrolled", "true");
            }
          ]]>
        </body>
      </method>

      <method name="_createTabMenuItem">
        <parameter name="aTabNode"/>
        <body>
          <![CDATA[
            let menuItem = document.createElementNS(
              "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
              "menuitem");
            menuItem.setAttribute("class", "menuitem-iconic alltabs-item icon-holder");
            menuItem.setAttribute("label", aTabNode.label);
            menuItem.setAttribute("crop",  aTabNode.getAttribute("crop"));
            menuItem.setAttribute("image", aTabNode.getAttribute("image"));

            let attributes = ["busy", "selected", "type", "NewMessages", "ServerType",
                              "SpecialFolder", "ImapShared", "BiffState", "IsServer",
                              "IsSecure", "Attachment", "IMAPDeleted", "Offline",
                              "MessageType"];

            attributes.forEach(
              function(attribute)
              {
                if (aTabNode.hasAttribute(attribute))
                {
                  menuItem.setAttribute(attribute, aTabNode.getAttribute(attribute));
                }
              }
            );

            // Keep some attributes of the menuitem in sync with its
            // corresponding tab (e.g. the tab label)
            aTabNode.mCorrespondingMenuitem = menuItem;
            document.addBroadcastListenerFor(aTabNode, menuItem, "label");
            document.addBroadcastListenerFor(aTabNode, menuItem, "crop");
            document.addBroadcastListenerFor(aTabNode, menuItem, "image");
            document.addBroadcastListenerFor(aTabNode, menuItem, "busy");
            document.addBroadcastListenerFor(aTabNode, menuItem, "selected");
            document.addBroadcastListenerFor(aTabNode, menuItem, "NewMessages");
            document.addBroadcastListenerFor(aTabNode, menuItem, "BiffState");
            aTabNode.addEventListener("TabClose", this, false);
            menuItem.tab = aTabNode;
            menuItem.addEventListener("command", this, false);
            this.appendChild(menuItem);
            return menuItem;
          ]]>
        </body>
      </method>
    </implementation>

    <handlers>
      <handler event="popupshowing">
        <![CDATA[
          // set up the menu popup
          let tabcontainer = document.getBindingParent(this);
          let tabs = tabcontainer.childNodes;

          // Listen for changes in the tab bar.
          let tabbrowser = document.getBindingParent(tabcontainer);
          tabbrowser.addEventListener("TabOpen", this, false);
          tabcontainer.mTabstrip.addEventListener("scroll", this, false);

          // if an animation is in progress and the user
          // clicks on the "all tabs" button, stop the animation
          tabcontainer._stopAnimation();

          for (let i = 0; i < tabs.length; i++)
            this._createTabMenuItem(tabs[i]);
          this._updateTabsVisibilityStatus();
        ]]>
      </handler>

      <handler event="popuphiding">
        <![CDATA[
          // clear out the menu popup and remove the listeners
          while (this.hasChildNodes())
          {
            let menuItem = this.lastChild;
            document.removeBroadcastListenerFor(menuItem.tab, menuItem, "label");
            document.removeBroadcastListenerFor(menuItem.tab, menuItem, "crop");
            document.removeBroadcastListenerFor(menuItem.tab, menuItem, "image");
            document.removeBroadcastListenerFor(menuItem.tab, menuItem, "busy");
            document.removeBroadcastListenerFor(menuItem.tab, menuItem, "selected");
            document.removeBroadcastListenerFor(menuItem.tab, menuItem, "NewMessages");
            document.removeBroadcastListenerFor(menuItem.tab, menuItem, "BiffState");
            menuItem.removeEventListener("command", this, false);
            menuItem.tab.removeEventListener("TabClose", this, false);
            menuItem.tab.mCorrespondingMenuitem = null;
            menuItem.remove();
          }
          let tabcontainer = document.getBindingParent(this);
          tabcontainer.mTabstrip.removeEventListener("scroll", this, false);
          document.getBindingParent(tabcontainer).removeEventListener("TabOpen", this, false);
        ]]>
      </handler>

      <handler event="command">
        <![CDATA[
          let tabcontainer = document.getBindingParent(this);
          tabcontainer.selectedItem = event.target.tab;
        ]]>
      </handler>
    </handlers>
  </binding>

  <!-- new-tab-button/close-tab-button binding
       These bindings rely on the structure of the tabbrowser binding.
       Therefore they should only be used as a child of the tab or the tabs
       element (in both cases, when they are anonymous nodes of <tabbrowser>).
       These bindings are exposed as pseudo-public-APIs, so themes can customize
       the tabbar appearance without having to be scriptable
       (see globalBindings.xml in osx for example).
  -->
  <binding id="tabmail-new-tab-button"
           extends="chrome://global/content/bindings/toolbarbutton.xml#toolbarbutton-image">
    <handlers>
      <handler event="command">
        <![CDATA[
          let bindingParent = document.getBindingParent(this);
          if (bindingParent)
          {
            let tabmail = document.getBindingParent(bindingParent);
            if (bindingParent.localName == "tabs")
            {
              // new-tab-button only appears in the tabstrip
              // duplicate the current tab
              tabmail.openTab();
            }
          }
        ]]>
      </handler>
      <handler event="dblclick" button="0" phase="capturing">
        <![CDATA[
          event.stopPropagation();
        ]]>
      </handler>
    </handlers>
  </binding>

  <binding id="tabmail-close-tab-button"
           extends="chrome://global/content/bindings/toolbarbutton.xml#toolbarbutton-image">
    <handlers>
      <handler event="command">
        <![CDATA[
          let bindingParent = document.getBindingParent(this);
          if (bindingParent)
          {
            let tabmail = document.getBindingParent(bindingParent);
            if (bindingParent.localName == "tab")
            {
              /* The only sequence in which a second click event (i.e. dblclik)
               * can be dispatched on an in-tab close button is when it is shown
               * after the first click (i.e. the first click event was dispatched
               * on the tab). This happens when we show the close button only on
               * the active tab. (bug 352021)
               * The only sequence in which a third click event can be dispatched
               * on an in-tab close button is when the tab was opened with a
               * double click on the tabbar. (bug 378344)
               * In both cases, it is most likely that the close button area has
               * been accidentally clicked, therefore we do not close the tab.
               */
              if (event.detail > 1)
                return;

              tabmail.removeTab(bindingParent);
              tabmail._blockDblClick = true;

              /* XXXmano hack (see bug 343628):
               * Since we're removing the event target, if the user
               * double-clicks this button, the dblclick event will be dispatched
               * with the tabbar as its event target (and explicit/originalTarget),
               * which treats that as a mouse gesture for opening a new tab.
               * In this context, we're manually blocking the dblclick event
               * (see onTabBarDblClick).
               */
              let clickedOnce = false;
              function enableDblClick(event)
              {
                var target = event.originalTarget;
                if (target.className == "tab-close-button")
                  target._ignoredClick = true;
                if (!clickedOnce)
                {
                  clickedOnce = true;
                  return;
                }
                tabContainer._blockDblClick = false;
                tabContainer.removeEventListener("click", enableDblClick, true);
              }
              tabContainer.addEventListener("click", enableDblClick, true);
            }
            else
            {
              // "tabs"
              tabmail.removeCurrentTab();
            }
          }
        ]]>
      </handler>
      <handler event="dblclick" button="0" phase="capturing">
        <![CDATA[
          // for the one-close-button case
          event.stopPropagation();
        ]]>
      </handler>
    </handlers>
  </binding>

</bindings>