c - How to clear gtk, goocanvas surface? -


i'm using goocanvas draw simple diagram on gtk frame , diagram dimensions based on values on same window. diagram should change it's dimensions in real time, when user changes values on interface using spinner buttons.

i managed render initial diagram on top of gtk window->hbox->frame.

problem when changes values. redraws on top of drawing.

so there "clear" functionality available goo canvas?

or there other method 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 , varaibles*/  struct dia_struct {     gdouble diam;     const gchar *tbp, *thtp, *wt; };  struct dia_struct set_dia; /* intitializing structure */  /* goo canvas items */ goocanvasitem *root, *path, *path2;  gtkwidget  *canvas;  /*  * function:  ofdra  * --------------------  * main function draw 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  * --------------------  * main function run when in user changed value.  *  *  glade_wdgets: widget-object  *  poblist: array of objects go through  *    *  returns: none  */ void ofruta (gtkwidget *glade_wdgets, gpointer *poblist) {     /* common varaibles needed */      struct dia_struct dia; /* intitializing 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 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 file. if error occurs, report , quit application.      * replace "tut.glade" saved project. */     if( ! gtk_builder_add_from_file( builder, "dia_glade.glade", &error ) )     {         g_warning( "%s", error->message );         g_free( error );         return( 1 );     }      /* main window pointer 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 don't need anymore */     g_object_unref( g_object( builder ) );      /* show window. other widgets automatically shown gtkbuilder */     gtk_widget_show( window );      /* start main loop */     gtk_main();      return( 0 ); } 

when click on button called "measure" handler called "ofruta" in "ofruta" calls drawing function.

is correct method?

here example based on https://developer.gnome.org/goocanvas/unstable/goocanvas-model-view-canvas.html :

#include <stdlib.h> #include <goocanvas.h>  static gboolean on_rect_button_press (goocanvasitem  *view,         goocanvasitem  *target,         gdkeventbutton *event,         gpointer        data);  static gboolean on_delete_event      (gtkwidget      *window,         gdkevent       *event,         gpointer        unused_data);   goocanvasitemmodel *root, *rect_model, *text_model, *path;  int main (int argc, char *argv[]) {     gtkwidget *window, *scrolled_win, *canvas;     goocanvasitem *rect_item;      gtk_set_locale ();     gtk_init (&argc, &argv);      window = gtk_window_new (gtk_window_toplevel);     gtk_window_set_default_size (gtk_window (window), 640, 600);     gtk_widget_show (window);     g_signal_connect (window, "delete_event", (gtksignalfunc) on_delete_event,             null);      scrolled_win = gtk_scrolled_window_new (null, null);     gtk_scrolled_window_set_shadow_type (gtk_scrolled_window (scrolled_win),             gtk_shadow_in);     gtk_widget_show (scrolled_win);     gtk_container_add (gtk_container (window), scrolled_win);      canvas = goo_canvas_new ();     gtk_widget_set_size_request (canvas, 600, 450);     goo_canvas_set_bounds (goo_canvas (canvas), 0, 0, 1000, 1000);     gtk_widget_show (canvas);     gtk_container_add (gtk_container (scrolled_win), canvas);      root = goo_canvas_group_model_new (null, null);      rect_model = goo_canvas_rect_model_new (root, 100, 100, 400, 400,             "line-width", 10.0,             "radius-x", 20.0,             "radius-y", 10.0,             "stroke-color", "yellow",             "fill-color", "red",             null);      text_model = goo_canvas_text_model_new (root, "hello world", 300, 300, -1,             gtk_anchor_center,             "font", "sans 24",             null);     goo_canvas_item_model_rotate (text_model, 45, 300, 300);      goo_canvas_set_root_item_model (goo_canvas (canvas), root);      goocanvaslinedash *dash = goo_canvas_line_dash_new (2, 5.0, 5.0);     const char *buf = "m 0 128 l 1000 256";     path = goo_canvas_path_model_new (root, buf,             "stroke-color", "red",             "line-dash", dash,             null);      rect_item = goo_canvas_get_item (goo_canvas (canvas), rect_model);     g_signal_connect (rect_item, "button_press_event",             (gtksignalfunc) on_rect_button_press, null);      goo_canvas_line_dash_unref(dash);      gtk_main ();      return 0; }   static gboolean on_rect_button_press (goocanvasitem  *item,         goocanvasitem  *target,         gdkeventbutton *event,         gpointer        data) {     g_print ("rect item received button press event\n");      static float = 128, b = 256;     += 16, b += 16;      char buf[64];     snprintf(buf, sizeof(buf), "m 0 %f l 1000 %f", a, b);     g_object_set(g_object(path), "data", buf, null);      return true; }   static gboolean on_delete_event (gtkwidget *window,         gdkevent  *event,         gpointer   unused_data) {     exit (0); } 

by modifying path's data property g_object_set moves when click on rectangle occurs. properties names should taken documentation, e.g. https://developer.gnome.org/goocanvas/unstable/goocanvaspathmodel.html


Comments