#
#   Python GUI - Pixmap - Gtk
#

from gtk import gdk
from GtkImageScaling import gtk_scale_pixbuf
from Canvases import Canvas
from GPixmaps import Pixmap as GPixmap

class Pixmap(GPixmap):

    def __init__(self, width, height):
        gdk_root = gdk.get_default_root_window()
        self._gdk_pixmap = gdk.Pixmap(gdk_root, width, height, -1)
    
    def get_width(self):
        return self.size[0]
    
    def get_height(self):
        return self.size[1]
    
    def get_size(self):
        return self._gdk_pixmap.get_size()
    
    def draw(self, canvas, src_rect, dst_rect):
        gdk_drawable = canvas._gdk_drawable
        gdk_gc = canvas._gdk_gc
        gdk_pixmap = self._gdk_pixmap
        sx, sy, sr, sb = src_rect
        dx, dy, dr, db = dst_rect
        sw = sr - sx
        sh = sb - sy
        dw = dr - dx
        dh = db - dy
        if sw == dw and sh == dh:
            gdk_drawable.draw_drawable(gdk_gc, gdk_pixmap,
                sx, sy, dx, dy, sw, sh)
        else:
            color_space = gdk.COLORSPACE_RGB
            color_map = gdk_drawable.get_colormap()
            gdk_pixbuf = gdk.Pixbuf(color_space, 0, 8, sw, sh)
            gdk_pixbuf.get_from_drawable(gdk_pixmap, color_map,
                sx, sy, 0, 0, sw, sh)
            gdk_scaled_pixbuf = gtk_scale_pixbuf(gdk_pixbuf, 0, 0, sw, sh, dw, dh)
            gdk_scaled_pixbuf.render_to_drawable(
                gdk_drawable, gdk_gc, 0, 0, dx, dy, dw, dh,
                gdk.RGB_DITHER_NORMAL, 0, 0)

    def with_canvas(self, proc):
        canvas = Canvas(self._gdk_pixmap, (0, 0), self.bounds)
        proc(canvas)

    