go-gtk icon indicating copy to clipboard operation
go-gtk copied to clipboard

Gtk-CRITICAL **: IA__gtk_widget_set_events: assertion 'GTK_IS_WIDGET (widget)' failed

Open RodrigoLourenco2018 opened this issue 6 years ago • 2 comments

Don't know what caused this error can someone help.

I'm using ROS and Gtk2.0 at the same time, but I'm having the filling that my error is the way I use Gtk2.0.

Can some one help me, thank's in advance.


The program is based on ROS as GTK2.0 plus cairo.

Since ROS and Gtk are event driven plataforms the number_callback function takes care of the ROS interface and the GTK interface runs with gtk_main_iteration_do(0);

My problem I thought it was related with this error but the error is solved and my problem persists and is related with g_timeout_add_full

The problem is that this function runs once and them stops been called, I tried with g_timeout_add_full and g_timeout_add the problem is the same, I don't see how can I solve this problem.

Thank's in advance.


#include "ros/ros.h"
#include "std_msgs/Int16.h"
#include "std_msgs/Float64.h"
#include "std_msgs/Float32.h"
#include "pubsub/RaposaWheels.h"
#include "pubsub/main.h"
#include <geometry_msgs/Quaternion.h>
#include <std_msgs/Header.h>
#include <iostream>
#include <sstream>
#include <cairo.h>
#include <gtk/gtk.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <ros/rate.h>
#include <time.h>

#define  pi  3.14159
#define draw_Wigth   180
#define draw_height  180

typedef struct orien_ang{
    float oldx;
    float oldy;
    float x;
    float y;
    float Nang;
    float Oang;
}ang;

double body_roll;

ang* Gorien;
GtkWidget *darea1;
GtkWidget *darea2;
GtkWidget *GlobalTable;
GtkWidget *window;

static void do_drawing(GtkWidget *widget, GdkEventExpose *event, gpointer user_data);
static void do_drawing2(GtkWidget *widget, GdkEventExpose *event, gpointer user_data);

static void do_drawing(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{

    double int_x, int_y;
    double radius;
    double inset;
    double aux_ang1, aux_ang2, aux_ang3, aux_ang4;
    int i;
    cairo_t *cr;

    cr = gdk_cairo_create(widget->window);


    int_x = draw_Wigth/2;
    int_y = draw_height/2;

    radius = draw_Wigth/3 - 5;

    cairo_set_source_rgb(cr, 0, 0, 0); 
    cairo_stroke(cr);

    i = 0;
    while (i < 36){
        cairo_save(cr);
        inset = (1.0/18)*radius;
        cairo_set_line_width(cr, 0.5 * cairo_get_line_width(cr));
        cairo_move_to(cr, int_x + (radius - inset)* cos(i * pi /18.0), int_y + (radius - inset)* sin(i * pi /18.0));
        cairo_line_to(cr, int_x + radius* cos(i * pi/18.0), int_y + radius* sin(i * pi/18.0));
        cairo_stroke(cr);
        cairo_restore(cr);
        i = i + 1;
    }

    i = 1;
    while ( i < 9)
    {
        cairo_save(cr);

        inset = 0.15 * radius;
        cairo_move_to(cr, int_x + (radius - inset) * cos(i * pi/4.0), int_y + (radius-inset)*sin(i * pi / 4.0));
        cairo_line_to(cr, int_x + radius * cos(i * pi/4.0), int_y + radius*sin(i * pi / 4.0));
        cairo_stroke(cr);
        cairo_restore(cr);
        i = i + 1;
    }

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr,0, 1, 0); //Green
    cairo_move_to(cr, int_x + radius*0.6*( -1* cos((*Gorien).x)), int_y + radius*0.6*sin((*Gorien).x));
    cairo_line_to(cr, int_x + radius*0.6*cos((*Gorien).x), int_y + radius*0.6*(-1*sin((*Gorien).x)));
    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr, 0, 1, 0); //Green 

    aux_ang1 = (int_x + radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 + 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y - radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 + 0.2*radius*cos((*Gorien).x));
    cairo_move_to(cr, aux_ang2, aux_ang4);

    aux_ang1 = (int_x + radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 - 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y - radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 - 0.2*radius*cos((*Gorien).x));
    cairo_line_to(cr, aux_ang2, aux_ang4);

    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);  

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr, 0, 1, 0); //Green 

    aux_ang1 = (int_x - radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 + 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y + radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 + 0.2*radius*cos((*Gorien).x));
    cairo_move_to(cr, aux_ang2, aux_ang4);

    aux_ang1 = (int_x - radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 - 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y + radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 - 0.2*radius*cos((*Gorien).x));
    cairo_line_to(cr, aux_ang2, aux_ang4);

    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);  

}

