I'm using goocanvas to draw a simple diagram on a GTK frame and diagram dimensions are based on the values on same window. The diagram should change it's dimensions in real time, when the user changes the values on the interface using spinner buttons.
I successfully managed to render the initial diagram on top of the gtk window->hbox->frame.
Problem is when changes the values. It redraws on top of the drawing.
So is there any "clear" functionality available for goo canvas?
Or is there any other method to achieve desired results?
Edit:
Current code:
/*
* Compile me with:
* gcc -o dia dia.c `pkg-config --libs --cflags gtk+-2.0 goocanvas` -rdynamic -I./headers/ -lm
*
*/
#include <stdio.h>
#include <gtk/gtk.h>
#include <goocanvas.h>
/*Common structs and varaibles*/
struct dia_struct
{
gdouble diam;
const gchar *tbp, *thtp, *wt;
};
struct dia_struct set_dia; /* intitializing the structure */
/* Goo canvas items */
GooCanvasItem *root, *path, *path2;
GtkWidget *canvas;
/*
* Function: ofdra
* --------------------
* This is the main function to draw the diagram on screen
*
* returns: none
*/
void ofdra (double diam)
{
double dfactor = diam*50;
path = NULL;
path2 = NULL;
double x = (465-dfactor)/2;
double y = 465-x;
char buf[100];
char buf2[100];
sprintf(buf, "M 0 %f L 1000 %f", x, x);
sprintf(buf2, "M 0 %f L 1000 %f", y, y);
path = goo_canvas_path_new (root, buf, "stroke-color", "red", NULL);
path2 = goo_canvas_path_new (root, buf2, "stroke-color", "red", NULL);
/*goo_canvas_item_update (path, TRUE, );*/
/*goo_canvas_item_update (path2);*/
}
/*
* Function: ofruta
* --------------------
* This is the main function to run when in a user changed a value.
*
* glade_wdgets: widget-object
* pObList: array of objects to go through
*
* returns: none
*/
void ofruta (GtkWidget *glade_wdgets, gpointer *pObList)
{
/* common varaibles needed */
struct dia_struct dia; /* intitializing the structure */
/* getters */
dia.diam = gtk_spin_button_get_value (GTK_SPIN_BUTTON(pObList[0])); /* diameter reading */
set_diamond.diam = diamond.diam;
gtk_spin_button_set_value(GTK_SPIN_BUTTON(pObList[0]), set_diamond.diam);
/* Loads all dynamically generated diagram drawing */
ofdra(set_diamond.diam);
}
//===========================================================================
/*
* main
*
* Program begins here
*/
int main( int argc, char **argv )
{
GtkBuilder *builder;
GtkWidget *window;
GError *error = NULL;
GtkButton *button;
GtkLabel *label;
cairo_surface_t *surface;
/* Init GTK+ */
gtk_init( &argc, &argv );
/* Create new GtkBuilder object */
builder = gtk_builder_new();
/* Load UI from file. If error occurs, report it and quit application.
* Replace "tut.glade" with your saved project. */
if( ! gtk_builder_add_from_file( builder, "dia_glade.glade", &error ) )
{
g_warning( "%s", error->message );
g_free( error );
return( 1 );
}
/* Get main window pointer from UI */
window = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );
gpointer spinners[] = { gtk_builder_get_object( builder, "diam" )};
/* Connect signals */
gtk_builder_connect_signals( builder, spinners );
g_signal_connect(G_OBJECT(window), "delete-event", (GCallback)gtk_main_quit, NULL);
g_signal_connect(G_OBJECT("measure"), "clicked", (GCallback)ofruta, spinners);
canvas = goo_canvas_new ();
gtk_widget_set_size_request (canvas, 600, 465);
goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 1000, 1000);
gtk_widget_show (canvas);
gtk_container_add (GTK_CONTAINER (gtk_builder_get_object( builder, "draw_area" )), canvas);
root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
/* Destroy builder, since we don't need it anymore */
g_object_unref( G_OBJECT( builder ) );
/* Show window. All other widgets are automatically shown by GtkBuilder */
gtk_widget_show( window );
/* Start main loop */
gtk_main();
return( 0 );
}
When click on button called "measure" it handler called "ofruta" in "ofruta" it calls the drawing function.
Is this a correct method?