Skip to content

Commit

Permalink
Changed listener API for OnWidgetAdded/OnWidgetRemoved.
Browse files Browse the repository at this point in the history
NOTE: Local listeners are now called on the parent widget instead of the child!
It makes more sense to use a listener to listen for additions/removes in a certain
widget, than listening to when a specific widget is added/removed anywhere.
  • Loading branch information
fruxo committed May 20, 2014
1 parent 90c9766 commit 16e13dc
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 26 deletions.
8 changes: 4 additions & 4 deletions Demo/demo01/ResourceEditWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,15 @@ bool ResourceEditWindow::OnWidgetInvokeEvent(TBWidget *widget, const TBWidgetEve
return false;
}

void ResourceEditWindow::OnWidgetAdded(TBWidget *widget)
void ResourceEditWindow::OnWidgetAdded(TBWidget *parent, TBWidget *child)
{
if (m_build_container && m_build_container->IsAncestorOf(widget))
if (m_build_container && m_build_container->IsAncestorOf(child))
UpdateWidgetList(false);
}

void ResourceEditWindow::OnWidgetRemove(TBWidget *widget)
void ResourceEditWindow::OnWidgetRemove(TBWidget *parent, TBWidget *child)
{
if (m_build_container && m_build_container->IsAncestorOf(widget))
if (m_build_container && m_build_container->IsAncestorOf(child))
UpdateWidgetList(false);
}

Expand Down
4 changes: 2 additions & 2 deletions Demo/demo01/ResourceEditWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class ResourceEditWindow : public TBWindow, public TBMessageHandler, public TBWi

// == TBWidgetListener ========================================================
virtual bool OnWidgetInvokeEvent(TBWidget *widget, const TBWidgetEvent &ev);
virtual void OnWidgetAdded(TBWidget *widget);
virtual void OnWidgetRemove(TBWidget *widget);
virtual void OnWidgetAdded(TBWidget *parent, TBWidget *child);
virtual void OnWidgetRemove(TBWidget *parent, TBWidget *child);
private:
TBSelectList *m_widget_list;
TBSelectItemSourceList<ResourceItem> m_widget_list_source;
Expand Down
4 changes: 2 additions & 2 deletions src/tb/animation/tb_widget_animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ bool TBWidgetsAnimationManager::OnWidgetDying(TBWidget *widget)
return handled;
}

void TBWidgetsAnimationManager::OnWidgetAdded(TBWidget *widget)
void TBWidgetsAnimationManager::OnWidgetAdded(TBWidget *parent, TBWidget *widget)
{
if (TBWindow *window = TBSafeCast<TBWindow>(widget))
{
Expand All @@ -231,7 +231,7 @@ void TBWidgetsAnimationManager::OnWidgetAdded(TBWidget *widget)
}
}

void TBWidgetsAnimationManager::OnWidgetRemove(TBWidget *widget)
void TBWidgetsAnimationManager::OnWidgetRemove(TBWidget *parent, TBWidget *widget)
{
}

Expand Down
4 changes: 2 additions & 2 deletions src/tb/animation/tb_widget_animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class TBWidgetsAnimationManager : public TBWidgetListener
// == TBWidgetListener ==================
virtual void OnWidgetDelete(TBWidget *widget);
virtual bool OnWidgetDying(TBWidget *widget);
virtual void OnWidgetAdded(TBWidget *widget);
virtual void OnWidgetRemove(TBWidget *widget);
virtual void OnWidgetAdded(TBWidget *parent, TBWidget *child);
virtual void OnWidgetRemove(TBWidget *parent, TBWidget *child);
};

}; // namespace tb
Expand Down
4 changes: 2 additions & 2 deletions src/tb/tb_widgets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ void TBWidget::AddChildRelative(TBWidget *child, WIDGET_Z_REL z, TBWidget *refer
{
OnChildAdded(child);
child->OnAdded();
TBWidgetListener::InvokeWidgetAdded(child);
TBWidgetListener::InvokeWidgetAdded(this, child);
}
InvalidateLayout(INVALIDATE_LAYOUT_RECURSIVE);
Invalidate();
Expand All @@ -298,7 +298,7 @@ void TBWidget::RemoveChild(TBWidget *child, WIDGET_INVOKE_INFO info)

OnChildRemove(child);
child->OnRemove();
TBWidgetListener::InvokeWidgetRemove(child);
TBWidgetListener::InvokeWidgetRemove(this, child);
}

