el h / SimpleGUI

Fork of SimpleGUI by Duncan McIntyre

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ContainerWidget.cpp Source File

ContainerWidget.cpp

00001 #include "ContainerWidget.h"
00002 
00003 ContainerWidget::ContainerWidget(GraphicsContext *context)
00004     :
00005     Window(context), _layout(HORIZONTAL)
00006 {
00007 }
00008 
00009 void ContainerWidget::setLayout(Layout l)
00010 {
00011     if(_layout != l) {
00012         _layout = l;
00013         damage();
00014     }
00015 }
00016 
00017 void ContainerWidget::setSize(int width, int height)
00018 {
00019     if(_minWidth != width || _minHeight != height) {
00020         _minWidth = width;
00021         _minHeight = height;
00022         damage();
00023     }
00024     Window::setSize(width, height);
00025 }
00026 
00027 void ContainerWidget::attach(Widget *child)
00028 {
00029     Window::attach(child);
00030     if(_layout == FIXED) {
00031         // Re-interpret child's location referenced to the container
00032         child->setOffset(x(), y());
00033     }
00034 }
00035 
00036 void ContainerWidget::_adjust()
00037 {
00038     setSize(_minWidth, _minHeight);
00039     Window::_adjust();
00040 
00041     int wx = _inner.x;
00042     int wy = _inner.y;
00043     int width = 0;
00044     int height = 0;
00045 
00046 
00047 
00048     Widget *w;
00049 
00050     _widgets.reset();
00051 
00052     while((w = _widgets.next()) != NULL) {
00053 
00054         if(_layout == FIXED) {
00055             w->setOffset(wx, wy);
00056         } else {
00057 
00058             // Position the widgets
00059             w->setLocation(wx, wy);
00060 
00061             if(_layout == HORIZONTAL) {
00062                 wx += w->width();
00063                 if(w->height() > height) {
00064                     height = w->height();
00065                 }
00066             } else {
00067                 wy += w->height();
00068                 if(w->width() > width) {
00069                     width = w->width();
00070                 }
00071             }
00072         }
00073     }
00074 
00075     if(_layout != FIXED) {
00076 
00077         int neededWidth;
00078         int neededHeight;
00079 
00080         if(_layout == HORIZONTAL) {
00081             neededWidth = _padding + _borderWidth + wx - _outer.x;
00082             neededHeight = 2 * (_padding + _borderWidth) + height;
00083         } else {
00084             neededWidth = 2 * (_padding + _borderWidth) + width;
00085             neededHeight = _padding + _borderWidth + wy - _outer.y;
00086         }
00087 
00088         if(neededWidth > _outer.width) {
00089             setWidth(neededWidth);
00090         }
00091 
00092         if(neededHeight > _outer.height) {
00093             setHeight(neededHeight);
00094         }
00095 
00096         if(_layout == VERTICAL_CENTER) {
00097             // layout again to center the widgets
00098             int center = (_outer.width/2) + _outer.x;
00099             _widgets.reset();
00100             while((w = _widgets.next()) != NULL) {
00101                 w->setLocation(center - (w->width() / 2), w->y());
00102             }
00103         }
00104     }
00105 
00106     Window::_adjust();
00107 }