1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: textapi.cxx,v $
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
;
54 class UndoTextAPIChanged
: public SdrUndoAction
57 UndoTextAPIChanged( SdrModel
& rModel
, TextApiObject
* pTextObj
);
58 ~UndoTextAPIChanged();
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() )
73 , mxTextObj( pTextObj
)
77 UndoTextAPIChanged::~UndoTextAPIChanged()
83 void UndoTextAPIChanged::Undo()
86 mpNewText
= mxTextObj
->CreateText();
88 mxTextObj
->SetText( *mpOldText
);
91 void UndoTextAPIChanged::Redo()
95 mxTextObj
->SetText( *mpNewText
);
99 struct TextAPIEditSource_Impl
101 // needed for "internal" refcounting
102 SdDrawDocument
* mpDoc
;
103 Outliner
* mpOutliner
;
104 SvxOutlinerForwarder
* mpTextForwarder
;
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
);
118 TextAPIEditSource(SdDrawDocument
* pDoc
);
119 virtual ~TextAPIEditSource();
122 void SetText( OutlinerParaObject
& rText
);
123 void SetString( const String
& rText
);
124 OutlinerParaObject
* CreateText();
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},
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()
159 rtl::Reference
< TextApiObject
> TextApiObject::create( SdDrawDocument
* pDoc
)
161 rtl::Reference
< TextApiObject
> xRet( new TextApiObject( new TextAPIEditSource( pDoc
) ) );
165 void SAL_CALL
TextApiObject::dispose() throw(RuntimeException
)
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() );
212 pImpl
= dynamic_cast< TextApiObject
* >( SvxUnoTextBase::getImplementation( xText
) );
217 TextAPIEditSource::TextAPIEditSource( const TextAPIEditSource
& rSource
)
218 : SvxEditSource( *this )
220 // shallow copy; uses internal refcounting
221 pImpl
= rSource
.pImpl
;
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
)
239 pImpl
->mpOutliner
= 0;
240 pImpl
->mpTextForwarder
= 0;
244 TextAPIEditSource::~TextAPIEditSource()
250 void TextAPIEditSource::Dispose()
253 delete pImpl
->mpTextForwarder
;
254 pImpl
->mpTextForwarder
= 0;
256 delete pImpl
->mpOutliner
;
257 pImpl
->mpOutliner
= 0;
260 SvxTextForwarder
* TextAPIEditSource::GetTextForwarder()
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
)
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
)
297 if( !pImpl
->mpOutliner
)
299 //init draw model first
300 pImpl
->mpOutliner
= new Outliner( pImpl
->mpDoc
, OUTLINERMODE_TEXTOBJECT
);
301 pImpl
->mpDoc
->SetCalcFieldValueHdl( pImpl
->mpOutliner
);
304 pImpl
->mpOutliner
->Clear();
305 pImpl
->mpOutliner
->Insert( rText
);
309 OutlinerParaObject
* TextAPIEditSource::CreateText()
311 if ( pImpl
->mpDoc
&& pImpl
->mpOutliner
)
312 return pImpl
->mpOutliner
->CreateParaObject();
317 String
TextAPIEditSource::GetText()
319 if ( pImpl
->mpDoc
&& pImpl
->mpOutliner
)
320 return pImpl
->mpOutliner
->GetEditEngine().GetText();