/*=============================================================================
 *
 *  Filename:						Sort3Lst.hpp
 *  Version:            1.0
 *  Date:								06/11/96
 *  Author:             V.L.
 *
 *  Description:				interface of ThreeSortedList class
 *
 *===========================================================================*/
 
#ifndef __SORT3LST_HPP__
#define __SORT3LST_HPP__
 
#ifdef ACTIVE_EDITOR
 
/****************************************/
#ifndef CPA_EXPORT
#if defined(CPA_WANTS_IMPORT)
#define CPA_EXPORT __declspec(dllimport)
#elif defined(CPA_WANTS_EXPORT)
#define CPA_EXPORT __declspec(dllexport)
#else
#define CPA_EXPORT
#endif
#endif
/****************************************/
 
 
#include <afxtempl.h>
#include <afxcview.h>
#include "FRMBase.hpp"
#include "CPADD.hpp"
 
/*=============================================================================
* CBitmapList : list of CBitmap item + function to open & draw bitmaps
=============================================================================*/
 
#define C_cVariableSize		TRUE
#define C_cFixedSize			FALSE
 
/*uncomment to make the CBitmapList create small bitmaps for faster (?) subsequent loads*/
/*#define D_UseSmallBitmaps*/
 
#define BA_TOP						0
#define BA_LEFT						0
#define BA_CENTER					1
#define BA_RIGHT					2
#define BA_BOTTOM					2
 
 
typedef struct tdstBitmapItem_
{
	long			lData;
	CString			m_csName;
	HBITMAP			m_hBitmap;
	BOOL			m_bDeleted;
} tdstBitmapItem;
 
class CPA_EXPORT CBitmapList
{
private:
	FILE *m_hBadTexturesLogFile;
	BOOL	m_bVariableSize;	/* indicate that size can be variable or not*/
	CSize m_oMaxSize;				/* maximum size of a bitmap. if Variable size is false, all bitmap*/
													/* are converted to this size*/
	CList< tdstBitmapItem *, tdstBitmapItem *> m_oItemList;
	HBITMAP m_hLoadImage(
		CString _csName, int _cx, int _cy,
		CString _csNameOfSmallVersion,
		BOOL _bOverrideOptimizedImages
	);
#if defined(D_UseSmallBitmaps)
	BOOL m_bUseSmallCopies;
#endif /*D_UseSmallBitmaps*/
	BOOL m_bLoadOnlyOptimizedImages; /*size is a power of 2*/
 
public:
	CBitmapList();
	~CBitmapList();
 
	void m_fn_vCreate( 
		BOOL _bSize = C_cVariableSize, int _cx = 0, int _cy = 0, 
		BOOL _bLoadOnlyOptimizedImages = FALSE, BOOL _bUseSmallCopies = FALSE
	);
 
	long m_fn_lGetCount( void );
 
	int  m_fn_iAdd(CString _csNameToLoad, int _cx = 0, int _cy = 0, BOOL _bSmallCopyIfRelevant = TRUE, BOOL _bOverrideOptimizedImages = FALSE);
	void m_fn_vReload( CString _csBitmapName );
	void m_fn_vDelete( int iBitmap );
	void m_fn_vDeleteAll( void );
 
	void m_fn_vDraw( CDC* pDC, int iImage, POINT pt, RECT *pClipBox, char cAlignX = BA_LEFT, char cAlignY = BA_TOP );
 
	int m_fn_iGetBitmapIndex( char *szBitmapName );
	int m_fn_iGetBitmapIndexByLong( long lValue );
 
	HBITMAP m_fn_hGetBitmap( int iImage );
	CString m_fn_csGetBitmapName( int iImage );
 
	long m_fn_lGetBitmapLong( int iImage );
	void m_fn_vSetBitmapLong( int iImage, long lLong );
 
	void m_fn_vGetBoundBox( int& _cx, int& _cy);
 
private:
	void	m_fn_vIsCreated();
	int		m_fn_iFindBitmapByName( CString csName );
	BOOL	m_fn_bGetBitmapSize( CString csFileName, CSize *sz);
	int		m_fn_iGetFreeIndex( void );
};
 
/*=============================================================================
* CThreeSortedList class : class to manage a three sorted list
*											a list item is a string, an image index and a void pointer
*											order are : alphabetic, common, edited
=============================================================================*/
 
#define		C_cAlphaOrder					0
#define   C_cCommonOrder				1
#define		C_cEditedOrder				2
 
#define		TSL_cItemNotFound			-1
 
 
typedef struct tdstTSLItem_
{
	CString	m_csName;
	short	m_wImage;
	long	m_lContent;
} tdstTSLItem;
 
