/***************************************************************************/
/* JITES                                                                   */
/* prog AL-KHANSA JAMAL                                                    */
/* r‚vis‚ … partir du 10-9-1992 comme maquette bxls                        */
/* LE 6-2-1991                                                             */
/* LANGAGE TC                                                              */
/*                                                                         */
/*                                VARIABLES                                */
/* Hcadre_candid = hauteur du cadre candidat                               */
/* Interl_candid = Interligne entre les cadres candidats                   */
/* Hsaisie_candid= hauteur de la zone de saisie dans cadre candidat        */
/* Lsaisie_candid = longueur de la zone de saisie candidat                 */
/* Lcadre_candid  = longueur du cadre candidat                             */
/* Interc_candid  = Intercolonne entre les cadres candidats                */
/* x_dep          = valeur de la coordonn‚e x de la  1 ere colonne         */
/* nbc_col        = nombre de candidats par colonne                        */
/***************************************************************************/
/******************   S E L E C T I O N  D U  C A N D I D A T  ************/
/**************************************************************************/
#define true  1
#define false 0

#define SECOND_LINE_CENTERED 0

int candidtmp,nbc_cols,Condense;
char bufc[10],bufitoa[10];
int Rest_Div,Tampon,Clon;
struct palettetype palette;  /*pour lire les infos de la palette*/

