#include "H\General.H"

/***********************************************************************
 *
 * Ayudas, errores, advertencias y acerca de
 *
 ***********************************************************************/
typedef struct DAyu {
	UInt16 		id;
	Char        tp;
	UInt8       *col;
	UInt32  	p,m;
	FieldType 	*fldP;
	UInt16      f;
} Ayu;

MemHandle mayu; // nada
Ayu		  *ayu;

Int16     frm;

void 	TextoAyuda		(FormType*)							ES2;
void 	PintaTit		(FormType*)							ES2;

Boolean Ayuda(Int16 id, Char tp, UInt8 *col,UInt16 f) {
	mayu=MemHandleNew(sizeof(Ayu));
	ayu=(Ayu *)MemHandleLock(mayu);
    ayu->id=id; ayu->col=col; ayu->tp=tp; ayu->f=f;
    switch(ayu->tp) {
   		case 0: FrmPopupForm(FAcerca); break;
   		case 1: FrmPopupForm(FAyuda); break;
   		case 2: FrmPopupForm(FError); break;
   		case 3: FrmPopupForm(FAdver); break;
    }
    return true;
}
void FinAyuda() { MemHandleUnlock(mayu); MemHandleFree(mayu); }

void TextoAyuda(FormType *f) {
Int16       id;	
MemHandle 	resH;
Char        *c;
UInt16      sp,th,fh;
Int8        aa;
	FrmDrawForm(f);
	id=ayu->id; resH=DmGetResource(strRsc,id); c=MemHandleLock(resH);
	PonTexto(c,90,16,B,C,140,false,-1,-1,&aa);
	MemPtrUnlock(c); DmReleaseResource(resH); 
	id++; ayu->fldP=GetObjPtr(f,100);
   	resH=DmGetResource(strRsc,id); c=MemHandleLock(resH);
	FldInsert(ayu->fldP,c,StrLen(c));
	FldSetDirty(ayu->fldP,false);
	MemPtrUnlock(c); DmReleaseResource(resH);
	FldGetScrollValues(ayu->fldP,&sp,&th,&fh); 
	ayu->m=((fh>th)?0:th-fh); ayu->p=0;
	CtlHideControl(GetObjPtr(f,BSub));
	if (ayu->m<=0) CtlHideControl(GetObjPtr(f,BBaj));
	FldSetUsable(ayu->fldP,false);
	FldGetScrollValues(ayu->fldP,&sp,&th,&fh); 
	FldSetScrollPosition(ayu->fldP,0);
	//FldDrawField(ayu->fldP);
}
void PintaTit(FormType *f) {
UInt16 			x,y,w,h;
MemHandle		resH;
BitmapPtr		resP;
Char            ss[50];
Int8        	a;
	FrmDrawForm(FrmGetActiveForm());
 	x=2; y=13; w=152; h=37;
	PintaB(255,x,y,w,h,4,9,3,9,true,ayu->col);
	resH = DmGetResource(bitmapRsc,105);
	resP = MemHandleLock(resH);
	WinDrawBitmap (resP, x+5, y+1);
	MemPtrUnlock(resP);
	DmReleaseResource(resH);
	SacaTexto(ss,1000,0,50);
	y+=5; y+=1+PonTexto(ss,95,y,'L','c',0,false,ayu->col[3],ayu->col[4],&a);
	SacaTexto(ss,1000,1,50); StrCat(ss,VERs); 
	PonTexto(ss,95,y,'n','c',0,false,ayu->col[10],ayu->col[4],&a);
}
Boolean AyudaError(EventType* pEvent) {
UInt16 id;
	switch (pEvent->eType) {
		case frmOpenEvent:
		    if (ayu->tp!=0) TextoAyuda(FrmGetActiveForm());
			else            PintaTit(FrmGetActiveForm());
			return true;
	    case ctlRepeatEvent:
	        id=pEvent->data.ctlRepeat.controlID; 
	    case ctlEnterEvent:
	        if (pEvent->eType==ctlEnterEvent) id=pEvent->data.ctlEnter.controlID;
		    switch(id) {
			    case BSub: if (ayu->p==0) break;
			    	       ayu->p--; FldScrollField(ayu->fldP,1,winUp);
                           if (ayu->p==0) CtlHideControl(GetObjPtr(FrmGetActiveForm(),BSub));
			    	       if (ayu->p==ayu->m-1) CtlShowControl(GetObjPtr(FrmGetActiveForm(),BBaj));
			    	       return true;
			    case BBaj: if (ayu->p==ayu->m) break;
			    	       ayu->p++; FldScrollField(ayu->fldP,1,winDown);
                           if (ayu->p==ayu->m) CtlHideControl(GetObjPtr(FrmGetActiveForm(),BBaj));
       					   if (ayu->p==1) CtlShowControl(GetObjPtr(FrmGetActiveForm(),BSub));
			    	       return true;
		    }
	        break;
		case ctlSelectEvent:
		    switch(pEvent->data.ctlSelect.controlID) { 
			    case BAce: id=ayu->f; FinAyuda(); 
			    		   FrmUpdateForm(id,90); FrmReturnToForm(0);
			    		   return true;
		    }
		    break;
		default: break;
	}
	return false;
}