Search code examples
haskellgtk3

How to create a Pixbuf from ByteString in GTK's Haskell bindings?


Here is the code:

  let myFile = $(embedFile "img/termonad-lambda.png")
  let wordsss = unpack myFile
  img_ptr <- mallocBytes (length wordsss)
  forM_ (zip wordsss [0 ..]) $ \(word8, off) ->
    pokeByteOff img_ptr off (CUChar word8)
  pb <- pixbufNewFromData img_ptr ColorspaceRgb False 8 50 50 (50 * 3)
  GI.Gtk.Objects.Window.windowSetDefaultIcon pb

It uses GTK binding for Haskell for some GUI program. At the last line, I am trying to set an icon for the main window. That function accepts a Pixbuf. I am getting the Pixbuf from a ByteString. The first line embeds (using this library) an image into the binary, at compile time, as a ByteString. I get the following error on the last line:

 • Could not deduce: Data.GI.Base.Overloading.CheckForAncestorType
                          Pixbuf
                          gi-gdkpixbuf-2.0.29:GI.GdkPixbuf.Objects.Pixbuf.Pixbuf
                          (Data.GI.Base.Overloading.ParentTypes Pixbuf)
        arising from a use of ‘windowSetDefaultIcon’
  1. Can somebody please explain what this error means in human?
  2. Maybe I am converting the ByteString into Pixbuf incorrectly. Please suggest another way.

UPDATE: After @Ismor's helpful and correct answer, here is my final code for future googlers:

  let myFile = $(embedFile "img/termonad-lambda.png")
  let wordsss = unpack myFile
  img_ptr <- newArray (map CUChar wordsss)
  ccc <- GI.GLib.Structs.Bytes.bytesNewTake (Just myFile)
  pb <- pixbufNewFromBytes ccc ColorspaceRgb False 8 50 50 (50 * 3)
  GI.Gtk.Objects.Window.windowSetIcon win (Just pb)

Solution

  • From the type signature you are using pixbufNewFromData from package gtk whereas windowSetDefaultIcon comes from package gi-gtk-hs. That means you are depending on two different set of gtk bindings

    The package with most downloads is gtk but in its repo they recommend using gi-gtk-hs as they are automatic bindings created with GObject introspection, hence more complete bindings.