Learn about XUL, a subset of XML used to describe user interfaces, that helps you to make rich user interfaces with nothing more complicated than a text editor. In the sixth part of this series learn about XBL, another of XML’s many faces and a partner in crime with XUL, also called XML Binding Language.
This simply defines a class of a menubar pointing at the binding id. This is done with the filename of the binding file followed by the # symbol followed by the id of the binding element you wish to use. The –moz-binding: prefix is a special CSS property that tells the stylesheet file that you’re binding to an XBL file. Because the binding file we are using is very basic and contains only one binding element, this could be omitted. Save this file as bindingexample.css in the same location that the first file was saved.
Finally, create the XUL file that references the binding style sheet:
The xml-stylesheet reference points at the CSS file, which in turn points to the binding. Save this file as bindingexample.XUL, again in the same location as the above two. You needn’t open this via a chrome URL (although for real-world XBL usage it is advisable), so revert to the command line call used in the first couple of articles. You should get a very small window containing the default menubar at the top. As the class has been assigned specifically to the menubar tag, you are free to define menubars in the XUL file that will not inherit the menu items defined in the binding tag. Add another menubar element to the XUL file without declaring the style class and it should appear as normal beneath the XBL menu:
However, if you then assign the second menu to the mymenu class, the menu you have added manually will override the menu elements defined in the XBL file and they will not appear in the second menu. You can get around this by using the <children/> element in the XBL file. Add the element beneath the <XUL:menu> elements and then execute the file again. Now the menus defined in the XBL will be there, with the special menu in the location that the children element appears (i.e. after the other menus). The power this gives you when designing applications is phenomenal; you can define a default menu set that you want to appear in all of your applications, and then just tweak the XUL files to add any additional menus relevant to the application you’re building. This promotes code reuse, which is something pushed by all modern programming languages.