Blob Blame History Raw
From 4bd7e435c3476b7d267755b9e53ed7d35ce78220 Mon Sep 17 00:00:00 2001
From: Miklos Vajna <vmiklos@collabora.co.uk>
Date: Fri, 13 Nov 2015 17:30:22 +0100
Subject: [PATCH 326/398] sw lok comments: fix position of blinking cursor
 after mouse click

LOK always works in absolute twips (origo being the top left corner of
SwEditWin), so not only the callbacks have to translate relative twips
to absolute ones, but the opposite have to be done for mouse event
coordinates.

With this, clicking at a random position inside a comment places the
blinking cursor at a reasonable position, not always at 0,0.

(cherry picked from commit 57972554b58a680f47a05f4d6711c99106f80523)

Change-Id: Ic8d20f177acd9e1908acf17698c53a1470bd4aec
---
 sw/source/uibase/docvw/SidebarWin.cxx | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/sw/source/uibase/docvw/SidebarWin.cxx b/sw/source/uibase/docvw/SidebarWin.cxx
index 47f5022e8044..71b763b84224 100644
--- a/sw/source/uibase/docvw/SidebarWin.cxx
+++ b/sw/source/uibase/docvw/SidebarWin.cxx
@@ -358,14 +358,25 @@ void SwSidebarWin::Draw(OutputDevice* pDev, const Point& rPt, const Size& rSz, s
     }
 }
 
-/// We want to work in absolute twips: so set delta between rChild and rParent as origin on rChild, then disable map mode on rChild.
-static void lcl_setAbsoluteTwips(vcl::Window& rParent, vcl::Window& rChild)
+/// Translate absolute <-> relative twips: LOK wants absolute coordinates as output and gives absolute coordinates as input.
+static void lcl_translateTwips(vcl::Window& rParent, vcl::Window& rChild, MouseEvent* pMouseEvent)
 {
+    // Set map mode, so that callback payloads will contain absolute coordinates instead of relative ones.
     Point aOffset(rChild.GetOutOffXPixel() - rParent.GetOutOffXPixel(), rChild.GetOutOffYPixel() - rParent.GetOutOffYPixel());
+    aOffset = rChild.PixelToLogic(aOffset);
     MapMode aMapMode(rChild.GetMapMode());
-    aMapMode.SetOrigin(rChild.PixelToLogic(aOffset));
+    aMapMode.SetOrigin(aOffset);
     rChild.SetMapMode(aMapMode);
     rChild.EnableMapMode(false);
+
+    if (pMouseEvent)
+    {
+        // Set event coordinates, so they contain relative coordinates instead of absolute ones.
+        Point aPos = pMouseEvent->GetPosPixel();
+        aPos.Move(-aOffset.getX(), -aOffset.getY());
+        MouseEvent aMouseEvent(aPos, pMouseEvent->GetClicks(), pMouseEvent->GetMode(), pMouseEvent->GetButtons(), pMouseEvent->GetModifier());
+        *pMouseEvent = aMouseEvent;
+    }
 }
 
 void SwSidebarWin::KeyInput(const KeyEvent& rKeyEvent)
@@ -373,7 +384,7 @@ void SwSidebarWin::KeyInput(const KeyEvent& rKeyEvent)
     if (mpSidebarTextControl)
     {
         mpSidebarTextControl->Push(PushFlags::MAPMODE);
-        lcl_setAbsoluteTwips(*EditWin(), *mpSidebarTextControl);
+        lcl_translateTwips(*EditWin(), *mpSidebarTextControl, nullptr);
 
         mpSidebarTextControl->KeyInput(rKeyEvent);
 
@@ -386,9 +397,10 @@ void SwSidebarWin::MouseButtonDown(const MouseEvent& rMouseEvent)
     if (mpSidebarTextControl)
     {
         mpSidebarTextControl->Push(PushFlags::MAPMODE);
-        lcl_setAbsoluteTwips(*EditWin(), *mpSidebarTextControl);
+        MouseEvent aMouseEvent(rMouseEvent);
+        lcl_translateTwips(*EditWin(), *mpSidebarTextControl, &aMouseEvent);
 
-        mpSidebarTextControl->MouseButtonDown(rMouseEvent);
+        mpSidebarTextControl->MouseButtonDown(aMouseEvent);
 
         mpSidebarTextControl->Pop();
     }
-- 
2.12.0