Drawing text on GTK's DrawingArea in Haskell
Asked Answered
F

2

1

I have a DrawingArea onto which I can draw using primitives such as drawRectangle and drawLine. How do I draw text onto that area? I'm most interested in something that quickly outputs a single line of text.

Graphics.UI.Gtk.Gdk.Drawable.layoutLine seems to be what I want, but it wants a Graphics.Rendering.Pango.Layout.LayoutLine as input. How do I construct that LayoutLine?

Are there better alternatives than doing it this way?

Thanks!

Fulvia answered 14/3, 2011 at 6:25 Comment(0)
F
1

I've found the way to do this with Pango.

layout <- widgetCreateLayout drawAreaWidget stringToDraw

Then you can use this newly-created layout with functions such as drawLayout and drawLayoutWithColors.

Fulvia answered 20/3, 2011 at 20:47 Comment(0)
M
4

I don't know if you would consider using Cairo. If so, I think the function showText may be what you are looking for. Use the cairo function moveTo to move to a specific location before writing the text. This is one of the simplest working examples I can produce:

import Graphics.UI.Gtk
import Graphics.Rendering.Cairo

main :: IO ()
main = do
    initGUI
    window      <- windowNew
    drawingArea <- drawingAreaNew
    containerAdd window drawingArea

    drawingArea `onExpose` (\_ -> renderScene drawingArea)
    window `onDestroy` mainQuit

    windowSetDefaultSize window 640 480
    widgetShowAll window
    mainGUI

renderScene :: DrawingArea -> IO Bool
renderScene da = do
    dw <- widgetGetDrawWindow da
    renderWithDrawable dw $ do setSourceRGBA 0.5 0.5 0.5 1.0
                               moveTo 100.0 100.0
                               showText "HelloWorld"
    return True

I found the following to be an excellent guide, even though it was not for Haskell: http://zetcode.com/tutorials/cairographicstutorial/cairotext/

Myron answered 18/3, 2011 at 10:0 Comment(0)
F
1

I've found the way to do this with Pango.

layout <- widgetCreateLayout drawAreaWidget stringToDraw

Then you can use this newly-created layout with functions such as drawLayout and drawLayoutWithColors.

Fulvia answered 20/3, 2011 at 20:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.