/*

        text display for 16 color graphic mode

                      1993.6.27 v1.0
                      copyright Y.Ouchi

			input	*log    : log data
					logleng    : log length
					**title : title data
					*intext : log analize result
					*textno : display text no

			output	0  : all end
					1  : log save & quit
					2  : title mode switch
					3  : new file read
					4  : mode change

*/

#include	<stdio.h>
#include	<string.h>
#include	<stdlib.h>
#include	<jctype.h>
#include	<time.h>
#include	"egb.h"
#include	"mos.h"
#include	"snd.h"
#include	"bb.h"

	/* 関数プロトタイプ宣言 */
int		nextchek( );

extern	char	egbwork[];
extern	char	moswork[];
extern	void	linedsp(int, int, char *);
extern	void	linedspv(int, int, char *, int, unsigned int, unsigned int);
extern	void	linedspvl(int,int,char *,int,int,unsigned int,unsigned int);
extern	void	box(int, int, int, int, int, int);
extern	int		menuread(void);
extern	int		mouse_in(int *, int *);
extern	int		key_in(void);
extern	int		pad_in(void);
extern	int		crfindb(int, char *, int *, int *);

extern	int		max_text;
extern	int		dispmode;

int textdsp(char *log,int logleng,char **title,
				struct logcontent *intext,short *disptext,int *textno)
{
	char	*nontitle={"no title"};
	int		i,j,k,line,startp,leng,textleng,next_pos;
	int		dispstartline,dispendline;
	int		overlapline=1;
	int		texthead;
	struct	dspcont{
				short	pos;
				short	leng;
			}	dsptext[500];
	int		endcode=0;
	int		contflag=1,textdispflag;
	int		dispmakeflag;
	int		back=0;
	int		nowtext;
	unsigned	char	*text;
	char	footer[90]={"      lines       bytes"};
	int		titleno;

	titleno=-2;

