OpenOffice.org Macros Explained

As OOo operates, it generates events to inform listeners that something has occurred. The event listener/supplier framework allows a macro to be called or a listener to be notified when a specific event occurs-for example, when a document is loaded or modified, or when text selection changes. Each document type supports the two interfaces com.sun.star.document.XEventBroadcaster and com.sun.star.document.XEventsSupplier, allowing them to support event- related activities-for example, broadcasting events to listeners and providing a list of supported events.

The macro in Listing 4 lists the event listeners registered with the current document. Another use for this macro is to list the events supported by the document. As shown in Figure 3 , although there are no registered listeners, this macro provides a nice list of the supported event types.

Figure 3: Events supported by ThisComponent.

Listing 4: GetEventListeners is found in the Generic module in this chapter's source code files as SC12.sxw.

Sub GetEventListeners Dim vListeners 'com.sun.star.container.XNameReplace Dim sEventNames 'Array of event types REM com.sun.star.container.XNameReplace REM This macro also provides named access vListeners = ThisComponent.getEvents() sEventNames = vListeners.getElementNames() MsgBox Join(sEventNames, Chr$(10)) REM To enumerate the listeners themselves, use this REM code! 'For i = 0 To UBound(sEventNames) ' vv = vListeners.getByName(sEventNames(i)) 'Next End Sub

 

Tip  

Many objects besides documents support event listeners. For example, you can listen to an individual cell in a Calc document.

Категории