tags:

views:

642

answers:

3

I'm not asking for a code implementation, but given GTK+'s skillset, what would be from an abstract perspective the best way to implement a grid such that each square is clickable and the like?

+2  A: 

Personally to do this I would use a table - http://library.gnome.org/devel/gtk/stable/GtkTable.html - and fill it with buttons - http://library.gnome.org/devel/gtk/stable/GtkButton.html. From there if you didn't want the buttons to look like they were buttons I would then set the relief - http://library.gnome.org/devel/gtk/stable/GtkButton.html#gtk-button-set-relief - to None.

Doing it this way will give you a grid where each element is the same size and an event is triggered when you click an item in the grid.

The Above assumes that the grid will have more than one row, if you only plan to have one row you would be better off using either the GtkHButtonBox - http://library.gnome.org/devel/gtk/stable/GtkHButtonBox.html - or the GtkVButtonBox -http://library.gnome.org/devel/gtk/stable/GtkVButtonBox.html.

Mike Lowen
A: 

I second mlowen's suggestion to use GtkTable for the main organization of the grid, it's very easy to get a grid-like look. You might also want to investigate populating each cell with a plain GtkEventBox, if you don't need the built-in drawing of the button.

The eventbox is an invisible widget, that basically adds a window and makes the covered area clickable. It's useful to make some of GTK+'s otherwise "silent" widgets more interactive, for instance it's commonly used behind a GtkLabel to make it clickable; if the label provides enough drawing capability for your needs, that's a possible way to continue too.

unwind
+1  A: 

If the grid will be large, you could also consider GtkTree.

It can display trees as grids, and supports rectangular selection. Cells can display text, numbers, and simple widgets like push buttons, toggles, option menus, progress bars, etc. It performs well, even on large datasets (many thousands of rows) and has nice features like column sort and so on.

The API is rather complex however :-( It's a full MVC thing and takes a little poking about to get it working well.

There's some example code in gtk-demo: look at the "Editable Cells" demo under "Tree View".

jrgc