/*************************************************************************
 *
 *  $RCSfile: svdhdl.hxx,v $
 *
 *  $Revision: 1.6 $
 *
 *  last change: $Author: aw $ $Date: 2002/09/13 12:16:31 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _SVDHDL_HXX
#define _SVDHDL_HXX

#ifndef _GEN_HXX //autogen
#include <tools/gen.hxx>
#endif

#ifndef _POINTR_HXX //autogen
#include <vcl/pointr.hxx>
#endif

#ifndef _CONTNR_HXX //autogen
#include <tools/contnr.hxx>
#endif

#ifndef _SOLAR_HRC
#include <svtools/solar.hrc>
#endif

#ifndef _B2D_MPNT_HXX
#include <goodies/b2dmpnt.hxx>
#endif

#ifndef _B2D_MLINE_HXX
#include <goodies/b2dmline.hxx>
#endif

#ifndef _B2D_MBMP_HXX
#include <goodies/b2dmbmp.hxx>
#endif

#include <svx/xpoly.hxx>
#include <svx/svdoedge.hxx>

class VirtualDevice;
class OutputDevice;
class Region;
class Window;
class SdrHdlList;
class SdrObject;
class SdrPageView;
class SdrHdlBitmapSet;

////////////////////////////////////////////////////////////////////////////////////////////////////

// Jedes Objekt muss in der Lage seine Handles zu erzeugen. Diese werden dann
// bei einer Selektion abgeholt, bei der View angemeldet und sichtbar gemacht.
// Wird ein Handle von der Maus beruehrt (IsHit()), so wird von der View der
// entsprechende Mauszeiger vom Handle abgeholt und der App auf Anfrage zum
// reinschalten uebergeben.
// Handles wie z.B. der Rotationsmittelpunkt oder die Spiegelachse werden von
// der View generiert, wenn vom Controller der entsprechende Transformations-
// Modus selektiert wird.
// HDL_MOVE...HDL_LWRGT muessen im enum immer zusammen stehen bleiben!

enum SdrHdlKind 
{
	HDL_MOVE,		// Handle zum Verschieben des Objekts
	HDL_UPLFT,		// Oben links
	HDL_UPPER,		// Oben
	HDL_UPRGT,		// Oben rechts
	HDL_LEFT,		// Links
	HDL_RIGHT,		// Rechts
	HDL_LWLFT,		// Unten links
	HDL_LOWER,		// Unten
	HDL_LWRGT,		// Unten rechts
	HDL_POLY,		// Punktselektion an Polygon oder Bezierkurve
	HDL_BWGT,		// Gewicht an einer Bezierkurve
	HDL_CIRC,		// Winkel an Kreissegmenten, Eckenradius am Rect
	HDL_REF1,		// Referenzpunkt 1, z.B. Rotationsmitte
	HDL_REF2,		// Referenzpunkt 2, z.B. Endpunkt der Spiegelachse
	HDL_MIRX,		// Die Spiegelachse selbst
	HDL_GLUE,		// GluePoint
	HDL_ANCHOR,		// anchor symbol (SD, SW)
	HDL_TRNS,		// interactive transparence
	HDL_GRAD,		// interactive gradient
	HDL_COLR,		// interactive color
	HDL_USER,
	HDL_ANCHOR_TR	// #101688# Anchor handle with (0,0) at top right for SW
};

////////////////////////////////////////////////////////////////////////////////////////////////////

enum BitmapColorIndex
{
	LightGreen,
	Cyan,
	LightCyan,
	Red,
	LightRed
};

////////////////////////////////////////////////////////////////////////////////////////////////////

enum BitmapMarkerKind
{
	Rect_7x7,
	Rect_9x9,
	Rect_11x11,
	Rect_13x13,
	Circ_7x7,
	Circ_9x9,
	Circ_11x11,
	Elli_7x9,
	Elli_9x11,
	Elli_9x7,
	Elli_11x9,
	RectPlus_7x7,
	RectPlus_9x9,
	RectPlus_11x11,
	Crosshair,
	Glue,
	Anchor,

	// #98388# add AnchorPressed to be able to aninate anchor control, too.
	AnchorPressed,

	// #101688# AnchorTR for SW
	AnchorTR,
	AnchorPressedTR
};

////////////////////////////////////////////////////////////////////////////////////////////////////

class SdrHdl
{
	friend class				SdrMarkView; // fuer den Zugriff auf nObjHdlNum
	friend class				SdrHdlList;

	static SdrHdlBitmapSet*		pSimpleSet;
	static SdrHdlBitmapSet*		pModernSet;

	// #101928#
	static SdrHdlBitmapSet*		pHighContrastSet;

	// #101928#
	BitmapEx& ImpGetBitmapEx(BitmapMarkerKind eKindOfMarker, sal_uInt16 nInd, sal_Bool bFine, sal_Bool bIsHighContrast);

protected:
	SdrObject*					pObj;      // Gehoert das Handle zu einem Objekt?
	SdrPageView*				pPV;       // Gehoert das Handle zu einem Objekt in einer bestimmten PageView?
	SdrHdlList*					pHdlList;  // Zum Feststelen der Handlegroesse

	// group of IAOs, normally only one
	B2dIAOGroup					aIAOGroup;

	Point						aPos;

	SdrHdlKind					eKind;

	long						nDrehWink; // Handles bzw. Mauszeiger drehen
	USHORT						nObjHdlNum; // wird von der MarkView benoetigt
	USHORT						nPolyNum;  // Polygonpunktes
	USHORT						nPPntNum;  // Punktnummer des Polygons
	USHORT						nSourceHdlNum; // ist noch vollstaendig zu implementieren

	unsigned					bSelect : 1;   // Ein selektierter Polygonpunkt?
	unsigned					b1PixMore : 1; // True=Handle wird 1 Pixel groesser dargestellt
	unsigned					bPlusHdl : 1;  // u.a. fuer Hld-Paint Optimierung bei MarkPoint/UnmarkPoint, ...

	// create marker for this kind
	virtual void CreateB2dIAObject();

	// cleanup marker if one exists
	void GetRidOfIAObject();

protected:
	B2dIAObject* CreateMarkerObject(B2dIAOManager* pMan, Point aPos, BitmapColorIndex eColIndex, BitmapMarkerKind eKindOfMarker);
	BitmapMarkerKind GetNextBigger(BitmapMarkerKind eKnd) const;

public:
	SdrHdl();
	SdrHdl(const Point& rPnt, SdrHdlKind eNewKind=HDL_MOVE);
	virtual ~SdrHdl();

	const B2dIAOGroup& GetIAOGroup() const { return aIAOGroup; }

	void SetHdlList(SdrHdlList* pList);
	SdrHdlKind GetKind() const { return eKind; }
	void Touch();

	const Point& GetPos() const { return aPos; }
	void SetPos(const Point& rPnt);

	SdrPageView* GetPageView() const { return pPV; }
	void SetPageView(SdrPageView* pNewPV) { pPV=pNewPV; }

	SdrObject* GetObj() const { return pObj;  }
	void SetObj(SdrObject* pNewObj);

	BOOL IsSelected() const { return bSelect; }
	void SetSelected(BOOL bJa=TRUE);

	void Set1PixMore(BOOL bJa=TRUE);
	void SetDrehWink(long n);

	BOOL IsCornerHdl() const { return eKind==HDL_UPLFT || eKind==HDL_UPRGT || eKind==HDL_LWLFT || eKind==HDL_LWRGT; }
	BOOL IsVertexHdl() const { return eKind==HDL_UPPER || eKind==HDL_LOWER || eKind==HDL_LEFT  || eKind==HDL_RIGHT; }

	void SetObjHdlNum(USHORT nNum) { nObjHdlNum=nNum; }
	USHORT GetObjHdlNum() const { return nObjHdlNum; }

	void SetPolyNum(USHORT nNum) { nPolyNum=nNum; }
	USHORT GetPolyNum() const { return nPolyNum; }

	void SetPointNum(USHORT nNum) { nPPntNum=nNum; }
	USHORT GetPointNum() const { return nPPntNum; }

	void SetPlusHdl(BOOL bOn) { bPlusHdl=bOn; }
	BOOL IsPlusHdl() const { return bPlusHdl; }

	void SetSourceHdlNum(USHORT nNum) { nSourceHdlNum=nNum; }
	USHORT GetSourceHdlNum() const { return nSourceHdlNum; }

	virtual Pointer GetPointer() const;
	virtual BOOL IsHit(const Point& rPnt, const OutputDevice& rOut) const;

	// #97016# II
	virtual BOOL IsFocusHdl() const;
};

////////////////////////////////////////////////////////////////////////////////////////////////////

#define	SDR_HANDLE_COLOR_SIZE_NORMAL			Size(13, 13)
#define	SDR_HANDLE_COLOR_SIZE_SELECTED			Size(17, 17)

class SdrHdlColor : public SdrHdl
{
private:
	// size of colr markers
	Size						aMarkerSize;

	// color
	Color						aMarkerColor;

	// callback link when value changed
    Link						aColorChangeHdl;

	// use luminance values only
	unsigned					bUseLuminance : 1;

	// create marker for this kind
	virtual void CreateB2dIAObject();

	// help functions
	Bitmap CreateColorDropper(Color aCol);
	Color GetLuminance(const Color& rCol);
	void CallColorChangeLink();

public:
	SdrHdlColor(const Point& rRef, Color aCol = Color(COL_BLACK), const Size& rSize = Size(11, 11), BOOL bLum = FALSE);
	virtual ~SdrHdlColor();

	BOOL IsUseLuminance() const { return bUseLuminance; }

	Color GetColor() const { return aMarkerColor; }
	void SetColor(Color aNew, BOOL bCallLink = FALSE);

	const Size& GetSize() const { return aMarkerSize; }
	void SetSize(const Size& rNew);

    void SetColorChangeHdl(const Link& rLink) { aColorChangeHdl = rLink; }
    const Link& GetColorChangeHdl() const { return aColorChangeHdl; }
};

////////////////////////////////////////////////////////////////////////////////////////////////////

class SdrHdlGradient : public SdrHdl
{
private:
	// pointer to used color handles
	SdrHdlColor*				pColHdl1;
	SdrHdlColor*				pColHdl2;

	// 2nd position
	Point						a2ndPos;

	// is this a gradient or a transparence
	unsigned					bGradient : 1;

	// select which handle to move
	unsigned					bMoveSingleHandle : 1;
	unsigned					bMoveFirstHandle : 1;

	// create marker for this kind
	virtual void CreateB2dIAObject();

public:
	SdrHdlGradient(const Point& rRef1, const Point& rRef2, BOOL bGrad = TRUE);
	virtual ~SdrHdlGradient();

	BOOL IsGradient() const { return bGradient; }

	// set the associated color handles
	void SetColorHandles(SdrHdlColor* pL1, SdrHdlColor* pL2) { pColHdl1 = pL1; pColHdl2 = pL2; }
	SdrHdlColor* GetColorHdl1() const { return pColHdl1; }
	SdrHdlColor* GetColorHdl2() const { return pColHdl2; }

	const Point& Get2ndPos() const { return a2ndPos; }
	void Set2ndPos(const Point& rPnt);

	// the link called by the color handles
	DECL_LINK(ColorChangeHdl, SdrHdl*);

	// transformation call, create gradient from this handle
	void FromIAOToItem(SdrObject* pObj, BOOL bSetItemOnObject, BOOL bUndo);

	// selection flags for interaction
	BOOL IsMoveSingleHandle() const { return bMoveSingleHandle; }
	void SetMoveSingleHandle(BOOL bNew) { bMoveSingleHandle = bNew; }
	BOOL IsMoveFirstHandle() const { return bMoveFirstHandle; }
	void SetMoveFirstHandle(BOOL bNew) { bMoveFirstHandle = bNew; }
};

////////////////////////////////////////////////////////////////////////////////////////////////////

// Spiegelachse
class SdrHdlLine: public SdrHdl
{
	// create marker for this kind
	virtual void CreateB2dIAObject();

protected:
	SdrHdl*						pHdl1;
	SdrHdl*						pHdl2;

public:
	SdrHdlLine(SdrHdl& rHdl1, SdrHdl& rHdl2, SdrHdlKind eNewKind=HDL_MIRX) { eKind=eNewKind; pHdl1=&rHdl1; pHdl2=&rHdl2; }
	virtual ~SdrHdlLine();

	virtual Pointer GetPointer() const;
	virtual BOOL IsHit(const Point& rPnt, const OutputDevice& rOut) const;
};

// Ein SdrHdlBezWgt hat Kenntnis von seinem "BasisHandle". Seine Draw-Methode
// zeichnet zusaetzlich eine Linie von seiner Position zur Position dieses
// BasisHandles.
class SdrHdlBezWgt: public SdrHdl
{
	// create marker for this kind
	virtual void CreateB2dIAObject();

protected:
	const SdrHdl* pHdl1;

public:
	// das ist kein Copy-Ctor!!!
	SdrHdlBezWgt(const SdrHdl* pRefHdl1, SdrHdlKind eNewKind=HDL_BWGT) { eKind=eNewKind; pHdl1=pRefHdl1; }
	virtual ~SdrHdlBezWgt();
};

////////////////////////////////////////////////////////////////////////////////////////////////////

class E3dVolumeMarker : public SdrHdl
{
	XPolyPolygon	aWireframePoly;

	// create marker for this kind
	virtual void CreateB2dIAObject();

public:
	E3dVolumeMarker(const XPolyPolygon& rXPP);
};

////////////////////////////////////////////////////////////////////////////////////////////////////

class ImpEdgeHdl: public SdrHdl
{
	SdrEdgeLineCode eLineCode;

	// create marker for this kind
	virtual void CreateB2dIAObject();

public:
	ImpEdgeHdl(const Point& rPnt, SdrHdlKind eNewKind): SdrHdl(rPnt,eNewKind),eLineCode(MIDDLELINE) {}
	virtual ~ImpEdgeHdl();

	void SetLineCode(SdrEdgeLineCode eCode);
	SdrEdgeLineCode GetLineCode() const     { return eLineCode; }
	BOOL IsHorzDrag() const;
	virtual Pointer GetPointer() const;
};

////////////////////////////////////////////////////////////////////////////////////////////////////

class ImpMeasureHdl: public SdrHdl
{
	// create marker for this kind
	virtual void CreateB2dIAObject();

public:
	ImpMeasureHdl(const Point& rPnt, SdrHdlKind eNewKind): SdrHdl(rPnt,eNewKind) {}
	virtual ~ImpMeasureHdl();

	virtual Pointer GetPointer() const;
};

////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

// #97016# II
class ImplHdlListData;

class SdrHdlList
{
protected:
	// #97016# II: use ImplHdlListData here to hold two data items
	//SdrMarkView*				pView;
	ImplHdlListData*			pImpl;

	Container					aList;
	USHORT						nHdlSize;

	unsigned					bRotateShear : 1;
	unsigned					bDistortShear : 1;
	unsigned					bMoveOutside : 1;      // Handles nach aussen ruecken (fuer TextEdit)
	unsigned					bFineHandles : 1;

private:
	SdrHdlList(const SdrHdlList&): aList(1024,64,64)  {}
	void operator=(const SdrHdlList&)                 {}
	BOOL operator==(const SdrHdlList&) const      { return FALSE; }
	BOOL operator!=(const SdrHdlList&) const      { return FALSE; }

public:
	SdrHdlList(SdrMarkView* pV);
	~SdrHdlList();
	void Clear();

	// #97016# II
	void TravelFocusHdl(sal_Bool bForward);
	SdrHdl* GetFocusHdl() const;
	void SetFocusHdl(SdrHdl* pNew);
	void ResetFocusHdl();

	// Access to View
	// #97016# II: moved implementation to cxx
	SdrMarkView* GetView() const;

	// Sortierung: 1.Level Erst Refpunkt-Handles, dann normale Handles, dann Glue, dann User, dann Plushandles
	//             2.Level PageView (Pointer)
	//             3.Level Position (x+y)
	void     Sort();
	ULONG    GetHdlCount() const                       { return aList.Count(); }
	SdrHdl*  GetHdl(ULONG nNum) const                  { return (SdrHdl*)(aList.GetObject(nNum)); }
	ULONG    GetHdlNum(const SdrHdl* pHdl) const;
	void     SetHdlSize(USHORT nSiz);
	USHORT   GetHdlSize() const                        { return nHdlSize; }
	void     SetMoveOutside(BOOL bOn);
	BOOL IsMoveOutside() const                     { return bMoveOutside; }
	void     SetRotateShear(BOOL bOn);
	BOOL IsRotateShear() const                     { return bRotateShear; }
	void     SetDistortShear(BOOL bOn);
	BOOL IsDistortShear() const                    { return bDistortShear; }
	void     SetFineHdl(BOOL bOn);
	BOOL IsFineHdl() const                        { return bFineHandles; }

	// AddHdl uebernimmt das Handle in sein Eigentum. Es muss
	// also auf dem Heap stehen, da Clear() ein delete macht.
	void    AddHdl(SdrHdl* pHdl, BOOL bAtBegin=FALSE);
	SdrHdl* RemoveHdl(ULONG nNum);

	// Zuletzt eingefuegte Handles werden am ehesten getroffen
	// (wenn Handles uebereinander liegen).
	SdrHdl* HitTest(const Point& rPnt, const OutputDevice& rOut, BOOL bBack=FALSE, BOOL bNext=FALSE, SdrHdl* pHdl0=NULL) const;
	SdrHdl* GetHdl(SdrHdlKind eKind1) const;
};

#endif //_SVDHDL_HXX

