Fiddling with Gtk+

Tagged:  •    •    •    •    •  

I spent the last couple of weekends hacking with GTK+ making some custom widgets. The idea is that after 3.1 is out I would like to work on a nice (and innovative) GUI for GNU Smalltalk, and I wanted to get up to speed with GTK+.

I made quite a few widgets (I'm at 6 classes right now). All of them are interesting things on their own, even though each of them has at least 50% of the code copied from somewhere else. I published them in a git repository at http://www.inf.unisi.ch/phd/bonzini/webdav/gtk-widgets.git (expect rebasing on all branches but master).

My first two experiments were a "resizer" widget and an "ellipsis" widget. The first is a resizing grip similar to the one in status bars, but to resize a widget vertically. It can be useful to make a textarea widget smaller or larger. The code here was taken mostly by GtkPaned and GtkVPaned.

The second is similar to an "expander" widget (and indeed that's where I took a lot of code from) but has a different UI: in particular, the label and ellipsis disappear when you expand the widget. It is meant to be used to alternate a brief non-editable view with a full-blown editable view.

[pictures will come soon]

The other classes are part of a more complex system to bring stack-flow layout to GTK+.

It is a very cool system that allows to make web-like user interfaces; it is different from what GTK+ uses for its geometry management, and I could not find a way to retrofit it. Well, the stack looks like just a GtkVBox, but it seems to me that it actually does not match well with the GTK+ concept that widgets have to provide their final size before you lay them down in the window. Not only a flow can benefit of extra width and height and can use it to reduce the space it uses; a stack knows that and indeed only allocates to a flow the space that it needs.

For now I wrote four classes: the main class (inspired by GtkLayout); the abstract class in the composite design pattern (inspired by GtkContainer); the leaf class (inspired by GtkBin); the stack layout manager (at last I wrote something of my own). I don't have flows yet actually, but the code is already more than an expensive GtkVBox. Also, the design of the classes is already complete, and I am positive that I will not need to change the interface when I write the flow class. They should be very similar to stack, which is only ~250 lines of code. Keep in mind that this is OO programming in C and you have to write a lot of boilerplate code; compare to 2500 lines for the existing parts, and to almost 3000 for the resizer+ellipsis pair.

Later on, I want to write some visual layout managers, i.e. widgets that the user can interact with to affect the layout. I am not sure, but I think I can write this simply as an adaptor (I already have an adaptor for leaf widgets, but it does not work for GtkContainers). In particular, a likely possibility is to use interfaces and add this behavior to the existing GTK+ classes. It would be quite ironic that you can do this (add new interfaces to existing classes) in C and not in Java.

Let's see in the comments if someone guesses what this is up to... (Hint: there was a very short-lived thread in the GNU Smalltalk mailing list). There is a lot of work to do, and I hope the possibility to shape up your own programming tools will bring some more people to GNU Smalltalk. Luckily, there are also a lot of technologies that we can use. For example, I would like to base my work on a framework similar to OmniBrowser, but extended to support arbitrary data (instead of just a text box) using Magritte descriptions for them.

More ideas, encouragement, code reviews, funding for what is now just a week-end+commute-time project is welcome.

Io is using OpenGL as its graphics backend and building their widgets on top of that.

User login