ozilla 1.0 and Netscape 7.0, the re-write and upgrade to Netscape’s 4.x Communicator suite, is more than just standards support and new code. During the re-write, the Netscape (now mozilla.org) engineers decided to emphasize XML as the foundation of (nearly) everything in the new browser code. One important result is the XML-based XUL language.
In this article I’ll show you how kick XUL’s tires a bit and explore how it works. I’ve used Mozilla here rather than Netscape 7.0, because of the availability of friendly development tools such as the DOM Inspector and the debugger. Otherwise, the two browsers are the same.
|What You Need|
|To run the code that accompanies this article, you’ll need Mozilla version 1.0 or later. Netscape 6.x contains Mozilla 0.94, and won’t work for this demo. Recent changes to some XUL tags require a full 1.0 release. You can download the official version, or last night’s build, at www.mozilla.org. If you’re willing to provide feedback, you can do the Mozilla developers a favor and try a talkback version. If you manage to crash the browser, the talkback release generates a diagnostic file that Mozilla developers can use for a post-mortem.|
The Who-What-Where of XUL
Most XML standards describe some kind of document, but XUL is the “not a document” XML standard. Before document-centric architectures we had plain ol’ user-interfaces (GUIs), coded using Visual Basic, HyperCard or C/C++, to name just a few. When the Web came along, focus shifted strongly toward displaying documents in HTML. In the XML world, XUL is a hybrid solution that provides a way to describe scrollbars, toolbars, tabbed interfaces, and other standard UI controls. However, because XUL files consist of plain-text XML, XUL avoids the problem of 3GL code tortuously exploiting some low-level library like Win32, Swing or GTK.
Even though it’s XML-based, you won’t find XUL on http://www.w3.org with the other XML-standards. XUL is entirely a Mozilla invention. But you’ll find XUL implemented inside all recent AOL/TimeWarner browsers. Mozilla, Netscape 6+, CompuServe 7+ and derived products, are packed with XUL documents. Indeed, the difference between Mozilla and Netscape 7.0 is partly bundling, and partly just different uses of XUL.
Author’s Note: XUL might be officially pronounced “zool”, after a character in the film Ghostbusters?, but this author can’t handle it any more. It’s “eks-you-ell” in my head, to match the pronunciation of all the other X-standards, such as XLink and XHTML.XUL is Critical in Mozilla
You’ll need Mozilla to see the examples in this article, so download it if you don’t already have it installed. You can install Mozilla multiple times in different directories, but be careful of upgrade issues. Read the release notes first.
|Author’s Note: Very important! If you have two installations (say an official 1.0 and an experimental 1.1), then, on Windows, running both at once doesn’t work well. If you want to read some HTML and at the same time play with XUL, use IE to view the HTML and use Mozilla for development. You can do both with Mozilla, but if you have an obscure problem, debugging isn’t as clean.|
|Figure 1: A blank document in Mozilla. Note that the URL is “about:blank”, meaning there’s no document loaded. Everything except the window frame and the title bar is generated by XUL.|
Here’s how to find some XUL. By default, when running Windows, Mozilla installs to the C:Program FilesMozilla directory. For the purposes of this article, I’ll assume that that’s where Mozilla’s installed. However, if you upgraded from an earlier version of Mozilla, the upgrade process can put key files in C:WindowsApplication DataMozilla…. If you have that directory, substitute it wherever you see C:Program FilesMozilla… in the following discussion. In either case, when you start the browser, you see a normal HTML window. Type the URL about:blank into the address bar. That displays a blank page with no HTML in the window, as shown in Figure 1. Everything else visible in the screenshot, except for the Microsoft Windows blue title bar, is XUL, including the toolbars, background, menus?in short, everything visible except the window frame.
|Figure 2: After removing Mozilla’s access to the “chrome” folder and thus the XUL files that define the browser’s interface, launching the browser shows only the window frame and title bar, but no controls or content.|
Here’s a second (highly experimental) test. Shut down all instances of Mozilla, and temporarily rename the folder C:Program FilesMozillachrome to something else, such as chromeX. Then relaunch Mozilla. You’ll see a tiny window as shown in Figure 2.
By renaming the chrome folder, you removed Mozilla’s access to all the default XUL files, and consequently, to Mozilla’s entire user interface. Clearly XUL plays a critical role in the browser.
Don’t forget to undo the renaming operation.
|Figure 3: The Mozilla browser with the NetSheet1.xul file loaded. The XUL file describes a spreadsheet-like grid application.|
In the rest of this article, you’ll see how easy it is to combine XUL and script to create custom interfaces. Listing 1 shows the beginning of a MS-Excel/Lotus 1-2-3/VisiCalc style spreadsheet application, written in XUL. Load it into the browser just like any Web document. Figure 3 shows the result.
When you load the file in this manner, you see the normal browser controls, plus the new content with its own controls. You can also make the browser appear less like a browser, giving a more polished effect, by loading the URL from a command prompt or from a shortcut, substituting your own installed location for Mozilla and the NetSheet1.xul file, for example::
"C:Program FilesMozillamozilla.exe" --chrome "file:C:/tmp/NetSheet1.xul"
When you load the file in this manner (see Figure 4), only the new XUL elements decorate the window.
window.open("file:C:/tmp/NetSheet1.xul", "win_name", "chrome");
XUL Markup Elements
Here’s a quick review of the markup elements in NetSheet1.xul. At the top of each file, you’ll find some standard XML headers. The file doesn’t have to have an .xul extension?you could give it an .xml extension?but .xul is the convention. The xmlns namespace http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul is not just a weird pun, it declares the XUL DTD. Mozilla recognizes this URI, so the declaration is not an instruction to download anything.
HTML uses an tag as a container for all other content. XUL uses the uses the
|Figure 4: When you load the NetSheet1 file as a local application, the browser’s standard toolbar disappears, and only the XUL elements described in the file appear in the browser.|
Most of the other tags have names that correctly hint at their purposes. You can see XUL documents contain a lot of structure but not much “content”. What little content exists has been limited to the
You’ll see a reference to the XML stylesheet NetSheet.css included at the top of the file. If you look at that file, you’ll see that it’s a standard CSS2-compliant stylesheet. XUL uses the same styling model (“the box model”) as HTML. CSS styles are one of several ways that XUL helps keep applications flexible. In addition, XUL exposes many unique style properties, but this example doesn’t use any.
Even without any coding, the application shows a degree of interactivity. You can slide the scroll bars, click the buttons, collapse the toolbars, select menu items, change tabs, and view tool tips. You can also enter data into the cells. If you think the scrollbars aren’t working, that’s because they’re placed independently, not drawn as part of a
First, you’ll find an onload() handler, which installs the event handlers for the interface elements. You can find that code in NetSheet.js, one of several included files that contain script. Next, a bit of XUL-specific markup, the
Still, this is only a demo so tread lightly! Try this series of steps. Click on cell B2. Note that it highlights (a scripted CSS2 change) and that the toolbar now displays the correct cell name. Click on the input box and type in “=12“. Click on cell E5. In the toolbar text entry box, type:
=cell("B2")*2 + Math.cos(Math.PI)
Press return, or move to another cell, and 23 should appear in cell E5 (that’s 12*2 + (-1)). You should be able to use the formula sum(“B1″,”D3”) as well, but at the moment it always returns 42. Oh well, mañana. You can type strings into cells too, although the formatting could be better.Connecting to the Back End
Now if what you’ve seen were all XUL in Mozilla had to offer, this technology would quickly come to a halt. What you really want to do is Save and Load the application, either locally to your hard disk, or to the server, or both, just like any application.
In Mozilla, as in .NET and Java, there are about 4001 ways this can be done. Here’s the simplest possible solution, which loads and saves the data to a plain text file on the local drive.
Writing to the local drive is a security issue, so you have to change tactics. A quick and dirty way to proceed follows. Create a directory such as:
Unpack the final XUL NetSheet demo step3.zip in the sample code into that directory. Shut down Mozilla. Open the file C:Program FilesMozillachromeinstalled-chrome.txt, and add the following line:
to the end of that file:
Save the file. After altering the file, you can now refer to the demo by this URL:
The file NetSheet3.xul acts much the same as before, except that the File | Save As menu item also works. This is done through the new script file NetSheet3.js (see Listing 2), which contains the plumbing. Here’s a critical piece of it:
var Picker = Components.interfaces.nsIFilePicker; fpobj = Components.classes ["@mozilla.org/filepicker;1"]; fpobj = fpobj.createInstance(Picker); fpobj.init(window, "File Save Dialog Box", Picker.modeSave); result = fpobj.show();
The preceding code gets a named component from Mozilla’s component registry, and then chooses a named interface to that component. The component provides the familiar File Picker popup dialog box, so you don’t have to code one from scratch. You control this component in the same way you script any object library via C/C++, VB, Java, Perl, Tcl or C#. In the end you get the filename chosen by the user. Here’s the second critical piece of code:
o1 = Components.classes ["@mozilla.org/network/file-transport-service;1"]; o2 = o1.getService (Components.interfaces.nsIFileTransportService); o3 = o2.createTransport(file_thing, fWriteModes, fPerms,true); o4 = o3.openOutputStream(foffset, fmaxbytes, fflags); o4.write(content,content.length); o4.close();
This time the component registry provides a non-GUI component used (eventually) to write the spreadsheet to disk. See the comments in the code for details.
What does this have to do with XUL? These components complement the XUL GUI elements. They provide scriptable services that turn passive XUL elements into real applications. They also provide common functionality such as File Pickers. Better still, the result is 100% portable across Windows, Linux/Unix, Mac and elsewhere.
Off the Beaten Track
Not shown in this application are some of the more bizarre combinations of XUL tags: checkboxes inside buttons; HTML pages inside grid cells; images inside scrollbars. All these are feasible, although many are probably confusing to the user. The stranger combinations are less well tested, too.
Because of Mozilla’s deep support for XML, you can mix HTML and XUL in one document. You can also combine different documents in a structured way using
Mozilla is a big product and there are many different technologies to explore. You can get a little done quickly with XUL as shown here, but XUL used systematically can produce a flexible and bulletproof application. XUL is a very fast GUI language, highly portable and the basis for a look-and-feel that is more application-centered than document-centered. XUL makes Mozilla-based browsers completely customizable.
GM Creates Open Source uProtocol and Invites Automakers to Adopt It: Revolutionizing Automotive Software Development.
General Motors (GM) recently announced its entry into the Eclipse Foundation. The Eclipse Foundation is a prominent open-source software foundation. In addition, GMC announced its contribution of “uProtocol” to facilitate
What is Metadata?
What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular
What We Should Expect from Cell Phone Tech in the Near Future
The earliest cell phones included boxy designs full of buttons and antennas, and they only made calls. Needless to say, we’ve come a long way from those classic brick phones
The Best Mechanical Keyboards For Programmers: Where To Find Them
When it comes to programming, a good mechanical keyboard can make all the difference. Naturally, you would want one of the best mechanical keyboards for programmers. But with so many
The Digital Panopticon: Is Big Brother Always Watching Us Online?
In the age of digital transformation, the internet has become a ubiquitous part of our lives. From socializing, shopping, and learning to more sensitive activities such as banking and healthcare,
Embracing Change: How AI Is Revolutionizing the Developer’s Role
The world of software development is changing drastically with the introduction of Artificial Intelligence and Machine Learning technologies. In the past, software developers were in charge of the entire development
The Benefits of Using XDR Solutions
Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved
How AI is Revolutionizing Fraud Detection
Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across
Companies Leading AI Innovation in 2023
Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several
Step-by-Step Guide to Properly Copyright Your Website
Creating a website is not easy, but protecting your website is equally important. Implementing copyright laws ensures that the substance of your website remains secure and sheltered. Copyrighting your website
Fivetran Pricing Explained
One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of
Kubernetes Logging: What You Need to Know
Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes