/*	Copyright  (c)	Günter Woigk 2012 - 2015
					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 MEMORYINSPECTOR_H
#define MEMORYINSPECTOR_H

#include "kio/kio.h"
#include <QWidget>
#include "Inspector.h"
#include "Templates/Array.h"
#include "SimpleTerminal.h"
#include <QScrollBar>
#include "Cpu.h"


class MemoryInspector;
class SimpleTerminal;
class QScrollBar;
class QPushButton;
class QLineEdit;
class MyScrollBar;
class QCheckBox;
class QWidget;
class QBoxLayout;
class QRadioButton;
class QTextEdit;
class QComboBox;
class MyLineEdit;


extern const QColor paper_color_reg;	//gelb
extern const QColor paper_color_sp;		//grün
extern const QColor paper_color_hl;		//cyan
extern const QColor paper_color_pc;		//rot
extern const QColor color_white;
extern const QColor color_black;
extern const QColor color_light_grey;





// ==============================================================


/*	Memory Page Descriptor:
*/
struct Page
{
	uint addr, size;
	Page(uint a,uint sz) :addr(a),size(sz){}
};




// ==============================================================


class MIHeadWidget : public QWidget
{
	enum { PADDING_L=5,PADDING_R=5,PADDING_T=5,PADDING_B=5,SPACING_H=5,SPACING_V=5 };

public:
				MIHeadWidget(QWidget*p);
	int			heightForWidth(int) const;
	int			setWidth(int);				// return: neue Höhe
	int			updateLayout();				// return: neue Höhe
};


// ==============================================================

enum MIDisplayMode 	{ Bytes, Disass, MemGraph, MemAccess };						// displaymode
enum Register		{ regIP, regSP, regHP, regA0, regA1, regATMP, regD2AR };	// combobox_register



class MemoryInspector : public Inspector
{
	Q_OBJECT

public:
	enum { L_MARGIN=1, R_MARGIN=0, V_MARGINS=1, H_SPACE=2, COMBOBOX_HEIGHT=26, TEXTEDIT_HEIGHT=26, BUTTON_HEIGHT=32 };


protected:
	QComboBox*		combobox_register;
	MyLineEdit*		lineedit_baseaddress;
	MIHeadWidget*	head_widget;
	QWidget*		data_widget;
	QWidget*		stretch1;
	QWidget*		stretch2;
	MyScrollBar*	scrollbar;

	Cpu*			cpu;

	uint16			old_display_base_address;	// for update()
	int				scrollbar_width;			// for convenience
	bool			needs_aligned_addresses;
	Time			resized;

	uint			min_w,min_h,dw,dh;			// size increment handling. the Qt stuff is totally broken

// data source:
	MIDisplayMode	display_mode;			// Bytes, Words, Disass, MemGraph, MemAccess
	uint			bytes_per_row;
	uint			rows;
	uint16			display_base_address;	// offset to data_start of currently displayed data
	bool			update_all;


protected:
	virtual void	resizeEvent	(QResizeEvent*);
	virtual void	wheelEvent(QWheelEvent*);

	virtual void	setBaseAddress	(uint16);
	virtual void	updateScrollbar	();

public:
					MemoryInspector	(QWidget*, Cpu*cpu, MIDisplayMode, cstr idf);
					~MemoryInspector();

	void			updateAll()		{ update_all = true; }


protected slots:
	virtual void	setAddressFromTextEdit();		// textedit_baseaddress
	virtual void	setScrollPosition(int);			// scrollbar
	virtual void	update();						// timer

private slots:
	void			setAddressFromRegister(int);	// combobox_register



signals:
	void			scrollPositionChanged(int);		// -> scrollbar
	void			sizeConstraintsChanged();
};


#endif // MEMORYINSPECTOR_H

























