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_radiobutton.cpp

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

#include "gui_radiobutton.h"

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

GRadioButton IMPLEMENTATION

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

/*
==============
GRadioButton::GRadioButton

All radio-buttons start disabled
==============
*/
GRadioButton::GRadioButton(GPanel *pParent, int x, int y, int w, int h,
                           const char *szText, int iAlign, int id)
    : GButton(pParent, x, y, w, h, szText, iAlign, id)
{
    m_bOn = false;
}


GRadioButton::GRadioButton(GPanel *pParent, const char *szPic, int type, u32 colorkey,
            int x, int y, int w, int h, int id)
    : GButton(pParent, szPic, type, colorkey, x, y, w, h, id)
{
    m_bOn = false;
}

/*
==============
GRadioButton::GetIconSize

Return the size of the small icon next to the text
==============
*/
void GRadioButton::GetIconSize(int *pw, int *ph)
{
    *pw = 11;
    *ph = 11;
}

/*
==============
GRadioButton::DrawIcon

Actually draw the icon
==============
*/
void GRadioButton::DrawIcon(int x, int y, int r, int g, int b, int a)
{
    hal->DrawLine(x-5, y-5, x+5, y-5, r, g, b, a);
    hal->DrawLine(x+5, y-5, x+5, y+5, r, g, b, a);
    hal->DrawLine(x+5, y+5, x-5, y+5, r, g, b, a);
    hal->DrawLine(x-5, y+5, x-5, y-5, r, g, b, a);

    if (m_bOn)
        hal->FillRect(x-3, y-3, 7, 7, r, g, b, a);
}

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

GRadioButtonGroup IMPLEMENTATION

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

/*
==============
GRadioButtonGroup::GRadioButtonGroup

Initialize empty
==============
*/
GRadioButtonGroup::GRadioButtonGroup()
{
    numclients = 0;
    clients = 0;
    current = -1;
}

/*
==============
GRadioButtonGroup::~GRadioButtonGroup

Do _not_ free the actual radio buttons, since we don't own them
==============
*/
GRadioButtonGroup::~GRadioButtonGroup()
{
    if (clients)
        L_Free(clients);
}

/*
==============
GRadioButtonGroup::Add

Append a radiobutton to the list and wire it for cooperation,
return its (0-based) id.
==============
*/
int GRadioButtonGroup::Add(GRadioButton *rb)
{
    int id;

    id = numclients++;

    clients = (GRadioButton **)L_Realloc(clients, sizeof(GRadioButton *)*numclients,
                    TAG_GUI);
    clients[id] = rb;

    rb->SetId(id);
    rb->Clicked.Connect(this, &GRadioButtonGroup::Set);

    return id;
}

/*
==============
GRadioButtonGroup::Set

Set the previous button to off and new one to on
==============
*/
void GRadioButtonGroup::Set(int id)
{
    lassert(id >= 0 && id < numclients);

    if (current >= 0)
        clients[current]->SetOn(false);

    clients[id]->SetOn(true);
    current = id;
}