
This is a little example that shows how to use C++ with
fdesign. They key point here is how one can recover a
valid "this" pointer in a static method. This is simply done
by storing the "this" pointer in the u_vdata field of the
form. When a callback is called, it is very simple to
get to the pointer because the FL_OBJECT has a field
form which is the form it is in. So we do something like:

   ClassPtr *ptr = (ClassPtr *) object->form->u_vdata;

Now we have a valid object pointer and we can call regular
methods from this poiinter.

This example contains the following sources:


FormApp.{cc, h}       The base class that encapsulate the Forms Library
MyFormClass.{cc, h}   A FormApp derived class that wraps around the
                      form generated by fdesign
SubFormClass.{cc,h}   Another FormApp derived class that wraps around the
                      form generated by fdesign. But I included
                      one of these in the MyFormClass class.
main.cc               An example of using the MyFormClass
MyForm.fd             The fdesign file
MyForm.{c, h}         The files generated by fdesign
Makefile              this on should be evident per see
Classes.h             A file containing all the headers of the classes
                      referenced in MyForm.c. When generating the
                      code with fdesign, the -I Classes.h should
                      be used.


There is also one last point. When fdesign generates the
header file, it adds some prototypes of callback
functions without checking if they are C++ methods. And
it causes a compiler error because we have a line that
looks like:

extern void  MyFormClass::_MyFormCallback(FL_OBJECT *, long);

I put in the Makefile a little workaround for that. It is just
a perl statement that comments out these lines in the
header file. This bug should be fixed in the next release.

I guess it is it. 

Hope this helps.

Have fun

Jacques Tremblay <jackt@gel.ulaval.ca>
Universite Laval
Quebec

