g_object_get_data Programmer error



I am trying to use g_object_get_data() to retrieve the GtkWidget * of an object. Seems simple enough but The attached code doesn't work. I must have missed something simple but darned if I can see it. All help appreciated.
/*
*  This program is free software; you can redistribute it and/or
*  modify it under the terms of version 2 of the GNU General Public License
*  as published by the Free Software Foundation.
*
*  GTKTest - Simple program to test various aspects of the Gtk API
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*  Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*
*  Copyright (C) Steven Harrington 2009 <steven harrington verizon net>
*/

/* GTKBuilder file GTKTest.xml:

<?xml version="1.0"?>
<interface>
<requires lib="gtk+" version="2.16"/>
<!-- interface-naming-policy project-wide -->
<object class="GtkWindow" id="Main">
<signal name="destroy" handler="GTKExit"/>
<signal name="activate_default" handler="GTKButtonCB"/>
<child>
  <object class="GtkButton" id="Button">
    <property name="label" translatable="yes">button</property>
    <property name="visible">True</property>
    <property name="can_focus">True</property>
    <property name="receives_default">True</property>
    <signal name="clicked" handler="GTKButtonCB"/>
  </object>
</child>
</object>
</interface>

*/

/*
*  Compile with:
* gcc -o GTKTest GTKTest.c `pkg-config --cflags gtk+-2.0 gmodule-2.0` * `pkg-config --libs gtk+-2.0 gmodule-2.0`
*/

#include <stdlib.h>
#include <gtk/gtk.h>

void GTKExit( GtkWidget *widget, gpointer data );
void GTKButtonCB( GtkWidget *widget, gpointer data );

static GtkBuilder *builder;
static GtkWidget  *Main;

void GTKExit( GtkWidget *widget, gpointer data )
{
exit( EXIT_SUCCESS ); }

void GTKButtonCB( GtkWidget *widget, gpointer data )
{
GtkWidget      *W;


if( (W = g_object_get_data(G_OBJECT(Main), "Button")) != NULL )
{
  GtkWidget *dialog;
      dialog = gtk_message_dialog_new( NULL,
                   GTK_DIALOG_DESTROY_WITH_PARENT,
                   GTK_MESSAGE_ERROR,
                   GTK_BUTTONS_CLOSE,
                   "%s",
                   "GTKButtonCB" );
  gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG(dialog),
                    "%s",
                    "Found \"Button\" widget" );
  gtk_dialog_run( GTK_DIALOG(dialog) );
  gtk_widget_destroy( dialog );
}
else
{
  GtkWidget *dialog;
      dialog = gtk_message_dialog_new( NULL,
                   GTK_DIALOG_DESTROY_WITH_PARENT,
                   GTK_MESSAGE_ERROR,
                   GTK_BUTTONS_CLOSE,
                   "%s",
                   "GTKButtonCB" );
  gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG(dialog),
                    "%s",
                    "\"Button\" widget not found" );
  gtk_dialog_run( GTK_DIALOG(dialog) );
  gtk_widget_destroy( dialog );
}
}

int main( int argc, char *argv[] )
{
GError     *error=NULL;    gtk_init( &argc, &argv );

builder = gtk_builder_new( );
if( gtk_builder_add_from_file( builder, "GTKTest.xml", &error ) == 0 )
{
  GtkWidget *dialog;
      /* Create the widgets */
  dialog = gtk_message_dialog_new( NULL,
                   GTK_DIALOG_DESTROY_WITH_PARENT,
                   GTK_MESSAGE_ERROR,
                   GTK_BUTTONS_CLOSE,
                   "GTK_Builder" );
  gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG(dialog),
                    "%s",
                    error->message );
  gtk_dialog_run( GTK_DIALOG(dialog) );
  gtk_widget_destroy( dialog );
  exit( EXIT_FAILURE );
}

Main = GTK_WIDGET( gtk_builder_get_object(builder, "Main") );
gtk_builder_connect_signals( builder, NULL );
g_object_unref( G_OBJECT(builder) );
gtk_widget_show_all( Main );
gtk_main( );

/* Should never get here */  exit( EXIT_FAILURE );
}




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