Re: Argument passing / GTKTextView



Raymond Wan wrote:
>         My GTK+ program is getting fairly large and until now, I've had
> one big global variable structure that has all the information I need.  I
> was wondering if there is an alternative to this.

Hi Raymond, there are lots of ways to fix this.

One of the easiest is to have a struct for every window you pop up.
Allocate and fill the struct when you make one of the windows, pass the
address of the struct to every callback from widgets in that window, and
destroy the struct when the window is destroyed.

Something like (untested, I just typed this in):

	struct Wombat {
		GtkWidget *window;
		GtkWidget *label;
		GtkWidget *button;
		int my_data;
	}

	static void
	wombat_clicked( GtkWidget *button, Wombat *wom )
	{
		wom->my_data += 1;
	}

	static void
	wombat_destroy( GtkWidget *window, Wombat *wom )
	{
		printf( "you've had %d wombats\n", wom->my_data );

		g_free( wom );
	}

	Wombat *
	wombat_new( void )
	{
		Wombat *wom = g_new( Wombat, 1 );

		wom->window = gtk_window_new( ...
		wom->button = gtk_button_new( ...etc.

		gtk_signal_connect( GTK_OBJECT( wom->button ),
			"clicked",
			GTK_SIGNAL_FUNC( wombat_click ),
			wom );
		gtk_signal_connect( GTK_OBJECT( wom->window ),
			"destroy",
			GTK_SIGNAL_FUNC( wombat_destroy ),
			wom );

		gtk_widget_show( wom->window );
	}

Next up, you could make your own widgets: there's a section in the
tutorial about this:

	http://www.gtk.org/tutorial/sec-creatingacompositewidget.html

This lets you group widgets together, and have them operate as one
chunk. You can make your application as a big set of widgets which
contain other widgets. It's easier then it sounds.

Finally, you can go model/view, and split your app into things which
model your data (but do no display), and widgets which hold no
information themselves, but just give a view of the stuff in the model
widgets. This is very nice and flexible, but harder work.

HTH, John




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