/*	Copyright  (c)	Günter Woigk 2016 - 2016
					mailto:kio@little-bat.de

	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.

	Permission to use, copy, modify, distribute, and sell this software and
	its documentation for any purpose is hereby granted without fee, provided
	that the above copyright notice appear in all copies and that both that
	copyright notice and this permission notice appear in supporting
	documentation, and that the name of the copyright holder not be used
	in advertising or publicity pertaining to distribution of the software
	without specific, written prior permission.  The copyright holder makes no
	representations about the suitability of this software for any purpose.
	It is provided "as is" without express or implied warranty.

	THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
	INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
	EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
	DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
	PERFORMANCE OF THIS SOFTWARE.
*/


#ifndef TTYPIXMAP_H
#define TTYPIXMAP_H
#include "kio/kio.h"
#include "Pixmap.h"
#include <QPainter>



extern QFont  default_tty_font;
extern QRgb   default_tty_paper;
extern QRgb   default_tty_pen;



// =============================================================
//				Pixmap for TTYPainter
//				adds TTY state to the Pixmap
// =============================================================


class TTYPixmap : public Pixmap
{
public:
	// const values calculated by creator:
	int 	rows, cols;				// size of terminal in character rows and columns
	int 	char_width;				// width and ..
	int		line_height;			// .. height of character cell
	int		pointsize;				// nominal font size [points]
	int		ascent,descent,char_height,leading;	// char_height = ascent+descent+1; line_height = char_height+leading

	// state:
	QRgb	paper,	pushed_paper;	// paper color when printing in opaque mode
	QRgb	pen,	pushed_pen;		// pen color for printing
	uint	attr,	pushed_attr;	// print style
	int 	row,	pushed_row;		// current print position
	int 	col,	pushed_col;		// ""
	bool	blob,	pushed_blob;	// cursor blob visible?

	// the terminal font. calc_font_metrics() if changed:
	QFont	font;					// terminal font
	QRgb	dflt_pen;				// for reset
	//QRgb	dflt_paper;				// == fillcolor

	uint8*	udg;

	QPainter painter;

	TTYPixmap(int w, int h);
	TTYPixmap(int w, int h, QRgb paper);
	TTYPixmap(int w, int h, const QFont&, QRgb paper, QRgb pen);
	TTYPixmap(int w, int h, QRgb paper, QRgb pen);
	~TTYPixmap()					{ delete[] udg; }

	void	reset();
	void	pushState();
	void	popState();
	void	showBlob()				{ if(!blob) invert_blob(); }
	void	hideBlob()				{ if(blob) invert_blob(); }
	void	setAttr(uint);
	void	setPen(QRgb);
	void	setPaper(QRgb);

	void	forceInside();
	void	gotoPosition(int row, int col);
	void	gotoColumn(int col);
	void	cursorLeft(int n)		{ hideBlob(); col -= n; }
	void	cursorRight(int n)		{ hideBlob(); col += n; }
	void	cursorUp(int n)			{ hideBlob(); row -= n; }
	void	cursorDown(int n)		{ hideBlob(); row += n; }
	void	cursorTabulator(int n)	{ hideBlob(); col = ((uint)col/8u+n) * 8; }
	void	cursorReturn()			{ hideBlob(); col =  0; }

	void	clearScreen();
	void	clearRect(int rows, int cols);
	void	scrollScreenRight(int n);
	void	scrollScreenUp(int n);
	void	copyRect(int z_row, int z_col, int rows, int cols);

	void	drawText(uchar* text, int len);
	void	drawChar(uchar);
	void	drawChar(uchar, int n);
	void	drawBmp(uint8*, int h, int n=1);
	void	setUDG(uchar c, uint8* bmp);

private:
	void	set_colors();
	void	set_attr();
	void	calc_font_metrics();
	void	invert_blob();
	void	draw_text(uchar* text, int len);
	void	draw_bmp(QImage&, int n);
};




#endif














