Re: nameless instances of objects

That's exactly what I needed, thanks!!!

However.. not sure how to use it.. or if I am doing something wrong..

I have code like:

if(event->state == GDK_BUTTON1_MASK)
{ stuff }

But the if statement fails, and 'stuff' isn't done.

If I try:

gint x= (gint)event->state;

or g_print(event->state);

I get a segmentation fault..

So I'm not entirely sure what's going on..

(anyone notice how I ask 100 questions a day and seem to make progress
very very slowly? ;)

On Thu, 2002-12-12 at 11:37, TORRI Vincent wrote:
On 12 Dec 2002, Azrael wrote:

I answer myself:

if(event->type == GDK_3BUTTON_PRESS)

However doesn't do what I thought :)
this checks if you click 3 times, and not if you use the third mouse
button - which is what I wanted.
Oh well :)

  the signal "button-press-event" (of a GtkWidget) gives you a 
GdkEventButton * ev. Then you can look at the state of this event 
(ev->state): the bit GDK_BUTTON3_MASK tests if the third button is pressed 
or not. 

 hope this helps

Vincent TORRI

On Wed, 2002-12-11 at 23:16, Michael Hill wrote:
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.


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

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.

Michael H. 

On Wed, 2002-12-11 at 16:46, Tristan Van Berkom wrote: 

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? 


That I can reuse the same name for each button I
create and add?

maybe, probably undefined behaviour in
widgets can be named but dont _have_ to be:

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())

gtk-app-devel-list mailing list
gtk-app-devel-list gnome org


           `0_ O  )   `-.  (     ).`-.__.`) 
           (_Y_.)'  ._   )  `._ `. ``-..-'  
         _..`--'_..-_/  /--'_.' .'          
        ((i).-''  ((i).'  (((.-'         
Of all God's creatures there is only one that cannot be made the slave
of the lash. That one is the cat. If man could be crossed with a cat it
would improve man, but it would deteriorate the cat. 


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