Skip to content

Commit

Permalink
GUI2/Grid: added child getter by contained widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Vultraz committed Apr 8, 2017
1 parent 8c4e09e commit 2053362
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 26 deletions.
15 changes: 15 additions & 0 deletions src/gui/widgets/grid.cpp
Expand Up @@ -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;
Expand Down
77 changes: 51 additions & 26 deletions src/gui/widgets/grid.hpp
Expand Up @@ -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 ***** ***** ***** *****/

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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);

Expand All @@ -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
{
Expand Down Expand Up @@ -479,15 +476,43 @@ class grid : public widget
* is: rows_ * col + row. All other vectors use the same access formula.
*/
std::vector<child> 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);

Expand Down

0 comments on commit 2053362

Please sign in to comment.