#!/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 cairo")
        self.window.connect('delete_event', Gtk.main_quit)
        self.window.connect('destroy', lambda quit: Gtk.main_quit())

        self.drawing_area = Gtk.DrawingArea()
        self.drawing_area.set_size_request(50,50)

        self.drawing_area.add_events(Gdk.EventMask.TOUCH_MASK)
        self.drawing_area.connect('draw', self.expose)
        self.drawing_area.set_double_buffered(False)

        self.window.add(self.drawing_area)
        self.window.show_all()

    def expose(self, widget, context):
        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()
