コード例 #1
0
def _ContextFromDC(dc):
    """
    Creates and returns a Cairo context object using the wxDC as the
    surface.  (Only window, client, paint and memory DC's are allowed
    at this time.)
    """
    if not isinstance(dc, wx.WindowDC) and not isinstance(dc, wx.MemoryDC):
        raise TypeError(
            "Only window and memory DC's are supported at this time.")

    if 'wxMac' in wx.PlatformInfo:
        width, height = dc.GetSize()

        # use the CGContextRef of the DC to make the cairo surface
        cgc = dc.GetHandle()
        assert cgc is not None, "Unable to get CGContext from DC."
        cgref = voidp(cgc)
        surfaceptr = voidp(surface_create(cgref, width, height))

        # create a cairo context for that surface
        ctxptr = voidp(cairo_create(surfaceptr))

        # Turn it into a pycairo context object
        ctx = pycairoAPI.Context_FromContext(ctxptr, pycairoAPI.Context_Type,
                                             None)

        # The context keeps its own reference to the surface
        cairoLib.cairo_surface_destroy(surfaceptr)

    elif 'wxMSW' in wx.PlatformInfo:
        # This one is easy, just fetch the HDC and use PyCairo to make
        # the surface and context.
        hdc = dc.GetHandle()
        # Ensure the pointer value is clampped into the range of a C signed long
        hdc = ctypes.c_long(hdc)
        surface = cairo.Win32Surface(hdc.value)
        ctx = cairo.Context(surface)

    elif 'wxGTK' in wx.PlatformInfo:
        if 'gtk3' in wx.PlatformInfo:
            # With wxGTK3, GetHandle() returns a cairo context directly
            ctxptr = voidp(dc.GetHandle())

            # pyCairo will try to destroy it so we need to increase ref count
            cairoLib.cairo_reference(ctxptr)
        else:
            # Get the GdkDrawable from the dc
            drawable = voidp(dc.GetHandle())

            # Call a GDK API to create a cairo context
            ctxptr = gdkLib.gdk_cairo_create(drawable)

        # Turn it into a pycairo context object
        ctx = pycairoAPI.Context_FromContext(ctxptr, pycairoAPI.Context_Type,
                                             None)

    else:
        raise NotImplementedError("Help  me, I'm lost...")

    return ctx
コード例 #2
0
def ContextFromDC(dc):
    """
    Creates and returns a Cairo context object using the wxDC as the
    surface.  (Only window, client, paint and memory DC's are allowed
    at this time.)
    """
    if not isinstance(dc, wx.WindowDC) and not isinstance(dc, wx.MemoryDC):
        raise TypeError, "Only window and memory DC's are supported at this time."

    if 'wxMac' in wx.PlatformInfo:
        width, height = dc.GetSize()

        # use the CGContextRef of the DC to make the cairo surface
        cgc = dc.GetCGContext()
        assert cgc is not None, "Unable to get CGContext from DC."
        cgref = voidp(cgc)
        surfaceptr = voidp(
            cairoLib.cairo_quartz_surface_create_for_cg_context(
                cgref, width, height))

        # create a cairo context for that surface
        ctxptr = cairoLib.cairo_create(surfaceptr)

        # Turn it into a pycairo context object
        ctx = pycairoAPI.Context_FromContext(ctxptr, pycairoAPI.Context_Type,
                                             None)

        # The context keeps its own reference to the surface
        cairoLib.cairo_surface_destroy(surfaceptr)

    elif 'wxMSW' in wx.PlatformInfo:
        # This one is easy, just fetch the HDC and use PyCairo to make
        # the surface and context.
        hdc = dc.GetHDC()
        surface = cairo.Win32Surface(hdc)
        ctx = cairo.Context(surface)

    elif 'wxGTK' in wx.PlatformInfo:
        gdkLib = _findGDKLib()

        # Get the GdkDrawable from the dc
        drawable = voidp(dc.GetGdkDrawable())

        # Call a GDK API to create a cairo context
        gdkLib.gdk_cairo_create.restype = ctypes.c_void_p
        ctxptr = gdkLib.gdk_cairo_create(drawable)

        # Turn it into a pycairo context object
        ctx = pycairoAPI.Context_FromContext(ctxptr, pycairoAPI.Context_Type,
                                             None)

    else:
        raise NotImplementedError, "Help  me, I'm lost..."

    return ctx
コード例 #3
0
ファイル: surfaces.py プロジェクト: Mortal/graphing
 def __new__(cls, *args, **kwargs):
     import cairo
     return cairo.Win32Surface(*args, **kwargs)