Foundation classes for a basic GUI implementing simple widgets and events
Dependents: TouchScreenGUIDemo
Widgets/ContainerWidget.h@5:b7ce5721a0b5, 2016-03-28 (annotated)
- Committer:
- duncanFrance
- Date:
- Mon Mar 28 10:47:39 2016 +0000
- Revision:
- 5:b7ce5721a0b5
- Child:
- 7:303850a4b30c
Added BitmapWidget
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duncanFrance | 5:b7ce5721a0b5 | 1 | #ifndef SIMPLEGUI_CONTAINER_WIDGET_H |
duncanFrance | 5:b7ce5721a0b5 | 2 | #define SIMPLEGUI_CONTAINER_WIDGET_H |
duncanFrance | 5:b7ce5721a0b5 | 3 | |
duncanFrance | 5:b7ce5721a0b5 | 4 | #include "Widget.h" |
duncanFrance | 5:b7ce5721a0b5 | 5 | |
duncanFrance | 5:b7ce5721a0b5 | 6 | class WidgetList |
duncanFrance | 5:b7ce5721a0b5 | 7 | { |
duncanFrance | 5:b7ce5721a0b5 | 8 | |
duncanFrance | 5:b7ce5721a0b5 | 9 | public: |
duncanFrance | 5:b7ce5721a0b5 | 10 | |
duncanFrance | 5:b7ce5721a0b5 | 11 | WidgetList(Widget* w) : widget(w), next(NULL) {} |
duncanFrance | 5:b7ce5721a0b5 | 12 | |
duncanFrance | 5:b7ce5721a0b5 | 13 | Widget* widget; |
duncanFrance | 5:b7ce5721a0b5 | 14 | WidgetList* next; |
duncanFrance | 5:b7ce5721a0b5 | 15 | |
duncanFrance | 5:b7ce5721a0b5 | 16 | }; |
duncanFrance | 5:b7ce5721a0b5 | 17 | |
duncanFrance | 5:b7ce5721a0b5 | 18 | /** |
duncanFrance | 5:b7ce5721a0b5 | 19 | * Simple container hold widgets side-by-side and draws a border |
duncanFrance | 5:b7ce5721a0b5 | 20 | * It will expand as needed to hold the widgets |
duncanFrance | 5:b7ce5721a0b5 | 21 | **/ |
duncanFrance | 5:b7ce5721a0b5 | 22 | class ContainerWidget : public Widget |
duncanFrance | 5:b7ce5721a0b5 | 23 | { |
duncanFrance | 5:b7ce5721a0b5 | 24 | |
duncanFrance | 5:b7ce5721a0b5 | 25 | public: |
duncanFrance | 5:b7ce5721a0b5 | 26 | |
duncanFrance | 5:b7ce5721a0b5 | 27 | ContainerWidget(GraphicsDisplay* display) : Widget(display), _padding(0), _widgets(NULL) { |
duncanFrance | 5:b7ce5721a0b5 | 28 | } |
duncanFrance | 5:b7ce5721a0b5 | 29 | |
duncanFrance | 5:b7ce5721a0b5 | 30 | /** |
duncanFrance | 5:b7ce5721a0b5 | 31 | * Set the amount of padding between the border and a widget edge |
duncanFrance | 5:b7ce5721a0b5 | 32 | **/ |
duncanFrance | 5:b7ce5721a0b5 | 33 | void setPadding(int pixels) { |
duncanFrance | 5:b7ce5721a0b5 | 34 | if(_padding != pixels) { |
duncanFrance | 5:b7ce5721a0b5 | 35 | _padding = pixels; |
duncanFrance | 5:b7ce5721a0b5 | 36 | adjust(); |
duncanFrance | 5:b7ce5721a0b5 | 37 | } |
duncanFrance | 5:b7ce5721a0b5 | 38 | } |
duncanFrance | 5:b7ce5721a0b5 | 39 | |
duncanFrance | 5:b7ce5721a0b5 | 40 | void setBorder(int width, uint16_t colour) { |
duncanFrance | 5:b7ce5721a0b5 | 41 | _borderColour = colour; |
duncanFrance | 5:b7ce5721a0b5 | 42 | if(_borderWidth != width) { |
duncanFrance | 5:b7ce5721a0b5 | 43 | _borderWidth = width; |
duncanFrance | 5:b7ce5721a0b5 | 44 | adjust(); |
duncanFrance | 5:b7ce5721a0b5 | 45 | } |
duncanFrance | 5:b7ce5721a0b5 | 46 | } |
duncanFrance | 5:b7ce5721a0b5 | 47 | |
duncanFrance | 5:b7ce5721a0b5 | 48 | void append(Widget* widget) { |
duncanFrance | 5:b7ce5721a0b5 | 49 | |
duncanFrance | 5:b7ce5721a0b5 | 50 | WidgetList* w = new WidgetList(widget); |
duncanFrance | 5:b7ce5721a0b5 | 51 | |
duncanFrance | 5:b7ce5721a0b5 | 52 | if(_widgets == NULL) { |
duncanFrance | 5:b7ce5721a0b5 | 53 | _widgets = w; |
duncanFrance | 5:b7ce5721a0b5 | 54 | } else { |
duncanFrance | 5:b7ce5721a0b5 | 55 | _widgets->next = w; |
duncanFrance | 5:b7ce5721a0b5 | 56 | } |
duncanFrance | 5:b7ce5721a0b5 | 57 | |
duncanFrance | 5:b7ce5721a0b5 | 58 | adjust(); |
duncanFrance | 5:b7ce5721a0b5 | 59 | } |
duncanFrance | 5:b7ce5721a0b5 | 60 | |
duncanFrance | 5:b7ce5721a0b5 | 61 | virtual void setLocation(int x, int y) { |
duncanFrance | 5:b7ce5721a0b5 | 62 | Widget::setLocation(x,y); |
duncanFrance | 5:b7ce5721a0b5 | 63 | adjust(); |
duncanFrance | 5:b7ce5721a0b5 | 64 | } |
duncanFrance | 5:b7ce5721a0b5 | 65 | |
duncanFrance | 5:b7ce5721a0b5 | 66 | virtual void setSize(int width, int height) { |
duncanFrance | 5:b7ce5721a0b5 | 67 | Widget::setSize(width, height); |
duncanFrance | 5:b7ce5721a0b5 | 68 | _minWidth = width; |
duncanFrance | 5:b7ce5721a0b5 | 69 | _minHeight = height; |
duncanFrance | 5:b7ce5721a0b5 | 70 | adjust(); |
duncanFrance | 5:b7ce5721a0b5 | 71 | } |
duncanFrance | 5:b7ce5721a0b5 | 72 | |
duncanFrance | 5:b7ce5721a0b5 | 73 | virtual void adjust() { |
duncanFrance | 5:b7ce5721a0b5 | 74 | int wx = _x + _padding + _borderWidth; |
duncanFrance | 5:b7ce5721a0b5 | 75 | int wy = _y + _padding + _borderWidth; |
duncanFrance | 5:b7ce5721a0b5 | 76 | int h = 0; |
duncanFrance | 5:b7ce5721a0b5 | 77 | |
duncanFrance | 5:b7ce5721a0b5 | 78 | _width = _minWidth; |
duncanFrance | 5:b7ce5721a0b5 | 79 | _height = _minHeight; |
duncanFrance | 5:b7ce5721a0b5 | 80 | |
duncanFrance | 5:b7ce5721a0b5 | 81 | WidgetList* p = _widgets; |
duncanFrance | 5:b7ce5721a0b5 | 82 | while(p != NULL) { |
duncanFrance | 5:b7ce5721a0b5 | 83 | // Position the widget |
duncanFrance | 5:b7ce5721a0b5 | 84 | p->widget->setLocation(wx, wy); |
duncanFrance | 5:b7ce5721a0b5 | 85 | wx += p->widget->width(); |
duncanFrance | 5:b7ce5721a0b5 | 86 | if(p->widget->height() > h) { |
duncanFrance | 5:b7ce5721a0b5 | 87 | h = p->widget->height(); |
duncanFrance | 5:b7ce5721a0b5 | 88 | } |
duncanFrance | 5:b7ce5721a0b5 | 89 | |
duncanFrance | 5:b7ce5721a0b5 | 90 | p = p->next; |
duncanFrance | 5:b7ce5721a0b5 | 91 | } |
duncanFrance | 5:b7ce5721a0b5 | 92 | |
duncanFrance | 5:b7ce5721a0b5 | 93 | int neededWidth = _padding + _borderWidth + wx - _x; |
duncanFrance | 5:b7ce5721a0b5 | 94 | int neededHeight = 2 * (_padding + _borderWidth) + h; |
duncanFrance | 5:b7ce5721a0b5 | 95 | |
duncanFrance | 5:b7ce5721a0b5 | 96 | if(neededWidth > _width) { |
duncanFrance | 5:b7ce5721a0b5 | 97 | _width = neededWidth; |
duncanFrance | 5:b7ce5721a0b5 | 98 | } |
duncanFrance | 5:b7ce5721a0b5 | 99 | |
duncanFrance | 5:b7ce5721a0b5 | 100 | if(neededHeight > _height) { |
duncanFrance | 5:b7ce5721a0b5 | 101 | _height = neededHeight; |
duncanFrance | 5:b7ce5721a0b5 | 102 | } |
duncanFrance | 5:b7ce5721a0b5 | 103 | } |
duncanFrance | 5:b7ce5721a0b5 | 104 | |
duncanFrance | 5:b7ce5721a0b5 | 105 | virtual void draw() { |
duncanFrance | 5:b7ce5721a0b5 | 106 | // Draw the border |
duncanFrance | 5:b7ce5721a0b5 | 107 | // Top |
duncanFrance | 5:b7ce5721a0b5 | 108 | _display->fillrect(_x, _y, _x+_width, _y+_borderWidth, _borderColour); |
duncanFrance | 5:b7ce5721a0b5 | 109 | // Bottom |
duncanFrance | 5:b7ce5721a0b5 | 110 | _display->fillrect(_x, _y + _height - _borderWidth, _x + _width, _y+_height, _borderColour); |
duncanFrance | 5:b7ce5721a0b5 | 111 | // Left |
duncanFrance | 5:b7ce5721a0b5 | 112 | _display->fillrect(_x, _y, _x+_borderWidth, _y+_height, _borderColour); |
duncanFrance | 5:b7ce5721a0b5 | 113 | // Right |
duncanFrance | 5:b7ce5721a0b5 | 114 | _display->fillrect(_x+_width-_borderWidth, _y, _x+_width, _y+_height, _borderColour); |
duncanFrance | 5:b7ce5721a0b5 | 115 | |
duncanFrance | 5:b7ce5721a0b5 | 116 | WidgetList* p = _widgets; |
duncanFrance | 5:b7ce5721a0b5 | 117 | while(p != NULL) { |
duncanFrance | 5:b7ce5721a0b5 | 118 | p->widget->draw(); |
duncanFrance | 5:b7ce5721a0b5 | 119 | p = p->next; |
duncanFrance | 5:b7ce5721a0b5 | 120 | } |
duncanFrance | 5:b7ce5721a0b5 | 121 | } |
duncanFrance | 5:b7ce5721a0b5 | 122 | |
duncanFrance | 5:b7ce5721a0b5 | 123 | |
duncanFrance | 5:b7ce5721a0b5 | 124 | protected: |
duncanFrance | 5:b7ce5721a0b5 | 125 | |
duncanFrance | 5:b7ce5721a0b5 | 126 | int _padding; |
duncanFrance | 5:b7ce5721a0b5 | 127 | WidgetList* _widgets; |
duncanFrance | 5:b7ce5721a0b5 | 128 | int _minWidth, _minHeight; |
duncanFrance | 5:b7ce5721a0b5 | 129 | int _borderWidth; |
duncanFrance | 5:b7ce5721a0b5 | 130 | uint16_t _borderColour; |
duncanFrance | 5:b7ce5721a0b5 | 131 | }; |
duncanFrance | 5:b7ce5721a0b5 | 132 | |
duncanFrance | 5:b7ce5721a0b5 | 133 | #endif |