| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Benjamin Grauer |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_GUI |
|---|
| 17 | |
|---|
| 18 | #include "glgui_box.h" |
|---|
| 19 | #include <cassert> |
|---|
| 20 | #include "debug.h" |
|---|
| 21 | |
|---|
| 22 | namespace OrxGui |
|---|
| 23 | { |
|---|
| 24 | ObjectListDefinition(GLGuiBox); |
|---|
| 25 | /** |
|---|
| 26 | * standard constructor |
|---|
| 27 | */ |
|---|
| 28 | GLGuiBox::GLGuiBox (OrxGui::Orientation orientation) |
|---|
| 29 | { |
|---|
| 30 | this->init(); |
|---|
| 31 | |
|---|
| 32 | this->setOrientation(orientation); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | /** |
|---|
| 37 | * standard deconstructor |
|---|
| 38 | */ |
|---|
| 39 | GLGuiBox::~GLGuiBox() |
|---|
| 40 | { |
|---|
| 41 | // unpack all the widgets. |
|---|
| 42 | while(!this->_children.empty()) |
|---|
| 43 | { |
|---|
| 44 | /// not deleting children here. |
|---|
| 45 | this->_children.front()->setParentWidget(NULL); |
|---|
| 46 | this->_children.pop_front(); |
|---|
| 47 | } |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | /** |
|---|
| 51 | * initializes the GUI-element |
|---|
| 52 | */ |
|---|
| 53 | void GLGuiBox::init() |
|---|
| 54 | { |
|---|
| 55 | this->registerObject(this, GLGuiBox::_objectList); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void GLGuiBox::pack(GLGuiWidget* widget) |
|---|
| 59 | { |
|---|
| 60 | assert (widget != NULL); |
|---|
| 61 | this->_children.push_back(widget); |
|---|
| 62 | |
|---|
| 63 | this->packing(widget); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | void GLGuiBox::pack(GLGuiWidget* widget, std::list<GLGuiWidget*>::iterator pos) |
|---|
| 68 | { |
|---|
| 69 | this->_children.insert(pos, widget); |
|---|
| 70 | this->packing(widget); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void GLGuiBox::pack(GLGuiWidget* widget, unsigned int position) |
|---|
| 74 | { |
|---|
| 75 | if (this->_children.empty()) |
|---|
| 76 | this->pack(widget); |
|---|
| 77 | |
|---|
| 78 | unsigned int pos = 0; |
|---|
| 79 | std::list<GLGuiWidget*>::iterator it = this->_children.begin(); |
|---|
| 80 | |
|---|
| 81 | for (; pos < position; ++pos) |
|---|
| 82 | { |
|---|
| 83 | if (this->_children.end() == ++it) |
|---|
| 84 | { |
|---|
| 85 | PRINTF(2)("Reached end of packing list, without getting to the designated position %d (i am at %d)\n", position, pos); |
|---|
| 86 | this->pack(widget); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | this->_children.insert(it, widget); |
|---|
| 90 | this->packing(widget); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | void GLGuiBox::pack(GLGuiWidget* widget, const GLGuiWidget* widgetPointer) |
|---|
| 94 | { |
|---|
| 95 | assert (widget != NULL && widgetPointer != NULL); |
|---|
| 96 | |
|---|
| 97 | std::list<GLGuiWidget*>::iterator it = this->_children.begin(); |
|---|
| 98 | for (; it != this->_children.end(); ++it) |
|---|
| 99 | { |
|---|
| 100 | if (widgetPointer == *it) |
|---|
| 101 | { |
|---|
| 102 | this->_children.insert(it, widget); |
|---|
| 103 | this->packing(widget); |
|---|
| 104 | return; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | PRINTF(2)("WidgetPointer %p not found, inserting at the end\n", widgetPointer); |
|---|
| 108 | this->pack(widget); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void GLGuiBox::packFront(GLGuiWidget* widget) |
|---|
| 112 | { |
|---|
| 113 | this->_children.push_front(widget); |
|---|
| 114 | this->packing(widget); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void GLGuiBox::packBack(GLGuiWidget* widget) |
|---|
| 118 | { |
|---|
| 119 | this->pack(widget); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | void GLGuiBox::packing(GLGuiWidget* widget) |
|---|
| 123 | { |
|---|
| 124 | widget->setParentWidget(this); |
|---|
| 125 | this->resize(); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void GLGuiBox::unpack(GLGuiWidget* widget) |
|---|
| 129 | { |
|---|
| 130 | assert(widget != NULL); |
|---|
| 131 | |
|---|
| 132 | std::list<GLGuiWidget*>::iterator delWidget = std::find(this->_children.begin(), this->_children.end(), widget); |
|---|
| 133 | if (delWidget != this->_children.end()) |
|---|
| 134 | { |
|---|
| 135 | (*delWidget)->setParentWidget(NULL); |
|---|
| 136 | this->_children.erase(delWidget); |
|---|
| 137 | } |
|---|
| 138 | this->resize(); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | void GLGuiBox::clear() |
|---|
| 142 | { |
|---|
| 143 | this->_children.clear(); |
|---|
| 144 | this->resize(); |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | void GLGuiBox::showAll() |
|---|
| 148 | { |
|---|
| 149 | std::list<GLGuiWidget*>::iterator itC = this->_children.begin(); |
|---|
| 150 | while (itC != this->_children.end()) |
|---|
| 151 | { |
|---|
| 152 | if ((*itC)->isA(GLGuiContainer::staticClassID())) |
|---|
| 153 | static_cast<GLGuiContainer*>(*itC)->showAll(); |
|---|
| 154 | else |
|---|
| 155 | (*itC)->show(); |
|---|
| 156 | itC++; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | this->show(); |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | void GLGuiBox::hideAll() |
|---|
| 163 | { |
|---|
| 164 | std::list<GLGuiWidget*>::iterator itC = this->_children.begin(); |
|---|
| 165 | while (itC != this->_children.end()) |
|---|
| 166 | { |
|---|
| 167 | if ((*itC)->isA(GLGuiContainer::staticClassID())) |
|---|
| 168 | static_cast<GLGuiContainer*>(*itC)->hideAll(); |
|---|
| 169 | else |
|---|
| 170 | (*itC)->hide(); |
|---|
| 171 | itC++; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | this->hide(); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | void GLGuiBox::resize() |
|---|
| 178 | { |
|---|
| 179 | if (orientation() == OrxGui::Vertical) |
|---|
| 180 | { |
|---|
| 181 | float height = borderTop(); |
|---|
| 182 | float width = 0.0f; |
|---|
| 183 | std::list<GLGuiWidget*>::iterator widget; |
|---|
| 184 | |
|---|
| 185 | // find out how big the Widgets are. |
|---|
| 186 | for (widget = this->_children.begin(); widget != this->_children.end(); ++widget) |
|---|
| 187 | { |
|---|
| 188 | (*widget)->setRelCoor2D(borderLeft(), height); |
|---|
| 189 | height += (*widget)->getSizeY2D(); |
|---|
| 190 | width = fmax(width, (*widget)->getSizeX2D()); |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | width += borderLeft() + borderRight(); |
|---|
| 194 | height += borderBottom(); /* *2 done further up */ |
|---|
| 195 | |
|---|
| 196 | this->setSize2D(width, height); |
|---|
| 197 | } |
|---|
| 198 | else |
|---|
| 199 | { |
|---|
| 200 | float height = borderTop(); |
|---|
| 201 | float width = borderLeft(); |
|---|
| 202 | std::list<GLGuiWidget*>::iterator widget; |
|---|
| 203 | |
|---|
| 204 | // find out how big the Widgets are. |
|---|
| 205 | for (widget = this->_children.begin(); widget != this->_children.end(); ++widget) |
|---|
| 206 | { |
|---|
| 207 | (*widget)->setRelCoor2D(width, borderTop()); |
|---|
| 208 | height = fmax(height, (*widget)->getSizeY2D()); |
|---|
| 209 | width += (*widget)->getSizeX2D(); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | width += borderRight() ; |
|---|
| 213 | height += borderBottom(); /* *2 done further up */ |
|---|
| 214 | |
|---|
| 215 | this->setSize2D(width, height); |
|---|
| 216 | } |
|---|
| 217 | GLGuiWidget::resize(); |
|---|
| 218 | |
|---|
| 219 | // resize everything. |
|---|
| 220 | //for (widget = this->_children.begin(); widget != this->_children.end(); ++widget) |
|---|
| 221 | //{} |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | /** |
|---|
| 225 | * @brief draws the GLGuiBox |
|---|
| 226 | */ |
|---|
| 227 | void GLGuiBox::draw() const |
|---|
| 228 | { |
|---|
| 229 | this->beginDraw(); |
|---|
| 230 | GLGuiWidget::draw(); |
|---|
| 231 | this->endDraw(); |
|---|
| 232 | } |
|---|
| 233 | } |
|---|