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
|
#!/usr/bin/env python
# -*- Mode: Python; py-indent-offset: 4 -*-
# vim: tabstop=4 shiftwidth=4 expandtab
#
# Copyright (C) 2010 Red Hat, Inc., John (J5) Palmieri <johnp@redhat.com>
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA
title = "Clipboard"
description = """
GtkClipboard is used for clipboard handling. This demo shows how to
copy and paste text to and from the clipboard.
It also shows how to transfer images via the clipboard or via
drag-and-drop, and how to make clipboard contents persist after
the application exits. Clipboard persistence requires a clipboard
manager to run.
"""
from gi.repository import Gtk, Gdk
class ClipboardApp:
def __init__(self):
self.window = Gtk.Window()
self.window.set_title('Clipboard demo')
self.window.connect('destroy', lambda w: Gtk.main_quit())
vbox = Gtk.VBox(homogeneous=False, spacing=0)
vbox.set_border_width(8)
self.window.add(vbox)
label = Gtk.Label(label='"Copy" will copy the text\nin the entry to the clipboard')
vbox.pack_start(label, False, False, 0)
hbox = Gtk.HBox(homogeneous=False, spacing=4)
hbox.set_border_width(8)
vbox.pack_start(hbox, False, False, 0)
# create first entry
entry = Gtk.Entry()
hbox.pack_start(entry, True, True, 0)
# create button
button = Gtk.Button.new_from_stock(Gtk.STOCK_COPY)
hbox.pack_start(button, False, False, 0)
button.connect('clicked', self.copy_button_clicked, entry)
label = Gtk.Label(label='"Paste" will paste the text from the clipboard to the entry')
vbox.pack_start(label, False, False, 0)
hbox = Gtk.HBox(homogeneous=False, spacing=4)
hbox.set_border_width(8)
vbox.pack_start(hbox, False, False, 0)
# create secondary entry
entry = Gtk.Entry()
hbox.pack_start(entry, True, True, 0)
# create button
button = Gtk.Button.new_from_stock(Gtk.STOCK_PASTE)
hbox.pack_start(button, False, False, 0)
button.connect('clicked', self.paste_button_clicked, entry)
label = Gtk.Label(label='Images can be transferred via the clipboard, too')
vbox.pack_start(label, False, False, 0)
hbox = Gtk.HBox(homogeneous=False, spacing=4)
hbox.set_border_width(8)
vbox.pack_start(hbox, False, False, 0)
# create the first image
image = Gtk.Image(stock=Gtk.STOCK_DIALOG_WARNING,
icon_size=Gtk.IconSize.BUTTON)
ebox = Gtk.EventBox()
ebox.add(image)
hbox.add(ebox)
# make ebox a drag source
ebox.drag_source_set(Gdk.ModifierType.BUTTON1_MASK,
None, Gdk.DragAction.COPY)
ebox.drag_source_add_image_targets()
ebox.connect('drag-begin', self.drag_begin, image)
ebox.connect('drag-data-get', self.drag_data_get, image)
# accept drops on ebox
ebox.drag_dest_set(Gtk.DestDefaults.ALL,
None, Gdk.DragAction.COPY)
ebox.drag_dest_add_image_targets()
ebox.connect('drag-data-received', self.drag_data_received, image)
# context menu on ebox
ebox.connect('button-press-event', self.button_press, image)
# create the second image
image = Gtk.Image(stock=Gtk.STOCK_STOP,
icon_size=Gtk.IconSize.BUTTON)
ebox = Gtk.EventBox()
ebox.add(image)
hbox.add(ebox)
# make ebox a drag source
ebox.drag_source_set(Gdk.ModifierType.BUTTON1_MASK,
None, Gdk.DragAction.COPY)
ebox.drag_source_add_image_targets()
ebox.connect('drag-begin', self.drag_begin, image)
ebox.connect('drag-data-get', self.drag_data_get, image)
# accept drops on ebox
ebox.drag_dest_set(Gtk.DestDefaults.ALL,
None, Gdk.DragAction.COPY)
ebox.drag_dest_add_image_targets()
ebox.connect('drag-data-received', self.drag_data_received, image)
# context menu on ebox
ebox.connect('button-press-event', self.button_press, image)
# tell the clipboard manager to make data persistent
# FIXME: Allow sending strings a Atoms and convert in PyGI
atom = Gdk.atom_intern('CLIPBOARD', True)
clipboard = Gtk.Clipboard.get(atom)
clipboard.set_can_store(None)
self.window.show_all()
def copy_button_clicked(self, button, entry):
# get the default clipboard
atom = Gdk.atom_intern('CLIPBOARD', True)
clipboard = entry.get_clipboard(atom)
# set the clipboard's text
# FIXME: don't require passing length argument
clipboard.set_text(entry.get_text(), -1)
def paste_received(self, clipboard, text, entry):
if text is not None:
entry.set_text(text)
def paste_button_clicked(self, button, entry):
# get the default clipboard
atom = Gdk.atom_intern('CLIPBOARD', True)
clipboard = entry.get_clipboard(atom)
# set the clipboard's text
clipboard.request_text(self.paste_received, entry)
def get_image_pixbuf(self, image):
# FIXME: We should hide storage types in an override
storage_type = image.get_storage_type()
if storage_type == Gtk.ImageType.PIXBUF:
return image.get_pixbuf()
elif storage_type == Gtk.ImageType.STOCK:
(stock_id, size) = image.get_stock()
return image.render_icon(stock_id, size, None)
return None
def drag_begin(self, widget, context, data):
pixbuf = self.get_image_pixbuf(data)
Gtk.drag_set_icon_pixbuf(context, pixbuf, -2, -2)
def drag_data_get(self, widget, context, selection_data, info, time, data):
pixbuf = self.get_image_pixbuf(data)
selection_data.set_pixbuf(pixbuf)
def drag_data_received(self, widget, context, x, y, selection_data, info, time, data):
if selection_data.get_length() > 0:
pixbuf = selection_data.get_pixbuf()
data.set_from_pixbuf(pixbuf)
def copy_image(self, item, data):
# get the default clipboard
atom = Gdk.atom_intern('CLIPBOARD', True)
clipboard = Gtk.Clipboard.get(atom)
pixbuf = self.get_image_pixbuf(data)
clipboard.set_image(pixbuf)
def paste_image(self, item, data):
# get the default clipboard
atom = Gdk.atom_intern('CLIPBOARD', True)
clipboard = Gtk.Clipboard.get(atom)
pixbuf = clipboard.wait_for_image()
if pixbuf is not None:
data.set_from_pixbuf(pixbuf)
def button_press(self, widget, event, data):
if event.button != 3:
return False
self.menu = Gtk.Menu()
item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_COPY, None)
item.connect('activate', self.copy_image, data)
item.show()
self.menu.append(item)
item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_PASTE, None)
item.connect('activate', self.paste_image, data)
item.show()
self.menu.append(item)
self.menu.popup(None, None, None, None, event.button, event.time)
def main(demoapp=None):
ClipboardApp()
Gtk.main()
if __name__ == '__main__':
main()
|