#include "checkbox.h"

using namespace genv;

/// ---- Események -----------------------
void checkBox::onClick(float posX, float posY, char button)
{
    if ((x+boxX) < posX && posX < (x+boxX+boxW) && (y+boxY) < posY && posY < (y+boxY+boxH))
        checked = !checked;
}

void checkBox::onKeyPress(int keyCode)
{
    if (keyCode == key_space) checked = ! checked;
}

/// ---- Működéshez szükséges függvények ---
checkBox::checkBox(float x0, float y0, float width0, float height0) : widget(x0, y0, width0, height0)
{
    checked = false;
    myLabel = new label(x0, y0, width0, height0);
    myLabel->setMargin(boxX*2 + boxW, boxY + 5);
    myLabel->setText("CheckBox");
    tabStop = true;
}

void checkBox::draw() const
{
    myLabel->draw();
    if (focus) colorize(activeColor);
    else colorize(white);
    gout << move_to(x+boxX, y+boxY) << box(boxW,boxH);
    if (checked)
    {
        colorize(black);
        gout << move_to(x+boxX+5, y+boxY+5) << line(boxW-2*boxX,boxH-2*boxY);
        gout << move_to(x+boxX+5, y-boxY+boxH+5) << line(boxW-2*boxX,-boxH+2*boxX);
    }
}

void checkBox::setText(std::string txt)
{
    myLabel->setText(txt);
}

std::string checkBox::getText() const
{
    return myLabel->getText();
}
