#include "xtext.h"

int              CharHeightASCII;
int              CharWidthASCII;
int              CharHeightASCIITINY;
int              CharWidthASCIITINY;
int              CharHeightASCIIHOR;
int              CharWidthASCIIHOR;
int              CharHeightKANJI;
int              CharWidthKANJI;
int              CharHeightKANJITINY;
int              CharWidthKANJITINY;

void xchar::set(ulong   Clr,int BID,fontType FType,
		int X,int Y,uchar Byte1,uchar Byte2)
{
  Color=Clr;ButtonID=BID;
  FontType=FType;XY[0]=X;XY[1]=Y;
  if(Byte1==0x23 && Byte2>=0x30 && Byte2 <=0x39)
    {
      switch(Byte2)
	{
	case 0x30: Byte1=0x21;Byte2=0x3b;break;
	case 0x31: Byte1=0x30;Byte2=0x6c;break;
	case 0x32: Byte1=0x46;Byte2=0x73;break;
	case 0x33: Byte1=0x3b;Byte2=0x30;break;
	case 0x34: Byte1=0x3b;Byte2=0x4d;break;
	case 0x35: Byte1=0x38;Byte2=0x5e;break;
	case 0x36: Byte1=0x4f;Byte2=0x3b;break;
	case 0x37: Byte1=0x3c;Byte2=0x37;break;
	case 0x38: Byte1=0x48;Byte2=0x2c;break;
	case 0x39: Byte1=0x36;Byte2=0x65;break;
	}
    }
  Code[0]=Byte1;Code[1]=Byte2;
}

xchar*  textPage::XCharListWork=NULL;

inline  int textPage::buttonID(xTextWindow* XTWin,int X,int Y)
{
  int CharHeightASCII    =XTWin->CharHeightASCII;
  int CharWidthASCII     =XTWin->CharWidthASCII;
  int CharHeightASCIITINY=XTWin->CharHeightASCIITINY;
  int CharWidthASCIITINY =XTWin->CharWidthASCIITINY;
  int CharHeightASCIIHOR =XTWin->CharHeightASCIIHOR;
  int CharWidthASCIIHOR  =XTWin->CharWidthASCIIHOR;
  int CharHeightKANJI    =XTWin->CharHeightKANJI;
  int CharWidthKANJI     =XTWin->CharWidthKANJI;
  int CharHeightKANJITINY=XTWin->CharHeightKANJITINY;
  int CharWidthKANJITINY =XTWin->CharWidthKANJITINY;
  int Xc,Yc;
  for(int I=0;I<CharCnt;++I)
    {
      Xc=XCharList[I].x();Yc=XCharList[I].y();
      switch(XCharList[I].type())
	{
	case CH_ASCII:
	  if(Xc<=X&&X<=Xc+CharWidthASCII&&Yc<=Y&&Y<=Yc+CharHeightASCII)
	    return XCharList[I].buttonID();
	  break;
	case CH_ASCII_TINY:
	  if(Xc<=X&&X<=Xc+CharWidthASCIITINY&&Yc<=Y&&Y<=Yc+CharHeightASCIITINY)
	    return XCharList[I].buttonID();
	  break;
	case CH_ASCII_HOR:
	  if(Xc<=X&&X<=Xc+CharWidthASCIIHOR&&Yc<=Y&&Y<=Yc+CharHeightASCIIHOR)
	    return XCharList[I].buttonID();
	  break;
	case CH_KANJI:
	  if(Xc<=X&&X<=Xc+CharWidthKANJI&&Yc<=Y&&Y<=Yc+CharHeightKANJI)
	    return XCharList[I].buttonID();
	  break;
	case CH_KANJI_TINY:
	  if(Xc<=X&&X<=Xc+CharWidthKANJITINY&&Yc<=Y&&Y<=Yc+CharHeightKANJITINY)
	    return XCharList[I].buttonID();
	  break;
	}
    }
  return 0;
}

xTextWindow::xTextWindow(int Width,int Height,
			 char* FG,char* SG,char* BG,
			 char* FNA,char* FNAT,
			 char* FNK,char* FNKT)
  : xwindow(Width,Height,FG,BG)
{
  FontA    =new fontA(FNA); FontATiny=new fontA(FNAT);
  FontK    =new fontK(FNK); FontKTiny=new fontK(FNKT);
  ShadowColor=colorID(SG);ShadowFlag =0;
  TextPageFirst=TextPageLast=TextPageToShow=TextPageToWrite=new textPage;
  DrawTextFlag=1;
  CharHeightASCII    =FontA    ->fontWidth ();
  CharWidthASCII     =FontA    ->fontHeight();
  CharHeightASCIITINY=FontATiny->fontWidth ();
  CharWidthASCIITINY =FontATiny->fontHeight();
  CharHeightASCIIHOR =FontA    ->fontHeight();
  CharWidthASCIIHOR  =FontA    ->fontWidth ();
  CharHeightKANJI    =FontK    ->fontHeight();
  CharWidthKANJI     =FontK    ->fontWidth ();
  CharHeightKANJITINY=FontKTiny->fontHeight();
  CharWidthKANJITINY =FontKTiny->fontWidth ();
  mapRaised();
}

