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
|
from gi.repository import Gtk
from gi.repository import GdkPixbuf
from gi.repository import Gdk
from gi.repository import Pango
# The images used in this example are these ones:
# http://git.gnome.org/browse/pygobject/tree/demos/gtk-demo/demos/data/apple-red.png
def _destroy_cb(widget, data=None):
Gtk.main_quit()
def drag_begin_event(widget, context, data):
print 'drag_BEGIN_event'
widget.drag_source_set_icon_pixbuf(widget.get_child().get_pixbuf())
def drag_data_get_event(widget, context, selection_data, info,
timestamp, data):
print 'drag_data_GET_event'
selection_data.set_pixbuf(widget.get_child().get_pixbuf())
def drag_data_received_event(widget, drag_context, x, y, data,
info, time, user_data):
print 'drag_data_RECEIVED_event'
if user_data == 'Image':
widget.set_from_pixbuf(data.get_pixbuf())
elif user_data == 'TextView':
buf = widget.get_buffer()
buf.insert_pixbuf(buf.get_start_iter(), data.get_pixbuf())
Gtk.drag_finish(drag_context, True, False, time)
window = Gtk.Window()
window.set_title('Gtk3 Drag And Drop Example')
window.set_default_size(300, 180)
window.connect("destroy", _destroy_cb)
image = Gtk.Image()
imagebuf = GdkPixbuf.Pixbuf.new_from_file('apple-red.png')
image.set_from_pixbuf(imagebuf)
imagebox = Gtk.EventBox()
imagebox.add(image)
imagebox.drag_source_set(
Gdk.ModifierType.BUTTON1_MASK,
[],
Gdk.DragAction.COPY)
imagebox.drag_source_add_image_targets()
imagebox.connect('drag-begin', drag_begin_event, None)
imagebox.connect('drag-data-get', drag_data_get_event, None)
hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
hbox.pack_start(imagebox, True, True, 10)
text = Gtk.TextView()
text.set_wrap_mode(Gtk.WrapMode.WORD)
text.set_editable(True)
text.modify_font(Pango.FontDescription('arial 12'))
text.connect('drag-data-received', drag_data_received_event, 'TextView')
text.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)
# text.drag_dest_set_target_list(None)
text.drag_dest_add_text_targets()
text.drag_dest_add_image_targets()
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
vbox.add(hbox)
vbox.add(text)
window.add(vbox)
window.show_all()
Gtk.main()
|