class CPA_EXPORT CThreeSortedList
{
	private:
		CBitmapList		*m_p_oBitmapList;			/* list of bitmaps*/
        /*tdstTSLItem     *m_d_stItem;                    // list's items*/
        CArray<tdstTSLItem*,tdstTSLItem*> m_oArrayOfTSLItem;
        long                    *m_d_lCommonIndex;      /* index of items sorted by common used order*/
        long                    *m_d_lEditedIndex;      /* index of items sorted by edited order*/
		long					m_lNumberOfElements;	/* number of items*/
		long					m_lMaxElements;				/* number max of items*/
 
		long					m_lSelectedItem;			/* index of selected item or TSL_cItemNotFound*/
 
		short m_fn_wGetIndexSwitchOrder( short wIndex, char cOrder );
		short m_fn_wAlphaToAnotherOrder( short wIndex, char cOrder );
 
	public:
		/*constructor*/
		CThreeSortedList();
		~CThreeSortedList();
 
		/*adding / changing/ deleting an element*/
		int  m_fn_iAddItem( CString csName, short wImage = -1, long lContent = 0, char cSortType = C_cAlphaOrder );
 
		int  m_fn_iSetItemName( long lIndex, char *szName, char cSortType = C_cAlphaOrder );
		void m_fn_vSetItemImage( long lIndex, short wImage, char cSortType = C_cAlphaOrder );
 
        void m_fn_vSetItemContent( long lIndex, long lContent, char cSortType = C_cAlphaOrder );
 
		BOOL m_fn_bDeleteItem( long lIndex, char cSortType = C_cAlphaOrder );
		BOOL m_fn_bDeleteAllItems(void);
 
		/* swap item*/
		void m_fn_vSwapEditedItems( long lIndex1, long lIndex2 );
		void m_fn_vDeplaceOrderedItem( long lIndex, long lNewIndex, char cSortType);
		void m_fn_vSetCommonItem( long lIndex );
 
		/* get number of element*/
		long m_fn_lGetCount( void ) { return m_lNumberOfElements; }
		BOOL m_fn_bIsEmpty ( void ) { return (m_lNumberOfElements==0); }
 
		/* get item or item's component*/
		tdstTSLItem *m_fn_p_stGetItem( long lIndex, char cSortType = C_cAlphaOrder);
		CString				m_fn_csGetName( long lIndex, char cSortType = C_cAlphaOrder );
		short				m_fn_wGetImage   ( long lIndex, char cSortType = C_cAlphaOrder );
		long				m_fn_lGetContent ( long lIndex, char cSortType = C_cAlphaOrder );
 
		/* find element*/
		long m_fn_lFindItem   ( long lStartAfter, tdstTSLItem *p_stItem, char cSortType = C_cAlphaOrder );
		long m_fn_lFindName   ( long lStartAfter, CString csName, char cSortType = C_cAlphaOrder );
		long m_fn_lFindImage  ( long lStartAfter, short wImage, char cSortType = C_cAlphaOrder );
		long m_fn_lFindContent( long lStartAfter, long lContent, char cSortType = C_cAlphaOrder );
 
		/*set / get image list*/
		void m_fn_vSetBitmapList( CBitmapList *_p_oBitmapList ) { m_p_oBitmapList = _p_oBitmapList;}
		CBitmapList *m_fn_p_oGetBitmapList( void ) { return m_p_oBitmapList; }
 
		/* selected item*/
		void m_fn_vSetSelectedItem( long lIndex, char cSortType = C_cAlphaOrder );
		long m_fn_lGetSelectedItem( char cSortType = C_cAlphaOrder );
		BOOL m_fn_bIsItemSelected( long lIndex, char cSortType = C_cAlphaOrder);
		void m_fn_vUnselectItem( void );
};
 
/*=============================================================================
* C3ListFrame class : frame that display a threesortedlist
=============================================================================*/
class CPA_EXPORT C3ListFrame: public FRMBase
{
	DECLARE_DYNCREATE(C3ListFrame)
 
	/* Constructor/destructor*/
	/*-----------------------*/
	public:
		C3ListFrame();
 
	/* Message map*/
	/*------------*/
	protected:
		afx_msg int     OnCreate(LPCREATESTRUCT);
		afx_msg void    OnClose(void);
		/*afx_msg void		OnDrawItem( LPDRAWITEMSTRUCT );*/
 
		DECLARE_MESSAGE_MAP()
};
 
/*=============================================================================
* C3ListView : view that display a ThreeSortedList
=============================================================================*/
#define C_cDisplayBitmapAndText			0
#define C_cDisplayText					1
#define C_cDisplayBitmap				2
#define C_cNumberOfDisplayTypes			3
 
#define C_cSwap							0
#define C_cDeplace						1
 
