Re: Set up callbacks for dynamically created buttons



On Fri, 2006-04-21 at 23:11 -0700, 3saul wrote:
I'm creating some buttons using:
buttons = g_new(GtkWidget*, n);
for (i = 0; i < n; i++) {
   buttons[i] = gtk_button_new...
   gtk_button_whatever(GTK_BUTTON(buttons[i])...
   ...
   gtk_box_pack_start(GTK_BOX(some_box), buttons[i]...
}
how would I set these buttons up with call backs once they're created?

That depends on what you want the buttons to do.. if you merely wanted
to know which button was pressed, you could create a callback something
like:

gint button_callback(GtkWidget *button, gint i)
{
        printf("Button %d was clicked\n", i);
}

and connect to it like:

g_signal_connect(G_OBJECT(buttons[i]), "clicked", G_CALLBACK(button_callback), (gpointer)i)


Alternatively if you want to pass some other data to the callback you
could determine which button was pressed by calling
gtk_button_get_label(GTK_BUTTON(button)
from withing the callback to determine the label of the button.


Hope this helps.
-- 
Daniel Pekelharing
<legendstar gmail com>




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]