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
TextWidget.cpp
00001 #include "TextWidget.h" 00002 00003 /** 00004 * A basic widget implementation which just draws some text. 00005 * If the text does not fit in the bounding-box it will be clipped 00006 **/ 00007 TextWidget::TextWidget(GraphicsContext *context) : 00008 Widget(context), 00009 _text(NULL), 00010 _renderer(context->fontRenderer()), 00011 _font(context->defaultFont()), 00012 _halign(LEFT), _valign(TOP) 00013 { 00014 } 00015 00016 TextWidget::TextWidget(GraphicsContext *context, FontRenderer* renderer) : 00017 Widget(context), 00018 _text(NULL), 00019 _renderer(renderer), 00020 _font(context->defaultFont()), 00021 _halign(LEFT), _valign(TOP) 00022 { 00023 } 00024 00025 TextWidget::TextWidget(GraphicsContext *context, FontRenderer* renderer, Font* font) : 00026 Widget(context), 00027 _text(NULL), 00028 _renderer(renderer), 00029 _font(font), 00030 _halign(LEFT), _valign(TOP) 00031 { 00032 } 00033 00034 void TextWidget::setText(char* text) 00035 { 00036 _text = text; 00037 dirty(); 00038 } 00039 00040 void TextWidget::setFont(Font* font) 00041 { 00042 _font = font; 00043 dirty(); 00044 } 00045 00046 Font *TextWidget::getFont() { 00047 return _font; 00048 } 00049 00050 void TextWidget::setHAlign(HAlign alignment) { 00051 _halign = alignment; 00052 dirty(); 00053 } 00054 00055 void TextWidget::setVAlign(VAlign alignment) { 00056 _valign = alignment; 00057 dirty(); 00058 } 00059 00060 void TextWidget::_draw() 00061 { 00062 Widget::_draw(); 00063 00064 /** 00065 * Figure out how many lines of text we have 00066 **/ 00067 int numLines = 1; 00068 char *c = _text; 00069 while(*c != NULL) { 00070 if(*c == '\n') { 00071 numLines++; 00072 } 00073 c++; 00074 } 00075 00076 /******************************************************************/ 00077 /* --------------------------------- ^ */ 00078 /* | | | */ 00079 /* | | | */ 00080 /* | | inner.height */ 00081 /* | | | */ 00082 /* | | | */ 00083 /* | | | */ 00084 /* | | | */ 00085 /* --------------------------------- | */ 00086 /******************************************************************/ 00087 /** 00088 * We need a window as high as the font with it's origin: 00089 * VALIGN=TOP : (0, 0) 00090 * VALIGN=MIDDLE : (0, inner.height/2 - numLines * font.height/2) 00091 * VALIGN=BOTTOM : (0, inner.height - numLines * font.height) 00092 **/ 00093 int offset=0; 00094 switch(_valign) { 00095 case TOP: offset = 0; break; 00096 case MIDDLE: offset = (_inner.height - (numLines * _font->zoomedHeight()))/2; break; 00097 case BOTTOM: offset = (_inner.height - (numLines * _font->zoomedHeight())); break; 00098 } 00099 00100 _renderer->setForeground(_fg); 00101 _renderer->setBackground(_bg); 00102 00103 // Renderer window is only high enough for the number of lines to draw. 00104 int h = _font->zoomedHeight() * numLines; 00105 // Clip to fit within the TextWidget inner 00106 if((h + offset) > _inner.height) { 00107 h = _inner.height - offset; 00108 } 00109 00110 _renderer->window(_inner.x, _inner.y + offset, _inner.width, h, false); 00111 _renderer->puts(_text, display(), _font); 00112 display()->copy_to_lcd(); 00113 }
Generated on Tue Jul 12 2022 22:27:14 by
