Wednesday, July 27, 2011

GTK program organization

Ok, so source control methods aside the one other thing I want to try differently is overall program structure.

The organization so far has had all of the GUI code right in the main() function. This is generally probably not a good idea. I've been trying to hash out the behavior of what's really happening when I'm writing some of this GUI code.

See, MOST of the stuff I'm writing is initializing pointers to GTK objects with some basic behaviors and then connecting signals to functions. The real business doesn't start until gtk_main() is called. What that means is I can offload widget initializations to other functions (in other .c files) and then call those functions in main() right before I call gtk_main().

So, here's an example of this in action:



It's important to remember that there's a lot happening in DrawWindow but all it's doing is initializing pointers that aren't going to go out of scope once the function is over. Calling the function DrawWindow was dumb since it's not doing the drawing. The function gtk_widget_show_all can be in main() instead of DrawWindow... I'm not sure which is "proper" but in this simple example it doesn't seem to make a difference.

I might add in a little interactivity just to verify things work like I expect them to, but hey I'll call tonights efforts a win.

No comments:

Post a Comment