#include "defines.h"
/*     Bed a menu-driven multi dataformat binary editor for Linux            */
/*     Copyright (C) 1998 Jaap Korthals Altes <jkaltes@hetnet.nl>       */
/*                                                                           */
/*     This program is free software; you can redistribute it and/or modify  */
/*     it under the terms of the GNU General Public License as published by  */
/*     the Free Software Foundation; either version 2 of the License, or     */
/*     (at your option) any later version.                                   */
/*                                                                           */
/*     This program is distributed in the hope that it will be useful,       */
/*     but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/*     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
/*     GNU General Public License for more details.                          */
/*                                                                           */
/*     You should have received a copy of the GNU General Public License     */
/*     along with this program; if not, write to the Free Software           */
/*     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             */
/* Fri Dec  8 22:14:27 2000                                                  */
#include "desturbe.h"
#include <stdarg.h>
#include "editor.h"
#include "inout.h"
#include "readcolors.h"
#include "main.h"
#include "screen.h"
#include "offtypes.h"
#include "system.h"

extern char messagebuf[];
static const int maxmessagebuf=200;
char messagebuf[maxmessagebuf]="";
 void Editor::clearmessage(void) { messagebuf[0]=0; if(nomessage) textovermessage();else fileinfo(); }
 int Editor::output(const char info[]) {
	strcpyn(messagebuf,info,maxmessagebuf);
	return realoutput(messagebuf);
	}
 int Editor::realoutput(const char info[]) {
	INCDEST();
	editmove(MESSAGELINE,0);
	COMMANDON;
	overline() ;
	editmove(MESSAGELINE,0);
	waddnstr(editscreen,(char *)info,Screenwidth);
	COMMANDOFF;
	ismessage=1;
	DECDEST();
	return 0;
	}


extern char showfileinfo;
 int Editor::showmessage(void) {
	if(messagebuf[0]!=0&&!showfileinfo) {
		realoutput(messagebuf);	
		}
	else {
		fileinfo();
		showfileinfo=0;
		}
	messagebuf[0]=0;
	return 0;
	}
#include "screenpart.h"
 int Editor::showposition(void) {
	if(nomessage)
		return 0;
	INCDEST();
	COMMANDON;
	int start=Screenwidth-maxnum-1; 
	int bytes=parts[mode]->bytes;
	wmove(editscreen,MESSAGELINE,start); 
	waddch(editscreen, ' ');
	wprintw(editscreen,numstr ,((editpos/bytes)*bytes)+filepos);
	COMMANDOFF;
	DECDEST();
	return 0;
	}
	
 int Editor::message(char *format ...) {
	va_list ap;
	char str[255];
	va_start(ap,format);
	vsnprintf( str,255, format,  ap);
	va_end(ap);
	return output(str);
	}
int Editor::getinput(const char question[],char *answer) {
	answer[0]='\0';
	if(getanswer(question,answer,80)<0) {
		beep();return -1;
		}
	return 0;
	}
/*
int Editor::getinput(const char question[],char *answer) {
	while(1) {
		COMMANDON;
		editmove(MESSAGELINE,0);
		overline() ;
		editmove(MESSAGELINE,0);
		waddnstr(editscreen,question,Screenwidth-1);
		echoon();
		SHOWCURSOR;
		wrefresh(editscreen);
		waitinput=1;
		if((mysigsetjmp(resizejmp,2))) {

			resized();
			rewriteall();
			waitinput=0;
			continue;
			};
		break;
		}
	mywgetstr(editscreen,answer);
	HIDECURSOR;
	waitinput=0;
	ismessage=1;
	COMMANDOFF;
	noecho();
	clearmessage();
	wrefresh(editscreen);
	return 0;
	}
*/
int Editor::inputget(char *answer,const char *format,...) {
	char question[255];
	va_list ap;
	va_start(ap,format);
	vsnprintf( question, 255,format,  ap);
	va_end(ap);
	answer[0]='\0';
	if(getanswer(question,answer,255)<0) {
		beep();return -1;
		}
	return 0;
	}
/*
	while(1) {
		COMMANDON;
		editmove(MESSAGELINE,0);
		overline() ;
		editmove(MESSAGELINE,0);
		waddnstr(editscreen,question,Screenwidth-1);
		echoon();
		SHOWCURSOR;
		wrefresh(editscreen);
		waitinput=1;
		if(!(mysigsetjmp(resizejmp,3))) {
			break;
			};
		waitinput=0;
		resized();
		rewriteall();
		};
	mywgetstr(editscreen,answer);
	HIDECURSOR;
	waitinput=0;
	ismessage=1;
	COMMANDOFF;
	noecho();
	clearmessage();
	wrefresh(editscreen);
*/

 int Editor::getkeyinput(char *format ...) {
	int key;
	va_list ap;
	char str[255];
	va_start(ap,format);
	vsnprintf( str,255, format,  ap);
	va_end(ap);
	while(1) {
		realoutput(str);
		key= getch();
		if(key!=RESIZE) {
			return key;
			}
		resized();
		rewriteall();
		}
	}

#define kgetch() ggetch(stdscr)
extern int ggetch(WINDOW *w) ;


extern bool mykbhit(void) ;
int getkeys(unsigned char *keys,int maxkeys) {
	int i;
	keys[0]=kgetch();
	for(i=1;i<maxkeys;i++) {
		keys[i]=(unsigned char)ERR;
		for(int j=0;j<2;j++) {
			if(mykbhit()) {
				 keys[i]=kgetch();
				break;
				}
			usleep(10000);
			}
		if(keys[i]==(unsigned char)ERR) {
			flushinp();
			return i;
			}
		}
	flushinp();
	return i;
	}
