Re: nameless instances of objects
- From: Michael Hill <mjh rdasys com>
- To: Azrael <azrael azrael-uk f2s com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: nameless instances of objects
- Date: 11 Dec 2002 18:16:15 -0500
Sorry, I can't answer the last question (no experience in reading GdkEvents) but if you really wanted to use an OOP approach to GTK (i am partial to C++ OOP myself) then I would suggest using one of the wrapper tool kits such as gcode or gtkmm. Using one of those WILL allow to do something like gtk_button_new()->connect(blah blah), as you would in Java programming.
Cheers,
Michael
On Wed, 2002-12-11 at 17:50, Azrael wrote:
Michael is right in that by 'name' I mean the variable name. I didn't
know that an object could have a different sort of name, yay - I learn!
In java one can do:
ObjectType variableName = new ObjectType();
which is what I translate approx into:
GtkWidget button = gtk_button_new();
However in Java if I wanted to add buttons to something I might do:
for(int i=0;i<10;i++)
{
Something.add(new JButton());
}
And this is what I mean by the object not having a name - not having a
variable name.
Now I want to do something similar in Gtk, but am not totally sure how.
Also, in Java I could 'attach' actions to the button by having:
Something.add(new JButton().addActionListener(new ActionListener(xyz)));
This way you don't need a name/handle to the object to attach things.
Again, I want to do something similar in Gtk, but don't know how.
So far the signal/callbacks etc I have created, are attached to objects
via their variable name.
If I had 20 dynamically created buttons with no variable names, how do I
attach signals?
basically to re-phrase my question:
I want to dynamically create X number of buttons, all with the same
signal attached, but with a different piece of data to send.
because I never know until runtime how many buttons I want (and indeed,
it will differ every time) I am not sure how to do this dynamically in
Gtk.
Up to this point I say I don't know.. but I think Michael answers..
Michael says I can do this by using the same variable name.. in which
case that now answers that question. I can do:
for(int i=0;i<10;i++)
{
addButton(TheBoxIAmIn, SomePieceOfData);
/* change the data */
}
void addButton(GtkWidget *container, gpointer* data)
{
button = gtk_button_new();
g_signal_connect(G_OBJECT(button), "button_press_event",
G_CALLBACK(MyCallBack), data);
gtk_box_pack_start(GTK_BOX(container), widget, TRUE, FALSE, 0);
}
I really should have thought of this myself, but goes to show that
thinking in java really does rot the brain ;)
Unless of course you can do:
gtk_box_back_start(GTK_BOX(container), gtk_button_new(), TRUE, FALSE, 0)
But this gives no way to attach the signal anyway, as far as I can
see... and really needs a proper object oriented language as opposed to
the way Gtk seems to work.. (don't sue me if I am wrong).
I think the above addButton() code solves my question, and I hope it
also clears up what I meant. Thanks also to Tristan for his answer, it
was useful.
I suppose this is as good a time to ask a related question... in the
callback function, I wanted to check for the type of button_press, and I
tried to compare 'event' (from GdkEventButton *event) with
GDK_BUTTON_PRESS, however that didn't work, and from reading the docs I
have been reading I have been unable to work out exactly how to use
GDK_BUTTON_PRESS to check what type of button_press sent the signal.
Any words or url's of enlightenment?
many thanks
On Wed, 2002-12-11 at 22:20, Michael Hill wrote:
> Whoaa.... I don't believe you guys are on the same track but if you
> are then please disregard. When Azrael speaks of a "name", I believe
> what he means is the name of the variable. Like in his example
> widget = gtk_button_new(...);
> ^^ (he is calling this the "name" when it's
> actually the variable)
>
> If I'm right, then yes you can use multiple buttons with the same
> "variable name". Like this:
>
> widget = gtk_button_new();
> g_signal_connect(G_OBJECT(widget), "clicked",
> G_CALLBACK(MyCallBack), "First Button");
> gtk_box_pack_start(GTK_BOX(abox), widget, TRUE, FALSE, 0);
> /* Now we have a button added to a container that will be unique.
> */
>
> widget = gtk_button_new(); /* A new button using the same
> variable name, this will WORK! */
> g_signal_connect(G_OBJECT(widget, "clicked",
> G_CALLBACK(MyCallBack), "Second Button");
> gtk_box_pack_start(GTK_BOX(abox), widget, TRUE, FALSE, 0);
> /* Now we have a second button using the same widget name and the
> same callback but
> * passing different data! */
>
> After you add widget to some sort of container (window, box, etc.) the
> container becomes the widget's parent and manages it from this point
> on. You no longer have to worry about the widget that was created.
>
> If Azrael, you are talking about the "Name" property of a widget, then
> Tristan is correct and widgets do not have to have these unless you
> plan on theming them.
>
> Cheers,
> Michael H.
>
>
>
> On Wed, 2002-12-11 at 16:46, Tristan Van Berkom wrote:
> > *** WIDGETS DONT HAVE TO HAVE NAMES ***
> >
> > > But when I do:
> > > widget = gtk_button_new(...);
> > > I won't know the widget name.. are you telling me that I don't need to
> > > give it a unique name?
> >
> > Yes.
> >
> > > That I can reuse the same name for each button I
> > > create and add?
> >
> > maybe, probably undefined behaviour in
> > gtk_rc_parse();
> > widgets can be named but dont _have_ to be:
> > http://developer.gnome.org/doc/API/2.0/gtk/GtkWidget.html#gtk-widget-set-name
> >
> > exept for theeming; I dont see why you would _need_ your
> > widgets to have names. (in order to tell them apart ?
> > see g_object_set_data())
> >
> > Cheers,
> > -Tristan
> > _______________________________________________
> > gtk-app-devel-list mailing list
> > gtk-app-devel-list gnome org
> > http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]