/**************************************************************
 * 
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 *************************************************************/


#ifndef _SVIMPICN_HXX
#define _SVIMPICN_HXX

#ifndef _VIRDEV_HXX
#include <vcl/virdev.hxx>
#endif

#ifndef _SCRBAR_HXX
#include <vcl/scrbar.hxx>
#endif
#include <limits.h>

class SvLBoxEntry;
class SvLBoxTreeList;
class SvImpIconView;
class ImpIcnCursor;
class SvPtrarr;

#define PAINTFLAG_HOR_CENTERED	0x0001
#define PAINTFLAG_VER_CENTERED	0x0002

#define SELRECT_BORDER_OFFS	-7
// Flags
#define F_VER_SBARSIZE_WITH_HBAR		0x00000001
#define F_HOR_SBARSIZE_WITH_VBAR		0x00000002
#define F_IGNORE_NEXT_MOUSEMOVE			0x00000004	// OS/2 only
#define F_ENTRY_REMOVED					0x00000008
// ist gesetzt, wenn nach Clear oder Ctor mind. einmal gepaintet wurde
#define F_PAINTED						0x00000010
#define F_ADD_MODE						0x00000020
#define F_MOVING_SIBLING				0x00000040
#define F_SELRECT_VISIBLE				0x00000080
#define F_CMD_ARRIVED					0x00000100
#define F_DRAG_SOURCE					0x00000200
#define F_GRIDMODE						0x00000400
// beim Einfuegen eines Eintrags ergibt sich dessen Position
// durch simples Addieren auf die Position des zuletzt eingefuegten Eintrags
#define F_GRID_INSERT					0x00000800
#define F_DOWN_CTRL						0x00001000
#define F_DOWN_DESELECT					0x00002000
// Hack fuer D&D: Hintergrund des Entries nicht painten
#define F_NO_EMPHASIS					0x00004000
// Selektion per Gummiband
#define F_RUBBERING						0x00008000
#define F_START_EDITTIMER_IN_MOUSEUP	0x00010000

class SvImpIconView
{
	friend class ImpIcnCursor;
	ScrollBar   	aVerSBar;
	ScrollBar   	aHorSBar;
	Rectangle		aCurSelectionRect;
	SvPtrarr		aSelectedRectList;
	MouseEvent		aMouseMoveEvent;
	Timer			aEditTimer;  // fuer Inplace-Editieren
	Timer			aMouseMoveTimer; // generiert MouseMoves bei Gummibandselektion
	// Boundrect des zuletzt eingefuegten Entries
	Rectangle		aPrevBoundRect;
	Size			aOutputSize;		// Pixel
	Size			aVirtOutputSize;    // expandiert automatisch
	Point			aDDLastEntryPos;
	Point			aDDLastRectPos;

	SvLBoxTreeList* pModel;
	SvIconView*		pView;
	ImpIcnCursor*	pImpCursor;
	long			nMaxVirtWidth;      // max.breite aVirtOutputSize
	SvPtrarr*		pZOrderList;
	long			nGridDX,
					nGridDY;
	long			nHorSBarHeight,
					nVerSBarWidth;
	int				nViewMode;
	long			nHorDist;
	long			nVerDist;
	long			nMaxBmpWidth;
	long			nMaxBmpHeight;
	long			nMaxTextWidth;
	long			nMaxBoundHeight; // Hoehe des hoechsten BoundRects
	sal_uLong			nFlags;
	sal_uLong			nCurUserEvent;
	SvLBoxEntry*	pCurParent;
	SvLBoxEntry* 	pCursor;
	SvLBoxEntry*	pNextCursor; // wird in MovingEntry gesetzt und ist
								 // nur in EntryMoved gueltig!
	SvLBoxEntry*	pDDRefEntry;
	VirtualDevice*	pDDDev;
	VirtualDevice*	pDDBufDev;
	VirtualDevice*  pDDTempDev;