m_children.Remove(child);
Expand Down
16 changes: 8 additions & 8 deletions src/tb/tb_widgets_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,24 @@ bool TBWidgetListener::InvokeWidgetDying(TBWidget *widget)
return handled;
}

void TBWidgetListener::InvokeWidgetAdded(TBWidget *widget)
void TBWidgetListener::InvokeWidgetAdded(TBWidget *parent, TBWidget *child)
{
TBLinkListOf<TBWidgetListenerGlobalLink>::Iterator global_i = g_listeners.IterateForward();
TBLinkListOf<TBWidgetListener>::Iterator local_i = widget->m_listeners.IterateForward();
TBLinkListOf<TBWidgetListener>::Iterator local_i = parent->m_listeners.IterateForward();
while (TBWidgetListener *listener = local_i.GetAndStep())
listener->OnWidgetAdded(widget);
listener->OnWidgetAdded(parent, child);
while (TBWidgetListenerGlobalLink *link = global_i.GetAndStep())
static_cast<TBWidgetListener*>(link)->OnWidgetAdded(widget);
static_cast<TBWidgetListener*>(link)->OnWidgetAdded(parent, child);
}

void TBWidgetListener::InvokeWidgetRemove(TBWidget *widget)
void TBWidgetListener::InvokeWidgetRemove(TBWidget *parent, TBWidget *child)
{
TBLinkListOf<TBWidgetListenerGlobalLink>::Iterator global_i = g_listeners.IterateForward();
TBLinkListOf<TBWidgetListener>::Iterator local_i = widget->m_listeners.IterateForward();
TBLinkListOf<TBWidgetListener>::Iterator local_i = parent->m_listeners.IterateForward();
while (TBWidgetListener *listener = local_i.GetAndStep())
listener->OnWidgetRemove(widget);
listener->OnWidgetRemove(parent, child);
while (TBWidgetListenerGlobalLink *link = global_i.GetAndStep())
static_cast<TBWidgetListener*>(link)->OnWidgetRemove(widget);
static_cast<TBWidgetListener*>(link)->OnWidgetRemove(parent, child);
}

void TBWidgetListener::InvokeWidgetFocusChanged(TBWidget *widget, bool focused)
Expand Down
14 changes: 8 additions & 6 deletions src/tb/tb_widgets_listener.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ class TBWidgetListener : public TBLinkOf<TBWidgetListener>, public TBWidgetListe
use TBWidgetSafePointer to safely handle that. */
virtual bool OnWidgetDying(TBWidget *widget) { return false; }

/** Called when widget has been added to a parent, after its parents OnChildAdded. */
virtual void OnWidgetAdded(TBWidget *widget) {}
/** Called when the child has been added to parent, after its parents OnChildAdded.
Local listeners are invoked on the parent widget. */
virtual void OnWidgetAdded(TBWidget *parent, TBWidget *child) {}

/** Called when widget is about to be removed from a parent, after its parents OnChildRemove. */
virtual void OnWidgetRemove(TBWidget *widget) {}
/** Called when the child is about to be removed from parent, after its parents OnChildRemove.
Local listeners are invoked on the parent widget. */
virtual void OnWidgetRemove(TBWidget *parent, TBWidget *child) {}

/** Called when widget focus has changed on a widget. */
virtual void OnWidgetFocusChanged(TBWidget *widget, bool focused) {}
Expand All @@ -61,8 +63,8 @@ class TBWidgetListener : public TBLinkOf<TBWidgetListener>, public TBWidgetListe
friend class TBWidget;
static void InvokeWidgetDelete(TBWidget *widget);
static bool InvokeWidgetDying(TBWidget *widget);
static void InvokeWidgetAdded(TBWidget *widget);
static void InvokeWidgetRemove(TBWidget *widget);
static void InvokeWidgetAdded(TBWidget *parent, TBWidget *child);
static void InvokeWidgetRemove(TBWidget *parent, TBWidget *child);
static void InvokeWidgetFocusChanged(TBWidget *widget, bool focused);
static bool InvokeWidgetInvokeEvent(TBWidget *widget, const TBWidgetEvent &ev);
};
Expand Down

0 comments on commit 16e13dc

Please sign in to comment.