Re: [linux-audio-dev] [ot] How do GUI-libs notify the program of changes?

From: Alfons Adriaensen <fons.adriaensen@email-addr-hidden>
Date: Mon Apr 10 2006 - 17:09:01 EEST

On Mon, Apr 10, 2006 at 03:11:49PM +0200, Julien Claassen wrote:
> Hi all!
> I know it's completely OT, but I think there maybe people here, who could
> help me.
> Problem is: I'm still on my libcui (character user interface) project and I
> wonder:
> I push a button, slide a slider... How does the UI notify the program of
> this change? How do Engine and UI communicate?
> Please anyone: HELP ME!

Hello Julien,

This is a tricky subject, and most toolkits have their own peculiar
methods of solving the problem.

In general terms this needs callback functions - a function supplied
as a creation parameter to the widget, and which it will call when
it has something to report.

In C this is quite easy to arrange. The widget is given a function
pointer, and usually also a void* which becomes the first argument
of the callback fucntion and points to any context the receiver wants
to be available when called back. Other arguments depend on what the
callback needs to report.

In C++ you will want class function members to act as callbacks, and
here things get complicated. Since callback functions are strongly
typed and this type depends on the class they are in, there is no single
variable type you can put into the widget to hold the function pointer.

Three solutions:

1. All callbacks first go to a static class function which in most
compilers have "C" semantics. So this requires two functions for each
callback, the first static one which is seen by the widget as a plain
C function, and which then calls the second one. In that case the
void* mentioned above would point to the C++ object that receives the
call, and the static callback casts it to the right class pointer type
and then calls the class member function via that pointer.

2. Use templates. This is nicely encapsulated in GLib, part of GTK,
which you can probably re-use (it doesn't depend on the graphical
stuff AFAIK).

3. Define on or more callback classes, containing on or more virtual
functions. All receiver classes inherit from this or these classses,
and a pointer to this type is passed at creation time to each widget.
This is what I use in libclxclient.

Unless you want to do a lot of things yourself, I guess the GLib
way will be the easiest, or else stick to plain C (complicating
matters for C++ users, by forcing them to use the indirect callback
method explained above).

-- 
FA
Follie! Follie! Delirio vano e' questo!
Received on Mon Apr 10 20:15:03 2006

This archive was generated by hypermail 2.1.8 : Mon Apr 10 2006 - 20:15:03 EEST