static void do_drawing2(GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{
        char angle[10];
        cairo_t *cr;

        cr = gdk_cairo_create(widget->window);

        cairo_set_source_rgb(cr, 0, 0, 0);
        cairo_set_font_size(cr, 14);
        cairo_move_to(cr,20,20);
        cairo_show_text(cr,"Body Roll");
        snprintf(angle, 20, "%4.2f%s", body_roll-90, "º");
        cairo_move_to(cr,20,60);
        cairo_show_text(cr,angle);


}

static void timer1(gpointer data)
{

    double int_x, int_y;
    double radius;
    double inset;
    double aux_ang1, aux_ang2, aux_ang3, aux_ang4;
    int i;
    cairo_t *cr;

    int_x = draw_Wigth/2;
    int_y = draw_height/2;

    radius = draw_Wigth/3 - 5;

    cairo_set_source_rgb(cr, 0, 0, 0);
    cairo_stroke(cr);

    i = 0;
    while (i < 36){
        cairo_save(cr);
        inset = (1.0/18)*radius;
        cairo_set_line_width(cr, 0.5 * cairo_get_line_width(cr));
        cairo_move_to(cr, int_x + (radius - inset)* cos(i * pi /18.0), int_y + (radius - inset)* sin(i * pi /18.0));
        cairo_line_to(cr, int_x + radius* cos(i * pi/18.0), int_y + radius* sin(i * pi/18.0));
        cairo_stroke(cr);
        cairo_restore(cr);
        i = i + 1;
    }

    i = 1;
    while ( i < 9)
    {
        cairo_save(cr);

        inset = 0.15 * radius;
        cairo_move_to(cr, int_x + (radius - inset) * cos(i * pi/4.0), int_y + (radius-inset)*sin(i * pi / 4.0));
        cairo_line_to(cr, int_x + radius * cos(i * pi/4.0), int_y + radius*sin(i * pi / 4.0));
        cairo_stroke(cr);
        cairo_restore(cr);
        i = i + 1;
    }

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr,0, 1, 0); //Green
    cairo_move_to(cr, int_x + radius*0.6*( -1* cos((*Gorien).x)), int_y + radius*0.6*sin((*Gorien).x));
    cairo_line_to(cr, int_x + radius*0.6*cos((*Gorien).x), int_y + radius*0.6*(-1*sin((*Gorien).x)));
    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr, 0, 1, 0); //Green 

    aux_ang1 = (int_x + radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 + 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y - radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 + 0.2*radius*cos((*Gorien).x));
    cairo_move_to(cr, aux_ang2, aux_ang4);

    aux_ang1 = (int_x + radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 - 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y - radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 - 0.2*radius*cos((*Gorien).x));
    cairo_line_to(cr, aux_ang2, aux_ang4);

    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);  

    cairo_save(cr);
    cairo_set_line_width(cr, 4 * cairo_get_line_width(cr));
    cairo_set_source_rgb(cr, 0, 1, 0); //Green 

    aux_ang1 = (int_x - radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 + 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y + radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 + 0.2*radius*cos((*Gorien).x));
    cairo_move_to(cr, aux_ang2, aux_ang4);

    aux_ang1 = (int_x - radius*0.6*cos((*Gorien).x)); 
    aux_ang2 = (aux_ang1 - 0.2*radius*sin((*Gorien).x));
    aux_ang3 = (int_y + radius*0.6*sin((*Gorien).x));
    aux_ang4 = (aux_ang3 - 0.2*radius*cos((*Gorien).x));
    cairo_line_to(cr, aux_ang2, aux_ang4);

    cairo_close_path(cr);
    cairo_stroke(cr);
    cairo_restore(cr);  

}

