Re: Changing the graph color at run time



Hi Roland,
 
Thanks for the information, I tried the approach you have suggested by checking all pointers also by putting the code into idle function but still it is giving segmentation fault for me.. I am using pthread actually in my application i am getting data from some sensors and trying to display the points received. When i tried for multiple sensors i am getting this segmentation problem while changing color or graph.....
 
Exactly in my application i am using some thing like this.......I am attaching the peace of code as follows
 
I have the following structure--
 
typedef struct dispS
{
 gint moteid;
 gint gui_is_created;
 GdkColor color;
 GtkDataboxGraph *graph[6];
 gfloat signalX_acc[POINTS_X];
 gfloat signalY_acc[POINTS_Y];
 gfloat signalX1_acc[POINTS_X];
 gfloat signalY1_acc[POINTS_Y];
 }dispSig;
 
dispSig *p[10] = {NULL};
 
GtkWidget *box[6];
 
static gboolean
signal_idle_func (GtkDatabox * box)
{
   if (!GTK_IS_DATABOX (box))
     return FALSE;
 
   if(GTK_IS_DATABOX(box))
   {
       if (p[0]->gui_is_created != 1)
       {
          p[0]->gui_is_created = 1;
       }
 
   }
 
   gtk_databox_redraw (GTK_DATABOX (box));
   return TRUE;
}
 
 
Main()
{
    /* Here i just call a function to create the boxes */
   

    GdkColor color;
    GtkWidget *box_main;
    GtkWidget *table[6];      
    GtkDataboxGraph *graph;
    //GtkWidget separator;
    gint i;
    gint signal_idle = 0;
    GtkWidget *separator;
    box_main = gtk_vbox_new(FALSE, 0); 
        gtk_container_add( GTK_CONTAINER(window), box_main);
 

 /* Create the databoxes */
 for( i = 0; i<6; i++)
 {
  /* Create a GtkDatabox widget along with scrollbars and rulers */
         gtk_databox_create_box_with_scrollbars_and_rulers (&box[i], &table[i],
            FALSE, FALSE, FALSE, FALSE);
  gtk_box_pack_start (GTK_BOX (box_main), table[i], TRUE, TRUE, 0);
  separator = gtk_hseparator_new ();
  gtk_box_pack_start (GTK_BOX (box_main), separator, FALSE, FALSE, 0);
 }
 
        // To make cross lines on the boxes

 color.red = 32768;
 color.green = 32768;
 color.blue = 32768;

    graph = gtk_databox_cross_simple_new (&color, 0);
    
 for( i = 0; i<6; i++)
 {
  gtk_databox_graph_add (GTK_DATABOX (box[i]), graph);
 }

 // To fill up the boxes with black color
 color.red = 0;
 color.green = 0;
 color.blue = 0;//32768;

 for( i = 0; i<6; i++)
 {
  gtk_widget_modify_bg (box[i], GTK_STATE_NORMAL, &color);
 }
 
 gint id = 0;
 p[id] = (dispSig*)malloc(sizeof(dispSig));
 p[id]->moteid = i;
 p[id]->color.red = 65535;
 p[id]->color.green = 65535;
 p[id]->color.blue = 0;
 p[id]->graph[0] = gtk_databox_lines_new (POINTS_X, p[id]->signalX_acc, p[id]->signalY_acc, &p[id]->color, 1);
 p[id]->graph[1] = gtk_databox_lines_new (POINTS_X, p[id]->signalX1_acc, p[id]->signalY1_acc, &p[id]->color, 1);

    /* Also attach idle function with each box */

  for( i = 0; i<6; i++)
     signal_idle = g_idle_add ((GSourceFunc) signal_idle_func, box[i]);

}

 
thread 1
{
 
       /* Just drawing the signals */
 
       I for(i=0; i < POINTS_X; i++)
          p[0]->signalX_acc[i] = i;
        for(i=POINTS_Y-3; i >= 0; i--) {
          p[0]->signalY_acc[i+2] = p[0]->signalY_acc[i];
        }
        p[0]->signalY_acc[0] = (pld.x10_acc);
        p[0]->signalY_acc[1] = (pld.x11_acc);
        if(p[0]->signalY_acc[0] >= 2048) p[0]->signalY_acc[0] -= 4096;
       if(p[0]->signalY_acc[1] >= 2048) p[0]->signalY_acc[1] -= 4096;

}
 
Please let me know if this kind of structure can be maintained logically i am not able to find my mistake....
 
I also used gdb debugger to check where the segmentation fault occurring but the problem it is failing in
 
random places.. though I am sure that it is due to draw box as if i remove the multiple graphs and color
 
 logic it works absolutely fine. Your some inputs about the Gtkdatabox would be very useful to sort out
 
problem.
 
Regards,
Nisha
 
 
On 4/2/08, Jim George <jimgeorge gmail com> wrote:
I haven't examined the source code for gtkdatabox, but if the code for
gtk_databox_add_graph calls gdk at any time, that would surely require
some sort of synchronization between multiple threads. The best (and
so-called "correct") approach for such problems is to put all code
that touches gdk in one thread, and use glib's "g_idle_add" or
"g_timeout_add" functions from the secondary threads. The idle or
timeout callback will get called by the GMainContext running in your
primary thread, so all will be well. This has the added benefit of
being portable to Win32 and other ports of gtk/glib, since the
gdk_thread stuff only seems to work well under X11.

-Jim

On Wed, Apr 2, 2008 at 12:58 AM, Roland Bock <box2OO6 eudoxos de> wrote:
> Hi Nisha,
>
>  I see no reason why this should not work in principle.
>
>  Remarks:
>
>  a) To me (not knowing your application) it seems a little strange that
>  you want to put each graph into a different box? Your introduction
>  sounds different.
>
>  b) If p and i are shared among the threads, you need some thread
>  synchronization. Otherwise you could have situations where i does not
>  stay the same during that code sequence
>
>
>  Regards,
>
>  Roland
>
>
>
>  nisha jain wrote:
>  > Hi All,
>  >
>  > I would like to change the color of signals at run time also
>  > i want to draw signals on gtk data box at run time.
>  > I am not sure how should i do it? I am using following
>  > code to currently draw the signals on the gtk data box
>  >
>  > where the p is a structure array containing the GtkColor and
>  > graph as the memmbers..
>  >
>  >   p[i]->color.red = 65535;
>  >   p[i]->color.green = 65535;
>  >   p[i]->color.blue = 0;
>  >   p[i]->graph = gtk_databox_lines_new (POINTS, p[i]->signalX_acc,
>  > p[i]->signalY_acc, &p[i]->color, 1);
>  >   gtk_databox_graph_add(GTK_DATABOX (box[i]), p[i]->graph);
>  >
>  > when i tried to move this in a thread at run time i am getting run time
>  > exceptions
>  > I am not sure if i can do it or not?
>  >
>  > Thanks and Regards,
>  > Nisha
>  >
>  >
>  > ------------------------------------------------------------------------
>  >
>  > _______________________________________________
>  > gtkdatabox-list mailing list
>  > gtkdatabox-list gnome org
>  > http://mail.gnome.org/mailman/listinfo/gtkdatabox-list
>
>  _______________________________________________
>  gtkdatabox-list mailing list
>  gtkdatabox-list gnome org
>  http://mail.gnome.org/mailman/listinfo/gtkdatabox-list
>
_______________________________________________
gtkdatabox-list mailing list
gtkdatabox-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkdatabox-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]