![]() |
![]()
| ![]() |
![]()
NAMEPrima::Clipboard - GUI interprocess data exchange DESCRIPTIONPrima::Clipboard is an interface to system clipboards. Depending on the OS, there can be only one clipboard (Win32), or three (X11). The class is also used for data exchange in drag-and-drop interactions. SYNOPSISmy $c = $::application-> Clipboard; # paste data my $string = $c-> text; my $image = $c-> image; my $other = $c-> fetch('Other type'); # copy datum $c-> text( $string); # copy data $c-> open; $c-> text( $string); $c-> image( $image); $c-> store( $image); $c-> close; # clear $c-> clear; USAGEPrima::Clipboard provides access to the system clipboard data storage. For the easier communication, the system clipboard has one 'format' field, that is stored along with the data. This field is used to distinguish between data formats. Moreover, a clipboard can hold simultaneously several data instances, of different data formats. Since the primary usage of a clipboard is 'copying' and 'pasting', an application can store copied information in several formats, increasing possibility that the receiving application can recognize the data. Different systems provide spectrum of predefined data types, but the toolkit uses only three of these out of the box - ascii text, utf8 text, and image. It does not limit, however, the data format being one of these three types - an application is free to register its own formats. Both predefined and newly defined data formats are described by a string, and the three predefined formats are represented by 'Text', 'UTF8', and 'Image' string constants. The most frequent usage of Prima::Clipboard is to preform two tasks - copying and pasting. Both can be exemplified by the following: my $c = $::application-> Clipboard; # paste my $string = $c-> text; # copy $c-> text( $string); Here is what happens under the hood: First, the default clipboard is accessible by an implicit name call, as an object named 'Clipboard'. This scheme makes it easily overridable. A more important point is, that the default clipboard object might be accompanied by other clipboard objects. This is the case with X11 environment, which defines also 'Primary' and 'Secondary' system clipboards. Their functionality is identical to the default clipboard, however. get_standard_clipboards() method returns strings for the clipboards, provided by the system. Second, code for fetching and/or storing multi-format data is somewhat different. Clipboard is viewed as a shared system resource, and has to be 'opened', before a process can grab it, so other processes can access the clipboard data only after the clipboard is 'closed' ( note: It is not so under X11, where there is no such thing as clipboard locking, -- but the toolkit imposes this model for the consistency sake). fetch() and store() implicitly call open() and close(), but these functions must be called explicitly for the multi-format data handling. The code below illustrates the said: # copy text and image if ( $c-> open) { $c-> clear; $c-> store('Text', $string); $c-> store('Image', $image); $c-> close; } # check present formats and paste if ( $c-> open) { if ( $c-> format_exists('Text')) { $string = $c-> fetch('Text'); } # or, check the desired format alternatively my %formats = map { $_ => 1 } $c-> get_formats; if ( $formats{'Image'}) { $image = $c-> fetch('Image'); } $c-> close; } The clear() call in the copying code is necessary so the newly written data will not mix with the old. At last, the newly registered formats can be accessed by a program: my $myformat = 'Very Special Old Pale Data Format'; if ( $c-> register_format($myformat)) { $c-> open; $c-> clear; $c-> store('Text', 'sample text'); $c-> store($myformat', 'sample ## text'); $c-> close; } On-demand storageUnder X11 it is possible to skip the generation of data in all possible clipboard format when when copying. The native X11 mechanism allows to ask the source application for the exact data format needed by the target application, and the toolkit uses special event "onClipboard" triggered on the application whenever necessary. By default this event handler responds to querying image in file encoded formats (gif,jpg) under X11 on the fly. It can be extended to generate other formats as well. See "Events" in Prima::Application Clipboard for the details. Custom formatsOnce registered, all processes in a GUI space can access the data by this format. The registration must take place also if a Prima-driven program needs to read data in a format, defined by an another program. In either case, the duplicate registration is a valid case. When no longer needed, a format can be de-registered. It is not a mandatory action, however - the toolkit cleans up before exit. Moreover, the system maintains a reference counter on the custom-registered formats; de-registering thus does not mean deletion. If two processes use a custom format, and one exits and re-starts, the other still can access the data in the same format, registered by its previous incarnation. UnicodeApplications can interchange text in both ascii and utf8, leaving the selection choice to reader programs. While it is possible to access both at the same time, by "fetch"'ing content of "Text" and "UTF8" clipboard slots, the widget proposes its own pasting scheme, where the mechanics are hidden under the "text" property call. The property is advised to be used instead of individual 'Text' and 'UTF8' formats. This method is used in all the standard widgets, and is implemented so the programmer can reprogram its default action by overloading "PasteText" notification of "Prima::Application" ( see "PasteText" in Prima::Application ). The default action of "PasteText" is to query first if 'Text' format is available, and if so, return the ascii text scalar. If "Prima::Application::wantUnicodeInput" is set (default), 'UTF8' format is checked before resorting to 'Text'. It is clear that this scheme is not the only possibly needed, for example, an application may want to ignore ASCII text, or, ignore UTF8 text but have "Prima::Application::wantUnicodeInput" set, etc. The symmetric action is "CopyText", that allows for a custom text conversion code to be installed. ImagesImage data can be transferred in different formats in different OSes. The lowest level is raw pixel data in display-based format, whereas GTK-based applications can also exchange images in file-based formats, such as bmp, png etc. To avoid further complications in the implementations, "PasteImage" action was introduced to handle these cases, together with a symmetrical "CopyImage". The default action of "PasteImage" is to check whether lossless encoded image data is present, and if so, load a new image from this data, before falling back to OS-dependent image storage. When storing the image on the clipboard, only the default format, raw pixel data is used. Under X11 the toolkit can also serve images encoded as file formats. Note: Under X11 you'll need to keep the image alive during the whole time it might get copied from the application - Prima doesn't keep a copy of the image, only the reference. Changing the image after it was stored in the clipboard will affect the clipboard content. Exact and meta formatsPrima registers two special meta formats, "Image" and "Text", that interoperate with the system clipboard, storing data in the format that matches best with system convention when copying and pasting images and text, correspondingly. It is recommended to use meta-format calls (has_format, text, image, copy, paste) rather than exact format calls (format_exists, store, fetch) when possible. Where the exact format method operate on a single format data storage, meta format calls may operate on several exact formats. F.ex. "text" can check whether there exists a UTF-8 text storage, before resorting to 8-bit text. "image" on X11 is even more complicated, and may use image codecs to transfer encoded PNG streams, for example. APIProperties
Methods
AUTHORDmitry Karasik, <dmitry@karasik.eu.org>. SEE ALSOPrima, Prima::Component, Prima::Application
|