#
#		Python GUI - Images - Gtk
#

from gtk import gdk
from GtkImageScaling import gtk_scale_pixbuf
from GImages import Image as GImage

class Image(GImage):

    def _init_from_file(self, file):
        self._gdk_pixbuf = gdk.pixbuf_new_from_file(file)
    
    def get_width(self):
        return self._gdk_pixbuf.get_width()
    
    def get_height(self):
        return self._gdk_pixbuf.get_height()
    
    def draw(self, canvas, src_rect, dst_rect):
        sx, sy, sr, sb = src_rect
        dx, dy, dr, db = dst_rect
        sw = sr - sx
        sh = sb - sy
        dw = dr - dx
        dh = db - dy
        gdk_pixbuf = self._gdk_pixbuf
        if sw <> dw or sh <> dh:
            #gdk_sub_pixbuf = self._gdk_pixbuf.new_subpixbuf(sx, sy, sw, sh)
            #gdk_scaled_pixbuf = gdk_sub_pixbuf.scale_simple(dw, dh, gdk.INTERP_BILINEAR)
            
#			gdk_scaled_pixbuf = gdk.Pixbuf(
#				gdk_pixbuf.get_colorspace(), gdk_pixbuf.get_has_alpha(),
#				gdk_pixbuf.get_bits_per_sample(), dw, dh)
#			xscale = float(dw) / sw
#			yscale = float(dh) / sh
#			xoffset = - xscale * sx
#			yoffset = - yscale * sy
#			gdk_pixbuf.scale(gdk_scaled_pixbuf, 0, 0, dw, dh,
#				xoffset, yoffset, xscale, yscale, gdk.INTERP_BILINEAR)

            gdk_scaled_pixbuf = gtk_scale_pixbuf(gdk_pixbuf, sx, sy, sw, sh, dw, dh)
                
            gdk_scaled_pixbuf.render_to_drawable(
                canvas._gdk_drawable, canvas._gdk_gc,
                0, 0, dx, dy, dw, dh, gdk.RGB_DITHER_NORMAL, 0, 0)
        else:
            self._gdk_pixbuf.render_to_drawable(
                canvas._gdk_drawable, canvas._gdk_gc,
                sx, sy, dx, dy, sw, sh, gdk.RGB_DITHER_NORMAL, 0, 0)
