// MenuBase.h: interface for the CMenuBase class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MENUBASE_H__D7668B31_57D4_11D2_BDA0_0060971BDC6D__INCLUDED_)
#define AFX_MENUBASE_H__D7668B31_57D4_11D2_BDA0_0060971BDC6D__INCLUDED_

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

#include "ltguimgr.h"
#include "stdlith.h"

class CLTGUIFadeItemCtrl;
class CLTGUITextItemCtrl;
class CMenuBase : public CLTGUICommandHandler
{
public:
	CMenuBase();
	virtual ~CMenuBase();

	// Initialization/Termination
	// NOTE:  You must also initialize the static members before using this class.  See the member
	//        variable access methods below.
    virtual LTBOOL   Init(ILTClient *pClientDE, CMenuBase *pParentMenu, DWORD dwMenuID, int nMenuHeight);
	virtual void	Term();

	// Builds the menu
    virtual LTBOOL   Build()                             { m_bBuilt=LTTRUE; return TRUE; }
    LTBOOL           IsBuilt()                           { return m_bBuilt; }

	// Get the parent menu
	CMenuBase		*GetParentMenu()					{ return m_pParentMenu; }

	// Get the menu ID
    uint32          GetMenuID()                         { return m_dwMenuID; }

	// Renders the menu to a surface
    virtual LTBOOL   Render(HSURFACE hDestSurf);

	// Creates the title for the menu from the path to the image.  The non-Enlish version
	// overwrites the surface with a text string specified by the string ID.
    LTBOOL           CreateTitle(char *lpszTitleSurf, int nStringID, LTIntPt titlePos, LTBOOL bUseBitmapSurf, LTBOOL bDrawSolid = LTTRUE);
    LTBOOL           CreateTitle(char *lpszTitleSurf, int nStringID, int xPos, int yPos, LTBOOL bUseBitmapSurf, LTBOOL bDrawSolid = LTTRUE);

	// This is called when the menu gets or loses focus
    virtual void    OnFocus(LTBOOL bFocus)               { }

