How to block a gtk button from being pressed two times?

I have this code, that fills a table with buttons:

box1 = gtk_builder_get_object( builder, "box1");

grid = gtk_grid_new();
gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE);
gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE);

gtk_box_pack_start(GTK_BOX(box1), grid, TRUE, TRUE, 0);

///////////////////////////////////////////////////////
for (i = 0, fila = 0; i < CANT_BOTONES ; i++)
{
    /* Boton a ser creado */
    //GtkWidget *botontab;

    /* Crear boton con el texto concatenado */
    //botontab = gtk_button_new();
    tabbotones[i].buttontab = gtk_button_new();

    //turn = *ptrturn;
    g_signal_connect(G_OBJECT(tabbotones[i].buttontab), "clicked", G_CALLBACK( juega_gtk ), i);

    /* Calcular columna del grid donde se ubicará el botón */
    columna = i % CANT_COLUMNAS;

    /* Calcular fila del grid donde se ubicará el botón */
    if (i && !columna)
        fila++;

    tabbotones[i].fila = fila;
    tabbotones[i].columna = columna;

    /* Agregar botón al grid */
    gtk_grid_attach(GTK_GRID(grid), GTK_WIDGET(tabbotones[i].buttontab), columna, fila, ANCHURA_BOTON, ALTURA_BOTON);
    printf("%d %d -- %d %d\n", fila, columna, tabbotones[i].fila, tabbotones[i].columna );
}

What I want, is that only one time a button can be pressed, not two or more times. I dont know how to block this. The only thing I came out searching on the internet is this: How to Implement a button-press-event on GtkTable but how I save the information of the gtkbutton that was pressed already?, I think the only way is to save that info in my struct that has the buttons and its coordinates on it, but how?. Thanks in advance!.

Upvotes: 0

Views: 338

Answers (1)

ntd
ntd

Reputation: 7434

You can attach arbitrary values to any GObject (thereby to any GtkWidget) with g_object_set_data.

If I understood your question, you can leverage this feature to accomplish what you need:

/* In the loop */
g_object_set_data(button, "clicks", GINT_TO_POINTER(0));


/* In the callback */
gint clicks = GPOINTER_TO_INT(g_object_get_data(button, "clicks") + 1;
g_object_set_data(button, "clicks", GINT_TO_POINTER(clicks));

if (clicks == 1) {
    /* Handle your first click */
}

GPOINTER_TO_INT and GINT_TO_POINTER are just C conveniences to avoid the use of dynamic memory.

Upvotes: 2

Related Questions