Add Marathi autocorrect
[LibreOffice.git] / sw / source / core / fields / textapi.cxx
blob187785a0b340aaebbd0bbb1ae9c2e0610cee92d7
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 <textapi.hxx>
21 #include <doc.hxx>
22 #include <IDocumentDrawModelAccess.hxx>
23 #include <docsh.hxx>
24 #include <docstyle.hxx>
25 #include <strings.hrc>
26 #include <SwStyleNameMapper.hxx>
27 #include <unoprnms.hxx>
28 #include <editeng/eeitem.hxx>
29 #include <editeng/editeng.hxx>
30 #include <editeng/outlobj.hxx>
31 #include <editeng/outliner.hxx>
32 #include <editeng/unoprnms.hxx>
33 #include <editeng/unoforou.hxx>
34 #include <editeng/unoipset.hxx>
36 #include <com/sun/star/text/XTextField.hpp>
37 #include <com/sun/star/container/XNameContainer.hpp>
38 #include <com/sun/star/beans/PropertyAttribute.hpp>
39 #include <com/sun/star/lang/Locale.hpp>
41 using namespace com::sun::star;
43 static const SvxItemPropertySet* ImplGetSvxTextPortionPropertySet()
45 static const SfxItemPropertyMapEntry aSvxTextPortionPropertyMap[] =
47 SVX_UNOEDIT_CHAR_PROPERTIES,
48 SVX_UNOEDIT_FONT_PROPERTIES,
49 SVX_UNOEDIT_OUTLINER_PROPERTIES,
50 SVX_UNOEDIT_PARA_PROPERTIES,
51 {UNO_NAME_PARA_STYLE_NAME, WID_PARASTYLENAME,
52 cppu::UnoType<OUString>::get(), beans::PropertyAttribute::MAYBEVOID, 0 },
53 {u"TextField"_ustr, EE_FEATURE_FIELD,
54 cppu::UnoType<text::XTextField>::get(), beans::PropertyAttribute::READONLY, 0 },
55 {u"TextPortionType"_ustr, WID_PORTIONTYPE,
56 ::cppu::UnoType<OUString>::get(), beans::PropertyAttribute::READONLY, 0 },
57 {u"TextUserDefinedAttributes"_ustr, EE_CHAR_XMLATTRIBS,
58 cppu::UnoType<css::container::XNameContainer>::get(), 0, 0},
59 {u"ParaUserDefinedAttributes"_ustr, EE_PARA_XMLATTRIBS,
60 cppu::UnoType<css::container::XNameContainer>::get(), 0, 0},
62 static SvxItemPropertySet aSvxTextPortionPropertySet( aSvxTextPortionPropertyMap, EditEngine::GetGlobalItemPool() );
63 return &aSvxTextPortionPropertySet;
66 SwTextAPIObject::SwTextAPIObject( std::unique_ptr<SwTextAPIEditSource> p )
67 : SvxUnoText( p.get(), ImplGetSvxTextPortionPropertySet(), uno::Reference < text::XText >() )
68 , m_pSource(std::move(p))
72 SwTextAPIObject::~SwTextAPIObject() noexcept
74 m_pSource->Dispose();
75 m_pSource.reset();
78 struct SwTextAPIEditSource_Impl
80 // needed for "internal" refcounting
81 SfxItemPool* mpPool;
82 SwDoc* mpDoc;
83 std::unique_ptr<Outliner> mpOutliner;
84 std::unique_ptr<SvxOutlinerForwarder> mpTextForwarder;
85 sal_Int32 mnRef;
88 namespace {
90 class SwTextAPIForwarder : public SvxOutlinerForwarder
92 public:
93 using SvxOutlinerForwarder::SvxOutlinerForwarder;
94 OUString GetStyleSheet(sal_Int32 nPara) const override
96 return SwStyleNameMapper::GetProgName(SvxOutlinerForwarder::GetStyleSheet(nPara), SwGetPoolIdFromName::TxtColl);
99 void SetStyleSheet(sal_Int32 nPara, const OUString& rStyleName) override
101 SvxOutlinerForwarder::SetStyleSheet(nPara, SwStyleNameMapper::GetUIName(rStyleName, SwGetPoolIdFromName::TxtColl));
107 SwTextAPIEditSource::SwTextAPIEditSource( const SwTextAPIEditSource& rSource )
108 : SvxEditSource( *this )
110 // shallow copy; uses internal refcounting
111 m_pImpl = rSource.m_pImpl;
112 m_pImpl->mnRef++;
115 std::unique_ptr<SvxEditSource> SwTextAPIEditSource::Clone() const
117 return std::unique_ptr<SvxEditSource>(new SwTextAPIEditSource( *this ));
120 void SwTextAPIEditSource::UpdateData()
122 // data is kept in outliner all the time
125 SwTextAPIEditSource::SwTextAPIEditSource(SwDoc* pDoc)
126 : m_pImpl(new SwTextAPIEditSource_Impl)
128 if (SwDocShell* pShell = pDoc->GetDocShell())
130 m_pImpl->mpPool = &pShell->GetPool();
131 m_pImpl->mpDoc = pDoc;
132 m_pImpl->mnRef = 1;
136 SwTextAPIEditSource::~SwTextAPIEditSource()
138 if (!--m_pImpl->mnRef)
139 delete m_pImpl;
142 void SwTextAPIEditSource::Dispose()
144 m_pImpl->mpPool=nullptr;
145 m_pImpl->mpDoc=nullptr;
146 m_pImpl->mpTextForwarder.reset();
147 m_pImpl->mpOutliner.reset();
150 void SwTextAPIEditSource::EnsureOutliner()
152 if( !m_pImpl->mpOutliner )
154 if (SwDocShell* pShell = m_pImpl->mpDoc->GetDocShell())
156 //init draw model first
157 m_pImpl->mpDoc->getIDocumentDrawModelAccess().GetOrCreateDrawModel();
158 m_pImpl->mpOutliner.reset(new Outliner(m_pImpl->mpPool, OutlinerMode::TextObject));
159 m_pImpl->mpOutliner->SetStyleSheetPool(
160 static_cast<SwDocStyleSheetPool*>(pShell->GetStyleSheetPool())->GetEEStyleSheetPool());
161 m_pImpl->mpDoc->SetCalcFieldValueHdl(m_pImpl->mpOutliner.get());
166 SvxTextForwarder* SwTextAPIEditSource::GetTextForwarder()
168 if( !m_pImpl->mpPool )
169 return nullptr; // mpPool == 0 can be used to flag this as disposed
171 EnsureOutliner();
173 if( !m_pImpl->mpTextForwarder )
175 m_pImpl->mpTextForwarder.reset(new SwTextAPIForwarder(*m_pImpl->mpOutliner, false));
178 return m_pImpl->mpTextForwarder.get();
181 void SwTextAPIEditSource::SetText( OutlinerParaObject const & rText )
183 if ( m_pImpl->mpPool )
185 EnsureOutliner();
186 m_pImpl->mpOutliner->SetText( rText );
190 void SwTextAPIEditSource::SetString( const OUString& rText )
192 if ( !m_pImpl->mpPool )
193 return;
195 if ( m_pImpl->mpOutliner )
196 m_pImpl->mpOutliner->Clear();
198 EnsureOutliner();
200 if (auto pStyle = m_pImpl->mpOutliner->GetStyleSheetPool()->Find(SwResId(STR_POOLCOLL_COMMENT), SfxStyleFamily::Para))
201 m_pImpl->mpOutliner->SetStyleSheet(0, static_cast<SfxStyleSheet*>(pStyle));
202 m_pImpl->mpOutliner->Insert( rText );
205 std::optional<OutlinerParaObject> SwTextAPIEditSource::CreateText()
207 if ( m_pImpl->mpPool && m_pImpl->mpOutliner )
208 return m_pImpl->mpOutliner->CreateParaObject();
209 else
210 return std::nullopt;
213 OUString SwTextAPIEditSource::GetText() const
215 if ( m_pImpl->mpPool && m_pImpl->mpOutliner )
216 return m_pImpl->mpOutliner->GetEditEngine().GetText();
217 else
218 return OUString();
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */