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::unique_ptr
<OutlinerParaObject
> mpOldText
;
57 std::unique_ptr
<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::unique_ptr
<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},
129 { u
"", 0, css::uno::Type(), 0, 0 }
131 static SvxItemPropertySet
aSdTextPortionPropertyMap( aSdTextPortionPropertyEntries
, SdrObject::GetGlobalDrawObjectItemPool() );
133 return &aSdTextPortionPropertyMap
;
136 TextApiObject::TextApiObject( std::unique_ptr
<TextAPIEditSource
> pEditSource
)
137 : SvxUnoText( pEditSource
.get(), ImplGetSdTextPortionPropertyMap(), Reference
< XText
>() )
138 , mpSource(std::move(pEditSource
))
142 TextApiObject::~TextApiObject() throw()
147 rtl::Reference
< TextApiObject
> TextApiObject::create( SdDrawDocument
* pDoc
)
149 rtl::Reference
< TextApiObject
> xRet( new TextApiObject( std::make_unique
<TextAPIEditSource
>( pDoc
) ) );
153 void TextApiObject::dispose()
163 std::unique_ptr
<OutlinerParaObject
> TextApiObject::CreateText()
165 return mpSource
->CreateText();
168 void TextApiObject::SetText( OutlinerParaObject
const & rText
)
170 SdrModel
* pModel
= mpSource
->GetDoc();
171 if( pModel
&& pModel
->IsUndoEnabled() )
172 pModel
->AddUndo( std::make_unique
<UndoTextAPIChanged
>( *pModel
, this ) );
174 mpSource
->SetText( rText
);
175 maSelection
.nStartPara
= EE_PARA_MAX_COUNT
;
178 OUString
TextApiObject::GetText() const
180 return mpSource
->GetText();
183 TextApiObject
* TextApiObject::getImplementation( const css::uno::Reference
< css::text::XText
>& xText
)
185 TextApiObject
* pImpl
= dynamic_cast< TextApiObject
* >( xText
.get() );
188 pImpl
= dynamic_cast< TextApiObject
* >( comphelper::getUnoTunnelImplementation
<SvxUnoTextBase
>( xText
) );
193 TextAPIEditSource::TextAPIEditSource(const TextAPIEditSource
& rSource
)
194 : SvxEditSource(*this)
195 , m_xImpl(rSource
.m_xImpl
) // shallow copy; uses internal refcounting
199 std::unique_ptr
<SvxEditSource
> TextAPIEditSource::Clone() const
201 return std::unique_ptr
<SvxEditSource
>(new TextAPIEditSource( *this ));
204 void TextAPIEditSource::UpdateData()
206 // data is kept in outliner all the time
209 TextAPIEditSource::TextAPIEditSource(SdDrawDocument
* pDoc
)
210 : m_xImpl(std::make_shared
<TextAPIEditSource_Impl
>())
212 m_xImpl
->mpDoc
= pDoc
;
213 m_xImpl
->mpOutliner
= nullptr;
214 m_xImpl
->mpTextForwarder
= nullptr;
217 void TextAPIEditSource::Dispose()
219 m_xImpl
->mpDoc
=nullptr;
220 delete m_xImpl
->mpTextForwarder
;
221 m_xImpl
->mpTextForwarder
= nullptr;
223 delete m_xImpl
->mpOutliner
;
224 m_xImpl
->mpOutliner
= nullptr;
227 SvxTextForwarder
* TextAPIEditSource::GetTextForwarder()
230 return nullptr; // mpDoc == 0 can be used to flag this as disposed
232 if (!m_xImpl
->mpOutliner
)
234 //init draw model first
235 m_xImpl
->mpOutliner
= new SdOutliner(m_xImpl
->mpDoc
, OutlinerMode::TextObject
);
236 SdDrawDocument::SetCalcFieldValueHdl(m_xImpl
->mpOutliner
);
239 if (!m_xImpl
->mpTextForwarder
)
240 m_xImpl
->mpTextForwarder
= new SvxOutlinerForwarder(*m_xImpl
->mpOutliner
, false);
242 return m_xImpl
->mpTextForwarder
;
245 void TextAPIEditSource::SetText( OutlinerParaObject
const & rText
)
249 if (!m_xImpl
->mpOutliner
)
251 //init draw model first
252 m_xImpl
->mpOutliner
= new SdOutliner(m_xImpl
->mpDoc
, OutlinerMode::TextObject
);
253 SdDrawDocument::SetCalcFieldValueHdl(m_xImpl
->mpOutliner
);
256 m_xImpl
->mpOutliner
->SetText( rText
);
260 std::unique_ptr
<OutlinerParaObject
> TextAPIEditSource::CreateText()
262 if (m_xImpl
->mpDoc
&& m_xImpl
->mpOutliner
)
263 return m_xImpl
->mpOutliner
->CreateParaObject();
268 OUString
TextAPIEditSource::GetText() const
270 if (m_xImpl
->mpDoc
&& m_xImpl
->mpOutliner
)
271 return m_xImpl
->mpOutliner
->GetEditEngine().GetText();
278 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */