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_button.cpp -- button implementation

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

#include "gui_button.h"

/*
==============
GButton::GButton

Initialize the button
==============
*/
GButton::GButton(GPanel *pParent, int x, int y, int w, int h, const char *szText,
                 int iAlign, int id)
    : GLabel(pParent, x, y, w, h, szText, iAlign)
{
    int i;

    SetCanFocus(true);

    for(i = 0; i < 4; i++) {
        m_disabledcolor[i] = 255;
        m_focuscolor[i] = 255;
        m_mouseovercolor[i] = 255;
        m_clickedcolor[i] = 255;
    }

    m_id = id;
}

GButton::GButton(GPanel *pParent, const char *szPic, int type, u32 colorkey,
            int x, int y, int w, int h, int id)
    : GLabel(pParent, szPic, type, colorkey, x, y, w, h)
{
    int i;

    SetCanFocus(true);

    for(i = 0; i < 4; i++) {
        m_disabledcolor[i] = 255;
        m_focuscolor[i] = 255;
        m_mouseovercolor[i] = 255;
        m_clickedcolor[i] = 255;
    }

    m_id = id;
}


/*
===============
GButton::Fire

Causes the button to fire as if it was pressed.
===============
*/
void GButton::Fire()
{
    if (IsOblivious())
        return;

    Clicked.Emit(m_id);
}


/*
==============
GButton::Draw

Overridden to pick the correct color depending on the current state
==============
*/
void GButton::Draw()
{
    byte *color;

    if (IsOblivious()) {
        //L_Printf("%s: oblivious\n", m_pszText);
        color = m_disabledcolor;
    } else if (GrabbedMouse()) {
        //L_Printf("%s: grabbed mouse\n", m_pszText);
        color = m_clickedcolor;
    } else if (IsFocussed()) {
        //L_Printf("%s: focussed\n", m_pszText);
        color = m_focuscolor;
    } else if (IsMouseOver()) {
        //L_Printf("%s: mouseover\n", m_pszText);
        color = m_mouseovercolor;
    } else
        color = m_color;

    doDraw(color[0], color[1], color[2], color[3]);
}

/*
==============
GButton::Key

Enter triggers the button action
==============
*/
bool GButton::Key(int code, char c, bool down)
{
    if (!down)
        return false;

    if (code == KEY_RETURN) {
        Fire();
        return true;
    }

    return false;
}

/*
==============
GButton::MouseButton

Handle mouse button clicks.
==============
*/
bool GButton::MouseButton(int btn, int x, int y, bool down)
{
    if (btn != 0)
        return false;

    if (down) {
        GrabMouse(true);
    } else {
        if (GrabbedMouse()) {
            GrabMouse(false);

            if (IsMouseOver())
                Fire();
        }
    }

    return true;
}