Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SimpleGUI by
Widgets/TextWidget.cpp@1:48796b602c86, 2016-03-25 (annotated)
- Committer:
- duncanFrance
- Date:
- Fri Mar 25 16:47:33 2016 +0000
- Revision:
- 1:48796b602c86
- Parent:
- 0:0a590815d51c
- Child:
- 2:bb9183379488
Implement default isEventTarget on Widget
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duncanFrance | 0:0a590815d51c | 1 | #include "TextWidget.h" |
duncanFrance | 0:0a590815d51c | 2 | |
duncanFrance | 0:0a590815d51c | 3 | /** |
duncanFrance | 0:0a590815d51c | 4 | * A basic widget implementation which just draws some text. |
duncanFrance | 0:0a590815d51c | 5 | * If the text does not fit in the bounding-box it will be clipped |
duncanFrance | 0:0a590815d51c | 6 | **/ |
duncanFrance | 0:0a590815d51c | 7 | |
duncanFrance | 0:0a590815d51c | 8 | TextWidget::TextWidget(GraphicsDisplay* display) : Widget(display) {} |
duncanFrance | 0:0a590815d51c | 9 | |
duncanFrance | 1:48796b602c86 | 10 | void TextWidget::setText(char* text) |
duncanFrance | 1:48796b602c86 | 11 | { |
duncanFrance | 0:0a590815d51c | 12 | _text = text; |
duncanFrance | 0:0a590815d51c | 13 | } |
duncanFrance | 0:0a590815d51c | 14 | |
duncanFrance | 1:48796b602c86 | 15 | void TextWidget::setFont(unsigned char* f, unsigned char firstascii, unsigned char lastascii, bool proportional) |
duncanFrance | 1:48796b602c86 | 16 | { |
duncanFrance | 1:48796b602c86 | 17 | _font = f; |
duncanFrance | 1:48796b602c86 | 18 | // read font parameter from start of array in format understood by UniGraphics for now |
duncanFrance | 1:48796b602c86 | 19 | //fontoffset = font[0]; // bytes / char |
duncanFrance | 1:48796b602c86 | 20 | _fontWidth = _font[1]; |
duncanFrance | 1:48796b602c86 | 21 | _fontHeight = _font[2]; |
duncanFrance | 1:48796b602c86 | 22 | //fontbpl = font[3]; // bytes per line |
duncanFrance | 1:48796b602c86 | 23 | _fontBytesPerLine = (_fontHeight+7) >> 3; //bytes per line, rounded up to multiple of 8 |
duncanFrance | 1:48796b602c86 | 24 | _fontOffset = (_fontWidth * _fontBytesPerLine) + 1; |
duncanFrance | 1:48796b602c86 | 25 | _fontFirstAscii = firstascii; // first ascii code present in font array (usually 32) |
duncanFrance | 1:48796b602c86 | 26 | _fontLastAscii = lastascii; // last ascii code present in font array (usually 127) |
duncanFrance | 1:48796b602c86 | 27 | _fontProportional = proportional; |
duncanFrance | 0:0a590815d51c | 28 | } |
duncanFrance | 0:0a590815d51c | 29 | |
duncanFrance | 1:48796b602c86 | 30 | void TextWidget::draw() |
duncanFrance | 1:48796b602c86 | 31 | { |
duncanFrance | 0:0a590815d51c | 32 | |
duncanFrance | 0:0a590815d51c | 33 | int right = _x + _width; // right side of clipping window |
duncanFrance | 1:48796b602c86 | 34 | int bottom = _y + _height; // bottom edge of clipping window |
duncanFrance | 0:0a590815d51c | 35 | |
duncanFrance | 0:0a590815d51c | 36 | int _cx = _x; |
duncanFrance | 0:0a590815d51c | 37 | // Start drawing characters at top left |
duncanFrance | 1:48796b602c86 | 38 | int _cy = _y; |
duncanFrance | 0:0a590815d51c | 39 | |
duncanFrance | 0:0a590815d51c | 40 | char c; |
duncanFrance | 0:0a590815d51c | 41 | char *p = _text; |
duncanFrance | 1:48796b602c86 | 42 | unsigned char* glyph; |
duncanFrance | 1:48796b602c86 | 43 | uint8_t charWidth; |
duncanFrance | 1:48796b602c86 | 44 | |
duncanFrance | 0:0a590815d51c | 45 | _display->foreground(_fg); |
duncanFrance | 0:0a590815d51c | 46 | _display->background(_bg); |
duncanFrance | 1:48796b602c86 | 47 | _display->set_font(_font); |
duncanFrance | 1:48796b602c86 | 48 | |
duncanFrance | 1:48796b602c86 | 49 | |
duncanFrance | 1:48796b602c86 | 50 | while(*p != NULL) { |
duncanFrance | 0:0a590815d51c | 51 | c = *p; |
duncanFrance | 0:0a590815d51c | 52 | p++; |
duncanFrance | 0:0a590815d51c | 53 | if(c=='\n') { |
duncanFrance | 1:48796b602c86 | 54 | _cy = _cy + _fontHeight; |
duncanFrance | 0:0a590815d51c | 55 | _cx = _x; |
duncanFrance | 0:0a590815d51c | 56 | } else { |
duncanFrance | 0:0a590815d51c | 57 | // Only draw the character if it is not clipped |
duncanFrance | 1:48796b602c86 | 58 | if( (_cx+_fontWidth) < right && (_cy +_fontHeight) < bottom) { |
duncanFrance | 0:0a590815d51c | 59 | _display->character(_cx, _cy, c); |
duncanFrance | 1:48796b602c86 | 60 | glyph = &_font[((c - _fontFirstAscii) * _fontOffset) + 4]; // start of char bitmap |
duncanFrance | 1:48796b602c86 | 61 | charWidth = glyph[0]; // width of actual char |
duncanFrance | 1:48796b602c86 | 62 | |
duncanFrance | 1:48796b602c86 | 63 | if(_fontProportional) { |
duncanFrance | 1:48796b602c86 | 64 | if((charWidth+1) < _fontWidth) { |
duncanFrance | 1:48796b602c86 | 65 | _cx += charWidth + 1; // put at least 1 blank after variable-width characters, except characters that occupy whole fonthor space like "_" |
duncanFrance | 1:48796b602c86 | 66 | } else { |
duncanFrance | 1:48796b602c86 | 67 | _cx += _fontWidth; |
duncanFrance | 1:48796b602c86 | 68 | } |
duncanFrance | 1:48796b602c86 | 69 | } else { |
duncanFrance | 1:48796b602c86 | 70 | _cx += _fontWidth; // fixed width |
duncanFrance | 1:48796b602c86 | 71 | } |
duncanFrance | 0:0a590815d51c | 72 | } |
duncanFrance | 0:0a590815d51c | 73 | } |
duncanFrance | 0:0a590815d51c | 74 | } |
duncanFrance | 0:0a590815d51c | 75 | } |
duncanFrance | 0:0a590815d51c | 76 | |
duncanFrance | 1:48796b602c86 | 77 | bool TextWidget::isEventTarget(Event e) |
duncanFrance | 1:48796b602c86 | 78 | { |
duncanFrance | 0:0a590815d51c | 79 | return e.screenX < (_x+_width) && _x <= e.screenX && e.screenY < (_y+_height) && _y <= e.screenY; |
duncanFrance | 0:0a590815d51c | 80 | } |