PageRenderTime 20ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 1ms

/toolkit/content/widgets/dialog.xml

http://github.com/zpao/v8monkey
XML | 438 lines | 386 code | 52 blank | 0 comment | 0 complexity | b98afbad3707de987e8c597a97cdd1de MD5 | raw file
  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" extends="chrome://global/content/bindings/general.xml#root-element">
  9    <resources>
 10      <stylesheet src="chrome://global/skin/dialog.css"/>
 11    </resources>
 12    <content>
 13      <xul:vbox class="box-inherit dialog-content-box" flex="1">
 14        <children/>
 15      </xul:vbox>
 16          
 17      <xul:hbox class="dialog-button-box" anonid="buttons"
 18                xbl:inherits="pack=buttonpack,align=buttonalign,dir=buttondir,orient=buttonorient"
 19#ifdef XP_UNIX
 20                >
 21        <xul:button dlgtype="disclosure" class="dialog-button" hidden="true"/>
 22        <xul:button dlgtype="help" class="dialog-button" hidden="true"/>
 23        <xul:button dlgtype="extra2" class="dialog-button" hidden="true"/>
 24        <xul:button dlgtype="extra1" class="dialog-button" hidden="true"/>
 25        <xul:spacer anonid="spacer" flex="1"/>
 26        <xul:button dlgtype="cancel" class="dialog-button"/>
 27        <xul:button dlgtype="accept" class="dialog-button" xbl:inherits="disabled=buttondisabledaccept"/>
 28#else
 29                pack="end">
 30        <xul:button dlgtype="extra2" class="dialog-button" hidden="true"/>
 31        <xul:spacer anonid="spacer" flex="1" hidden="true"/>
 32        <xul:button dlgtype="accept" class="dialog-button" xbl:inherits="disabled=buttondisabledaccept"/>
 33        <xul:button dlgtype="extra1" class="dialog-button" hidden="true"/>
 34        <xul:button dlgtype="cancel" class="dialog-button"/>
 35        <xul:button dlgtype="help" class="dialog-button" hidden="true"/>
 36        <xul:button dlgtype="disclosure" class="dialog-button" hidden="true"/>
 37#endif
 38      </xul:hbox>
 39    </content>
 40
 41    <implementation>
 42      <field name="_mStrBundle">null</field>
 43      <field name="_closeHandler">(function(event) {
 44        if (!document.documentElement.cancelDialog())
 45          event.preventDefault();
 46      })</field>
 47
 48      <property name="buttons"
 49                onget="return this.getAttribute('buttons');"
 50                onset="this._configureButtons(val); return val;"/>
 51
 52      <property name="defaultButton">
 53        <getter>
 54        <![CDATA[
 55          if (this.hasAttribute("defaultButton"))
 56            return this.getAttribute("defaultButton");
 57          else // default to the accept button
 58            return "accept";
 59        ]]>
 60        </getter>
 61        <setter>
 62        <![CDATA[
 63          this._setDefaultButton(val);
 64          return val;
 65        ]]>
 66        </setter>
 67      </property>
 68
 69      <method name="acceptDialog">
 70        <body>
 71        <![CDATA[
 72          return this._doButtonCommand("accept");
 73        ]]>
 74        </body>
 75      </method>
 76      
 77      <method name="cancelDialog">
 78        <body>
 79        <![CDATA[
 80          return this._doButtonCommand("cancel");
 81        ]]>
 82        </body>
 83      </method>
 84      
 85      <method name="getButton">
 86        <parameter name="aDlgType"/>
 87        <body>
 88        <![CDATA[
 89          return this._buttons[aDlgType];
 90        ]]>
 91        </body>
 92      </method>
 93
 94      <method name="moveToAlertPosition">
 95        <body>
 96        <![CDATA[
 97          // hack. we need this so the window has something like its final size
 98          if (window.outerWidth == 1) {
 99            dump("Trying to position a sizeless window; caller should have called sizeToContent() or sizeTo(). See bug 75649.\n");
100            sizeToContent();
101          }
102
103          var xOffset = (opener.outerWidth - window.outerWidth) / 2;
104          var yOffset = opener.outerHeight / 5;
105
106          var newX = opener.screenX + xOffset;
107          var newY = opener.screenY + yOffset;
108
109          // ensure the window is fully onscreen (if smaller than the screen)
110          if (newX < screen.availLeft)
111            newX = screen.availLeft + 20;
112          if ((newX + window.outerWidth) > (screen.availLeft + screen.availWidth))
113            newX = (screen.availLeft + screen.availWidth) - window.outerWidth - 20;
114
115          if (newY < screen.availTop)
116            newY = screen.availTop + 20;
117          if ((newY + window.outerHeight) > (screen.availTop + screen.availHeight))
118            newY = (screen.availTop + screen.availHeight) - window.outerHeight - 60;
119
120          window.moveTo( newX, newY );
121        ]]>
122        </body>
123      </method>
124
125      <method name="centerWindowOnScreen">
126        <body>
127        <![CDATA[
128          var xOffset = screen.availWidth/2 - window.outerWidth/2;
129          var yOffset = screen.availHeight/2 - window.outerHeight/2; //(opener.outerHeight *2)/10;
130  
131          xOffset = xOffset > 0 ? xOffset : 0;
132          yOffset = yOffset > 0 ? yOffset : 0;
133          window.moveTo(xOffset, yOffset);
134        ]]>
135        </body>
136      </method>
137
138      <constructor>
139      <![CDATA[
140        this._configureButtons(this.buttons);
141
142        // listen for when window is closed via native close buttons
143        window.addEventListener("close", this._closeHandler, false);
144
145        // for things that we need to initialize after onload fires
146        window.addEventListener("load", this.postLoadInit, false);
147
148        window.moveToAlertPosition = this.moveToAlertPosition;
149        window.centerWindowOnScreen = this.centerWindowOnScreen;
150      ]]>
151      </constructor>
152
153      <method name="postLoadInit">
154        <parameter name="aEvent"/>
155        <body>
156        <![CDATA[
157          function focusInit() {
158            const dialog = document.documentElement;
159            const defaultButton = dialog.getButton(dialog.defaultButton);
160            // give focus to the first focusable element in the dialog
161            if (!document.commandDispatcher.focusedElement) {
162              document.commandDispatcher.advanceFocusIntoSubtree(dialog);
163
164              var focusedElt = document.commandDispatcher.focusedElement;
165              if (focusedElt) {
166                var initialFocusedElt = focusedElt;
167                while (focusedElt.localName == "tab" ||
168                       focusedElt.getAttribute("noinitialfocus") == "true") {
169                  document.commandDispatcher.advanceFocusIntoSubtree(focusedElt);
170                  focusedElt = document.commandDispatcher.focusedElement;
171                  if (focusedElt == initialFocusedElt)
172                    break;
173                }
174
175                if (initialFocusedElt.localName == "tab") {
176                  if (focusedElt.hasAttribute("dlgtype")) {
177                    // We don't want to focus on anonymous OK, Cancel, etc. buttons,
178                    // so return focus to the tab itself
179                    initialFocusedElt.focus();
180                  }
181                }
182#ifndef XP_MACOSX
183                else if (focusedElt.hasAttribute("dlgtype") && focusedElt != defaultButton) {
184                  defaultButton.focus();
185                }
186#endif
187              }
188            }
189
190            try {
191              if (defaultButton)
192                window.notifyDefaultButtonLoaded(defaultButton);
193            } catch (e) { }
194          }
195
196          // Give focus after onload completes, see bug 103197.
197          setTimeout(focusInit, 0);
198        ]]>
199        </body>
200      </method>                
201
202      <property name="mStrBundle">
203        <getter>
204        <![CDATA[
205          if (!this._mStrBundle) {
206            // need to create string bundle manually instead of using <xul:stringbundle/>
207            // see bug 63370 for details
208            this._mStrBundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
209                                         .getService(Components.interfaces.nsIStringBundleService)
210                                         .createBundle("chrome://global/locale/dialog.properties");
211          }
212          return this._mStrBundle;
213        ]]></getter>
214      </property>
215      
216      <method name="_configureButtons">
217        <parameter name="aButtons"/>
218        <body>
219        <![CDATA[
220          // by default, get all the anonymous button elements
221          var buttons = {};
222          this._buttons = buttons;
223          buttons.accept = document.getAnonymousElementByAttribute(this, "dlgtype", "accept");
224          buttons.cancel = document.getAnonymousElementByAttribute(this, "dlgtype", "cancel");
225          buttons.extra1 = document.getAnonymousElementByAttribute(this, "dlgtype", "extra1");
226          buttons.extra2 = document.getAnonymousElementByAttribute(this, "dlgtype", "extra2");
227          buttons.help = document.getAnonymousElementByAttribute(this, "dlgtype", "help");
228          buttons.disclosure = document.getAnonymousElementByAttribute(this, "dlgtype", "disclosure");
229
230          // look for any overriding explicit button elements
231          var exBtns = this.getElementsByAttribute("dlgtype", "*");
232          var dlgtype;
233          var i;
234          for (i = 0; i < exBtns.length; ++i) {
235            dlgtype = exBtns[i].getAttribute("dlgtype");
236            buttons[dlgtype].hidden = true; // hide the anonymous button
237            buttons[dlgtype] = exBtns[i];
238          }
239
240          // add the label and oncommand handler to each button
241          for (dlgtype in buttons) {
242            var button = buttons[dlgtype];
243            button.addEventListener("command", this._handleButtonCommand, true);
244
245            // don't override custom labels with pre-defined labels on explicit buttons
246            if (!button.hasAttribute("label")) {
247              // dialog attributes override the default labels in dialog.properties
248              if (this.hasAttribute("buttonlabel"+dlgtype)) {
249                button.setAttribute("label", this.getAttribute("buttonlabel"+dlgtype));
250                if (this.hasAttribute("buttonaccesskey"+dlgtype))
251                  button.setAttribute("accesskey", this.getAttribute("buttonaccesskey"+dlgtype));
252              } else if (dlgtype != "extra1" && dlgtype != "extra2") {
253                button.setAttribute("label", this.mStrBundle.GetStringFromName("button-"+dlgtype));
254                var accessKey = this.mStrBundle.GetStringFromName("accesskey-"+dlgtype);
255                if (accessKey)
256                  button.setAttribute("accesskey", accessKey);
257              }
258            }
259            // allow specifying alternate icons in the dialog header
260            if (!button.hasAttribute("icon")) {
261              // if there's an icon specified, use that
262              if (this.hasAttribute("buttonicon"+dlgtype))
263                button.setAttribute("icon", this.getAttribute("buttonicon"+dlgtype));
264              // otherwise set defaults
265              else
266                switch (dlgtype) {
267                  case "accept":
268                    button.setAttribute("icon","accept");
269                    break;
270                  case "cancel":
271                    button.setAttribute("icon","cancel");
272                    break;
273                  case "disclosure":
274                    button.setAttribute("icon","properties");
275                    break;
276                  case "help":
277                    button.setAttribute("icon","help");
278                    break;
279                  default:
280                    break;
281                }
282            }
283          }
284
285          // ensure that hitting enter triggers the default button command
286          this.defaultButton = this.defaultButton;
287          
288          // if there is a special button configuration, use it
289          if (aButtons) {
290            // expect a comma delimited list of dlgtype values
291            var list = aButtons.split(",");
292
293            // mark shown dlgtypes as true
294            var shown = { accept: false, cancel: false, help: false,
295                          disclosure: false, extra1: false, extra2: false };
296            for (i = 0; i < list.length; ++i)
297              shown[list[i].replace(/ /g, "")] = true;
298
299            // hide/show the buttons we want
300            for (dlgtype in buttons) 
301              buttons[dlgtype].hidden = !shown[dlgtype];
302
303#ifdef XP_WIN
304#           show the spacer on Windows only when the extra2 button is present
305            var spacer = document.getAnonymousElementByAttribute(this, "anonid", "spacer");
306            spacer.removeAttribute("hidden");
307            spacer.setAttribute("flex", shown["extra2"]?"1":"0");
308#endif
309
310          }
311        ]]>
312        </body>
313      </method>
314
315      <method name="_setDefaultButton">
316        <parameter name="aNewDefault"/>
317        <body>
318        <![CDATA[
319          // remove the default attribute from the previous default button, if any
320          var oldDefaultButton = this.getButton(this.defaultButton);
321          if (oldDefaultButton)
322            oldDefaultButton.removeAttribute("default");
323
324          var newDefaultButton = this.getButton(aNewDefault);
325          if (newDefaultButton) {
326            this.setAttribute("defaultButton", aNewDefault);
327            newDefaultButton.setAttribute("default", "true");
328          }
329          else {
330            this.setAttribute("defaultButton", "none");
331            if (aNewDefault != "none")
332              dump("invalid new default button: " +  aNewDefault + ", assuming: none\n");
333          }
334        ]]>
335        </body>
336      </method>
337
338      <method name="_handleButtonCommand">
339        <parameter name="aEvent"/>
340        <body>
341        <![CDATA[
342          return document.documentElement._doButtonCommand(
343                                        aEvent.target.getAttribute("dlgtype"));
344        ]]>
345        </body>
346      </method>
347      
348      <method name="_doButtonCommand">
349        <parameter name="aDlgType"/>
350        <body>
351        <![CDATA[
352          var button = this.getButton(aDlgType);
353          if (!button.disabled) {
354            var noCancel = this._fireButtonEvent(aDlgType);
355            if (noCancel) {
356              if (aDlgType == "accept" || aDlgType == "cancel")
357                window.close();
358            }
359            return noCancel;
360          }
361          return true;
362        ]]>
363        </body>
364      </method>
365      
366      <method name="_fireButtonEvent">
367        <parameter name="aDlgType"/>
368        <body>
369        <![CDATA[
370          var event = document.createEvent("Events");
371          event.initEvent("dialog"+aDlgType, true, true);
372          
373          // handle dom event handlers
374          var noCancel = this.dispatchEvent(event);
375          
376          // handle any xml attribute event handlers
377          var handler = this.getAttribute("ondialog"+aDlgType);
378          if (handler != "") {
379            var fn = new Function("event", handler);
380            var returned = fn(event);
381            if (returned == false)
382              noCancel = false;
383          }
384          
385          return noCancel;
386        ]]>
387        </body>
388      </method>
389
390      <method name="_hitEnter">
391        <parameter name="evt"/>
392        <body>
393        <![CDATA[
394          if (evt.defaultPrevented)
395            return;
396
397          var btn = this.getButton(this.defaultButton);
398          if (btn)
399            this._doButtonCommand(this.defaultButton);
400        ]]>
401        </body>
402      </method>
403
404    </implementation>
405    
406    <handlers>
407      <handler event="keypress" keycode="VK_ENTER"
408               group="system" action="this._hitEnter(event);"/>
409      <handler event="keypress" keycode="VK_RETURN"
410               group="system" action="this._hitEnter(event);"/>
411      <handler event="keypress" keycode="VK_ESCAPE" group="system">
412        if (!event.defaultPrevented)
413          this.cancelDialog();
414      </handler>
415#ifdef XP_MACOSX
416      <handler event="keypress" key="." modifiers="meta" phase="capturing" action="this.cancelDialog();"/>
417#else
418      <handler event="focus" phase="capturing">
419        var btn = this.getButton(this.defaultButton);
420        if (btn)
421          btn.setAttribute("default", event.originalTarget == btn || !(event.originalTarget instanceof Components.interfaces.nsIDOMXULButtonElement));
422      </handler>
423#endif
424    </handlers>
425
426  </binding>
427
428  <binding id="dialogheader">
429    <resources>
430      <stylesheet src="chrome://global/skin/dialog.css"/>
431    </resources>
432    <content>
433      <xul:label class="dialogheader-title" xbl:inherits="value=title,crop" crop="right" flex="1"/>
434      <xul:label class="dialogheader-description" xbl:inherits="value=description"/>
435    </content>
436  </binding>
437
438</bindings>