	SvIconViewTextMode eTextMode;
	sal_Bool			bMustRecalcBoundingRects;

	void			CheckAllSizes();
	void 			CheckSizes( SvLBoxEntry* pEntry,
						const SvIcnVwDataEntry* pViewData = 0  );
	void 			ShowCursor( sal_Bool bShow );

	void			SetNextEntryPos(const Point& rPos);
	Point			FindNextEntryPos( const Size& rBoundSize );
	void			ImpArrange();
	void			AdjustVirtSize( const Rectangle& );
	void			ResetVirtSize();
	void			CheckScrollBars();

					DECL_LINK( ScrollUpDownHdl, ScrollBar * );
					DECL_LINK( ScrollLeftRightHdl, ScrollBar * );
					DECL_LINK( MouseMoveTimeoutHdl, Timer* );
					DECL_LINK( EditTimeoutHdl, Timer* );
					DECL_LINK( UserEventHdl, void* );
	void			AdjustScrollBars();
	void			PositionScrollBars( long nRealWidth, long nRealHeight );
	void			CalcDocPos( Point& aMousePos );
	sal_Bool			GetResizeRect( Rectangle& );
	void			PaintResizeRect( const Rectangle& );
	SvLBoxEntry*	GetNewCursor();
	void			ToggleSelection( SvLBoxEntry* );
	void			DeselectAllBut( SvLBoxEntry* );
	void			Center( SvLBoxEntry* pEntry, SvIcnVwDataEntry* ) const;
	void			StopEditTimer() { aEditTimer.Stop(); }
	void			StartEditTimer() { aEditTimer.Start(); }
	void 			ImpHideDDIcon();
	void 			ImpDrawXORRect( const Rectangle& rRect );
	void			AddSelectedRect( const Rectangle&, short nOffset = SELRECT_BORDER_OFFS );
	void			ClearSelectedRectList();
	Rectangle		CalcMaxTextRect( const SvLBoxEntry* pEntry,
									const SvIcnVwDataEntry* pViewData ) const;

	void			ClipAtVirtOutRect( Rectangle& rRect ) const;
	void 			AdjustAtGrid( const SvPtrarr& rRow, SvLBoxEntry* pStart=0 );
	Point			AdjustAtGrid(
						const Rectangle& rCenterRect, // "Schwerpunkt" des Objekts (typ. Bmp-Rect)
						const Rectangle& rBoundRect ) const;
	SvIconViewTextMode GetEntryTextModeSmart( const SvLBoxEntry* pEntry,
						const SvIcnVwDataEntry* pViewData ) const;

	sal_Bool			CheckVerScrollBar();
	sal_Bool			CheckHorScrollBar();
	void 			CancelUserEvent();

public:

					SvImpIconView( SvIconView* pView, SvLBoxTreeList*, WinBits nWinStyle );
					~SvImpIconView();

	void 			Clear( sal_Bool bInCtor = sal_False );
	void 			SetStyle( const WinBits i_nWinStyle );
	void 			SetModel( SvLBoxTreeList* pTree, SvLBoxEntry* pParent )
						{ pModel = pTree; SetCurParent(pParent); }
	void 			EntryInserted( SvLBoxEntry*);
	void 			RemovingEntry( SvLBoxEntry* pEntry );
	void 			EntryRemoved();
	void 			MovingEntry( SvLBoxEntry* pEntry );
	void 			EntryMoved( SvLBoxEntry* pEntry );
	void 			TreeInserted( SvLBoxEntry* pEntry );
	void 			ChangedFont();
	void 			ModelHasEntryInvalidated( SvListEntry* );
	void 			EntryExpanded( SvLBoxEntry* pEntry );
	void 			EntryCollapsed( SvLBoxEntry* pEntry );
	void 			CollapsingEntry( SvLBoxEntry* pEntry );
	void 			EntrySelected( SvLBoxEntry*, sal_Bool bSelect );

