#
#   Python GUI - Common Image/Pixmap facilities for Mac
#

from Carbon import QuickDraw, Qd, Qdoffs
from Geometry import rect_size

class MacGWorld:

    def _mac_init_gworld(self, bounds):
        gworld = Qdoffs.NewGWorld(0, bounds, None, None, 0)
        self._mac_gworld = gworld
        self._size = rect_size(bounds)
        return gworld
    
    def _mac_set_gworld(self):
        old_gworld = Qdoffs.GetGWorld()
        Qdoffs.SetGWorld(self._mac_gworld.as_GrafPtr(), None)
        return old_gworld
    
    def get_width(self):
        return self._size[0]
    
    def get_height(self):
        return self._size[1]

    def get_size(self):
        return self._size

    def draw(self, canvas, src_rect, dst_rect):
        src_pix = self._mac_gworld.as_GrafPtr().GetPortBitMapForCopyBits()
        dst_pix = Qd.GetPort().GetPortBitMapForCopyBits()
        # Would like to LockPixels(src_pix) here, but Qd module
        # doesn't seem to provide it.
        Qd.CopyBits(src_pix, dst_pix, src_rect, dst_rect, QuickDraw.srcCopy, None)
    