1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
<!--
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
-->
<meta>
<topic id="text/sbasic/python/python_document_events">
<title id="tit" xml-lang="en-US">Python : Monitoring Document Events</title>
<filename>/text/sbasic/python/python_document_events.xhp</filename>
</topic>
</meta>
<body>
<bookmark branch="index" id="N0526">
<bookmark_value>Basic;Monitoring Document Events</bookmark_value>
<bookmark_value>Python;Monitoring Document Events</bookmark_value>
<bookmark_value>Access2Base;Console</bookmark_value>
<bookmark_value>Access2Base;Trace</bookmark_value>
<bookmark_value>Tools;Strings</bookmark_value>
<bookmark_value>API;GlobalScope.BasicLibraries</bookmark_value>
<bookmark_value>API;document.DocumentEvent: Monitoring Document Event</bookmark_value>
<bookmark_value>API;document.XDocumentEventBroadcaster: Monitoring Document Event</bookmark_value>
<bookmark_value>API;document.XDocumentEventListener: Monitoring Document Event</bookmark_value>
<bookmark_value>API;frame.Desktop: Monitoring Document Event</bookmark_value>
<bookmark_value>API;frame.GlobalEventBroadcaster: Monitoring Document Event</bookmark_value>
<bookmark_value>API;lang.EventObject: Monitoring Document Event</bookmark_value>
<bookmark_value>API;script.provider.MasterScriptProviderFactory: Monitoring Document Event</bookmark_value>
<bookmark_value>API;script.provider.XScript: Monitoring Document Event</bookmark_value>
</bookmark>
<section id="pythonmonitor1">
<h1 id="N0527"><variable id="pythonmonitor"><link href="text/sbasic/python/python_document_events.xhp" name="Monitoring Document Events">Monitoring Document Events</link></variable></h1>
</section>
<paragraph role="paragraph" id="N0528">Next to <link href="text/sbasic/shared/01040000.xhp" name ="assigning macros to events">assigning macros to events</link>, one can monitor events raised by %PRODUCTNAME documents. Application Programming Interface (API) broadcasters are responsible for calling event scripts. Unlike listeners that require to define all supported methods, even if unused, document monitors require only two methods next to hooked event scripts.</paragraph>
<h2 id="N0529">Listening to Document Events</h2>
<paragraph role="paragraph" id="N0530">Monitoring is illustrated herewith for Basic and Python languages using object-oriented programming. Assigning <literal>OnLoad</literal> script, to the <literal>Open Document</literal> event, suffices to initiate and terminate document event monitoring. <menuitem>Tools – Customise...</menuitem> menu <menuitem>Events</menuitem> tab is used to assign either scripts.</paragraph>
<paragraph role="paragraph" id="N0531">Intercepting events helps setting scripts pre- and post-conditions such as loading and unloading libraries or track script processing in the background. <literal>Access2Base Trace</literal> module usage is illustrating that second context.</paragraph>
<h3 id="N0532">With Python</h3>
<paragraph role="paragraph" id="N0533">Events monitoring starts from object instantiation and ultimately stops when Python releases the object. Raised events are reported using <literal>Access2Base</literal> console.</paragraph>
<note id="N0534"><literal>OnLoad</literal> and <literal>OnUnload</literal> events can be used to respectively set and unset Python programs path. They are described as <literal>Open document</literal> and <literal>Document closed</literal>.</note>
<pycode>
<paragraph role="pycode" localize="false" id="N0535"># -*- coding: utf-8 -*-</paragraph>
<paragraph role="pycode" localize="false" id="N0536">from __future__ import unicode_literals</paragraph>
<paragraph role="pycode" localize="false" id="N0537"> </paragraph>
<paragraph role="pycode" localize="false" id="N0538">import os.path, uno, unohelper</paragraph>
<paragraph role="pycode" localize="false" id="N0539">from com.sun.star.document import DocumentEvent, \</paragraph>
<paragraph role="pycode" localize="false" id="N0540"> XDocumentEventListener as AdapterPattern</paragraph>
<paragraph role="pycode" localize="false" id="N0541">from com.sun.star.lang import EventObject</paragraph>
<paragraph role="pycode" localize="false" id="N0542"> </paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0543">class UiDocument(unohelper.Base, AdapterPattern):</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0544"> """ Monitor document events """</paragraph>
<paragraph role="pycode" localize="false" id="N0545"> '''</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0546"> adapted from 'Python script to monitor OnSave event' at</paragraph>
<paragraph role="pycode" localize="false" id="N0547"> https://forum.openoffice.org/en/forum/viewtopic.php?t=68887</paragraph>
<paragraph role="pycode" localize="false" id="N0548"> '''</paragraph>
<paragraph role="pycode" localize="false" id="N0549"> def __init__(self):</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0550"> """ Document events monitor """</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0551"> ''' report using Access2Base.Trace console OR</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0552"> report in 1st sheet, 1st column for Calc docs '''</paragraph>
<paragraph role="pycode" localize="false" id="N0553"> ctx = uno.getComponentContext()</paragraph>
<paragraph role="pycode" localize="false" id="N0554"> smgr = ctx.getServiceManager()</paragraph>
<paragraph role="pycode" localize="false" id="N0555"> desktop = smgr.createInstanceWithContext(</paragraph>
<paragraph role="pycode" localize="false" id="N0556"> 'com.sun.star.frame.Desktop' , ctx)</paragraph>
<paragraph role="pycode" localize="false" id="N0557"> self.doc = desktop.CurrentComponent</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0558"> #self.row = 0 # uncomment for Calc documents only</paragraph>
<paragraph role="pycode" localize="false" id="N0559"> Console.setLevel("DEBUG")</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0560"> self.listen() # Start monitoring doc. events</paragraph>
<paragraph role="pycode" localize="false" id="N0562"> </paragraph>
<paragraph role="pycode" localize="false" id="N0563"> @property</paragraph>
<paragraph role="pycode" localize="false" id="N0564"> def Filename(self) -> str:</paragraph>
<paragraph role="pycode" localize="false" id="N0565"> sys_filename = uno.fileUrlToSystemPath(self.doc.URL)</paragraph>
<paragraph role="pycode" localize="false" id="N0566"> return os.path.basename(sys_filename)</paragraph>
<paragraph role="pycode" localize="false" id="N0567"> </paragraph>
<paragraph role="pycode" localize="false" id="N0568"> def setCell(self, calcDoc, txt: str):</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0569"> """ Output doc. events on 1st column of a Calc spreadsheet """</paragraph>
<paragraph role="pycode" localize="false" id="N0570"> sheet = calcDoc.getSheets().getByIndex(0)</paragraph>
<paragraph role="pycode" localize="false" id="N0571"> sheet.getCellByPosition(0,self.row).setString(txt)</paragraph>
<paragraph role="pycode" localize="false" id="N0572"> self.row = self.row + 1</paragraph>
<paragraph role="pycode" localize="false" id="N0574"> </paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0575"> def listen(self, *args): # OnLoad/OnNew at the earliest</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0576"> """ Start doc. events monitoring """</paragraph>
<paragraph role="pycode" localize="false" id="N0577"> self.doc.addDocumentEventListener(self)</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0578"> Console.log("INFO", "Document events are being logged", True)</paragraph>
<paragraph role="pycode" localize="false" id="N0579"> </paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0580"> def sleep(self, *args): # OnUnload at the latest (optional)</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0581"> """ Stop doc. events monitoring """</paragraph>
<paragraph role="pycode" localize="false" id="N0582"> self.doc.removeDocumentEventListener(self)</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0583"> Console.log("INFO", "Document events have been logged", True)</paragraph>
<paragraph role="pycode" localize="false" id="N0585"> </paragraph>
<paragraph role="pycode" localize="false" id="N0586"> def documentEventOccured(self, event: DocumentEvent):</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0587"> """ Intercepts all doc. events """</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0588"> #self.setCell(event.Source, event.EventName) # only for Calc docs</paragraph>
<paragraph role="pycode" localize="false" id="N0589"> Console.log("DEBUG",</paragraph>
<paragraph role="pycode" localize="false" id="N0590"> event.EventName+" in "+self.Filename,</paragraph>
<paragraph role="pycode" localize="false" id="N0591"> False)</paragraph>
<paragraph role="pycode" localize="false" id="N0593"> </paragraph>
<paragraph role="pycode" localize="false" id="N0594"> def disposing(self, event: EventObject):</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0595"> """ Release all activities """</paragraph>
<paragraph role="pycode" localize="false" id="N0596"> self.sleep()</paragraph>
<paragraph role="pycode" localize="false" id="N0597"> Console.show()</paragraph>
<paragraph role="pycode" localize="false" id="N0599"> </paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0601">def OnLoad(*args): # 'Open Document' event</paragraph>
<paragraph role="pycode" localize="false" id="N0602"> listener = UiDocument() # Initiates listening</paragraph>
<paragraph role="pycode" localize="false" id="N0603"> </paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0604">def OnUnload(*args): # 'Document has been closed' event</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0605"> pass # (optional) performed when disposed</paragraph>
<paragraph role="pycode" localize="false" id="N0606"> </paragraph>
<paragraph role="pycode" localize="false" id="N0607">g_exportedScripts = (OnLoad,)</paragraph>
<paragraph role="pycode" localize="false" id="N0609"> </paragraph>
<paragraph role="pycode" localize="false" id="N0610">from com.sun.star.script.provider import XScript</paragraph>
<paragraph role="pycode" localize="false" id="N0611">class Console():</paragraph>
<paragraph role="pycode" localize="false" id="N0612"> """</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0613"> (Back/Fore)ground console to report/log program execution.</paragraph>
<paragraph role="pycode" localize="false" id="N0614"> """</paragraph>
<paragraph role="pycode" localize="false" id="N0615"> @staticmethod</paragraph>
<paragraph role="pycode" localize="false" id="N0616"> def trace(*args,**kwargs):</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0617"> """ Print free item list to console """</paragraph>
<paragraph role="pycode" localize="false" id="N0618"> scr = Console._a2bScript(script='DebugPrint', module='Compatible')</paragraph>
<paragraph role="pycode" localize="false" id="N0619"> scr.invoke((args),(),())</paragraph>
<paragraph role="pycode" localize="false" id="N0620"> @staticmethod</paragraph>
<paragraph role="pycode" localize="false" id="N0621"> def log(level: str, text: str, msgBox=False):</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0622"> """ Append log message to console, optional user prompt """</paragraph>
<paragraph role="pycode" localize="false" id="N0623"> scr = Console._a2bScript(script='TraceLog')</paragraph>
<paragraph role="pycode" localize="false" id="N0624"> scr.invoke((level,text,msgBox),(),())</paragraph>
<paragraph role="pycode" localize="false" id="N0625"> @staticmethod</paragraph>
<paragraph role="pycode" localize="false" id="N0626"> def setLevel(logLevel: str):</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0627"> """ Set log messages lower limit """</paragraph>
<paragraph role="pycode" localize="false" id="N0628"> scr = Console._a2bScript(script='TraceLevel')</paragraph>
<paragraph role="pycode" localize="false" id="N0629"> scr.invoke((logLevel,),(),())</paragraph>
<paragraph role="pycode" localize="false" id="N0630"> @staticmethod</paragraph>
<paragraph role="pycode" localize="false" id="N0631"> def show():</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0632"> """ Display console content/dialog """</paragraph>
<paragraph role="pycode" localize="false" id="N0633"> scr = Console._a2bScript(script='TraceConsole')</paragraph>
<paragraph role="pycode" localize="false" id="N0634"> scr.invoke((),(),())</paragraph>
<paragraph role="pycode" localize="false" id="N0635"> @staticmethod</paragraph>
<paragraph role="pycode" localize="false" id="N0636"> def _a2bScript(script: str, library='Access2Base',</paragraph>
<paragraph role="pycode" localize="false" id="N0637"> module='Trace') -> XScript:</paragraph>
<paragraph role="pycode" localize="false" id="N0638"> ''' Grab application-based Basic script '''</paragraph>
<paragraph role="pycode" localize="false" id="N0639"> sm = uno.getComponentContext().ServiceManager</paragraph>
<paragraph role="pycode" localize="false" id="N0640"> mspf = sm.createInstanceWithContext(</paragraph>
<paragraph role="pycode" localize="false" id="N0641"> "com.sun.star.script.provider.MasterScriptProviderFactory",</paragraph>
<paragraph role="pycode" localize="false" id="N0642"> uno.getComponentContext())</paragraph>
<paragraph role="pycode" localize="false" id="N0643"> scriptPro = mspf.createScriptProvider("")</paragraph>
<paragraph role="pycode" localize="false" id="N0644"> scriptName = "vnd.sun.star.script:"+library+"."+module+"."+script+"?language=Basic&location=application"</paragraph>
<paragraph role="pycode" localize="false" id="N0645"> xScript = scriptPro.getScript(scriptName)</paragraph>
<paragraph role="pycode" localize="false" id="N0646"> return xScript</paragraph>
</pycode>
<warning id="N0647">Mind the misspelled <literal>documentEventOccured</literal> method that inherits a typo from %PRODUCTNAME Application Programming Interface (API).</warning>
<paragraph role="tip" id="N0648"><literal>Start application</literal> and <literal>Close application</literal> events can respectively be used to set and to unset Python path for user scripts or %PRODUCTNAME scripts. In a similar fashion, document based Python libraries or modules can be loaded and released using <literal>Open document</literal> and <literal>Document closed</literal> events. Refer to <link href="text/sbasic/python/python_import.xhp" name ="Importing Python Modules">Importing Python Modules</link> for more information.</paragraph>
<h3 id="N0649">With %PRODUCTNAME Basic</h3>
<paragraph role="paragraph" id="N0650">The <literal>Onload</literal> script is assigned to <literal>Open document</literal> event using <menuitem>Tools – Customise...</menuitem> menu <menuitem>Events</menuitem> tab. Events monitoring starts from the moment a <literal>ConsoleLogger</literal> object is instantiated and ultimately stops when Basic engine releases it. <literal>OnLoad</literal> event loads necessary Basic libraries, while caught events are reported using <literal>Access2Base.Trace</literal> module.</paragraph>
<bascode>
<paragraph role="bascode" xml-lang="en-US" id="N0651">REM controller.Events module</paragraph>
<paragraph role="bascode" localize="false" id="N0652">Option Explicit</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0653">Private _obj As Object ' controller.ConsoleLogger instance</paragraph>
<paragraph role="bascode" localize="false" id="N0654"> </paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0655">Sub OnLoad(evt As com.sun.star.document.DocumentEvent) ' >> Open Document <<</paragraph>
<paragraph role="bascode" localize="false" id="N0656"> _obj = New ConsoleLogger : _obj.Start(evt)</paragraph>
<paragraph role="bascode" localize="false" id="N0657">End Sub ' controller.OnLoad</paragraph>
<paragraph role="bascode" localize="false" id="N0658">' ----</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0659">REM controller.ConsoleLogger class module</paragraph>
<paragraph role="bascode" localize="false" id="N0660">Option Explicit</paragraph>
<paragraph role="bascode" localize="false" id="N0661">Option Compatible</paragraph>
<paragraph role="bascode" localize="false" id="N0662">Option ClassModule</paragraph>
<paragraph role="bascode" localize="false" id="N0663"> </paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0664">' ADAPTER design pattern object to be instantiated in « Open Document » event</paragraph>
<paragraph role="bascode" localize="false" id="N0665">Private Const UI_PROMPT = True</paragraph>
<paragraph role="bascode" localize="false" id="N0666">Private Const UI_NOPROMPT = False ' Set it to True to visualise documents events</paragraph>
<paragraph role="bascode" localize="false" id="N0667"> </paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0668">' CONSTRUCTOR/DESTRUCTOR</paragraph>
<paragraph role="bascode" localize="false" id="N0669">Private Sub Class_Initialize()</paragraph>
<paragraph role="bascode" localize="false" id="N0670">End Sub ' controller.ConsoleLogger.Initialize</paragraph>
<paragraph role="bascode" localize="false" id="N0671">Private Sub Class_Terminate()</paragraph>
<paragraph role="bascode" localize="false" id="N0672">End Sub ' controller.ConsoleLogger.Terminate</paragraph>
<paragraph role="bascode" localize="false" id="N0673"> </paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0674">' MEMBERS</paragraph>
<paragraph role="bascode" localize="false" id="N0675">Private _evtAdapter As Object ' com.sun.star.document.XDocumentEventBroadcaster</paragraph>
<paragraph role="bascode" localize="false" id="N0676"> </paragraph>
<paragraph role="bascode" localize="false" id="N0677">' PROPERTIES</paragraph>
<paragraph role="bascode" localize="false" id="N0678">Private Property Get FileName As String</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0679"> ''' System-dependent filename '''</paragraph>
<paragraph role="bascode" localize="false" id="N0680"> Const _LIBRARY = "Tools" : With GlobalScope.BasicLibraries</paragraph>
<paragraph role="bascode" localize="false" id="N0681"> If Not .IsLibraryLoaded(_LIBRARY) Then .LoadLibrary(_LIBRARY)</paragraph>
<paragraph role="bascode" localize="false" id="N0682"> End With</paragraph>
<paragraph role="bascode" localize="false" id="N0683"> Filename = Tools.Strings.FilenameOutofPath(ThisComponent.URL)</paragraph>
<paragraph role="bascode" localize="false" id="N0684">End Property ' controller.ConsoleLogger.Filename</paragraph>
<paragraph role="bascode" localize="false" id="N0685"> </paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0686">' METHODS</paragraph>
<paragraph role="bascode" localize="false" id="N0687">Private Sub _documentEventOccured(evt As com.sun.star.document.DocumentEvent)</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0688"> ''' Monitor document events '''</paragraph>
<paragraph role="bascode" localize="false" id="N0689"> Access2Base.Trace.TraceLog("DEBUG", _</paragraph>
<paragraph role="bascode" localize="false" id="N0690"> evt.EventName &" in "& Filename(evt.Source.URL), _</paragraph>
<paragraph role="bascode" localize="false" id="N0691"> UI_NOPROMPT)</paragraph>
<paragraph role="bascode" localize="false" id="N0692"> Select Case evt.EventName</paragraph>
<paragraph role="bascode" localize="false" id="N0693"> Case "OnUnload" : _Stop(evt)</paragraph>
<paragraph role="bascode" localize="false" id="N0694"> End Select</paragraph>
<paragraph role="bascode" localize="false" id="N0695">End Sub ' controller.ConsoleLogger._documentEventOccured</paragraph>
<paragraph role="bascode" localize="false" id="N0696"> </paragraph>
<paragraph role="bascode" localize="false" id="N0697">Private Sub _disposing(evt As com.sun.star.lang.EventObject)</paragraph>
<paragraph role="bascode" localize="false" id="N0698">End Sub ' controller.ConsoleLogger.disposing</paragraph>
<paragraph role="bascode" localize="false" id="N0699"> </paragraph>
<paragraph role="bascode" localize="false" id="N0700">Public Sub Start(Optional evt As com.sun.star.document.DocumentEvent)</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0701"> ''' Initialize document events logging '''</paragraph>
<paragraph role="bascode" localize="false" id="N0702"> Const _LIBRARY = "Access2Base" : With GlobalScope.BasicLibraries</paragraph>
<paragraph role="bascode" localize="false" id="N0703"> If Not .IsLibraryLoaded(_LIBRARY) Then .LoadLibrary(_LIBRARY)</paragraph>
<paragraph role="bascode" localize="false" id="N0704"> End With : Access2Base.Trace.TraceLevel("DEBUG")</paragraph>
<paragraph role="bascode" localize="false" id="N0705"> Access2Base.Trace.TraceLog("INFO", _</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0706"> IIf(IsMissing(evt),"",evt.EventName & "-") & "Document events are being logged", _</paragraph>
<paragraph role="bascode" localize="false" id="N0707"> UI_PROMPT)</paragraph>
<paragraph role="bascode" localize="false" id="N0708"> </paragraph>
<paragraph role="bascode" localize="false" id="N0709"> _evtAdapter = CreateUnoListener( "_", "com.sun.star.document.XDocumentEventListener" )</paragraph>
<paragraph role="bascode" localize="false" id="N0710"> ThisComponent.addDocumentEventListener( _evtAdapter )</paragraph>
<paragraph role="bascode" localize="false" id="N0711">End Sub ' controller.ConsoleLogger.Start</paragraph>
<paragraph role="bascode" localize="false" id="N0712"> </paragraph>
<paragraph role="bascode" localize="false" id="N0713">Private Sub _Stop(Optional evt As com.sun.star.document.DocumentEvent)</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0714"> ''' Terminate document events logging '''</paragraph>
<paragraph role="bascode" localize="false" id="N0715"> ThisComponent.removeDocumentEventListener( _evtAdapter )</paragraph>
<paragraph role="bascode" localize="false" id="N0716"> Access2Base.Trace.TraceLog("INFO", _</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0717"> IIf(IsMissing(evt),"",evt.EventName & "-") & "Document events have been logged", _</paragraph>
<paragraph role="bascode" localize="false" id="N0718"> UI_PROMPT)</paragraph>
<paragraph role="bascode" localize="false" id="N0719"> Access2Base.Trace.TraceConsole() ' Captured events dialog</paragraph>
<paragraph role="bascode" localize="false" id="N0720">End Sub ' controller.ConsoleLogger._Stop</paragraph>
<paragraph role="bascode" localize="false" id="N0721"> </paragraph>
<paragraph role="bascode" localize="false" id="N0722">' EVENTS</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0723">' Your code for handled events goes here</paragraph>
</bascode>
<warning id="N0724">Mind the misspelled <literal>_documentEventOccured</literal> method that inherits a typo from %PRODUCTNAME Application Programming Interface (API).</warning>
<h2 id="N0725">Discovering Documents Events</h2>
<paragraph role="paragraph" id="N0726">The broadcaster API object provides the list of events it is responsible for:</paragraph>
<h3 id="N0727">With Python</h3>
<pycode>
<paragraph role="pycode" localize="false" id="N0728"># -*- coding: utf-8 -*-</paragraph>
<paragraph role="pycode" localize="false" id="N0729">from __future__ import unicode_literals</paragraph>
<paragraph role="pycode" localize="false" id="N0730"> </paragraph>
<paragraph role="pycode" localize="false" id="N0731">import uno, apso_utils as ui</paragraph>
<paragraph role="pycode" localize="false" id="N0732"> </paragraph>
<paragraph role="pycode" localize="false" id="N0733">def displayAvailableEvents():</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0734"> """ Display document events """</paragraph>
<paragraph role="pycode" localize="false" id="N0735"> '''</paragraph>
<paragraph role="pycode" xml-lang="en-US" id="N0736"> adapted from DisplayAvailableEvents() by A. Pitonyak</paragraph>
<paragraph role="pycode" localize="false" id="N0737"> https://forum.openoffice.org/en/forum/viewtopic.php?&t=43689</paragraph>
<paragraph role="pycode" localize="false" id="N0738"> '''</paragraph>
<paragraph role="pycode" localize="false" id="N0739"> ctx = XSCRIPTCONTEXT.getComponentContext()</paragraph>
<paragraph role="pycode" localize="false" id="N0740"> smgr = ctx.ServiceManager</paragraph>
<paragraph role="pycode" localize="false" id="N0741"> geb = smgr.createInstanceWithContext(</paragraph>
<paragraph role="pycode" localize="false" id="N0742"> "com.sun.star.frame.GlobalEventBroadcaster", ctx)</paragraph>
<paragraph role="pycode" localize="false" id="N0743"> events = geb.Events.getElementNames()</paragraph>
<paragraph role="pycode" localize="false" id="N0744"> ui.msgbox('; '.join(events))</paragraph>
<paragraph role="pycode" localize="false" id="N0745"> </paragraph>
<paragraph role="pycode" localize="false" id="N0746">g_exportedScripts = (displayAvailableEvents,)</paragraph>
</pycode>
<note id="N0747">The <link href="https://extensions.libreoffice.org/extensions/apso-alternative-script-organizer-for-python" name ="Alternative Python Script Organizer">Alternative Python Script Organizer (APSO)</link> extension is used to render events information on screen.</note>
<h3 id="N0748">With %PRODUCTNAME Basic</h3>
<bascode>
<paragraph role="bascode" localize="false" id="N0749">Sub DisplayAvailableEvents</paragraph>
<paragraph role="bascode" xml-lang="en-US" id="N0750"> ''' Display document events '''</paragraph>
<paragraph role="bascode" localize="false" id="N0751"> Dim geb As Object ' com.sun.star.frame.GlobalEventBroadcaster</paragraph>
<paragraph role="bascode" localize="false" id="N0752"> Dim events() As String</paragraph>
<paragraph role="bascode" localize="false" id="N0753"> geb = CreateUnoService("com.sun.star.frame.GlobalEventBroadcaster")</paragraph>
<paragraph role="bascode" localize="false" id="N0754"> events = geb.Events.ElementNames()</paragraph>
<paragraph role="bascode" localize="false" id="N0755"> MsgBox Join(events, "; ")</paragraph>
<paragraph role="bascode" localize="false" id="N0756">End Sub</paragraph>
</bascode>
<section id="relatedtopics" >
<embed href="text/sbasic/python/python_listener.xhp#pythonlistener"/>
<embed href="text/sbasic/python/python_import.xhp#pythonimporth1"/>
<embed href="text/sbasic/python/python_handler.xhp#pythonhandler_h1"/>
<embed href="text/sbasic/python/python_examples.xhp#pythonexamples2"/>
<embed href="text/sbasic/python/python_programming.xhp#pythonprogrammingheading"/>
<embed href="text/sbasic/python/python_2_basic.xhp#py2ba_h1"/>
</section>
</body>
</helpdocument>
|