diff --git a/gtktest.py b/gtktest.py new file mode 100644 index 0000000..56824f7 --- /dev/null +++ b/gtktest.py @@ -0,0 +1,64 @@ +from jgui.surface.surface import * +import pygtk, gtk, gobject, cairo +from gtk import gdk + +class JGUIWidget(gtk.DrawingArea): + buttons = {1:'mouse-left', + 2:'mouse-middle', + 3:'mouse-right'} + + def __init__(self, width, height): + super(JGUIWidget, self).__init__() + self.surf = Surface([width, height]) + self.connect("expose_event", self.expose) + self.connect("motion_notify_event", self.mousemoved) + self.connect("button_press_event", self.mouse_down) + self.connect("button_release_event", self.mouse_up) + self.add_events(gdk.BUTTON_PRESS_MASK | gdk.BUTTON_RELEASE_MASK | gdk.POINTER_MOTION_MASK) + gobject.timeout_add(int((1.0/60.0)*1000), self.tick) + self.width, self.height = width, height + self.set_size_request(width, height) + + def mouse_up(self, widget, event): + self.surf.inject_mouse_up(self.buttons[event.button]) + + def mouse_down(self, widget, event): + self.surf.inject_mouse_down(self.buttons[event.button]) + + def tick(self): + self.alloc = self.get_allocation() + rect = gdk.Rectangle(self.alloc.x, self.alloc.y, self.alloc.width, self.alloc.height) + self.window.invalidate_rect(rect, True) + return True + + def expose(self, widget, event): + self.surf.draw() + context = widget.window.cairo_create() + #Just set the source image to be the cairo image surface of the Surface object + context.set_source_surface(self.surf.csurface, 0, 0) + context.paint() + + def mousemoved(self, widget, event): + self.surf.inject_mouse_position([event.x, event.y]) + + +class GTKJGUI(object): + def __init__(self): + self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) + self.window.set_title('JGUI Test') + self.window.connect("realize", self.realize) + self.window.connect("delete_event", gtk.main_quit) + self.drawingarea = JGUIWidget(800, 800) + self.window.add(self.drawingarea) + self.drawingarea.show() + self.window.show() + gtk.main() + + def realize(self, widget): + pixmap = gtk.gdk.Pixmap(None, 1, 1, 1) + color = gtk.gdk.Color() + cursor = gtk.gdk.Cursor(pixmap, pixmap, color, color, 0, 0) + widget.window.set_cursor(cursor) + +if __name__ == '__main__': + GTKJGUI()