extensions.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Sat, 27 Dec 2014 00:45:13 +0100
changeset 28 1bd3d147cd77
parent 22 834ae4dd6815
child 30 c8fe298c8cc7
permissions -rw-r--r--
Added utility methods to SimpleView... ...to ease writing of Cairo-based views.

"{ Package: 'stx:goodies/libcairo' }"!

!DeviceGraphicsContext methodsFor:'cairo support'!

drawableId

    ^drawableId

    "Created: / 10-07-2008 / 10:20:04 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!GraphicsDevice methodsFor:'cairo support'!

cairoSurfaceFor: view

    self error:'Graphics device not supported'

    "Created: / 10-07-2008 / 10:16:21 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!GraphicsDevice methodsFor:'accessing'!

displayId
    ^ displayId

    "Created: / 04-07-2008 / 12:58:56 / Jan Vrany <vranyj1@fel.cvut.cz>"
! !

!SimpleView methodsFor:'accessing - cairo'!

cairo
    "Return a Cairo context for drawing onto this view"           
    ^ gc cairo

    "Created: / 10-09-2008 / 18:23:11 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified (comment): / 26-12-2014 / 23:29:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairo
    | cr |

    cr := self cairo.
    [  
        self redrawWithCairo: cr
    ] ensure:[ 
        cr release
    ].

    "Created: / 27-12-2014 / 00:30:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairo: cr x: x y: y width: w height: h
    cr rectangleX: x  y: y width: w height: h. 
    cr clip.
    self redrawWithCairo: cr

    "Created: / 27-12-2014 / 00:29:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoBuffered
    | cr |

    cr := self cairo.
    [  
        self redrawWithCairoBuffered: cr
    ] ensure:[ 
        cr release
    ].

    "Created: / 27-12-2014 / 00:30:27 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoBuffered: view_cr

    | image_surface image_cr |

    [     
        image_surface := Cairo::Surface forImageFormatARGB32width: self width  height: self height.
        image_cr := Cairo::GraphicsContext on: image_surface. 
        self redrawWithCairo: image_cr.  
        view_cr setSourceSurface: image_surface. 
        view_cr paint.
    ] ensure:[ 
        image_surface notNil ifTrue:[ image_surface release ].
        image_cr notNil ifTrue:[ image_cr release ]
    ].

    "Created: / 27-12-2014 / 00:13:29 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoBuffered: view_cr x: x y: y width: w height: h

    | image_surface image_cr |

    [     
        image_surface := Cairo::Surface forImageFormatARGB32width: self width  height: self height.
        image_cr := Cairo::GraphicsContext on: image_surface. 
        image_cr rectangleX: x  y: y width: w height: h. 
        image_cr clip.
        self redrawWithCairo: image_cr x: x y: y width: w height: h.
        view_cr rectangleX: x  y: y width: w height: h. 
        view_cr clip.
        view_cr setSourceSurface: image_surface. 
        view_cr paint.
    ] ensure:[ 
        image_surface notNil ifTrue:[ image_surface release ].
        image_cr notNil ifTrue:[ image_cr release ]
    ].

    "Created: / 27-12-2014 / 00:28:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoBufferedX: x y: y width: w height: h     
    | cr |

    cr := self cairo.
    [  
        self redrawWithCairoBuffered: cr x: x y: y width: w height: h     
    ] ensure:[ 
        cr release
    ].

    "Created: / 27-12-2014 / 00:31:33 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!SimpleView methodsFor:'redrawing - cairo'!

redrawWithCairoX: x y: y width: w height: h     
    | cr |

    cr := self cairo.
    [  
        self redrawWithCairo: cr x: x y: y width: w height: h     
    ] ensure:[ 
        cr release
    ].

    "Created: / 27-12-2014 / 00:31:22 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!XGraphicsContext methodsFor:'accessing'!

cairo
    "Return a Cairo context for drawing onto this GC"    
    ^ Cairo::GraphicsContext on: self cairoSurfaceId

    "Created: / 26-12-2014 / 23:28:49 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!XGraphicsContext methodsFor:'accessing'!

cairoSurfaceId
    | view |

    view := device viewFromId: drawableId.
    cairoSurfaceId isNil ifTrue:[ 
        cairoSurfaceId := device cairoSurfaceFor: view.
        ^ cairoSurfaceId
    ].
    "/ Adjust width and height
    cairoSurfaceId width: view width height: view height.
    ^ cairoSurfaceId

    "Modified: / 26-12-2014 / 23:30:03 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!XWorkstation methodsFor:'cairo support'!

cairoSurfaceFor: view

    | surface |
    surface := Cairo::Surface
                forXlib: displayId
                drawable: view drawableId address
                visual: self queryDefaultVisual
                width: view width
                height: view height.
    surface setView: view.
    "/view addDependent: surface.
    ^surface

    "Created: / 10-07-2008 / 10:16:36 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 09-09-2008 / 22:57:22 / Jan Vrany <vranyj1@fel.cvut.cz>"
    "Modified: / 21-09-2014 / 00:54:10 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!stx_goodies_libcairo class methodsFor:'documentation'!

extensionsVersion_HG

    ^ '$Changeset: <not expanded> $'
! !