	/////////////////////////////////////////
	// Adds a menu options using an HSTRING
	CLTGUIFadeItemCtrl		*AddFadeItemOption(char *lpszOptionSurfPrefix, int nSurfaces, HSTRING hOptionText, DWORD dwCommandID, int xPos=-1, int yPos=-1, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    CLTGUITextItemCtrl      *AddTextItemOption(HSTRING hText, DWORD dwCommandID, CLTGUIFont *pFont, LTBOOL bDrawSolid=LTTRUE, int *pnValue=LTNULL, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
	CLTGUIColumnTextCtrl	*AddColumnTextOption(DWORD dwCommandID, CLTGUIFont *pFont, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    CLTGUISliderCtrl        *AddSliderOption(HSTRING hText, CLTGUIFont *pFont, int nSliderOffset, HSURFACE hBarSurf, HSURFACE hTabSurf, LTBOOL bNumDisplay=LTFALSE, int *pnValue=LTNULL);
    CLTGUIOnOffCtrl         *AddOnOffOption(HSTRING hText, CLTGUIFont *pFont, int nRightColumnOffset, LTBOOL *pbValue=LTNULL);
    CLTGUIEditCtrl          *AddEditOption(HSTRING hDescription, DWORD dwCommandID, CLTGUIFont *pFont, int nEditStringOffset, int nBufferSize, char *lpszValue=LTNULL, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    CLTGUICycleCtrl         *AddCycleOption(HSTRING hText,  CLTGUIFont *pFont, int nHeaderWidth, int nSpacerWidth= 25, int *pnValue = LTNULL);


	// Just a wrapper for adding fading text options with large fonts
    CLTGUITextItemCtrl      *AddLargeTextItemOption(HSTRING hText, DWORD dwCommandID, LTBOOL bUseBitmapFontArray = LTFALSE, DWORD dwFadeTime = 300, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    CLTGUITextItemCtrl      *AddSmallTextItemOption(HSTRING hText, DWORD dwCommandID, LTBOOL bUseBitmapFontArray = LTFALSE, DWORD dwFadeTime = 300, DWORD dwParam1 = 0, DWORD dwParam2 = 0);

	///////////////////////////////////////////////////////////////////////////////////
	// Wrappers for adding menu items with a resource string ID instead of an HSTRING
	CLTGUIFadeItemCtrl		*AddFadeItemOption(char *lpszOptionSurfPrefix, int nSurfaces, int messageCode, DWORD dwCommandID, int xPos=-1, int yPos=-1, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    CLTGUITextItemCtrl      *AddTextItemOption(int messageCode, DWORD dwCommandID, CLTGUIFont *pFont, LTBOOL bDrawSolid=LTTRUE, int *pnValue=LTNULL, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    CLTGUISliderCtrl        *AddSliderOption(int messageCode, CLTGUIFont *pFont, int nSliderOffset, HSURFACE hBarSurf, HSURFACE hTabSurf, LTBOOL bNumDisplay=LTFALSE, int *pnValue=LTNULL);
    CLTGUIOnOffCtrl         *AddOnOffOption(int messageCode, CLTGUIFont *pFont, int nRightColumnOffset, LTBOOL *pbValue=LTNULL);
    CLTGUIEditCtrl          *AddEditOption(int messageCode, DWORD dwCommandID, CLTGUIFont *pFont, int nEditStringOffset, int nBufferSize, char *lpszValue=LTNULL, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    CLTGUICycleCtrl         *AddCycleOption(int messageCode,  CLTGUIFont *pFont, int nHeaderWidth, int nSpacerWidth= 25, int *pnValue = LTNULL);
    CLTGUITextItemCtrl      *AddLargeTextItemOption(int messageCode, DWORD dwCommandID, LTBOOL bUseBitmapFontArray = LTFALSE, DWORD dwFadeTime = 300, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    CLTGUITextItemCtrl      *AddSmallTextItemOption(int messageCode, DWORD dwCommandID, LTBOOL bUseBitmapFontArray = LTFALSE, DWORD dwFadeTime = 300, DWORD dwParam1 = 0, DWORD dwParam2 = 0);

	///////////////////////////////////////////////////////////////////////////////////
	// Wrappers for adding menu items with a char* instead of a string ID or HSTRING
	inline CLTGUIFadeItemCtrl	*AddFadeItemOption(char *lpszOptionSurfPrefix, int nSurfaces, char *lpszOptionText, DWORD dwCommandID, int xPos=-1, int yPos=-1, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    inline CLTGUITextItemCtrl   *AddTextItemOption(char *lpszText, DWORD dwCommandID, CLTGUIFont *pFont, LTBOOL bDrawSolid=LTTRUE, int *pnValue=LTNULL, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    inline CLTGUISliderCtrl     *AddSliderOption(char *lpszText, CLTGUIFont *pFont, int nSliderOffset, HSURFACE hBarSurf, HSURFACE hTabSurf, LTBOOL bNumDisplay=LTFALSE, int *pnValue=LTNULL);
    inline CLTGUIOnOffCtrl      *AddOnOffOption(char *lpszText, CLTGUIFont *pFont, int nRightColumnOffset, LTBOOL *pbValue=LTNULL);
    inline CLTGUIEditCtrl       *AddEditOption(char *lpszDescription, DWORD dwCommandID, CLTGUIFont *pFont, int nEditStringOffset, int nBufferSize, char *lpszValue=LTNULL, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    inline CLTGUICycleCtrl      *AddCycleOption(char *lpszText,  CLTGUIFont *pFont, int nHeaderWidth, int nSpacerWidth= 25, int *pnValue = LTNULL);
    inline CLTGUITextItemCtrl   *AddLargeTextItemOption(char *lspzText, DWORD dwCommandID, LTBOOL bUseBitmapFontArray = LTFALSE, DWORD dwFadeTime = 300, DWORD dwParam1 = 0, DWORD dwParam2 = 0);
    inline CLTGUITextItemCtrl   *AddSmallTextItemOption(char *lspzText, DWORD dwCommandID, LTBOOL bUseBitmapFontArray = LTFALSE, DWORD dwFadeTime = 300, DWORD dwParam1 = 0, DWORD dwParam2 = 0);

	// Sets the parent menu
	void			SetParentMenu(CMenuBase *pParent)		{ m_pParentMenu=pParent; }

	// Calls UpdateData on each control in the menu
    void            UpdateData(LTBOOL bSaveAndValidate=LTTRUE);

	// Set the current menu item selection index.  Note that this will reset any animations.
	void			SetCurrentItem ( int nItemIndex );

	// Gets the currently selected item
	int				GetCurrentItemIndex()					{ return m_listOption.GetSelectedItem(); }
	CLTGUICtrl		*GetCurrentItem();

	// Sets the position of the title and menu option
	void			SetTitlePos(int x, int y)				{ m_titlePos.x=x; m_titlePos.y=y; }
    void            SetTitlePos(LTIntPt pt)                  { SetTitlePos(pt.x, pt.y); }
    void            SetTitleCenter(LTBOOL bCenter)           { m_nTitleAlign = (bCenter ? LTF_JUSTIFY_CENTER : LTF_JUSTIFY_LEFT) ; }
	void			SetTitleAlignment(int nAlignment)		{ m_nTitleAlign = nAlignment; }
    void            SetTitleColor(HLTCOLOR titleColor)      { m_hTitleColor = titleColor; }

	void			SetOptionPos(int x, int y)				{ m_listOption.SetPos(x, y); }
    void            SetOptionPos(LTIntPt pt)                 { SetOptionPos(pt.x, pt.y); }
    void            SetOptionCenter(LTBOOL bCenter)          { m_listOption.SetCenter(bCenter); }
	void			SetOptionAlignment(int nAlignment)		{ m_listOption.SetAlignment(nAlignment); }
    void            SetOptionColors(HLTCOLOR selColor, HLTCOLOR nonSelColor, HLTCOLOR disabledColor=SETRGB(128,128,128))
															{	m_hSelectedColor = selColor; m_hNonSelectedColor = nonSelColor; m_hDisabledColor = disabledColor;}


	// Sets the height of the menu
	void			SetMenuHeight(int nHeight)				{ m_listOption.SetHeight(nHeight); }

	// Turns on and off low resolution fonts for the menus
    void            SetLowResolutionFonts(LTBOOL bLowRes, LTBOOL bUseBitmapFontArray);

	// Sets the item spacing
	void			SetItemSpacing(int nSpacing)			{ m_listOption.SetItemSpacing(nSpacing); }

	// Sets whether or not the items should wrap when scrolling
    void            SetScrollWrap(LTBOOL bWrap)              { m_listOption.SetScrollWrap(bWrap); }

	// Call this if you wish to enable highlighting the item that is under the mouse
	// cursor and changing selection when the mouse is moved.
    void            EnableMouseMoveSelect(LTBOOL bEnable);

	// Set the maximum number of items to scroll per second when using the mouse
	void			SetMaxScrollRate(int nItemsPerSecond)	{ m_nMaxScrollRate=nItemsPerSecond; }

	// Turns the up/down arrows on/off and sets the X position that they should be pointing
    void            UseArrows(LTBOOL bUse, int xCenter, int yUpAdjust = 0, int yDownAdjust = 0);

	// Handles a key press.  Returns FALSE if the key was not processed through this method.
	// Left, Up, Down, Right, and Enter are automatically passed through OnUp(), OnDown(), etc.
    virtual LTBOOL   HandleKeyDown(int key, int rep);

	// Handle a device tracking message.
    // Return LTTRUE if the device tracking should stop.
    virtual LTBOOL   HandleDeviceTrack(DeviceInput *pInput, uint32 dwNumDevices);

	// Mouse messages
    virtual LTBOOL   OnLButtonDown(int x, int y);
    virtual LTBOOL   OnLButtonUp(int x, int y);
    virtual LTBOOL   OnLButtonDblClick(int x, int y);
    virtual LTBOOL   OnRButtonDown(int x, int y);
    virtual LTBOOL   OnRButtonUp(int x, int y);
    virtual LTBOOL   OnRButtonDblClick(int x, int y);
    virtual LTBOOL   OnMouseMove(int x, int y);
    virtual LTBOOL   OnMouseWheel(DWORD dwFlags, short zDelta, WORD xPos, WORD yPos);

protected:
    virtual uint32  OnCommand(uint32 dwCommand, uint32 dwParam1, uint32 dwParam2);

	// Handle input
    virtual LTBOOL   OnUp();
    virtual LTBOOL   OnDown();
    virtual LTBOOL   OnLeft();
    virtual LTBOOL   OnRight();
    virtual LTBOOL   OnEnter();

	// Play sounds
	virtual void	PlaySelectSound();
	virtual void	PlayEnterSound();

	// Removes all of the options from the menu
	void		RemoveAllOptions();

	// Sorts a file list.  This was taken from the old menu code.
	FileEntry*	SortFileList(FileEntry *pfe);

	// Renders the up/down arrows
	void		RenderArrows(HSURFACE hDestSurf);

	// Returns the bouding box for where the up/down arrows are
    LTBOOL       GetArrowBoundingBox(LTRect *prcUpArrow, LTRect *prcDownArrow);

	// Handle scrolling up and down from mouse clicks
	void		HandleMouseScrolling();

    void        SetTransparentColor(HLTCOLOR hTrans) { m_hTransparentColor = hTrans; }

protected:
	// Pure virtual function calls.  Basically these are used to get access
	// to members of the class that contains all of the menus.
	virtual void			FreeSharedSurface(HSURFACE hSurf)=0;
	virtual HSURFACE		GetSharedSurface(char *lpszSurf)=0;

	virtual CLTGUIFont		*GetTitleFont()=0;
	virtual CLTGUIFont		*GetSmallFont()=0;
	virtual CLTGUIFont		*GetLargeFont()=0;

	virtual CLTGUIFont		*GetLargeFadeFonts()=0;

	virtual HSURFACE		GetSurfaceUpArrow()=0;
	virtual HSURFACE		GetSurfaceDownArrow()=0;
    virtual LTBOOL           IsLowResolution()=0;

protected:
    LTBOOL           m_bInit;
    LTBOOL           m_bBuilt;

    ILTClient       *m_pClientDE;       // Client interface

    uint32          m_dwMenuID;         // The ID of this menu
	CMenuBase		*m_pParentMenu;		// The parent menu

	HSURFACE		m_hTitleSurf;		// The title surface
	HSTRING			m_hTitleString;		// The title string

    LTIntPt          m_titlePos;         // The title position
	int				m_nTitleAlign;		// Center the title on/off
    LTBOOL           m_bTitleSolid;      // Draw the title string using solid font

	CLTGUIListCtrl	m_listOption;		// The list of menu items

    LTBOOL           m_bBoxFormat;       // Should we format the list options in a box? or have free locations

    LTBOOL           m_bShowArrows;      // TRUE if the up/down arrows should be drawn
	int				m_nArrowCenter;		// The X coordinate of where the arrow center should be
	int				m_nUpAdjust;		// The Y coordinate adjustment for rendering the up arrow
	int				m_nDownAdjust;		// The Y coordinate adjustment for rendering the up arrow

    LTBOOL           m_bScrollingUp;     // TRUE if we are scrolling up from an arrow click
    LTBOOL           m_bScrollingDown;   // TRUE if we are scrolling down from an arrow click
	int				m_nMaxScrollRate;	// The max number of items per second to scroll
	float			m_fLastScrollTime;	// The last time that the menu was scrolled
    HLTCOLOR        m_hTransparentColor; // The transparent color

    HLTCOLOR        m_hTitleColor;       // Color to use for title
    HLTCOLOR        m_hSelectedColor;    // Selected option color
    HLTCOLOR        m_hNonSelectedColor; // UnSelected option color
    HLTCOLOR        m_hDisabledColor;    // Disabled option color

	// Array of controls that this menu owns
	CMoArray<CLTGUICtrl *>	m_controlArray;

	// Stores controls added by AddLargeTextItemOption
	CMoArray<CLTGUITextItemCtrl *>	m_largeFontItemArray;
};

inline CLTGUIFadeItemCtrl *CMenuBase::AddFadeItemOption(char *lpszOptionSurfPrefix, int nSurfaces, char *lpszOptionText, DWORD dwCommandID, int xPos, int yPos, DWORD dwParam1, DWORD dwParam2)
{
	HSTRING hString=m_pClientDE->CreateString(lpszOptionText);
	CLTGUIFadeItemCtrl *pCtrl=AddFadeItemOption(lpszOptionSurfPrefix, nSurfaces, hString, dwCommandID, xPos, yPos, dwParam1, dwParam2);
	m_pClientDE->FreeString(hString);

	return pCtrl;
}

inline CLTGUITextItemCtrl *CMenuBase::AddTextItemOption(char *lpszText, DWORD dwCommandID, CLTGUIFont *pFont, LTBOOL bDrawSolid, int *pnValue, DWORD dwParam1, DWORD dwParam2)
{
	HSTRING hString=m_pClientDE->CreateString(lpszText);
	CLTGUITextItemCtrl *pCtrl=AddTextItemOption(hString, dwCommandID, pFont, bDrawSolid, pnValue, dwParam1, dwParam2);
	m_pClientDE->FreeString(hString);

	return pCtrl;
}


inline CLTGUISliderCtrl *CMenuBase::AddSliderOption(char *lpszText, CLTGUIFont *pFont, int nSliderOffset, HSURFACE hBarSurf, HSURFACE hTabSurf, LTBOOL bNumDispaly, int *pnValue)
{
	HSTRING hString=m_pClientDE->CreateString(lpszText);
	CLTGUISliderCtrl *pCtrl=AddSliderOption(hString, pFont, nSliderOffset, hBarSurf, hTabSurf, bNumDispaly, pnValue);
	m_pClientDE->FreeString(hString);

	return pCtrl;
}

inline CLTGUIOnOffCtrl *CMenuBase::AddOnOffOption(char *lpszText, CLTGUIFont *pFont, int nRightColumnOffset, LTBOOL *pbValue)
{
	HSTRING hString=m_pClientDE->CreateString(lpszText);
	CLTGUIOnOffCtrl *pCtrl=AddOnOffOption(hString, pFont, nRightColumnOffset, pbValue);
	m_pClientDE->FreeString(hString);

	return pCtrl;
}

inline CLTGUIEditCtrl *CMenuBase::AddEditOption(char *lpszDescription, DWORD dwCommandID, CLTGUIFont *pFont, int nEditStringOffset, int nBufferSize, char *lpszValue, DWORD dwParam1, DWORD dwParam2)
{
	HSTRING hString=m_pClientDE->CreateString(lpszDescription);
	CLTGUIEditCtrl *pCtrl=AddEditOption(hString, dwCommandID, pFont, nEditStringOffset, nBufferSize, lpszValue, dwParam1, dwParam2);
	m_pClientDE->FreeString(hString);

	return pCtrl;
}

inline CLTGUICycleCtrl *CMenuBase::AddCycleOption(char *lpszText, CLTGUIFont *pFont, int nHeaderWidth, int nSpacerWidth, int *pnValue)
{
	HSTRING hString=m_pClientDE->CreateString(lpszText);
	CLTGUICycleCtrl *pCtrl=AddCycleOption(hString, pFont, nHeaderWidth, nSpacerWidth, pnValue);
	m_pClientDE->FreeString(hString);

	return pCtrl;
}

inline CLTGUITextItemCtrl *CMenuBase::AddLargeTextItemOption(char *lpszText, DWORD dwCommandID, LTBOOL bUseBitmapFontArray, DWORD dwFadeTime, DWORD dwParam1, DWORD dwParam2)
{
	HSTRING hString=m_pClientDE->CreateString(lpszText);
	CLTGUITextItemCtrl *pCtrl=AddLargeTextItemOption(hString, dwCommandID, bUseBitmapFontArray, dwFadeTime, dwParam1, dwParam2);
	m_pClientDE->FreeString(hString);

	return pCtrl;
}

inline CLTGUITextItemCtrl *CMenuBase::AddSmallTextItemOption(char *lpszText, DWORD dwCommandID, LTBOOL bUseBitmapFontArray, DWORD dwFadeTime, DWORD dwParam1, DWORD dwParam2)
{
	HSTRING hString=m_pClientDE->CreateString(lpszText);
	CLTGUITextItemCtrl *pCtrl=AddSmallTextItemOption(hString, dwCommandID, bUseBitmapFontArray, dwFadeTime, dwParam1, dwParam2);
	m_pClientDE->FreeString(hString);

	return pCtrl;
}


#endif // !defined(AFX_MENUBASE_H__D7668B31_57D4_11D2_BDA0_0060971BDC6D__INCLUDED_)