Re: XForms: Global data

Ian Leonard (ian@eonsw.demon.co.uk)
Fri, 13 Mar 1998 08:03:33 +0000 (GMT)

# To subscribers of the xforms list from Ian Leonard <ian@eonsw.demon.co.uk> :

>From the keyboard of Chris comes:-
>
># To subscribers of the xforms list from Chris <deyz@hiphop.tbp.mb.ca> :
>
>I'm new at programming in X, and XForms is the first library I've really
>looked at in detail. I'm curious if there's a way to avoid using global
>data for a program using XForms? I can't see how passing variables
>(declared structures, etc) which haven't been declared globally, as
>parameters to callback functions which are called by xforms, is possible.
>If this issue has been brought up before, or if it's just general X
>programming, or if I'm not making any sense, could somebody point me in
>the right direction? My structures professor would kick my ass if I
>handed in a project with global data.
>
>Chris ..

You can treat C++ as C with extensions or you can use it as a Object
Orientated programming language. I have appended an example of how
I handle this. You need to create (say) a button class and a base class.
The base class has a function called callback () which is virtual.

When you call

fl_set_object_callback ( objs, base_callback, (long) this);

you pass the "this" pointer. The xforms callback functions converts this to
the base type class and calls callback in the base class. This is dynamical
bound to your button class. Clear?

This really is quite simple provided you understand C++. It never fails to
amaze me how may programmers haven't grasped this because (IMHO) it's the
only way to do it - but you have to construct your whole application
in an OO way.

I am writing a set of classes that use this system. If anyone is interested
in more detains please let me know because there are other issues involved
not all of which can be solved with the OO model.

-- 
Ian

Ian Leonard eMail: ian@eonsw.demon.co.uk Phone Fax: +44 (0)1865 434757 Fax: +44 (0)1865 434758 19 Stapleton Road Headington, Oxford, OX3 7LX, UK

Please ignore spelling and punctuation - I did.

-- example.cc --

#include <stdio.h> #include <forms.h>

// Must be in C scope extern "C" { void base_callback ( FL_OBJECT *obj, long th); }

// This is the base class used for all X forms classes class CBASE { public: virtual void callback ( CBASE *) { printf ( "Never gets called\n"); } };

// This receives a pointer to the base class void base_callback ( FL_OBJECT *obj, long th) { CBASE *base = (CBASE *) th;

base->callback ( base); }

class BUTTON : public CBASE { FL_OBJECT *objs; public:

BUTTON () { objs = fl_add_button( FL_NORMAL_BUTTON, 20, 20, 100, 40, "C++ test"); fl_set_object_callback ( objs, base_callback, (long) this); }

// This class is dynamically bound to the base class member virtual void callback ( CBASE *) { printf ( "Button pressed\n"); }

};

// This is just the normal stuff // main ( int argc, char *argv[]) { FL_FORM *flform; BUTTON *b; // Our button class FL_OBJECT *obj;

fl_initialize ( &argc, argv, "C++", 0, 0);

flform = fl_bgn_form ( FL_NO_BOX, 200, 200); fl_add_box( FL_FLAT_BOX,0,0, 200, 200,"");

b = new BUTTON;

fl_end_form ();

fl_show_form( flform,FL_PLACE_CENTER,FL_FULLBORDER, "C++ Example");

do { obj = fl_do_forms(); } while (1);

return 0; }

// The end _________________________________________________ To unsubscribe, send the message "unsubscribe" to xforms-request@bob.usuf2.usuhs.mil or see http://bob.usuf2.usuhs.mil/mailserv/xforms.html XForms Home Page: http://bragg.phys.uwm.edu/xforms List Archive: http://bob.usuf2.usuhs.mil/mailserv/list-archives/