Richard Parker / EALCD
Revision:
3:24fbf4dbd7e5
Child:
7:6cf21b018420
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/widgets/EAButton.cpp	Wed Mar 31 22:22:21 2010 +0000
@@ -0,0 +1,140 @@
+// Copyright 2010 Richard Parker
+
+#include "mbed.h"
+
+#include "EAButton.h"
+
+#include "../manager/EATouchManager.h"
+#include "../manager/EAHitBox.h"
+#include "../manager/EAHitHandler.h"
+#include "../graphics/EAPen.h"
+#include "../graphics/EAColor.h"
+#include "../screen/EALCD.h"
+
+EAButton::EAButton( EATouchManager& manager, 
+                    short x, 
+                    short y, 
+                    unsigned short width, 
+                    unsigned short height, 
+                    char* text, 
+                    EAHitHandler* handler)
+:   _manager(manager),
+    _box(NULL),
+    _text(NULL)
+{
+    _updateHitBox();
+
+    setX(x);
+    setY(y);
+    setWidth(width);
+    setHeight(height);
+    setText(text);
+    setHandler(handler);    
+}
+
+EAButton::~EAButton()
+{
+    // If created box then delete from manager.
+    if (_box != NULL)
+    {
+        _manager.deleteHitBox(_box);
+        _box = NULL;
+    }
+    
+    if (_text != NULL)
+    {
+        delete[] _text;
+        _text = NULL;
+    }    
+}
+
+void EAButton::paint(EALCD& lcd)
+{
+    lcd.drawFilledRect(x(), y(), width(), height());
+    if (_text != NULL)
+    {
+        lcd.drawText(x()+10, y()+10, _text);
+    }
+}
+
+bool EAButton::setText(char* text)
+{
+    if (text == NULL)
+    {
+        // Invalid text passed in.
+        return false;
+    }
+    
+    // Store the text for later
+    int textLen = strlen(text);
+    
+    // If already have some text then clear to load the new text.
+    if (_text != NULL)
+    {
+        delete[] _text;
+        _text = NULL;
+    }
+    
+    // Now allocate enough space to hold text. Note +1 for null character.
+    _text = new char[textLen+1];
+    
+    // Now copy over passed in text to text variable.
+    strcpy(_text, text);
+    
+    return true;
+}
+
+void EAButton::_updateHitBox()
+{
+    if (_box == NULL)
+    {
+        // Create the hitbox.
+        _box = _manager.createHitBox();
+    }
+    
+    // Update the position of the hit box to match the size and position of the 
+    // drawn button.
+    if (_box != NULL)
+    {
+        _box->setX(x());
+        _box->setY(y());
+        _box->setWidth(width());
+        _box->setHeight(height());
+    }
+}
+
+EAHitHandler* EAButton::handler() const
+{
+    if (_box != NULL)
+    {
+        return _box->handler();
+    } else {
+        return NULL;
+    }
+}
+
+void EAButton::setHandler(EAHitHandler* handler)
+{
+    if (_box != NULL)
+    {
+        _box->setHandler(handler);
+    }
+}
+
+bool EAButton::enabled() const
+{
+    if (_box != NULL)
+    {
+        return _box->enabled();
+    } else {
+        return false;
+    }
+}
+
+void EAButton::setEnabled(bool yes)
+{
+    if (_box != NULL)
+    {
+        _box->setEnabled(yes);
+    }
+}