Resolves: tdf#145959 menu pos should be relative to SmCmdBoxWindow

Change-Id: Ie8da9bbb4385b3315fd62e6f2dc8ab394ea3709f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/126237
Tested-by: Jenkins
Reviewed-by: Adolfo Jayme Barrientos <fitojb@ubuntu.com>
diff --git a/starmath/inc/view.hxx b/starmath/inc/view.hxx
index 3f77dd7..749db73 100644
--- a/starmath/inc/view.hxx
+++ b/starmath/inc/view.hxx
@@ -212,6 +212,10 @@ public:
    virtual void StateChanged( StateChangedType nStateChange ) override;
    virtual void Command(const CommandEvent& rCEvt) override;

    Point WidgetToWindowPos(const weld::Widget& rWidget, const Point& rPos);

    void ShowContextMenu(const Point& rPos);

    void AdjustPosition();

    SmEditWindow& GetEditWindow()
diff --git a/starmath/source/edit.cxx b/starmath/source/edit.cxx
index 2588e9f6..e8e79a2 100644
--- a/starmath/source/edit.cxx
+++ b/starmath/source/edit.cxx
@@ -266,10 +266,9 @@ bool SmEditTextWindow::Command(const CommandEvent& rCEvt)

    if (rCEvt.GetCommand() == CommandEventId::ContextMenu)
    {
        // purely for "ExecutePopup" taking a vcl::Window and
        // we assume SmEditTextWindow 0,0 is at SmEditWindow 0,0
        ReleaseMouse();
        mrEditWindow.GetCmdBox().Command(rCEvt);
        SmCmdBoxWindow& rCmdBox = mrEditWindow.GetCmdBox();
        rCmdBox.ShowContextMenu(rCmdBox.WidgetToWindowPos(*GetDrawingArea(), rCEvt.GetMousePosPixel()));
        GrabFocus();
        return true;
    }
diff --git a/starmath/source/view.cxx b/starmath/source/view.cxx
index e0b79fc..d369ad9 100644
--- a/starmath/source/view.cxx
+++ b/starmath/source/view.cxx
@@ -827,15 +827,29 @@ SmCmdBoxWindow::SmCmdBoxWindow(SfxBindings *pBindings_, SfxChildWindow *pChildWi
    aInitialFocusTimer.SetTimeout(100);
}

Point SmCmdBoxWindow::WidgetToWindowPos(const weld::Widget& rWidget, const Point& rPos)
{
    Point aRet(rPos);
    int x(0), y(0), width(0), height(0);
    rWidget.get_extents_relative_to(*m_xContainer, x, y, width, height);
    aRet.Move(x, y);
    aRet.Move(m_xBox->GetPosPixel().X(), m_xBox->GetPosPixel().Y());
    return aRet;
}

void SmCmdBoxWindow::ShowContextMenu(const Point& rPos)
{
    ToTop();
    SmViewShell *pViewSh = GetView();
    if (pViewSh)
        pViewSh->GetViewFrame()->GetDispatcher()->ExecutePopup("edit", this, &rPos);
}

void SmCmdBoxWindow::Command(const CommandEvent& rCEvt)
{
    if (rCEvt.GetCommand() == CommandEventId::ContextMenu)
    {
        ToTop();
        Point aPoint = rCEvt.GetMousePosPixel();
        SmViewShell *pViewSh = GetView();
        if (pViewSh)
            pViewSh->GetViewFrame()->GetDispatcher()->ExecutePopup("edit", this, &aPoint);
        ShowContextMenu(rCEvt.GetMousePosPixel());
        return;
    }