	while ( contflag ){

					/* first display text search			*/
		for (i=*textno;i<max_text;i++){
			if(intext->text[disptext[i]].text.id!=TITLE_ID) break;
		}
		*textno=i;
		nowtext=disptext[i];
		texthead=intext->text[nowtext].text.pos;

					/* title display on header			*/
		if (titleno!=intext->text[nowtext].text.title_index){
			EGB_writePage(egbwork,1);
			box(0,16,639,34,TITLEBACK_COL,1);
			EGB_color(egbwork, 0, TITLE_COL);
			if (intext->text[nowtext].text.id == NOTITLE_TEXT_ID){
				linedspv(0,32,nontitle,1,TITLE_COL,TITLEBACK_COL);
			}
			else{
				if (intext->text[nowtext].text.title_index>=0){
					linedspv(0,32,
							title[intext->text[nowtext].text.title_index],
							1,TITLE_COL,TITLEBACK_COL);
				}
				else {
					linedspv(0,32,nontitle,1,TITLE_COL,TITLEBACK_COL);
				}
			}
			EGB_color(egbwork, 0, TEXT_COL);
			EGB_writePage(egbwork,0);
			titleno=intext->text[nowtext].text.title_index;
		}

					/* text display */
						/* display data make */
		text=(unsigned char *)&log[(int)intext->text[nowtext].text.pos];
		textleng=intext->text[nowtext].text.leng;
		startp=leng=line=0;

		dispmakeflag=1;
		while(dispmakeflag){
			i=crfindb(textleng-startp,(char *)&text[startp],&leng,&next_pos);
			if (i>0){
				if (leng>80){
					do {
						k=0;
						while(k<80){
							if (iskanji(text[startp+k])) k=k+2;
							else k=k+1;
						}
						if (k==81) k=79;
						dsptext[line].pos=startp;
						dsptext[line].leng=k;
						leng=leng-k;
						startp=startp+k;
						next_pos=next_pos-k;
						line=line+1;
						if (line>500) break;
					} while(leng>80);
				}
				if (line>500) break;
				dsptext[line].pos=startp;
				dsptext[line].leng=leng;
				line=line+1;
				if (line>500) break;
				if (leng==80){
					dsptext[line].pos=0x00;
					dsptext[line].leng=0x00;
					line=line+1;
					if (line>500) break;
				}
				for(j=1;j<i;j++){
					dsptext[line].pos=0x00;
					dsptext[line].leng=0x00;
					line=line+1;
					if (line>500) break;
				}
				if (next_pos<=0) dispmakeflag=0;
				else startp=startp+next_pos;
			}
			else{
				if (leng>0){
					dsptext[line].pos=startp;
					dsptext[line].leng=leng;
					line=line+1;
				}
				dispmakeflag=0;
			}
		}
		for(i=line-1;i>0;i--) if (dsptext[i].leng!=0x00) break;
		line=i+1;

						/* footer write (line no & size) */
		j=line;
		for(i=4;i>0;i--){
			if (j>0){
				footer[i]=j%10+0x30;
				j=j/10;
			}
			else footer[i]=0x20;
		}
		j=(int)intext->text[nowtext].text.leng;
		for(i=16;i>11;i--){
			if (j>0){
				footer[i]=j%10+0x30;
				j=j/10;
			}
			else footer[i]=0x20;
		}
		EGB_writePage(egbwork,1);
		EGB_color(egbwork, 0, TITLE_COL);
		linedsp(0,477,footer);
		EGB_writePage(egbwork,0);
		EGB_color(egbwork, 0, TEXT_COL);

						/* text display */
		dispstartline=0;
		if (back==1){
			while (dispstartline+TEXT_LINE_NO<line){
				dispstartline=dispstartline+TEXT_LINE_NO-overlapline;
			}
		}
		textdispflag=1;
		while ( textdispflag ){
			EGB_clearScreen(egbwork);
			dispendline = dispstartline + TEXT_LINE_NO ;
			if (dispendline > line) dispendline=line;
			for (i=0;i<dispendline-dispstartline;i++){
				linedspvl(0,i*17+51,
							&(log[texthead+dsptext[dispstartline+i].pos]),
							dsptext[dispstartline+i].leng,0,TEXT_COL,BACK_COL);
			}
			switch ( nextchek() ){
				case 0 :	/* all end */
					endcode=0;
					textdispflag = 0 ;
					contflag=0;
					break ;
				case 1 :	/* next page */
					if (dispstartline+TEXT_LINE_NO >= line) {
						for (i=*textno+1;i<max_text;i++){
							if (intext->text[disptext[i]].text.id!=TITLE_ID)
											break;
						}
						if (i < max_text){
							*textno=i;
							back=0;
						}
						else back=1;
						textdispflag = 0 ;
					}
					else dispstartline=dispstartline+TEXT_LINE_NO-overlapline;
					break ;
				case 2 :	/* previous page */
					if (dispstartline-TEXT_LINE_NO+overlapline < 0){
						for (i=*textno-1;i>=0;i--){
							if (intext->text[disptext[i]].text.id!=TITLE_ID)
										break;
						}
						if (i>=0){
							*textno=i;
							back=1;
						}
						else back=0;
						textdispflag = 0 ;
					}
					else dispstartline=dispstartline-TEXT_LINE_NO+overlapline;
					break ;
				case 3 :	/* next text */
					for (i=*textno+1;i<max_text;i++){
						if (intext->text[disptext[i]].text.id!=TITLE_ID) break;
					}
					if (i < max_text){
						*textno=i;
						back=0;
					}
					textdispflag = 0 ;
					break ;
				case 4 :	/* previous text */
					for (i=*textno-1;i>=0;i--){
						if (intext->text[disptext[i]].text.id!=TITLE_ID) break;
					}
					if (i>=0){
						*textno=i;
					}
					back=0;
					textdispflag = 0 ;
					break ;
				case 5 :	/* next title */
					for (i=*textno+1;i<max_text-1;i++){
						if (intext->text[disptext[i]].text.id==TITLE_ID) break;
					}
					for (j=i+1;j<max_text;j++){
						if (intext->text[disptext[j]].text.id!=TITLE_ID){
							*textno=j;
							break;
						}
					}
					back=0;
					textdispflag = 0 ;
					break ;
				case 6 :	/* previous title */
					for (i=*textno-2;i>=0;i--){
						if (intext->text[disptext[i]].text.id==TITLE_ID) break;
					}
					for (j=i+1;j<max_text;j++){
						if (intext->text[disptext[j]].text.id!=TITLE_ID){
							*textno=j;
							break;
						}
					}
					back=0;
					textdispflag = 0 ;
					break ;
				case 7 :	/* top text */
					*textno=0;
					back=0;
					textdispflag = 0 ;
					break ;
				case 8 :	/* last text */
					*textno=max_text-1;
					back=0;
					textdispflag = 0 ;
					break ;
				case 9 :	/* log save & quit */
					endcode = 1;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case 10 :	/* title mode switch */
					endcode = 2;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case 11 :	/* new file select */
					endcode = 3;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case 12 :	/* mode change */
					endcode = 4;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case 13 :	/* console */
					endcode = 5;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case 14 :	/* conf title mode */
					endcode = 6;
					contflag=0;
					textdispflag = 0 ;
					break ;
				default :
					break ;
			}
		}
		EGB_writePage(egbwork,1);
		box(0,462,190,479,TITLEBACK_COL,1);
		EGB_writePage(egbwork,0);
		EGB_color(egbwork, 0, TEXT_COL);

	}

