/*

        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	action id
					ALL_QUIT  : all end
					SAVE_QUIT  : log save & quit
					TITLE_MODE  : title mode switch
					NEW_FILE  : new file read
					MODE_CHG  : 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"
#include	"textdsp.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"};
	char	*bbstitle={"ＢＢＳ　ｍｅｓｓａｇｅ"};
	char	*mailtitle={"ＭＡＩＬ　ｔｅｘｔ"};
	int		i,j,k,line,startp,leng,textleng,next_pos;
	int		dispstartline,dispendline;
	int		disp_skip_flag;
	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=-99;

	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);

			switch (intext->text[nowtext].text.id){
				case NOTITLE_TEXT_ID :
					linedspv(0,32,nontitle,1,TITLE_COL,TITLEBACK_COL);
					break;
				case MAIL_ID :
					linedspv(0,32,mailtitle,1,TITLE_COL,TITLEBACK_COL);
					break;
				case BBSMES_ID :
					linedspv(0,32,bbstitle,1,TITLE_COL,TITLEBACK_COL);
					break;
				default :
					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){

			/*	display skip charactor check */
				disp_skip_flag=0;
				if (leng<40){
					/* '続き (改行で･･･ ' check -- 2 lines skip(＞) */
					for (j=0;j<strlen(ptnchar[0]);j++)
						if ((char)text[startp+j]!=ptnchar[0][j]) break;
					if (j==strlen(ptnchar[0])){
						if (text[startp+next_pos]==0x81
								&& text[startp+next_pos+1]==0x84){
							for (j=0;j<6;j++)
								if (text[startp+next_pos+2+j]==0x0d
										&& text[startp+next_pos+3+j]==0x0a)
												 break;

/*	printf("続きを見つけた後の処理 ＞を探して j=%d \n",j);	*/

							if (j<6){
								startp=startp+next_pos+4+j;
								disp_skip_flag=1;
							}
						}
					}
					/*	'more>' check -- 1 line skip */
					if (disp_skip_flag==0){
						for (j=0;j<strlen(ptnchar[1]);j++)
							if ((char)text[startp+j]!=ptnchar[1][j]) break;
						if (j==strlen(ptnchar[1])){
							startp=startp+next_pos;
							disp_skip_flag=1;
						}
					}
				}

				if (disp_skip_flag==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 ALL_QUIT :	/* all end */
					endcode=ALL_QUIT;
					textdispflag = 0 ;
					contflag=0;
					break ;
				case NEXT_PAGE :	/* 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 PREV_PAGE :	/* 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 NEXT_TEXT :	/* 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 PREV_TEXT :	/* 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 NEXT_TITLE :	/* 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 PREV_TITLE :	/* 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 TOP_TEXT :	/* top text */
					*textno=0;
					back=0;
					textdispflag = 0 ;
					break ;
				case LAST_TEXT :	/* last text */
					*textno=max_text-1;
					back=0;
					textdispflag = 0 ;
					break ;
				case SAVE_QUIT :	/* log save & quit */
					endcode = SAVE_QUIT;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case TITLE_MODE :	/* title mode switch */
					endcode = TITLE_MODE;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case NEW_FILE :	/* new file select */
					endcode = NEW_FILE;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case MODE_CHG :	/* mode change */
					endcode = MODE_CHG;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case CONSOLE :	/* console */
					endcode = CONSOLE;
					contflag=0;
					textdispflag = 0 ;
					break ;
				case CONF_TITLE :	/* conf title mode */
					endcode = CONF_TITLE;
					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	action id
					ALL_QUIT  : all end
					NEXT_PAGE  : next page
					PREV_PAGE  : previous page
					NEXT_TEXT  : next text
					PREV_TEXT  : previous text
					NEXT_TITLE  : next title
					PREV_TITLE  : previous title
					TOP_TEXT  : top text
					LAST_TEXT  : last text
					SAVE_QUIT  : log save & quit
					TITLE_MODE  : title mode switch
					NEW_FILE  : new file select
					MODE_CHG  : mode change

*/

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=PREV_PAGE;
				contflag=0;
				break;
			case 2 :			/* left bottan  */
				endcode=NEXT_PAGE;
				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=NEXT_PAGE;
				contflag=0;
				break;
			case 3 :			/* up key   */
				endcode=PREV_PAGE;
				contflag=0;
				break;
			case 4 :			/* right key */
				endcode=NEXT_TEXT;
				contflag=0;
				break;
			case 5 :			/* left key  */
				endcode=PREV_TEXT;
				contflag=0;
				break;
			case 6 :			/* CR key    */
				endcode=NEXT_PAGE;
				contflag=0;
				break;
			case 7 :			/* 取消 key  */
				endcode=PREV_PAGE;
				contflag=0;
				break;
			default : break;
		}

					/* joy pad status check */
		switch (pad_in()){
			case 0 : break;		/* no input  */
			case 1 :			/* A trigger */
				endcode=NEXT_PAGE;
				contflag=0;
				break;
			case 2 :			/* B trigger */
				endcode=PREV_PAGE;
				contflag=0;
				break;
			case 3 :			/* down key  */
				endcode=NEXT_PAGE;
				contflag=0;
				break;
			case 4 :			/* up key    */
				endcode=PREV_PAGE;
				contflag=0;
				break;
			case 5 :			/* right key */
				endcode=NEXT_TEXT;
				contflag=0;
				break;
			case 6 :			/* left key   */
				endcode=PREV_TEXT;
				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 );
}

