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 ...@@ -29,6 +29,7 @@ interface XPasteListener : com::sun::star::uno::XInterface
<p>The following keys may be used: <p>The following keys may be used:
<ul> <ul>
<li>TextRange</li> <li>TextRange</li>
<li>TextGraphicObject</li>
</ul></p> </ul></p>
*/ */
void notifyPasteEvent([in] sequence< ::com::sun::star::beans::PropertyValue > aEvent); void notifyPasteEvent([in] sequence< ::com::sun::star::beans::PropertyValue > aEvent);
......
...@@ -18,9 +18,11 @@ ...@@ -18,9 +18,11 @@
#include <com/sun/star/awt/XToolkit.hpp> #include <com/sun/star/awt/XToolkit.hpp>
#include <comphelper/propertyvalue.hxx> #include <comphelper/propertyvalue.hxx>
#include <toolkit/helper/vclunohelper.hxx> #include <toolkit/helper/vclunohelper.hxx>
#include <vcl/graphicfilter.hxx>
#include <wrtsh.hxx> #include <wrtsh.hxx>
#include <ndtxt.hxx> #include <ndtxt.hxx>
#include <swdtflvr.hxx> #include <swdtflvr.hxx>
#include <view.hxx>
using namespace ::com::sun::star; using namespace ::com::sun::star;
using namespace ::com::sun::star::uno; using namespace ::com::sun::star::uno;
...@@ -34,11 +36,13 @@ char const DATA_DIRECTORY[] = "/sw/qa/extras/unowriter/data/"; ...@@ -34,11 +36,13 @@ char const DATA_DIRECTORY[] = "/sw/qa/extras/unowriter/data/";
class PasteListener : public cppu::WeakImplHelper<text::XPasteListener> class PasteListener : public cppu::WeakImplHelper<text::XPasteListener>
{ {
OUString m_aString; OUString m_aString;
uno::Reference<text::XTextContent> m_xTextGraphicObject;
public: public:
void SAL_CALL notifyPasteEvent(const uno::Sequence<beans::PropertyValue>& rEvent) override; void SAL_CALL notifyPasteEvent(const uno::Sequence<beans::PropertyValue>& rEvent) override;
OUString& GetString(); OUString& GetString();
uno::Reference<text::XTextContent>& GetTextGraphicObject();
}; };
void PasteListener::notifyPasteEvent(const uno::Sequence<beans::PropertyValue>& rEvent) void PasteListener::notifyPasteEvent(const uno::Sequence<beans::PropertyValue>& rEvent)
...@@ -50,10 +54,24 @@ void PasteListener::notifyPasteEvent(const uno::Sequence<beans::PropertyValue>& ...@@ -50,10 +54,24 @@ void PasteListener::notifyPasteEvent(const uno::Sequence<beans::PropertyValue>&
auto xTextRange = it->second.get<uno::Reference<text::XTextRange>>(); auto xTextRange = it->second.get<uno::Reference<text::XTextRange>>();
if (xTextRange.is()) if (xTextRange.is())
m_aString = xTextRange->getString(); 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; } OUString& PasteListener::GetString() { return m_aString; }
uno::Reference<text::XTextContent>& PasteListener::GetTextGraphicObject()
{
return m_xTextGraphicObject;
}
} }
/// Test to assert UNO API call results of Writer. /// Test to assert UNO API call results of Writer.
...@@ -533,6 +551,19 @@ DECLARE_UNOAPI_TEST(testPasteListener) ...@@ -533,6 +551,19 @@ DECLARE_UNOAPI_TEST(testPasteListener)
// Make sure that paste overwrote "BC". // Make sure that paste overwrote "BC".
CPPUNIT_ASSERT_EQUAL(OUString("ADEDEF"), xBodyText->getString()); 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. // Deregister paste listener, make sure it's not invoked.
xBroadcaster->removePasteEventListener(xListener); xBroadcaster->removePasteEventListener(xListener);
pListener->GetString().clear(); pListener->GetString().clear();
......
...@@ -126,6 +126,7 @@ ...@@ -126,6 +126,7 @@
#include <itabenum.hxx> #include <itabenum.hxx>
#include <iodetect.hxx> #include <iodetect.hxx>
#include <unotextrange.hxx> #include <unotextrange.hxx>
#include <unoframe.hxx>
#include <vcl/GraphicNativeTransform.hxx> #include <vcl/GraphicNativeTransform.hxx>
#include <vcl/GraphicNativeMetadata.hxx> #include <vcl/GraphicNativeMetadata.hxx>
...@@ -1125,6 +1126,24 @@ SwPasteContext::~SwPasteContext() ...@@ -1125,6 +1126,24 @@ SwPasteContext::~SwPasteContext()
if (m_rWrtShell.GetPasteListeners().getLength() == 0) if (m_rWrtShell.GetPasteListeners().getLength() == 0)
return; return;
beans::PropertyValue aPropertyValue;
switch (m_rWrtShell.GetView().GetShellMode())
{
case ShellMode::Graphic:
{
SwFrameFormat* pFormat = m_rWrtShell.GetFlyFrameFormat();
if (!pFormat)
return;
aPropertyValue.Name = "TextGraphicObject";
aPropertyValue.Value
<<= SwXTextGraphicObject::CreateXTextGraphicObject(*pFormat->GetDoc(), pFormat);
break;
}
default:
{
if (!m_pPaM) if (!m_pPaM)
return; return;
...@@ -1146,14 +1165,21 @@ SwPasteContext::~SwPasteContext() ...@@ -1146,14 +1165,21 @@ SwPasteContext::~SwPasteContext()
// Starting point is no longer text. // Starting point is no longer text.
return; return;
m_pPaM->GetPoint()->nContent.Assign(static_cast<SwContentNode*>(&rNode), m_nStartContent); m_pPaM->GetPoint()->nContent.Assign(static_cast<SwContentNode*>(&rNode),
m_nStartContent);
// Invoke the listeners.
beans::PropertyValue aPropertyValue;
aPropertyValue.Name = "TextRange"; aPropertyValue.Name = "TextRange";
const uno::Reference<text::XTextRange> xTextRange = SwXTextRange::CreateXTextRange( const uno::Reference<text::XTextRange> xTextRange = SwXTextRange::CreateXTextRange(
*m_pPaM->GetDoc(), *m_pPaM->GetPoint(), m_pPaM->GetMark()); *m_pPaM->GetDoc(), *m_pPaM->GetPoint(), m_pPaM->GetMark());
aPropertyValue.Value <<= xTextRange; aPropertyValue.Value <<= xTextRange;
break;
}
}
if (aPropertyValue.Name.isEmpty())
return;
// Invoke the listeners.
uno::Sequence<beans::PropertyValue> aEvent{ aPropertyValue }; uno::Sequence<beans::PropertyValue> aEvent{ aPropertyValue };
comphelper::OInterfaceIteratorHelper2 it(m_rWrtShell.GetPasteListeners()); 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