xref: /AOO41X/main/sd/source/core/text/textapi.cxx (revision 79aad27f7f29270c03e208e3d687e8e3850af11d)
1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sd.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/text/XTextField.hpp>
28cdf0e10cSrcweir #include <com/sun/star/container/XNameContainer.hpp>
29cdf0e10cSrcweir #include <com/sun/star/beans/PropertyAttribute.hpp>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include <textapi.hxx>
32cdf0e10cSrcweir #include <drawdoc.hxx>
33cdf0e10cSrcweir #include <editeng/eeitem.hxx>
34cdf0e10cSrcweir #include <editeng/editeng.hxx>
35cdf0e10cSrcweir #include <editeng/outlobj.hxx>
36cdf0e10cSrcweir #include "Outliner.hxx"
37cdf0e10cSrcweir #include <svx/svdpool.hxx>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir using ::rtl::OUString;
40cdf0e10cSrcweir 
41cdf0e10cSrcweir using namespace ::com::sun::star::uno;
42cdf0e10cSrcweir using namespace ::com::sun::star::text;
43cdf0e10cSrcweir using namespace ::com::sun::star::beans;
44cdf0e10cSrcweir using namespace ::com::sun::star::container;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir namespace sd {
47cdf0e10cSrcweir 
48cdf0e10cSrcweir class UndoTextAPIChanged : public SdrUndoAction
49cdf0e10cSrcweir {
50cdf0e10cSrcweir public:
51cdf0e10cSrcweir 	UndoTextAPIChanged( SdrModel& rModel, TextApiObject* pTextObj );
52cdf0e10cSrcweir 	~UndoTextAPIChanged();
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 	virtual void Undo();
55cdf0e10cSrcweir 	virtual void Redo();
56cdf0e10cSrcweir 
57cdf0e10cSrcweir protected:
58cdf0e10cSrcweir 	OutlinerParaObject*	mpOldText;
59cdf0e10cSrcweir 	OutlinerParaObject*	mpNewText;
60cdf0e10cSrcweir 	rtl::Reference< TextApiObject > mxTextObj;
61cdf0e10cSrcweir };
62cdf0e10cSrcweir 
UndoTextAPIChanged(SdrModel & rModel,TextApiObject * pTextObj)63cdf0e10cSrcweir UndoTextAPIChanged::UndoTextAPIChanged(SdrModel& rModel, TextApiObject* pTextObj )
64cdf0e10cSrcweir : SdrUndoAction( rModel )
65cdf0e10cSrcweir , mpOldText( pTextObj->CreateText() )
66cdf0e10cSrcweir , mpNewText( 0 )
67cdf0e10cSrcweir , mxTextObj( pTextObj )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
~UndoTextAPIChanged()71cdf0e10cSrcweir UndoTextAPIChanged::~UndoTextAPIChanged()
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     delete mpOldText;
74cdf0e10cSrcweir     delete mpNewText;
75cdf0e10cSrcweir }
76cdf0e10cSrcweir 
Undo()77cdf0e10cSrcweir void UndoTextAPIChanged::Undo()
78cdf0e10cSrcweir {
79cdf0e10cSrcweir     if( !mpNewText )
80cdf0e10cSrcweir         mpNewText = mxTextObj->CreateText();
81cdf0e10cSrcweir 
82cdf0e10cSrcweir     mxTextObj->SetText( *mpOldText );
83cdf0e10cSrcweir }
84cdf0e10cSrcweir 
Redo()85cdf0e10cSrcweir void UndoTextAPIChanged::Redo()
86cdf0e10cSrcweir {
87cdf0e10cSrcweir     if( mpNewText )
88cdf0e10cSrcweir     {
89cdf0e10cSrcweir         mxTextObj->SetText( *mpNewText );
90cdf0e10cSrcweir     }
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir struct TextAPIEditSource_Impl
94cdf0e10cSrcweir {
95cdf0e10cSrcweir 	// needed for "internal" refcounting
96cdf0e10cSrcweir 	SdDrawDocument*					mpDoc;
97cdf0e10cSrcweir 	Outliner*						mpOutliner;
98cdf0e10cSrcweir 	SvxOutlinerForwarder*			mpTextForwarder;
99cdf0e10cSrcweir 	sal_Int32						mnRef;
100cdf0e10cSrcweir };
101cdf0e10cSrcweir 
102cdf0e10cSrcweir class TextAPIEditSource : public SvxEditSource
103cdf0e10cSrcweir {
104cdf0e10cSrcweir 	TextAPIEditSource_Impl* pImpl;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir 	virtual SvxEditSource*		Clone() const;
107cdf0e10cSrcweir 	virtual SvxTextForwarder*	GetTextForwarder();
108cdf0e10cSrcweir 	virtual void				UpdateData();
109cdf0e10cSrcweir 	explicit			TextAPIEditSource( const TextAPIEditSource& rSource );
110cdf0e10cSrcweir 
111cdf0e10cSrcweir public:
112cdf0e10cSrcweir 						TextAPIEditSource(SdDrawDocument* pDoc);
113cdf0e10cSrcweir 	virtual				~TextAPIEditSource();
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	void				Dispose();
116cdf0e10cSrcweir 	void				SetText( OutlinerParaObject& rText );
117cdf0e10cSrcweir 	OutlinerParaObject* CreateText();
118cdf0e10cSrcweir 	String				GetText();
GetDoc()119cdf0e10cSrcweir 	SdDrawDocument*     GetDoc() { return pImpl->mpDoc; }
120cdf0e10cSrcweir };
121cdf0e10cSrcweir 
ImplGetSdTextPortionPropertyMap()122cdf0e10cSrcweir const SvxItemPropertySet* ImplGetSdTextPortionPropertyMap()
123cdf0e10cSrcweir {
124cdf0e10cSrcweir     static const SfxItemPropertyMapEntry aSdTextPortionPropertyEntries[] =
125cdf0e10cSrcweir 	{
126cdf0e10cSrcweir 		SVX_UNOEDIT_CHAR_PROPERTIES,
127cdf0e10cSrcweir 		SVX_UNOEDIT_FONT_PROPERTIES,
128cdf0e10cSrcweir 		SVX_UNOEDIT_OUTLINER_PROPERTIES,
129cdf0e10cSrcweir 		SVX_UNOEDIT_PARA_PROPERTIES,
130cdf0e10cSrcweir 		{MAP_CHAR_LEN("TextField"),						EE_FEATURE_FIELD,	&::getCppuType((const Reference< XTextField >*)0),	PropertyAttribute::READONLY, 0 },
131cdf0e10cSrcweir 		{MAP_CHAR_LEN("TextPortionType"),				WID_PORTIONTYPE,	&::getCppuType((const OUString*)0), PropertyAttribute::READONLY, 0 },
132cdf0e10cSrcweir 		{MAP_CHAR_LEN("TextUserDefinedAttributes"),		EE_CHAR_XMLATTRIBS,		&::getCppuType((const Reference< XNameContainer >*)0)  , 		0,     0},
133cdf0e10cSrcweir 		{MAP_CHAR_LEN("ParaUserDefinedAttributes"),		EE_PARA_XMLATTRIBS,		&::getCppuType((const Reference< XNameContainer >*)0)  , 		0,     0},
134cdf0e10cSrcweir 		{0,0,0,0,0,0}
135cdf0e10cSrcweir 	};
136cdf0e10cSrcweir 	static SvxItemPropertySet aSdTextPortionPropertyMap( aSdTextPortionPropertyEntries, SdrObject::GetGlobalDrawObjectItemPool() );
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	return &aSdTextPortionPropertyMap;
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
TextApiObject(TextAPIEditSource * pEditSource)141cdf0e10cSrcweir TextApiObject::TextApiObject( TextAPIEditSource* pEditSource )
142cdf0e10cSrcweir : SvxUnoText( pEditSource, ImplGetSdTextPortionPropertyMap(), Reference < XText >() )
143cdf0e10cSrcweir , mpSource(pEditSource)
144cdf0e10cSrcweir {
145cdf0e10cSrcweir }
146cdf0e10cSrcweir 
~TextApiObject()147cdf0e10cSrcweir TextApiObject::~TextApiObject() throw()
148cdf0e10cSrcweir {
149cdf0e10cSrcweir 	dispose();
150cdf0e10cSrcweir }
151cdf0e10cSrcweir 
create(SdDrawDocument * pDoc)152cdf0e10cSrcweir rtl::Reference< TextApiObject > TextApiObject::create( SdDrawDocument* pDoc )
153cdf0e10cSrcweir {
154cdf0e10cSrcweir     rtl::Reference< TextApiObject > xRet( new TextApiObject( new TextAPIEditSource( pDoc ) ) );
155cdf0e10cSrcweir     return xRet;
156cdf0e10cSrcweir }
157cdf0e10cSrcweir 
dispose()158cdf0e10cSrcweir void SAL_CALL TextApiObject::dispose() throw(RuntimeException)
159cdf0e10cSrcweir {
160cdf0e10cSrcweir     if( mpSource )
161cdf0e10cSrcweir     {
162cdf0e10cSrcweir         mpSource->Dispose();
163cdf0e10cSrcweir         delete mpSource;
164cdf0e10cSrcweir         mpSource = 0;
165cdf0e10cSrcweir     }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir     // SvxUnoText::dispose();
168cdf0e10cSrcweir }
169cdf0e10cSrcweir 
CreateText()170cdf0e10cSrcweir OutlinerParaObject* TextApiObject::CreateText()
171cdf0e10cSrcweir {
172cdf0e10cSrcweir     return mpSource->CreateText();
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
SetText(OutlinerParaObject & rText)175cdf0e10cSrcweir void TextApiObject::SetText( OutlinerParaObject& rText )
176cdf0e10cSrcweir {
177cdf0e10cSrcweir     SdrModel* pModel = mpSource->GetDoc();
178cdf0e10cSrcweir     if( pModel && pModel->IsUndoEnabled() )
179cdf0e10cSrcweir         pModel->AddUndo( new UndoTextAPIChanged( *pModel, this ) );
180cdf0e10cSrcweir 
181cdf0e10cSrcweir     mpSource->SetText( rText );
182cdf0e10cSrcweir 	maSelection.nStartPara = 0xffff;
183cdf0e10cSrcweir }
184cdf0e10cSrcweir 
GetText()185cdf0e10cSrcweir String TextApiObject::GetText()
186cdf0e10cSrcweir {
187cdf0e10cSrcweir     return mpSource->GetText();
188cdf0e10cSrcweir }
189cdf0e10cSrcweir 
getImplementation(const::com::sun::star::uno::Reference<::com::sun::star::text::XText> & xText)190cdf0e10cSrcweir TextApiObject* TextApiObject::getImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::text::XText >& xText )
191cdf0e10cSrcweir {
192cdf0e10cSrcweir 	TextApiObject* pImpl = dynamic_cast< TextApiObject* >( xText.get() );
193cdf0e10cSrcweir 
194cdf0e10cSrcweir 	if( !pImpl )
195cdf0e10cSrcweir 		pImpl = dynamic_cast< TextApiObject* >(  SvxUnoTextBase::getImplementation( xText ) );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	return pImpl;
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
TextAPIEditSource(const TextAPIEditSource & rSource)200cdf0e10cSrcweir TextAPIEditSource::TextAPIEditSource( const TextAPIEditSource& rSource )
201cdf0e10cSrcweir : SvxEditSource( *this )
202cdf0e10cSrcweir {
203cdf0e10cSrcweir 	// shallow copy; uses internal refcounting
204cdf0e10cSrcweir 	pImpl = rSource.pImpl;
205cdf0e10cSrcweir 	pImpl->mnRef++;
206cdf0e10cSrcweir }
207cdf0e10cSrcweir 
Clone() const208cdf0e10cSrcweir SvxEditSource* TextAPIEditSource::Clone() const
209cdf0e10cSrcweir {
210cdf0e10cSrcweir 	return new TextAPIEditSource( *this );
211cdf0e10cSrcweir }
212cdf0e10cSrcweir 
UpdateData()213cdf0e10cSrcweir void TextAPIEditSource::UpdateData()
214cdf0e10cSrcweir {
215cdf0e10cSrcweir 	// data is kept in outliner all the time
216cdf0e10cSrcweir }
217cdf0e10cSrcweir 
TextAPIEditSource(SdDrawDocument * pDoc)218cdf0e10cSrcweir TextAPIEditSource::TextAPIEditSource(SdDrawDocument* pDoc)
219cdf0e10cSrcweir : pImpl(new TextAPIEditSource_Impl)
220cdf0e10cSrcweir {
221cdf0e10cSrcweir 	pImpl->mpDoc = pDoc;
222cdf0e10cSrcweir 	pImpl->mpOutliner = 0;
223cdf0e10cSrcweir 	pImpl->mpTextForwarder = 0;
224cdf0e10cSrcweir 	pImpl->mnRef = 1;
225cdf0e10cSrcweir }
226cdf0e10cSrcweir 
~TextAPIEditSource()227cdf0e10cSrcweir TextAPIEditSource::~TextAPIEditSource()
228cdf0e10cSrcweir {
229cdf0e10cSrcweir 	if (!--pImpl->mnRef)
230cdf0e10cSrcweir 		delete pImpl;
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
Dispose()233cdf0e10cSrcweir void TextAPIEditSource::Dispose()
234cdf0e10cSrcweir {
235cdf0e10cSrcweir 	pImpl->mpDoc=0;
236cdf0e10cSrcweir 	delete pImpl->mpTextForwarder;
237cdf0e10cSrcweir     pImpl->mpTextForwarder = 0;
238cdf0e10cSrcweir 
239cdf0e10cSrcweir 	delete pImpl->mpOutliner;
240cdf0e10cSrcweir     pImpl->mpOutliner = 0;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
GetTextForwarder()243cdf0e10cSrcweir SvxTextForwarder* TextAPIEditSource::GetTextForwarder()
244cdf0e10cSrcweir {
245cdf0e10cSrcweir 	if( !pImpl->mpDoc )
246cdf0e10cSrcweir 		return 0; // mpDoc == 0 can be used to flag this as disposed
247cdf0e10cSrcweir 
248cdf0e10cSrcweir 	if( !pImpl->mpOutliner )
249cdf0e10cSrcweir 	{
250cdf0e10cSrcweir         //init draw model first
251cdf0e10cSrcweir 		pImpl->mpOutliner = new Outliner( pImpl->mpDoc, OUTLINERMODE_TEXTOBJECT );
252cdf0e10cSrcweir 		pImpl->mpDoc->SetCalcFieldValueHdl( pImpl->mpOutliner );
253cdf0e10cSrcweir 	}
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	if( !pImpl->mpTextForwarder )
256cdf0e10cSrcweir 		pImpl->mpTextForwarder = new SvxOutlinerForwarder( *pImpl->mpOutliner, 0 );
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	return pImpl->mpTextForwarder;
259cdf0e10cSrcweir }
260cdf0e10cSrcweir 
SetText(OutlinerParaObject & rText)261cdf0e10cSrcweir void TextAPIEditSource::SetText( OutlinerParaObject& rText )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir 	if ( pImpl->mpDoc )
264cdf0e10cSrcweir 	{
265cdf0e10cSrcweir 		if( !pImpl->mpOutliner )
266cdf0e10cSrcweir 		{
267cdf0e10cSrcweir             //init draw model first
268cdf0e10cSrcweir             pImpl->mpOutliner = new Outliner( pImpl->mpDoc, OUTLINERMODE_TEXTOBJECT );
269cdf0e10cSrcweir 			pImpl->mpDoc->SetCalcFieldValueHdl( pImpl->mpOutliner );
270cdf0e10cSrcweir 		}
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 		pImpl->mpOutliner->SetText( rText );
273cdf0e10cSrcweir 	}
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
CreateText()276cdf0e10cSrcweir OutlinerParaObject* TextAPIEditSource::CreateText()
277cdf0e10cSrcweir {
278cdf0e10cSrcweir 	if ( pImpl->mpDoc && pImpl->mpOutliner )
279cdf0e10cSrcweir 		return pImpl->mpOutliner->CreateParaObject();
280cdf0e10cSrcweir 	else
281cdf0e10cSrcweir 		return 0;
282cdf0e10cSrcweir }
283cdf0e10cSrcweir 
GetText()284cdf0e10cSrcweir String TextAPIEditSource::GetText()
285cdf0e10cSrcweir {
286cdf0e10cSrcweir 	if ( pImpl->mpDoc && pImpl->mpOutliner )
287cdf0e10cSrcweir 		return pImpl->mpOutliner->GetEditEngine().GetText();
288cdf0e10cSrcweir 	else
289cdf0e10cSrcweir 		return String();
290cdf0e10cSrcweir }
291cdf0e10cSrcweir 
292cdf0e10cSrcweir } // namespace sd
293