	void 			Paint( const Rectangle& rRect );
	void 			RepaintSelectionItems();
	void 			MouseButtonDown( const MouseEvent& );
	void 			MouseButtonUp( const MouseEvent& );
	void 			MouseMove( const MouseEvent&);
	sal_Bool 			KeyInput( const KeyEvent& );
	void 			Resize();
	void 			GetFocus();
	void 			LoseFocus();
	void 			UpdateAll();
	void 			PaintEntry( SvLBoxEntry* pEntry,
						SvIcnVwDataEntry* pViewData = 0 );
	void 			PaintEntry( SvLBoxEntry*, const Point&,
						SvIcnVwDataEntry* pViewData = 0, OutputDevice* pOut = 0);
	void 			SetEntryPosition( SvLBoxEntry* pEntry, const Point& rPos,
								      sal_Bool bAdjustRow = sal_False,
                                      sal_Bool bCheckScrollBars = sal_False );
	void 			InvalidateEntry( SvLBoxEntry* );
	void 			ViewDataInitialized( SvLBoxEntry* pEntry );
	SvLBoxItem* 	GetItem( SvLBoxEntry*, const Point& rAbsPos );

	void 			SetNoSelection();
	void 			SetDragDropMode( DragDropMode eDDMode );
	void 			SetSelectionMode( SelectionMode eSelMode  );

	void 			SttDrag( const Point& rPos );
	void 			EndDrag();

	SvLBoxEntry* 	GetCurEntry() const { return pCursor; }
	void 			SetCursor( SvLBoxEntry* );

	sal_Bool			IsEntryInView( SvLBoxEntry* );
	SvLBoxEntry* 	GetEntry( const Point& rDocPos );
	SvLBoxEntry*	GetNextEntry( const Point& rDocPos, SvLBoxEntry* pCurEntry );
	SvLBoxEntry*	GetPrevEntry( const Point& rDocPos, SvLBoxEntry* pCurEntry  );

	Point 			GetEntryPosition( SvLBoxEntry* );
	void 			MakeVisible( SvLBoxEntry* pEntry );

	void			Arrange();

	void			SetSpaceBetweenEntries( long nHor, long Ver );
	long			GetHorSpaceBetweenEntries() const { return nHorDist; }
	long			GetVerSpaceBetweenEntries() const { return nVerDist; }

	Rectangle		CalcFocusRect( SvLBoxEntry* );

	Rectangle		CalcBmpRect( SvLBoxEntry*, const Point* pPos = 0,
						SvIcnVwDataEntry* pViewData=0 );
	Rectangle		CalcTextRect( SvLBoxEntry*, SvLBoxString* pItem = 0,
								  const Point* pPos = 0,
								  sal_Bool bForInplaceEdit = sal_False,
								  SvIcnVwDataEntry* pViewData = 0 );

	long			CalcBoundingWidth( SvLBoxEntry*, const SvIcnVwDataEntry* pViewData = 0) const;
	long			CalcBoundingHeight( SvLBoxEntry*, const SvIcnVwDataEntry* pViewData= 0 ) const;
	Size			CalcBoundingSize( SvLBoxEntry*,
						SvIcnVwDataEntry* pViewData = 0 ) const;
	void			FindBoundingRect( SvLBoxEntry* pEntry,
						SvIcnVwDataEntry* pViewData = 0 );
	// berechnet alle BoundRects neu
	void			RecalcAllBoundingRects();
	// berechnet alle ungueltigen BoundRects neu
	void			RecalcAllBoundingRectsSmart();
	const Rectangle&  GetBoundingRect( SvLBoxEntry*,
						SvIcnVwDataEntry* pViewData=0);
	void			InvalidateBoundingRect( SvLBoxEntry* );
	void			InvalidateBoundingRect( Rectangle& rRect ) { rRect.Right() = LONG_MAX; }
	sal_Bool			IsBoundingRectValid( const Rectangle& rRect ) const { return (sal_Bool)( rRect.Right() != LONG_MAX ); }

