With Haskell and Gtk2hs, how do I create a new widget and related events?

I have an application I'm working on, and I mainly program the GUI for self-study. I asked quite a question about programers.stackexchange. This question concerns the mechanics of an idea that I have not tried.

I have three widgets: TreeView, TextField and DrawingArea. Each of the three widgets interacts very closely with events on one necessarily triggering action with the other. These three widgets basically do not interact with the rest of the application, except (for now), reading the MVar containing the global state of the application.

Currently, I can not come up with a single case where a larger application should interact directly with any of these three widgets. In addition, this identical model will be replicated to view other data having the same form. So, it seems to me that it makes sense to actually link these three widgets together with a large composite widget that can interact with a regular GTK event queue. So for example

type MyDataViewWidget = (TreeView, TextField, DrawingArea) data DataUpdatedSignal a = DataUpdatedSignal a data RedrawEvent a = RedrawEvent a 

So, the widget will use a DataUpdatedEvent to indicate to the rest of the application that something inside MyDataViewWidget has changed, and RedrawEvent will tell the widget that it needs to redraw or re-read the original data.

(technically, I didn’t think semantically what the various actions in the composite widget will do ... whether the widgets will have only a copy of the application data for reading and should receive new copies only for reading with RedrawEvent or maybe the widgets will have MVAR and allow modify data in MVar, etc. Now I'm wondering how to actually do this)

Are there any examples of doing something like this? Basically, what instances do I need to implement to create a new widget and two signals? I would rather stick with Haskell, but I could go down to C to create a new widget.

+4
source share
1 answer

Unfortunately, there is currently no pure-Haskell way (correctly) to implement a class like Widget . You will need to implement your widget in C and then import it through FFI. There are many examples of this - basically all gtk + / gtk2hs are a collection of hundreds of examples of this.

+5
source

Source: https://habr.com/ru/post/1435346/


All Articles