/*

        callback.cxx
        ---------
        Co. E.Benninghaus
            Pfaffenhoffenstr. 1
            78166 Donaueschingen
	
        created:   08.09.2000
        last Work: 08.09.2000

*/


#ifndef __callback_cxx
#define __callback_cxx


extern void threaded_grabbing(char * data);
int cfgshown = 0;


void cb_btn_exit(Fl_Button*, void*) {
	
	endall();
}


void cb_btn_start(Fl_Button*, void*) {

	if(cfgshown){
		return;
	}

	btn_stop->value(0);
	
	if( (! run ) && (! threaded_work_busy ) && (! threaded_grab_busy ) ){
		ethread = NULL;
		if(pthread_create(&ethread, NULL, (void *)&threaded_grabbing, NULL) != 0 ){
			fprintf(stderr,"\nCannot create grabbing thread\n");
		}
		else {
			btn_stop->value(1);			
		}
	}
}


void cb_btn_stop(Fl_Light_Button*, void*) {

	if(cfgshown){
		return;
	}

	if( cfg.snap ){
		cfg.snap = 0;
		btn_snap->value(0);
	}
	
	run = 0;
	btn_stop->value(0);

}



void cb_btn_showcross(Fl_Light_Button*, void*) {

	if(cfgshown){
		return;
	}
	
	if(cfg.showcross){
		cfg.showcross = 0;
		btn_showcross->value(0);
	}
	else {
		if(cfg.view){
			cfg.showcross = 1;
			btn_showcross->value(1);
		}
	}
}


void cb_btn_realview(Fl_Light_Button*, void*) {

	if(cfgshown){
		return;
	}
	
	if(cfg.savebuf){
		cfg.savebuf = 0;
		btn_realview->value(0);
	}
	else {
		if(cfg.view){
			cfg.savebuf = 1;
			btn_realview->value(1);
		}
	}
}



void cb_btn_snap(Fl_Light_Button*, void*) {

	if(cfgshown){
		return;
	}
	
	if( cfg.snap ){
		btn_snap->value(0);
		cfg.snap = 0;
	}
	else {
		btn_snap->value(1);
		cfg.snap = 1;
	}
}



void cb_btn_view(Fl_Light_Button*, void*) {

	if(cfgshown){
		return;
	}
	
	if( cfg.view ){
		btn_view->value(0);
		cfg.view = 0;
	}
	else {
		cfg.view = 1;
		btn_view->value(1);
	}
}


void cb_btn_control(Fl_Light_Button*, void*) {

	if(cfgshown){
		return;
	}
	
	if( cfg.control ){
		btn_control->value(0);
		cfg.control = 0;
	}
	else {
		if(cfg.fifo){
			cfg.control = 1;
			btn_control->value(1);
		}
	}
}




void ShowStrategyItems() {

	int st = ch_strategy->value();

	switch( st ){

		case 0:
			in_calcstart->show();
			in_maxcalckoor->hide();
			in_percentcorrect->hide();
			in_xraster->hide();
			in_yraster->hide();
			in_coltolerance->show();
			in_seeing->show();
			in_coverinterval->hide();
		break;

		case 1:
			in_calcstart->show();
			in_maxcalckoor->show();
			in_percentcorrect->show();
			in_xraster->show();
			in_yraster->show();
			in_coltolerance->show();
			in_seeing->show();
			in_coverinterval->hide();
		break;

		case 2:
			in_calcstart->show();
			in_maxcalckoor->hide();
			in_percentcorrect->show();
			in_xraster->show();
			in_yraster->show();
			in_coltolerance->show();
			in_seeing->show();
			in_coverinterval->show();
		break;

	}
}



void cb_ch_strategy(Fl_Choice*, void*)
{
	ShowStrategyItems();
}


char cb_tmptxt1[20];
char cb_tmptxt2[20];
char cb_tmptxt3[20];
char cb_tmptxt4[20];
char cb_tmptxt5[20];
char cb_tmptxt6[20];
char cb_tmptxt7[20];
char cb_tmptxt8[84];
char cb_tmptxt9[40];
char cb_tmptxt10[20];
char cb_tmptxt11[40];

void cb_btn_cfg(Fl_Button*, void*) {
	
	if( run || cfgshown ){
	  	return;
	}

	cfgshown = 1;

	sprintf(cb_tmptxt1,"%ld",cfg.calcstart);
	in_calcstart->value(cb_tmptxt1);

	sprintf(cb_tmptxt2,"%d",cfg.maxcalckoor);
	in_maxcalckoor->value(cb_tmptxt2);

	sprintf(cb_tmptxt3,"%d",cfg.coltolerance);
	in_coltolerance->value(cb_tmptxt3);

	sprintf(cb_tmptxt4,"%d",cfg.percentcorrect);
	in_percentcorrect->value(cb_tmptxt4);

	sprintf(cb_tmptxt5,"%d",cfg.seeing);
	in_seeing->value(cb_tmptxt5);

	sprintf(cb_tmptxt6,"%d",cfg.xraster);
	in_xraster->value(cb_tmptxt6);

	sprintf(cb_tmptxt7,"%d",cfg.yraster);
	in_yraster->value(cb_tmptxt7);

	sprintf(cb_tmptxt8,"%s",cfg.snapprefix);
	in_snapprefix->value(cb_tmptxt8);

	sprintf(cb_tmptxt9,"%ld",cfg.maxsnap);
	in_maxsnap->value(cb_tmptxt9);

	sprintf(cb_tmptxt10,"%d",cfg.snapquality);
	in_snapquality->value(cb_tmptxt10);

	sprintf(cb_tmptxt11,"%ld",cfg.coverinterval);
	in_coverinterval->value(cb_tmptxt11);


	ch_strategy->value(cfg.strategy);
	ch_koortype->value(cfg.koortype);
	ch_snapsize->value(cfg.snapsize);

	ShowStrategyItems();
	
	cfgwin->show();
}



void cb_btn_cfg_quit(Fl_Button*, void*) {

	
	cfg.calcstart  		= atol((char *)in_calcstart->value());
	cfg.maxcalckoor  	= atoi((char *)in_maxcalckoor->value());
	cfg.coltolerance  	= atoi((char *)in_coltolerance->value());
	cfg.percentcorrect  = atoi((char *)in_percentcorrect->value());
	cfg.seeing  		= atoi((char *)in_seeing->value());
	cfg.xraster  		= atoi((char *)in_xraster->value());
	cfg.yraster  		= atoi((char *)in_yraster->value());
	cfg.snapquality     = atoi((char *)in_snapquality->value());
	cfg.maxsnap  		= atol((char *)in_maxsnap->value());
	cfg.coverinterval  	= atol((char *)in_coverinterval->value());

	cfg.strategy        = ch_strategy->value();
	cfg.koortype        = ch_koortype->value();
	cfg.snapsize        = ch_snapsize->value();

	if(strcmp( (const char *)in_snapprefix->value(), (const char *)cfg.snapprefix ) != 0){	
		sprintf(cfg.snapprefix,"%s",in_snapprefix->value());
		cursnapcount        = 0;
	}

	correctCfgValues();

	cfgshown = 0;
	cfgwin->hide();
}




#endif /*__callback_cxx*/