C programming using GTK

15

I have to make a game like work in college (Scrabble).

The problem is as follows. When the event_box of the player's lectern is pressed I call a callback function, so that the image that is in the% pressed% is removed and then adds another image. But the new image is not added. And when I run and click on event_box , the image is simply deleted.

I use the event_box function to remove the image and gtk_container_remove to add the new image (something that does not happen).

Remembering that I started using GTK a week ago.

void selecFichas(GtkWidget *casillaAtril, gpointer data){
    int i;

    image = gtk_image_new_from_file("casilla.png");
    g_object_ref(image);

    estado = 1;
    for(i = 0; i < LETRAS; i++)
        if(casillaAtril == atrilJogador1[i]){
            Element = i; // determina cual event_box(casilla del atril) fue presionado
            break;
        }
    gtk_container_remove(GTK_CONTAINER[atrilJugador1[Element]), fichasABC[coordImagen[Element][0]][coodImagen[Element][1]]);
    gtk_container_add(GTK_CONTAINER[atrilJugador1[Element]), image);
}
    
asked by anonymous 20.11.2015 / 02:30

1 answer

1

As this answer there is no need to create a new Widget to swap the image. Just use gtk_image_set_from_image () __ deprecated __ (or perhaps gtk_image_set_from_file () )
>
I extracted the code below from this other answer and I imagine that it will solve your problem. The following example is an array of images representing "leds"; if you click on an image, for example, the led will light / erase with a change of the img widget image of the struct led (see callback click_handler() ). The code is very easy to understand, enjoy: -)

#include <gtk/gtk.h>

#define ICON_WIDTH 16
#define ICON_HEIGHT 16
#define NUM_LEDS 2500

typedef enum {
    ON,
    OFF
} led_status;

typedef struct {
    GtkWidget *img;
    struct {
        gint x;
        gint y;
    } pos;
    led_status status;
} led;

static led leds[NUM_LEDS];
static GdkPixbuf *led_on;
static GdkPixbuf *led_off;

static gboolean click_handler(GtkWidget *widget,
                              GdkEvent *event,
                              gpointer user_data)
{
    led *info = user_data;

    if (info->status == ON) {
        gtk_image_set_from_pixbuf(GTK_IMAGE(info->img), led_off);
        info->status = OFF;
    } else {
        gtk_image_set_from_pixbuf(GTK_IMAGE(info->img), led_on);
        info->status = ON;
    }

    return TRUE;
}

int main(int argc, char** argv)
{
    GtkWidget *window, *layout;
    int i = 0, x, y;

    gtk_init(&argc, &argv);

    /* Load our images (ignoring errors - as any good sample code would) */
    led_on  = gdk_pixbuf_new_from_file("led-on.png", NULL);
    led_off = gdk_pixbuf_new_from_file("led-off.png", NULL);

    /* Initialize our array */
    for (x = 0; x < 50; x++) {
        for (y = 0; y < 50; y++) {
            leds[i].img = gtk_image_new();
            leds[i].pos.x = x * ICON_WIDTH;
            leds[i].pos.y = y * ICON_HEIGHT;
            leds[i].status = OFF;

            /* Initialize our image from the pixbuf we've already loaded */
            gtk_image_set_from_pixbuf(GTK_IMAGE(leds[i].img), led_off);
            i++;
        }
    }

    /* Create a window */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "LEDs");
    gtk_signal_connect(GTK_OBJECT(window),
                       "destroy",
                       G_CALLBACK(gtk_main_quit),
                       NULL);

    /* Create the widget */
    layout = gtk_layout_new(NULL, NULL);

    for (i = 0; i < NUM_LEDS; i++) {
        /*
         * A GtkImage doesn't have a window, so we need to put it inside
         * a GtkEventBox so we can capture events.
         */
        GtkWidget *eb = gtk_event_box_new();
        g_signal_connect(G_OBJECT(eb),
                         "button_press_event",
                         G_CALLBACK(click_handler),
                         &leds[i]);
        gtk_container_add(GTK_CONTAINER(eb), leds[i].img);
        gtk_layout_put(GTK_LAYOUT(layout), eb, leds[i].pos.x, leds[i].pos.y);
    }

    gtk_container_add(GTK_CONTAINER(window), layout);
    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}
    
28.11.2016 / 19:59