Re: Set up callbacks for dynamically created buttons

2006-04-22 Thread Daniel Pekelharing
On Sat, 2006-04-22 at 04:10 -0700, 3saul wrote:
> So how should I implement this with the changes that David pointed out?
It's not a very complex change actually, something like this:

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

and connect to it like:

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

-- 
Daniel Pekelharing
<[EMAIL PROTECTED]>

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Set up callbacks for dynamically created buttons

2006-04-22 Thread 3saul

So how should I implement this with the changes that David pointed out?
--
View this message in context: 
http://www.nabble.com/Set-up-callbacks-for-dynamically-created-buttons-t1490320.html#a4039872
Sent from the Gtk+ - Apps Dev forum at Nabble.com.

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Set up callbacks for dynamically created buttons

2006-04-22 Thread Daniel Pekelharing
On Sat, 2006-04-22 at 10:33 +0200, David Necas (Yeti) wrote:
> Please do not do this.  Perhaps it works on your i386 box,
> but the macros GINT_TO_POINTER() and GPOINTER_TO_INT() exist
> for a good reason.

Thanks for pointing that out, I was not aware of these macros..
Not that it makes any difference on an i386, as according to the GLib
reference manual it is defined as:
#define GINT_TO_POINTER(i)  ((gpointer)  (i))

-- 
Daniel Pekelharing
<[EMAIL PROTECTED]>

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Set up callbacks for dynamically created buttons

2006-04-22 Thread David Necas (Yeti)
On Sat, Apr 22, 2006 at 09:22:44AM +0200, Daniel Pekelharing wrote:
> 
> 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)

Please do not do this.  Perhaps it works on your i386 box,
but the macros GINT_TO_POINTER() and GPOINTER_TO_INT() exist
for a good reason.

> 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.

Alternatively (and better), you can use g_object_set_data()
to attach arbitrary data to each button.

Yeti


--
That's enough.
___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Re: Set up callbacks for dynamically created buttons

2006-04-22 Thread Daniel Pekelharing
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
<[EMAIL PROTECTED]>

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


Set up callbacks for dynamically created buttons

2006-04-21 Thread 3saul

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?
--
View this message in context: 
http://www.nabble.com/Set-up-callbacks-for-dynamically-created-buttons-t1490320.html#a4038183
Sent from the Gtk+ - Apps Dev forum at Nabble.com.

___
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list