FILE *ptc, *fpc;
union REGS rin,rout;
int affichc()
{
  int m,status, val,nbccols;
  int nombre_col_bis,j;
  char cBuff[80], bufitoa[10];

  strcpy(Sname,f_name);
  for(j = 0;(j < 49) && (Sname[j] != 0x00) ; j++)
  {
    if(Sname[j] == '.') Sname[j] = 0x00;
  }

  Rest_Div = swpas = 0;
  /**************************   ENTETE CANDIDAT  ****************************/

  setfillstyle(SOLID_FILL,BLACK); /* Set to solid fill in color   */
  bar( 0,0,639,479);      /* Draw backgnd */
  setcolor( WHITE);
  settextstyle(SMALL_FONT, HORIZ_DIR, 6);
  setfillstyle(SOLID_FILL,7);
/**************                 TETE DE LISTE                 ***************/
  posc[0][0] = 82;

  /*if(iCandidateDisplayType == 1 && nombre_col == 3) posc[0][0] -= 7;*/

  posc[0][1] = posc[0][0] + 50;
  posc[0][2] = 2;
  posc[0][3] = 27;
  bar(posc[0][0] + 10 ,posc[0][2], posc[0][1] - 10 , posc[0][3]);
  cercle(0);

  /**************************   ENTETE CANDIDAT  ****************************/

  setfillstyle(SOLID_FILL,WHITE); /* Set to solid fill in color   */
  setcolor( WHITE);

  settextjustify(RIGHT_TEXT,CENTER_TEXT);  /* */
  settextstyle( SMALL_FONT, HORIZ_DIR, 5);
  outtextxy( 630, 13, Msg1);
  outtextxy( 629, 13, Msg1);

  settextstyle( SMALL_FONT, HORIZ_DIR, 6);

  settextjustify(RIGHT_TEXT,CENTER_TEXT);
  outtextxy(  posc[0][0] -10, 13, Msg2);
  outtextxy(  posc[0][0] -11, 13, Msg2);
  outtextxy(  posc[0][0] -10, 14, Msg2);
  outtextxy(  posc[0][0] -11, 14, Msg2);

  settextjustify(LEFT_TEXT,CENTER_TEXT);
  outtextxy( posc[0][1] +10, 13, Msg3);
  outtextxy( posc[0][1] +11, 13, Msg3);
  outtextxy( posc[0][1] +10, 14, Msg3);
  outtextxy( posc[0][1] +11, 14, Msg3);

  setcolor( WHITE);
  line(0,30,639,30);

  /**************                AFFICHAGE DES CANDIDATS        ***************/

  settextjustify(CENTER_TEXT,CENTER_TEXT);
  settextstyle( SMALL_FONT, HORIZ_DIR, 4);
  setcolor( BLACK);

  if (ts > 0) nombre_col = nombre_col -1;

  nombre_col_bis  = te / nbc_col;
  if((te % nbc_col) > 0) nombre_col_bis++;
  if(nombre_col_bis < nombre_col) nombre_col = nombre_col_bis;
  nbc_cols = nbc_col;
  candid = te + ts;

  nbc_col  = te / (nombre_col);
  Rest_Div = te % (nombre_col);

  if(nbc_col > 23)
  { nbc_col  = 23;
    Rest_Div =  0;
  }

  for(k=1,x = x_dep;(k < te + 1);x+= Lcadre_candid + Interc_candid)
   {
    if((Rest_Div == 0) || (nbc_col == nbc_cols))
                                        Tampon = nbc_col;
    else
     {
         Tampon = nbc_col + 1;
         Rest_Div--;
     }


     for(j = 0,y=Debut_Affichage;(k < te + 1) && j < Tampon;y+= Hcadre_candid + Interl_candid,k++,j++)
     {
         posc[k][0]= x;
         posc[k][1]= (x + Lcadre_candid) - 1;
         posc[k][2]=  y;
         posc[k][3]= (y + Hcadre_candid) - 1;

         settextjustify(CENTER_TEXT,CENTER_TEXT);
    	 settextstyle( SMALL_FONT, HORIZ_DIR, 5);
    	 setcolor( WHITE);

         settextjustify(CENTER_TEXT,CENTER_TEXT);
         settextstyle( SMALL_FONT, HORIZ_DIR, 4);
         setcolor( BLACK);

         setfillstyle(SOLID_FILL,WHITE);
         bar(x,y,posc[k][1],posc[k][3]);

         /* Num */
         settextjustify(CENTER_TEXT,CENTER_TEXT);
         settextstyle( SMALL_FONT, HORIZ_DIR, 5);
         outtextxy(x + 10,(y + Hcadre_candid/2) -2, itoa(k,bufitoa,10));
         outtextxy(x + 11,(y + Hcadre_candid/2) -2, itoa(k,bufitoa,10));
         settextstyle( SMALL_FONT, HORIZ_DIR, 4);
         /* Sep */
         setfillstyle(SOLID_FILL,BLACK);
         bar(x+20,y,x+20,posc[k][3]);

         setfillstyle(SOLID_FILL,7);
         bar((x+Lcadre_candid)-Lsaisie_candid, y,
	        posc[k][1], posc[k][3]);
         Cal_affiche();
         cercle(k);
         fc++;
       }
    }

  nbc_cols = ts  + 1;

  settextstyle(SMALL_FONT, HORIZ_DIR, 6);
  setcolor( WHITE);

  settextjustify(CENTER_TEXT,CENTER_TEXT);

  if ((ts > 0) && ( te < 70))
	{
         x = 639 -( x_dep + Lcadre_candid);
         y = Debut_Affichage;
         j = 1;
         outtextxy(x + (Lcadre_candid + Lsaisie_candid)/2 ,y + ((Hcadre_candid/2) - (Interl_candid + 1)),Msg4);
         outtextxy(x + (Lcadre_candid + Lsaisie_candid)/2 + 1 ,y + ((Hcadre_candid/2) - (Interl_candid + 1)),Msg4);
	 settextstyle( SMALL_FONT, HORIZ_DIR, 4);
	 setcolor( BLACK);
	}
  else
      if((ts > 0) && ( te > 69))
       {
         j = j++;
         x-= Lcadre_candid + Interc_candid;
         outtextxy(x + (Lcadre_candid + Lsaisie_candid)/2,y + ((Hcadre_candid/2) - (Interl_candid + 1)),Msg4);
         outtextxy(x + (Lcadre_candid + Lsaisie_candid)/2 + 1 ,y + ((Hcadre_candid/2) - (Interl_candid + 1)),Msg4);
	 settextstyle( SMALL_FONT, HORIZ_DIR, 4);
	 setcolor( BLACK);
       }

  settextjustify(LEFT_TEXT,CENTER_TEXT);

for(;(k < candid + 1);x+= Lcadre_candid + Interc_candid,y =  0,j = 0)
  for( y += ((Hcadre_candid + Interl_candid)) ;(k < candid + 1) ;y+= Hcadre_candid + Interl_candid,k++,j++)
    {
      if(j == 0) y += Debut_Affichage - (Hcadre_candid + Interl_candid);

      posc[k][0]= x;
      posc[k][1]= (x + Lcadre_candid) - 1;
      posc[k][2]=  y;
      posc[k][3]= (y + Hcadre_candid) - 1;

      setfillstyle(SOLID_FILL,WHITE);
      bar(x,y,posc[k][1],posc[k][3]);

      setfillstyle(SOLID_FILL,7);
      bar((x+Lcadre_candid)-Lsaisie_candid, y,
        posc[k][1], posc[k][3]);

      Cal_affiche();
      cercle(k);

      fc++;
    }

 val = true;
 while(val)
  {
	setfillstyle(SOLID_FILL,BLACK); /* Set to solid fill in color   */
	bar( 0,447,639,479);      /* Draw backgnd */
	setcolor( WHITE);
	settextjustify(CENTER_TEXT,CENTER_TEXT);
	settextstyle( SMALL_FONT, HORIZ_DIR, 6);
    /* Build Msg5 (Informations (bottom of the screen)) */
    if(strstr(Msg5, "%d"))
    {
      if(nbr_max != iNbrCandMax) strcpy(cBuff, "ONVOLLEDIGE");
      else strcpy(cBuff, "VOLLEDIGE");
      strcpy(Msg5bis, Msg5);
      sprintf(Msg5, Msg5bis, nbr_max, iNbrCandMin, iNbrCandMax, cBuff);
    }
	outtextxy( 320,470, Msg5);

	c = getch();

	/*if (c == 'P' ||  c == 'p' || c =='I' || c == 'i')
	  {
		rin.h.ah = 2;
		rin.x.dx = 0;
		int86(0x17,&rin,&rout);
		if(rout.h.ah != 0x90)
		  {
			 setfillstyle(SOLID_FILL,BLACK); / Set to solid fill in color   /
			 bar( 0,438,639,479);      / Draw backgnd /
			 setcolor( WHITE);
			 settextjustify(CENTER_TEXT,CENTER_TEXT);
			 settextstyle(SMALL_FONT, HORIZ_DIR, 6);
			 outtextxy( 320,470, "Imprimante occup‚e, pressez une touche.");
			 c = getch();
		  }
		else
		 {
			int86(0x05,&rin,&rout);
			ptc = fopen("PRN","w");
			bufc[0]  = 0x0C;
			bufc[1] = 0x00;
			fputs(bufc,ptc);
			fclose(ptc);
			val = false;
		 }
	  }
	else*/
	   if (c == 'F' ||  c == 'f')
	     {
		 pcxDisplayFile(bufnom,0,0,639,479,0);
	      }
	   else
		  val = false;
  }
}

