Re: How to determine the type of a widget?



Hi,

"Marc K." <marc centrin net id> writes:

> After reading the tutorial (again, counting 3), still, the question
> is the same. Never mind. I dont think it's possible.  I think we are
> thinking in different worlds.

I am pretty sure it is possible but perhaps you should try to come up
with a question and stick to it. So far you have changed your question
with each mail.

Let's see how we started. Your question was:

> For example, gtk_container_get_children will give me a GList
> of widget objects. I need to know what are the type of those
> widgets so I can dispatch the correct methods.

>	switch (get_the_type_of_this_widget (w))
>	{
>		case XXX:  ...
>		case YYY: ...
>		... etc.
>	}
>

So you were asking for get_the_type_of_this_widget(w) where w is an
instance of GtkWidget. I answered that G_TYPE_FROM_INSTANCE() would do
but you responded that you don't have any instances. Oh well, that is
not what you said in your first post but let's assume you just changed
your mind.  In your next mail you asked about the following code:

> Class *ButtonClass = ClassNew ("Button", BinClass, ....
> klass = get_the_class_of (GtkButton)
> id = the_id_of_class (klass)
> set_class_id (ButtonClass, id)
>  ...
> then later (stupid code but i hope it makes the point)
>  ....
> id = get_id_of_this_widget_instance (w)
> Class c = get_class_from_id (id)
> if (c == ButtonClass)
>   ...
> else if (c == FrameClass)
>   ...
> etc.

This can be written using g_type_from_name("GtkButton") or
alternatively using GTK_TYPE_BUTTON(), both will return you an id for
the GtkButton type which can be used to access the class
(g_type_class_peek) or to instantiate the button (g_object_new).
There's an issue with using g_type_from_name() since you need to make
sure that the type is already registered at this point, but this can
be solved with little effort.

> Here is the basic: no GTK object defines a public FooClass struct
> or anything I can use as a pointer reference. Fiddling into the
> internals via GObject is the last thing I want to do. I can appreciate
> how GObject does its internals but without a pre-initialization hint,
> it makes things, in my world, impossible.

I am sorry but I don't understand what a public FooClass struct would
be. The GType of the widget should however be good enough to be used
as a reference.


Sven



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