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 artifacts, keyboard inputs, mouse moves and other man/machine interactions can be controlled using UNO listeners that watch for the user’s behavior. 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 import XActionListener
         from import ActionEvent
         from import EventObject
         from \
             import OK, CANCEL
         import msgbox as util
         _MY_BUTTON =  "Button1"
         _MY_LABEL = 'Python listens..'
         _DLG_PROVIDER = ""
         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()
             rc = ui.execute()
             if rc == OK:
                 MsgBox("The user acknowledged the dialog.")
             elif rc == CANCEL:
                 MsgBox("The user canceled the dialog.")
             ui.dispose()  # ui.endExecute
         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"
                 dp = smgr.createInstanceWithContext(_DLG_PROVIDER, ctx)
                 location = "?location=application"
             dlg = dp.createDialog(""+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
                 if evt.Source.Model.Name == _MY_BUTTON:
                     evt.Source.Model.Label = _MY_LABEL+ str( self.count )
             def disposing(self, evt: EventObject):  # mandatory routine
         def MsgBox(txt: str):
             mb = util.MsgBox(uno.getComponentContext())
   , 0, "Python")
         g_exportedScripts = (Main,) in {installation}/program/ directory has some examples of button listeners.

With LibreOffice 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 '
             Dim dlg As Object
             Dim ui As Object  ' stardiv.Toolkit.UnoDialogControl
             Dim ctl As Object ' stardiv.Toolkit.UnoButtonControl
             Dim act As Object '
             Dim rc As Object : rc =
             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_", "")
             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()
         End Sub
         Private Sub awt_actionPerformed(evt As
             ''' 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 ' 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.

Mohon dukung kami!