#
#   Python GUI - Views - Gtk
#

from math import floor, ceil
import gtk
from gtk import gdk
from Canvases import Canvas
from Events import Event
from GViews import View as GView
#from GtkInput import GtkInput

class View(GView):

    _extent_origin = (0, 0)

    def __init__(self, _gtk_outer = None, **kwds):
        gtk_layout = gtk.Layout()
        gtk_layout.add_events(gdk.EXPOSURE_MASK)
        gtk_layout.show()
        self._gtk_connect(gtk_layout, 'expose-event',
            self._gtk_expose_event_signal)
        if _gtk_outer:
            _gtk_outer.add(gtk_layout)
        else:
            _gtk_outer = gtk_layout
        GView.__init__(self, _gtk_outer = _gtk_outer, _gtk_inner = gtk_layout,
            _gtk_focus = gtk_layout, _gtk_input = gtk_layout)
        #GtkInput.__init__(self, gtk_layout)
        self.set(**kwds)
    
    #
    #		Other methods
    #

    def with_canvas(self, proc):
        hadj, vadj = self._gtk_adjustments()
        clip = rect_sized((hadj.value, vadj.value), self.size)
        canvas = Canvas(self._gtk_inner_widget.bin_window, self._extent_origin, clip)
        proc(canvas)
    
    def invalidate_rect(self, (l, t, r, b)):
        gdk_window = self._gtk_inner_widget.bin_window
        if gdk_window:
            x0, y0 = self._extent_origin
            gdk_rect = (int(floor(l - x0)), int(floor(t - y0)),
                int(ceil(r - l)), int(ceil(b - t)))
            #print "View.invalidate_rect: gdk_rect =", gdk_rect ###
            gdk_window.invalidate_rect(gdk_rect, 0)
    
    def update(self):
        gdk_window = self._gtk_inner_widget.bin_window
        gdk_window.process_updates()

    #
    #		Internal
    #

    def _gtk_expose_event_signal(self, gtk_event):
        #print "View._gtk_expose_event_signal:", self ###
        l, t, w, h = gtk_event.area
        clip = (l, t, l + w, t + h)
        #print "...clip =", clip ###
        gtk_layout = self._gtk_inner_widget
        canvas = Canvas(gtk_layout.bin_window, self._extent_origin, clip)
        self.draw(canvas, clip)