	return(endcode);

}


/*
        next action check
                      1993.4.10 v.00
                      1993.4.17 v.01	menu made
                      1993.4.24 v.02	pad add
                      copyright Y.Ouchi

			input	non

			output	0  : all end
					1  : next page
					2  : previous page
					3  : next text
					4  : previous text
					5  : next title
					6  : previous title
					7  : top text
					8  : last text
					9  : log save & quit
					10  : title mode switch
					11  : new file select
					12  : mode change

					0 & 4-12 menuread return
*/

int nextchek( )
{
	int		i,x,y;
	int		endcode=0;
	time_t	now;
	char	nowchar[30];
	char	oldmin=0x00;
	int		contflag;

	contflag=1;
	while ( contflag ) {

					/* mouse status check */
		switch (mouse_in(&x,&y)){
			case 0 : break;		/* no input */
			case 1 :			/* right bottan */
				endcode=2;
				contflag=0;
				break;
			case 2 :			/* left bottan  */
				endcode=1;
				contflag=0;
				break;
			case 3 :			/* both bottan  */
				endcode=menuread( );
				contflag=0;
				break;
			default : break;
		}

					/* key board status check */
		switch (key_in()){
			case 0 : break;		/* no input */
			case 1 :			/* ESC key  */
				endcode=menuread( );
				contflag=0;
				break;
			case 2 :			/* down key */
				endcode=1;
				contflag=0;
				break;
			case 3 :			/* up key   */
				endcode=2;
				contflag=0;
				break;
			case 4 :			/* right key */
				endcode=3;
				contflag=0;
				break;
			case 5 :			/* left key  */
				endcode=4;
				contflag=0;
				break;
			case 6 :			/* CR key    */
				endcode=1;
				contflag=0;
				break;
			case 7 :			/* 取消 key  */
				endcode=2;
				contflag=0;
				break;
			default : break;
		}

					/* joy pad status check */
		switch (pad_in()){
			case 0 : break;		/* no input  */
			case 1 :			/* A trigger */
				endcode=1;
				contflag=0;
				break;
			case 2 :			/* B trigger */
				endcode=2;
				contflag=0;
				break;
			case 3 :			/* down key  */
				endcode=1;
				contflag=0;
				break;
			case 4 :			/* up key    */
				endcode=2;
				contflag=0;
				break;
			case 5 :			/* right key */
				endcode=3;
				contflag=0;
				break;
			case 6 :			/* left key   */
				endcode=4;
				contflag=0;
				break;
			case 7 :			/* RUN key    */
				endcode=menuread( );
				contflag=0;
				break;
			case 8 :			/* SELECT key */
				endcode=menuread( );
				contflag=0;
				break;
			default : break;
		}

		now=time(NULL);
		strncpy(nowchar,ctime(&now),24);
		for(i=16;i<21;i++) nowchar[i]=nowchar[i+3];
		nowchar[21]=0x00;
		if (oldmin!=nowchar[15]){
			EGB_writePage(egbwork,1);
			box(460,462,639,479,TITLEBACK_COL,1);
			EGB_color(egbwork, 0, TIME_COL);
			linedsp(460,478,nowchar);
			EGB_writePage(egbwork,0);
			EGB_color(egbwork, 0, TEXT_COL);
			oldmin=nowchar[15];
		}

	}
	EGB_color(egbwork, 0, TEXT_COL);
	EGB_color(egbwork, 1, BACK_COL);
	return ( endcode );
}