	void 			PaintEmphasis( const Rectangle&, sal_Bool bSelected,
						           sal_Bool bCursored, OutputDevice* pOut = 0 );
	void 			PaintItem( const Rectangle& rRect, SvLBoxItem* pItem,
						SvLBoxEntry* pEntry, sal_uInt16 nPaintFlags, OutputDevice* pOut = 0 );
	// berechnet alle BoundingRects neu, wenn bMustRecalcBoundingRects == sal_True
	void			CheckBoundingRects() { if (bMustRecalcBoundingRects) RecalcAllBoundingRects(); }
	// berechnet alle invalidierten BoundingRects neu
	void			UpdateBoundingRects();
	void			ShowTargetEmphasis( SvLBoxEntry* pEntry, sal_Bool bShow );
	SvLBoxEntry* 	GetDropTarget( const Point& rPosPixel );
	sal_Bool			NotifyMoving( SvLBoxEntry* pTarget, SvLBoxEntry* pEntry,
						SvLBoxEntry*& rpNewParent, sal_uLong& rNewChildPos );
	sal_Bool			NotifyCopying( SvLBoxEntry* pTarget, SvLBoxEntry* pEntry,
						SvLBoxEntry*& rpNewParent, sal_uLong& rNewChildPos );

	void 			WriteDragServerInfo( const Point&, SvLBoxDDInfo* );
	void 			ReadDragServerInfo( const Point&, SvLBoxDDInfo* );
	void			ToTop( SvLBoxEntry* );

	void			SetCurParent( SvLBoxEntry* pNewParent );
	SvLBoxEntry* 	GetCurParent() const { return pCurParent; }
	sal_uInt16			GetSelectionCount() const;
	void			SetGrid( long nDX, long nDY );
	void			Scroll( long nDeltaX, long nDeltaY, sal_Bool bScrollBar = sal_False );
	const Size&		GetItemSize( SvIconView* pView, SvLBoxEntry*, SvLBoxItem*,
						const SvIcnVwDataEntry* pViewData = 0 ) const;
	void			PrepareCommandEvent( const Point& rPt );

	void 			HideDDIcon();
	void 			ShowDDIcon( SvLBoxEntry* pRefEntry, const Point& rPos );
	void			HideShowDDIcon( SvLBoxEntry* pRefEntry, const Point& rPos );

	SvLBoxEntry* 	mpViewData;

	sal_Bool			IsOver(	SvPtrarr* pSelectedRectList, const Rectangle& rEntryBoundRect ) const;
	void			SelectRect( const Rectangle&, sal_Bool bAdd = sal_True,
						SvPtrarr* pOtherRects = 0,
						short nOffs = SELRECT_BORDER_OFFS );
	void			DrawSelectionRect( const Rectangle& );
	void			HideSelectionRect();
	void			CalcScrollOffsets( const Point& rRefPosPixel,
						long& rX, long& rY, sal_Bool bDragDrop = sal_False,
						sal_uInt16 nBorderWidth = 10 );
	void			EndTracking();
	sal_Bool			IsTextHit( SvLBoxEntry* pEntry, const Point& rDocPos );
	void			MakeVisible( const Rectangle& rDocPos,sal_Bool bInScrollBarEvent=sal_False);
	void			AdjustAtGrid( SvLBoxEntry* pStart = 0 );
	void			SetTextMode( SvIconViewTextMode, SvLBoxEntry* pEntry = 0 );
	SvIconViewTextMode GetTextMode( const SvLBoxEntry* pEntry = 0,
					const SvIcnVwDataEntry* pViewData = 0 ) const;
	void			ShowFocusRect( const SvLBoxEntry* pEntry );
};

inline void SvImpIconView::MakeVisible( SvLBoxEntry* pEntry )
{
	const Rectangle& rRect = GetBoundingRect( pEntry );
	MakeVisible( rRect );
}

#endif // #ifndef _SVIMPICN_HXX


