hi LO-folks,
I’m trying hard to register an ImageControl Button in a non-modal dialog with an event listener (basic macro). I found a similar question that was answered by Ratslinger about 4 years ago, but it’s about usage in a forms document. My intention is to start the dialog with no LO-doc loaded yet. Once the user has made some choices a calc-doc will be loaded later.
I’ve got the macro working ok when I use the dialog editor, but the downside there is, that the reference to the image is kind of “hard-coded”. To avoid that I’d like to create the image-ctrl-button on the fly when the dialog is starting. Creating the button works fine, but the registration using something like oCbutton.registerScriptEvent(x,aEvent) fails with basic runtime error: Eigenschaft oder Methode nicht gefunden: registerScriptEvent
i.e. the registration Method seems not to be supported in a dialog? Is there another way I can achieve this?
Thank’s a lot for any help or hint,
Charly
maybe show your code and event config …
hello Pierre, thank’s for responding!
the dialog is created with the dialog editor, omitting the CBStart button.
this is my test code:
global Continue as boolean
' Aufruf: soffice macro:///ClubBH.nonmodaldlg.StartDialog'
Sub StartDialog()
Dim oLib As Object, oDlg As Object, oICBL as object, oImgCTl as object
dim nx as integer, nStep as integer, sImgUrl as string, sScriptURL as string
dim oEvent as object, oModel as object
''
DialogLibraries.LoadLibrary("ClubBH")
oLib = DialogLibraries.GetByName("ClubBH")
oDlg = CreateUnoDialog(oLib.GetByName("Dialog1"))
fnSetGeneralDefaults
oDlg.setVisible(true)
' create listener '
oICBL=createUnoListener("ICBL_", "com.sun.star.awt.XMouseListener")
' create control button'
dim ax()
ax = oDlg.getControls() : nx=UBound(ax)+1 : nStep=0
sImgUrl=ConvertToURL("/media/charly/Data/Club/BH/images/OOmyBH.png")
fnInsertACtl(oDlg.GetModel, nx,"CBStart","com.sun.star.awt.UnoControlImageButtonModel",_
Array("PositionX",121, "PositionY",82, "Width",30, "Height",28, "Step",nStep,_
"ImageURL",sImgUrl,"ScaleImage", true,_
"HelpText","help me") )
oImgCTl=oDlg.getControl("CBStart")
oImgCTl.addEventListener(oICBL)
' assign action '
sScriptURL=ConvertToURL("/ClubBH.nonmodaldlg.MouseButtonPressed") 'the exec function'
Set oEvent = CreateUnoStruct("com.sun.star.script.ScriptEventDescriptor")
With oEvent
.AddListenerParam = ""
.EventMethod ="mousePressed" ' sEvent'
.ListenerType ="XMouseListener"
.ScriptType = "Script" ' Better than "Basic"'
.ScriptCode = sScriptURL
End With
oDlg.registerScriptEvent(nx,oEvent) ' <<- error: method registerScriptEvent not found '
' I also tried: oImgCTl.registerScriptEvent(nx,oEvent) <<- this fails'
' and oModel=oDlg.GetModel : oModel.registerScriptEvent(nx,oEvent) <<- this fails'
Continue = true
dim ct as integer
do while Continue
wait 500 : ct=ct+1
if ct > 100 then Continue=false ' this is for tests only'
loop
oImgCTl.removeEventListener(oICBL)
MsgBox ("Dlg ended")
End Sub
Sub MouseButtonPressed(ByRef oEvent As Variant)
MsgBox("MouseButtonPressed 1 ")
Continue=false
end sub
Sub ICBL_mousePressed(ByRef oEvent As Variant)
MsgBox("MouseButtonPressed 2 ")
Continue=false
end sub
Function fnInsertACtl(oDlgModel,index%,sName$,sType$,props())
Dim oModel
oModel = oDlgModel.createInstance(sType$)
fnSetProps(oModel, Array("Name", sName$, "TabIndex", index%))
fnSetProps(oModel, props())
oDlgModel.insertByName(sName$, oModel)
End function
Function fnSetProps(oModel, props())
Dim i As Integer
For i=LBound(props()) To UBound(props()) Step 2
oModel.setPropertyValue(props(i), props(i+1))
Next i
End Function
You draw an ordinary push button on your dialog and assign the graphics property to some picture file. Then you assign any of the available script events to your macro.
hi Villeroy, thank’s for your response, but that’s how my dialogs (5 in total) currently work ok. However, it also is exactly wat I’m trying to avoid! I want to create the button on the fly after loading the dialog. The reason is simply to be more flexible when copying the application to a different environment. All I have to “adjust” then is the “installpath”, while currently each “hardcoded” reference to an image for a button has to be changed.
A different environment other than LibreOffice or OpenOffice? If you distribute your project as an extension package, it is possible to declare all picture paths relative to the extension’s installation path. Don’t ask me for the details, but this is how extensions handle pictures.
Runtime listeners in StarBasic are difficult to implement because StarBasic does not support object oriented programming. However, it is possible. See CreateUnoListener Function
hi Villeroy, thank’s for the hint, I finally got it running the way I wanted it! There’s only one item of criticism left: You have to provide a sub for each of the possible events fired by the listener. Yes, it is according to documentation, but couldn’t there be a “general-other-events” - sub, which would satisfy all the events you’re not interested in? Do you think that’s worth opening an enhancement request for LO? Writing the extra subs is not really the problem, but finding out which events there are, cause each listener type has it’s own set of events…
But thank’s again for your help!
With Python as macro language, the implemention is easier and more flexible.