Ticket #3766: gtk3-drag-and-drop-example.py

File gtk3-drag-and-drop-example.py, 3.1 KB (added by humitos, 12 years ago)

Drag N Drop Gtk3 Example

Line 
1from gi.repository import Gtk
2from gi.repository import GdkPixbuf
3from gi.repository import Gdk
4from 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
11def _destroy_cb(widget, data=None):
12    Gtk.main_quit()
13
14
15def 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
20def 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
27def 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
37window = Gtk.Window()
38window.set_title('Gtk3 Drag And Drop Example')
39window.set_default_size(300, 180)
40window.connect("destroy", _destroy_cb)
41
42
43image = Gtk.Image()
44imagebuf = GdkPixbuf.Pixbuf.new_from_file('apple-red.png')
45image.set_from_pixbuf(imagebuf)
46
47imagebox = Gtk.EventBox()
48imagebox.add(image)
49
50imagebox.drag_source_set(
51            Gdk.ModifierType.BUTTON1_MASK,
52            [],
53            Gdk.DragAction.COPY)
54imagebox.drag_source_add_image_targets()
55
56imagebox.connect('drag-begin', drag_begin_event, None)
57imagebox.connect('drag-data-get', drag_data_get_event, None)
58
59destination_image = Gtk.Image()
60
61destination_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()
64destination_image.drag_dest_add_image_targets()
65
66destination_image.connect('drag-data-received', drag_data_received_event,
67                          'Image')
68
69imagebuf = GdkPixbuf.Pixbuf.new_from_file('gnome-foot.png')
70destination_image.set_from_pixbuf(imagebuf)
71
72
73hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)
74hbox.pack_start(imagebox, True, True, 10)
75hbox.pack_start(destination_image, True, True, 10)
76
77label = Gtk.Label(label='Drag and drop the apple to the foot, '
78                  'and then drag the apple to the TextView at the bottom...')
79
80text = Gtk.TextView()
81text.set_wrap_mode(Gtk.WrapMode.WORD)
82text.set_editable(True)
83text.modify_font(Pango.FontDescription('arial 12'))
84text.connect('drag-data-received', drag_data_received_event, 'TextView')
85text.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)
86# text.drag_dest_set_target_list(None)
87# text.drag_dest_add_text_targets()
88text.drag_dest_add_image_targets()
89
90vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
91vbox.add(label)
92vbox.add(hbox)
93vbox.add(text)
94
95window.add(vbox)
96
97window.show_all()
98Gtk.main()