Using Haskell Gtk widgets with Graphics.SOE package - haskell

I have been working through the book The Haskell School of Expression by Paul Hudak and using its associated gtk based graphics library Graphics.SOE.Gtk (link to documentation) for small 2D drawing experiments.
This library is very basic however, and only really has the ability to draw shapes. At the moment I am writing some programs which require particular GUI widgets such as buttons and text boxes. My question is: Is it possible to use the drawing capabilities of the SOE library alongside the GUI widgets found in gtk2hs? E.g be able to write a program where I can click a button which causes the program to draw a triangle shape in another container in the same window.
I have searched online for a way to do this, but most tutorials suggest using cairo to do any graphics drawing with Gtk ; SOE graphic's API has the appearance of being a relatively self contained thing.

No, there's not a really meaningful way for soegtk and regular gtk to interact. The reason is that soegtk keeps all its data types abstract; this is good practice from a "makes it easy for the implementor to change the implementation without changing the interface" point of view but it can be a bit limiting from a "I'm just a user that wants to munge things in ways the interface don't promise to allow" point of view.
You could:
make a copy of the text of the single module in the soegtk package, and adjust the export line to export more things and happily break any abstraction boundaries you dislike
interact non-meaningfully; e.g. have your gtk button open an soegtk window with the graphics of interest
learn a different drawing library, say, cairo or diagrams

Related

TK (LTK) Paint Program (Raster)

I really want to make a raster paint program while keeping the pixel model CPU side.
I want to expose the underlying data structure to users of the program so they can extend it with their own functionality. (Think how Emacs provides a few primitives yet users extend the program indefinitely with Emacs Lisp)
Now, from what I can tell Tk (and LTK by extension) mostly favors a Canvas which deals with pinning premade objects to it acting as a growing queue. My need is going to be having functionality of simply editing smallish bitmaps. Is it even feasible to build a program as simple as Microsoft Paint in Tk? Keep in mind, I do NOT want vector shapes-I want a real time raster editor. The whole model of editing raster objects in real time seems to fight the way computer graphics are done today and I'm simply trying to find a system that allows me to edit an array of "pixels" and display them with sufficient "human speed".
I'm not bound to Ltk but it's one of the most complete toolkits we have in Common Lisp it can also send messages directly to Tk so we get new features as they roll out. I'm just kind of fighting a modern graphics model to maintain a clean API for users' scripting.

Moving a graphic over a background image

I have a new project but I'm really not sure where to start, other than I think GNAT Ada would be good. I taught myself Ada three years ago and I have managed some static graphics with GNAT but this is different.
Please bear with me, all I need is a pointer or two towards where I might start, I'm not asking for a solution. My history is in back-end languages that are now mostly obsolete, so graphics is still a bit of a challenge.
So, the project:
With a static background image (a photograph) - and a moveable line with an adjustable cursor somewhere between the ends of the line. I need to rotate the line and adjust the length, as well as to move it around the screen and slide the cursor along the line; I have no problem calculating the positions of each element of the line. Once in place, I need to report on the position of the cursor relative to the overall length of the line. I can probably handle the reporting with what I already know but I have no clue as to how to create a graphic that I can slide around over another image. In the past I have failed to detect mouse events in GNAT Ada and I am sure I will need to get on top of that - in fact if I could, I would probably manage to control the line but doing it over an existing image is beyond me.
If I am wrong to choose GNAT Ada for this, please suggest an alternative.
I have looked in Stackoverflow for anything similar but have found answers only for Blackberry and Java, neither of which seems relevant.
For background, this will be a useful means of measuring relative lengths of the features of insect bodies from photographs, hopefully to set up some definitive identification guides for closely-related species.
With a static background image (a photograph)
So first you need a window to put your interface in. You can get this from any GUI framework (link as given by trashgod in the comments).
and a moveable line with an adjustable cursor somewhere between the ends of the line. I need to rotate the line and adjust the length, as well as to move it around the screen and slide the cursor along the line; I have no problem calculating the positions of each element of the line.
These are affine transformations. They are commonly employed in low-level graphics rendering. You can, like Zerte suggested, employ OpenGL – however modern OpenGL has a steep learning curve for beginners.
GtkAda includes a binding to the Cairo graphics library which supports such transformations, so you can create a window with GtkAda with a Cairo surface and then render your image & line on it. Cairo does have a learning curve and I never used the Ada binding, so I cannot really give an opinion about how complex this will be.
Another library that fully supports what you want to do is SDL which has Ada bindings here. The difference to GtkAda is that SDL is a pure graphics drawing library so you need to „draw“ any interactive controls yourself. On the other hand, setting up a window via SDL and drawing things will be somewhat simpler than doing it via GtkAda & Cairo.
SFML which has also been mentioned in comments is on the same level as SDL. I do not know it well enough to give a more informed opinion but what I said about SDL will most probably also apply to SFML.
In the past I have failed to detect mouse events in GNAT Ada and I am sure I will need to get on top of that - in fact if I could, I would probably manage to control the line but doing it over an existing image is beyond me.
HID event processing is handled by whatever GUI library you use. If you use SDL, you'll get mouse events from SDL; if you use GTK, you'll get mouse events from GTK, and so on.

