<?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/. -->

<!-- this file depends on printUtils.js -->

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

<bindings id="printPreviewBindings"
   xmlns="http://www.mozilla.org/xbl"
   xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">

  <binding id="printpreviewtoolbar" 
           extends="chrome://global/content/bindings/toolbar.xml#toolbar">
    <resources>
      <stylesheet src="chrome://global/skin/printPreview.css"/>
    </resources>

    <content>
      <xul:button label="&print.label;" accesskey="&print.accesskey;"
        oncommand="PrintUtils.print();" icon="print"/>

      <xul:button label="&pageSetup.label;" accesskey="&pageSetup.accesskey;"
        oncommand="this.parentNode.doPageSetup();"/>

      <xul:vbox align="center" pack="center">
        <xul:label value="&page.label;" accesskey="&page.accesskey;" control="pageNumber"/>
      </xul:vbox>
      <xul:toolbarbutton class="home-arrow tabbable"
        oncommand="parentNode.navigate(0, 0, 'home');" tooltiptext="&homearrow.tooltip;"/>
      <xul:toolbarbutton class="previous-arrow tabbable"
        oncommand="parentNode.navigate(-1, 0, 0);" tooltiptext="&previousarrow.tooltip;"/>
      <xul:hbox align="center" pack="center">
        <xul:textbox id="pageNumber" size="3" value="1" min="1" type="number"
          hidespinbuttons="true" onchange="navigate(0, this.valueNumber, 0);"/>
        <xul:label value="&of.label;"/>
        <xul:label value="1"/>
      </xul:hbox>
      <xul:toolbarbutton class="next-arrow tabbable"
        oncommand="parentNode.navigate(1, 0, 0);" tooltiptext="&nextarrow.tooltip;"/>
      <xul:toolbarbutton class="end-arrow tabbable"
        oncommand="parentNode.navigate(0, 0, 'end');" tooltiptext="&endarrow.tooltip;"/>

      <xul:toolbarseparator class="toolbarseparator-primary"/>
      <xul:vbox align="center" pack="center">
        <xul:label value="&scale.label;" accesskey="&scale.accesskey;" control="scale"/>
      </xul:vbox>

      <xul:hbox align="center" pack="center">
        <xul:menulist id="scale" crop="none" 
          oncommand="parentNode.parentNode.scale(this.selectedItem.value);">
          <xul:menupopup>
            <xul:menuitem value="0.3" label="&p30.label;"/>
            <xul:menuitem value="0.4" label="&p40.label;"/>
            <xul:menuitem value="0.5" label="&p50.label;"/>
            <xul:menuitem value="0.6" label="&p60.label;"/>
            <xul:menuitem value="0.7" label="&p70.label;"/>
            <xul:menuitem value="0.8" label="&p80.label;"/>
            <xul:menuitem value="0.9" label="&p90.label;"/>
            <xul:menuitem value="1" label="&p100.label;"/>
            <xul:menuitem value="1.25" label="&p125.label;"/>
            <xul:menuitem value="1.5" label="&p150.label;"/>
            <xul:menuitem value="1.75" label="&p175.label;"/>
            <xul:menuitem value="2" label="&p200.label;"/>
            <xul:menuseparator/>
            <xul:menuitem flex="1" value="ShrinkToFit" 
              label="&ShrinkToFit.label;"/>
            <xul:menuitem value="Custom" label="&Custom.label;"/>
          </xul:menupopup>
        </xul:menulist>
      </xul:hbox>

      <xul:toolbarseparator class="toolbarseparator-primary"/>
      <xul:hbox align="center" pack="center">
        <xul:toolbarbutton label="&portrait.label;" checked="true"
          accesskey="&portrait.accesskey;"
          type="radio" group="orient" class="toolbar-portrait-page tabbable"
          oncommand="parentNode.parentNode.orient('portrait');"/>
        <xul:toolbarbutton label="&landscape.label;" 
          accesskey="&landscape.accesskey;"
          type="radio" group="orient" class="toolbar-landscape-page tabbable"
          oncommand="parentNode.parentNode.orient('landscape');"/>
      </xul:hbox>

      <xul:toolbarseparator class="toolbarseparator-primary"/>
      <xul:button label="&close.label;" accesskey="&close.accesskey;"
        oncommand="PrintUtils.exitPrintPreview();" icon="close"/>
      <xul:data value="&customPrompt.title;"/>
    </content>

    <implementation>
      <field name="mPrintButton">
        document.getAnonymousNodes(this)[0]
      </field>
      <field name="mPageTextBox">
        document.getAnonymousNodes(this)[5].childNodes[0]
      </field>
      <field name="mTotalPages">
        document.getAnonymousNodes(this)[5].childNodes[2]
      </field>
      <field name="mScaleLabel">
        document.getAnonymousNodes(this)[9].firstChild
      </field>
      <field name="mScaleCombobox">
        document.getAnonymousNodes(this)[10].firstChild
      </field>
      <field name="mOrientButtonsBox">
        document.getAnonymousNodes(this)[12]
      </field>
      <field name="mPortaitButton">
        this.mOrientButtonsBox.childNodes[0]
      </field>
      <field name="mLandscapeButton">
        this.mOrientButtonsBox.childNodes[1]
      </field>
      <field name="mCustomTitle">
        document.getAnonymousNodes(this)[15].firstChild
      </field>
      <field name="mPrintPreviewObs">
      </field>
      <field name="mWebProgress">
      </field>
     
      <constructor>
      <![CDATA[
        var print = PrintUtils.getPrintPreview();
        this.mTotalPages.value = print.printPreviewNumPages;
        this.mPageTextBox.max = print.printPreviewNumPages;

        this.updateToolbar();
      ]]>
      </constructor>

      <method name="doPageSetup">
        <body>
        <![CDATA[
          var didOK = PrintUtils.showPageSetup();
          if (didOK) {
            // the changes that effect the UI
            this.updateToolbar();

            // Now do PrintPreview
            PrintUtils.printPreview();
          }
        ]]>
        </body>
      </method>

      <method name="navigate">
        <parameter name="aDirection"/>
        <parameter name="aPageNum"/>
        <parameter name="aHomeOrEnd"/>
        <body>
        <![CDATA[          
          var print = PrintUtils.getPrintPreview();

          // we use only one of aHomeOrEnd, aDirection, or aPageNum
          if (aHomeOrEnd)
          {
            var homeOrEnd;
            if (aHomeOrEnd == "home")
            {
              homeOrEnd = print.PRINTPREVIEW_HOME;
              this.mPageTextBox.value = 1;  
            }
            else
            {
              homeOrEnd = print.PRINTPREVIEW_END;
              this.mPageTextBox.value = print.printPreviewNumPages;
            }
            
            print.printPreviewNavigate(homeOrEnd, 0);
          }
          else if (aDirection)
          {
            this.mPageTextBox.valueNumber += aDirection;
            print.printPreviewNavigate(
              print.PRINTPREVIEW_GOTO_PAGENUM,
              this.mPageTextBox.valueNumber);
          }
          else 
          {
            print.printPreviewNavigate(
              print.PRINTPREVIEW_GOTO_PAGENUM, aPageNum);
          }
        ]]>
        </body>
      </method>

      <method name="promptForScaleValue">
        <parameter name="aValue"/>
        <body>
        <![CDATA[
          var value = Math.round(aValue);
          var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
          var promptStr = this.mScaleLabel.value;
          var renameTitle = this.mCustomTitle;
          var result = {value:value};
          var confirmed = promptService.prompt(window, renameTitle, promptStr, result, null, {value:value});
          if (!confirmed || (!result.value) || (result.value == "") || result.value == value) {
            return(-1);
          }
          return result.value;
        ]]>
        </body>
      </method>

      <method name="setScaleCombobox">
        <parameter name="aValue"/>
        <body>
        <![CDATA[
          var scaleValues = [0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.25, 1.5, 1.75, 2];

          aValue = new Number(aValue);

          for (var i = 0; i < scaleValues.length; i++) {
            if (aValue == scaleValues[i]) {
              this.mScaleCombobox.selectedIndex = i;
              return;
            }
          }
          this.mScaleCombobox.value = "Custom";
        ]]>
        </body>
      </method>

      <method name="scale">
        <parameter name="aValue"/>
        <body>
        <![CDATA[
          var settings = PrintUtils.getPrintSettings();
          if (aValue == "ShrinkToFit") {
            if (!settings.shrinkToFit) {
              settings.shrinkToFit = true;
              this.savePrintSettings(settings, settings.kInitSaveShrinkToFit | settings.kInitSaveScaling);
              PrintUtils.printPreview();
            }
            return;
          }

          if (aValue == "Custom") {
            aValue = this.promptForScaleValue(settings.scaling * 100.0);
            if (aValue >= 10) {
              aValue /= 100.0;
            } else {
              if (this.mScaleCombobox.hasAttribute('lastValidInx')) {
                this.mScaleCombobox.selectedIndex = this.mScaleCombobox.getAttribute('lastValidInx');
              }
              return;
            }
          }

          this.setScaleCombobox(aValue);
          this.mScaleCombobox.setAttribute('lastValidInx', this.mScaleCombobox.selectedIndex);

          if (settings.scaling != aValue || settings.shrinkToFit)
          {
            settings.shrinkToFit = false;
            settings.scaling = aValue;
            this.savePrintSettings(settings, settings.kInitSaveShrinkToFit | settings.kInitSaveScaling);
            PrintUtils.printPreview();
          }
        ]]>
        </body>
      </method>

      <method name="orient">
        <parameter name="aOrientation"/>
        <body>
        <![CDATA[
          const kIPrintSettings = Components.interfaces.nsIPrintSettings;
          var orientValue = (aOrientation == "portrait") ? kIPrintSettings.kPortraitOrientation :
                                                           kIPrintSettings.kLandscapeOrientation;
          var settings = PrintUtils.getPrintSettings();
          if (settings.orientation != orientValue)
          {
            settings.orientation = orientValue;
            this.savePrintSettings(settings, settings.kInitSaveOrientation);
            PrintUtils.printPreview();
          }
        ]]>
        </body>
      </method>

      <method name="updateToolbar">
        <body>
        <![CDATA[
          var print = PrintUtils.getPrintPreview();
          var settings = PrintUtils.getPrintSettings();

          var isPortrait = settings.orientation == Components.interfaces.nsIPrintSettings.kPortraitOrientation;

          this.mPortaitButton.checked = isPortrait;
          this.mLandscapeButton.checked = !isPortrait;

          if (settings.shrinkToFit) {
            this.mScaleCombobox.value = "ShrinkToFit";
          } else {
            this.setScaleCombobox(settings.scaling);
          }

          this.mTotalPages.value = print.printPreviewNumPages;
          this.mPageTextBox.max = print.printPreviewNumPages;
          this.mPageTextBox.value = 1;
        ]]>
        </body>
      </method>

      <method name="savePrintSettings">
        <parameter name="settings"/>
        <parameter name="flags"/>
        <body><![CDATA[
          var PSSVC = Components.classes["@mozilla.org/gfx/printsettings-service;1"]
                                .getService(Components.interfaces.nsIPrintSettingsService);
          PSSVC.savePrintSettingsToPrefs(settings, true, flags);
        ]]></body>
      </method>
    </implementation>
  </binding>

</bindings>
