Manipulating an Image Button in a Calc Sheet

I’d like to develop a Basic macro to add/remove an Image Button Form Control from a Calc sheet but can’t seem to find any information related to doing that in a macro. As a work-around, I’ve added the button manually in Form Design mode and use the model.EnableVisible property to show/hide it. I can successfully change the image associated with the control by setting the model.imageURL. I also would like to be able to manipulate the position and size of such a button control. But I cannot find any information related to manipulating the PositionX/Y or the Height/Width of the button control. It appears that position and size information is held within the associated Dialog Model, but I cannot find a way to obtain the Dialog Model to obtain those properties.

  1. How do I add/remove an Image Button via Basic macro?
  2. How can I obtain the Dialog Model associated with the button control?
  3. How do I change the button PositionX/PositionY and Height/Width via macro?

Hello,

What you are asking for is a lot of information as so many things interact with each other. Will present what may be just a start for information.

First, you discuss the model, inserting and setting position and size of a control. From StarOffice 7 Basic Programmers Guide:

The Three Aspects of a Control Element Form

A control element of a form has three
aspects:

  • First, there is the Model of the control element. This is the key
    object for the StarOffice
    Basic-programmer when working with
    control element forms.

  • The counterpart to this is the View of the control element, which
    administers the display information.

  • Since control element forms within the documents are administered like a
    special drawing element, there is also
    a Shape object which reflects the
    drawing element-specific properties of
    the control element (in particular its
    position and size).

The StarOffice guide PDF is here → https://www.mitomap.org/foswiki/pub/MITOWIKI/ArchivedProjectDataCuration/StarOffice_7_Basic_Programmers_Guide.pdf

A few other good references:

OpenOffice macros explained by Andrew Pitonyak → http://www.pitonyak.org/OOME_3_0.pdf

Andrew Base → http://www.pitonyak.org/database/AndrewBase.pdf

There is also some code in regard to you question. See → Creating Buttons and Assigning Code from Macro

Now that contains the instance for a command button. Here is a page with other controls (including image control) → com::sun::star::form::component Module Reference

This should be enough to get you started in the right direction.

Edit:

Here is the code from my comment and the pieces needed from the link and the control for image button:

Option Explicit

Sub CreateButton
    Dim oDoc As Object
    Dim oSheet As Object
    Dim oDrawPage As Object
    Dim oButtonModel As Object
    Dim sScriptURL As String
    DeleteControl
    oDoc = ThisComponent
    oSheet = oDoc.Sheets.getByIndex(0)
    oDrawPage = oSheet.DrawPage 
    sScriptURL = "vnd.sun.star.script:Standard.Module1.ButtonPushEvent?language=Basic&location=document"
    oButtonModel = AddNewButton("Button_1", "Button_1", oDoc, oDrawPage)
End Sub

Function AddNewButton(sName As String, sLabel As String, oDoc As Object, oDrawPage As Object) As Object
    Dim oControlShape As Object
    Dim aPoint
    Dim aSize
    Dim oButtonModel 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.ImageButton")
    oButtonModel.Name = sName
    oButtonModel.imageURL = "YOUR_IMAGE_LOCATION_HERE"
    oControlShape.setControl(oButtonModel)
    oDrawPage.add(oControlShape)
    AddNewButton = oButtonModel
End Function

Sub DeleteControl
    Dim Doc As Object
    Dim Shape as Object
    Dim Sheet as Object
    Dim DrawPage as Object
    Dim I as integer
    Doc = ThisComponent
    Sheet = Doc.Sheets.getByIndex(0)
    DrawPage = Sheet.DrawPage
    For i = 0 to DrawPage.Count - 1
        Shape = DrawPage(i)
        If HasUnoInterfaces(Shape, "com.sun.star.drawing.XControlShape") Then
            If Shape.Control.Name = "Button_1" Then
                DrawPage.remove(Shape)
                Exit For
            End If
       End If
    Next
End Sub

This now deletes previous image button on first sheet and inserts a new one. You must insert your location for the image button. Will delete comment code relating to the deleting of the control.

Edit #2:

Also be aware there is an open bug report → tdf#46579

Many thanks @ratslinger for the numerous references. Before I posted my question, I had looked thru several of those references without finding what I was looking for. Undoubtedly, the answers are there.
But the object model is complex and I readily acknowledge that I often (always?) get lost in the abstraction long before I get to an answer. And reading thru (let alone understanding) the nearly 300-page StarOffice Programmers Guide is a months-long endeavor. Andrew Pitonyak’s references have long been a life-saver by providing working examples that may not do exactly what I want, but give me enough of a start that I can find a way to my goal. In this case, your link above to the ‘Creating Buttons and Assigning Code from Macro’ thread in the OpenOffice Forum provides some promising leads which I’ll pursue.

Again, many thanks for the pointers. When I find my way, I’ll try to post an update here as an assist for others struggling like I am. I’ll mark this as answered then.

@Chucko1,

Dealing with the API is not a days, weeks or months effort but rather years. But don’t let that be a deterrent. Items fit together over time. It took me quite a while to get some grasp on the MVS paradigm (Model-View-Shape).

If you need further help, it would be best to ask the question about a specific instead of the broad range question presented here. Post what code you may have. Samples are always helpful.

Also, the code in the link does present how to insert a button, size it and set the position. In deleting a control, you may need to delete it from the DrawPage. Do not have an example of this currently at hand. As for getting the model, obtaining this, the view & the shape are noted in the StarOffice guide on page #249 (which is immediately after the quoted section in my answer.

I believe this answers your specific questions in the post.

Thanks @Ratslinger for going above and beyond in helping with this question. The code you provided in your edit does just what I wanted. Also, thanks for pointing out the related bug report.