#!/usr/bin/env python2
# I, Danny Milosavljevic, hereby place this file into the public domain.

import pygtk
pygtk.require("2.0")
import gtk
import gobject
import sys
import time
import gobjectc
import array
SCREEN_WIDTH = 0
SCREEN_HEIGHT = 0

class EventBox(gtk.EventBox):
    def __init__(self, controls):
        gtk.EventBox.__init__(self)
        self.controls = controls
        self.props.can_focus = True
        #self.pressed_keys = set()
        #self.keymap = gtk.gdk.keymap_get_default()
        self.connect("key-press-event", self.handle_key_press)
        self.connect("key-release-event", self.handle_key_release)
        self.connect("button-press-event", self.handle_button_press)
        self.connect("motion-notify-event", self.handle_mouse_motion)
    def handle_button_press(self, widget, event):
        self.grab_focus()
        return False
    def handle_mouse_motion(self, widget, event):
        return self.controls.handle_mouse_motion(event.x, event.y, event.state)
    def handle_key_press(self, widget, event):
        return self.controls.handle_key_press(event.hardware_keycode)
    def handle_key_release(self, widget, event):
        # hardware_keycode
        return self.controls.handle_key_release(event.hardware_keycode)
        #self.pressed_keys.discard(event.keycode)
class View(object): # graphical part.
    def __init__(self, toplevel, controls):
        global SCREEN_WIDTH
        global SCREEN_HEIGHT
        self.screen = toplevel.PPU.screen
        SCREEN_WIDTH = self.screen.width
        SCREEN_HEIGHT = self.screen.height
        native_pixbuf = self.screen.get_rendered_pixbuf()
        if native_pixbuf != 0:
            self.pixbuf = gobjectc.PyGObjectCPAI().pygobject_new(native_pixbuf)
            self.B_create_flip_pixbuf = False
        else:
            self.pixbuf = None
            self.B_create_flip_pixbuf = True
        #self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, SCREEN_WIDTH, SCREEN_HEIGHT)
        #self.pixbuf.fill(0x000000FF)
        self.window = gtk.Window()
        self.event_box = EventBox(controls)
        self.drawing_area = gtk.DrawingArea()
        self.drawing_area.connect("realize", self.allocate_GC)
        self.drawing_area.connect("expose-event", self.repaint_X)
        self.drawing_area.set_size_request(SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2) # FIXME make configurable.
        self.drawing_area.show()
        self.event_box.show()
        gobject.timeout_add(33, self.repaint_T)
        box = gtk.HBox()
        self.event_box.add(self.drawing_area)
        box.pack_start(self.event_box, False, False)
        box.pack_start(controls, False, False)
        box.show()
        self.window.add(box)
        self.window.show_all()
    def allocate_GC(self, widget, *args, **kwargs):
        self.GC = widget.window.new_gc()
    def repaint_X(self, widget, event):
        self.repaint()
    def repaint_T(self):
        self.repaint()
        return(True)
    def repaint(self):
        if self.B_create_flip_pixbuf:
            s = self.screen.pixbuf_obj.get_rendered_image()
            assert(len(s) == 245760)
            self.pixbuf = gtk.gdk.pixbuf_new_from_data(s, gtk.gdk.COLORSPACE_RGB, True, 8, SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_WIDTH * 4) # TODO optimize!!
            self.pixbuf = self.pixbuf.scale_simple(SCREEN_WIDTH*2, SCREEN_HEIGHT*2, gtk.gdk.INTERP_NEAREST)
        widget = self.drawing_area
        if widget.window: # window already realized
            #print("YEP", data)
            widget.window.draw_pixbuf(self.GC, self.pixbuf, 0, 0, 0, 0, SCREEN_WIDTH * 2, SCREEN_HEIGHT * 2, gtk.gdk.RGB_DITHER_NONE, 0, 0)
            #drawable(self.pixmap_GC, self.pixmap, 0, 0, 0, 0, -1, -1)
