...
3. dynamically growing arrays
...
It uses growing GArrays and converts it to gfloat* when we need
to talk "GtkDataboxese". I'm using this on a project of mine (
http://i.imgur.com/s585f.png)
and the result is as expected. The only thing that needs to be
done from the "outside" is updating the GtkDatabox (redrawing
it). Like connecting the "notify::length" signal to a simple
function to handle a rescale (hack to make it work I'm actually
using).
The attached code is somewhat a rewrite of the original
gtkdatabox_xyc_graph and gtkdatabox_lines with added push_point
and some NULL pointers safety. To use it in any project:
{
...
/* Create the databox as usual */
GtkWidget *table;
GtkWidget *box;
gtk_databox_create_box_with_scrollbars_and_rulers(
&box, &table, TRUE, TRUE, TRUE, TRUE);
/* Set a color for the graph */
GdkColor color;
color.red = 0;
color.green = 0;
color.blue = 0;
/* create two GArrays, this step is not neccessary as the
push_point
* function will do that if they are NULL ;) */
GArray *X = g_array_new(FALSE, FALSE, sizeof (gfloat));
GArray *Y = g_array_new(FALSE, FALSE, sizeof (gfloat));
/* create the graph */
graph = gtk_databox_rt_lines_new(X, Y, &color, 1);
/* make sure of removing the self reference ;) */
g_array_unref(X);
g_array_unref(Y);
/* add it to the box */
gtk_databox_graph_add(GTK_DATABOX(box), graph);
/* add a signal to update the graph if neccesary */
g_signal_connect(graph, "notify::length",
G_CALLBACK(redraw_graph), GTK_DATABOX(box));
...
}
Then I added this single function to redraw each time:
static void
redraw_graph (GtkDataboxGraph *graph,
gint len,
GtkDatabox *box)
{
gtk_databox_auto_rescale(GTK_DATABOX(box), 0.05);
}
To Add the data call:
gtk_databox_rt_xyc_graph_push_point(graph,
x_point, y_point);
I would suggest again to move the entire API to a GArray based
one (we can have a compatibility layer for existing code) they
are fast and really easy to manage, and would be more effective
in this RT cases and can be used for static graphs too.
The GtkDatabox can be provided with means to manage automatic
redraws by connecting internally to it's child "lenght" property
and queue a redraw (invalidate it's own rectagle).
Is it worth a meeting on IRC to talk this stuff (new features,
new developers, coding standars, repository updates, porting to
GTK3, etc.)? Since there is more movement in the mailing list
than the codebase, with a lot of patches and code and little
getting into de repository, we can chat about it on freenode or
sth.
Btw: this code is done in GTK2 and is not ported to GTK3 as the
only working configuration I have right now is GTK2 :(
Cya guys and best regards! :)
Joaquín Aramendía
PS: I added the screenshot to img.ur to fit the message quota