/***************************************************************************/
/******                       Calcul de position                     ******/
/***************************************************************************/

Cal_affiche()
{
 int nombre/*, diff3*/;


  nombre = 0;
  if(fc->nom_c[0] != 0x00)    nombre = 1;
  if(fc->prenom_c[0] != 0x00) nombre = 2;
  if(Condense == 1)
  {
   (fc->nom_c[18] = 0x00);
   (fc->prenom_c[18] = 0x00);
  }

   switch(nombre)
   {
	case 1 :
         settextjustify(LEFT_TEXT,CENTER_TEXT);
		 outtextxy(x +22/*1*/,y + giNomY, fc->nom_c);
		 break;
	case 2 :
         settextjustify(LEFT_TEXT,CENTER_TEXT);
		 outtextxy(x +22/*1*/,y + giNomY, fc->nom_c);
    if(!SECOND_LINE_CENTERED)
      outtextxy(x +22/*1*/,y + giPrenomY, fc->prenom_c);
    else
    {
      settextjustify(CENTER_TEXT,CENTER_TEXT);
      outtextxy(x+ 22 + (Lcadre_candid-28) /2,y + giPrenomY, fc->prenom_c);
    }
		 break;
  }
}

/***************************************************************************/
/******          MENU DE CONFIRMATION DU CHOIX DU CANDIDAT + CERCLE   ******/
/***************************************************************************/
cercle(z)
int z;
{
	int xc,yc;
	xc = posc[z][1];

	if(z == 0)
	 {
	  xc -= (posc[z][1] - posc[z][0] ) / 2;
	  yc = 15;
	  settextstyle(SMALL_FONT, HORIZ_DIR, 6);
	  setcolor(8);
	  circle(xc ,yc, 11);
	  setcolor(BLACK);
	  setfillstyle(SOLID_FILL, WHITE);
	  floodfill(xc,yc, 8);
	  settextstyle( SMALL_FONT, HORIZ_DIR, 4);
	 }
	else
	 {
	  xc = xc - Lsaisie_candid/2;
	  yc = posc[z][2] - HAsaisie_candid;
	  settextstyle(SMALL_FONT, HORIZ_DIR, 6);
	  setcolor(8);
	  circle(xc ,yc +(Hcadre_candid / 2), 8);
	  setcolor(BLACK);
	  setfillstyle(SOLID_FILL, WHITE);
	  floodfill(xc,yc +( Hcadre_candid / 2), 8);
	  settextstyle( SMALL_FONT, HORIZ_DIR, 4);
	 }

}

