/***************************************************************************
 *   Copyright (C) 2008 by Jürgen Böhm   *
 *   juergen.boehm@aviduratas.de   *
 *                                                                         *
 *   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.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#ifndef QJBDISPLAYVPORT_H
#define QJBDISPLAYVPORT_H

/**
@author Jürgen Böhm
*/


#include <qfont.h>
#include <qfontmetrics.h>
#include <qpainter.h>
#include <qcolor.h>
#include <qpalette.h>
#include <qapplication.h>
#include <qevent.h>

#include <qwidget.h>

#include <new>
#include <iostream>


#define min(a,b) (((a) < (b)) ? (a) : (b))



class QjbDisplayVPort : public QWidget {

		Q_OBJECT

		public:

		QjbDisplayVPort (QWidget* w, const char* s, int alines, int acols) : QWidget(w,s), parent(w),
						ilines(alines), icols(acols) {

					bufsize = ilines * icols;

					mem = getMem (ilines, icols);

					QFont* fo = new QFont("Monospace", 10);
					// fo->setFixedPitch ( true );
					// fo->setStretch ( 72 );

					outFont = fo;
					setFontInfo();

					QPalette p = palette();
					p.setColor ( QColorGroup::Dark, QColor (255, 255, 255 ) );
					setPalette ( p );

					for (int i = 0; i < bufsize; i++) {
							mem[4 * i] = '+';
							mem[4 * i + 1] = 0;
					};


		}


		~QjbDisplayVPort () { delete[] mem; 
												  delete outFont;
		};

		int ilines;
		int icols;

		int bufsize;

		char* mem;

		char* getMem (int lines, int cols);
		void setFontInfo ();

		inline void drawCharacter (QPainter* p, int row, int col );


		void byteWrite (int offset, char val);
    void wordWrite (int offset, int val);


		QFont* outFont;
		QFontMetrics* fm;

		int pixelWidthX;
		int pixelHeight;


		QWidget* parent;

		void drawContents ( QPainter * p, int clipx, int clipy, int clipw, int cliph );


		protected:

		virtual void paintEvent ( QPaintEvent * ev);

		virtual void enterEvent ( QEvent * ev) { QWidget::enterEvent(ev); grabKeyboard (); }
		virtual void leaveEvent ( QEvent * ev) { QWidget::leaveEvent(ev); releaseKeyboard (); }

		virtual bool x11Event ( XEvent* xev);

protected:
    QColor qcolf, qcolb;
};


#endif
