Search code examples
libreofficelibreoffice-basic

LibreOffice macro showing simple TextBox shape


I cannot figure out (or find an example) how to perform the following simple thing in the LibreOffice Calc 6.2:

I have a drawing shape (e.g. a simple rectangle) in a sheet (call it ShapeA) and a textbox shape in another sheet (call it TextboxB). I want to do the following: when I click on the ShapeA, the TextboxB must appear on the screen (without changing the current sheet, maybe in a dialog box) and then be closed with a mouse click.

I guess the macro associated with ShapeA could look something like this:

Sub Main
oDrawPage = ThisComponent.getDrawPage()
oTb = oDrawPage.getByName("TextBoxB")
oTb.show()
End Sub

Could someone advise what I should put into this macro to accomplish the described task?

UPDATE: What I want to accomplish ( in reply to Jim K.).

I have a very cluttered diagram with many shapes. Each shape has some textual information associated with it. There is not enough space on each shape or around it to contain this info. So there is must be a way to display this info about each shape. Also this information should be displayed in a preformatted way (it contains code and other structured info).

My plan is to create a textbox with the relevant information for each diagram shape, place these textboxes in other sheet and have a possibility, when viewing diagram, to click on any shape and view the associated info in the poped up textbox without leaving the diagram, and then close the textbox with a simple action (e.g. by clicking on it).

Does this task sound feasible to be realized with the LO's shapes and macros?


Solution

  • How about this: Put everything on the same sheet but keep the text boxes hidden until needed.

    Use the following code adapted from https://ask.libreoffice.org/en/question/93050/how-can-i-hideshow-a-shape-of-my-spreadsheet-using-a-macro/.

    Sub ShapeClickedA
        ShowHideShape("TextBoxA")
    End Sub
    Sub ShapeClickedB
        ShowHideShape("TextBoxB")
    End Sub
    
    Sub ShowHideShape(shapeName As String)
        oDrawPage = ThisComponent.getSheets().getByName("Sheet1").getDrawPage()
        For iShape = 0 To oDrawPage.Count - 1
            oShape = oDrawPage.getByIndex(iShape)
            If oShape.Name = shapeName Then
                If oShape.Visible Then
                    oShape.Visible = 0  'Not Visible
                Else
                    oShape.Visible = 1  'Visible
                End If
            End If
        Next iShape
    End Sub
    

    If you haven't yet, set the names of the text boxes by right-clicking and choosing Name... Then right click on both ShapeA and TextBoxA and assign the macro ShapeClickedA. Do likewise for other pairs of shapes. The result works like this:

    1. Before anything is clicked.

    no text boxes displayed

    1. Click on ShapeA. (To close it again, click on either ShapeA or TextBoxA). ShapeB functions similarly.

    TextBoxA displayed

    1. It's also possible to display both at the same time.

    TextBoxA and TextBoxB displayed