[Gllug] Handwavy GTK2 question

Pete Ryland pdr at accucard.com
Fri Feb 6 14:49:50 UTC 2004


Hi will,

On Thu, Feb 05, 2004 at 11:25:55PM +0000, will wrote:
> I have created a window and then a button and statusbar insite that:
> 
> window------ statusbar
>          |
>           `- button
> 
> I have registered a callback on the button being pressed and this works, 
> I am using gprint to print out just an 'x' to stdout to test it.  The 
> problem is when I want to update the status bar from the callback. 
> Because the status bar was declared in a subroutine earlier on in the 
> program, the statusbar widget does not exist in the scope of the callback:
> 
> void on_ok_button_clicked (GtkButton *button, gpointer user_data) {
>   //this line fails, statusbar does not exist here:
>   gtk_statusbar_push(statusbar1, 
> gtk_statusbar_get_context_id(statusbar1, "a"), "foo");
> 
>   g_print ("x");
> }
> 
> Does anyone know how to reference the statusbar that was created in a 
> different subroutine in this instance?

It is common practice to use a technique something like this (below is
untested):

int
main (blah)
{
  ...
  statusbar = gtk_statusbar_new(...);
  g_object_set_data(G_OBJECT(gtk_widget_get_toplevel(GTK_WIDGET(button))), "statusbar", statusbar)
  ...
}

void
my_callback (GtkButton *button, gpointer user_data)
{
  GtkWidget *statusbar = g_object_get_data(G_OBJECT(gtk_widget_get_toplevel(GTK_WIDGET(button))), "statusbar");
  ...
}

This is very similar from what glade spits out.  See lookup_widget() in
support.c of glade.

Alternatively, you could make your window into a new class that derives from
GtkWindow and have the status bar as an attribute.

Pete
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list