diff --git a/src/gui/widgets/grid.cpp b/src/gui/widgets/grid.cpp index 6a578e318c10..ae04e443ad2f 100644 --- a/src/gui/widgets/grid.cpp +++ b/src/gui/widgets/grid.cpp @@ -931,6 +931,21 @@ point grid::child::border_space() const return result; } +grid::child* grid::get_child(widget* w) +{ + if(!w) { + return nullptr; + } + + for(auto& child : children_) { + if(w == child.get_widget()) { + return &child; + } + } + + return nullptr; +} + void grid::layout(const point& origin) { point orig = origin; diff --git a/src/gui/widgets/grid.hpp b/src/gui/widgets/grid.hpp index ce7714212fae..ac923f355e93 100644 --- a/src/gui/widgets/grid.hpp +++ b/src/gui/widgets/grid.hpp @@ -37,27 +37,25 @@ class grid : public widget virtual ~grid(); /***** ***** ***** ***** LAYOUT FLAGS ***** ***** ***** *****/ - static const unsigned VERTICAL_SHIFT = 0; - static const unsigned VERTICAL_GROW_SEND_TO_CLIENT = 1 << VERTICAL_SHIFT; - static const unsigned VERTICAL_ALIGN_TOP = 2 << VERTICAL_SHIFT; - static const unsigned VERTICAL_ALIGN_CENTER = 3 << VERTICAL_SHIFT; - static const unsigned VERTICAL_ALIGN_BOTTOM = 4 << VERTICAL_SHIFT; - static const unsigned VERTICAL_MASK = 7 << VERTICAL_SHIFT; - - static const unsigned HORIZONTAL_SHIFT = 3; - static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT = 1 - << HORIZONTAL_SHIFT; - static const unsigned HORIZONTAL_ALIGN_LEFT = 2 << HORIZONTAL_SHIFT; - static const unsigned HORIZONTAL_ALIGN_CENTER = 3 << HORIZONTAL_SHIFT; - static const unsigned HORIZONTAL_ALIGN_RIGHT = 4 << HORIZONTAL_SHIFT; - static const unsigned HORIZONTAL_MASK = 7 << HORIZONTAL_SHIFT; - - static const unsigned BORDER_TOP = 1 << 6; - static const unsigned BORDER_BOTTOM = 1 << 7; - static const unsigned BORDER_LEFT = 1 << 8; - static const unsigned BORDER_RIGHT = 1 << 9; - static const unsigned BORDER_ALL = BORDER_TOP | BORDER_BOTTOM | BORDER_LEFT - | BORDER_RIGHT; + static const unsigned VERTICAL_SHIFT = 0; + static const unsigned VERTICAL_GROW_SEND_TO_CLIENT = 1 << VERTICAL_SHIFT; + static const unsigned VERTICAL_ALIGN_TOP = 2 << VERTICAL_SHIFT; + static const unsigned VERTICAL_ALIGN_CENTER = 3 << VERTICAL_SHIFT; + static const unsigned VERTICAL_ALIGN_BOTTOM = 4 << VERTICAL_SHIFT; + static const unsigned VERTICAL_MASK = 7 << VERTICAL_SHIFT; + + static const unsigned HORIZONTAL_SHIFT = 3; + static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT = 1 << HORIZONTAL_SHIFT; + static const unsigned HORIZONTAL_ALIGN_LEFT = 2 << HORIZONTAL_SHIFT; + static const unsigned HORIZONTAL_ALIGN_CENTER = 3 << HORIZONTAL_SHIFT; + static const unsigned HORIZONTAL_ALIGN_RIGHT = 4 << HORIZONTAL_SHIFT; + static const unsigned HORIZONTAL_MASK = 7 << HORIZONTAL_SHIFT; + + static const unsigned BORDER_TOP = 1 << 6; + static const unsigned BORDER_BOTTOM = 1 << 7; + static const unsigned BORDER_LEFT = 1 << 8; + static const unsigned BORDER_RIGHT = 1 << 9; + static const unsigned BORDER_ALL = BORDER_TOP | BORDER_BOTTOM | BORDER_LEFT | BORDER_RIGHT; /***** ***** ***** ***** ROW COLUMN MANIPULATION ***** ***** ***** *****/ @@ -112,7 +110,7 @@ class grid : public widget * * @param widget The widget to put in the grid. * @param row The row of the cell. - * @param col The columnof the cell. + * @param col The column of the cell. * @param flags The flags for the widget in the cell. * @param border_size The size of the border for the cell, how the * border is applied depends on the flags. @@ -147,7 +145,7 @@ class grid : public widget * Removes and frees a widget in a cell. * * @param row The row of the cell. - * @param col The columnof the cell. + * @param col The column of the cell. */ void remove_child(const unsigned row, const unsigned col); @@ -171,7 +169,6 @@ class grid : public widget */ void set_active(const bool active); - /** Returns the widget in the selected cell. */ const widget* get_widget(const unsigned row, const unsigned col) const { @@ -479,15 +476,43 @@ class grid : public widget * is: rows_ * col + row. All other vectors use the same access formula. */ std::vector children_; - const child& get_child(const unsigned row, const unsigned col) const + + /** + * Gets the grid child in the specified cell. + * + * @param row The row of the cell. + * @param col The column of the cell. + * + * @returns A const reference to the specified grid child. + */ + const grid::child& get_child(const unsigned row, const unsigned col) const { return children_[rows_ * col + row]; } - child& get_child(const unsigned row, const unsigned col) + + /** + * Gets the grid child in the specified cell. + * + * @param row The row of the cell. + * @param col The column of the cell. + * + * @returns A const reference to the specified grid child. + */ + grid::child& get_child(const unsigned row, const unsigned col) { return children_[rows_ * col + row]; } + /** + * Gets the grid child containing the specified widget. + * + * @param w The widget to search for. + * + * @returns A pointer to the relevant grid child, or nullptr + * if no grid cell 'owns' the given widget. + */ + grid::child* get_child(widget* w); + /** Layouts the children in the grid. */ void layout(const point& origin);