Creating a Dialog Handler

Next to assigning macros to events or creating event listeners, one can use dialog handlers, whose principle is to define UNO keywords, or methods, that are mapped to events to watch for. The event handler is responsible for executing methods using the<method_name> protocol. Unlike listeners that require to define all supported methods, even if unused, dialog handlers require only two methods on top of intended control hook scripts.

Las ventajas de este método son:

This mechanism is illustrated herewith for Basic and Python languages using an imported copy of Access2Base dlgTrace dialog. Exception handling and localisation are omitted for clarity.

Asignación de métodos de Dialog

Exporte el cuadro de diálogo dlgTrace de Access2Base e impórtelo en la biblioteca de aplicación MyLib.

Inside the control properties pane of the Dialog Editor, use the Events tab to replace macro assignments by component assignments, and type in the intended method names:

Events assigned actions should mention the protocol.

Creating the handler

createDialogWithHandler method of service is used to set the dialog and its handler. The handler is responsible for implementing interface.


All component method names must be explicitly declared when using a dialog handler.

Con Python

En este ejemplo, el cuadro de diálogo se localiza en el equipo.

      # -*- coding: utf-8 -*-
      from __future__ import unicode_literals
      import uno, unohelper
      from import XDialogEventHandler
      _DLG_PROVIDER = ""
      class Console(unohelper.Base, XDialogEventHandler):
          """ Manipulador de consola de Access2Base """
          ''' adaptado de «Créer un dialogue avec gestionnaire d’événements» de J.-M. Zambon
          def show(self):
              dialog = self.getDialog("MyLib.dlgTrace", embedded=True)
              dialog.Title = "Konsole"
          def callHandlerMethod(self, dialog, event, method):
              if method == '_dump2File':
                  event.Source.setLabel("dump requested")
                  scr = getBasicScript(script="_DumpToFile", module="Trace",
                  scr.invoke((event,), (), ())
              elif method == '_openHelp':
                  _msgbox('Not yet implemented')
                  return False
          def getSupportedMethodNames(self):
              return ('_dump2File', '_openHelp')
          def getDialog(self, 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.createDialogWithHandler(""+libr_dlg+location, self)
              return dlg
      # def getBasicScript()  # see note
      def _msgbox(prompt='', title=''):
          ''' Ugly MsgBox '''
          import msgbox
          mb = msgbox.MsgBox(uno.getComponentContext())
      def ConsoleHandler():
      g_exportedScripts = (ConsoleHandler,)

Como es esperable, la falta del método onOkHasFocus provoca una excepción.


Refer to Python calls to LibreOffice Basic page for getBasicScript routine description and for details about cross-language scripting execution.

Con LibreOffice Basic

En este ejemplo el cuadro de diálogo se ha incrustado en un documento, pero también podría igualmente localizarse en el equipo.

      ' <MyLib>.<Handler> module
      Public Sub Console_Show()
          Dim dp as Object '
          Dim dialog As Object ',
          Dim eventHandler As Object '
          dp = CreateUnoService("")
          dp.Initialize(Array(ThisComponent)) ' si se trata de un diálogo incrustado en el documento
          eventHandler = CreateUnoListener("Console_", "")
          dialog = dp.createDialogWithHandler("", eventHandler)
          dialog.Title = "Konsole"
      End Sub ' <Handler>.Console_Show()
      Private Function Console_callHandlerMethod(dialog as Object, _
              event As, _
              method As String) As Boolean
          ''' Intercept dialog events using .UNO protocol '''
          Console_callHandlerMethod = True
          Select Case method
              Case "_dump2File"
                  event.Source.setLabel("dump requested")
                  With GlobalScope.BasicLibraries
                      If Not .IsLibraryLoaded("Access2Base") Then .LoadLibrary("Access2Base")
                  End With
              Case "_openHelp" 
                  MsgBox "Not yet implemented",0 , "Howdy"
                  'dialog.endDialog(1) if computer-based dialog
              Case Else : Console_callHandlerMethod = False
          End Select
      End Function ' <Handler>.Console_callHandlerMethod
      Private Function Console_getSupportedMethodNames()
          Console_getSupportedMethodNames = Array("_dump2File", "_openHelp")
      End Function ' <Handler>.Console _getSupportedMethodNames
      ' adapted from « Créer un dialogue avec gestionnaire d'événements » by JM Zambon

Como es esperable, la falta del método onOkHasFocus provoca una excepción.

¡Necesitamos su ayuda!