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