Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example using "Use cairo for drawing" #55

Closed
joe9 opened this issue Sep 7, 2016 · 1 comment
Closed

Example using "Use cairo for drawing" #55

joe9 opened this issue Sep 7, 2016 · 1 comment

Comments

@joe9
Copy link

joe9 commented Sep 7, 2016

Hello,

Thanks for sharing haskell-gi.

Just want to check if there is an example or sample code using haskell-gi to render to a gtk3 window using Cairo.

Thanks

@joe9 joe9 closed this as completed Sep 8, 2016
@garetxe
Copy link
Collaborator

garetxe commented Sep 10, 2016

Hi,

There was no example, but I had been meaning to get around to this for a while, so I decided to finally write an example. Please see https://github.com/haskell-gi/haskell-gi/blob/master/examples/Cairo.hs , which is a straight port of the cairo-clock example in gtk2hs to haskell-gi (you will need GHC 8.0 to compile this example).

The issue is that there is no useful introspection data in cairo itself, so the haskell-gi bindings are not useful at all in themselves. Luckily one can use the hand-written bindings in the cairo library without much effort. Everything is fairly straightforward, the only tricky part is the following function:

-- | This function bridges gi-cairo with the hand-written cairo
-- package. It takes a `GI.Cairo.Context` (as it appears in gi-cairo),
-- and a `Render` action (as in the cairo lib), and renders the
-- `Render` action into the given context.
renderWithContext :: GI.Cairo.Context -> Render () -> IO ()
renderWithContext ct r = withManagedPtr ct $ \p ->
runReaderT (runRender r) (Cairo (castPtr p))

I hope this helps. If anything is confusing please do not hesitate to ask.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants