1pod::Prima::Clipboard(3U)ser Contributed Perl Documentatipoond::Prima::Clipboard(3)
2
3
4
6 Prima::Clipboard - GUI interprocess data exchange
7
9 Prima::Clipboard class is a descendant of Prima::Component. It serves
10 as an interface to the specific data storage, called clipboard, visible
11 to all clients of one GUI space. The system clipboard is intended for
12 the exchange of information of an arbitrary type between graphic
13 applications.
14
16 my $c = $::application-> Clipboard;
17
18 # paste data
19 my $string = $c-> text;
20 my $image = $c-> image;
21 my $other = $c-> fetch('Other type');
22
23 # copy datum
24 $c-> text( $string);
25
26 # copy data
27 $c-> open;
28 $c-> text( $string);
29 $c-> image( $image);
30 $c-> store( $image);
31 $c-> close;
32
33 # clear
34 $c-> clear;
35
37 Prima::Clipboard provides access to the system clipboard data storage.
38 For the easier communication, the system clipboard has one 'format'
39 field, that is stored along with the data. This field is used to
40 distinguish between data formats. Moreover, a clipboard can hold
41 simultaneously several data instances, of different data formats. Since
42 the primary usage of a clipboard is 'copying' and 'pasting', an
43 application can store copied information in several formats, increasing
44 possibility that the receiving application recognizes the data.
45
46 Different systems provide spectrum of predefined data types, but the
47 toolkit uses only three of these - ascii text, utf8 text, and image. It
48 does not limit, however, the data format being one of these three types
49 - an application is free to register its own formats. Both predefined
50 and newly defined data formats are described by a string, and the three
51 predefined formats are represented by 'Text', 'UTF8', and 'Image'
52 string constants.
53
54 The most frequent usage of Prima::Clipboard is to preform two tasks -
55 copying and pasting. Both can be exemplified by the following:
56
57 my $c = $::application-> Clipboard;
58
59 # paste
60 my $string = $c-> text;
61
62 # copy
63 $c-> text( $string);
64
65 This simplistic code hides other aspects of Prima::Clipboard class.
66
67 First, the default clipboard is accessible by an implicit name call, as
68 an object named 'Clipboard'. This scheme makes it easily overridable.
69 A more important point is, that the default clipboard object might be
70 accompanied by other clipboard objects. This is the case with X11
71 environment, which defines also 'Primary' and 'Secondary' system
72 clipboards. Their functionality is identical to the default clipboard,
73 however. "get_standard_clipboards()" method returns strings for the
74 clipboards, provided by the system.
75
76 Second, code for fetching and storing multi-format data is somewhat
77 different. Clipboard is viewed as a shared system resource, and have
78 to be 'opened', before a process can grab it, so other processes can
79 access the clipboard data only after the clipboard is 'closed' ( Note:
80 It is not so under X11, where there the clipboard locking is advisory,
81 and any process can grab clipboard at any time) .
82
83 "fetch()" and "store()" implicitly call "open()" and "close()", but
84 these functions must be called explicitly for the multi-format data
85 handling. The code below illustrates the said:
86
87 # copy text and image
88 if ( $c-> open) {
89 $c-> clear;
90 $c-> store('Text', $string);
91 $c-> store('Image', $image);
92 $c-> close;
93 }
94
95 # check present formats and paste
96 if ( $c-> open) {
97 if ( $c-> format_exists('Text')) {
98 $string = $c-> fetch('Text');
99 }
100 # or, check the desired format alternatively
101 my %formats = map { $_ => 1 } $c-> get_formats;
102 if ( $formats{'Image'}) {
103 $image = $c-> fetch('Image');
104 }
105
106 $c-> close;
107 }
108
109 The clear() call in the copying code is necessary so the newly written
110 data will not mix with the old.
111
112 At last, the newly registered formats can be accessed by a program:
113
114 my $myformat = 'Very Special Old Pale Data Format';
115 if ( $c-> register_format($myformat)) {
116 $c-> open;
117 $c-> clear;
118 $c-> store('Text', 'sample text');
119 $c-> store($myformat', 'sample ## text');
120 $c-> close;
121 }
122
123 Custom formats
124 Once registered, all processes in a GUI space can access the data by
125 this format. The registration must take place also if a Prima-driven
126 program needs to read data in a format, defined by an another program.
127 In either case, the duplicate registration is a valid event. When no
128 longer needed, a format can be de-registered. It is not a mandatory
129 action, however - the toolkit cleans up before exit. Moreover, the
130 system maintains a reference counter on the custom-registered formats;
131 de-registering does not mean deletion, thus. If two processes use a
132 custom format, and one exits and re-starts, it still can access the
133 data in the same format, registered by its previous incarnation.
134
135 Unicode
136 In real life, application often interchange text in both ascii and
137 utf8, leaving the choice to reader programs. While it is possible to
138 access both at the same time, by "fetch"'ing content of "Text" and
139 "UTF8" clipboard slots, widgets implement their own pasting scheme. To
140 avoid hacking widget code, usage of "text" property is advised instead
141 of indicating 'Text' and 'UTF8' constants. This method is used in
142 standard widgets, and is implemented so the programmer can reprogram
143 its default action by overloading "PasteText" notification of
144 "Prima::Application" ( see "PasteText" in Prima::Application ).
145
146 The default action of "PasteText" is to query first if 'Text' format is
147 available, and if so, return the ascii text scalar. If
148 "Prima::Application::wantUnicodeInput" is set, 'UTF8' format is checked
149 before resorting to 'Text'. It is clear that this scheme is not the
150 only possibly needed, for example, an application may want to ignore
151 ASCII text, or, ignore UTF8 text but have
152 "Prima::Application::wantUnicodeInput" set, etc.
153
154 The symmetric action is "CopyText", that allows for a custom text
155 conversion code to be installed.
156
157 Images
158 Image data can be transferred in different formats in different OSes.
159 The lowest level is raw pixel data in display-based format, whereas
160 GTK-based applications can also exchange images in file-based formats,
161 such as bmp, png etc. To avoid further complications in the
162 implementations, "PasteImage" action was introduced to handle these
163 cases, together with a symmetrical "CopyImage".
164
165 The default action of "PasteImage" is to query first if 'Image' format
166 is available, and if so, return the Image object. This by default reads
167 data from raw image buffer, but if fails, on unix the logic proceeds by
168 checking data in formats 'image/bmp', 'image/png' etc. "BMP" is
169 checked first because the corresponding codec is always compiled in
170 Prima, it doesn't depend on external libraries. Next is checked "PNG"
171 format, because it is lossless, then "TIFF", then all others.
172
173 When storing the image on the clipboard, only the default format, raw
174 pixel data is used.
175
177 Properties
178 image OBJECT
179 Provides access to an image, stored in the system clipboard. In
180 get-mode call, return "undef" if no image is stored.
181
182 text STRING
183 Provides access to text stored in the system clipboard. In get-
184 mode call, return "undef" if no text information is present.
185
186 Methods
187 clear
188 Deletes all data from clipboard.
189
190 close
191 Closes the open/close brackets. open() and close() can be called
192 recursively; only the last close() removes the actual clipboard
193 locking, so other processes can use it as well.
194
195 deregister_format FORMAT_STRING
196 De-registers a previously registered data format. Called
197 implicitly for all not de-registered format before a clipboard
198 object is destroyed.
199
200 fetch FORMAT_STRING
201 Returns the data of FORMAT_STRING data format, if present in the
202 clipboard. Depending on FORMAT_STRING, data is either text string
203 for 'Text' format, Prima::Image object for 'Image' format and a
204 binary scalar value for all custom formats.
205
206 format_exists FORMAT_STRING
207 Returns a boolean flag, showing whether FORMAT_STRING format data
208 is present in the clipboard or not.
209
210 get_handle
211 Returns a system handle for a clipboard object.
212
213 get_formats
214 Returns array of strings, where each is a format ID, reflecting the
215 formats present in the clipboard.
216
217 Only the predefined formats, and the formats registered via
218 "register_format()" are returned. There is no way to see if a
219 format, not registered before, is present.
220
221 get_registered_formats
222 Returns array of strings, each representing a registered format.
223 "Text" and "Image" are returned also.
224
225 get_standard_clipboards
226 Returns array of strings, each representing a system clipboard. The
227 default "Clipboard" is always present. Other clipboards are
228 optional. As an example, this function returns only "Clipboard"
229 under win32, but also "Primary" and "Secondary" under X11. The
230 code, specific to these clipboards must refer to this function
231 first.
232
233 open
234 Opens a system clipboard and locks it for the process single use;
235 returns a success flag. Subsequent "open" calls are possible, and
236 always return 1. Each "open()" must correspond to "close()",
237 otherwise the clipboard will stay locked until the blocking process
238 is finished.
239
240 register_format FORMAT_STRING
241 Registers a data format under FORMAT_STRING string ID, returns a
242 success flag. If a format is already registered, 1 is returned. All
243 formats, registered via "register_format()" are de-registered with
244 "deregister_format()" when a program is finished.
245
246 store FORMAT_STRING, SCALAR
247 Stores SCALAR value into the clipboard in FORMAT_STRING data
248 format. Depending of FORMAT_STRING, SCALAR is treated as follows:
249
250 FORMAT_STRING SCALAR
251 ------------------------------------
252 Text text string in ASCII
253 UTF8 text string in UTF8
254 Image Prima::Image object
255 other formats binary scalar value
256
257 NB. All custom formats treated as a binary data. In case when the
258 data are transferred between hosts with different byte orders no
259 implicit conversions are made. It is up to the programmer whether
260 to convert the data in a portable format, or leave it as is. The
261 former option is of course preferable. As far as the author knows,
262 the Storable module from CPAN collection provides the system-
263 independent conversion routines.
264
266 Dmitry Karasik, <dmitry@karasik.eu.org>.
267
269 Prima, Prima::Component, Prima::Application
270
271
272
273perl v5.30.0 2019-08-21 pod::Prima::Clipboard(3)