From 4ff9ffa9f74290ecc8dae66034e16fda64aa369f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LibreO=EF=AC=83ciant?= Date: Mon, 8 Apr 2019 15:38:23 +0100 Subject: [PATCH] New Python page + updates to existing pages to support embedded references Change-Id: Ie7bd884db45e3ca59dee1ffa79b2ea14ced3d160 Reviewed-on: https://gerrit.libreoffice.org/70420 Tested-by: Jenkins Reviewed-by: Olivier Hallot --- AllLangHelp_sbasic.mk | 1 + source/text/sbasic/guide/sample_code.xhp | 6 +- source/text/sbasic/python/python_examples.xhp | 2 +- source/text/sbasic/python/python_listener.xhp | 163 ++++++++++++++++++ .../text/sbasic/python/python_programming.xhp | 2 +- 5 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 source/text/sbasic/python/python_listener.xhp diff --git a/AllLangHelp_sbasic.mk b/AllLangHelp_sbasic.mk index 9325e065f6..c4d4a0c149 100644 --- a/AllLangHelp_sbasic.mk +++ b/AllLangHelp_sbasic.mk @@ -374,6 +374,7 @@ $(eval $(call gb_AllLangHelp_add_helpfiles,sbasic,\ helpcontent2/source/text/sbasic/python/python_examples \ helpcontent2/source/text/sbasic/python/python_ide \ helpcontent2/source/text/sbasic/python/python_import \ + helpcontent2/source/text/sbasic/python/python_listener \ helpcontent2/source/text/sbasic/python/python_locations \ helpcontent2/source/text/sbasic/python/python_platform \ helpcontent2/source/text/sbasic/python/python_programming \ diff --git a/source/text/sbasic/guide/sample_code.xhp b/source/text/sbasic/guide/sample_code.xhp index 334d88cd08..cc3ab51d84 100644 --- a/source/text/sbasic/guide/sample_code.xhp +++ b/source/text/sbasic/guide/sample_code.xhp @@ -39,8 +39,10 @@ examples; programming controls dialog editor;programming examples for controls -Programming Examples for Controls in the Dialog Editor - +
+ Programming Examples for Controls in the Dialog Editor + +
The following examples are for a new dialog called "Dialog1". Use the tools on the Toolbox bar in the dialog editor to create the dialog and add the following controls: a Check Box called "CheckBox1", a Label Field called "Label1", a Button called "CommandButton1", and a List Box called "ListBox1". Be consistent with uppercase and lowercase letter when you attach a control to an object variable. Global Function for Loading Dialogs diff --git a/source/text/sbasic/python/python_examples.xhp b/source/text/sbasic/python/python_examples.xhp index 779bfddef1..2fadd429c0 100644 --- a/source/text/sbasic/python/python_examples.xhp +++ b/source/text/sbasic/python/python_examples.xhp @@ -28,13 +28,13 @@ +
diff --git a/source/text/sbasic/python/python_listener.xhp b/source/text/sbasic/python/python_listener.xhp new file mode 100644 index 0000000000..3a0796f7f9 --- /dev/null +++ b/source/text/sbasic/python/python_listener.xhp @@ -0,0 +1,163 @@ + + + + + + Python Listeners : Creating Event Listeners + /text/sbasic/python/python_listener.xhp + + + + + Python;Event Listener + Python;createUnoListener + Basic;Event Listener + +
+

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 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 canceled 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 %PRODUCTNAME 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. +
+ + CreateUnoListener Function + Events mapping to objects + See also Document events, Form events. + + +
+ +
diff --git a/source/text/sbasic/python/python_programming.xhp b/source/text/sbasic/python/python_programming.xhp index 80c5ff5162..5b3998ef60 100644 --- a/source/text/sbasic/python/python_programming.xhp +++ b/source/text/sbasic/python/python_programming.xhp @@ -202,7 +202,7 @@ - See Opening a Dialog + See Opening a Dialog CreateUnoDialog()