Re: nameless instances of objects



Thanks for all the help with this guys.
I'm really not comfortable reading the GTK documentation stuff yet.. I
guess practise will help. 

        Hmmm yes, the development style is a little
different. I doubt anyone was paid to write the
gtk api reference; contrary to javadoc. 
On the other hand; personaly I like to 
keep the source code of any libraries I'm 
working with handy. This way I can always 
check the fuction calls to know _exactly_ 
what they do and I can always compile the 
library with `-g' and single step through 
entire apps including third party libraries.

so you have some advantages and
some disadvantages ;-)

Guess I have been spoilt with the convenience
of Javadoc ;)

If I were in the oposite position; I would
probably say that I've been spoiled with
always having the source code handy.


Good luck!
                -Tristan

Azrael wrote:

Continuing in my habit of answering my own questions.. I am really
tempted to ban myself asking questions until /2/ days of failure rather
than only 1 day

What I need to check for mouse-button-1 is:

if(event->button == 1)

Thanks for all the help with this guys.
I'm really not comfortable reading the GTK documentation stuff yet.. I
guess practise will help. Guess I have been spoilt with the convenience
of Javadoc ;)

On Thu, 2002-12-12 at 12:58, Azrael wrote:
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;
g_print(x);

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.

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

--
                Azrael

           ("\''/").___..--'''"-._
           `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.

ICQ#52944566

_______________________________________________
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]