本篇指南將引導您學習如何使用 XUL (XML User Interface Language) 。XUL 可用來建立應用程式的使用者介面，而且能跨平台使用。
在這裡，我們將引領您建立一個簡單的「尋找檔案」介面。就像是 Mac 的 Sherlock，或是 Windows 的尋找檔案對話視窗。不過這份文件只會教你如何建立介面，而不會實作真正的檔案搜尋功能。 A blue line will appear to the left of a paragraph where the find file dialog is being modified. You can follow along by looking for these sections.
XUL (讀做 zool，有點像 cool ) 被發明出來的目的，是用來增進 Mozilla 瀏覽器的開發速度。XUL 本身屬於 XML，因此 XML 的特性 XUL 也都有。
Most applications need to be developed using features of a specific platform making building cross-platform software time-consuming and costly. A number of cross-platform solutions have been developed in the past. Java, for example, has portability as a main selling point. XUL is one such language designed specifically for building portable user interfaces. It takes a long time to build an application even for only one platform. The time required to compile and debug can be lengthy. With XUL, an interface can be implemented and modified quickly and easily.
XUL has all the advantages of other XML languages. For example XHTML or other XML languages such as MathML or SVG can be inserted within it. Also, text displayed with XUL is easily localizable, which means that it can be translated into other languages with little effort.
XUL provides the ability to create most elements found in modern graphical interfaces. Some elements that can be created are:
- Input controls such as textboxes and checkboxes
- Toolbars with buttons or other content
- Menus on a menu bar or pop up menus
- Tabbed dialogs
- Trees for hierarchical or tabular information
- Keyboard shortcuts
The displayed content can be created from the contents of a XUL file or with data from a datasource. In Mozilla, such datasources include a user's mailbox, their bookmarks and search results. The contents of menus, trees and other elements can be populated with this data, or with your own data supplied in an RDF file.
There are several ways you can use XUL:
- Firefox extension -- an extension adds functionality to the browser itself, often in the form of extra toolbars, context menus, or customizations to the browser's user interface. This is done using a feature of XUL called an overlay, which allows the UI provided from one source, in this case, the Firefox browser, to be merged together with the UI from the extension. Extensions may also be applied to other Mozilla based products such as Thunderbird.
- Standalone XULRunner application -- XULRunner is a packaged version of the Mozilla platform which allows you to create standalone XUL applications. A browser isn't required to run these applications, as they have their own executable file.
- XUL package -- in between the other two are applications which are created in the same way as an extension, but they act like a separate application in a separate window. This is used when you don't want to have the larger size of a complete XULRunner application, but don't mind requiring a Mozilla browser to be installed to be able to run the application.
- Remote XUL application -- you can also just place XUL code on a web server and open it in a browser, as you would any other web page. This method is limited however, as there are security concerns that will limit the kinds of things you will be able to do, such as opening other windows.
The first three types all require an installation to be performed on the user's machine. However, these types of applications do not have security restrictions placed on them, so they may access local files and read and write preferences, for example. For extensions, the XUL files and associated scripts and images used by an application would be packaged into a single file and downloaded and installed by the user. Mozilla applications such as Firefox provide an extension manager which allows packages to be installed without having to write a lot of complex code.
It is also possible to open XUL files directly from the file system or from a remote web site; however they will be restricted in the kinds of operations they can do, and some aspects of XUL will not work. However, if you do want to load XUL content from a remote site, the Web server must be set up to send XUL files with the content type "application/vnd.mozilla.xul+xml". XUL is usually stored in files with a
.xul extension. You can open a XUL file with Mozilla as you would any other file, using the Open File command from the File menu or typing the URL into the address bar.
What do I need to know to use this tutorial?
You should have an understanding of HTML and at least a basic understanding of XML and CSS. Here are some guidelines to keep in mind:
- XUL elements and attributes should all be entered in lowercase as XML is case-sensitive (unlike HTML).
- Attribute values in XUL must be placed inside quotes, even if they are numbers.
- XUL files are usually split into four files, one each for the layout and elements, for style declarations, for entity declarations (used for localization) and for scripts. In addition, you may have extra files for images or for platform specific data.
XUL is supported in Mozilla and browsers that are also based upon on the Gecko engine, such as Netscape 6 or later and Mozilla Firefox. Due to various changes in XUL syntax over time, you will want to get the latest version for the examples to work properly. Most examples should work in Mozilla 1.0 or later. XUL is fairly similar in Firefox and to other browsers, although it has some specific differences such as support for customizable toolbars.
This tutorial attempts to cover much of XUL's functionality, however, not all features are discussed. Once you are familiar with XUL, you can use the XUL Reference to find out about other features supported by specific elements.