home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / viewZoomOverlay.js < prev    next >
Text File  |  2003-06-08  |  9KB  |  298 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  
  3.  * The contents of this file are subject to the Mozilla Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/MPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is this file as it was released upon
  14.  * January 6, 2001.
  15.  *
  16.  * The Initial Developer of the Original Code is Peter Annema.
  17.  * Portions created by Peter Annema are Copyright (C) 2000
  18.  * Peter Annema. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Peter Annema <disttsc@bart.nl> (Original Author)
  22.  *   Jonas Sicking <sicking@bigfoot.com>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the
  25.  * terms of the GNU General Public License Version 2 or later (the
  26.  * "GPL"), in which case the provisions of the GPL are applicable
  27.  * instead of those above.  If you wish to allow use of your
  28.  * version of this file only under the terms of the GPL and not to
  29.  * allow others to use your version of this file under the MPL,
  30.  * indicate your decision by deleting the provisions above and
  31.  * replace them with the notice and other provisions required by
  32.  * the GPL.  If you do not delete the provisions above, a recipient
  33.  * may use your version of this file under either the MPL or the
  34.  * GPL.
  35.  */
  36.  
  37. /** Document Zoom Management Code
  38.  *
  39.  * To use this, you'll need to have a <menu id="menu_textZoom"/>
  40.  * and a getMarkupDocumentViewer() function which returns a
  41.  * nsIMarkupDocumentViewer.
  42.  *
  43.  **/
  44.  
  45. function ZoomManager() {
  46.   this.bundle = document.getElementById("bundle_viewZoom");
  47.  
  48.   // factorAnchor starts on factorOther
  49.   this.factorOther = parseInt(this.bundle.getString("valueOther"));
  50.   this.factorAnchor = this.factorOther;
  51. }
  52.  
  53. ZoomManager.prototype = {
  54.   instance : null,
  55.  
  56.   getInstance : function() {
  57.     if (!ZoomManager.prototype.instance)
  58.       ZoomManager.prototype.instance = new ZoomManager();
  59.  
  60.     return ZoomManager.prototype.instance;
  61.   },
  62.  
  63.   MIN : 1,
  64.   MAX : 2000,
  65.  
  66.   bundle : null,
  67.  
  68.   zoomFactorsString : "", // cache
  69.   zoomFactors : null,
  70.  
  71.   factorOther : 300,
  72.   factorAnchor : 300,
  73.   steps : 0,
  74.  
  75.   get textZoom() {
  76.     var currentZoom;
  77.     try {
  78.       currentZoom = Math.round(getMarkupDocumentViewer().textZoom * 100);
  79.       if (this.indexOf(currentZoom) == -1) {
  80.         if (currentZoom != this.factorOther) {
  81.           this.factorOther = currentZoom;
  82.           this.factorAnchor = this.factorOther;
  83.         }
  84.       }
  85.     } catch (e) {
  86.       currentZoom = 100;
  87.     }
  88.     return currentZoom;
  89.   },
  90.  
  91.   set textZoom(aZoom) {
  92.     if (aZoom < this.MIN || aZoom > this.MAX)
  93.       throw Components.results.NS_ERROR_INVALID_ARG;
  94.  
  95.     getMarkupDocumentViewer().textZoom = aZoom / 100;
  96.   },
  97.  
  98.   enlarge : function() {
  99.     this.jump(1);
  100.   },
  101.  
  102.   reduce : function() {
  103.     this.jump(-1);
  104.   },
  105.  
  106.   reset : function() {
  107.     this.textZoom = 100;
  108.   },
  109.  
  110.   getZoomFactors : function() {
  111.     this.ensureZoomFactors();
  112.  
  113.     return this.zoomFactors;
  114.   },
  115.  
  116.   indexOf : function(aZoom) {
  117.     this.ensureZoomFactors();
  118.  
  119.     var index = -1;
  120.     if (this.isZoomInRange(aZoom)) {
  121.       index = this.zoomFactors.length - 1;
  122.       while (index >= 0 && this.zoomFactors[index] != aZoom)
  123.         --index;
  124.     }
  125.  
  126.     return index;
  127.   },
  128.  
  129.   /***** internal helper functions below here *****/
  130.  
  131.   ensureZoomFactors : function() {
  132.     var zoomFactorsString = this.bundle.getString("values");
  133.     if (this.zoomFactorsString != zoomFactorsString) {
  134.       this.zoomFactorsString = zoomFactorsString;
  135.       this.zoomFactors = zoomFactorsString.split(",");
  136.       for (var i = 0; i<this.zoomFactors.length; ++i)
  137.         this.zoomFactors[i] = parseInt(this.zoomFactors[i]);
  138.     }
  139.   },
  140.  
  141.   isLevelInRange : function(aLevel) {
  142.     return (aLevel >= 0 && aLevel < this.zoomFactors.length);
  143.   },
  144.  
  145.   isZoomInRange : function(aZoom) {
  146.     return (aZoom >= this.zoomFactors[0] && aZoom <= this.zoomFactors[this.zoomFactors.length - 1]);
  147.   },
  148.  
  149.   jump : function(aDirection) {
  150.     if (aDirection != -1 && aDirection != 1)
  151.       throw Components.results.NS_ERROR_INVALID_ARG;
  152.  
  153.     this.ensureZoomFactors();
  154.  
  155.     var currentZoom = this.textZoom;
  156.     var insertIndex = -1;
  157.     var stepFactor = parseFloat(this.bundle.getString("stepFactor"));
  158.  
  159.     // temporarily add factorOther to list
  160.     if (this.isZoomInRange(this.factorOther)) {
  161.       insertIndex = 0;
  162.       while (this.zoomFactors[insertIndex] < this.factorOther)
  163.         ++insertIndex;
  164.  
  165.       if (this.zoomFactors[insertIndex] != this.factorOther)
  166.         this.zoomFactors.splice(insertIndex, 0, this.factorOther);
  167.     }
  168.  
  169.     var factor;
  170.     var done = false;
  171.  
  172.     if (this.isZoomInRange(currentZoom)) {
  173.       var index = this.indexOf(currentZoom);
  174.       if (aDirection == -1 && index == 0 ||
  175.           aDirection ==  1 && index == this.zoomFactors.length - 1) {
  176.         this.steps = 0;
  177.         this.factorAnchor = this.zoomFactors[index];
  178.       } else {
  179.         factor = this.zoomFactors[index + aDirection];
  180.         done = true;
  181.       }
  182.     }
  183.  
  184.     if (!done) {
  185.       this.steps += aDirection;
  186.       factor = this.factorAnchor * Math.pow(stepFactor, this.steps);
  187.       if (factor < this.MIN || factor > this.MAX) {
  188.         this.steps -= aDirection;
  189.         factor = this.factorAnchor * Math.pow(stepFactor, this.steps);
  190.       }
  191.       factor = Math.round(factor);
  192.       if (this.isZoomInRange(factor))
  193.         factor = this.snap(factor);
  194.       else
  195.         this.factorOther = factor;
  196.     }
  197.  
  198.     if (insertIndex != -1)
  199.       this.zoomFactors.splice(insertIndex, 1);
  200.  
  201.     this.textZoom = factor;
  202.   },
  203.  
  204.   snap : function(aZoom) {
  205.     if (this.isZoomInRange(aZoom)) {
  206.       var level = 0;
  207.       while (this.zoomFactors[level + 1] < aZoom)
  208.         ++level;
  209.  
  210.       // if aZoom closer to [level + 1] than [level], snap to [level + 1]
  211.       if ((this.zoomFactors[level + 1] - aZoom) < (aZoom - this.zoomFactors[level]))
  212.         ++level;
  213.  
  214.       aZoom = this.zoomFactors[level];
  215.     }
  216.  
  217.     return aZoom;
  218.   }
  219. }
  220.  
  221. /***** init and helper functions for viewZoomOverlay.xul *****/
  222. window.addEventListener("load", registerZoomManager, false);
  223.  
  224. function registerZoomManager()
  225. {
  226.   var textZoomMenu = document.getElementById("menu_textZoom");
  227.   var zoom = ZoomManager.prototype.getInstance();
  228.  
  229.   var parentMenu = textZoomMenu.parentNode;
  230.   parentMenu.addEventListener("popupshowing", updateViewMenu, false);
  231.  
  232.   var insertBefore = document.getElementById("menu_textZoomInsertBefore");
  233.   var popup = insertBefore.parentNode;
  234.   var accessKeys = zoom.bundle.getString("accessKeys").split(",");
  235.   var zoomFactors = zoom.getZoomFactors();
  236.   for (var i = 0; i < zoomFactors.length; ++i) {
  237.     var menuItem = document.createElement("menuitem");
  238.     menuItem.setAttribute("type", "radio");
  239.     menuItem.setAttribute("name", "textZoom");
  240.  
  241.     var label;
  242.     if (zoomFactors[i] == 100)
  243.       label = zoom.bundle.getString("labelOriginal");
  244.     else
  245.       label = zoom.bundle.getString("label");
  246.  
  247.     menuItem.setAttribute("label", label.replace(/%zoom%/, zoomFactors[i]));
  248.     menuItem.setAttribute("accesskey", accessKeys[i]);
  249.     menuItem.setAttribute("oncommand", "ZoomManager.prototype.getInstance().textZoom = this.value;");
  250.     menuItem.setAttribute("value", zoomFactors[i]);
  251.     popup.insertBefore(menuItem, insertBefore);
  252.   }
  253. }
  254.  
  255. function updateViewMenu()
  256. {
  257.   var zoom = ZoomManager.prototype.getInstance();
  258.  
  259.   var textZoomMenu = document.getElementById("menu_textZoom");
  260.   var menuLabel = zoom.bundle.getString("menuLabel").replace(/%zoom%/, zoom.textZoom);
  261.   textZoomMenu.setAttribute("label", menuLabel);
  262. }
  263.  
  264. function updateTextZoomMenu()
  265. {
  266.   var zoom = ZoomManager.prototype.getInstance();
  267.  
  268.   var currentZoom = zoom.textZoom;
  269.  
  270.   var textZoomOther = document.getElementById("menu_textZoomOther");
  271.   var label = zoom.bundle.getString("labelOther");
  272.   textZoomOther.setAttribute("label", label.replace(/%zoom%/, zoom.factorOther));
  273.   textZoomOther.setAttribute("value", zoom.factorOther);
  274.  
  275.   var popup = document.getElementById("menu_textZoomPopup");
  276.   var item = popup.firstChild;
  277.   while (item) {
  278.     if (item.getAttribute("name") == "textZoom") {
  279.       if (item.getAttribute("value") == currentZoom)
  280.         item.setAttribute("checked","true");
  281.       else
  282.         item.removeAttribute("checked");
  283.     }
  284.     item = item.nextSibling;
  285.   }
  286. }
  287.  
  288. function setTextZoomOther()
  289. {
  290.   var zoom = ZoomManager.prototype.getInstance();
  291.  
  292.   // open dialog and ask for new value
  293.   var o = {value: zoom.factorOther, zoomMin: zoom.MIN, zoomMax: zoom.MAX};
  294.   window.openDialog("chrome://communicator/content/askViewZoom.xul", "AskViewZoom", "chrome,modal,titlebar", o);
  295.   if (o.zoomOK)
  296.     zoom.textZoom = o.value;
  297. }
  298.