summaryrefslogtreecommitdiff
path: root/sw/source/uibase
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@collabora.com>2021-03-09 11:49:06 +0100
committerMiklos Vajna <vmiklos@collabora.com>2021-03-09 20:12:24 +0100
commit8e7dc248f2787df0e658c4ece33220944fca7f16 (patch)
treeaf8a6063517b33392ad761bf6abf436996d49943 /sw/source/uibase
parentacfd19df520693bb0e052d08df152befd7832a41 (diff)
sw: add UNO API to find the closest doc model position based on pixel position
The use-case is drag&drop: if an UNO API client registers its drag&drop listener, then it gets a DropTargetDropEvent and has to decide if it wants to handle that event or allow Writer to handle it. In case it decides to handle the event, it would be good to able to e.g. insert a string at the point where the Writer UI indicates it to the user. But DropTargetDropEvent only exposes a pixel position and Writer requires you to have an XTextRange when inserting content. Fix the problem by introducing a new createTextRangeByPixelPosition() which first does a pixel -> logic coordinate conversion (this is window-specific, in case you have multiple windows), then picks the doc model position which is the closest to a logic coordinate. Change-Id: I6a8e69e3c39b9d99209342469653c0e0bd99bf53 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112201 Reviewed-by: Miklos Vajna <vmiklos@collabora.com> Tested-by: Jenkins
Diffstat (limited to 'sw/source/uibase')
-rw-r--r--sw/source/uibase/inc/unotxvw.hxx6
-rw-r--r--sw/source/uibase/uno/unotxvw.cxx27
2 files changed, 33 insertions, 0 deletions
diff --git a/sw/source/uibase/inc/unotxvw.hxx b/sw/source/uibase/inc/unotxvw.hxx
index 473633defd9e..1f52f7ea6230 100644
--- a/sw/source/uibase/inc/unotxvw.hxx
+++ b/sw/source/uibase/inc/unotxvw.hxx
@@ -23,6 +23,7 @@
#include <comphelper/interfacecontainer2.hxx>
#include <com/sun/star/text/XTextViewCursor.hpp>
#include <com/sun/star/text/XTextViewCursorSupplier.hpp>
+#include <com/sun/star/text/XTextViewTextRangeSupplier.hpp>
#include <com/sun/star/text/XRubySelection.hpp>
#include <com/sun/star/view/XFormLayerAccess.hpp>
#include <com/sun/star/view/XScreenCursor.hpp>
@@ -52,6 +53,7 @@ class SwXTextView final :
public css::lang::XServiceInfo,
public css::view::XFormLayerAccess,
public css::text::XTextViewCursorSupplier,
+ public css::text::XTextViewTextRangeSupplier,
public css::text::XRubySelection,
public css::view::XViewSettingsSupplier,
public css::beans::XPropertySet,
@@ -101,6 +103,10 @@ public:
//XTextViewCursorSupplier
virtual css::uno::Reference< css::text::XTextViewCursor > SAL_CALL getViewCursor() override;
+ // XTextViewTextRangeSupplier
+ virtual css::uno::Reference<css::text::XTextRange>
+ SAL_CALL createTextRangeByPixelPosition(const css::awt::Point& rPixelPosition) override;
+
//XViewSettings
virtual css::uno::Reference< css::beans::XPropertySet > SAL_CALL getViewSettings() override;
diff --git a/sw/source/uibase/uno/unotxvw.cxx b/sw/source/uibase/uno/unotxvw.cxx
index e6621293391d..1b805102c6d7 100644
--- a/sw/source/uibase/uno/unotxvw.cxx
+++ b/sw/source/uibase/uno/unotxvw.cxx
@@ -64,6 +64,8 @@
#include <unotextrange.hxx>
#include <sfx2/docfile.hxx>
#include <swdtflvr.hxx>
+#include <rootfrm.hxx>
+#include <edtwin.hxx>
#include <vcl/svapp.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/profilezone.hxx>
@@ -133,6 +135,7 @@ Sequence< uno::Type > SAL_CALL SwXTextView::getTypes( )
cppu::UnoType<XServiceInfo>::get(),
cppu::UnoType<XFormLayerAccess>::get(),
cppu::UnoType<XTextViewCursorSupplier>::get(),
+ cppu::UnoType<XTextViewTextRangeSupplier>::get(),
cppu::UnoType<XViewSettingsSupplier>::get(),
cppu::UnoType<XRubySelection>::get(),
cppu::UnoType<XPropertySet>::get(),
@@ -185,6 +188,11 @@ uno::Any SAL_CALL SwXTextView::queryInterface( const uno::Type& aType )
uno::Reference<text::XTextViewCursorSupplier> xRet = this;
aRet <<= xRet;
}
+ else if (aType == cppu::UnoType<text::XTextViewTextRangeSupplier>::get())
+ {
+ uno::Reference<text::XTextViewTextRangeSupplier> xRet = this;
+ aRet <<= xRet;
+ }
else if(aType == cppu::UnoType<view::XViewSettingsSupplier>::get())
{
uno::Reference<view::XViewSettingsSupplier> xRet = this;
@@ -514,6 +522,25 @@ uno::Reference< text::XTextViewCursor > SwXTextView::getViewCursor()
return mxTextViewCursor;
}
+uno::Reference<text::XTextRange>
+SwXTextView::createTextRangeByPixelPosition(const awt::Point& rPixelPosition)
+{
+ SolarMutexGuard aGuard;
+
+ Point aPixelPoint(rPixelPosition.X, rPixelPosition.Y);
+ if (!m_pView)
+ throw RuntimeException();
+
+ Point aLogicPoint = m_pView->GetEditWin().PixelToLogic(aPixelPoint);
+ SwWrtShell& rSh = m_pView->GetWrtShell();
+ SwPosition aPosition(*rSh.GetCurrentShellCursor().GetPoint());
+ rSh.GetLayout()->GetModelPositionForViewPoint(&aPosition, aLogicPoint);
+ uno::Reference<text::XTextRange> xRet
+ = SwXTextRange::CreateXTextRange(*rSh.GetDoc(), aPosition, /*pMark=*/nullptr);
+
+ return xRet;
+}
+
uno::Reference< beans::XPropertySet > SwXTextView::getViewSettings()
{
SolarMutexGuard aGuard;