File: scrap.rst.txt

package info (click to toggle)
pygame 2.1.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,416 kB
  • sloc: ansic: 66,042; python: 46,176; javascript: 9,214; objc: 273; sh: 78; makefile: 56; cpp: 25
file content (240 lines) | stat: -rw-r--r-- 7,990 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
.. include:: common.txt

:mod:`pygame.scrap`
===================

.. module:: pygame.scrap
   :synopsis: pygame module for clipboard support.

| :sl:`pygame module for clipboard support.`

**EXPERIMENTAL!**: This API may change or disappear in later pygame releases. If
you use this, your code may break with the next pygame release.

The scrap module is for transferring data to/from the clipboard. This allows
for cutting and pasting data between pygame and other applications. Some basic
data (MIME) types are defined and registered:

::

   pygame         string
  constant        value        description
  --------------------------------------------------
  SCRAP_TEXT   "text/plain"    plain text
  SCRAP_BMP    "image/bmp"     BMP encoded image data
  SCRAP_PBM    "image/pbm"     PBM encoded image data
  SCRAP_PPM    "image/ppm"     PPM encoded image data

``pygame.SCRAP_PPM``, ``pygame.SCRAP_PBM`` and ``pygame.SCRAP_BMP`` are
suitable for surface buffers to be shared with other applications.
``pygame.SCRAP_TEXT`` is an alias for the plain text clipboard type.

Depending on the platform, additional types are automatically registered when
data is placed into the clipboard to guarantee a consistent sharing behaviour
with other applications. The following listed types can be used as strings to
be passed to the respective :mod:`pygame.scrap` module functions.

For **Windows** platforms, these additional types are supported automatically
and resolve to their internal definitions:

::

  "text/plain;charset=utf-8"   UTF-8 encoded text
  "audio/wav"                  WAV encoded audio
  "image/tiff"                 TIFF encoded image data

For **X11** platforms, these additional types are supported automatically and
resolve to their internal definitions:

::

  "text/plain;charset=utf-8"   UTF-8 encoded text
  "UTF8_STRING"                UTF-8 encoded text
  "COMPOUND_TEXT"              COMPOUND text

User defined types can be used, but the data might not be accessible by other
applications unless they know what data type to look for.
Example: Data placed into the clipboard by
``pygame.scrap.put("my_data_type", byte_data)`` can only be accessed by
applications which query the clipboard for the ``"my_data_type"`` data type.

For an example of how the scrap module works refer to the examples page
(:func:`pygame.examples.scrap_clipboard.main`) or the code directly in GitHub
(`pygame/examples/scrap_clipboard.py <https://github.com/pygame/pygame/blob/master/examples/scrap_clipboard.py>`_).

.. versionadded:: 1.8

.. note::
   The scrap module is currently only supported for Windows, X11 and Mac OS X.
   On Mac OS X only text works at the moment - other types may be supported in
   future releases.

.. function:: init

   | :sl:`Initializes the scrap module.`
   | :sg:`init() -> None`

   Initialize the scrap module.

   :raises pygame.error: if unable to initialize scrap module

   .. note:: The scrap module requires :func:`pygame.display.set_mode()` be
      called before being initialized.

   .. ## pygame.scrap.init ##

.. function:: get_init

   | :sl:`Returns True if the scrap module is currently initialized.`
   | :sg:`get_init() -> bool`

   Gets the scrap module's initialization state.

   :returns: ``True`` if the :mod:`pygame.scrap` module is currently
      initialized, ``False`` otherwise
   :rtype: bool

   .. versionadded:: 1.9.5

   .. ## pygame.scrap.get_init ##

.. function:: get

   | :sl:`Gets the data for the specified type from the clipboard.`
   | :sg:`get(type) -> bytes | None`

   Retrieves the data for the specified type from the clipboard. The data is
   returned as a byte string and might need further processing (such as
   decoding to Unicode).

   :param string type: data type to retrieve from the clipboard

   :returns: data (bytes object) for the given type identifier or ``None`` if
      no data for the given type is available
   :rtype: bytes | None

   ::

     text = pygame.scrap.get(pygame.SCRAP_TEXT)
     if text:
         print("There is text in the clipboard.")
     else:
         print("There does not seem to be text in the clipboard.")

   .. ## pygame.scrap.get ##

