[Solved] Assign event to button by copying from GUI example?

Creating a macro - Writing a Script - Using the API

[Solved] Assign event to button by copying from GUI example?

Postby MisterBill » Fri Dec 11, 2009 8:09 pm

Hello. I've cobbled together some code that lets me populate a form with buttons via a macro as sort of a poor-man's image map of garden plots for a little application I'm writing. The user defines the dimensions of the bed in a database. When that bed is clicked, my code builds a "drawing" of it using a command button for each square foot "plot". When the user clicks on a plot, the code opens a form with 144 "sub-plot" buttons (one for each square inch in the selected square-foot plot). Each of these buttons can be populated with a plant ID (by selecting the plant by name from a drop-down list and then clicking on all of the square-inch buttons where you're planting that particular species). When you're done, it writes all the changes to a database, along with the date planted and some other information. I've got this all working except for the part where clicking one of the buttons the code drew getting you to the "sub-plot" screen. I can add an event (using the GUI) to one of the code-created buttons and that works, but I want to be able to include the event action in the button properties when they're created. I've found lots of information about adding event listeners, etc, but 1) they seem more complicated than what I'm trying to do should be, and 2) even if I were going to use that method, I can't seem to find a way to list the "event" properties for one of my manually edited controls to see how the event is defined there, so I can copy it. I can't find any properties or methods using xray which seem to apply. Any suggestions? Can I add an event to the form the buttons are on somehow, and then use something like:

Code: Select all   Expand viewCollapse view
   oThisDoc = thisComponent.getDrawPage()
   oForms =oThisDoc.getForms()
   oForm = oForms.getByName("Standard")
   oPlantList = oForm.getByName("PlantList")
   oShovel = oForm.getByName("btnShovel")
   


to identify the button that was clicked (they all have unique names based on the plot they represent) and do it that way? Suggestions would be much appreciated.

Code: Select all   Expand viewCollapse view

Sub AddButton(Xsize as integer,Ysize as integer,color as integer, oButtonName as string, Xpos as integer, Ypos as integer)

Dim frm
Dim oPage
Dim pt as new com.sun.star.awt.Point ' a point structure that will be used to demonstrate
' how to move the created listbox after it has been placed on the spreadsheet

   oPage = thisComponent.getDrawPage()

frm = oPage.Forms.getByIndex(0)

oButtonModel = createUnoService("com.sun.star.form.component.CommandButton")
'xray oButtonModel
PropNames = Array( "Name", "Enabled", "BackgroundColor" )
PropValues = Array(oButtonName,True,color)
setProperties(oButtonModel,PropNames,PropValues)

oShape = thisComponent.createInstance("com.sun.star.drawing.ControlShape")
Dim pos as new com.sun.star.awt.Point
pos.X = Xpos
pos.Y = Ypos
oShape.Position = pos
Erase pos
Dim size as New com.sun.star.awt.Size
size.Width = Xsize
size.Height = Ysize
oShape.Size = size
Erase size

'Assign the model to the shape
oShape.Control = oButtonModel
oShape.AnchorType=0

'insert the control model into the first form of the forms collection of the draw page for the document
frm.insertByIndex(0,oButtonModel)
'Add the shape to the drawpage

oPage.add(oShape)

'xray oShape
End Sub
Last edited by Hagar Delest on Sun Dec 13, 2009 11:33 pm, edited 1 time in total.
Reason: tagged [Solved].
Ubuntu 8.04, OpenOffice.org 2.4.1
User avatar
MisterBill
 
Posts: 19
Joined: Sat Dec 05, 2009 5:25 pm
Location: Lincoln, Nebraska, USA

Re: Code to assign event to button by copying from GUI example?

Postby hanya » Sat Dec 12, 2009 8:42 am

Copying the control through API is not good solution because the event of the form control is managed by its index of the control in the css.form.FormComponents container. I think copying is working if it dose through dispatch.
You can assign event for new contorl like the following. If you assign the same event to some button, reuse the ScriptEventDescriptor.
Code: Select all   Expand viewCollapse view
Sub CreateButton
  oDoc = ThisComponent
  oDrawPage = oDoc.getDrawPage()
 
  sScriptURL = "vnd.sun.star.script:Standard.Module1.ButtonPushEvent?language=Basic&location=application"
  oButtonModel = AddNewButton("Button_1", "Button_1", oDoc, oDrawPage)
  oForm = oDrawPage.getForms().getByIndex(0)
  ' find index inside the form container
  nIndex = GetIndex(oButtonModel, oForm)
  AssignAction(nIndex, sScriptURL, oForm)
End Sub

' assign sScriptURL event as css.awt.XActionListener::actionPerformed.
' event is assigned to the control described by the nIndex in the oForm container
Sub AssignAction(nIndex As Integer, sScriptURL As String, oForm As Object)
  aEvent = CreateUnoStruct("com.sun.star.script.ScriptEventDescriptor")
  With aEvent
    .AddListenerParam = ""
    .EventMethod = "actionPerformed"
    .ListenerType = "XActionListener"
    .ScriptCode = sScriptURL
    .ScriptType = "Script"
  End With
 
  oForm.registerScriptEvent(nIndex, aEvent)
End Sub


Function AddNewButton(sName As String, sLabel As String, oDoc As Object, oDrawPage As Object) As Object
  oControlShape = oDoc.createInstance("com.sun.star.drawing.ControlShape")

  aPoint = CreateUnoStruct("com.sun.star.awt.Point")
  aSize = CreateUnoStruct("com.sun.star.awt.Size")
  aPoint.X = 1000
  aPoint.Y = 1000
  aSize.Width = 3000
  aSize.Height = 1000
  oControlShape.setPosition(aPoint)
  oControlShape.setSize(aSize)

  oButtonModel = CreateUnoService("com.sun.star.form.component.CommandButton")
  oButtonModel.Name = sName
  oButtonModel.Label = sLabel
 
  oControlShape.setControl(oButtonModel)
  oDrawPage.add(oControlShape)
 
  AddNewButton = oButtonModel
End Function


Function GetIndex(oControl As Object, oForm As Object) As Integer
  Dim nIndex As Integer
  nIndex = -1
  For i = 0 To oForm.getCount() - 1 step 1
    If EqualUnoObjects(oControl, oForm.getByIndex(i)) Then
      nIndex = i
      Exit For
    End If
  Next
  GetIndex = nIndex
End Function


Sub ButtonPushEvent(ev as com.sun.star.awt.ActionEvent)
  msgbox ev.Source.Model.Name
End Sub
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
hanya
Volunteer
 
Posts: 884
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

[Solved] Re: Code to assign event to button by copying from

Postby MisterBill » Sun Dec 13, 2009 10:37 pm

Actually, I ended up using Villeroy's suggestion here: http://user.services.openoffice.org/en/forum/viewtopic.php?f=20&t=25499&p=116386#p116386 along with parts of what you've taught me. Thank you very much. Domo arigato gozaimashita! (?)
Ubuntu 8.04, OpenOffice.org 2.4.1
User avatar
MisterBill
 
Posts: 19
Joined: Sat Dec 05, 2009 5:25 pm
Location: Lincoln, Nebraska, USA


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 1 guest