#define TLVM_lMenu						0x1
#define TLVM_lGrid						0x2
#define TLVM_lAlphabetic				0x10
#define TLVM_lCommon					0x20
#define TLVM_lEdited					0x40
#define	TLVM_lBitmapAndText				0x100
#define TLVM_lBitmap					0x200
#define TLVM_lText						0x400
#define TLVM_lDisplay					(TLVM_lBitmap | TLVM_lText | TLVM_lBitmapAndText)
#define TLVM_lOrder						(TLVM_lAlphabetic | TLVM_lCommon | TLVM_lEdited)
 
class CPA_EXPORT C3ListView : public CScrollView, public CThreeSortedList
{
	DECLARE_DYNCREATE(C3ListView)
 
	protected:
		/*
		 * member variable for managing a tips to show long name that are cut
		 */
		short			m_wCaseWithTips;		/* index of case that have a tips.*/
		RECT			m_stTipsRect;			/* saving tips rect*/
		CDC				m_oCDCTip;				/* saving background of tips*/
		CBitmap			m_oBitmapTip;			/* bitmap under tips*/
 
 
		long			m_lMenuFlags;			/* flags that determines which entries in menu are visible*/
		CSize			m_a_oDisplayBox[ C_cNumberOfDisplayTypes ];
		CSize			m_oCurrentBox;			/* current case size*/
 
		char			m_cDisplayType;			/* display type*/
		short			m_wNumberOfCols;		/* number of colums to display all list*/
		short			m_wNumberOfLines;		/* number of colums to display all list*/
 
		char			m_cCurrentOrder;		/* order;*/
		char			m_cMoveType;			/* swap or deplace*/
		BOOL			m_bGrid;				/* display grid or not*/
 
		DD_Object		m_oDDCase;				/* object for Drag and drop*/
 
		short			m_fn_wGetCaseFromClientPoint( int x, int y);
		void            m_fn_vDrawItem(short wItem, CDC *pDC, RECT *rect, short _wSpecialImage = TSL_cItemNotFound);
 
		/*
		 * function to show/hide tip
		 */
		void			m_fn_vHideTips( void );
		void			m_fn_vShowTips( void );
 
	public:
		C3ListView( void );
 
		void			m_fn_vGetCaseRect( short wIndex, RECT *pRect );
 
		void			m_fn_vChangeOrder( char _cOrder );
        char            m_fn_cGetOrder() { return m_cCurrentOrder; }
		void			m_fn_vChangeDisplayType( char _cDisplayType );
 
		void			m_fn_vSetBox( char _cDisplayType, int _iWidth, int _iHeight);
		void			m_fn_vSetDDTypeAndEditor( UINT uiDataType, char *szEditorname );
 
		void			OnDraw( CDC *pDC );
		virtual void	m_fn_vOverriddenDrawItem(short wItem, CDC *pDC, RECT *rect) { m_fn_vDrawItem( wItem, pDC, rect); }
 
		void			m_vScrollToMakeItemVisible(short _wItem);
		int				m_fn_iAddItem( CString csName, short wImage = -1, long lContent = 0, char cSortType = C_cAlphaOrder);
		BOOL			m_fn_bDeleteItem( long lIndex, char cSortType = C_cAlphaOrder );
		BOOL			m_fn_bDeleteAllItems();
 
		void			m_fn_vEnableMenuItem( long lFlags );
		void			m_fn_vDisableMenuItem( long lFlags );
 
	protected:
		BOOL			OnCommand( WPARAM wParam, LPARAM lParam );
		BOOL			PreTranslateMessage( MSG* pMsg );
 
		afx_msg int				OnCreate( LPCREATESTRUCT );
		afx_msg void			OnRButtonDown(UINT nFlags, CPoint pt );
		afx_msg void			OnMouseMove( UINT nFlags, CPoint pt );
		virtual afx_msg void	OnLButtonDown(UINT nFlags, CPoint pt );
		virtual afx_msg LONG	OnDragDropEnd( UINT wParam , LONG lParam);
		virtual afx_msg LONG	OnDragDropMove( UINT wParam , LONG lParam);
		virtual afx_msg LONG	OnDragDropScroll( UINT wParam , LONG lParam);
		afx_msg void			OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags );
		afx_msg void			OnWindowPosChanged( WINDOWPOS* lpwndpos );
 
		virtual void			OnSelectItem( short wCase );
 
		/*enables handling of user-defined popup menu entries*/
		virtual void	_OnCommand(short _wMenuId) {}
		virtual void	_InitPopupMenu(CMenu &r_oMenu, short wFirstValue) {}
 
		DECLARE_MESSAGE_MAP()
};
 
 
#endif /*ACTIVE_EDITOR*/
 
#endif /* __SORT3LST_HPP__ */
