Re: which way is better?




>be better to use global variables for Gtk widgets, and have all
>functions in that file use them, or create separate widgets for each
>function?

Well, if it's a quick program that isn't going to get big, global is ok
but it's not really stellar coding style and doesn't scale well at all.
In fact, it becomes an ugly mess pretty fast if you're not really careful.

>void exfunc(void)
>{
>    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
>    button = gtk_button_new_with_label("Button");
>    gtk_widget_show(button);                     
>    gtk_widget_show(window);
>}

Umm...I dunno if this is just example code that you wrote, or if you're
actually using it, but I think you need a line in there that says 
gtk_container_add(GTK_CONTAINER(window), button); to put the button i n
the window.

>By defining those widgets as globals, do I reduce the amount of memory
>used by the program?

No.  You just save yourself the "inconvenience" of passing them around in
signal handlers.  It's much better to use signal handlers though.  Example
of passing stuff around:

button = gtk_button_new_with_label("Foobar");
gtk_signal_connect(GTK_OBJECT(button), "clicked",
	          GTK_SIGNAL_FUNC(foobar_callback), "Foobar");

void foobar_callback(GtkWidget *emitter, gpointer data)
{
	/* Here, emitter is the button */
	printf("%s\n",(char *)data);
}

The "gpointer data" can be whatever you want, including widgets, data
structures, whatever. 

Hope this helps somewhat.

David Allen
http://opop.nols.com/ Free Software Development
Der Horizont vieler Menschen ist ein Kreis mit Radius Null -- und das
nennen sie ihren Standpunkt.



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