Kaydet (Commit) 2a054445 authored tarafından Miklos Vajna's avatar Miklos Vajna

sw paste listener: expose pasted images as well

Do it similar to SwXTextView::getSelection(), so that
SwView::GetShellMode() determines when an image is selected (and
otherwise assume text selection).

Change-Id: I717e1358428daba842309260b54f82b62a0aaec1
Reviewed-on: https://gerrit.libreoffice.org/66879Reviewed-by: 's avatarMiklos Vajna <vmiklos@collabora.com>
Tested-by: Jenkins
üst 78317bc6
......@@ -29,6 +29,7 @@ interface XPasteListener : com::sun::star::uno::XInterface
<p>The following keys may be used:
<ul>
<li>TextRange</li>
<li>TextGraphicObject</li>
</ul></p>
*/
void notifyPasteEvent([in] sequence< ::com::sun::star::beans::PropertyValue > aEvent);
......
......@@ -18,9 +18,11 @@
#include <com/sun/star/awt/XToolkit.hpp>
#include <comphelper/propertyvalue.hxx>
#include <toolkit/helper/vclunohelper.hxx>
#include <vcl/graphicfilter.hxx>
#include <wrtsh.hxx>
#include <ndtxt.hxx>
#include <swdtflvr.hxx>
#include <view.hxx>
using namespace ::com::sun::star;
using namespace ::com::sun::star::uno;
......@@ -34,11 +36,13 @@ char const DATA_DIRECTORY[] = "/sw/qa/extras/unowriter/data/";
class PasteListener : public cppu::WeakImplHelper<text::XPasteListener>
{
OUString m_aString;
uno::Reference<text::XTextContent> m_xTextGraphicObject;
public:
void SAL_CALL notifyPasteEvent(const uno::Sequence<beans::PropertyValue>& rEvent) override;
OUString& GetString();
uno::Reference<text::XTextContent>& GetTextGraphicObject();
};
void PasteListener::notifyPasteEvent(const uno::Sequence<beans::PropertyValue>& rEvent)
......@@ -50,10 +54,24 @@ void PasteListener::notifyPasteEvent(const uno::Sequence<beans::PropertyValue>&
auto xTextRange = it->second.get<uno::Reference<text::XTextRange>>();
if (xTextRange.is())
m_aString = xTextRange->getString();
return;
}
it = aMap.find("TextGraphicObject");
if (it != aMap.end())
{
auto xTextGraphicObject = it->second.get<uno::Reference<text::XTextContent>>();
if (xTextGraphicObject.is())
m_xTextGraphicObject = xTextGraphicObject;
}
}
OUString& PasteListener::GetString() { return m_aString; }
uno::Reference<text::XTextContent>& PasteListener::GetTextGraphicObject()
{
return m_xTextGraphicObject;
}
}
/// Test to assert UNO API call results of Writer.
......@@ -533,6 +551,19 @@ DECLARE_UNOAPI_TEST(testPasteListener)
// Make sure that paste overwrote "BC".
CPPUNIT_ASSERT_EQUAL(OUString("ADEDEF"), xBodyText->getString());
// Test image paste.
SwView& rView = pWrtShell->GetView();
OUString aGraphicURL = m_directories.getURLFromSrc(DATA_DIRECTORY) + "test.jpg";
rView.InsertGraphic(aGraphicURL, OUString(), /*bAsLink=*/false,
&GraphicFilter::GetGraphicFilter());
pTransfer->Cut();
pListener->GetString().clear();
SwTransferable::Paste(*pWrtShell, aHelper);
// Without the working image listener in place, this test would have
// failed, the listener was not invoked in case of a graphic paste.
CPPUNIT_ASSERT(pListener->GetTextGraphicObject().is());
CPPUNIT_ASSERT(pListener->GetString().isEmpty());
// Deregister paste listener, make sure it's not invoked.
xBroadcaster->removePasteEventListener(xListener);
pListener->GetString().clear();
......
......@@ -126,6 +126,7 @@
#include <itabenum.hxx>
#include <iodetect.hxx>
#include <unotextrange.hxx>
#include <unoframe.hxx>
#include <vcl/GraphicNativeTransform.hxx>
#include <vcl/GraphicNativeMetadata.hxx>
......@@ -1125,35 +1126,60 @@ SwPasteContext::~SwPasteContext()
if (m_rWrtShell.GetPasteListeners().getLength() == 0)
return;
if (!m_pPaM)
return;
beans::PropertyValue aPropertyValue;
SwPaM* pCursor = m_rWrtShell.GetCursor();
if (!pCursor)
return;
switch (m_rWrtShell.GetView().GetShellMode())
{
case ShellMode::Graphic:
{
SwFrameFormat* pFormat = m_rWrtShell.GetFlyFrameFormat();
if (!pFormat)
return;
if (!pCursor->GetPoint()->nNode.GetNode().IsTextNode())
// Non-text was pasted.
return;
aPropertyValue.Name = "TextGraphicObject";
aPropertyValue.Value
<<= SwXTextGraphicObject::CreateXTextGraphicObject(*pFormat->GetDoc(), pFormat);
break;
}
// Update mark after paste.
*m_pPaM->GetMark() = *pCursor->GetPoint();
default:
{
if (!m_pPaM)
return;
// Restore point.
++m_pPaM->GetPoint()->nNode;
SwNode& rNode = m_pPaM->GetNode();
if (!rNode.IsTextNode())
// Starting point is no longer text.
return;
SwPaM* pCursor = m_rWrtShell.GetCursor();
if (!pCursor)
return;
m_pPaM->GetPoint()->nContent.Assign(static_cast<SwContentNode*>(&rNode), m_nStartContent);
if (!pCursor->GetPoint()->nNode.GetNode().IsTextNode())
// Non-text was pasted.
return;
// Update mark after paste.
*m_pPaM->GetMark() = *pCursor->GetPoint();
// Restore point.
++m_pPaM->GetPoint()->nNode;
SwNode& rNode = m_pPaM->GetNode();
if (!rNode.IsTextNode())
// Starting point is no longer text.
return;
m_pPaM->GetPoint()->nContent.Assign(static_cast<SwContentNode*>(&rNode),
m_nStartContent);
aPropertyValue.Name = "TextRange";
const uno::Reference<text::XTextRange> xTextRange = SwXTextRange::CreateXTextRange(
*m_pPaM->GetDoc(), *m_pPaM->GetPoint(), m_pPaM->GetMark());
aPropertyValue.Value <<= xTextRange;
break;
}
}
if (aPropertyValue.Name.isEmpty())
return;
// Invoke the listeners.
beans::PropertyValue aPropertyValue;
aPropertyValue.Name = "TextRange";
const uno::Reference<text::XTextRange> xTextRange = SwXTextRange::CreateXTextRange(
*m_pPaM->GetDoc(), *m_pPaM->GetPoint(), m_pPaM->GetMark());
aPropertyValue.Value <<= xTextRange;
uno::Sequence<beans::PropertyValue> aEvent{ aPropertyValue };
comphelper::OInterfaceIteratorHelper2 it(m_rWrtShell.GetPasteListeners());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment