On 27 Apr 2011, at 16:33, chris tyt wrote:

> void sleepcb(Fl_Widget *, void *)
> {
>       b1->deactivate();
>       Fl::flush();
> 
>       sleep(3);

I don't think your use of sleep() here is valid - it's a blocking call, so you 
will block the fltk event handling *whilst you are inside a callback event* and 
then all bets are off.

You want to try using a fl_timer to set/clear the button state, and see if the 
effect is still present.
See amended code below...

> 
>       Fl::flush();
>       b1->activate();
> }

Anyway, in general, DO NOT do long calculations in a callback, or any sort of 
blocking operation, as that *will* break the event handling.

If you must do long calculations, contrive to pump the event loop by calling 
Fl::check() from time to time. (Though that obviously does not help for 
blocking calls.)

If you *need* to make blocking calls, look into using timer callbacks, or 
Fl::add_fd() to handle pipe/socket events, or use a thread.
Don't ever stick them into a callback, as that will always bite you at some 
point!

/* revised code */
#include <stdio.h>
#include <unistd.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Button.H>

Fl_Button* b1;

void hellocb(Fl_Widget *, void *) {
  printf("hello\n");
}

void timercb(void*) {
  b1->activate();
}

void sleepcb(Fl_Widget *, void *) {
  b1->deactivate();
  Fl::add_timeout(3.0, timercb);
}

int main( int argc, char** argv) {
  Fl_Window *window = new Fl_Window(320,65);
  b1 = new Fl_Button(20,20, 200, 25, "hello");
  Fl_Button *b2 = new Fl_Button(220,20, 80, 25, "sleep(3)");
  b1->callback(hellocb,0);
  b2->callback(sleepcb,0);
  window->end();
  window->show();
  
  return Fl::run();
}

/* end of code */



_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to