Text Documents (Writer)
HTML Documents (Writer Web)
Spreadsheets (Calc)
Presentations (Impress)
Drawings (Draw)
Database Functionality (Base)
Formulae (Math)
Charts and Diagrams
Macros and Scripting
Office Installation
Common Help Topics
OneOffice Logo

Creating Event Listeners

Events raised by dialogs, documents, forms or graphical controls can be linked to macros, which is referred to as event-driven programming. The most common method to relate events to macros are the Events tab in Tools – Customize menu and the Dialog Editor Control properties pane from Tools - Macros – Organise Dialogs... menu.

Graphical artefacts, keyboard inputs, mouse moves and other man/machine interactions can be controlled using UNO listeners that watch for the user’s behaviour. Listeners are dynamic program code alternatives to macro assignments. One may create as many UNO listeners as events to watch for. A single listener can also handle multiple user interface controls.

Creating an event listener

Listeners get attached to controls held in dialogs, as well as to document or form events. Listeners are also used when creating runtime dialogs or when adding controls to a dialog on the fly.

This example creates a listener for Button1 control of Dialog1 dialog in Standard library.

With Python


         # -*- coding: utf-8 -*-
         from --future-- import unicode-literals
             
         import uno, unohelper
         from com.sun.star.awt import XActionListener
         from com.sun.star.awt import ActionEvent
         from com.sun.star.lang import EventObject
         from com.sun.star.ui.dialogs.ExecutableDialogResults \
             import OK, CANCEL
         import msgbox as util
             
         -MY-BUTTON =  "Button1"
         -MY-LABEL = 'Python listens..'
         -DLG-PROVIDER = "com.sun.star.awt.DialogProvider"
             
         def Main(*args):
             ui = createUnoDialog("Standard.Dialog1", embedded=True)
             ui.Title = "Python X[any]Listener"
             ctl = ui.getControl(-MY-BUTTON)
             ctl.Model.Label = -MY-LABEL
             act = ActionListener()
             ctl.addActionListener(act)
             rc = ui.execute()
             if rc == OK:
                 MsgBox("The user acknowledged the dialog.")
             elif rc == CANCEL:
                 MsgBox("The user cancelled the dialog.")
             ui.dispose()  # ui.endExecute
             ctl.removeActionListener(act)
             
         def createUnoDialog(libr-dlg: str, embedded=False):
             """ Create a Dialog from its location """
             smgr = XSCRIPTCONTEXT.getComponentContext().ServiceManager
             if embedded:
                 model = XSCRIPTCONTEXT.getDocument()
                 dp = smgr.createInstanceWithArguments(-DLG-PROVIDER, (model,))
                 location = "?location=document"
             else:
                 dp = smgr.createInstanceWithContext(-DLG-PROVIDER, ctx)
                 location = "?location=application"
             dlg = dp.createDialog("vnd.sun.star.script:"+libr-dlg+location)
             return dlg
             
         class ActionListener(unohelper.Base, XActionListener):
             """ Listen to & count button clicks """
             def --init--(self):
                 self.count = 0
             
             def actionPerformed(self, evt: ActionEvent):
                 self.count = self.count + 1
                 #mri(evt)
                 if evt.Source.Model.Name == -MY-BUTTON:
                     evt.Source.Model.Label = -MY-LABEL+ str( self.count )
             return
             
             def disposing(self, evt: EventObject):  # mandatory routine
                 pass
             
         def MsgBox(txt: str):
             mb = util.MsgBox(uno.getComponentContext())
             mb.addButton("Ok")
             mb.show(txt, 0, "Python")
             
         g-exportedScripts = (Main,)
      

msgbox.py in {installation}/program/ directory has some examples of button listeners.

With Office Basic


         Option Explicit
             
         Const MY-LIBRARY = "Standard", MY-DIALOG = "Dialog1", MY-BUTTON = "Button1"
         Const MY-LABEL = "Basic listens.."
         Dim count As Integer
             
         Sub Main
             Dim libr As Object ' com.sun.star.script.XLibraryContainer
             Dim dlg As Object
             Dim ui As Object  ' stardiv.Toolkit.UnoDialogControl
             Dim ctl As Object ' stardiv.Toolkit.UnoButtonControl
             Dim act As Object ' com.sun.star.awt.XActionListener
             Dim rc As Object : rc = com.sun.star.ui.dialogs.ExecutableDialogResults
             
             BasicLibraries.LoadLibrary(MY-LIBRARY)
             libr = DialogLibraries.GetByName(MY-LIBRARY)
             dlg = libr.GetByName(MY-DIALOG)
             ui = CreateUnoDialog(dlg)
             ui.Title = "Basic X[any]Listener example"
             count = 0
             ctl = ui.GetControl(MY-BUTTON)
             ctl.Model.Label = MY-LABEL
             act = CreateUnoListener("awt-", "com.sun.star.awt.XActionListener")
             ctl.addActionListener(act)
             Select Case ui.Execute
                 Case rc.OK : MsgBox "The user acknowledged the dialog.",, "Basic"
                 Case rc.CANCEL : MsgBox "The user canceled the dialog.",, "Basic"
             End Select
             ui.dispose ' ui.endExecute()
             ctl.removeActionListener(act)
         End Sub
             
         Private Sub awt-actionPerformed(evt As com.sun.star.awt.ActionEvent)
             ''' Listen to & count button clicks '''
             With evt.Source.Model
                 If .Name = MY-BUTTON Then
                     count = count + 1
                     .Label = MY-LABEL+Cstr(count)
                 End If
             End With
         End Sub ' awt-actionPerformed
             
         Private Sub awt-disposing(evt As com.sun.star.lang.EventObject) ' mandatory Sub
             ' your code goes here
         End Sub ' awt-disposing
      

Other Event Listeners

Listeners are usually coded along with dialog opening. Numerous listener approaches are possible such as event handlers for dialogs or event monitors for documents or forms.