#!/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(): x = 10 y = 10 current_x = 0 current_y = 0 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.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [],Gdk.DragAction.MOVE) 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.connect("motion-notify-event", self.move) self.drawing_area.add_events(Gdk.EventMask.TOUCH_MASK) self.drawing_area.connect('draw', self.expose) 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.show_all() def on_drag_data_received(self, widget, drag_context, x,y, data,info, time): self.x = x self.y = y print 'drop recieve' print x print y def on_drag_finish(self, widget, event): print event print dir(event.get_dest_window) print event.get_dest_window.get_type() self.x = self.current_x self.y = self.current_y print 'drop finish' def on_drag_data_get(self, widget, event): print 'drag' def on_configure_event(self, widget, event): return True def move(self, widget, event): self.current_x = event.x self.current_y = event.y print self.x def expose(self, widget, context): print 'expose' print self.x context.set_source_rgb(1, 0.5, 1) context.move_to(self.x, self.y) context.rectangle(self.x, self.y, self.x+40, self.y+40) #context.rel_line_to(100, 100) context.fill() context.save() def main(): g = gui() Gtk.main() if __name__ == '__main__': main()