Using FLUID


Say you have a C++ class called Class. Here's how to make a fluid interface for its variables.



Make a fluid file called ClassUI.fl, and add:

new > code> widget class
C++tab > Name: ClassUI_
new > code > decl > static  const int WW=200,HH=100;
new > code > decl > virtual void sync(Fl_Widget*o) {}

WW and HH is copied from GUItab > Width & Height. Cut-and-paste the fluid code to make more widget classes. You have to fill in WW and HH whenever you change the widget's size.


Add widgets in fluid, give each valuator a name (on the C++ tab) like xUI for controlling the x variable (in Class). Select all the widgets and add:

C++tab > Callback: sync(o);


In your c++ file, in emacs or whatever:

#include "ClassUI.h"              // what the FLUID file made

class ClassUI: public ClassUI_ {
 Class* obj;

 void sync(Fl_Widget*o) {
  if(!obj) return;
  //                              -- put changed values in class..
  if(o==xUI) obj->x( xUI->value() );
  if(o==yUI) obj->y( yUI->value() );
  //                              -- put class values in UI..

  xUI->value(obj->x());
  yUI->value(obj->y());
  //                              -- react to changes (if callback installed):
  if(o) do_callback();
 }

public:
ClassUI(Class*X=0): ClassUI_(0,0,WW,HH,"ClassUI") {obj=X; sync(0);}
};


Often all this fits in the header file, which saves typing.  The logic is all in one place, instead of hidden in fluid's tiny little boxes. You get to design the interface in fluid, and the fluid files have no dependencies and will always compile.



Installing a callback lets the rest of the program react when the UI is used.  Example:

class panel : public Fl_Window {

 static void CB(Fl_Widget*,void*v) {((panel*)v)->report();}
 Class     obj;
 ClassUI    ui;
 void report() { cout<<"obj was changed.%n"; }

public: panel(int W,int H,const char*l=0): Fl_Window(W,H,l),obj(),ui(&obj) {
   end();
   ui.callback(CB,this);
  } 

};




If the "Class" is another fltk widget, like a graphics window, the test program often looks like this:

#include <FL/Fl_Window.H>
#include <FL/Fl.H>
#include "Class.h"
#include "ClassUI.h"

int main(int argc,char**argv)  {
  int W=400,H=80;

  //                                -- put up widget:
  Fl_Window  win (W+20,H+20,"class");
  Class      widg(10,10,W,H);
  win.end(); win.resizable(&widg); win.show();

  //                                -- put up UI:
  Fl_Window winUI(ClassUI::WW,ClassUI::HH,"UI");
  UI           ui(&widg);
  winUI.end(); winUI.resizable(&ui); winUI.show();

  return Fl::run();
}