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 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 vnd.sun.star.UNO: 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.

The advantages of this approach are:

  • It packs the code that handles event-driven macros,
  • it decorrelates events from macros names which facilitates maintenance or updates, in particular when moving macros or modules.

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.

Assigning Dialog methods

Export Access2Base dlgTrace dialog and import it into MyLib application library.

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:

  • Set Dump to file dialog button component method name to -dump2File
  • Optionally define txtTracelog key pressed and mouse button pressed events component method names as -openHelp
  • Optionally define Ok button receiving focus event component method name as onOkHasfocus

Events assigned actions should mention the vnd.sun.star.UNO: protocol.

Creating the handler

createDialogWithHandler method of com.sun.star.awt.DialogProvider2 service is used to set the dialog and its handler. The handler is responsible for implementing com.sun.star.awt.XDialogEventHandler interface.

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

With Python

In this example the dialog is located on the computer.


      # -*- coding: utf-8 -*-
      from --future-- import unicode-literals
          
      import uno, unohelper
      from com.sun.star.awt import XDialogEventHandler
          
      -DLG-PROVIDER = "com.sun.star.awt.DialogProvider2"
          
      class Console(unohelper.Base, XDialogEventHandler):
          """ Access2Base Console Handler """
          ''' adapted from « Créer un dialogue avec gestionnaire d'événements » by JM Zambon
          https://wiki.openoffice.org/wiki/FR/Documentation/Python/Creating-Dialog-with-Handler '''
          def show(self):
              dialog = self.getDialog("MyLib.dlgTrace", embedded=True)
              dialog.Title = "Konsole"
              dialog.execute()
          
          def callHandlerMethod(self, dialog, event, method):
              if method == '-dump2File':
                  event.Source.setLabel("dump requested")
                  scr = getBasicScript(script="-DumpToFile", module="Trace",
                      library='Access2Base')
                  scr.invoke((event,), (), ())
              elif method == '-openHelp':
                  -msgbox('Not yet implemented')
                  dialog.endDialog(1)
              else:
                  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"
              else:
                  dp = smgr.createInstanceWithContext(-DLG-PROVIDER, ctx)
                  location = "?location=application"
              dlg = dp.createDialogWithHandler("vnd.sun.star.script:"+libr-dlg+location, self)
              return dlg
          
      
      # def getBasicScript()  # see note
           
      def -msgbox(prompt='', title=''):
          ''' Ugly MsgBox '''
          import msgbox
          mb = msgbox.MsgBox(uno.getComponentContext())
          mb.addButton('Howdy')
          mb.show(prompt,0,title)
          
      def ConsoleHandler():
          Console().show()
          
      g-exportedScripts = (ConsoleHandler,)
          
      

As expected, onOkHasFocus missing method throws an exception.

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

With Office Basic

In this example the dialog is embedded in a document, and can equally be located on the computer.


      ' . module
          
      Public Sub Console-Show()
          Dim dp as Object ' com.sun.star.awt.DialogProvider2
          Dim dialog As Object ' com.sun.star.awt.XDialog, com.sun.star.awt.XDialogEventHandler
          Dim eventHandler As Object ' com.sun.star.awt.XDialogEventHandler
          dp = CreateUnoService("com.sun.star.awt.DialogProvider2")
          dp.Initialize(Array(ThisComponent)) ' if doc-embedded dialog
          eventHandler = CreateUnoListener("Console-", "com.sun.star.awt.XDialogEventHandler")
          dialog = dp.createDialogWithHandler("vnd.sun.star.script:MyLib.dlgTrace?location=document", eventHandler)
          dialog.Title = "Konsole"
          dialog.execute()
      End Sub ' .Console-Show()
          
      Private Function Console-callHandlerMethod(dialog as Object, -
              event As com.sun.star.document.DocumentEvent, -
              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
                  Access2Base.Trace.-DumpToFile
              Case "-openHelp" 
                  MsgBox "Not yet implemented",0 , "Howdy"
                  'dialog.endDialog(1) if computer-based dialog
              Case Else : Console-callHandlerMethod = False
          End Select
      End Function ' .Console-callHandlerMethod
          
      Private Function Console-getSupportedMethodNames()
          Console-getSupportedMethodNames = Array("-dump2File", "-openHelp")
      End Function ' .Console -getSupportedMethodNames
          
      ' adapted from « Créer un dialogue avec gestionnaire d'événements » by JM Zambon
      ' https://wiki.openoffice.org/wiki/FR/Documentation/Python/Creating-Dialog-with-Handler
      

As expected, onOkHasFocus missing method throws an exception.