[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]
RE: widget array
- From: "Chris Jones" <chris black-sun co uk>
- To: <gtk-app-devel-list redhat com>
- Subject: RE: widget array
- Date: Wed, 20 Oct 1999 02:27:18 +0100
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi
> Is it possible to set up an array of gtk widgets and pass the
array
> to a function with gtk_signal_connect()? I have a window that
Having seen the code that has come out of this thread I would suggest
that you seriously think about putting the widgets in a struct rather
than an array. It will certainly make the code much easier to
understand!
Here is a snippet of code from an app I'm working on atm. I have a
window that has to be created and then setup some callbacks which must
be able to modify the data held within the main window's widgets.
The gadgets within the window are defined in a struct thus:
____________________________
struct account_dialog {
GtkWidget *dlg;
GtkWidget *root;
GtkWidget *rootpaned;
GtkWidget *rootpaned_left;
GtkWidget *rootpaned_right;
GtkWidget *root_bottom;
GtkWidget *account_list;
GtkWidget *right_vbox;
GtkWidget *button;
};
____________________________
Then the window is created thus:
____________________________
void cb_accounts_dialog_new( GtkWidget *widget, gpointer data )
{
struct account_dialog *acc;
acc = g_malloc( sizeof( *acc ) ); /* Allocate memory for the
structure */
acc->dlg = gnome_dialog_new("Accounts", GNOME_STOCK_BUTTON_OK, NULL
);
acc->root = gtk_vbox_new( FALSE, 0 );
acc->rootpaned = gtk_hbox_new( FALSE, 0 );
acc->rootpaned_left = gtk_clist_new_with_titles( 3,
accounts_list_titles );
acc->rootpaned_right = gtk_vbox_new( FALSE, 0 );
acc->root_bottom = gtk_hbox_new( FALSE, 0 );
gtk_box_pack_start( GTK_BOX( acc->rootpaned ), GTK_WIDGET(
acc->rootpaned_left ), TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX( acc->rootpaned ), GTK_WIDGET(
acc->rootpaned_right ), FALSE, TRUE, 5 );
gtk_box_pack_start( GTK_BOX( GNOME_DIALOG( acc->dlg )->vbox ),
GTK_WIDGET( acc->rootpaned ), TRUE, TRUE, 0 );
acc->button = gtk_button_new_with_label( " Add " );
gtk_box_pack_start( GTK_BOX( acc->rootpaned_right ), GTK_WIDGET(
acc->button ), FALSE, FALSE, 5 );
/**** NOTE: This is where I connect the signal handler to the widget
and pass it the structure */
gtk_signal_connect( GTK_OBJECT( acc->button ), "clicked",
GTK_SIGNAL_FUNC( cb_accounts_dialog_add_new ), acc );
acc->button = gtk_button_new_with_label( "Delete" );
gtk_box_pack_start( GTK_BOX( acc->rootpaned_right ), GTK_WIDGET(
acc->button ), FALSE, FALSE, 5 );
gtk_signal_connect( GTK_OBJECT( acc->button ), "clicked",
GTK_SIGNAL_FUNC( cb_accounts_dialog_delete ), acc );
fill_accounts_list( acc );
gtk_widget_show_all( acc->dlg );
gnome_dialog_run_and_close( GNOME_DIALOG( acc->dlg ) );
g_free( acc ); /* Free the structure's memory */
return;
}
____________________________
Then the first callback used ("cb_accounts_dialog_add_new") would be
thus:
____________________________
gint cb_accounts_dialog_add_new( GtkWidget *widget, gpointer data )
{
/* Do whatever */
/* We can easily manipulate the widgets within the structure */
gtk_clist_clear( GTK_CLIST( data->rootpaned_left ) );
fill_accounts_list( data );
}
____________________________
It's also worth noting that the callback doesn't have to have the data
argument defined as "gpointer data", you could pass pretty much
anything by replacing that parameter with the correct definition (e.g.
instead of "gpointer data", I could have used "struct account_dialog
*data").
Advantages over an array are that you don't have to keep advancing
pointers, you can represent each widget in an easily readable manner
(e.g. "data->widget_that_makes_coffee") and you don't have to be
concerned about doing things in a certain order. With an array you
have to access the widgets in a certain order and either remember or
document that order (neither are as good as a structure definition
which is plainly obvious to all!)
I'm no GTK+ expert by any means, but doing it this way seems to make
more sense to me than using an array.
- ---
_____ _ _ _____
| __ | |___ ___| |_ ___| __|_ _ ___ Chris "Ng" Jones
| __ -| | .'| _| '_|___|__ | | | | chris@black-sun.co.uk
|_____|_|__,|___|_,_| |_____|___|_|_| www.black-sun.co.uk
S o f t w a r e
"Linux is beating Windows" - David Cole, Microsoft Executive
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 5.5.3i for non-commercial use <http://www.pgpi.com>
iQA/AwUBOA0adphmBipjerS3EQJEMwCg2cw0rN/3AwYEyHVUGu64xjOi+dAAn1jb
t44wIilrs5VUmXUTQXGov+ue
=Qlny
-----END PGP SIGNATURE-----
[Date Prev][Date Next] [Thread Prev][Thread Next]
[Thread Index]
[Date Index]
[Author Index]