Moved child index methods to TBWidget from TBTabContainer.

This commit is contained in:
Emil Segerås 2014-11-02 18:26:46 +01:00
parent f2eb845f72
commit 305fc9e76d
4 changed files with 40 additions and 14 deletions

View File

@ -10,15 +10,6 @@ namespace tb {
// == TBTabLayout =======================================================================
int TBTabLayout::GetIndexFromChild(TBWidget *child) const
{
int index = 0;
for (TBWidget *tmp = GetFirstChild(); tmp; tmp = tmp->GetNext(), index++)
if (tmp == child)
return index;
return -1;
}
void TBTabLayout::OnChildAdded(TBWidget *child)
{
if (TBButton *button = TBSafeCast<TBButton>(child))
@ -103,11 +94,16 @@ void TBTabContainer::SetValue(int index)
int TBTabContainer::GetNumPages()
{
int count = 0;
for (TBWidget *tab = GetTabLayout()->GetFirstChild(); tab; tab = tab->GetNext())
for (TBWidget *tab = m_tab_layout.GetFirstChild(); tab; tab = tab->GetNext())
count++;
return count;
}
TBWidget *TBTabContainer::GetCurrentPageWidget() const
{
return m_content_root.GetChildFromIndex(m_current_page);
}
void TBTabContainer::SetAlignment(TB_ALIGN align)
{
bool horizontal = (align == TB_ALIGN_TOP || align == TB_ALIGN_BOTTOM);
@ -121,7 +117,8 @@ void TBTabContainer::SetAlignment(TB_ALIGN align)
bool TBTabContainer::OnEvent(const TBWidgetEvent &ev)
{
if ((ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN) &&
ev.target->GetID() == TBIDC("tab"))
ev.target->GetID() == TBIDC("tab") &&
ev.target->GetParent() == &m_tab_layout)
{
int clicked_index = m_tab_layout.GetIndexFromChild(ev.target);
SetValue(clicked_index);

View File

@ -18,9 +18,6 @@ public:
// For safe typecasting
TBOBJECT_SUBCLASS(TBTabLayout, TBLayout);
/** Get the index (in the list of children) of the given child, or -1 if not found. */
int GetIndexFromChild(TBWidget *child) const;
virtual void OnChildAdded(TBWidget *child);
virtual PreferredSize OnCalculatePreferredContentSize(const SizeConstraints &constraints);
};
@ -55,6 +52,9 @@ public:
int GetCurrentPage() { return GetValue(); }
int GetNumPages();
/** Return the widget that is the current page, or nullptr if none is active. */
TBWidget *GetCurrentPageWidget() const;
virtual void OnInflate(const INFLATE_INFO &info);
virtual bool OnEvent(const TBWidgetEvent &ev);
virtual void OnProcess();

View File

@ -698,6 +698,25 @@ TBWidget *TBWidget::GetWidgetAt(int x, int y, bool include_children) const
return last_match;
}
TBWidget *TBWidget::GetChildFromIndex(int index) const
{
int i = 0;
for (TBWidget *child = GetFirstChild(); child; child = child->GetNext())
if (i++ == index)
return child;
return nullptr;
}
int TBWidget::GetIndexFromChild(TBWidget *child) const
{
assert(child->GetParent() == this);
int i = 0;
for (TBWidget *tmp = GetFirstChild(); tmp; tmp = tmp->GetNext(), i++)
if (tmp == child)
return i;
return -1; ///< Should not happen!
}
bool TBWidget::IsAncestorOf(TBWidget *other_widget) const
{
while (other_widget)

View File

@ -562,6 +562,16 @@ public:
is true, the search will recurse into the childrens children. */
TBWidget *GetWidgetAt(int x, int y, bool include_children) const;
/** Get the child at the given index, or nullptr if there was no child at that index.
Note: Avoid calling this in loops since it does iteration. Consider iterating
the widgets directly instead! */
TBWidget *GetChildFromIndex(int index) const;
/** Get the child index of the given widget (that must be a child of this widget).
Note: Avoid calling this in loops since it does iteration. Consider iterating
the widgets directly instead! */
int GetIndexFromChild(TBWidget *child) const;
/** Get the text of a child widget with the given id, or an empty string if there was
no widget with that id. */
TBStr GetTextByID(const TBID &id);