summaryrefslogtreecommitdiff
path: root/plugingui/checkbox.cc
diff options
context:
space:
mode:
Diffstat (limited to 'plugingui/checkbox.cc')
-rw-r--r--plugingui/checkbox.cc169
1 files changed, 97 insertions, 72 deletions
diff --git a/plugingui/checkbox.cc b/plugingui/checkbox.cc
index faf3741..60ed7a6 100644
--- a/plugingui/checkbox.cc
+++ b/plugingui/checkbox.cc
@@ -30,101 +30,126 @@
#include <stdio.h>
-GUI::CheckBox::CheckBox(Widget *parent)
- : GUI::Widget(parent),
- bg_on(":switch_back_on.png"), bg_off(":switch_back_off.png"),
- knob(":switch_front.png")
+namespace GUI {
+
+CheckBox::CheckBox(Widget* parent)
+ : Widget(parent)
+ , bg_on(":switch_back_on.png")
+ , bg_off(":switch_back_off.png")
+ , knob(":switch_front.png")
+ , state(false)
+ , middle(false)
{
- middle = false;
- state = false;
- handler = NULL;
}
-void GUI::CheckBox::buttonEvent(ButtonEvent *e)
+void CheckBox::buttonEvent(ButtonEvent* buttonEvent)
{
- if(e->direction == -1 || e->doubleclick) {
- state = !state;
- middle = false;
- if(handler) handler(ptr);
- } else {
- middle = true;
- }
-
- repaintEvent(NULL);
+ if((buttonEvent->direction == Direction::up) || buttonEvent->doubleClick)
+ {
+ buttonDown = false;
+ middle = false;
+ if(inCheckbox)
+ {
+ internalSetChecked(!state);
+ }
+ }
+ else
+ {
+ buttonDown = true;
+ middle = true;
+ }
+
+ repaintEvent(nullptr);
}
-void GUI::CheckBox::setText(std::string text)
+void CheckBox::setText(std::string text)
{
- _text = text;
- repaintEvent(NULL);
+ _text = text;
+ repaintEvent(nullptr);
}
-void GUI::CheckBox::registerClickHandler(void (*handler)(void *), void *ptr)
+void CheckBox::keyEvent(KeyEvent* keyEvent)
{
- this->handler = handler;
- this->ptr = ptr;
+ if(keyEvent->keycode == Key::character && keyEvent->text == " ")
+ {
+ if(keyEvent->direction == Direction::up)
+ {
+ middle = false;
+ internalSetChecked(!state);
+ }
+ else
+ {
+ middle = true;
+ }
+
+ repaintEvent(nullptr);
+ }
}
-void GUI::CheckBox::keyEvent(KeyEvent *e)
+void CheckBox::repaintEvent(RepaintEvent* repaintEvent)
{
- if(e->keycode == GUI::KeyEvent::KEY_CHARACTER && e->text == " ") {
- if(e->direction == -1) {
- state = !state;
- middle = false;
- } else {
- middle = true;
- }
-
- repaintEvent(NULL);
- }
+ Painter p(*this);
+
+ p.clear();
+
+ p.drawImage(0, (knob.height() - bg_on.height()) / 2, state ? bg_on : bg_off);
+
+ if(middle)
+ {
+ p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, knob);
+ return;
+ }
+
+ if(state)
+ {
+ p.drawImage(bg_on.width() - 40 + 2, 0, knob);
+ }
+ else
+ {
+ p.drawImage(0, 0, knob);
+ }
}
-void GUI::CheckBox::repaintEvent(GUI::RepaintEvent *e)
+bool CheckBox::checked()
{
- Painter p(this);
-
- p.clear();
-
- if(state) {
- p.drawImage(0, (knob.height() - bg_on.height()) / 2, &bg_on);
- if(middle) p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, &knob);
- else p.drawImage(bg_on.width() - 40 + 2, 0, &knob);
- } else {
- p.drawImage(0, (knob.height() - bg_off.height()) / 2, &bg_off);
- if(middle) p.drawImage((bg_on.width() - knob.width()) / 2 + 1, 0, &knob);
- else p.drawImage(0, 0, &knob);
- }
- /*
- p.setColour(Colour(1));
- Font font;
- p.drawText(box + 8, height() / 2 + 5, font, _text);
- */
+ return state;
}
-bool GUI::CheckBox::checked()
+void CheckBox::setChecked(bool c)
{
- return state;
+ internalSetChecked(c);
}
-void GUI::CheckBox::setChecked(bool c)
+void CheckBox::mouseLeaveEvent()
{
- state = c;
- repaintEvent(NULL);
+ inCheckbox = false;
+ if(buttonDown)
+ {
+ middle = false;
+ repaintEvent(nullptr);
+ }
}
-#ifdef TEST_CHECKBOX
-//Additional dependency files
-//deps:
-//Required cflags (autoconf vars may be used)
-//cflags:
-//Required link options (autoconf vars may be used)
-//libs:
-#include "test.h"
-
-TEST_BEGIN;
-
-// TODO: Put some testcode here (see test.h for usable macros).
+void CheckBox::mouseEnterEvent()
+{
+ inCheckbox = true;
+ if(buttonDown)
+ {
+ middle = true;
+ repaintEvent(nullptr);
+ }
+}
-TEST_END;
+void CheckBox::internalSetChecked(bool checked)
+{
+ if(checked == state)
+ {
+ return;
+ }
+
+ state = checked;
+ stateChangedNotifier(state);
+ repaintEvent(nullptr);
+}
-#endif/*TEST_CHECKBOX*/
+} // GUI::