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