Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to observe document-level events

New Here ,
Sep 01, 2010 Sep 01, 2010

I want to observe document-level events. I seems that AutoAttach will never get called and nothing works.. what am I doing wrong?

M5.fr:

...

AddIn

{

     kDocBoss,

     kInvalidClass,

     {

          /** Implementation of IObserver that responds to document-level notifications

          @SEE M5DocObserver

          */

          IID_IM5DOCOBSERVER, kM5DocObserverImpl

     }

},

...

M5DocObserver.cpp:

...

class M5DocObserver : public CObserver

{

...

void M5DocObserver::AutoAttach()

{

  M5Utils::alert("HELLO!");

  CObserver::AutoAttach();

  InterfacePtr<IDocument> document(this, UseDefaultIID());

  if (document != nil)

    this->AttachDocument(document);

}

...

void M5DocObserver::AttachDocument(IDocument* document)

{

do

{

  InterfacePtr<ISubject> docSubject(document, UseDefaultIID());

  ASSERT(docSubject);

  if(!docSubject) {

   break;

  }

  if (!docSubject->IsAttached(ISubject::kLazyAttachment,this, IID_IHIERARCHY_DOCUMENT, IID_IM5DOCOBSERVER)) {

docSubject->AttachObserver(ISubject::kLazyAttachment,this, IID_IHIERARCHY_DOCUMENT, IID_IM5DOCOBSERVER);

  }

  if( !docSubject->IsAttached(ISubject::kLazyAttachment,this, IID_ISELECTIONMANAGER, IID_IM5DOCOBSERVER)) {

    docSubject->AttachObserver(ISubject::kLazyAttachment,this, IID_ISELECTIONMANAGER, IID_IM5DOCOBSERVER);

  }

} while (kFalse);

}

...

TOPICS
SDK
884
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 01, 2010 Sep 01, 2010

Hi Joni,

what is missing is the CResponder to attoattach your M5DocObserver.

To give you a scetch:

class DocumentResponder : public CResponder {
public:
    DocumentResponder( IPMUnknown* inIPMUnknown );
    virtual void Respond( ISignalMgr* inISignalMgr );
};


CREATE_PMINTERFACE( DocumentResponder, kDocumentResponderImpl )


DocumentResponder::DocumentResponder(
    IPMUnknown* inIPMUnknown )
:    CResponder( inIPMUnknown )
{
}


void
DocumentResponder::Respond(
    ISignalMgr*    inISignalMgr )
{
    do {
        if ( ! inISignalMgr ) {
            break;
        }
        InterfacePtr<IDocumentSignalData> iDocumentSignalData( inISignalMgr, UseDefaultIID() );
        if ( ! iDocumentSignalData ) {
            break;
        }
        const UIDRef docUIDRef = iDocumentSignalData->GetDocument();
        const ServiceID serviceID = inISignalMgr->GetServiceID();
        const int32 serviceIDValue = serviceID.Get();

        // Attach/detach MAExBoxAdornmentDocObserver
        switch ( serviceIDValue ) {
            case kAfterNewDocSignalResponderService:
            case kAfterOpenDocSignalResponderService:
                {
                    InterfacePtr<IObserver> iObserver( docUIDRef, IID_IM5DOCOBSERVER );
                    if ( ! iObserver ) {
                        break;
                    }
                    iObserver->AutoAttach();
                }
                break;
            case kBeforeCloseDocSignalResponderService:
                {
                    InterfacePtr<IObserver> iObserver( docUIDRef, IID_IM5DOCOBSERVER );
                    if ( ! iObserver ) {
                        break;
                    }
                    iObserver->AutoDetach();
                }
                break;
            default:
                break;
        }
    } while ( false );
}

HTH

Peter

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 02, 2010 Sep 02, 2010

Thanks, I decided to go with the Responder. Actually I don't see the point to have the observer at all (in my case..). I have now a working Responder and through the ServiceProvider I can register serviceIDs like kAfterOpenDocSignalResponderService and it works. But I still need a hook to the table selection event.

Is there a ServiceID for table selection etc. ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 01, 2010 Sep 01, 2010

I'm guessing that Document Event Handler is missing

AddIn

{ kDocBoss, kInvalidClass,

{ IID_IYOURDOCOBSERVER, kYOURDocumentObserverImpl,

IID_IYOURDOCEH, kYOUREventWatcherImpl }};

IEventDispatcher::EventTypeList m_events;

CEventWatcher::CEventWatcher(IPMUnknown* inBoss)

: CPMUnknown<IEventWatcher>(inBoss)

{

m_events.Empty();

m_events.Add(IEvent::kLButtonUp);

}

void CEventWatcher::StartWatching()

{

InterfacePtr<IApplication> theApp(gSession->QueryApplication());

InterfacePtr<IEventDispatcher> theDispatcher(theApp, UseDefaultIID());

theDispatcher->AddEventWatcher(this, m_events);

}

void CEventWatcher::StopWatching()

{

/////  code

}

IEventDispatcher::EventTypeList CEventWatcher::WatchEvent(IEvent* inEvent)

{

if(0 != inEvent)

{

switch(inEvent->GetType())

{

case IEvent::kLButtonDn:

{

     ///code

break;

}

default:

break;

}

}

return m_events;

}

REGARDS

BARTEK

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Sep 01, 2010 Sep 01, 2010

That example above is for watching events like mouse click

if you want to watch for events like kAddToHierarchyCmdBoss, kRemoveFromHierarchyCmdBoss, kUngroupCmdBoss, kGroupCmdBoss and so on

then you need to use your code. Not sure why you cant attach that observer

the only one differmce that i've found is

if (!iDocSubject->IsAttached(ISubject::kRegularAttachment,this, IID_IHIERARCHY_DOCUMENT, IID_IYOURDOCOBSERVER))

iDocSubject->AttachObserver(ISubject::kRegularAttachment,this, IID_IHIERARCHY_DOCUMENT, IID_IYOURDOCOBSERVER);

if (!iDocSubject->IsAttached(ISubject::kRegularAttachment, this, IID_IHIERARCHY, IID_IYOURDOCOBSERVER))

iDocSubject->AttachObserver(ISubject::kRegularAttachment, this, IID_IHIERARCHY, IID_IYOURDOCOBSERVER);

Bartek

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Sep 03, 2010 Sep 03, 2010
LATEST

Checking for isAttached hides ASSERTs that otherwise would be an indicator for unbalanced or duplicate Attach/Detach.

I prefer to fix the code rather than suppress the error message.

Dirk

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines