xref: /trunk/main/sw/source/core/fields/textapi.cxx (revision 79aad27f)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*efeef26fSAndrew Rist  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*efeef26fSAndrew Rist  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19*efeef26fSAndrew Rist  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <textapi.hxx>
28cdf0e10cSrcweir #include <doc.hxx>
29cdf0e10cSrcweir #include <docsh.hxx>
30cdf0e10cSrcweir #include <editeng/eeitem.hxx>
31cdf0e10cSrcweir #include <editeng/editeng.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <com/sun/star/text/XTextField.hpp>
34cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
35cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir using namespace com::sun::star;
38cdf0e10cSrcweir 
ImplGetSvxTextPortionPropertySet()39cdf0e10cSrcweir static const SvxItemPropertySet* ImplGetSvxTextPortionPropertySet()
40cdf0e10cSrcweir {
41cdf0e10cSrcweir     static const SfxItemPropertyMapEntry aSvxTextPortionPropertyMap[] =
42cdf0e10cSrcweir 	{
43cdf0e10cSrcweir 		SVX_UNOEDIT_CHAR_PROPERTIES,
44cdf0e10cSrcweir 		SVX_UNOEDIT_FONT_PROPERTIES,
45cdf0e10cSrcweir 		SVX_UNOEDIT_OUTLINER_PROPERTIES,
46cdf0e10cSrcweir 		SVX_UNOEDIT_PARA_PROPERTIES,
47cdf0e10cSrcweir 		{MAP_CHAR_LEN("TextField"),						EE_FEATURE_FIELD,	&::getCppuType((const uno::Reference< text::XTextField >*)0),	beans::PropertyAttribute::READONLY, 0 },
48cdf0e10cSrcweir 		{MAP_CHAR_LEN("TextPortionType"),				WID_PORTIONTYPE,	&::getCppuType((const ::rtl::OUString*)0), beans::PropertyAttribute::READONLY, 0 },
49cdf0e10cSrcweir 		{MAP_CHAR_LEN("TextUserDefinedAttributes"),		EE_CHAR_XMLATTRIBS,		&::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >*)0)  , 		0,     0},
50cdf0e10cSrcweir 		{MAP_CHAR_LEN("ParaUserDefinedAttributes"),		EE_PARA_XMLATTRIBS,		&::getCppuType((const ::com::sun::star::uno::Reference< ::com::sun::star::container::XNameContainer >*)0)  , 		0,     0},
51cdf0e10cSrcweir 		{0,0,0,0,0,0}
52cdf0e10cSrcweir 	};
53cdf0e10cSrcweir     static SvxItemPropertySet aSvxTextPortionPropertySet( aSvxTextPortionPropertyMap, EditEngine::GetGlobalItemPool() );
54cdf0e10cSrcweir 	return &aSvxTextPortionPropertySet;
55cdf0e10cSrcweir }
56cdf0e10cSrcweir 
SwTextAPIObject(SwTextAPIEditSource * p)57cdf0e10cSrcweir SwTextAPIObject::SwTextAPIObject( SwTextAPIEditSource* p )
58cdf0e10cSrcweir : SvxUnoText( p, ImplGetSvxTextPortionPropertySet(), uno::Reference < text::XText >() )
59cdf0e10cSrcweir , pSource(p)
60cdf0e10cSrcweir {
61cdf0e10cSrcweir }
62cdf0e10cSrcweir 
~SwTextAPIObject()63cdf0e10cSrcweir SwTextAPIObject::~SwTextAPIObject() throw()
64cdf0e10cSrcweir {
65cdf0e10cSrcweir 	pSource->Dispose();
66cdf0e10cSrcweir 	delete pSource;
67cdf0e10cSrcweir }
68cdf0e10cSrcweir 
69cdf0e10cSrcweir struct SwTextAPIEditSource_Impl
70cdf0e10cSrcweir {
71cdf0e10cSrcweir 	// needed for "internal" refcounting
72cdf0e10cSrcweir 	SfxItemPool*					mpPool;
73cdf0e10cSrcweir 	SwDoc*							mpDoc;
74cdf0e10cSrcweir 	Outliner*						mpOutliner;
75cdf0e10cSrcweir 	SvxOutlinerForwarder*			mpTextForwarder;
76cdf0e10cSrcweir 	sal_Int32						mnRef;
77cdf0e10cSrcweir };
78cdf0e10cSrcweir 
SwTextAPIEditSource(const SwTextAPIEditSource & rSource)79cdf0e10cSrcweir SwTextAPIEditSource::SwTextAPIEditSource( const SwTextAPIEditSource& rSource )
80cdf0e10cSrcweir : SvxEditSource( *this )
81cdf0e10cSrcweir {
82cdf0e10cSrcweir 	// shallow copy; uses internal refcounting
83cdf0e10cSrcweir 	pImpl = rSource.pImpl;
84cdf0e10cSrcweir 	pImpl->mnRef++;
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
Clone() const87cdf0e10cSrcweir SvxEditSource* SwTextAPIEditSource::Clone() const
88cdf0e10cSrcweir {
89cdf0e10cSrcweir 	return new SwTextAPIEditSource( *this );
90cdf0e10cSrcweir }
91cdf0e10cSrcweir 
UpdateData()92cdf0e10cSrcweir void SwTextAPIEditSource::UpdateData()
93cdf0e10cSrcweir {
94cdf0e10cSrcweir 	// data is kept in outliner all the time
95cdf0e10cSrcweir }
96cdf0e10cSrcweir 
SwTextAPIEditSource(SwDoc * pDoc)97cdf0e10cSrcweir SwTextAPIEditSource::SwTextAPIEditSource(SwDoc* pDoc)
98cdf0e10cSrcweir : pImpl(new SwTextAPIEditSource_Impl)
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	pImpl->mpPool = &pDoc->GetDocShell()->GetPool();
101cdf0e10cSrcweir 	pImpl->mpDoc = pDoc;
102cdf0e10cSrcweir 	pImpl->mpOutliner = 0;
103cdf0e10cSrcweir 	pImpl->mpTextForwarder = 0;
104cdf0e10cSrcweir 	pImpl->mnRef = 1;
105cdf0e10cSrcweir }
106cdf0e10cSrcweir 
~SwTextAPIEditSource()107cdf0e10cSrcweir SwTextAPIEditSource::~SwTextAPIEditSource()
108cdf0e10cSrcweir {
109cdf0e10cSrcweir 	if (!--pImpl->mnRef)
110cdf0e10cSrcweir 		delete pImpl;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir 
Dispose()113cdf0e10cSrcweir void SwTextAPIEditSource::Dispose()
114cdf0e10cSrcweir {
115cdf0e10cSrcweir 	pImpl->mpPool=0;
116cdf0e10cSrcweir 	pImpl->mpDoc=0;
117cdf0e10cSrcweir 	DELETEZ(pImpl->mpTextForwarder);
118cdf0e10cSrcweir 	DELETEZ(pImpl->mpOutliner);
119cdf0e10cSrcweir }
120cdf0e10cSrcweir 
GetTextForwarder()121cdf0e10cSrcweir SvxTextForwarder* SwTextAPIEditSource::GetTextForwarder()
122cdf0e10cSrcweir {
123cdf0e10cSrcweir 	if( !pImpl->mpPool )
124cdf0e10cSrcweir 		return 0; // mpPool == 0 can be used to flag this as disposed
125cdf0e10cSrcweir 
126cdf0e10cSrcweir 	if( !pImpl->mpOutliner )
127cdf0e10cSrcweir 	{
128cdf0e10cSrcweir         //init draw model first
129cdf0e10cSrcweir         pImpl->mpDoc->GetOrCreateDrawModel();
130cdf0e10cSrcweir 		pImpl->mpOutliner = new Outliner( pImpl->mpPool, OUTLINERMODE_TEXTOBJECT );
131cdf0e10cSrcweir 		pImpl->mpDoc->SetCalcFieldValueHdl( pImpl->mpOutliner );
132cdf0e10cSrcweir 	}
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 	if( !pImpl->mpTextForwarder )
135cdf0e10cSrcweir 		pImpl->mpTextForwarder = new SvxOutlinerForwarder( *pImpl->mpOutliner, 0 );
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 	return pImpl->mpTextForwarder;
138cdf0e10cSrcweir }
139cdf0e10cSrcweir 
SetText(OutlinerParaObject & rText)140cdf0e10cSrcweir void SwTextAPIEditSource::SetText( OutlinerParaObject& rText )
141cdf0e10cSrcweir {
142cdf0e10cSrcweir 	if ( pImpl->mpPool )
143cdf0e10cSrcweir 	{
144cdf0e10cSrcweir 		if( !pImpl->mpOutliner )
145cdf0e10cSrcweir 		{
146cdf0e10cSrcweir             //init draw model first
147cdf0e10cSrcweir             pImpl->mpDoc->GetOrCreateDrawModel();
148cdf0e10cSrcweir             pImpl->mpOutliner = new Outliner( pImpl->mpPool, OUTLINERMODE_TEXTOBJECT );
149cdf0e10cSrcweir 			pImpl->mpDoc->SetCalcFieldValueHdl( pImpl->mpOutliner );
150cdf0e10cSrcweir 		}
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 		pImpl->mpOutliner->SetText( rText );
153cdf0e10cSrcweir 	}
154cdf0e10cSrcweir }
155cdf0e10cSrcweir 
SetString(const String & rText)156cdf0e10cSrcweir void SwTextAPIEditSource::SetString( const String& rText )
157cdf0e10cSrcweir {
158cdf0e10cSrcweir 	if ( pImpl->mpPool )
159cdf0e10cSrcweir 	{
160cdf0e10cSrcweir 		if( !pImpl->mpOutliner )
161cdf0e10cSrcweir 		{
162cdf0e10cSrcweir             //init draw model first
163cdf0e10cSrcweir             pImpl->mpDoc->GetOrCreateDrawModel();
164cdf0e10cSrcweir 			pImpl->mpOutliner = new Outliner( pImpl->mpPool, OUTLINERMODE_TEXTOBJECT );
165cdf0e10cSrcweir 			pImpl->mpDoc->SetCalcFieldValueHdl( pImpl->mpOutliner );
166cdf0e10cSrcweir 		}
167cdf0e10cSrcweir 		else
168cdf0e10cSrcweir 			pImpl->mpOutliner->Clear();
169cdf0e10cSrcweir 		pImpl->mpOutliner->Insert( rText );
170cdf0e10cSrcweir 	}
171cdf0e10cSrcweir }
172cdf0e10cSrcweir 
CreateText()173cdf0e10cSrcweir OutlinerParaObject* SwTextAPIEditSource::CreateText()
174cdf0e10cSrcweir {
175cdf0e10cSrcweir 	if ( pImpl->mpPool && pImpl->mpOutliner )
176cdf0e10cSrcweir 		return pImpl->mpOutliner->CreateParaObject();
177cdf0e10cSrcweir 	else
178cdf0e10cSrcweir 		return 0;
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
GetText()181cdf0e10cSrcweir String SwTextAPIEditSource::GetText()
182cdf0e10cSrcweir {
183cdf0e10cSrcweir 	if ( pImpl->mpPool && pImpl->mpOutliner )
184cdf0e10cSrcweir 		return pImpl->mpOutliner->GetEditEngine().GetText();
185cdf0e10cSrcweir 	else
186cdf0e10cSrcweir 		return String();
187cdf0e10cSrcweir }
188