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