Foundation classes for a basic GUI implementing simple widgets and events
Dependents: TouchScreenGUIDemo
Widgets/ContainerWidget.cpp@12:63db16fea709, 2016-05-08 (annotated)
- Committer:
- duncanFrance
- Date:
- Sun May 08 14:42:08 2016 +0000
- Revision:
- 12:63db16fea709
- Child:
- 13:6714534e7974
Update to handle dirty pixels and geometric change/damage
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
duncanFrance | 12:63db16fea709 | 1 | #include "ContainerWidget.h" |
duncanFrance | 12:63db16fea709 | 2 | |
duncanFrance | 12:63db16fea709 | 3 | ContainerWidget::ContainerWidget(GraphicsContext *context) |
duncanFrance | 12:63db16fea709 | 4 | : |
duncanFrance | 12:63db16fea709 | 5 | Window(context), _layout(HORIZONTAL) |
duncanFrance | 12:63db16fea709 | 6 | { |
duncanFrance | 12:63db16fea709 | 7 | } |
duncanFrance | 12:63db16fea709 | 8 | |
duncanFrance | 12:63db16fea709 | 9 | void ContainerWidget::setLayout(Layout l) |
duncanFrance | 12:63db16fea709 | 10 | { |
duncanFrance | 12:63db16fea709 | 11 | if(_layout != l) { |
duncanFrance | 12:63db16fea709 | 12 | _layout = l; |
duncanFrance | 12:63db16fea709 | 13 | damage(); |
duncanFrance | 12:63db16fea709 | 14 | } |
duncanFrance | 12:63db16fea709 | 15 | } |
duncanFrance | 12:63db16fea709 | 16 | |
duncanFrance | 12:63db16fea709 | 17 | void ContainerWidget::setSize(int width, int height) |
duncanFrance | 12:63db16fea709 | 18 | { |
duncanFrance | 12:63db16fea709 | 19 | if(_minWidth != width || _minHeight != height) { |
duncanFrance | 12:63db16fea709 | 20 | _minWidth = width; |
duncanFrance | 12:63db16fea709 | 21 | _minHeight = height; |
duncanFrance | 12:63db16fea709 | 22 | damage(); |
duncanFrance | 12:63db16fea709 | 23 | } |
duncanFrance | 12:63db16fea709 | 24 | Window::setSize(width, height); |
duncanFrance | 12:63db16fea709 | 25 | } |
duncanFrance | 12:63db16fea709 | 26 | |
duncanFrance | 12:63db16fea709 | 27 | |
duncanFrance | 12:63db16fea709 | 28 | |
duncanFrance | 12:63db16fea709 | 29 | void ContainerWidget::_adjust() |
duncanFrance | 12:63db16fea709 | 30 | { |
duncanFrance | 12:63db16fea709 | 31 | setSize(_minWidth, _minHeight); |
duncanFrance | 12:63db16fea709 | 32 | Window::_adjust(); |
duncanFrance | 12:63db16fea709 | 33 | |
duncanFrance | 12:63db16fea709 | 34 | int wx = _inner.x; |
duncanFrance | 12:63db16fea709 | 35 | int wy = _inner.y; |
duncanFrance | 12:63db16fea709 | 36 | int width = 0; |
duncanFrance | 12:63db16fea709 | 37 | int height = 0; |
duncanFrance | 12:63db16fea709 | 38 | |
duncanFrance | 12:63db16fea709 | 39 | |
duncanFrance | 12:63db16fea709 | 40 | |
duncanFrance | 12:63db16fea709 | 41 | Widget *w; |
duncanFrance | 12:63db16fea709 | 42 | |
duncanFrance | 12:63db16fea709 | 43 | _widgets.reset(); |
duncanFrance | 12:63db16fea709 | 44 | |
duncanFrance | 12:63db16fea709 | 45 | while((w = _widgets.next()) != NULL) { |
duncanFrance | 12:63db16fea709 | 46 | |
duncanFrance | 12:63db16fea709 | 47 | // Position the widgets |
duncanFrance | 12:63db16fea709 | 48 | w->setLocation(wx, wy); |
duncanFrance | 12:63db16fea709 | 49 | |
duncanFrance | 12:63db16fea709 | 50 | if(_layout == HORIZONTAL) { |
duncanFrance | 12:63db16fea709 | 51 | wx += w->width(); |
duncanFrance | 12:63db16fea709 | 52 | if(w->height() > height) { |
duncanFrance | 12:63db16fea709 | 53 | height = w->height(); |
duncanFrance | 12:63db16fea709 | 54 | } |
duncanFrance | 12:63db16fea709 | 55 | } else { |
duncanFrance | 12:63db16fea709 | 56 | wy += w->height(); |
duncanFrance | 12:63db16fea709 | 57 | if(w->width() > width) { |
duncanFrance | 12:63db16fea709 | 58 | width = w->width(); |
duncanFrance | 12:63db16fea709 | 59 | } |
duncanFrance | 12:63db16fea709 | 60 | } |
duncanFrance | 12:63db16fea709 | 61 | } |
duncanFrance | 12:63db16fea709 | 62 | |
duncanFrance | 12:63db16fea709 | 63 | int neededWidth; |
duncanFrance | 12:63db16fea709 | 64 | int neededHeight; |
duncanFrance | 12:63db16fea709 | 65 | |
duncanFrance | 12:63db16fea709 | 66 | if(_layout == HORIZONTAL) { |
duncanFrance | 12:63db16fea709 | 67 | neededWidth = _padding + _borderWidth + wx - _outer.x; |
duncanFrance | 12:63db16fea709 | 68 | neededHeight = 2 * (_padding + _borderWidth) + height; |
duncanFrance | 12:63db16fea709 | 69 | } else { |
duncanFrance | 12:63db16fea709 | 70 | neededWidth = 2 * (_padding + _borderWidth) + width; |
duncanFrance | 12:63db16fea709 | 71 | neededHeight = _padding + _borderWidth + wy - _outer.y; |
duncanFrance | 12:63db16fea709 | 72 | } |
duncanFrance | 12:63db16fea709 | 73 | |
duncanFrance | 12:63db16fea709 | 74 | if(neededWidth > _outer.width) { |
duncanFrance | 12:63db16fea709 | 75 | setWidth(neededWidth); |
duncanFrance | 12:63db16fea709 | 76 | } |
duncanFrance | 12:63db16fea709 | 77 | |
duncanFrance | 12:63db16fea709 | 78 | if(neededHeight > _outer.height) { |
duncanFrance | 12:63db16fea709 | 79 | setHeight(neededHeight); |
duncanFrance | 12:63db16fea709 | 80 | } |
duncanFrance | 12:63db16fea709 | 81 | |
duncanFrance | 12:63db16fea709 | 82 | if(_layout == VERTICAL_CENTER) { |
duncanFrance | 12:63db16fea709 | 83 | // layout again to center the widgets |
duncanFrance | 12:63db16fea709 | 84 | int center = (_outer.width/2) + _outer.x; |
duncanFrance | 12:63db16fea709 | 85 | _widgets.reset(); |
duncanFrance | 12:63db16fea709 | 86 | while((w = _widgets.next()) != NULL) { |
duncanFrance | 12:63db16fea709 | 87 | w->setLocation(center - (w->width() / 2), w->y()); |
duncanFrance | 12:63db16fea709 | 88 | } |
duncanFrance | 12:63db16fea709 | 89 | } |
duncanFrance | 12:63db16fea709 | 90 | |
duncanFrance | 12:63db16fea709 | 91 | Window::_adjust(); |
duncanFrance | 12:63db16fea709 | 92 | } |