user2109682
user2109682

Reputation: 13

Modify several GTK buttons

I have a key pad with 12 buttons. When a certain button is pressed I want all buttons to get alternate labels. How can this be done, I suppose I need to use an array for the purpose?

Upvotes: 0

Views: 51

Answers (1)

unwind
unwind

Reputation: 399793

Yes, you're going to need to store the buttons in some kind of data structure which you then need to make available to a signal handler callback.

It's often best to wrap all such state information in a structure, since it scales well:

typedef struct {
  GtkWidget *keypad[12];
} GuiData;

Then just instantiate the structure once, perhaps early in main(), and pass it around:

int main(void)
{
  GuiData gui;

 gui.keypad[0] = gtk_button_new_with_label("1");

 /* ... */

 g_signal_connect(G_OBJECT(some_widget), "clicked",
                  G_CALLBACK(cb_some_button_clicked), &gui);
}

Note how &gui is used to pass a pointer to the GUI state structure to the callback function for dealing with the click of "some button".

Inside the callback, you can access the keypad:

static void cb_some_button_clicked(GtkWidget *object, gpointer user)
{
  GuiData *guidata = user;

  gtk_button_set_label(GTK_LABEL(guidata->keypad[0], "4711");
}

I didn't try this now, but the above should be roughly correct.

Upvotes: 1

Related Questions