You are not logged in.

#1 2010-09-29 19:41:45

fuzic
Member
Registered: 2010-09-29
Posts: 2

[Solved] GTK signal_connect on object in structure with structure as..

Hi all,
I'm writing my first GTK/opengl program in C and I've hit a snag. Apologies in advance if I haven't provided enough information. There's quite a bit of code but I'd be happy to provide more context if it would help.

When I initialize my GUI I create a bunch of widgets, and certain widgets which I would like to access in callbacks I group into a structure:

struct updateBlock
{
...
    GtkWidget *drawingArea;
    GtkWidget *method1;
    GtkWidget *method2;
    GtkWidget *method3; 
    int a;
...
};

struct updateBlock *updateChunk = g_new(updateBlock,1);
updateChunk->a = 50;

updateChunk then contains several radio buttons (method1, method2, method3), and drawingArea is the openGL context for the app, and an integer set to 50.

I've set up expose-event callbacks on drawingArea which work fine:

static gboolean exposeCb(GtkWidget *drawingAreaBase, GdkEventExpose *event, struct updateBlock *updateChunk)
{
    gprint("%d\n",updateChunk->a);
...
}

g_signal_connect(updateChunk->drawingArea, "expose-event", G_CALLBACK(exposeCb), updateChunk);

which prints 50. But then I've got this callback on the radio button:

static gboolean methodToggle(GtkWidget *widget, GdkEvent *event, struct updateBlock *updateChunk)
{    
    grint("%d\n",updateChunk->a);
    return true;
}

gtk_signal_connect_object (GTK_OBJECT(updateChunk->method1), "clicked",GTK_SIGNAL_FUNC(methodToggle),updateChunk);

which prints 1146147856 (seems uninitialized?) I'm not really sure what's happening.

If anyone has any ideas I'd be very thankful. It seems like I'm missing something pretty fundamental here. Also if more code would help I can provide that too.

The widgets and callbacks are defined at the same point in the code. It doesn't matter when the callbacks occur, for instance I can call the working callback and get the right number, then the broken callback and get the wrong number, and then the working one again with the right number. The members of the structure passed in the broken callback just aren't initialized, but this should be the same structure as in the other callback which works.

Sorry for the verbosity and thanks in advance.

*Edit*
I just tried changing the signal that I was connecting on the radio buttons. If I change it to say "button-press-event" or "button-release-event" then the callback function prints the correct value, but when the event is set to "toggled" or to "clicked" it returns 1144932112. If the signal is "button-press-event" or "button-release-event" though the radio buttons can't actually be toggled, so obviously this won't solve my problem.

*Edit 2*
Even this callback will print 1131948416...

static gboolean methodToggle(GtkWidget *widget, GdkEvent *event, gpointer a)
{    
    gprint("%d\n",GPOINTER_TO_INT(a));
    return true;
}

g_signal_connect(updateChunk->method1, "toggled", G_CALLBACK(methodToggle), GINT_TO_POINTER(2));

Last edited by fuzic (2010-09-29 20:27:58)

Offline

#2 2010-09-29 20:27:09

fuzic
Member
Registered: 2010-09-29
Posts: 2

Re: [Solved] GTK signal_connect on object in structure with structure as..

Ok... silly me... this is because the callback function only needs two arguments for the toggle buttons.

I don't really understand why the callback on drawingArea would need 3 arguments and this one would need two, and I'd love an answer to that, but I'll mark this as solved since it works now.

Offline

#3 2010-10-26 09:39:25

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: [Solved] GTK signal_connect on object in structure with structure as..

the 2nd argument to expose-event is from GDK, which handles stuff like X input events and rendering, which isn't needed for the "toggled" signal, because it has nothing to do with GDK.


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

Board footer

Powered by FluxBB