|
Introduction To The Clipboard and Drag & Drop |
||||
|
[Table of Contents] [Previous: Windows and Dialogs] [Next: The Clipboard] Feature Owner: Mike Pinkerton OverviewAs far as Mozilla is concerned, transfering data between applications via the clipboard and drag and drop are virtually identical, even though the OS-specific mechanisms under the hood are very different. The goal is to provide a uniform API for inter- and intra-application communication regardless of the transport mechanism used. This document covers that API, and subsequent documents describe the various services in detail. The meat of this API is nsITransferable, an interface to an object that contains various representations, or "flavors," of a piece of data. Each transferable holds only one item and its various flavors.
Data FlavorsEach piece of data, such as a hunk of text or a single bookmark item in a tree, can (and should) have multiple representations of the data of varying (usually decreasing) fidelity. This allows other applications to still be able to process the data even if they don't understand the richest representation used by mozilla. For example, the hunk of text might have html styled text, the stripped-down plain text representation, and possibly a gif illustrating the text. Mozilla knows what to do with the styled html, but SimpleText, for example, does not, yet we still want to be able to communicate with SimpleText. Each representation of the data is referred to as a "flavor." When referring to data in the transferable, you need to know the flavor. When you can accept a variety of flavors, there are mechanisms for asking for the best one. Data ObjectsIn order to work with XPConnect and allow writing clipboard/drag&drop code in JavaScript, the actual must be wrapped in typed objects. The two most common of these objects are nsISupportsString and nsISupportsWString for one byte and two byte strings, respectively. These "wrapper" objects must be created using the component manager and the actual data to transfer must be placed within them before passing the data to the Transferable object. Here's a code snipped in JavaScript to do this.
Converters<<talk about data converters>> Using The TransferablePlacing Data Into The TransferableThe basic usage pattern for adding data to the transferable is as follows:
Note that the flavor must be registered with the transferable before data of that type can be added. [Why? That seems quite stupid. Shouldn't it just add it if it can't find it?] Also note that the length parameter of setTransferData() is in bytes, not characters, so for double-byte strings, you need to make sure you do the math correctly.
Retrieving Data From The TransferableThere are two basic cases from retrieving data from the transferable: when you know exactly what you want and when you have a list of several flavors you support and you want the best one available (the more common case, probably). In either case, the basic steps are similiar:
The only tricky part (which only applies to JavaScript) is that since the objects returned by get[Any]TransferData() are out parameters, you must create new JS objects to hold the result and the length. After these objects are filled in, you can get at the actual out parameter values by accessing the value member of these objects. When you know exactly what you're looking forThis is the simple case or the case where you only care about one flavor. For this, use getTransferData().
When you want the best availableThe most common case is where a client supports a variety of flavors (mozilla flavors plus some from other applications), but certainly has a preference about which flavors it wants have if they are present. For this, use getAnyTransferData(). In order for getAnyTransferData() to work correctly, the order in which you register them is very important. You must register the flavors you are interested in from most interested to least interested (usually highest fidelity to lowest, but not necessarily). Haphazardly registering flavors in random order will cause you to not get the flavor you are expecting.
[Table of Contents] [Previous: Windows and Dialogs] [Next: The Clipboard] Contact us at xptoolkitstaff@netscape.com. Want to complain about the new documentation? Email Dave Hyatt. |