Following this discussion I experimented a little and made a test program for a
splash screen without messing around with timers and signals. I started with
the threads example in the test folder. Due the implementation of threads.cxx
this code snipped only works in the test folder because it uses threads.h and
config.h.
The program creates two successive splash screens after startup while
performing (or simulating to perform) some time consuming operations.
Afterwards it displays the main app screen.
I tested it only on Linux.
//
// splash.cxx
//
// example program showing splash screen using a thread.
//
#include "../config.h"
#if HAVE_PTHREAD || defined(WIN32)
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Button.H>
#include <FL/Fl_Value_Output.H>
#include <FL/fl_ask.H>
#include "threads.h"
#include <stdio.h>
#include <time.h> // nanosleep()
Fl_Thread alive_thread;
void mysleep( int ms_ )
{
struct timespec delay;
delay.tv_sec = ms_ / 1000;
delay.tv_nsec = (ms_ % 1000) * 100000;
nanosleep(&delay, NULL);
}
void *alive_func( void *p )
{
while ( 1 )
{
mysleep( 100 );
Fl::lock();
Fl::check();
Fl::unlock();
}
}
static void button_cb( Fl_Widget *w_ )
{
printf( "button_cb\n" );
w_->window()->hide();
}
int main( int argc, char **argv )
{
Fl_Double_Window *win = new Fl_Double_Window( 200, 200, "Splash" );
Fl_Button *button = new Fl_Button( 0, 0, win->w(), win->h(), "Loading..." );
win->resizable(win);
win->end();
win->position( (Fl::w() - win->w()) / 2, (Fl::h() - win->h()) / 2 );
win->show(argc, argv);
button->callback( button_cb );
// One thread to keep FLTK alive
fl_create_thread( alive_thread, alive_func, win );
// a long blocking operation...
mysleep( 5 * 1000 );
// another long blocking operation...
button->label( "Installing.." );
win->size( 400, 400 );
win->position( (Fl::w() - win->w()) / 2, (Fl::h() - win->h()) / 2 );
for ( int i = 0; i < 5; i++ )
{
if ( !win->shown() )
break; // window closed by user
printf( "Installing...\n" );
mysleep( 1000 );
}
win->hide();
// main app starting
win = new Fl_Double_Window( 640, 480, "Application" );
win->position( (Fl::w() - win->w()) / 2, (Fl::h() - win->h()) / 2 );
button = new Fl_Button( 10, 10, 100, 30, "quit" );
button->callback( button_cb );
win->show();
return Fl::run();
} // main
#else
# include <FL/fl_ask.H>
int main()
{
fl_alert("Sorry, threading not supported on this platform!");
}
#endif // HAVE_PTHREAD || WIN32
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk