summaryrefslogtreecommitdiff
path: root/sw
diff options
context:
space:
mode:
authorMiklos Vajna <vmiklos@collabora.com>2021-03-09 11:49:06 +0100
committerMiklos Vajna <vmiklos@collabora.com>2021-03-10 09:24:19 +0100
commit0a995ee4d736f6538d24a560559f94e6b0778e5d (patch)
tree9c584f78a790eb332106f1d365a53da4fb6ae23c /sw
parent31ba05c3650ebf7009005dcdb0f7adea45e778b7 (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. (cherry picked from commit 8e7dc248f2787df0e658c4ece33220944fca7f16) Conflicts: sw/qa/uibase/uno/uno.cxx Change-Id: I6a8e69e3c39b9d99209342469653c0e0bd99bf53
Diffstat (limited to 'sw')
-rw-r--r--sw/qa/extras/unowriter/unowriter.cxx41
-rw-r--r--sw/source/uibase/inc/unotxvw.hxx6
-rw-r--r--sw/source/uibase/uno/unotxvw.cxx27
3 files changed, 74 insertions, 0 deletions
diff --git a/sw/qa/extras/unowriter/unowriter.cxx b/sw/qa/extras/unowriter/unowriter.cxx
index 691d385d6de7..e6660c253817 100644
--- a/sw/qa/extras/unowriter/unowriter.cxx
+++ b/sw/qa/extras/unowriter/unowriter.cxx
@@ -17,6 +17,7 @@
#include <com/sun/star/text/XTextPortionAppend.hpp>
#include <com/sun/star/text/XTextContentAppend.hpp>
#include <com/sun/star/text/XTextRangeCompare.hpp>
+#include <com/sun/star/text/XTextViewTextRangeSupplier.hpp>
#include <com/sun/star/rdf/URI.hpp>
#include <com/sun/star/rdf/URIs.hpp>
#include <com/sun/star/awt/XDevice.hpp>
@@ -36,6 +37,8 @@
#include <AnnotationWin.hxx>
#include <flyfrm.hxx>
#include <fmtanchr.hxx>
+#include <edtwin.hxx>
+#include <unotextrange.hxx>
using namespace ::com::sun::star;
@@ -947,6 +950,44 @@ CPPUNIT_TEST_FIXTURE(SwUnoWriter, testTdf129841)
CPPUNIT_ASSERT_EQUAL(aRefColor, aColor);
}
+CPPUNIT_TEST_FIXTURE(SwUnoWriter, testCreateTextRangeByPixelPosition)
+{
+ // Given a document with 2 characters, and the pixel position of the point between them:
+ loadURL("private:factory/swriter", nullptr);
+ SwXTextDocument* pTextDoc = dynamic_cast<SwXTextDocument*>(mxComponent.get());
+ CPPUNIT_ASSERT(pTextDoc);
+ SwDoc* pDoc = pTextDoc->GetDocShell()->GetDoc();
+ SwDocShell* pDocShell = pDoc->GetDocShell();
+ SwWrtShell* pWrtShell = pDocShell->GetWrtShell();
+ pWrtShell->Insert2("AZ");
+ pWrtShell->Left(CRSR_SKIP_CHARS, /*bSelect=*/false, 1, /*bBasicCall=*/false);
+ Point aLogic = pWrtShell->GetCharRect().Center();
+ SwView* pView = pDocShell->GetView();
+ SwEditWin& rEditWin = pView->GetEditWin();
+ Point aPixel = rEditWin.LogicToPixel(aLogic);
+
+ // When converting that pixel position to a document model position (text range):
+ uno::Reference<frame::XModel2> xModel(mxComponent, uno::UNO_QUERY);
+ uno::Reference<container::XEnumeration> xControllers = xModel->getControllers();
+ uno::Reference<text::XTextViewTextRangeSupplier> xController(xControllers->nextElement(),
+ uno::UNO_QUERY);
+ awt::Point aPoint(aPixel.getX(), aPixel.getY());
+ uno::Reference<text::XTextRange> xTextRange
+ = xController->createTextRangeByPixelPosition(aPoint);
+
+ // Then make sure that text range points after the first character:
+ auto pTextRange = dynamic_cast<SwXTextRange*>(xTextRange.get());
+ SwPaM aPaM(pDoc->GetNodes());
+ pTextRange->GetPositions(aPaM);
+ sal_Int32 nActual = aPaM.GetPoint()->nContent.GetIndex();
+ // Without the needed PixelToLogic() call in place, this test would have failed with:
+ // - Expected: 1
+ // - Actual : 0
+ // i.e. the returned text range pointed before the first character, not between the first and
+ // the second character.
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), nActual);
+}
+
CPPUNIT_PLUGIN_IMPLEMENT();
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sw/source/uibase/inc/unotxvw.hxx b/sw/source/uibase/inc/unotxvw.hxx
index db2f6eae398f..4f17e0017a1f 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>
@@ -51,6 +52,7 @@ class SwXTextView :
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,
@@ -100,6 +102,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 b83a52500e4d..438e648f480c 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>
@@ -132,6 +134,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(),
@@ -183,6 +186,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;
@@ -507,6 +515,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;