/*	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 MEMORYBYTESINSPECTOR_H
#define MEMORYBYTESINSPECTOR_H

#include "kio/kio.h"
#include <QWidget>
#include "MemoryInspector.h"
#include "Cpu.h"
#include "Templates/Array.h"

class MemoryInspector;
class SimpleTerminal;
class QScrollBar;
class QPushButton;
class QLineEdit;
class MyScrollBar;
class QCheckBox;


class MemoryHexInspector : public MemoryInspector
{
	Q_OBJECT

	SimpleTerminal*	address_view;
	SimpleTerminal*	hex_view;
	SimpleTerminal*	ascii_view;
	MyScrollBar*	scrollbar;

	QPushButton*	set16;
	QPushButton*	set32;
//	QCheckBox*		checkbox_words;
//	bool			is_words;

	QWidget*		widget_edit_mode;
	QPushButton*	button_breakpoint_r;
	QPushButton*	button_breakpoint_w;
	QPushButton*	button_breakpoint_x;
	QPushButton*	button_edit_mode;

//	QWidget*		widget_register;
//	QPushButton*	button_goto_pc;
//	QPushButton*	button_goto_sp;
//	QPushButton*	button_goto_bc;
//	QPushButton*	button_goto_de;
//	QPushButton*	button_goto_hl;
//	QPushButton*	button_goto_ix;
//	QPushButton*	button_goto_iy;
//	QPushButton*	button_goto_xsp;

//	QPushButton*	button_register;

	uint16			ascii_edit_address;
	uint16			hex_edit_address;
	uint			hex_edit_nibble;
	bool			edit_flashphase;
	Time			edit_flashtime;

	uint			cw;			// Character width
	uint			rh;			// Row height
	uint16			ip,sp,hp,a0,a1,atmp,d2ar;	// last highlighted address for register

//	bool			is_edit_mode;
//	uint32			breakpoint_mode;
	uint			edit_mode;				// as set by combobox_editmode: enum{EDITMODE_VIEW,EDITMODE_EDIT,EDITMODE_BREAKPOINTS};
	uint16			breakpoint_mask;		// bitmask, if edit_mode == EDITMODE_BREAKPOINTS

// scroll position & currently displayed data values:
	Array<Byte>		displayed_data;			// bytes_per_row * rows
	uint			first_valid_row;
	uint			last_valid_row;			// excl.

	void			print_byte	(int row, int col, Byte byte);
	void			print_rows	(uint row_a, uint row_e, uint address, Byte* bytes, uint cnt);
	void			print_byte_seen_by_cpu(uint16 addr);
	void			set_bytes_per_row(int);
	uint			data_widget_width_for_bytes(uint n);
	uint			inspector_width_for_bytes(uint n);
	void			update_tooltip();
	void			show_hex_cursor(bool);
	void			show_ascii_cursor(bool);
	void			hide_cursor();
	void			show_cursor();
	void			step_left_in_hex();
	void			step_right_in_hex();
	bool			editing_in_ascii();
	bool			editing_in_hex();
	void			set_breakpoint(uint16 mask, bool f);

protected:
	virtual void	resizeEvent(QResizeEvent*);
//	virtual void	wheelEvent(QWheelEvent*);
//	virtual void	paintEvent(QPaintEvent*);
	virtual void	mousePressEvent(QMouseEvent*);
	virtual bool	event(QEvent*);
	virtual void	keyPressEvent(QKeyEvent*);
//	virtual void	keyReleaseEvent(QKeyEvent*);

	virtual void	setBaseAddress	(uint16);

public:
					MemoryHexInspector (QWidget*parent, Cpu*);
					~MemoryHexInspector();

signals:

public slots:
	virtual void	setScrollPosition	(int);		// scrollbar
	virtual void	update				();
	void			set16BytesPerRow	();
	void			set32BytesPerRow	();
//	void			setWordMode			(bool);		// checkbox_words
	void			setEditMode			(bool);
	void			setBreakpointR		(bool f)	{ set_breakpoint(cpu_break_r,f); }
	void			setBreakpointW		(bool f)	{ set_breakpoint(cpu_break_w,f); }
	void			setBreakpointX		(bool f)	{ set_breakpoint(cpu_break_x,f); }
	void			slotFocusChanged	(bool);
};


#endif // MEMORYBYTESINSPECTOR_H

























