Haskell, GTK4 and pictures

As part of my experiments with GTK4 and Haskell, I sought an application with a picture which would report on the co-ordinates if clicked on with a mouse. For my example image, I took the Flammarion wood engraving.

gtk-picture

With Stack, I created a new single-package project gtk-picture with Main.hs:

and package.yaml (extract):

and Stack project-level configuration:

GTK4

Including a picture was straightforward: there is a GTK4 widget Picture, and an action to create one from a file is yielded by pictureNewForFilename. I used a file Flammarion573x480.png located in a directory resources (as indicated by the package.yaml extract). I also used a Paths_gtk_picture module, autogenerated by Cabal. In that regard, I used Hpack’s modern behaviour by specifying spec-version: 0.36.0.

Handling the clicking on the picture also turned out to be straightforward. There is a GTK4 event controller GestureClick, and an action to create one is provided by new. The event controller provides a signal pressed and a callback function can be connected to it. So, it was a matter of using #addController to yield an action that added that event controller to the picture.

The result (stack exec -- gtk-picture) was as below:

Packaging

stack build reports that it has installed the executable in directory ...\gtk-picture.stack-work\install\5a333380\bin. The structure of the 5a333380 directory (being the directory returned by command stack path --local-install-root) includes:

Executing gtk-picture in the Stack environment puts certain MSYS2 directories on the PATH. Executing it outside of that environment results in errors if those directories are not otherwise on the PATH. For example:

The ldd command, provided by MSYS2, can be used to identify the dynamic link libraries (DLL) on which gtk-picture.exe depends:

Each line output by ldd begins with a tab character. The DLL located in /mingw64/bin can be isolated using a regular expression:

Further, each of those isolated DLL can be copied to the same directory as the executable:

Now, running the executable gtk-picture.exe does not result in errors. However, running the executable from Windows also results in a terminal opening. This can be avoided by specifying the ghc-option -optl-mwindows, which specifies the option -mwindows at the linking stage.