コード例 #1
0
ファイル: lomse_events.cpp プロジェクト: lenmus/lomse
//---------------------------------------------------------------------------------------
Observable* EventMouse::get_source()
{
    ImoObj* pImo = get_imo_object();
    if (pImo)
    {
        if (pImo->is_contentobj())
            return static_cast<Observable*>( static_cast<ImoContentObj*>(pImo) );
        else
            return pImo->get_observable_parent();
    }
    return NULL;
}
コード例 #2
0
ファイル: lomse_events.cpp プロジェクト: lenmus/lomse
//---------------------------------------------------------------------------------------
bool EventNotifier::notify_observers(SpEventInfo pEvent, Observable* target)
{
    //returns true if event is dispatched to an observer

    std::list<Observer*>::iterator it;
    for (it = m_observers.begin(); it != m_observers.end(); ++it)
    {
        Observable* observedTarget = (*it)->target();
        bool fNotify = (observedTarget == target);

        //bubbling phase. This observer is not observing target but might be
        //observing its parents
        if (!fNotify)
        {
            ImoObj* pImo = dynamic_cast<ImoObj*>(target);
            Observable* pObs = target;
            if (pImo && pObs)
            {
                while(pImo && pObs && pObs != observedTarget)
                {
                    pObs = pImo->get_observable_parent();
                    pImo = dynamic_cast<ImoObj*>( pObs );
                }
                fNotify = (pObs == observedTarget);
                //TODO: do notification and continue bubbling.
            }
        }

        if (fNotify)
        {
            LOMSE_LOG_DEBUG(Logger::k_events, "Posting event.");
            m_pDispatcher->post_event((*it), pEvent);
//            (*it)->notify(pEvent);
            return true;
            //TODO: remove 'return' when following problem is fixed:
            //    Object receiving notification might modify the document (i.e. link
            //    'new problem') and this will invalidate target and all remaining
            //    objects in m_observers (!!!!)
        }
    }
    LOMSE_LOG_DEBUG(Logger::k_events, "No observers. Event ignored");
    return false;
}