If you have comments or questions concerning this source file, discuss them in the forum.
/*
Copyright (c) 2002 Nicolai Haehnle

See the license.txt for details. If that file was not included in the
source distributions, please email <prefect@rtts.org>
*/
// gui_menu.cpp -- menu implementation

#include "library.h"
#include "hal.h"

#include "gui_menu.h"


/*
==============================================================================

GMenuItem IMPLEMENTATION

==============================================================================
*/

GMenuItem::GMenuItem(const char *pszString)
{
    m_pszString = L_Strdup(pszString, TAG_GUI);
    m_pFont = HFont::Get("font");
}

GMenuItem::~GMenuItem()
{
    L_Free(m_pszString);
    m_pFont->Release();
}

int GMenuItem::GetWidth()
{
    int w;
    m_pFont->StringSize(m_pszString, -1, 0, &w, 0);
    return w;
}

int GMenuItem::GetHeight()
{
    int h;
    m_pFont->StringSize(m_pszString, -1, 0, 0, &h);
    return h;
}

void GMenuItem::Draw(int ofsx, int ofsy, bool highlighted)
{
    int r, g, b;

    if (!highlighted) {
        r = 255;
        g = 255;
        b = 255;
    } else {
        r = 255;
        g = 255;
        b = 0;
    }

    m_pFont->DrawString(ofsx, ofsy, m_pszString, 0, -1, 0, r, g, b);
}


/*
==============================================================================

GMenu IMPLEMENTATION

==============================================================================
*/

/*
===============
GMenu::GMenu

Zero-initialize the menu
===============
*/
GMenu::GMenu(GPanel *pParent, int x, int y, int w, int h)
    : GPanel(pParent, x, y, w, h)
{
}

/*
===============
GMenu::~GMenu

Cleanup everything
===============
*/
GMenu::~GMenu()
{
    while(m_MenuItems.size())
        RemoveItem(m_MenuItems[0]);
}

/*
===============
GMenu::AddItem

Create a new menu item. Returns a pointer to the item descriptor
===============
*/
GMenuItem *GMenu::AddItem(const char *pszString)
{
    GMenuItem *pItem = new GMenuItem(pszString);
    m_MenuItems.push_back(pItem);
    return pItem;
}

/*
===============
GMenu::RemoveItem

Remove the given menu item from the list
===============
*/
void GMenu::RemoveItem(GMenuItem *pItem)
{
    GMenuItem::v_it pos = std::find(m_MenuItems.begin(), m_MenuItems.end(), pItem);
    assert(pos != m_MenuItems.end());

    m_MenuItems.erase(pos);
    delete pItem;
}