C programming using GTK

Asked

Viewed 736 times

15

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

The problem is this. When the event_box of the atrial player is pressed call a callback function, so that the image that is in the event_box pressed be removed and then add another image. But the new image is not added. And when I run and click on event_box, the image is simply deleted.

Use the function gtk_container_remove to remove the image and gtk_container_add to add the new image (thing that doesn’t 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);
}
  • 1

    Good night Ana, post the code, code images are more difficult to reproduce, if something can be text then use text, really it is complicated to take a picture of the code right? I hope you’ll take this as a positive criticism.

  • 1

    Code images are also invisible to search engines, so other people may not find the solution to their similar problem.

  • Ana Cris, you speak Portuguese? ¿Ana Cris, Usted hablas Portugués? I ask this because your code is in Spanish and you used the words "add" and "atril" in the text.

  • I speak both languages :)

  • @Anacris , college work requires you to use GTK or you chose to use GTK ?

  • @Anacris I would like to know if you have solved your problem - I believe you have. If so, put an answer yourself and mark it as a solution! If you have not solved the problem, could you tell me if that variable image is the type GtkWidget *? I would also like to say this: I particularly dislike the use of nonconstant global variables like image, estado, Element and the others present in its function. I recommend - I believe many others too - that you refer everything to the function you will use and only then modify its contents.

Show 1 more comment

1 answer

1

In accordance with this answer no need to create a new Widget to swap the image. Just use gtk_image_set_from_image() __deprecated__ (or maybe gtk_image_set_from_file() )

I extracted the code below this another answer and I imagine he solves his problem. The following example is a array of images representing "leds"; if you click on an image, for example, the led will light up/delete with a change of the widget image img of the struct led (see the callback click_handler()). The code is very easy to understand, take advantage :-)

#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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.