Manage mouse events in Calc spreadsheets

Hi,
I’ve just started studying LibreOffice Basic. I have extensively used VBA with Excel, now I would like to export some jobs on LO. The feature I’m struggling to export now is offering a cell context menu (popup menu against R-click) based on the cell content. The first thing I need to understand is how to kill the default context menu, the new popup, in fact, must be prepared on the fly. Here’s what I’ve done and consequent questions:

  • With Excel, I’ve done an .xlsm that simply suppresses the context menu in the range c3:f6 (using Worksheet_BeforeRightClick). I’ve loaded with Calc and found it works as expected with the only delta that in Calc the selected cell does not follow the R-Click when inside the C3:F6 range. If i try to save the file ( .xlsm or .odf same effect), the macro does not work any more, popping up the menu also inside the C3:F6 range. Q>> Do I miss something before/during the save? <<
  • I’ve found one code example and read the documentation of the objects used in it, in order to implement the above function in native LO Basic. I’ve used “com.sun.star.awt.XMouseClickHandler”:
    oMouseClickHandler = createUnoListener(“MouseOnClick_”, “com.sun.star.awt.XMouseClickHandler”)
    ThisComponent.getCurrentController().addMouseClickHandler(oMouseClickHandler)
    I’ve implemented MouseOnClick_mousePressed with a simple code:
    MouseOnClick_mousePressed = true
    Exit Function
    According to mousePressed() documentation for the XMouseClickHandler, returning TRUE should stop the other handlers waiting to process the event. The right-click pressure is captured, but the default popup is drawn after exiting the function. Q>> Since the original xlsm worked in LO, which interface should I use? <<. Q>> Alternatively: is it a bug in mousePressed()? <<.
    Thanks
    Stefano

⇒rightclick on SheetTab ⇒ Events ⇒ Rightclick … ⇒

def suppress_context( event ):
    addr = event.CellAddress
    # from Row 3 to 6 …… and …… Column C to F
    if (2 <= addr.Row <= 5) and (2 <= addr.Column <= 5):
        return True

Hi karolus,
not clear to me what you mean with the snippet.
suppress_context is not a methode defined somewhere, at least i haven’t found it in LO SDK API and LO help.
I initially put the same code in mousePressed … that is directly called after the R-Click, but as i explained before, returning true does not skip the pending action (default popup shown).

Thanks

Its defined here, straight above your comment!
All you have to do, is to install apso.oxt from there and use it to create and manage python-scripts in LO

Hi,
sorry i’ve completely missed your very first
“⇒rightclick on SheetTab ⇒ Events ⇒ Rightclick … ⇒”
Now i’ve got it … just create a function and assign to R-Click event.
I’ve done in Basic and it works. THANKS!!

Anyway, since i would like to acquire know how on SDK, if anyone could give me some tips about XMouseClickHandler and its broadcaster. I see that i can stop left click actions returning true, but no way for right click. May be there’s a more suitable interface to manage application GUIs.
Thanks

XContextMenuInterceptor probably is your friend here

see
https://wiki.openoffice.org/wiki/Documentation/DevGuide/WritingUNO/Intercepting_Context_Menus
https://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1ui_1_1XContextMenuInterceptor.html

There are working python examples of intercepting menus over on Live LibreOffice Python UNO Examples.

These examples use the OooDev Menus that may be helpful to point you in the direction you looking for.

Hi, thanks to all for feedbacks. I considered closed this thread.
Summarizing, concerning the cell context menu, with Basic, there are 3 approaches:

  1. Write a function and assign it to the RClick event of the sheet. The returned value is boolean and kills the context menu if ==true. The function argument is an object that represents the cell involved by the mouse click. Drawback: you do not have direct info about mouse coordinates.
  2. Create and register the listener XContextMenuInterceptor. Implement the methode “notifyContextMenuExecute” in which you can kill or modify the context menu. The argument of the function provides many info: Selection.Selection is the cell object (when a cell is involved), ExecutePosition has the mouse position, ActionTriggerContainer allows to add/remove/replace context menu items.
  3. Create and register the listener XEnhancedMouseClickHandler. It allows to manage all kinds of click without the issues seen with XMouseClickHandler. mousePressed and mouseReleased must be implemented as functions whose returned value kills all pendling event handlers when ==false. The argument, the same for both methodes, includes the involved cell and the mouse coordinates.

You can click not only on a cell, but also on a shape.