void xTextWindow::drawChar(xchar XChar)
{
  switch(XChar.type())
    {
    case CH_ASCII:
      if(ShadowFlag)
	{
	  setForeground(ShadowColor);
	  FontA->drawChar(TheWindow,TheGC,XChar.byte1(),
			  XChar.x()+1,XChar.y()+1);
	}
      setForeground(XChar.color());
      FontA->drawChar(TheWindow,TheGC,XChar.byte1(),XChar.x(),XChar.y());
      break;
    case CH_ASCII_TINY:
      if(ShadowFlag)
	{
	  setForeground(ShadowColor);
	  FontATiny->drawChar(TheWindow,TheGC,XChar.byte1(),
			      XChar.x()+1,XChar.y()+1);
	}
      setForeground(XChar.color());
      FontATiny->drawChar(TheWindow,TheGC,XChar.byte1(),XChar.x(),XChar.y());
      break;
    case CH_ASCII_HOR:
      if(ShadowFlag)
	{
	  setForeground(ShadowColor);
	  FontA->drawHorChar(TheWindow,TheGC,XChar.byte1(),
			     XChar.x()+1,XChar.y()+1);
	}
      setForeground(XChar.color());
      FontA->drawHorChar(TheWindow,TheGC,XChar.byte1(),XChar.x(),XChar.y());
      break;
    case CH_KANJI:
      if(ShadowFlag)
	{
	  setForeground(ShadowColor);
	  FontK->drawChar(TheWindow,TheGC,XChar.byte1(),XChar.byte2(),
			  XChar.x()+1,XChar.y()+1);
	}
      setForeground(XChar.color());
      FontK->drawChar(TheWindow,TheGC,XChar.byte1(),XChar.byte2(),
		      XChar.x(),XChar.y());
      break;
    case CH_KANJI_TINY:
      if(ShadowFlag)
	{
	  setForeground(ShadowColor);
	  FontKTiny->drawChar(TheWindow,TheGC,XChar.byte1(),XChar.byte2(),
			      XChar.x()+1,XChar.y()+1);
	}
      setForeground(XChar.color());
      FontKTiny->drawChar(TheWindow,TheGC,XChar.byte1(),XChar.byte2(),
			  XChar.x(),XChar.y());
      break;
    }
}

int xTextWindow::event(void)
{
  static Region Reg=NULL;
  XEvent TheEvent; KeySym TheKey;int K; char KeyText[11];
  int MX;int MY;int ButtonID;int Xc;int Yc;
  while(1)
    {
      XNextEvent(TheDisplay,&TheEvent);
      switch(TheEvent.type)
        {
	case Expose:
// 樫山 周平 (Syuuhei KASHIYAMA) 様に修正していただいたところ。
// ここから、
	  XRectangle Rect;
	  if(Reg == NULL) Reg = XCreateRegion();
	  Rect.x=TheEvent.xexpose.x; Rect.y=TheEvent.xexpose.y;
	  Rect.width =TheEvent.xexpose.width;
	  Rect.height=TheEvent.xexpose.height;
	  XUnionRectWithRegion(&Rect, Reg, Reg);
	  if(TheEvent.xexpose.count > 0) break;
	  XSetRegion(TheDisplay, TheGC, Reg);
	  drawText(False);
	  XDestroyRegion(Reg); Reg=XCreateRegion();Rect.x=Rect.y=0;
	  Rect.width=TheHints.width; Rect.height=TheHints.height;
	  XUnionRectWithRegion(&Rect, Reg, Reg);
	  XSetRegion(TheDisplay, TheGC, Reg);
	  XDestroyRegion(Reg); Reg=NULL;
	  break;
// ここまで。
	case ButtonPress:
	  if(TextPageToWrite==TextPageToShow&&DrawTextFlag)
	    {
	      MX=TheEvent.xbutton.x;MY=TheEvent.xbutton.y;
	      return TextPageToWrite->buttonID(this,MX,MY);
	    }
	  break;
        case MappingNotify:
	  XRefreshKeyboardMapping(&TheEvent.xmapping);return 0;
        case KeyPress:
          K=XLookupString(&TheEvent.xkey,KeyText,10,&TheKey,0);
	  if(K==1)
	    {
	      switch(KeyText[0])
		{
		case 'q': return -1;
		case 'p':
		  if(TextPageToShow->prior()!=NULL)
		    { TextPageToShow=TextPageToShow->prior();drawText();}
		  break;
		case 'n':
		  if(TextPageToShow!=TextPageToWrite)
		    { TextPageToShow=TextPageToShow-> next();drawText();}
		  else if(DrawTextFlag) return 0;
		  break;
		case 't':
		  if(TextPageToShow->prior()!=NULL)
		    { TextPageToShow=TextPageFirst;          drawText();}
		  break;
		case 'e':
		  if(TextPageToShow!=TextPageToWrite)
		    { TextPageToShow=TextPageToWrite;        drawText();}
		  else if(DrawTextFlag) return 0;
		  break;
		case ' ':
		  DrawTextFlag=DrawTextFlag==1?0:1;drawText();
		  break;
		default:
		  if(TextPageToShow==TextPageToWrite&&DrawTextFlag) return 0;
		  break;
		}
	    }
// 樫山 周平 (Syuuhei KASHIYAMA)様にデバッグしていただいたところ。
// ここから、
	  else if(TextPageToShow==TextPageToWrite&&DrawTextFlag) return 0;
// ここまで。
        }
    }
}

