Cairo__Examples2.st
author Jan Vrany <jan.vrany@fit.cvut.cz>
Thu, 25 Feb 2016 20:55:41 +0000
changeset 50 239120c68187
child 51 5293f2b851ab
permissions -rw-r--r--
Added classes with examples and example viewers * Cairo::Examples1 (and viewer Cairo::Examples1Viewer) contains some examples of using Cairo API for drawing. * Cairo::Examples2 (and viewer Cairo::Examples2Viewer) contains examples of using CairoGraphicsContext. The viewer application show result of the drawing on both native DeviceGraphicsContext and CairoGraphicsContext so one can easily compare results. Cairo should give better results.

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

"{ NameSpace: Cairo }"

TestCase subclass:#Examples2
	instanceVariableNames:'view surface'
	classVariableNames:''
	poolDictionaries:''
	category:'Cairo-Examples'
!

!Examples2 class methodsFor:'accessing'!

isTestSelector:aSelector
    | method |

    aSelector isNil ifTrue:[ ^ false ].
    method := self lookupMethodFor: aSelector.
    ^ method notNil 
        and:[(method hasAnnotation: #example:) or:[ method hasAnnotation: #example:category: ] ]

    "
    Cairo::Examples1 isTestSelector: #example01:
    "

    "Created: / 26-02-2016 / 21:57:20 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples2 class methodsFor:'private'!

testSelectors
        "The API method is allTestSelectors which now includes #shouldInheritSelectors and so handles all cases.  Unlike that method, this does not guarantee to return a sorted ordered collection."

        ^self sunitSelectors select: [:each | self isTestSelector: each ]

    "Created: / 26-02-2016 / 22:40:48 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples2 methodsFor:'examples'!

example01: gc <example: '01 - Rectangles'>
gc lineWidth: 1.
gc paint: Color blue.
gc fillRectangleX: 10 y: 10 width: gc width - 20 height: gc height - 20.
gc paint: Color red.
gc displayRectangleX: 10 y: 10 width: gc width - 20 height: gc height - 20.
gc paint: Color blue.
gc fillRectangleX: 10 y: 10 + ((gc height - 20) / 2) rounded width: gc width - 20 height: ((gc height - 20) / 2) rounded.
gc paint: Color black.
gc displayLineFromX: 1 y: (gc height / 2) rounded toX: gc width y: (gc height / 2) rounded.

    "Created: / 26-02-2016 / 22:57:09 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

example02: gc <example: '01 - Rectangles (rounded)'>
gc lineWidth: 10.
gc paint: Color red.
gc displayRoundRectangleX: 10 y: 10 width: gc width - 20 height: gc height - 20
   wCorner: 30 hCorner: 30.
gc paint: Color blue lighter.
gc fillRoundRectangleX: 10 y: 10 rounded width: gc width - 20 height: (gc height - 20) rounded
   wCorner: 30 hCorner: 30.
gc paint: Color black.
gc lineWidth: 1.
gc displayLineFromX: 1 y: (gc height / 2) rounded toX: gc width y: (gc height / 2) rounded.

    "Created: / 27-02-2016 / 09:04:11 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples2 methodsFor:'private'!

performTest
    | dGC cGC |

    dGC := view instVarNamed: #gc.
    cGC := CairoGraphicsContext onDeviceGraphicsContext: dGC.
    [
        view instVarNamed: #gc put: dGC.    
        self perform: testSelector sunitAsSymbol with: view.
        view instVarNamed: #gc put: cGC.    
        self perform: testSelector sunitAsSymbol with: view. 
    ] ensure:[ 
        view instVarNamed: #gc put: dGC.    
        cGC destroyCR.
    ].

    "Created: / 26-02-2016 / 22:01:46 / Jan Vrany <jan.vrany@fit.cvut.cz>"
    "Modified: / 27-02-2016 / 09:01:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !

!Examples2 methodsFor:'running'!

setUp
    | top |
    self assert: Display notNil.
    top := StandardSystemView new.
    top origin: 10@10 extent: 200@100.       
    view := SimpleView origin: 0.0 @ 0.0 corner: 1.0 @ 1.0 in: top.
    top openAndWait.

    "Created: / 26-02-2016 / 22:15:15 / Jan Vrany <jan.vrany@fit.cvut.cz>"
!

tearDown
    view notNil ifTrue:[ 
        view topView close.
    ].

    "Created: / 26-02-2016 / 22:18:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
! !