1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/beans/PropertyAttribute.hpp>
21 #include <com/sun/star/lang/Locale.hpp>
22 #include <com/sun/star/text/XTextField.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>
34 #include <svx/svdundo.hxx>
36 namespace com::sun::star::container
{ class XNameContainer
; }
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::text
;
40 using namespace ::com::sun::star::beans
;
41 using namespace ::com::sun::star::container
;
47 class UndoTextAPIChanged
: public SdrUndoAction
50 UndoTextAPIChanged( SdrModel
& rModel
, TextApiObject
* pTextObj
);
52 virtual void Undo() override
;
53 virtual void Redo() override
;
56 std::optional
<OutlinerParaObject
> mpOldText
;
57 std::optional
<OutlinerParaObject
> mpNewText
;
58 rtl::Reference
< TextApiObject
> mxTextObj
;
63 UndoTextAPIChanged::UndoTextAPIChanged(SdrModel
& rModel
, TextApiObject
* pTextObj
)
64 : SdrUndoAction( rModel
)
65 , mpOldText( pTextObj
->CreateText() )
66 , mxTextObj( pTextObj
)
70 void UndoTextAPIChanged::Undo()
73 mpNewText
= mxTextObj
->CreateText();
75 mxTextObj
->SetText( *mpOldText
);
78 void UndoTextAPIChanged::Redo()
82 mxTextObj
->SetText( *mpNewText
);
88 struct TextAPIEditSource_Impl
90 SdDrawDocument
* mpDoc
;
92 SvxOutlinerForwarder
* mpTextForwarder
;
97 class TextAPIEditSource
: public SvxEditSource
100 std::shared_ptr
<TextAPIEditSource_Impl
> m_xImpl
;
102 virtual std::unique_ptr
<SvxEditSource
> Clone() const override
;
103 virtual SvxTextForwarder
* GetTextForwarder() override
;
104 virtual void UpdateData() override
;
105 explicit TextAPIEditSource( const TextAPIEditSource
& rSource
);
108 explicit TextAPIEditSource(SdDrawDocument
* pDoc
);
111 void SetText( OutlinerParaObject
const & rText
);
112 std::optional
<OutlinerParaObject
> CreateText();
113 OUString
GetText() const;
114 SdDrawDocument
* GetDoc() { return m_xImpl
->mpDoc
; }
117 static const SvxItemPropertySet
* ImplGetSdTextPortionPropertyMap()
119 static const SfxItemPropertyMapEntry aSdTextPortionPropertyEntries
[] =
121 SVX_UNOEDIT_CHAR_PROPERTIES
,
122 SVX_UNOEDIT_FONT_PROPERTIES
,
123 SVX_UNOEDIT_OUTLINER_PROPERTIES
,
124 SVX_UNOEDIT_PARA_PROPERTIES
,
125 {u
"TextField", EE_FEATURE_FIELD
, cppu::UnoType
<XTextField
>::get(), PropertyAttribute::READONLY
, 0 },
126 {u
"TextPortionType", WID_PORTIONTYPE
, ::cppu::UnoType
<OUString
>::get(), PropertyAttribute::READONLY
, 0 },
127 {u
"TextUserDefinedAttributes", EE_CHAR_XMLATTRIBS
, cppu::UnoType
<XNameContainer
>::get(), 0, 0},
128 {u
"ParaUserDefinedAttributes", EE_PARA_XMLATTRIBS
, cppu::UnoType
<XNameContainer
>::get(), 0, 0},
130 static SvxItemPropertySet
aSdTextPortionPropertyMap( aSdTextPortionPropertyEntries
, SdrObject::GetGlobalDrawObjectItemPool() );
132 return &aSdTextPortionPropertyMap
;
135 TextApiObject::TextApiObject( std::unique_ptr
<TextAPIEditSource
> pEditSource
)
136 : SvxUnoText( pEditSource
.get(), ImplGetSdTextPortionPropertyMap(), Reference
< XText
>() )
137 , mpSource(std::move(pEditSource
))
141 TextApiObject::~TextApiObject() noexcept
146 rtl::Reference
< TextApiObject
> TextApiObject::create( SdDrawDocument
* pDoc
)
148 rtl::Reference
< TextApiObject
> xRet( new TextApiObject( std::make_unique
<TextAPIEditSource
>( pDoc
) ) );
152 void TextApiObject::dispose()
162 std::optional
<OutlinerParaObject
> TextApiObject::CreateText()
164 return mpSource
->CreateText();
167 void TextApiObject::SetText( OutlinerParaObject
const & rText
)
169 SdrModel
* pModel
= mpSource
->GetDoc();
170 if( pModel
&& pModel
->IsUndoEnabled() )
171 pModel
->AddUndo( std::make_unique
<UndoTextAPIChanged
>( *pModel
, this ) );
173 mpSource
->SetText( rText
);
174 maSelection
.nStartPara
= EE_PARA_MAX_COUNT
;
177 OUString
TextApiObject::GetText() const
179 return mpSource
->GetText();
182 TextApiObject
* TextApiObject::getImplementation( const css::uno::Reference
< css::text::XText
>& xText
)
184 TextApiObject
* pImpl
= dynamic_cast< TextApiObject
* >( xText
.get() );
187 pImpl
= dynamic_cast< TextApiObject
* >( comphelper::getFromUnoTunnel
<SvxUnoTextBase
>( xText
) );
192 TextAPIEditSource::TextAPIEditSource(const TextAPIEditSource
& rSource
)
193 : SvxEditSource(*this)
194 , m_xImpl(rSource
.m_xImpl
) // shallow copy; uses internal refcounting
198 std::unique_ptr
<SvxEditSource
> TextAPIEditSource::Clone() const
200 return std::unique_ptr
<SvxEditSource
>(new TextAPIEditSource( *this ));
203 void TextAPIEditSource::UpdateData()
205 // data is kept in outliner all the time
208 TextAPIEditSource::TextAPIEditSource(SdDrawDocument
* pDoc
)
209 : m_xImpl(std::make_shared
<TextAPIEditSource_Impl
>())
211 m_xImpl
->mpDoc
= pDoc
;
212 m_xImpl
->mpOutliner
= nullptr;
213 m_xImpl
->mpTextForwarder
= nullptr;
216 void TextAPIEditSource::Dispose()
218 m_xImpl
->mpDoc
=nullptr;
219 delete m_xImpl
->mpTextForwarder
;
220 m_xImpl
->mpTextForwarder
= nullptr;
222 delete m_xImpl
->mpOutliner
;
223 m_xImpl
->mpOutliner
= nullptr;
226 SvxTextForwarder
* TextAPIEditSource::GetTextForwarder()
229 return nullptr; // mpDoc == 0 can be used to flag this as disposed
231 if (!m_xImpl
->mpOutliner
)
233 //init draw model first
234 m_xImpl
->mpOutliner
= new SdOutliner(m_xImpl
->mpDoc
, OutlinerMode::TextObject
);
235 SdDrawDocument::SetCalcFieldValueHdl(m_xImpl
->mpOutliner
);
238 if (!m_xImpl
->mpTextForwarder
)
239 m_xImpl
->mpTextForwarder
= new SvxOutlinerForwarder(*m_xImpl
->mpOutliner
, false);
241 return m_xImpl
->mpTextForwarder
;
244 void TextAPIEditSource::SetText( OutlinerParaObject
const & rText
)
248 if (!m_xImpl
->mpOutliner
)
250 //init draw model first
251 m_xImpl
->mpOutliner
= new SdOutliner(m_xImpl
->mpDoc
, OutlinerMode::TextObject
);
252 SdDrawDocument::SetCalcFieldValueHdl(m_xImpl
->mpOutliner
);
255 m_xImpl
->mpOutliner
->SetText( rText
);
259 std::optional
<OutlinerParaObject
> TextAPIEditSource::CreateText()
261 if (m_xImpl
->mpDoc
&& m_xImpl
->mpOutliner
)
262 return m_xImpl
->mpOutliner
->CreateParaObject();
267 OUString
TextAPIEditSource::GetText() const
269 if (m_xImpl
->mpDoc
&& m_xImpl
->mpOutliner
)
270 return m_xImpl
->mpOutliner
->GetEditEngine().GetText();
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */