From 2791964914ba19b636ce49a60508bcab7af675cd Mon Sep 17 00:00:00 2001 From: Tomaž Vajngerl Date: Fri, 19 Apr 2024 22:36:41 +0900 Subject: annot: move TextAPI to svx and adapt the code MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TextApiObject is derived from SvxUnoText and holds and handles the text in the annotation. This functionality is changed to get rid of sd module specifics and moved to svx module. This now allows to move the most improtant parts of the Annotation object to svx. Change-Id: I34e238616e7fac973e75e79bb60a8d093c493258 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166497 Reviewed-by: Tomaž Vajngerl Tested-by: Tomaž Vajngerl --- include/svx/annotation/Annotation.hxx | 18 +- include/svx/annotation/TextAPI.hxx | 56 ++++++ sd/Library_sd.mk | 1 - sd/inc/Annotation.hxx | 14 -- sd/inc/textapi.hxx | 55 ------ sd/source/core/annotations/Annotation.cxx | 36 ---- sd/source/core/text/textapi.cxx | 263 ------------------------- sd/source/ui/annotations/annotationmanager.cxx | 4 +- sd/source/ui/annotations/annotationwindow.cxx | 10 +- sd/source/ui/annotations/annotationwindow.hxx | 8 +- solenv/clang-format/excludelist | 2 - svx/Library_svxcore.mk | 1 + svx/source/annotation/Annotation.cxx | 32 +++ svx/source/annotation/TextAPI.cxx | 262 ++++++++++++++++++++++++ 14 files changed, 376 insertions(+), 386 deletions(-) create mode 100644 include/svx/annotation/TextAPI.hxx delete mode 100644 sd/inc/textapi.hxx delete mode 100644 sd/source/core/text/textapi.cxx create mode 100644 svx/source/annotation/TextAPI.cxx diff --git a/include/svx/annotation/Annotation.hxx b/include/svx/annotation/Annotation.hxx index 5ec5e2ec3c34..be378d6a61de 100644 --- a/include/svx/annotation/Annotation.hxx +++ b/include/svx/annotation/Annotation.hxx @@ -20,6 +20,7 @@ #include #include #include +#include class SdrUndoAction; class SfxViewShell; @@ -70,6 +71,7 @@ protected: OUString m_Author; OUString m_Initials; css::util::DateTime m_DateTime; + rtl::Reference m_TextRange; bool m_bIsFreeText = false; @@ -84,11 +86,11 @@ public: virtual css::uno::Any SAL_CALL queryInterface(css::uno::Type const& type) override; virtual void SAL_CALL acquire() noexcept override { - ::comphelper::WeakComponentImplHelper::acquire(); + comphelper::WeakComponentImplHelper::acquire(); } virtual void SAL_CALL release() noexcept override { - ::comphelper::WeakComponentImplHelper::release(); + comphelper::WeakComponentImplHelper::release(); } css::geometry::RealPoint2D GetPosition() const { return m_Position; } @@ -106,8 +108,16 @@ public: css::util::DateTime GetDateTime() const { return m_DateTime; } void SetDateTime(const css::util::DateTime& rValue) { m_DateTime = rValue; } - virtual OUString GetText() = 0; - virtual void SetText(OUString const& rText) = 0; + virtual css::uno::Reference SAL_CALL getTextRange() override; + + // override WeakComponentImplHelperBase::disposing() + // This function is called upon disposing the component, + // if your component needs special work when it becomes + // disposed, do it here. + virtual void disposing(std::unique_lock& rGuard) override; + + OUString GetText(); + void SetText(OUString const& rText); SdrModel* GetModel() const; SdrPage const* getPage() const { return mpPage; } diff --git a/include/svx/annotation/TextAPI.hxx b/include/svx/annotation/TextAPI.hxx new file mode 100644 index 000000000000..c227b4a803c5 --- /dev/null +++ b/include/svx/annotation/TextAPI.hxx @@ -0,0 +1,56 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#pragma once + +#include +#include +#include +#include + +class SdrModel; + +namespace sdr::annotation +{ +class TextAPIEditSource; + +class SVXCORE_DLLPUBLIC TextApiObject final : public SvxUnoText +{ +public: + static rtl::Reference create(SdrModel* pModel); + + virtual ~TextApiObject() noexcept override; + + /// @throws css::uno::RuntimeException + void dispose(); + + std::optional CreateText(); + void SetText(OutlinerParaObject const& rText); + OUString GetText() const; + + static TextApiObject* getImplementation(const css::uno::Reference&); + +private: + std::unique_ptr mpSource; + TextApiObject(std::unique_ptr pEditSource); +}; + +} // namespace sdr::annotation + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sd/Library_sd.mk b/sd/Library_sd.mk index 643cce60ff82..de39df79b25b 100644 --- a/sd/Library_sd.mk +++ b/sd/Library_sd.mk @@ -172,7 +172,6 @@ $(eval $(call gb_Library_add_exception_objects,sd,\ sd/source/core/stlfamily \ sd/source/core/stlpool \ sd/source/core/stlsheet \ - sd/source/core/text/textapi \ sd/source/core/typemap \ sd/source/core/undo/undofactory \ sd/source/core/undo/undomanager \ diff --git a/sd/inc/Annotation.hxx b/sd/inc/Annotation.hxx index 00a0b2a22af8..aba2d27d77b7 100644 --- a/sd/inc/Annotation.hxx +++ b/sd/inc/Annotation.hxx @@ -29,7 +29,6 @@ #include #include "sdpage.hxx" -#include "textapi.hxx" #include "sddllapi.h" #include @@ -88,7 +87,6 @@ public: virtual void SAL_CALL setInitials(const OUString & the_value) override; SD_DLLPUBLIC virtual css::util::DateTime SAL_CALL getDateTime() override; virtual void SAL_CALL setDateTime(const css::util::DateTime & the_value) override; - SD_DLLPUBLIC virtual css::uno::Reference SAL_CALL getTextRange() override; void createChangeUndo(); @@ -107,21 +105,9 @@ public: return bool(m_pCustomAnnotationMarker); } - OUString GetText() override; - void SetText(OUString const& rText) override; - private: - // destructor is private and will be called indirectly by the release call virtual ~Annotation() {} - - // override WeakComponentImplHelperBase::disposing() - // This function is called upon disposing the component, - // if your component needs special work when it becomes - // disposed, do it here. - virtual void disposing(std::unique_lock& rGuard) override; - void createChangeUndoImpl(std::unique_lock& g); - rtl::Reference m_TextRange; std::unique_ptr m_pCustomAnnotationMarker; }; diff --git a/sd/inc/textapi.hxx b/sd/inc/textapi.hxx deleted file mode 100644 index 5491382e0564..000000000000 --- a/sd/inc/textapi.hxx +++ /dev/null @@ -1,55 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#pragma once - -#include -#include -#include - -class SdrModel; - -namespace sd { - -class TextAPIEditSource; - -class TextApiObject final : public SvxUnoText -{ -public: - static rtl::Reference create(SdrModel* pModel); - - virtual ~TextApiObject() noexcept override; - - /// @throws css::uno::RuntimeException - void dispose(); - - std::optional CreateText(); - void SetText( OutlinerParaObject const & rText ); - OUString GetText() const; - - static TextApiObject* getImplementation( const css::uno::Reference< css::text::XText >& ); - -private: - std::unique_ptr mpSource; - TextApiObject( std::unique_ptr pEditSource ); -}; - -} // namespace sd - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sd/source/core/annotations/Annotation.cxx b/sd/source/core/annotations/Annotation.cxx index 1d1786ff460c..2843333f29b4 100644 --- a/sd/source/core/annotations/Annotation.cxx +++ b/sd/source/core/annotations/Annotation.cxx @@ -76,20 +76,6 @@ Annotation::Annotation(const uno::Reference& context, Sd Annotation::~Annotation() {} -// override WeakComponentImplHelperBase::disposing() -// This function is called upon disposing the component, -// if your component needs special work when it becomes -// disposed, do it here. -void Annotation::disposing(std::unique_lock& /*rGuard*/) -{ - mpPage = nullptr; - if( m_TextRange.is() ) - { - m_TextRange->dispose(); - m_TextRange.clear(); - } -} - // com.sun.star.beans.XPropertySet: uno::Reference SAL_CALL Annotation::getPropertySetInfo() { @@ -220,18 +206,6 @@ void SAL_CALL Annotation::setDateTime(const util::DateTime & the_value) } } -OUString Annotation::GetText() -{ - uno::Reference xText(getTextRange()); - return xText->getString(); -} - -void Annotation::SetText(OUString const& rText) -{ - uno::Reference xText(getTextRange()); - return xText->setString(rText); -} - void Annotation::createChangeUndo() { std::unique_lock g(m_aMutex); @@ -259,16 +233,6 @@ void Annotation::createChangeUndoImpl(std::unique_lock& g) } } -uno::Reference SAL_CALL Annotation::getTextRange() -{ - std::unique_lock g(m_aMutex); - if( !m_TextRange.is() && (mpPage != nullptr) ) - { - m_TextRange = TextApiObject::create( static_cast< SdDrawDocument* >( &mpPage->getSdrModelFromSdrPage() ) ); - } - return m_TextRange; -} - std::unique_ptr CreateUndoInsertOrRemoveAnnotation(rtl::Reference& xAnnotation, bool bInsert) { if (xAnnotation) diff --git a/sd/source/core/text/textapi.cxx b/sd/source/core/text/textapi.cxx deleted file mode 100644 index c5f9ab631bff..000000000000 --- a/sd/source/core/text/textapi.cxx +++ /dev/null @@ -1,263 +0,0 @@ -/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ -/* - * This file is part of the LibreOffice project. - * - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. - * - * This file incorporates work covered by the following license notice: - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed - * with this work for additional information regarding copyright - * ownership. The ASF licenses this file to you under the Apache - * License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of - * the License at http://www.apache.org/licenses/LICENSE-2.0 . - */ - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace com::sun::star::container { class XNameContainer; } - -using namespace ::com::sun::star::uno; -using namespace ::com::sun::star::text; -using namespace ::com::sun::star::beans; -using namespace ::com::sun::star::container; - -namespace sd { - -namespace { - -class UndoTextAPIChanged : public SdrUndoAction -{ -public: - UndoTextAPIChanged( SdrModel& rModel, TextApiObject* pTextObj ); - - virtual void Undo() override; - virtual void Redo() override; - -protected: - std::optional mpOldText; - std::optional mpNewText; - rtl::Reference< TextApiObject > mxTextObj; -}; - -} - -UndoTextAPIChanged::UndoTextAPIChanged(SdrModel& rModel, TextApiObject* pTextObj ) -: SdrUndoAction( rModel ) -, mpOldText( pTextObj->CreateText() ) -, mxTextObj( pTextObj ) -{ -} - -void UndoTextAPIChanged::Undo() -{ - if( !mpNewText ) - mpNewText = mxTextObj->CreateText(); - - mxTextObj->SetText( *mpOldText ); -} - -void UndoTextAPIChanged::Redo() -{ - if( mpNewText ) - { - mxTextObj->SetText( *mpNewText ); - } -} - -namespace -{ - -struct OutlinerHolder -{ - SdrModel* mpModel; - std::unique_ptr mpOutliner; - std::unique_ptr mpTextForwarder; -}; - -} - -class TextAPIEditSource : public SvxEditSource -{ - // refcounted - std::shared_ptr mpHolder; - - virtual std::unique_ptr Clone() const override; - virtual SvxTextForwarder* GetTextForwarder() override; - virtual void UpdateData() override; - explicit TextAPIEditSource(const TextAPIEditSource& rSource); - -public: - explicit TextAPIEditSource(SdrModel* pModel); - - void SetText(OutlinerParaObject const & rText); - std::optional CreateText(); - OUString GetText() const; - SdrModel* getModel() { return mpHolder->mpModel; } -}; - -static const SvxItemPropertySet* ImplGetSdTextPortionPropertyMap() -{ - static const SfxItemPropertyMapEntry aSdTextPortionPropertyEntries[] = - { - SVX_UNOEDIT_CHAR_PROPERTIES, - SVX_UNOEDIT_FONT_PROPERTIES, - SVX_UNOEDIT_OUTLINER_PROPERTIES, - SVX_UNOEDIT_PARA_PROPERTIES, - {u"TextField"_ustr, EE_FEATURE_FIELD, cppu::UnoType::get(), PropertyAttribute::READONLY, 0 }, - {u"TextPortionType"_ustr, WID_PORTIONTYPE, ::cppu::UnoType::get(), PropertyAttribute::READONLY, 0 }, - {u"TextUserDefinedAttributes"_ustr, EE_CHAR_XMLATTRIBS, cppu::UnoType::get(), 0, 0}, - {u"ParaUserDefinedAttributes"_ustr, EE_PARA_XMLATTRIBS, cppu::UnoType::get(), 0, 0}, - }; - static SvxItemPropertySet aSdTextPortionPropertyMap( aSdTextPortionPropertyEntries, SdrObject::GetGlobalDrawObjectItemPool() ); - - return &aSdTextPortionPropertyMap; -} - -TextApiObject::TextApiObject( std::unique_ptr pEditSource ) -: SvxUnoText( pEditSource.get(), ImplGetSdTextPortionPropertyMap(), Reference < XText >() ) -, mpSource(std::move(pEditSource)) -{ -} - -TextApiObject::~TextApiObject() noexcept -{ - dispose(); -} - -rtl::Reference TextApiObject::create(SdrModel* pModel) -{ - rtl::Reference xRet(new TextApiObject(std::make_unique(pModel))); - return xRet; -} - -void TextApiObject::dispose() -{ - if (mpSource) - mpSource.reset(); -} - -std::optional TextApiObject::CreateText() -{ - return mpSource->CreateText(); -} - -void TextApiObject::SetText( OutlinerParaObject const & rText ) -{ - SdrModel* pModel = mpSource->getModel(); - if( pModel && pModel->IsUndoEnabled() ) - pModel->AddUndo( std::make_unique( *pModel, this ) ); - - mpSource->SetText( rText ); - maSelection.nStartPara = EE_PARA_MAX_COUNT; -} - -OUString TextApiObject::GetText() const -{ - return mpSource->GetText(); -} - -TextApiObject* TextApiObject::getImplementation( const css::uno::Reference< css::text::XText >& xText ) -{ - TextApiObject* pImpl = dynamic_cast< TextApiObject* >( xText.get() ); - - if( !pImpl ) - pImpl = dynamic_cast< TextApiObject* >( comphelper::getFromUnoTunnel( xText ) ); - - return pImpl; -} - -TextAPIEditSource::TextAPIEditSource(const TextAPIEditSource& rSource) - : SvxEditSource(*this) - , mpHolder(rSource.mpHolder) // shallow copy; uses internal refcounting -{ -} - -std::unique_ptr TextAPIEditSource::Clone() const -{ - return std::unique_ptr(new TextAPIEditSource(*this)); -} - -void TextAPIEditSource::UpdateData() -{ - // data is kept in outliner all the time -} - -TextAPIEditSource::TextAPIEditSource(SdrModel* pModel) - : mpHolder(std::make_shared()) -{ - mpHolder->mpModel = pModel; -} - -SvxTextForwarder* TextAPIEditSource::GetTextForwarder() -{ - if (!mpHolder->mpModel) - return nullptr; // mpModel == 0 can be used to flag this as disposed - - if (!mpHolder->mpOutliner) - { - //init draw model first - SfxItemPool* pPool = &mpHolder->mpModel->GetItemPool(); - mpHolder->mpOutliner.reset(new SdrOutliner(pPool, OutlinerMode::TextObject)); - SdDrawDocument::SetCalcFieldValueHdl(mpHolder->mpOutliner.get()); - } - - if (!mpHolder->mpTextForwarder) - mpHolder->mpTextForwarder.reset(new SvxOutlinerForwarder(*mpHolder->mpOutliner, false)); - - return mpHolder->mpTextForwarder.get(); -} - -void TextAPIEditSource::SetText( OutlinerParaObject const & rText ) -{ - if (mpHolder->mpModel) - { - if (!mpHolder->mpOutliner) - { - //init draw model first - SfxItemPool* pPool = &mpHolder->mpModel->GetItemPool(); - mpHolder->mpOutliner.reset(new SdrOutliner(pPool, OutlinerMode::TextObject)); - SdDrawDocument::SetCalcFieldValueHdl(mpHolder->mpOutliner.get()); - } - - mpHolder->mpOutliner->SetText(rText); - } -} - -std::optional TextAPIEditSource::CreateText() -{ - if (mpHolder->mpModel && mpHolder->mpOutliner) - return mpHolder->mpOutliner->CreateParaObject(); - else - return std::nullopt; -} - -OUString TextAPIEditSource::GetText() const -{ - if (mpHolder->mpModel && mpHolder->mpOutliner) - return mpHolder->mpOutliner->GetEditEngine().GetText(); - else - return OUString(); -} - -} // namespace sd - -/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sd/source/ui/annotations/annotationmanager.cxx b/sd/source/ui/annotations/annotationmanager.cxx index 0eb3adb2e977..bc4db37332d5 100644 --- a/sd/source/ui/annotations/annotationmanager.cxx +++ b/sd/source/ui/annotations/annotationmanager.cxx @@ -69,7 +69,7 @@ #include #include #include -#include +#include #include #include @@ -558,7 +558,7 @@ void AnnotationManagerImpl::ExecuteReplyToAnnotation( SfxRequest const & rReq ) sReplyText = static_cast( pPoolItem )->GetValue(); } - TextApiObject* pTextApi = getTextApiObject( xAnnotation ); + auto* pTextApi = getTextApiObject( xAnnotation ); if( !pTextApi ) return; diff --git a/sd/source/ui/annotations/annotationwindow.cxx b/sd/source/ui/annotations/annotationwindow.cxx index fffaf70ee863..59ccc11af604 100644 --- a/sd/source/ui/annotations/annotationwindow.cxx +++ b/sd/source/ui/annotations/annotationwindow.cxx @@ -54,7 +54,7 @@ #include #include #include -#include +#include #include #include @@ -484,12 +484,12 @@ IMPL_LINK(AnnotationWindow, ScrollHdl, weld::ScrolledWindow&, rScrolledWindow, v GetOutlinerView()->Scroll( 0, nDiff ); } -TextApiObject* getTextApiObject( const Reference< XAnnotation >& xAnnotation ) +sdr::annotation::TextApiObject* getTextApiObject( const Reference< XAnnotation >& xAnnotation ) { if( xAnnotation.is() ) { Reference< XText > xText( xAnnotation->getTextRange() ); - return TextApiObject::getImplementation( xText ); + return sdr::annotation::TextApiObject::getImplementation(xText); } return nullptr; } @@ -507,7 +507,7 @@ void AnnotationWindow::setAnnotation( const Reference< XAnnotation >& xAnnotatio mbProtected = aUserOptions.GetFullName() != xAnnotation->getAuthor(); mpOutliner->Clear(); - TextApiObject* pTextApi = getTextApiObject( mxAnnotation ); + auto* pTextApi = getTextApiObject( mxAnnotation ); if( pTextApi ) { @@ -572,7 +572,7 @@ void AnnotationWindow::SaveToDocument() // write changed text back to annotation if (mpOutliner->IsModified()) { - TextApiObject* pTextApi = getTextApiObject( xAnnotation ); + auto* pTextApi = getTextApiObject( xAnnotation ); if( pTextApi ) { diff --git a/sd/source/ui/annotations/annotationwindow.hxx b/sd/source/ui/annotations/annotationwindow.hxx index a150ebf1c8cd..fd5919b40d06 100644 --- a/sd/source/ui/annotations/annotationwindow.hxx +++ b/sd/source/ui/annotations/annotationwindow.hxx @@ -29,13 +29,13 @@ class OutlinerView; class Outliner; class SvxLanguageItem; class SdDrawDocument; +namespace sdr::annotation { class TextApiObject; } -namespace sd { +namespace sd +{ class AnnotationManagerImpl; class DrawDocShell; -class TextApiObject; - class AnnotationWindow; class AnnotationTextWindow : public WeldEditView @@ -135,7 +135,7 @@ public: void SetColor(); }; -TextApiObject* getTextApiObject( const css::uno::Reference< css::office::XAnnotation >& xAnnotation ); +sdr::annotation::TextApiObject* getTextApiObject( const css::uno::Reference< css::office::XAnnotation >& xAnnotation ); } // namespace sd diff --git a/solenv/clang-format/excludelist b/solenv/clang-format/excludelist index 8991519cf8db..8a02d8bab2d0 100644 --- a/solenv/clang-format/excludelist +++ b/solenv/clang-format/excludelist @@ -9359,7 +9359,6 @@ sd/inc/stlfamily.hxx sd/inc/stlpool.hxx sd/inc/stlsheet.hxx sd/inc/strings.hxx -sd/inc/textapi.hxx sd/inc/undo/undofactory.hxx sd/inc/undo/undomanager.hxx sd/qa/unit/SVGExportTests.cxx @@ -9452,7 +9451,6 @@ sd/source/core/shapelist.cxx sd/source/core/stlfamily.cxx sd/source/core/stlpool.cxx sd/source/core/stlsheet.cxx -sd/source/core/text/textapi.cxx sd/source/core/undo/undofactory.cxx sd/source/core/undo/undomanager.cxx sd/source/core/undo/undoobjects.cxx diff --git a/svx/Library_svxcore.mk b/svx/Library_svxcore.mk index a3e0b3fb64a7..5218e9582b43 100644 --- a/svx/Library_svxcore.mk +++ b/svx/Library_svxcore.mk @@ -108,6 +108,7 @@ endif $(eval $(call gb_Library_add_exception_objects,svxcore,\ svx/source/annotation/Annotation \ svx/source/annotation/AnnotationEnumeration \ + svx/source/annotation/TextAPI \ svx/source/core/extedit \ svx/source/core/graphichelper \ svx/source/core/extendedprimitive2dxmldump \ diff --git a/svx/source/annotation/Annotation.cxx b/svx/source/annotation/Annotation.cxx index 332573563df4..47b486228624 100644 --- a/svx/source/annotation/Annotation.cxx +++ b/svx/source/annotation/Annotation.cxx @@ -163,6 +163,38 @@ std::unique_ptr Annotation::createUndoAnnotation() return std::make_unique(*this); } +OUString Annotation::GetText() +{ + uno::Reference xText(getTextRange()); + return xText->getString(); +} + +void Annotation::SetText(OUString const& rText) +{ + uno::Reference xText(getTextRange()); + return xText->setString(rText); +} + +uno::Reference SAL_CALL Annotation::getTextRange() +{ + std::unique_lock g(m_aMutex); + if (!m_TextRange.is() && mpPage != nullptr) + { + m_TextRange = sdr::annotation::TextApiObject::create(&mpPage->getSdrModelFromSdrPage()); + } + return m_TextRange; +} + +void Annotation::disposing(std::unique_lock& /*rGuard*/) +{ + mpPage = nullptr; + if (m_TextRange.is()) + { + m_TextRange->dispose(); + m_TextRange.clear(); + } +} + } // namespace sdr::annotation /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/svx/source/annotation/TextAPI.cxx b/svx/source/annotation/TextAPI.cxx new file mode 100644 index 000000000000..cd13acd76d3b --- /dev/null +++ b/svx/source/annotation/TextAPI.cxx @@ -0,0 +1,262 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace com::sun::star::container +{ +class XNameContainer; +} + +using namespace ::com::sun::star::uno; +using namespace ::com::sun::star::text; +using namespace ::com::sun::star::beans; +using namespace ::com::sun::star::container; + +namespace sdr::annotation +{ +namespace +{ +class UndoTextAPIChanged : public SdrUndoAction +{ +public: + UndoTextAPIChanged(SdrModel& rModel, TextApiObject* pTextObj); + + virtual void Undo() override; + virtual void Redo() override; + +protected: + std::optional mpOldText; + std::optional mpNewText; + rtl::Reference mxTextObj; +}; +} + +UndoTextAPIChanged::UndoTextAPIChanged(SdrModel& rModel, TextApiObject* pTextObj) + : SdrUndoAction(rModel) + , mpOldText(pTextObj->CreateText()) + , mxTextObj(pTextObj) +{ +} + +void UndoTextAPIChanged::Undo() +{ + if (!mpNewText) + mpNewText = mxTextObj->CreateText(); + + mxTextObj->SetText(*mpOldText); +} + +void UndoTextAPIChanged::Redo() +{ + if (mpNewText) + { + mxTextObj->SetText(*mpNewText); + } +} + +namespace +{ +struct OutlinerHolder +{ + SdrModel* mpModel = nullptr; + std::unique_ptr mpOutliner; + std::unique_ptr mpTextForwarder; +}; +} + +class TextAPIEditSource : public SvxEditSource +{ + // refcounted + std::shared_ptr mpHolder; + + virtual std::unique_ptr Clone() const override; + virtual SvxTextForwarder* GetTextForwarder() override; + virtual void UpdateData() override; + explicit TextAPIEditSource(const TextAPIEditSource& rSource); + +public: + explicit TextAPIEditSource(SdrModel* pModel); + + void SetText(OutlinerParaObject const& rText); + std::optional CreateText(); + OUString GetText() const; + SdrModel* getModel() { return mpHolder->mpModel; } +}; + +static const SvxItemPropertySet* ImplGetSdTextPortionPropertyMap() +{ + static const SfxItemPropertyMapEntry aSdTextPortionPropertyEntries[] = { + SVX_UNOEDIT_CHAR_PROPERTIES, + SVX_UNOEDIT_FONT_PROPERTIES, + SVX_UNOEDIT_OUTLINER_PROPERTIES, + SVX_UNOEDIT_PARA_PROPERTIES, + { u"TextField"_ustr, EE_FEATURE_FIELD, cppu::UnoType::get(), + PropertyAttribute::READONLY, 0 }, + { u"TextPortionType"_ustr, WID_PORTIONTYPE, ::cppu::UnoType::get(), + PropertyAttribute::READONLY, 0 }, + { u"TextUserDefinedAttributes"_ustr, EE_CHAR_XMLATTRIBS, + cppu::UnoType::get(), 0, 0 }, + { u"ParaUserDefinedAttributes"_ustr, EE_PARA_XMLATTRIBS, + cppu::UnoType::get(), 0, 0 }, + }; + static SvxItemPropertySet aSdTextPortionPropertyMap(aSdTextPortionPropertyEntries, + SdrObject::GetGlobalDrawObjectItemPool()); + + return &aSdTextPortionPropertyMap; +} + +TextApiObject::TextApiObject(std::unique_ptr pEditSource) + : SvxUnoText(pEditSource.get(), ImplGetSdTextPortionPropertyMap(), Reference()) + , mpSource(std::move(pEditSource)) +{ +} + +TextApiObject::~TextApiObject() noexcept { dispose(); } + +rtl::Reference TextApiObject::create(SdrModel* pModel) +{ + rtl::Reference xRet( + new TextApiObject(std::make_unique(pModel))); + return xRet; +} + +void TextApiObject::dispose() +{ + if (mpSource) + mpSource.reset(); +} + +std::optional TextApiObject::CreateText() { return mpSource->CreateText(); } + +void TextApiObject::SetText(OutlinerParaObject const& rText) +{ + SdrModel* pModel = mpSource->getModel(); + if (pModel && pModel->IsUndoEnabled()) + pModel->AddUndo(std::make_unique(*pModel, this)); + + mpSource->SetText(rText); + maSelection.nStartPara = EE_PARA_MAX_COUNT; +} + +OUString TextApiObject::GetText() const { return mpSource->GetText(); } + +TextApiObject* TextApiObject::getImplementation(const css::uno::Reference& xText) +{ + TextApiObject* pImpl = dynamic_cast(xText.get()); + + if (!pImpl) + pImpl = dynamic_cast(comphelper::getFromUnoTunnel(xText)); + + return pImpl; +} + +TextAPIEditSource::TextAPIEditSource(const TextAPIEditSource& rSource) + : SvxEditSource(*this) + , mpHolder(rSource.mpHolder) // shallow copy; uses internal refcounting +{ +} + +std::unique_ptr TextAPIEditSource::Clone() const +{ + return std::unique_ptr(new TextAPIEditSource(*this)); +} + +void TextAPIEditSource::UpdateData() +{ + // data is kept in outliner all the time +} + +TextAPIEditSource::TextAPIEditSource(SdrModel* pModel) + : mpHolder(std::make_shared()) +{ + mpHolder->mpModel = pModel; +} + +SvxTextForwarder* TextAPIEditSource::GetTextForwarder() +{ + if (!mpHolder->mpModel) + return nullptr; // mpModel == 0 can be used to flag this as disposed + + if (!mpHolder->mpOutliner) + { + //init draw model first + SfxItemPool* pPool = &mpHolder->mpModel->GetItemPool(); + mpHolder->mpOutliner.reset(new SdrOutliner(pPool, OutlinerMode::TextObject)); + // TODO - we need a way to register this to the current module + // SdDrawDocument::SetCalcFieldValueHdl(mpHolder->mpOutliner.get()); + } + + if (!mpHolder->mpTextForwarder) + mpHolder->mpTextForwarder.reset(new SvxOutlinerForwarder(*mpHolder->mpOutliner, false)); + + return mpHolder->mpTextForwarder.get(); +} + +void TextAPIEditSource::SetText(OutlinerParaObject const& rText) +{ + if (mpHolder->mpModel) + { + if (!mpHolder->mpOutliner) + { + //init draw model first + SfxItemPool* pPool = &mpHolder->mpModel->GetItemPool(); + mpHolder->mpOutliner.reset(new SdrOutliner(pPool, OutlinerMode::TextObject)); + // TODO - we need a way to register this to the current module + //SdDrawDocument::SetCalcFieldValueHdl(mpHolder->mpOutliner.get()); + } + + mpHolder->mpOutliner->SetText(rText); + } +} + +std::optional TextAPIEditSource::CreateText() +{ + if (mpHolder->mpModel && mpHolder->mpOutliner) + return mpHolder->mpOutliner->CreateParaObject(); + else + return std::nullopt; +} + +OUString TextAPIEditSource::GetText() const +{ + if (mpHolder->mpModel && mpHolder->mpOutliner) + return mpHolder->mpOutliner->GetEditEngine().GetText(); + else + return OUString(); +} + +} // namespace sdr::annotation + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit