Re: GUI programming vs encapsulation
- From: "John (J5) Palmieri" <johnp redhat com>
- To: Daniel Haude <dunno stoptrick com>
- Cc: gtk-app-devel-list gnome org
- Subject: Re: GUI programming vs encapsulation
- Date: Fri, 04 Feb 2005 09:23:32 -0500
On Fri, 2005-02-04 at 04:54, Daniel Haude wrote:
Hello list,
as my Glade/GTK project is growing larger, I find myself confronted with
code organization issues. I have all my callbacks in a single file, but
of course there are groups of related widgets that logically go together
(say, the widgets inside a top-level dialog window). Also the widgets of
one dialog only need to know about each other, but not about widgets in
other windows.
The issue is how to separate these things logically other than just
splitting up the source code in comment-separated chunks.
1. Separate source files for each dialog.
-> unpractical because this is a single Glade project, and Glade
only writes single source files.
2. Storing information about widgets (i.e., pointers to widgets) inside
the parent top-level widget using the G_OBJECT methods
(g_object_set_data() and friends), and finding the widget with the
lookup_widget() funcion supplied with Glade
-> I somehow don't like it. I like mean, lean, and fast programs,
and the mere thought that each push of a button or movement of a slider
generates an avalanche of hash lookups gives me ulcers, and it litters
the code with "magic" lookup key strings.
3. For each widget, declare a global pointer variable, a "create" and a
"delete" callback. In the create callback I set the global variable to
the pointer to the widget, and in the delete callback I set the global
variable to NULL so that other parts of the program know that this
widget doesn't exist any more.
-> No encapsulation and more callbacks just to set/unset global
variables.
Currently I'm using a mixture of 2. and 3. (depending on how I feel that
day), but leaning towards 3. without much passion.
So how do you guys deal with this problem? I guess there is no real way
out of having all the callbacks spread out in this "flat" fashion
because from the C program's point of view there's no telling in what
order they might be called. Is writing unwieldy spaghetti code a
necessary part of GUI programming, or have I missed some important point?
C's not flat. Use gobject or at least structs to organize your code.
Never use globals and only use static variables sparingly. Look how
other large projects organize their code. Open Source is good in that
way. Usually large apps with have a main gobject that acts as a factory
for the app. Sample main:
int
main (int argc, char *argv[])
{
MyApp *app;
app = my_app_new();
gtk_main();
}
MyApp would be a gobject that holds all the widgets you care about
manipulating and pointers to any other data needed by parts of the
program. The reference to this object would be passed to callbacks as
userdata. You also don't need to hold the glade widget tree in memory
if you do things this way. Hope this helps.
--
J5
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]