Browse Source

Add Widget::hasChild(), addChildBefore(), and addChildAfter().

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
649a601fc2
2 changed files with 42 additions and 0 deletions
  1. +3
    -0
      include/widget/Widget.hpp
  2. +39
    -0
      src/widget/Widget.cpp

+ 3
- 0
include/widget/Widget.hpp View File

@@ -99,11 +99,14 @@ struct Widget : WeakBase {
return NULL;
}

bool hasChild(Widget* child);
/** Adds widget to list of children.
Gives ownership of widget to this widget instance.
*/
void addChild(Widget* child);
void addChildBottom(Widget* child);
void addChildBefore(Widget* child, Widget* sibling);
void addChildAfter(Widget* child, Widget* sibling);
/** Removes widget from list of children if it exists.
Does not delete widget but transfers ownership to caller
*/


+ 39
- 0
src/widget/Widget.cpp View File

@@ -145,9 +145,18 @@ math::Rect Widget::getViewport(math::Rect r) {
}


bool Widget::hasChild(Widget* child) {
if (!child)
return false;
auto it = std::find(children.begin(), children.end(), child);
return (it != children.end());
}


void Widget::addChild(Widget* child) {
assert(child);
assert(!child->parent);
// Add child
child->parent = this;
children.push_back(child);
// Trigger Add event
@@ -159,6 +168,7 @@ void Widget::addChild(Widget* child) {
void Widget::addChildBottom(Widget* child) {
assert(child);
assert(!child->parent);
// Add child
child->parent = this;
children.push_front(child);
// Trigger Add event
@@ -167,6 +177,35 @@ void Widget::addChildBottom(Widget* child) {
}


void Widget::addChildBefore(Widget* child, Widget* sibling) {
assert(child);
assert(!child->parent);
auto it = std::find(children.begin(), children.end(), sibling);
assert(it != children.end());
// Add child
child->parent = this;
children.insert(it, child);
// Trigger Add event
AddEvent eAdd;
child->onAdd(eAdd);
}


void Widget::addChildAfter(Widget* child, Widget* sibling) {
assert(child);
assert(!child->parent);
auto it = std::find(children.begin(), children.end(), sibling);
assert(it != children.end());
// Add child
child->parent = this;
it++;
children.insert(it, child);
// Trigger Add event
AddEvent eAdd;
child->onAdd(eAdd);
}


void Widget::removeChild(Widget* child) {
assert(child);
// Make sure `this` is the child's parent


Loading…
Cancel
Save