g_object_get_data Programmer error

View: New views
2 Messages — Rating Filter:   Alert me  

g_object_get_data Programmer error

by Steve Harrington-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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@...>
*/

/* 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 );
}

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@...
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Re: g_object_get_data Programmer error

by Florian Müllner :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

El mar, 03-11-2009 a las 07:39 -0800, Steve Harrington escribió:
> 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.

Indeed - g_object_get_data retrieves data explicitly set on an object
with g_object_set_data. No call to the latter in your code ...

Use something like the following snippet to make it work:

> Main = GTK_WIDGET( gtk_builder_get_object(builder, "Main") );

g_object_set_data( G_OBJECT(Main),
                   "Button",
                   gtk_builder_get_object(builder, "Button") );

> gtk_builder_connect_signals( builder, NULL );
> g_object_unref( G_OBJECT(builder) );
> gtk_widget_show_all( Main );
> gtk_main( );


Another annotation just in case:

> 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),
                      "Found \"Button\" widget%s",
                      W == widget ? ", which is passed as callback
parameter"
                                  : "");

>    gtk_dialog_run( GTK_DIALOG(dialog) );
>    gtk_widget_destroy( dialog );
>  }

So in case of the example, g_object_get_data is not necessary, as the
button widget is directly available to the callback handler.

Hope the above snippets are helpful.

Happy hacking,
Florian

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@...
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list