static void timer2 ()
{
        gdk_threads_enter();
        char angle[10];
        cairo_t *cr;

        cr = gdk_cairo_create(darea2->window);

        cairo_set_source_rgb(cr, 0, 0, 0);
        cairo_set_font_size(cr, 14);
        cairo_move_to(cr,20,20);
        cairo_show_text(cr,"Body Roll");
        snprintf(angle, 20, "%4.2f%s", body_roll-90, "º");
        cairo_move_to(cr,20,60);
        cairo_show_text(cr,angle);
        gtk_widget_queue_draw(darea2);
        gdk_threads_leave();

}

void number_callback(const pubsub::main::ConstPtr& msg)
{

    if ( GTK_IS_WIDGET (window) )
    {
        gtk_widget_show_all ( window ); 
    }
    else
    {
        ROS_INFO ("The top level window of the application was closed...Exiting the program.\n");
    }

    if (ros::ok())
    {
        ros::spinOnce();
        ROS_INFO("Recieved  msg [%f]",msg->body_roll);        
        body_roll = msg->body_roll;   
        gtk_widget_set_events (darea2, GDK_EXPOSURE_MASK);    
        gtk_main_iteration_do(0);

    }
    else
    {
        gtk_main_quit();
    }

}   

static void timeout1()
{
    g_timeout_add_full(G_PRIORITY_DEFAULT, 33, (GSourceFunc)timer2, NULL, (GDestroyNotify)timeout1);    
}

int main(int argc, char **argv)
{

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

  Gorien=(ang *)malloc(sizeof(ang)); 
  (*Gorien).oldx = 0;
  (*Gorien).oldy = 0;
  (*Gorien).x = 0;
  (*Gorien).y = 0;
  (*Gorien).x = 1;
  (*Gorien).y = 12;

  darea1 = gtk_drawing_area_new();
  darea2 = gtk_drawing_area_new();
  GlobalTable = gtk_table_new (1, 2, TRUE);

  gtk_widget_set_size_request(darea1, draw_Wigth, draw_height);
  gtk_widget_set_size_request(darea2, draw_Wigth, draw_height);

  gtk_table_attach_defaults (GTK_TABLE (GlobalTable), darea1, 0, 1, 0, 1);
  gtk_table_attach_defaults (GTK_TABLE (GlobalTable), darea2, 1, 2, 0, 1);
  gtk_container_add(GTK_CONTAINER(window), GlobalTable);

    g_signal_connect(darea1, "expose_event", 
    G_CALLBACK(do_drawing), NULL); 
    g_signal_connect(darea2, "expose_event", 
    G_CALLBACK(do_drawing2), NULL); 
    g_timeout_add_full(G_PRIORITY_DEFAULT, 33, (GSourceFunc)timer2, NULL, (GDestroyNotify)timeout1);

    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_default_size(GTK_WINDOW(window), 360, 180); 
    gtk_window_set_title(GTK_WINDOW(window), "GTK window");

    gtk_widget_show_all(window);

    ros::init(argc, argv,"sub7");
    ros::NodeHandle node_obj;
    ros::Rate loop_rate(100);   
    ros::Subscriber number_subscriber = node_obj.subscribe("/republished/raposang/telemetry",10,number_callback);
    ros::spin();

    gtk_main();    
    return 0;
}

RodrigoLourenco2018 avatar Jul 25 '18 11:07 RodrigoLourenco2018

Is this depend on go-gtk?

mattn avatar Jul 25 '18 13:07 mattn

No go-gtk as nothing to do with my program.

The program is based on ROS as GTK2.0 plus cairo.

Since ROS and Gtk are event driven plataforms the number_callback function takes care of the ROS interface and the GTK interface runs with gtk_main_iteration_do(0);

My problem I thought it was related with this error but is related with g_timeout_add_full

The problem is that this function runs once and them stops been called, I tried with g_timeout_add_full and g_timeout_add the problem is the same, I don't see how can I solve this problem.

Thank's in advance.

RodrigoLourenco2018 avatar Jul 25 '18 17:07 RodrigoLourenco2018