[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]

Re: Gtk Widget referencing



--- Dwivedi Ajay kumar <ajayd@xxxxxxxxxx> wrote:
> hi all,
>  I am very new to Gtk/Gdk and am writing an interface in it. I made
> the interface in glade and then modified the files generated.
>
>  The structure of the files is pretty standard, like main creates a
> gnome application widget app1, which contains a lot of widgets. The
> problem now I am facing is how to reference these widgets in
> functions. For example I want to access the value of a spin box when a

> button is pressed. How do i do it?
>
Any widget within a widget can be retrieved with the function
lookup_widget(parent, wid_name) provided in support.c file. The
parent widget is usually the top level widget returned
from the function create_XXX() generated by glade and
the wid_name is the widget name (string) given by you
when the widget was designed with glade (ie, "button1").

When you connect to a signal, give the top level widget
(in you case, may be the window) as the user_data to
the signal handler.

In the callback function, you can do:

void callback (GtkWidget* button, gpointer user_data)
{
     GtkWidget* top_win;
     GtkWidget* spin_button;

     top_win = user_data;

     /* You don't need ref here */
     spin_button = lookup_widget (top_win, "spinbutton1");
     ...
     ...
}


>  One way is to declare all pointers to these widgets global. But is
> there any other way?
>

That's very crude. Another alternative is to use a
struct for window and all it's widgets. But you
need to reference all the widgets manully and
assign the widget pointers to the content of the struct.
That means you will need to edit interface.c file
and this is bad.

>  Also I ..wanted to know what gtk_widget_ref() does ? Could not find
> in the docs.

Every widgets (more precisely gtk objects) have life time decided by
a ref count maintained within the objects. As soon as the ref count
becomes zero, the object is destroyed and all it's resources are
freed.

void gtk_object_ref (GTK_OBJECT(wid));
void gtk_object_unref (GTK_OBJECT(wid));

If you need to use the object and don't want to
get it destroyed by other operations then you
must ref it and when you are done with it
unref the object, otherwise, the widget will
will never be destroyed.

--

Regards,
-Naba

-------------------------------------------------------------
Thrashing is just virtual crashing.
-------------------------------------------------------------


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/