Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/gtk3_test_draganddrop.py
blob: c78d08976fc15175e47106f632a82b25616969da (plain)
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
from gi.repository import Gtk, Gdk, GdkPixbuf


class DragDropWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Drag and Drop Demo")

        hbox = Gtk.Box(spacing=12)
        self.add(hbox)

        self.drag_source = DragSource()
        self.drop_area = DropArea()

        hbox.pack_start(self.drag_source, True, True, 0)
        hbox.pack_start(self.drop_area, True, True, 0)

        self.drag_source.drag_source_add_image_targets()
        self.drop_area.drag_dest_add_image_targets()


class DragSource(Gtk.EventBox):

    def __init__(self):
        Gtk.EventBox.__init__(self)
        self.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [],
                             Gdk.DragAction.COPY)

        self.connect('drag-begin', self.drag_begin_event)
        self.connect("drag-data-get", self.on_drag_data_get)

        image = Gtk.Image()
        self.pixbuf = GdkPixbuf.Pixbuf.new_from_file('apple-red.png')
        image.set_from_pixbuf(self.pixbuf)
        self.add(image)

    def drag_begin_event(self, widget, context):
        self.drag_source_set_icon_pixbuf(self.pixbuf)

    def on_drag_data_get(self, widget, drag_context, data, info, time):
        print "DRAG-N-DROP"


class DropArea(Gtk.EventBox):

    def __init__(self):
        Gtk.EventBox.__init__(self)
        self.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.COPY)

        destination_image = Gtk.Image()
        imagebuf = GdkPixbuf.Pixbuf.new_from_file('gnome-foot.png')
        destination_image.set_from_pixbuf(imagebuf)
        self.add(destination_image)


win = DragDropWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()