Re: Pass more widgets as gpointer.



Hello again
Well, yes, you are right about globals (I don't use them, but tried to
show one of the ways to resolve a task). But, I think, usage of many
structures also makes code hard readable. Just imagine, you have
Struct1 (with some objects), Struct2, Struct3 etc. There are too many
structures with single (or double) time useful. It isn't good too.
For example, I have a structure type with pointer members (it is just
a type), then I create an object and fill it's members with allocated
memory chunks. After that, I pass it as a callback variable (and use
it in code). But!!! how do I detach and free these chunks or free
other pointers? In "handmade" destructor? No. After callback
initialization? Again no, because this callback is using only pointer,
and after the object deallocation, it will point to dirty place. You
will also have to use global variables.
That is why, I prefer GObjects and manipulate with "self" variables,
constructors and destructors.

Best regards,
Vlad Volodin.

2009/4/21 Claudio Saavedra <csaavedra igalia com>:
On Tue, Apr 21, 2009 at 08:57:41AM +0400, Vlad Volodin wrote:
Hello, Jens

The other way is to make your widgets global. Especially, me? I use
GOjects, and always pass "self" object. For example, I inherit Âmain
window object from GtkWindow and later in widgets definition pass
itself to callback. Also, you can use g_object_set_data for storing
some useful pointers.

Don't use global variables, they make the code hard to read and
difficult to understand the context of the variables.


Good luck

2009/4/20 Jens Hansen <jensh604 gmail com>:
Hello All.

I'm quite new to C, but have done some gtk programming in perl.
I've been looking for answers on the web, and in various tutorials, without
any luck.
My question is how to pass more widgets as a gpointer, to a callback
function. (in C )

For instance if a "OK" button, has to read the state of three other
widgets...

In perl I would make an array of the widgets, and then pass the ref as the
data argument to the callback. I cannot grasp how to do something like that
in C.

A typical approach to this is to create a FooContext structure that will
hold pointers to all the elements that you need to use in your callback.
You can instanciate it with g_new0() and later free it with g_free(). Be
careful to hold object references and release them when you don't need
them anymore.

You could also use an array for this, but I prefer the structure
approach, since you don't want to rely on the order you put the elements
in the array later when you use them, removing/adding more elements you
may need in the future can be a little cumbersome, etc.

Claudio


An example snippet would be really helpfull.

Thx

Jens
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

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