Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sd / source / core / text / textapi.cxx
bloba4bdb23e46bc5bb0fab7c598a59dec3406e98a6a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/text/XTextField.hpp>
21 #include <com/sun/star/container/XNameContainer.hpp>
22 #include <com/sun/star/beans/PropertyAttribute.hpp>
24 #include <textapi.hxx>
25 #include <drawdoc.hxx>
26 #include <editeng/eeitem.hxx>
27 #include <editeng/editeng.hxx>
28 #include <editeng/outlobj.hxx>
29 #include <editeng/unoforou.hxx>
30 #include <editeng/unoprnms.hxx>
31 #include <editeng/unoipset.hxx>
32 #include <Outliner.hxx>
33 #include <svx/svdpool.hxx>
35 using namespace ::com::sun::star::uno;
36 using namespace ::com::sun::star::text;
37 using namespace ::com::sun::star::beans;
38 using namespace ::com::sun::star::container;
40 namespace sd {
42 class UndoTextAPIChanged : public SdrUndoAction
44 public:
45 UndoTextAPIChanged( SdrModel& rModel, TextApiObject* pTextObj );
47 virtual void Undo() override;
48 virtual void Redo() override;
50 protected:
51 std::unique_ptr<OutlinerParaObject> mpOldText;
52 std::unique_ptr<OutlinerParaObject> mpNewText;
53 rtl::Reference< TextApiObject > mxTextObj;
56 UndoTextAPIChanged::UndoTextAPIChanged(SdrModel& rModel, TextApiObject* pTextObj )
57 : SdrUndoAction( rModel )
58 , mpOldText( pTextObj->CreateText() )
59 , mxTextObj( pTextObj )
63 void UndoTextAPIChanged::Undo()
65 if( !mpNewText )
66 mpNewText.reset( mxTextObj->CreateText() );
68 mxTextObj->SetText( *mpOldText );
71 void UndoTextAPIChanged::Redo()
73 if( mpNewText )
75 mxTextObj->SetText( *mpNewText );
79 struct TextAPIEditSource_Impl
81 SdDrawDocument* mpDoc;
82 Outliner* mpOutliner;
83 SvxOutlinerForwarder* mpTextForwarder;
86 class TextAPIEditSource : public SvxEditSource
88 // refcounted
89 std::shared_ptr<TextAPIEditSource_Impl> m_xImpl;
91 virtual std::unique_ptr<SvxEditSource> Clone() const override;
92 virtual SvxTextForwarder* GetTextForwarder() override;
93 virtual void UpdateData() override;
94 explicit TextAPIEditSource( const TextAPIEditSource& rSource );
96 public:
97 explicit TextAPIEditSource(SdDrawDocument* pDoc);
99 void Dispose();
100 void SetText( OutlinerParaObject const & rText );
101 OutlinerParaObject* CreateText();
102 OUString GetText();
103 SdDrawDocument* GetDoc() { return m_xImpl->mpDoc; }
106 const SvxItemPropertySet* ImplGetSdTextPortionPropertyMap()
108 static const SfxItemPropertyMapEntry aSdTextPortionPropertyEntries[] =
110 SVX_UNOEDIT_CHAR_PROPERTIES,
111 SVX_UNOEDIT_FONT_PROPERTIES,
112 SVX_UNOEDIT_OUTLINER_PROPERTIES,
113 SVX_UNOEDIT_PARA_PROPERTIES,
114 {OUString("TextField"), EE_FEATURE_FIELD, cppu::UnoType<XTextField>::get(), PropertyAttribute::READONLY, 0 },
115 {OUString("TextPortionType"), WID_PORTIONTYPE, ::cppu::UnoType<OUString>::get(), PropertyAttribute::READONLY, 0 },
116 {OUString("TextUserDefinedAttributes"), EE_CHAR_XMLATTRIBS, cppu::UnoType<XNameContainer>::get(), 0, 0},
117 {OUString("ParaUserDefinedAttributes"), EE_PARA_XMLATTRIBS, cppu::UnoType<XNameContainer>::get(), 0, 0},
118 { OUString(), 0, css::uno::Type(), 0, 0 }
120 static SvxItemPropertySet aSdTextPortionPropertyMap( aSdTextPortionPropertyEntries, SdrObject::GetGlobalDrawObjectItemPool() );
122 return &aSdTextPortionPropertyMap;
125 TextApiObject::TextApiObject( TextAPIEditSource* pEditSource )
126 : SvxUnoText( pEditSource, ImplGetSdTextPortionPropertyMap(), Reference < XText >() )
127 , mpSource(pEditSource)
131 TextApiObject::~TextApiObject() throw()
133 dispose();
136 rtl::Reference< TextApiObject > TextApiObject::create( SdDrawDocument* pDoc )
138 rtl::Reference< TextApiObject > xRet( new TextApiObject( new TextAPIEditSource( pDoc ) ) );
139 return xRet;
142 void TextApiObject::dispose()
144 if( mpSource )
146 mpSource->Dispose();
147 delete mpSource;
148 mpSource = nullptr;
153 OutlinerParaObject* TextApiObject::CreateText()
155 return mpSource->CreateText();
158 void TextApiObject::SetText( OutlinerParaObject const & rText )
160 SdrModel* pModel = mpSource->GetDoc();
161 if( pModel && pModel->IsUndoEnabled() )
162 pModel->AddUndo( new UndoTextAPIChanged( *pModel, this ) );
164 mpSource->SetText( rText );
165 maSelection.nStartPara = EE_PARA_MAX_COUNT;
168 OUString TextApiObject::GetText()
170 return mpSource->GetText();
173 TextApiObject* TextApiObject::getImplementation( const css::uno::Reference< css::text::XText >& xText )
175 TextApiObject* pImpl = dynamic_cast< TextApiObject* >( xText.get() );
177 if( !pImpl )
178 pImpl = dynamic_cast< TextApiObject* >( SvxUnoTextBase::getImplementation( xText ) );
180 return pImpl;
183 TextAPIEditSource::TextAPIEditSource(const TextAPIEditSource& rSource)
184 : SvxEditSource(*this)
185 , m_xImpl(rSource.m_xImpl) // shallow copy; uses internal refcounting
189 std::unique_ptr<SvxEditSource> TextAPIEditSource::Clone() const
191 return std::unique_ptr<SvxEditSource>(new TextAPIEditSource( *this ));
194 void TextAPIEditSource::UpdateData()
196 // data is kept in outliner all the time
199 TextAPIEditSource::TextAPIEditSource(SdDrawDocument* pDoc)
200 : m_xImpl(new TextAPIEditSource_Impl)
202 m_xImpl->mpDoc = pDoc;
203 m_xImpl->mpOutliner = nullptr;
204 m_xImpl->mpTextForwarder = nullptr;
207 void TextAPIEditSource::Dispose()
209 m_xImpl->mpDoc=nullptr;
210 delete m_xImpl->mpTextForwarder;
211 m_xImpl->mpTextForwarder = nullptr;
213 delete m_xImpl->mpOutliner;
214 m_xImpl->mpOutliner = nullptr;
217 SvxTextForwarder* TextAPIEditSource::GetTextForwarder()
219 if(!m_xImpl->mpDoc)
220 return nullptr; // mpDoc == 0 can be used to flag this as disposed
222 if (!m_xImpl->mpOutliner)
224 //init draw model first
225 m_xImpl->mpOutliner = new SdOutliner(m_xImpl->mpDoc, OutlinerMode::TextObject);
226 SdDrawDocument::SetCalcFieldValueHdl(m_xImpl->mpOutliner);
229 if (!m_xImpl->mpTextForwarder)
230 m_xImpl->mpTextForwarder = new SvxOutlinerForwarder(*m_xImpl->mpOutliner, false);
232 return m_xImpl->mpTextForwarder;
235 void TextAPIEditSource::SetText( OutlinerParaObject const & rText )
237 if (m_xImpl->mpDoc)
239 if (!m_xImpl->mpOutliner)
241 //init draw model first
242 m_xImpl->mpOutliner = new SdOutliner(m_xImpl->mpDoc, OutlinerMode::TextObject);
243 SdDrawDocument::SetCalcFieldValueHdl(m_xImpl->mpOutliner);
246 m_xImpl->mpOutliner->SetText( rText );
250 OutlinerParaObject* TextAPIEditSource::CreateText()
252 if (m_xImpl->mpDoc && m_xImpl->mpOutliner)
253 return m_xImpl->mpOutliner->CreateParaObject();
254 else
255 return nullptr;
258 OUString TextAPIEditSource::GetText()
260 if (m_xImpl->mpDoc && m_xImpl->mpOutliner)
261 return m_xImpl->mpOutliner->GetEditEngine().GetText();
262 else
263 return OUString();
266 } // namespace sd
268 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */