Reputation: 191
I'm making a board like this
GtkWidget *board[x][y];
If I do an array of buttons, how can I know which button was pressed?
Does
g_signal_connect(G_OBJECT(board[][]), "clicked",
G_CALLBACK(board_button_pressed), NULL);
// I want to know what [][] they pressed, how could I verify/check this?
return which button of the array was pressed? Or do I have to make a separate function for each of the board pieces?
For example:
OOO
OXO
OOO
How to know which button was pressed if all of the buttons are named the same?
Upvotes: 0
Views: 1863
Reputation: 11395
One of the simplest way would be to just send the information when you are connecting to the callback as data. Something on these lines:
...
typedef struct _identifier{
int x;
int y;
}identifier;
static void button_clicked_cb(GtkButton *button, gpointer data)
{
(void)button; /*To get rid of compiler warning*/
identifier *id = data;
printf("\n id = %d, %d\n", id->x, id->y);
return;
}
....
identifier id[x*y]; /* Size of x*y of the board*/
unsigned int counter = 0;
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
id[counter].x = i;
id[counter].y = j;
board[i][j] = gtk_button_new ();
g_signal_connect(board[i][j], "clicked", G_CALLBACK(button_clicked_cb), &id[counter]);
counter++;
}
}
Please note that "clicked"
signal is associated only with GtkButton
. If you need to use with GtkWidget
then look at "button-press-event"
or "button-release-event"
, in which case the callback signature will also change.
Hope this helps!
Upvotes: 2