On 20.09.2010, at 13:51, paul schrieb:

... in the FLTK it looks like the program reaches an end when it hits the 
return(Fl::run()); as it is the last accesible statement in Main, where is F1 
run() executing? so there is an event handling loop running 'somewhere'

That's exactly what it is doing in there. Handle all GUI events. It will
return only when all windows are closed (and thus the user can't create
events any more).

how can program flow be diverted back into my functions etc?
[...]
sorry its just a bit nuts to me, fundamentals i need to understand first, 
callbacks for example.

Yep, you got it ;-) It's the callback mechanism that lets you do most
things you want to do. Forget "event handling" in the first place -
that's some lower level thing you don't want to do for a simple GUI.

Let's see your previous description:

    while not quit
    {
       handle events
       switch event
       case clicked on start
            do algorithm        // this is called a callback
       break
       case //...
       case //...

       update screen
     }

That all is what Fl::run() does for you. The trick is that you
define a callback function that is called when you click the
start button. If you attach this callback function to a button,
it will be called when the user clicks the button. See attached
file simple-callback.cxx (a modified version of FLTK's hello world
program in the tutorial.

If you look at the code you will see that it's easy to start a
callback function. But callbacks must be short, because they
interrupt FLTK's event handling, and thus there will be no GUI
updates (drawing) until your program returns from the callback.

That's where you must periodically call FLTK's event loop *from*
your callback ("do algorithm") by either calling

  Fl::check(); or Fl::wait(0);

if your callback takes longer than about 1/10th of a second.

This lets FLTK serve other events while your algorithm can do
the calculations or data retrieval.

OTOH, things are different if you go Ian's example route with
a multithreaded program.

The attached program should only show you the basics about
flow control with callbacks.

HTH

Albrecht
// simple callback demo
// compile and run using:
//  $ fltk-config --compile simple-callback.cxx && ./simple-callback

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>

// global variables for easier coding

static int state = 0;

// simple callback function

void button_cb (Fl_Widget *w, void *) {

  Fl_Button *b = (Fl_Button *)w;        // the button

  if (!state) {
    b->label (" - S T O P -");
    state = 1;
  } else {
    b->label ("S T A R T");
   state = 0;
  }
  b->redraw();
}

int main(int argc, char **argv) {
  Fl_Window *window = new Fl_Window(300,180);
  Fl_Button *button = new Fl_Button(20,40,260,100,"S T A R T");
  button->labelfont(FL_BOLD+FL_ITALIC);
  button->labelsize(36);
  button->labeltype(FL_SHADOW_LABEL);
  button->callback(button_cb);          // this defines the button's callback !
  window->end();
  window->show(argc, argv);
  return Fl::run();
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to