.. function:: get_types

   | :sl:`Gets a list of the available clipboard types.`
   | :sg:`get_types() -> list`

   Gets a list of data type string identifiers for the data currently
   available on the clipboard. Each identifier can be used in the
   :func:`pygame.scrap.get()` method to get the clipboard content of the
   specific type.

   :returns: list of strings of the available clipboard data types, if there
      is no data in the clipboard an empty list is returned
   :rtype: list

   ::

     for t in pygame.scrap.get_types():
         if "text" in t:
             # There is some content with the word "text" in its type string.
             print(pygame.scrap.get(t))

   .. ## pygame.scrap.get_types ##

.. function:: put

   | :sl:`Places data into the clipboard.`
   | :sg:`put(type, data) -> None`

   Places data for a given clipboard type into the clipboard. The data must
   be a string buffer. The type is a string identifying the type of data to be
   placed into the clipboard. This can be one of the predefined
   ``pygame.SCRAP_PBM``, ``pygame.SCRAP_PPM``, ``pygame.SCRAP_BMP`` or
   ``pygame.SCRAP_TEXT`` values or a user defined string identifier.

   :param string type: type identifier of the data to be placed into the
      clipboard
   :param data: data to be place into the clipboard, a bytes object
   :type data: bytes

   :raises pygame.error: if unable to put the data into the clipboard

   ::

     with open("example.bmp", "rb") as fp:
         pygame.scrap.put(pygame.SCRAP_BMP, fp.read())
     # The image data is now on the clipboard for other applications to access
     # it.
     pygame.scrap.put(pygame.SCRAP_TEXT, b"A text to copy")
     pygame.scrap.put("Plain text", b"Data for user defined type 'Plain text'")

   .. ## pygame.scrap.put ##

.. function:: contains

   | :sl:`Checks whether data for a given type is available in the clipboard.`
   | :sg:`contains(type) -> bool`

   Checks whether data for the given type is currently available in the
   clipboard.

   :param string type: data type to check availability of

   :returns: ``True`` if data for the passed type is available in the
      clipboard, ``False`` otherwise
   :rtype: bool

   ::

     if pygame.scrap.contains(pygame.SCRAP_TEXT):
         print("There is text in the clipboard.")
     if pygame.scrap.contains("own_data_type"):
         print("There is stuff in the clipboard.")

   .. ## pygame.scrap.contains ##

.. function:: lost

   | :sl:`Indicates if the clipboard ownership has been lost by the pygame application.`
   | :sg:`lost() -> bool`

   Indicates if the clipboard ownership has been lost by the pygame
   application.

   :returns: ``True``, if the clipboard ownership has been lost by the pygame
      application, ``False`` if the pygame application still owns the clipboard
   :rtype: bool

   ::

     if pygame.scrap.lost():
         print("The clipboard is in use by another application.")

   .. ## pygame.scrap.lost ##

.. function:: set_mode

   | :sl:`Sets the clipboard access mode.`
   | :sg:`set_mode(mode) -> None`

   Sets the access mode for the clipboard. This is only of interest for X11
   environments where clipboard modes ``pygame.SCRAP_SELECTION`` (for mouse
   selections) and ``pygame.SCRAP_CLIPBOARD`` (for the clipboard) are
   available. Setting the mode to ``pygame.SCRAP_SELECTION`` in other
   environments will not change the mode from ``pygame.SCRAP_CLIPBOARD``.

   :param mode: access mode, supported values are ``pygame.SCRAP_CLIPBOARD``
      and ``pygame.SCRAP_SELECTION`` (``pygame.SCRAP_SELECTION`` only has an
      effect when used on X11 platforms)

   :raises ValueError: if the ``mode`` parameter is not
      ``pygame.SCRAP_CLIPBOARD`` or ``pygame.SCRAP_SELECTION``

   .. ## pygame.scrap.set_mode ##

.. ## pygame.scrap ##