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