/*	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 OBJECT_H
#define OBJECT_H
#include "kio/kio.h"
#include <QPixmap>
#include <Templates/RCPtr.h>
#include <Templates/RCArray.h>
#include "Pixmap.h"
#include "Stream.h"


typedef uint ObjID;

class Object;
typedef RCArray<Object> Objects;


// All existing objects:
// Object.obj_id is the index in this table.
extern Array<Object*> objects;
inline Object* getObject(ObjID o)	{ return objects[o]; }
extern void purgeAllObjects();



// =============================================================
//				Object for GFXPainter
//				implements a tree hierarchy
// =============================================================


class Object
{
	friend class RCPtr<Object>;
	friend class RCPtr<class RectObject>;
	friend class RCPtr<class GFXObject>;
	friend class RCPtr<class TTYObject>;
	friend class RCPtr<class CRTObject>;
	uint	rcnt;
	void	retain()			{ rcnt++; }
	void	release()			{ if(--rcnt==0) delete this; }

public:
	IsaID	isa_id;			// kind of object
	ObjID	obj_id;			// reference number

	Object*	parent;			// NULL for offscreen Objects
	Objects	children;

	QRect	zbox;			// destination rect in parent

private:
	ObjID	newObjId();

protected:
			Object(IsaID, const QRect& zbox);
			Object(IsaID, int x, int y, int w, int h);
			Object(IsaID, int w, int h);
			Object(const Object&) = delete;
	void	operator= (const Object&) = delete;
	virtual	~Object();

public:
virtual	bool isOpaque()					{ return no; }
virtual	void paint_out(QPainter&, int dx, int dy);
virtual int  paintjob(Stream&, QPainter&) throw(uint);
//virtual int  paint_in(QPainter&, uchar* data, int count);
virtual void resetGeometry()			{}
virtual void setQPos(int /*x*/, int /*y*/)		{}
virtual void setQSize(uint /*w*/, uint /*h*/)	{}

	uint	zwidth()					{ return zbox.width(); }
	uint	zheight()					{ return zbox.height(); }

	void	appendChild(Object*);
	void	removeChild(Object*);
	void	exchange(Object*);
	void	swap_all(Object*);
	void	dispose();

	void	moveToIndex(int i);
	void	moveToParent(Object*);
	void	replace(Object* old);

//	bool	isDescendantOf(Object* p) { return	p==parent || (parent && parent->isDescendantOf(p)); }
//	bool	isDescendantOf(Object* p) { return	parent && (p==parent || parent->isDescendantOf(p)); }
	bool	isDescendantOf(Object* p) { return	p==this || (parent && parent->isDescendantOf(p)); }
	int		index()					{ return parent ? parent->children.indexof(this) : -1; }
	Object*	firstChild()			{ return children.count() ? children[0].ptr() : NULL; }
	Object*	lastChild()				{ return children.count() ? children.last().ptr() : NULL; }
	Object* childAt(uint i)			{ return i < children.count() ? children[i].ptr() : NULL; }
	Object*	nextChild(uint i)		{ return childAt(i+1); }
	Object*	prevChild(uint i)		{ return childAt(i-1); }

	Object*	nextSibling()			{ return parent ? parent->childAt(index()+1) : NULL; }
	Object*	prevSibling()			{ return parent ? parent->childAt(index()-1) : NULL; }
};


class RectObject : public Object
{
public:
	QColor color;					// fill color

public:
	RectObject(const QRect&, QRgb);
	RectObject(int x, int y, int w, int h, QRgb);
	RectObject(int w, int h, QRgb);

	bool isOpaque() override				{ return color.alpha()==0xff; }
	void paint_out(QPainter&,int dx, int dy) override;
//	int  paint_in(QPainter&, uchar* data, int count) override;
	int  paintjob(Stream&, QPainter&) throw(uint) override;
//	void resetGeometry() override;
//	void setQPos(int x, int y) override;
//	void setQSize(uint w, uint h) override;
};


class CRTObject : public Object
{
public:
	QColor color;

public:
	CRTObject(const QRect&, QRgb);
	CRTObject(int x, int y, int w, int h, QRgb);
	CRTObject(int w, int h, QRgb);

	bool isOpaque() override				{ return no; }
	void paint_out(QPainter&,int dx, int dy) override;
	int  paintjob(Stream&, QPainter&) throw(uint) override;
	void resetGeometry() override;
//	void setQPos(int x, int y) override;
	void setQSize(uint w, uint h) override;
};


class GFXObject : public Object
{
public:
	RCPtr<GFXPixmap> pixmap;
	QRect	qbox;			// source rect (partial or full size)

public:
	GFXObject(GFXPixmap*, int x, int y);
	GFXObject(int w, int h, QRgb);

	bool isOpaque() override				{ return pixmap->isOpaque(); }
	void paint_out(QPainter&,int dx, int dy) override;
	int  paintjob(Stream&, QPainter&) throw(uint) override;
	void resetGeometry() override;
	void setQPos(int x, int y) override;
	void setQSize(uint w, uint h) override;
};




#endif










