Python...MouseClick in ListBox

Keyboard macros or custom scripts

Python...MouseClick in ListBox

Postby kiloran » Mon Nov 10, 2014 1:01 pm

I'm looking at creating dialogs using Python, and I'm struggling to work out how to handle a mouseclick event.

Here's a stripped-to-basics version of my code:
Code: Select all   Expand viewCollapse view
# example of creating a dialog using Python
import uno
from com.sun.star.awt.PosSize import POSSIZE

def addAwtModel(oDM,srv,sName,dProps):
   oCM = oDM.createInstance("com.sun.star.awt.UnoControl"+ srv +"Model")
   while dProps:
      prp = dProps.popitem()
      uno.invoke(oCM,"setPropertyValue",(prp[0],prp[1]))
      oCM.Name = sName
   oDM.insertByName(sName,oCM)
   
def addShareDlg():
   ctx = uno.getComponentContext()
   smgr = ctx.ServiceManager
   oDM = smgr.createInstance("com.sun.star.awt.UnoControlDialogModel")
   oDM.Title = 'Test'
   
   oDialog = smgr.createInstance("com.sun.star.awt.UnoControlDialog")
   oDialog.setModel(oDM)
   
   addAwtModel(oDM,'ListBox','myListBox',{})
   listText = oDialog.getControl('myListBox')
   listText.addItem('aaa',1)
   listText.addItem('bbb',2)
   listText.setPosSize(10,10,400,300, POSSIZE)    # x, y, width, height, flag
   
   oDialog.setPosSize(20,0,500,500,POSSIZE)
   oDialog.setVisible(True)
   x = oDialog.execute()

I want to execute some code when I click on an element in the ListBox.
Can someone please point me in the right direction? Many thanks.
--kiloran
LibreOffice 6 on Windows 10 and Linux Mint
kiloran
 
Posts: 34
Joined: Sun Oct 14, 2012 2:06 pm

Re: Python...MouseClick in ListBox

Postby Villeroy » Mon Nov 10, 2014 6:41 pm

http://www.openoffice.org/api/docs/comm ... stBox.html
not sure about the difference between ActionListener and ItemListener. Check this out.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04, no OpenOffice, LibreOffice 6.4
User avatar
Villeroy
Volunteer
 
Posts: 28571
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Python...MouseClick in ListBox

Postby kiloran » Tue Nov 11, 2014 11:04 pm

I found some code which implements a listener. It adds a count to a label when a button is clicked.

I then tried adding a ListBox and attached the same listener code to the ListBox.
This essentially works and would form the basis of what I want to do. HOWEVER.... I don't understand why I only need to click once on the button to increment the count, but I need to double-click on an item in the ListBox to achieve the same thing. I would prefer a single-click on the ListBox.

Can anyone provide any guidance
Code: Select all   Expand viewCollapse view
import uno
import unohelper
from com.sun.star.awt import XActionListener

class MyActionListener( unohelper.Base, XActionListener ):
   def __init__(self, labelControl, prefix ):
      self.nCount = 0
      self.labelControl = labelControl
      self.prefix = prefix
       
   def actionPerformed(self, actionEvent):
      # increase click counter
      self.nCount = self.nCount + 1
      self.labelControl.setText( self.prefix + str( self.nCount ) )
      
      
def createDialog():
   """Opens a dialog with a push button and a label, clicking the button increases the label counter."""
   try:
      ctx = uno.getComponentContext()
      smgr = ctx.ServiceManager

      # create the dialog model and set the properties
      dialogModel = smgr.createInstanceWithContext("com.sun.star.awt.UnoControlDialogModel", ctx)

      dialogModel.PositionX = 10
      dialogModel.PositionY = 10
      dialogModel.Width = 200
      dialogModel.Height = 100
      dialogModel.Title = "Runtime Dialog Demo"
      
      # create listbox
      listBoxModel = dialogModel.createInstance("com.sun.star.awt.UnoControlListBoxModel" )
      listBoxModel.PositionX = 10
      listBoxModel.PositionY = 5
      listBoxModel.Width = 100
      listBoxModel.Height = 40
      listBoxModel.Name = "myListBoxName"
      listBoxModel.StringItemList = ('a','b','c')


      # create the button model and set the properties
      buttonModel = dialogModel.createInstance("com.sun.star.awt.UnoControlButtonModel" )
      buttonModel.PositionX = 50
      buttonModel.PositionY  = 50
      buttonModel.Width = 50
      buttonModel.Height = 14
      buttonModel.Name = "myButtonName"
      buttonModel.Label = "Click Me"

      # create the label model and set the properties
      labelModel = dialogModel.createInstance( "com.sun.star.awt.UnoControlFixedTextModel" )
      labelModel.PositionX = 10
      labelModel.PositionY = 70
      labelModel.Width  = 100
      labelModel.Height = 14
      labelModel.Name = "myLabelName"
      labelModel.Label = "Clicks "

      # insert the control models into the dialog model
      dialogModel.insertByName( "myButtonName", buttonModel)
      dialogModel.insertByName( "myLabelName", labelModel)
      dialogModel.insertByName( "myListBoxName", listBoxModel)

      # create the dialog control and set the model
      controlContainer = smgr.createInstanceWithContext("com.sun.star.awt.UnoControlDialog", ctx)
      controlContainer.setModel(dialogModel)
      
      oBox = controlContainer.getControl("myListBoxName")
      oLabel = controlContainer.getControl("myLabelName")
      oButton = controlContainer.getControl("myButtonName")
      oBox.addItem('d',4)

      # add the action listener
      oButton.addActionListener(MyActionListener( oLabel,labelModel.Label ))
      oBox.addActionListener(MyActionListener( oLabel,labelModel.Label ))
      
      # create a peer
      toolkit = smgr.createInstanceWithContext( "com.sun.star.awt.ExtToolkit", ctx) 

      controlContainer.setVisible(False)
      controlContainer.createPeer(toolkit, None)

      # execute it
      controlContainer.execute()

      # dispose the dialog
      controlContainer.dispose()
   except:
      pass

g_exportedScripts = createDialog,
--kiloran
LibreOffice 6 on Windows 10 and Linux Mint
kiloran
 
Posts: 34
Joined: Sun Oct 14, 2012 2:06 pm


Return to OpenOffice Basic, Python, BeanShell, JavaScript

Who is online

Users browsing this forum: No registered users and 3 guests