Re: Newbie Question




i have a function defined by
void window_createMatrix(int rows, int colums, float **pointerMatrix, );
and a button "WhatSoEver", is there a way to use the function by
clicking the button?  i try to use the
g_connect_event(G_OBJECT(WhatSoEver), "clicked",
window_createMatrix, Parameters List); but the g_connect_event
only takes 4 parameters, and my function requires 3 more.

You can create atrbitrary complex structure and pass a pointer to
it as user_data. The signal handler then unpacks it and calls your
function with the appropriate arguments. 

A short example would be nice.....  How about (just off the top of
my head):

struct window_createMatrix_args {
  gint something;
  gchar *something_else;
  guint handler;  // just to demonstrate
};

...
struct window_createMatrix_args *ea;
ea = gnew0(struct window_createMatrix_args, 1);
ea->hander = g_connect_event(G_OBJECT(WhatSoEver), "clicked",
  (GCallback) window_createMatrix_cb, ea, (GClosureNotify) gfree, 0);
ea->something = 1234;
...

void window_createMatrix_cb(GtkButton *button, gpointer data) {
  struct window_createMatrix_args *ea = data;
  window_createMatrix(ea->something, ea->somethine_else);
}


Note that if the callback is going to exist for the length of the programs life, and the window will only be 
instantiated once, it may be easier to make the struct a global and pass NULL in the callback (also avoids 
allocating the memory and setting up the DestroyNotify).  But you still need an intermediate function to 
re-write the signal arguments (window_createMatrix_cb).


This is probably the
single most frequently asked question ever -- competing with `I
installed GLib to /somewhere, but forgot to add
/somewhere/lib/pkgconfig to PKG_CONFIG_PATH.'  Maybe it could be
added to the FAQ, even though it has nothing to do with Gtk+, just
with general programming in C.

Well, it is a feature of GTK that this concept is needed.  If GTK provided a means to reach callbacks with 
arbitrary arguments (I think I read that gtkmm supports this?), then such things wouldn't be needed.  
However, I'm not suggesting we go and write a g_signal_connect_with_arbitrary_arguments function, the 
overhead would probably be quite nasty for using it on a regular basis.  And the solution, though a little 
hackish, is quite easy and flexible.  (It wouldn't even be hard to go to the extent of boxing the structure 
so it could be passed to several callbacks, and having g_unref as their destroy notify function)

On the other hand, a general solution may not be too bad an idea, if someone knows how to attach the contents 
of a varargs to the end of a function call.  (I think some versions of gcc can do it...)


Fredderic

_______________________________________________
Join Excite! - http://www.excite.com
The most personalized portal on the Web!



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