Python macro: Dialog controls not responding to mouse clicks

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
frowdow
Posts: 3
Joined: Mon Nov 17, 2014 4:23 pm

Python macro: Dialog controls not responding to mouse clicks

Post by frowdow »

I have a python macro to display a dialog consisting of 2 radio buttons, 2 check boxes, and 2 default buttons, OK and CANCEL. I notice that only the first check box is responding to mouse clicks. But all controls are responding when i move to them using the keyboard and tabbing them. If anyone has any quick insight into why this might be the case, i would be most grateful. The code i have is shown below:
Thanks,
Sachin

Code: Select all


def inputbox(message, title="", default="", x=None, y=None):
    """ Shows dialog with input box.
        @param message message to show on the dialog
        @param title window title
        @param default default value
        @param x dialog positio in twips, pass y also
        @param y dialog position in twips, pass y also
        @return string if OK button pushed, otherwise zero length string
    """
    WIDTH = 300
    HORI_MARGIN = VERT_MARGIN = 8
    BUTTON_WIDTH = 100
    BUTTON_HEIGHT = 26
    HORI_SEP = VERT_SEP = 8
    LABEL_HEIGHT = BUTTON_HEIGHT * 2 + 5
    EDIT_HEIGHT = 24
    HEIGHT = 150
    import uno
    import unohelper
    from com.sun.star.awt.PosSize import POS, SIZE, POSSIZE
    from com.sun.star.awt.PushButtonType import OK, CANCEL
    from com.sun.star.util.MeasureUnit import TWIP
    from com.sun.star.awt import XActionListener
    from com.sun.star.awt import XItemListener

    ctx = uno.getComponentContext()
    def create(name):
        return ctx.getServiceManager().createInstanceWithContext(name, ctx)
    dialog = create("com.sun.star.awt.UnoControlDialog")
    dialog_model = create("com.sun.star.awt.UnoControlDialogModel")
    dialog.setModel(dialog_model)
    dialog.setVisible(False)
    dialog.setTitle(title)
    dialog.setPosSize(0, 0, WIDTH, HEIGHT, SIZE)
    def add(name, type, x_, y_, width_, height_, actListener, props):
        model = dialog_model.createInstance("com.sun.star.awt.UnoControl" + type + "Model")
        dialog_model.insertByName(name, model)
        control = dialog.getControl(name)
        control.setPosSize(x_, y_, width_, height_, POSSIZE)
        if actListener != 0:
            control.addItemListener(actListener(dialog, name))
        for key, value in props.items():
            setattr(model, key, value)
    class radioListener( unohelper.Base, XItemListener ):
        def __init__(self, cCont, name):
            self.nCount = 0
            self.cCont = cCont
            self.cName = name
        def itemStateChanged(self, actionEvent):
            cCont = self.cCont
            oCont = cCont.getControl(self.cName)
            oCont.setLabel("YIPPEE")
    add("Chk1", "CheckBox", 10, 5, 150, 150, 0, {"Label": "Show redeemed funds"})
    add("Chk2", "CheckBox", 20, 45, 150, 150, 0, {"Label":"By Fund Subtype"})
    add("Radio1", "RadioButton", 10, 25, 150, 150, radioListener, {"Label":"Fund Type", "State": 1})
    add("Radio2", "RadioButton", 10, 65, 150, 150, radioListener, {"Label":"By Fund AMC", "State": 0})
    """
    """
    add("btn_ok", "Button", 10, 85, 
            BUTTON_WIDTH, BUTTON_HEIGHT, 0, {"PushButtonType": OK})
    add("btn_cancel", "Button", 160, 85, 
            BUTTON_WIDTH, BUTTON_HEIGHT, 0, {"PushButtonType": CANCEL})
    frame = create("com.sun.star.frame.Desktop").getCurrentFrame()
    window = frame.getContainerWindow() if frame else None
    dialog.createPeer(create("com.sun.star.awt.Toolkit"), window)
    if not x is None and not y is None:
        ps = dialog.convertSizeToPixel(uno.createUnoStruct("com.sun.star.awt.Size", x, y), TWIP)
        _x, _y = ps.Width, ps.Height
    elif window:
        ps = window.getPosSize()
        _x = ps.Width / 2 - WIDTH / 2
        _y = ps.Height / 2 - HEIGHT / 2
    dialog.setPosSize(_x, _y, 0, 0, POS)
    dialog.execute()
    dialog.dispose()
    return 0
#This function can be called as follows:
def inpmacro(*args):
    inputbox("Please input some value", "Input", "Default value")
OpenOffice 3.1 Windows 8.1
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python macro: Dialog controls not responding to mouse cl

Post by Villeroy »

I think your controls are too big and overlapping. Try to resize them by means of http://www.openoffice.org/api/docs/comm ... rains.html
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
frowdow
Posts: 3
Joined: Mon Nov 17, 2014 4:23 pm

Re: Python macro: Dialog controls not responding to mouse cl

Post by frowdow »

Spot on. But then i observed that the labels for the controls were not getting displayed. So i pushed the code for sizing the controls using getPreferredSize() to after setting the labels, and then it worked. Where is all this documented? Seems a little arbitrary to me. Anyway, thanks a ton, Villeroy.
OpenOffice 3.1 Windows 8.1
User avatar
Villeroy
Volunteer
Posts: 31269
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python macro: Dialog controls not responding to mouse cl

Post by Villeroy »

Root node of the API reference. This is more or less automatically generated from the source code: http://www.openoffice.org/api/docs/comm ... le-ix.html

Developers Guide: https://wiki.openoffice.org/wiki/Docume ... pers_Guide

Self-documentation GUI: viewtopic.php?f=74&t=49294

Improved GUI for Python macros: https://extensions.libreoffice.org/exte ... for-python
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply