#!/usr/bin/env python
# [SNIPPET_NAME: gtk3 opengl example]
# [SNIPPET_CATEGORIES: opengl]
# [SNIPPET_TAGS: opengl, gtk3]
# [SNIPPET_DESCRIPTION: using gtk3 library lets draw using opengl]
# [SNIPPET_AUTHOR: Oliver Marks ]
# [SNIPPET_LICENSE: GPL]

import sys
from gi.repository import Gtk, GdkX11, Gdk
import cairo


class gui():

    def __init__(self):
        self.window = Gtk.Window()
        self.window.realize()
        self.window.resize(300, 300)
        self.window.set_resizable(True)
        self.window.set_reallocate_redraws(True)
        self.window.set_title("GTK3 with opengl")
        self.window.connect('delete_event', Gtk.main_quit)
        self.window.connect('destroy', lambda quit: Gtk.main_quit())

        self.fixed_area = Gtk.Layout()
        self.fixed_area.drag_dest_set(Gtk.DestDefaults.ALL, [], Gdk.DragAction.MOVE)
        self.fixed_area.connect("drag-data-received", self.on_drag_data_received)
        self.fixed_area.connect("drag-drop", self.on_drag_data_received)
        self.fixed_area.connect("drag-motion", self.on_drag_data_received)


        self.drawing_area = Gtk.DrawingArea()
        self.drawing_area.set_size_request(50,50)
        #self.drawing_area.set_border_width(50)
        self.drawing_area.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [],Gdk.DragAction.MOVE)

        #self.drawing_area.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, [],DRAG_ACTION)
        self.drawing_area.connect("drag-data-get", self.on_drag_data_get)
        self.drawing_area.connect("drag-begin", self.on_drag_data_get)
        self.drawing_area.connect("drag-end", self.on_drag_finish)

        #~ self.drawing_area.add_events(Gdk.EventMask.POINTER_MOTION_MASK)
        #~ self.drawing_area.add_events(Gdk.EventMask.SCROLL_MASK)
        self.drawing_area.add_events(Gdk.EventMask.TOUCH_MASK)
        #self.drawing_area.connect('configure_event', self.on_configure_event)
        #self.drawing_area.connect('draw', self.on_draw)
        self.drawing_area.connect('draw', self.expose)
        #self.drawing_area.connect('clicked', self.clicked)
        

        self.drawing_area.set_double_buffered(False)

        self.fixed_area.put(self.drawing_area,1,1)
        self.window.add(self.fixed_area)
        self.fixed_area.show_all()
        #self.window.add(self.drawing_area)
        print 'test'
        self.window.show_all()

    def on_drag_data_received(self, widget, drag_context, x,y, data,info, time):
        print 'drop'
        
    def on_drag_finish(self, widget, event):
        print event
        print dir(event.get_dest_window)
        print 'drop'

    def on_drag_data_get(self, widget, event):
        print 'drag'

    def on_configure_event(self, widget, event):
        return True

    def expose(self, widget, context):
        #self.context = widget.window.cairo_create()
        #context.rectangle(event.area.x, event.area.y,
        #                       event.area.width, event.area.height)
        #context.clip()
        context.set_source_rgb(1, 0.5, 1)
        context.rectangle(10, 10, 40, 40)

        context.move_to(0, 0)
        context.rel_line_to(100, 100)
        context.fill()
        context.save()


def main():
    g = gui()
    Gtk.main()

if __name__ == '__main__':
    main()
