Jump to content

Add an onClick Action to a shape

From The Document Foundation Wiki


Description

This macro will create a com.sun.star.drawing.RectangleShape[1] and assign a com.sun.star.presentation.ClickAction[2] to it that launches a Macro. See API Docs link for more ActionTypes.

Code

LibreOffice Basic

REM  *****  BASIC  *****

Option Explicit

Sub InsertRectangleShapeWithOnClick()
    '''Insert a RectangleShape to the currently selected slide'''

    Dim oDoc As Object ' com.sun.star.lang.XComponent
    Dim oSlide As Object ' com.sun.star.drawing.XDrawPage
    Dim oRectangle As Object ' com.sun.star.drawing.RectangleShape
    Dim oSize As New com.sun.star.awt.Size
    Dim oPosition As New com.sun.star.awt.Point

    oDoc = ThisComponent
    oSlide = oDoc.CurrentController.CurrentPage
    oRectangle = oDoc.createInstance("com.sun.star.drawing.RectangleShape")

    With oSlide
        .add(oRectangle)
        oPosition.X = .7*(.Width - oSize.Width)
        oPosition.Y = .7*(.Height - oSize.Height)
    End With ' oSlide

    oSize.Width = 5000 ' 50 mm
    oSize.Height = 3000 ' 30 mm
    With oRectangle
        .setSize(oSize)
        .setPosition(oPosition)
        .FillColor = RGB(0, 0, 255) ' BLUE
        .FillStyle = com.sun.star.drawing.FillStyle.SOLID ' or .NONE
        .FillTransparence = 30 ' 0-100 from opaque to transparent

        .LineStyle = com.sun.star.drawing.LineStyle.SOLID ' or .NONE
        .LineWidth = 100 ' 1 mm
        .LineColor = RGB(0, 0, 0) ' BLACK

        .setName("MyBasicRectangle")
        .setString("Click Me!")
        .CharFontName = "Liberation Sans"
        .CharColor = RGB(255, 255, 255) ' WHITE
        .ZOrder = 1

        'on Click Action is defined here
        .onClick = com.sun.star.presentation.ClickAction.MACRO
        .bookmark = "vnd.sun.star.script:Standard.Module1.MyOnClickMacro?language=Basic&location=document"
    End With ' oRectangle


End Sub ' InsertRectangleShape

Sub MyOnClickMacro()
    MsgBox("You clicked on the Shape!")
End Sub

ODP file to run macro

Notes