merge the formfield patch from ooo-build
[ooovba.git] / sd / source / core / text / textapi.cxx
blobdf2786d62ef9637b6ad90557fcb6f9fbaee9aa02
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: textapi.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sd.hxx"
34 #include <com/sun/star/text/XTextField.hpp>
35 #include <com/sun/star/container/XNameContainer.hpp>
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
38 #include <textapi.hxx>
39 #include <drawdoc.hxx>
40 #include <svx/eeitem.hxx>
41 #include <svx/editeng.hxx>
42 #include <svx/outlobj.hxx>
43 #include "Outliner.hxx"
45 using ::rtl::OUString;
47 using namespace ::com::sun::star::uno;
48 using namespace ::com::sun::star::text;
49 using namespace ::com::sun::star::beans;
50 using namespace ::com::sun::star::container;
52 namespace sd {
54 class UndoTextAPIChanged : public SdrUndoAction
56 public:
57 UndoTextAPIChanged( SdrModel& rModel, TextApiObject* pTextObj );
58 ~UndoTextAPIChanged();
60 virtual void Undo();
61 virtual void Redo();
63 protected:
64 OutlinerParaObject* mpOldText;
65 OutlinerParaObject* mpNewText;
66 rtl::Reference< TextApiObject > mxTextObj;
69 UndoTextAPIChanged::UndoTextAPIChanged(SdrModel& rModel, TextApiObject* pTextObj )
70 : SdrUndoAction( rModel )
71 , mpOldText( pTextObj->CreateText() )
72 , mpNewText( 0 )
73 , mxTextObj( pTextObj )
77 UndoTextAPIChanged::~UndoTextAPIChanged()
79 delete mpOldText;
80 delete mpNewText;
83 void UndoTextAPIChanged::Undo()
85 if( !mpNewText )
86 mpNewText = mxTextObj->CreateText();
88 mxTextObj->SetText( *mpOldText );
91 void UndoTextAPIChanged::Redo()
93 if( mpNewText )
95 mxTextObj->SetText( *mpNewText );
99 struct TextAPIEditSource_Impl
101 // needed for "internal" refcounting
102 SdDrawDocument* mpDoc;
103 Outliner* mpOutliner;
104 SvxOutlinerForwarder* mpTextForwarder;
105 sal_Int32 mnRef;
108 class TextAPIEditSource : public SvxEditSource
110 TextAPIEditSource_Impl* pImpl;
112 virtual SvxEditSource* Clone() const;
113 virtual SvxTextForwarder* GetTextForwarder();
114 virtual void UpdateData();
115 explicit TextAPIEditSource( const TextAPIEditSource& rSource );
117 public:
118 TextAPIEditSource(SdDrawDocument* pDoc);
119 virtual ~TextAPIEditSource();
121 void Dispose();
122 void SetText( OutlinerParaObject& rText );
123 void SetString( const String& rText );
124 OutlinerParaObject* CreateText();
125 String GetText();
126 SdDrawDocument* GetDoc() { return pImpl->mpDoc; }
129 const SvxItemPropertySet* ImplGetSdTextPortionPropertyMap()
131 static const SfxItemPropertyMapEntry aSdTextPortionPropertyEntries[] =
133 SVX_UNOEDIT_CHAR_PROPERTIES,
134 SVX_UNOEDIT_FONT_PROPERTIES,
135 SVX_UNOEDIT_OUTLINER_PROPERTIES,
136 SVX_UNOEDIT_PARA_PROPERTIES,
137 {MAP_CHAR_LEN("TextField"), EE_FEATURE_FIELD, &::getCppuType((const Reference< XTextField >*)0), PropertyAttribute::READONLY, 0 },
138 {MAP_CHAR_LEN("TextPortionType"), WID_PORTIONTYPE, &::getCppuType((const OUString*)0), PropertyAttribute::READONLY, 0 },
139 {MAP_CHAR_LEN("TextUserDefinedAttributes"), EE_CHAR_XMLATTRIBS, &::getCppuType((const Reference< XNameContainer >*)0) , 0, 0},
140 {MAP_CHAR_LEN("ParaUserDefinedAttributes"), EE_PARA_XMLATTRIBS, &::getCppuType((const Reference< XNameContainer >*)0) , 0, 0},
141 {0,0,0,0,0,0}
143 static SvxItemPropertySet aSdTextPortionPropertyMap( aSdTextPortionPropertyEntries );
145 return &aSdTextPortionPropertyMap;
148 TextApiObject::TextApiObject( TextAPIEditSource* pEditSource )
149 : SvxUnoText( pEditSource, ImplGetSdTextPortionPropertyMap(), Reference < XText >() )
150 , mpSource(pEditSource)
154 TextApiObject::~TextApiObject() throw()
156 dispose();
159 rtl::Reference< TextApiObject > TextApiObject::create( SdDrawDocument* pDoc )
161 rtl::Reference< TextApiObject > xRet( new TextApiObject( new TextAPIEditSource( pDoc ) ) );
162 return xRet;
165 void SAL_CALL TextApiObject::dispose() throw(RuntimeException)
167 if( mpSource )
169 mpSource->Dispose();
170 delete mpSource;
171 mpSource = 0;
174 // SvxUnoText::dispose();
177 OutlinerParaObject* TextApiObject::CreateText()
179 return mpSource->CreateText();
182 void TextApiObject::SetString( const String& rText )
184 SdrModel* pModel = mpSource->GetDoc();
185 if( pModel && pModel->IsUndoEnabled() )
186 pModel->AddUndo( new UndoTextAPIChanged( *pModel, this ) );
188 mpSource->SetString( rText );
189 maSelection.nStartPara = 0xffff;
192 void TextApiObject::SetText( OutlinerParaObject& rText )
194 SdrModel* pModel = mpSource->GetDoc();
195 if( pModel && pModel->IsUndoEnabled() )
196 pModel->AddUndo( new UndoTextAPIChanged( *pModel, this ) );
198 mpSource->SetText( rText );
199 maSelection.nStartPara = 0xffff;
202 String TextApiObject::GetText()
204 return mpSource->GetText();
207 TextApiObject* TextApiObject::getImplementation( const ::com::sun::star::uno::Reference< ::com::sun::star::text::XText >& xText )
209 TextApiObject* pImpl = dynamic_cast< TextApiObject* >( xText.get() );
211 if( !pImpl )
212 pImpl = dynamic_cast< TextApiObject* >( SvxUnoTextBase::getImplementation( xText ) );
214 return pImpl;
217 TextAPIEditSource::TextAPIEditSource( const TextAPIEditSource& rSource )
218 : SvxEditSource( *this )
220 // shallow copy; uses internal refcounting
221 pImpl = rSource.pImpl;
222 pImpl->mnRef++;
225 SvxEditSource* TextAPIEditSource::Clone() const
227 return new TextAPIEditSource( *this );
230 void TextAPIEditSource::UpdateData()
232 // data is kept in outliner all the time
235 TextAPIEditSource::TextAPIEditSource(SdDrawDocument* pDoc)
236 : pImpl(new TextAPIEditSource_Impl)
238 pImpl->mpDoc = pDoc;
239 pImpl->mpOutliner = 0;
240 pImpl->mpTextForwarder = 0;
241 pImpl->mnRef = 1;
244 TextAPIEditSource::~TextAPIEditSource()
246 if (!--pImpl->mnRef)
247 delete pImpl;
250 void TextAPIEditSource::Dispose()
252 pImpl->mpDoc=0;
253 delete pImpl->mpTextForwarder;
254 pImpl->mpTextForwarder = 0;
256 delete pImpl->mpOutliner;
257 pImpl->mpOutliner = 0;
260 SvxTextForwarder* TextAPIEditSource::GetTextForwarder()
262 if( !pImpl->mpDoc )
263 return 0; // mpDoc == 0 can be used to flag this as disposed
265 if( !pImpl->mpOutliner )
267 //init draw model first
268 pImpl->mpOutliner = new Outliner( pImpl->mpDoc, OUTLINERMODE_TEXTOBJECT );
269 pImpl->mpDoc->SetCalcFieldValueHdl( pImpl->mpOutliner );
272 if( !pImpl->mpTextForwarder )
273 pImpl->mpTextForwarder = new SvxOutlinerForwarder( *pImpl->mpOutliner, 0 );
275 return pImpl->mpTextForwarder;
278 void TextAPIEditSource::SetText( OutlinerParaObject& rText )
280 if ( pImpl->mpDoc )
282 if( !pImpl->mpOutliner )
284 //init draw model first
285 pImpl->mpOutliner = new Outliner( pImpl->mpDoc, OUTLINERMODE_TEXTOBJECT );
286 pImpl->mpDoc->SetCalcFieldValueHdl( pImpl->mpOutliner );
289 pImpl->mpOutliner->SetText( rText );
293 void TextAPIEditSource::SetString( const String& rText )
295 if ( pImpl->mpDoc )
297 if( !pImpl->mpOutliner )
299 //init draw model first
300 pImpl->mpOutliner = new Outliner( pImpl->mpDoc, OUTLINERMODE_TEXTOBJECT );
301 pImpl->mpDoc->SetCalcFieldValueHdl( pImpl->mpOutliner );
303 else
304 pImpl->mpOutliner->Clear();
305 pImpl->mpOutliner->Insert( rText );
309 OutlinerParaObject* TextAPIEditSource::CreateText()
311 if ( pImpl->mpDoc && pImpl->mpOutliner )
312 return pImpl->mpOutliner->CreateParaObject();
313 else
314 return 0;
317 String TextAPIEditSource::GetText()
319 if ( pImpl->mpDoc && pImpl->mpOutliner )
320 return pImpl->mpOutliner->GetEditEngine().GetText();
321 else
322 return String();
325 } // namespace sd