Graphical transformation handles in Haskell

I am experimenting with creating GUI and graphics based applications in Haskell using gtk2hs and cairo. Currently I am working on a program where a user can create and manipulate simple geometric shapes on screen.
The three manipulations I want the user to be able to do are: translation, rotation and scaling. The ideal implementation of this would have the transformation handles present in most image manipulation programs such as photoshop:
(i.e Where the object can be translated by dragging somewhere inside it, scaled by dragging the appropriate white box, and rotated by clicking and dragging in the direction of rotation outside of the object's box)
I cannot find a simple way of doing this "out-of-the-box" in either the gtk or cairo documentation, and have been unable to find a suitable library by searching on google. Does anyone know of a Haskell API which would allow me to manipulate graphics in this way or, failing that, know how I would go about implementing my own version of this type of functionality in Haskell?
There are not built-in widgets for this; you'll have to build it yourself by drawing all the appropriate elements (e.g. the actual shape, a bounding box or similar, rectangles on the corners and edges of the bounding bex, etc.) and handling mouse events by checking whether the events fall on these elements or not. It should not be difficult... though it may be a bit tedious.

How do I write an panel task bar in FLTK for use on Linux systems

I need to write a small application in C/C++ to implement a panel task bar like thing to display information along the top of a desktop window (specifically an xorg desktop on a Linux system). I need to avoid bloat and steep learning curves for the GUI programming.
My research is pointing me at GTK+/GTKmm or FLTK. It looks like FLTK is probably the simpler to get to grips with and the most likely to provide a small clean package with minimal dependencies. So I've based my research on FLTK so far.
I've been doing some reading and am struggling to find out how to write a basic program that will create a narrow undecorated window that covers the width of a monitor in such a way that maximising other applications would not obscure it. The FLTK tutorials I have found so far (including the FLTK documentation) only implement standard windows with borders that can be moved around the screen.
I'd like to start by writing a simple program in FLTK (or GTK+/GTKmm) that creates a 20 pixel deep bar across the with of the screen containing a "hello world" message. The bar's area would be reserved outside the area that other programs can access so that maximising another application would not hide the "hello world" message. I think this has something to do with a WM_STRUT_PARTIAL property but I can't find information about this in FLTK.
Doing this is partially to understand how to write a simple GUI program and partially to solve a specific need that I have.
I'm looking for any help/guidance to put me in the right direction to get started. Many thanks.
starfry, it is not a trivial task I believe. The problem is that your desktop (say GNOME2/Metacity) reserved that space, and paints its panel in the area where you want your bar. -
If you really want your tray-bar applet to be based on FLTK, the you would have to "embed" it in a (GNOME) applet. It was long ago when I did similar thing with SDL application, but I am afraid I forgot how to do it. The first thing that comes to my mind is to use somehow get the XID from the GNOME applet and somehow pass it to the FLTK part of it, and then let FLTK do the rest...
Sure, you may use another desktop, like KDE, or i3 or IceWM, they ALL have their own ways of dealing with the tray bar (there is no standard for it!) so, pardon my "French" - it is going to be a PITA to support all environments...
If I was on GNOME, i would write the applet entirely using GNOME/GTK. Forget FLTK in that case. That is my recommendation. If you target KDE, then do it using KDE/QT libraries (Plasma widget would be what to look for).
However, if you still want to use FLTK, start with the fltk::draw_into() function (it is probably called fl_draw_into() in FLTK 1.x), fltk::xid() and related functions.

Fast, Pixel Precision 2D Drawing API for Graphics App?

I woud like to create a cross-platform drawing program. The one requirement for writing my app is that I have pixel level precision over the canvas. For instance, I want to write my own line drawing algorithm rather than rely on someone elses. I do not want any form of anti-aliasing (again, pixel level control is required.) I would like the users interactions on the screen to be quick and responsive (pending my ability to write fast algorithms.)
Ideally, I would like to write this in Python, or perhaps Java as a second choice. The ability to easily make the final app cross-platform is a must. I will submit to different API's on different OS'es if necessary as long as I can write an abstraction layer around them. Any ideas?
addendum: I need the ability to draw on-screen. Drawing out to a file I've got figured out.
I just this week put together some slides and demo code for doing 2d graphics using OpenGL from python using the library pyglet. Here's a representative post: Pyglet week 2, better vertex throughput (or 3D stuff using the same basic ideas)
It is very fast (relatively speaking, for python) I have managed to get around 1,000 independently positioned and oriented objects moving around the screen, each with about 50 vertices.
It is very portable, all the code I have written in this environment works on windows and Linux and mac (and even obscure environments like Pypy) without me ever having to think about it.
Some of these posts are very old, with broken links between them. You should be able to find all the relevant posts using the 'graphics' tag.
The Pyglet library for Python might suit your needs. It lets you use OpenGL, a cross-platform graphics API. You can disable anti-aliasing and capture regions of the screen to a buffer or a file. In addition, you can use its event handling, resource loading, and image manipulation systems. You can probably also tie it into PIL (Python Image Library), and definitely Cairo, a popular cross-platform vector graphics library.
I mention Pyglet instead of pure PyOpenGL because Pyglet handles a lot of ugly OpenGL stuff transparently with no effort on your part.
A friend and I are currently working on a drawing program using Pyglet. There are a few quirks - for example, OpenGL is always double buffered on OS X, so we have to draw everything twice, once for the current frame and again for the other frame, since they are flipped whenever the display refreshes. You can look at our current progress in this subversion repository. (Splatterboard.py in trunk is the file you'll want to run.) If you're not up on using svn, I would be happy to email you a .zip of the latest source. Feel free to steal code if you look into it.
If language choice is open, a Flash file created with Haxe might have a place. Haxe is free, and a full, dynamic programming language. Then there's the related Neko, a virtual machine (like Java's, Ruby's, Parrot...) to run on Mac, Windows and Linux. Being in some ways a new improved form of Flash, naturally it can draw stuff. http://haxe.org/
QT's Canvas an QPainter are very good for this job if you'd like to use C++. and it is cross platform.
There is a python binding for QT but I've never used it.
As for Java, using SWT, pixel level manipulation of a canvas is somewhat difficult and slow so I would not recommend it. On the other hand Swing's Canvas is pretty good and responsive. I've never used the AWT option but you probably don't want to go there.
I would recommend wxPython
It's beautifully cross platform and you can get per pixel control and if you change your mind about that you can use it with libraries such as pyglet or agg.
You can find some useful examples for just what you are trying to do in the docs and demos download.

Resources