home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / toolkit.jar / content / global / bindings / dialog.xml < prev    next >
Extensible Markup Language  |  2005-07-29  |  13KB  |  349 lines

  1. <?xml version="1.0"?>
  2.  
  3. <bindings id="dialogBindings"
  4.           xmlns="http://www.mozilla.org/xbl"
  5.           xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  6.           xmlns:xbl="http://www.mozilla.org/xbl">
  7.  
  8.   <binding id="dialog-base">
  9.     <resources>
  10.       <stylesheet src="chrome://global/skin/dialog.css"/>
  11.     </resources>
  12.   </binding>
  13.   
  14.   <binding id="dialog" extends="chrome://global/content/bindings/dialog.xml#dialog-base">
  15.     <content>
  16.       <xul:vbox class="box-inherit dialog-content-box" flex="1">
  17.         <children/>
  18.       </xul:vbox>
  19.           
  20.       <xul:hbox class="dialog-button-box" anonid="buttons"
  21.                 xbl:inherits="pack=buttonpack,align=buttonalign,dir=buttondir,orient=buttonorient"
  22.                 pack="end">
  23.         <xul:button dlgtype="extra2" class="dialog-button" hidden="true"/>
  24.         <xul:spacer anonid="spacer" flex="1" hidden="true"/>
  25.         <xul:button dlgtype="accept" class="dialog-button"/>
  26.         <xul:button dlgtype="extra1" class="dialog-button" hidden="true"/>
  27.         <xul:button dlgtype="cancel" class="dialog-button"/>
  28.         <xul:button dlgtype="help" class="dialog-button" hidden="true"/>
  29.         <xul:button dlgtype="disclosure" class="dialog-button" hidden="true"/>
  30.       </xul:hbox>
  31.     </content>
  32.  
  33.     <implementation>
  34.       <field name="_mStrBundle">null</field>
  35.       <field name="_closeHandler">(function(event) {
  36.         if (!document.documentElement.cancelDialog())
  37.           event.preventDefault();
  38.       })</field>
  39.       <field name="enterDefaultAlways">
  40.         false
  41.       </field>
  42.  
  43.       <property name="buttons"
  44.                 onget="return this.getAttribute('buttons');"
  45.                 onset="this._configureButtons(val); return val;"/>
  46.  
  47.       <method name="acceptDialog">
  48.         <body>
  49.         <![CDATA[
  50.           return this._doButtonCommand("accept");
  51.         ]]>
  52.         </body>
  53.       </method>
  54.       
  55.       <method name="cancelDialog">
  56.         <body>
  57.         <![CDATA[
  58.           return this._doButtonCommand("cancel");
  59.         ]]>
  60.         </body>
  61.       </method>
  62.       
  63.       <method name="getButton">
  64.         <parameter name="aDlgType"/>
  65.         <body>
  66.         <![CDATA[
  67.           return this._buttons[aDlgType];
  68.         ]]>
  69.         </body>
  70.       </method>
  71.  
  72.       <method name="moveToAlertPosition">
  73.         <body>
  74.         <![CDATA[
  75.           // hack. we need this so the window has something like its final size
  76.           if (window.outerWidth == 1) {
  77.             dump("Trying to position a sizeless window; caller should have called sizeToContent() or sizeTo(). See bug 75649.\n");
  78.             sizeToContent();
  79.           }
  80.  
  81.           var xOffset = (opener.outerWidth - window.outerWidth) / 2;
  82.           var yOffset = opener.outerHeight / 5;
  83.  
  84.           var newX = opener.screenX + xOffset;
  85.           var newY = opener.screenY + yOffset;
  86.  
  87.           // ensure the window is fully onscreen (if smaller than the screen)
  88.           if (newX < screen.availLeft)
  89.             newX = screen.availLeft + 20;
  90.           if ((newX + window.outerWidth) > (screen.availLeft + screen.availWidth))
  91.             newX = (screen.availLeft + screen.availWidth) - window.outerWidth - 20;
  92.  
  93.           if (newY < screen.availTop)
  94.             newY = screen.availTop + 20;
  95.           if ((newY + window.outerHeight) > (screen.availTop + screen.availHeight))
  96.             newY = (screen.availTop + screen.availHeight) - window.outerHeight - 60;
  97.  
  98.           window.moveTo( newX, newY );
  99.         ]]>
  100.         </body>
  101.       </method>
  102.  
  103.       <method name="centerWindowOnScreen">
  104.         <body>
  105.         <![CDATA[
  106.           var xOffset = screen.availWidth/2 - window.outerWidth/2;
  107.           var yOffset = screen.availHeight/2 - window.outerHeight/2; //(opener.outerHeight *2)/10;
  108.   
  109.           xOffset = xOffset > 0 ? xOffset : 0;
  110.           yOffset = yOffset > 0 ? yOffset : 0;
  111.           window.moveTo(xOffset, yOffset);
  112.         ]]>
  113.         </body>
  114.       </method>
  115.  
  116.       <constructor>
  117.       <![CDATA[
  118.         this._configureButtons(this.buttons);
  119.  
  120.         // listen for when window is closed via native close buttons
  121.         window.addEventListener("close", this._closeHandler, false);
  122.  
  123.         // for things that we need to initialize after onload fires
  124.         window.addEventListener("load", this.postLoadInit, false);
  125.  
  126.         window.moveToAlertPosition = this.moveToAlertPosition;
  127.         window.centerWindowOnScreen = this.centerWindowOnScreen;
  128.       ]]>
  129.       </constructor>
  130.  
  131.       <method name="postLoadInit">
  132.         <parameter name="aEvent"/>
  133.         <body>
  134.         <![CDATA[
  135.           var focusInit = 
  136.             function() {
  137.               // give focus to the first focusable element in the dialog
  138.               if (!document.commandDispatcher.focusedElement)
  139.                 document.commandDispatcher.advanceFocusIntoSubtree(document.documentElement);
  140.             };
  141.  
  142.           // Give focus after onload completes, see bug 103197.
  143.           setTimeout(focusInit, 0);
  144.         ]]>
  145.         </body>
  146.       </method>                
  147.  
  148.       <property name="mStrBundle">
  149.         <getter>
  150.         <![CDATA[
  151.           if (!this._mStrBundle) {
  152.             // need to create string bundle manually instead of using <xul:stringbundle/>
  153.             // see bug 63370 for details
  154.             var localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"]
  155.                                   .getService(Components.interfaces.nsILocaleService);
  156.             var stringBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"]
  157.                                   .getService(Components.interfaces.nsIStringBundleService);
  158.             var bundleURL = "chrome://global/locale/dialog.properties";
  159.             this._mStrBundle = stringBundleService.createBundle(bundleURL, localeService.getApplicationLocale());
  160.           }
  161.           return this._mStrBundle;
  162.         ]]></getter>
  163.       </property>
  164.       
  165.       <method name="_configureButtons">
  166.         <parameter name="aButtons"/>
  167.         <body>
  168.         <![CDATA[
  169.           // by default, get all the anonymous button elements
  170.           var buttons = {};
  171.           this._buttons = buttons;
  172.           buttons.accept = document.getAnonymousElementByAttribute(this, "dlgtype", "accept");
  173.           buttons.cancel = document.getAnonymousElementByAttribute(this, "dlgtype", "cancel");
  174.           buttons.extra1 = document.getAnonymousElementByAttribute(this, "dlgtype", "extra1");
  175.           buttons.extra2 = document.getAnonymousElementByAttribute(this, "dlgtype", "extra2");
  176.           buttons.help = document.getAnonymousElementByAttribute(this, "dlgtype", "help");
  177.           buttons.disclosure = document.getAnonymousElementByAttribute(this, "dlgtype", "disclosure");
  178.  
  179.           // look for any overriding explicit button elements
  180.           var exBtns = this.getElementsByAttribute("dlgtype", "*");
  181.           var dlgtype;
  182.           var i;
  183.           for (i = 0; i < exBtns.length; ++i) {
  184.             dlgtype = exBtns[i].getAttribute("dlgtype");
  185.             buttons[dlgtype].hidden = true; // hide the anonymous button
  186.             buttons[dlgtype] = exBtns[i];
  187.           }
  188.  
  189.           // add the label and oncommand handler to each button
  190.           for (dlgtype in buttons) {
  191.             var button = buttons[dlgtype];
  192.             button.addEventListener("command", this._handleButtonCommand, true);
  193.             
  194.  
  195.             // don't override custom labels with pre-defined labels on explicit buttons
  196.             if (!button.hasAttribute("label")) {
  197.               // dialog attributes override the default labels in dialog.properties
  198.               if (this.hasAttribute("buttonlabel"+dlgtype)) {
  199.                 button.setAttribute("label", this.getAttribute("buttonlabel"+dlgtype));
  200.                 if (this.hasAttribute("buttonaccesskey"+dlgtype))
  201.                   button.setAttribute("accesskey", this.getAttribute("buttonaccesskey"+dlgtype));
  202.               } else if (dlgtype != "extra1" && dlgtype != "extra2") {
  203.                 button.setAttribute("label", this.mStrBundle.GetStringFromName("button-"+dlgtype));
  204.                 var accessKey = this.mStrBundle.GetStringFromName("accesskey-"+dlgtype);
  205.                 if (accessKey)
  206.                   button.setAttribute("accesskey", accessKey);
  207.               }
  208.             }
  209.           }
  210.  
  211.           // ensure that hitting enter triggers ondialogaccept
  212.           buttons["accept"].setAttribute("default", "true");
  213.           
  214.           // if there is a special button configuration, use it
  215.           if (aButtons) {
  216.             // expect a comma delimited list of dlgtype values
  217.             var list = aButtons.split(",");
  218.             // mark shown dlgtypes as true
  219.             var shown = { accept: false, cancel: false, help: false,
  220.                           disclosure: false, extra1: false, extra2: false };
  221.             for (i = 0; i < list.length; ++i)
  222.               shown[list[i].replace(/ /g, "")] = true;
  223.             
  224.             // hide/show the buttons we want
  225.             for (dlgtype in buttons) 
  226.               buttons[dlgtype].hidden = !shown[dlgtype];
  227.  
  228.             var spacer = document.getAnonymousElementByAttribute(this, "anonid", "spacer");
  229.             spacer.removeAttribute("hidden");
  230.             spacer.setAttribute("flex", shown["extra2"]?"1":"0");
  231.  
  232.           }
  233.         ]]>
  234.         </body>
  235.       </method>
  236.       
  237.       <method name="_handleButtonCommand">
  238.         <parameter name="aEvent"/>
  239.         <body>
  240.         <![CDATA[
  241.           return document.documentElement._doButtonCommand(
  242.                                         aEvent.target.getAttribute("dlgtype"));
  243.         ]]>
  244.         </body>
  245.       </method>
  246.       
  247.       <method name="_doButtonCommand">
  248.         <parameter name="aDlgType"/>
  249.         <body>
  250.         <![CDATA[
  251.           // calling window.close() while an oncommand event
  252.           // call is on the stack fails to close the window, 
  253.           // so we need to do this ugly setTimeout hack
  254. //          window.setTimeout(
  255. //            function(aDlgType) {
  256. //              document.documentElement._reallyDoButtonCommand(aDlgType);
  257. //            },
  258. //            0, aDlgType);
  259.           // The above hack no longer seems necessary... let's do this directly.
  260.           document.documentElement._reallyDoButtonCommand(aDlgType);
  261.         ]]>
  262.         </body>
  263.       </method>
  264.       
  265.       <method name="_reallyDoButtonCommand">
  266.         <parameter name="aDlgType"/>
  267.         <body>
  268.         <![CDATA[
  269.           var button = this.getButton(aDlgType);
  270.           if (!button.disabled) {
  271.             var noCancel = this._fireButtonEvent(aDlgType);
  272.             if (noCancel) {
  273.               if (aDlgType == "accept" || aDlgType == "cancel")
  274.                 window.close();
  275.             }
  276.             return noCancel;
  277.           }
  278.           return true;
  279.         ]]>
  280.         </body>
  281.       </method>
  282.       
  283.       <method name="_fireButtonEvent">
  284.         <parameter name="aDlgType"/>
  285.         <body>
  286.         <![CDATA[
  287.           var event = document.createEvent("Events");
  288.           event.initEvent("dialog"+aDlgType, false, true);
  289.           
  290.           // handle dom event handlers
  291.           var noCancel = this.dispatchEvent(event);
  292.           
  293.           // handle any xml attribute event handlers
  294.           var handler = this.getAttribute("ondialog"+aDlgType);
  295.           if (handler != "") {
  296.             var fn = new Function("event", handler);
  297.             var returned = fn(event);
  298.             if (returned == false)
  299.               noCancel = false;
  300.           }
  301.           
  302.           return noCancel;
  303.         ]]>
  304.         </body>
  305.       </method>
  306.  
  307.       <method name="_hitEnter">
  308.         <parameter name="evt"/>
  309.         <body>
  310.         <![CDATA[
  311.           var target = evt.originalTarget;
  312.           if ((target.localName == "textarea")&&(target.namespaceURI == "http://www.w3.org/1999/xhtml")) {
  313.             return;
  314.           }
  315.           // if a button is focused, do nothing, so that activating the button 
  316.           // doesn't cause the dialog to exit
  317.           if (!this.enterDefaultAlways) {
  318.             var focused = document.commandDispatcher.focusedElement;
  319.             if (focused && focused.localName == "button")
  320.               return;
  321.           }
  322.  
  323.           // only accept dialog if accept button is the default
  324.           var btn = this.getButton("accept");
  325.           if (btn && btn.hasAttribute("default"))
  326.             this.acceptDialog();
  327.         ]]>
  328.         </body>
  329.       </method>
  330.  
  331.     </implementation>
  332.     
  333.     <handlers>
  334.       <handler event="keypress" keycode="VK_ENTER" action="this._hitEnter(event);"/>
  335.       <handler event="keypress" keycode="VK_RETURN" action="this._hitEnter(event);"/>
  336.       <handler event="keypress" keycode="VK_ESCAPE" action="this.cancelDialog();"/>
  337.     </handlers>
  338.  
  339.   </binding>
  340.  
  341.   <binding id="dialogheader" extends="chrome://global/content/bindings/dialog.xml#dialog-base">
  342.     <content>
  343.       <xul:label class="dialogheader-title" xbl:inherits="value=title,crop" crop="right" flex="1"/>
  344.       <xul:label class="dialogheader-description" xbl:inherits="value=description"/>
  345.     </content>
  346.   </binding>
  347.  
  348. </bindings>
  349.