1 | from gi.repository import Gtk |
---|
2 | from gi.repository import GdkPixbuf |
---|
3 | from gi.repository import Gdk |
---|
4 | from gi.repository import Pango |
---|
5 | |
---|
6 | # The images used in this example are these ones: |
---|
7 | # http://git.gnome.org/browse/pygobject/tree/demos/gtk-demo/demos/data/apple-red.png |
---|
8 | # http://git.gnome.org/browse/pygobject/tree/demos/gtk-demo/demos/data/gnome-foot.png |
---|
9 | |
---|
10 | |
---|
11 | def _destroy_cb(widget, data=None): |
---|
12 | Gtk.main_quit() |
---|
13 | |
---|
14 | |
---|
15 | def drag_begin_event(widget, context, data): |
---|
16 | print 'drag_BEGIN_event' |
---|
17 | widget.drag_source_set_icon_pixbuf(widget.get_child().get_pixbuf()) |
---|
18 | |
---|
19 | |
---|
20 | def drag_data_get_event(widget, context, selection_data, info, |
---|
21 | timestamp, data): |
---|
22 | print 'drag_data_GET_event' |
---|
23 | selection_data.set_pixbuf(widget.get_child().get_pixbuf()) |
---|
24 | print 'selection_data.get_format:', selection_data.get_format() |
---|
25 | |
---|
26 | |
---|
27 | def drag_data_received_event(widget, drag_context, x, y, data, |
---|
28 | info, time, user_data): |
---|
29 | print 'drag_data_RECEIVED_event' |
---|
30 | if user_data == 'Image': |
---|
31 | widget.set_from_pixbuf(data.get_pixbuf()) |
---|
32 | elif user_data == 'TextView': |
---|
33 | buf = widget.get_buffer() |
---|
34 | buf.insert_pixbuf(buf.get_start_iter(), data.get_pixbuf()) |
---|
35 | Gtk.drag_finish(drag_context, True, False, time) |
---|
36 | |
---|
37 | window = Gtk.Window() |
---|
38 | window.set_title('Gtk3 Drag And Drop Example') |
---|
39 | window.set_default_size(300, 180) |
---|
40 | window.connect("destroy", _destroy_cb) |
---|
41 | |
---|
42 | |
---|
43 | image = Gtk.Image() |
---|
44 | imagebuf = GdkPixbuf.Pixbuf.new_from_file('apple-red.png') |
---|
45 | image.set_from_pixbuf(imagebuf) |
---|
46 | |
---|
47 | imagebox = Gtk.EventBox() |
---|
48 | imagebox.add(image) |
---|
49 | |
---|
50 | imagebox.drag_source_set( |
---|
51 | Gdk.ModifierType.BUTTON1_MASK, |
---|
52 | [], |
---|
53 | Gdk.DragAction.COPY) |
---|
54 | imagebox.drag_source_add_image_targets() |
---|
55 | |
---|
56 | imagebox.connect('drag-begin', drag_begin_event, None) |
---|
57 | imagebox.connect('drag-data-get', drag_data_get_event, None) |
---|
58 | |
---|
59 | destination_image = Gtk.Image() |
---|
60 | |
---|
61 | destination_image.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY) |
---|
62 | # destination_image.drag_dest_set_target_list(None) |
---|
63 | # destination_image.drag_dest_add_text_targets() |
---|
64 | destination_image.drag_dest_add_image_targets() |
---|
65 | |
---|
66 | destination_image.connect('drag-data-received', drag_data_received_event, |
---|
67 | 'Image') |
---|
68 | |
---|
69 | imagebuf = GdkPixbuf.Pixbuf.new_from_file('gnome-foot.png') |
---|
70 | destination_image.set_from_pixbuf(imagebuf) |
---|
71 | |
---|
72 | |
---|
73 | hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) |
---|
74 | hbox.pack_start(imagebox, True, True, 10) |
---|
75 | hbox.pack_start(destination_image, True, True, 10) |
---|
76 | |
---|
77 | label = Gtk.Label(label='Drag and drop the apple to the foot, ' |
---|
78 | 'and then drag the apple to the TextView at the bottom...') |
---|
79 | |
---|
80 | text = Gtk.TextView() |
---|
81 | text.set_wrap_mode(Gtk.WrapMode.WORD) |
---|
82 | text.set_editable(True) |
---|
83 | text.modify_font(Pango.FontDescription('arial 12')) |
---|
84 | text.connect('drag-data-received', drag_data_received_event, 'TextView') |
---|
85 | text.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY) |
---|
86 | # text.drag_dest_set_target_list(None) |
---|
87 | # text.drag_dest_add_text_targets() |
---|
88 | text.drag_dest_add_image_targets() |
---|
89 | |
---|
90 | vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) |
---|
91 | vbox.add(label) |
---|
92 | vbox.add(hbox) |
---|
93 | vbox.add(text) |
---|
94 | |
---|
95 | window.add(vbox) |
---|
96 | |
---|
97 | window.show_all() |
---|
98 | Gtk.main() |
---|