Foundation classes for a basic GUI implementing simple widgets and events

Dependents:   TouchScreenGUIDemo

Widgets/TextWidget.cpp

Committer:
duncanFrance
Date:
2016-03-25
Revision:
0:0a590815d51c
Child:
1:48796b602c86

File content as of revision 0:0a590815d51c:

#include "TextWidget.h"

/**
* A basic widget implementation which just draws some text.
* If the text does not fit in the bounding-box it will be clipped
**/

TextWidget::TextWidget(GraphicsDisplay* display) : Widget(display) {}

void TextWidget::setText(char* text) {
    _text = text;
}

void TextWidget::setFont(unsigned char *font) {
    _font = font;
    _fw = _font[1];
    _fh = _font[2];
}

void TextWidget::draw() {

    int right = _x + _width; // right side of clipping window

    int _cx = _x;
    // Start drawing characters at top left
    int _cy = _y + _height - _fh;

    char c;
    char *p = _text;
    
    _display->foreground(_fg);
    _display->background(_bg);
    
    while(p != NULL) {
        c = *p;
        p++;
        if(c=='\n') {
            _cy = _cy - _fh;
            _cx = _x;
        } else {
            // Only draw the character if it is not clipped
            if( (_cx+_fw) < right && (_cy -_fh) > _y) {
                _display->character(_cx, _cy, c);
                _cx += _fw;
                _cy -= _fh;
            }
        }
    }
}

bool TextWidget::isEventTarget(Event e) {
    return e.screenX < (_x+_width) && _x <= e.screenX && e.screenY < (_y+_height) && _y <= e.screenY;
}