xref: /aoo41x/main/sc/source/ui/unoobj/fielduno.cxx (revision b3f79822)
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_sc.hxx"
26 #include <svl/smplhint.hxx>
27 
28 
29 #include <editeng/eeitem.hxx>
30 
31 #include <editeng/editeng.hxx>
32 #include <editeng/editobj.hxx>
33 #include <editeng/flditem.hxx>
34 #include <rtl/uuid.h>
35 
36 #include <com/sun/star/beans/PropertyAttribute.hpp>
37 #include <com/sun/star/text/TextContentAnchorType.hpp>
38 #include <com/sun/star/text/WrapTextMode.hpp>
39 #include <com/sun/star/text/FilenameDisplayFormat.hpp>
40 
41 #include "fielduno.hxx"
42 #include "textuno.hxx"
43 #include "miscuno.hxx"
44 #include "docsh.hxx"
45 #include "hints.hxx"
46 #include "editsrc.hxx"
47 #include "cellsuno.hxx"
48 #include "servuno.hxx"		// fuer IDs
49 #include "unoguard.hxx"
50 #include "unonames.hxx"
51 #include "editutil.hxx"
52 
53 using namespace com::sun::star;
54 
55 //------------------------------------------------------------------------
56 
57 //	alles ohne Which-ID, Map nur fuer PropertySetInfo
58 
lcl_GetURLPropertySet()59 const SfxItemPropertySet* lcl_GetURLPropertySet()
60 {
61     static SfxItemPropertyMapEntry aURLPropertyMap_Impl[] =
62 	{
63         {MAP_CHAR_LEN(SC_UNONAME_ANCTYPE),  0,  &getCppuType((text::TextContentAnchorType*)0), beans::PropertyAttribute::READONLY, 0 },
64         {MAP_CHAR_LEN(SC_UNONAME_ANCTYPES), 0,  &getCppuType((uno::Sequence<text::TextContentAnchorType>*)0), beans::PropertyAttribute::READONLY, 0 },
65         {MAP_CHAR_LEN(SC_UNONAME_REPR),     0,  &getCppuType((rtl::OUString*)0),    0, 0},
66         {MAP_CHAR_LEN(SC_UNONAME_TARGET),   0,  &getCppuType((rtl::OUString*)0),    0, 0},
67         {MAP_CHAR_LEN(SC_UNONAME_TEXTWRAP), 0,  &getCppuType((text::WrapTextMode*)0), beans::PropertyAttribute::READONLY, 0 },
68         {MAP_CHAR_LEN(SC_UNONAME_URL),      0,  &getCppuType((rtl::OUString*)0),    0, 0},
69         {0,0,0,0,0,0}
70 	};
71     static SfxItemPropertySet aURLPropertySet_Impl( aURLPropertyMap_Impl );
72 	return &aURLPropertySet_Impl;
73 }
74 
lcl_GetHeaderFieldPropertySet()75 const SfxItemPropertySet* lcl_GetHeaderFieldPropertySet()
76 {
77     static SfxItemPropertyMapEntry aHeaderFieldPropertyMap_Impl[] =
78 	{
79         {MAP_CHAR_LEN(SC_UNONAME_ANCTYPE),  0,  &getCppuType((text::TextContentAnchorType*)0), beans::PropertyAttribute::READONLY, 0 },
80         {MAP_CHAR_LEN(SC_UNONAME_ANCTYPES), 0,  &getCppuType((uno::Sequence<text::TextContentAnchorType>*)0), beans::PropertyAttribute::READONLY, 0 },
81         {MAP_CHAR_LEN(SC_UNONAME_TEXTWRAP), 0,  &getCppuType((text::WrapTextMode*)0), beans::PropertyAttribute::READONLY, 0 },
82         {0,0,0,0,0,0}
83 	};
84     static SfxItemPropertySet aHeaderFieldPropertySet_Impl( aHeaderFieldPropertyMap_Impl );
85 	return &aHeaderFieldPropertySet_Impl;
86 }
87 
lcl_GetFileFieldPropertySet()88 const SfxItemPropertySet* lcl_GetFileFieldPropertySet()
89 {
90     static SfxItemPropertyMapEntry aFileFieldPropertyMap_Impl[] =
91 	{
92         {MAP_CHAR_LEN(SC_UNONAME_ANCTYPE),  0,  &getCppuType((text::TextContentAnchorType*)0), beans::PropertyAttribute::READONLY, 0 },
93         {MAP_CHAR_LEN(SC_UNONAME_ANCTYPES), 0,  &getCppuType((uno::Sequence<text::TextContentAnchorType>*)0), beans::PropertyAttribute::READONLY, 0 },
94         {MAP_CHAR_LEN(SC_UNONAME_FILEFORM), 0,  &getCppuType((sal_Int16*)0),        0, 0 },
95         {MAP_CHAR_LEN(SC_UNONAME_TEXTWRAP), 0,  &getCppuType((text::WrapTextMode*)0), beans::PropertyAttribute::READONLY, 0 },
96         {0,0,0,0,0,0}
97 	};
98     static SfxItemPropertySet aFileFieldPropertySet_Impl( aFileFieldPropertyMap_Impl );
99     return &aFileFieldPropertySet_Impl;
100 }
101 
102 //------------------------------------------------------------------------
103 
104 #define SCTEXTFIELD_SERVICE			"com.sun.star.text.TextField"
105 #define SCTEXTCONTENT_SERVICE		"com.sun.star.text.TextContent"
106 
107 SC_SIMPLE_SERVICE_INFO( ScCellFieldsObj, "ScCellFieldsObj", "com.sun.star.text.TextFields" )
108 SC_SIMPLE_SERVICE_INFO( ScHeaderFieldsObj, "ScHeaderFieldsObj", "com.sun.star.text.TextFields" )
109 
110 //------------------------------------------------------------------------
111 
112 //	ScUnoEditEngine nur um aus einer EditEngine die Felder herauszubekommen...
113 
114 enum ScUnoCollectMode
115 {
116 	SC_UNO_COLLECT_NONE,
117 	SC_UNO_COLLECT_COUNT,
118 	SC_UNO_COLLECT_FINDINDEX,
119 	SC_UNO_COLLECT_FINDPOS
120 };
121 
122 class ScUnoEditEngine : public ScEditEngineDefaulter
123 {
124 	ScUnoCollectMode	eMode;
125 	sal_uInt16				nFieldCount;
126 	TypeId				aFieldType;
127 	SvxFieldData*		pFound;			// lokale Kopie
128 	sal_uInt16				nFieldPar;
129 	xub_StrLen			nFieldPos;
130 	sal_uInt16				nFieldIndex;
131 
132 public:
133 				ScUnoEditEngine(ScEditEngineDefaulter* pSource);
134 				~ScUnoEditEngine();
135 
136 					//!	nPos should be xub_StrLen
137 	virtual String	CalcFieldValue( const SvxFieldItem& rField, sal_uInt16 nPara, sal_uInt16 nPos,
138 									Color*& rTxtColor, Color*& rFldColor );
139 
140 	sal_uInt16			CountFields(TypeId aType);
141 	SvxFieldData*	FindByIndex(sal_uInt16 nIndex, TypeId aType);
142 	SvxFieldData*	FindByPos(sal_uInt16 nPar, xub_StrLen nPos, TypeId aType);
143 
GetFieldPar() const144 	sal_uInt16			GetFieldPar() const		{ return nFieldPar; }
GetFieldPos() const145 	xub_StrLen		GetFieldPos() const		{ return nFieldPos; }
146 };
147 
ScUnoEditEngine(ScEditEngineDefaulter * pSource)148 ScUnoEditEngine::ScUnoEditEngine(ScEditEngineDefaulter* pSource) :
149 	ScEditEngineDefaulter( *pSource ),
150 	eMode( SC_UNO_COLLECT_NONE ),
151 	nFieldCount( 0 ),
152 	aFieldType( NULL ),
153 	pFound( NULL )
154 {
155 	if (pSource)
156 	{
157 		EditTextObject* pData = pSource->CreateTextObject();
158 		SetText( *pData );
159 		delete pData;
160 	}
161 }
162 
~ScUnoEditEngine()163 ScUnoEditEngine::~ScUnoEditEngine()
164 {
165 	delete pFound;
166 }
167 
CalcFieldValue(const SvxFieldItem & rField,sal_uInt16 nPara,sal_uInt16 nPos,Color * & rTxtColor,Color * & rFldColor)168 String ScUnoEditEngine::CalcFieldValue( const SvxFieldItem& rField,
169 			sal_uInt16 nPara, sal_uInt16 nPos, Color*& rTxtColor, Color*& rFldColor )
170 {
171 	String aRet(EditEngine::CalcFieldValue( rField, nPara, nPos, rTxtColor, rFldColor ));
172 	if (eMode != SC_UNO_COLLECT_NONE)
173 	{
174 		const SvxFieldData*	pFieldData = rField.GetField();
175 		if ( pFieldData )
176 		{
177 			if ( !aFieldType || pFieldData->Type() == aFieldType )
178 			{
179 				if ( eMode == SC_UNO_COLLECT_FINDINDEX && !pFound && nFieldCount == nFieldIndex )
180 				{
181 					pFound = pFieldData->Clone();
182 					nFieldPar = nPara;
183 					nFieldPos = nPos;
184 				}
185 				if ( eMode == SC_UNO_COLLECT_FINDPOS && !pFound &&
186 						nPara == nFieldPar && nPos == nFieldPos )
187 				{
188 					pFound = pFieldData->Clone();
189 					nFieldIndex = nFieldCount;
190 				}
191 				++nFieldCount;
192 			}
193 		}
194 	}
195 	return aRet;
196 }
197 
CountFields(TypeId aType)198 sal_uInt16 ScUnoEditEngine::CountFields(TypeId aType)
199 {
200 	eMode = SC_UNO_COLLECT_COUNT;
201 	aFieldType = aType;
202 	nFieldCount = 0;
203 	UpdateFields();
204 	aFieldType = NULL;
205 	eMode = SC_UNO_COLLECT_NONE;
206 
207 	return nFieldCount;
208 }
209 
FindByIndex(sal_uInt16 nIndex,TypeId aType)210 SvxFieldData* ScUnoEditEngine::FindByIndex(sal_uInt16 nIndex, TypeId aType)
211 {
212 	eMode = SC_UNO_COLLECT_FINDINDEX;
213 	nFieldIndex = nIndex;
214 	aFieldType = aType;
215 	nFieldCount = 0;
216 	UpdateFields();
217 	aFieldType = NULL;
218 	eMode = SC_UNO_COLLECT_NONE;
219 
220 	return pFound;
221 }
222 
FindByPos(sal_uInt16 nPar,xub_StrLen nPos,TypeId aType)223 SvxFieldData* ScUnoEditEngine::FindByPos(sal_uInt16 nPar, xub_StrLen nPos, TypeId aType)
224 {
225 	eMode = SC_UNO_COLLECT_FINDPOS;
226 	nFieldPar = nPar;
227 	nFieldPos = nPos;
228 	aFieldType = aType;
229 	nFieldCount = 0;
230 	UpdateFields();
231 	aFieldType = NULL;
232 	eMode = SC_UNO_COLLECT_NONE;
233 
234 	return pFound;
235 }
236 
237 //------------------------------------------------------------------------
238 
ScCellFieldsObj(ScDocShell * pDocSh,const ScAddress & rPos)239 ScCellFieldsObj::ScCellFieldsObj(ScDocShell* pDocSh, const ScAddress& rPos) :
240 	pDocShell( pDocSh ),
241 	aCellPos( rPos ),
242     mpRefreshListeners( NULL )
243 {
244 	pDocShell->GetDocument()->AddUnoObject(*this);
245 
246 	pEditSource = new ScCellEditSource( pDocShell, aCellPos );
247 }
248 
~ScCellFieldsObj()249 ScCellFieldsObj::~ScCellFieldsObj()
250 {
251     if (pDocShell)
252 		pDocShell->GetDocument()->RemoveUnoObject(*this);
253 
254 	delete pEditSource;
255 
256 	// increment refcount to prevent double call off dtor
257 	osl_incrementInterlockedCount( &m_refCount );
258 
259     if (mpRefreshListeners)
260 	{
261 		lang::EventObject aEvent;
262 		aEvent.Source.set(static_cast<cppu::OWeakObject*>(this));
263 		if (mpRefreshListeners)
264 		{
265 			mpRefreshListeners->disposeAndClear(aEvent);
266 			DELETEZ( mpRefreshListeners );
267 		}
268 	}
269 }
270 
Notify(SfxBroadcaster &,const SfxHint & rHint)271 void ScCellFieldsObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
272 {
273 	if ( rHint.ISA( ScUpdateRefHint ) )
274 	{
275 //        const ScUpdateRefHint& rRef = (const ScUpdateRefHint&)rHint;
276 
277 		//!	Ref-Update
278 	}
279 	else if ( rHint.ISA( SfxSimpleHint ) &&
280 			((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
281 	{
282 		pDocShell = NULL;		// ungueltig geworden
283 	}
284 
285 	//	EditSource hat sich selber als Listener angemeldet
286 }
287 
288 // XIndexAccess (via XTextFields)
289 
GetObjectByIndex_Impl(sal_Int32 Index) const290 ScCellFieldObj* ScCellFieldsObj::GetObjectByIndex_Impl(sal_Int32 Index) const
291 {
292 	//!	Feld-Funktionen muessen an den Forwarder !!!
293 	ScEditEngineDefaulter* pEditEngine = ((ScCellEditSource*)pEditSource)->GetEditEngine();
294 	ScUnoEditEngine aTempEngine(pEditEngine);
295 
296 	if ( aTempEngine.FindByIndex( (sal_uInt16)Index, NULL ) )	// in der Zelle ist der Typ egal
297 	{
298 		sal_uInt16 nPar = aTempEngine.GetFieldPar();
299 		xub_StrLen nPos = aTempEngine.GetFieldPos();
300 		ESelection aSelection( nPar, nPos, nPar, nPos+1 );		// Feld ist 1 Zeichen
301 		return new ScCellFieldObj( pDocShell, aCellPos, aSelection );
302 	}
303 	return NULL;
304 }
305 
getCount()306 sal_Int32 SAL_CALL ScCellFieldsObj::getCount() throw(uno::RuntimeException)
307 {
308 	ScUnoGuard aGuard;
309 
310 	//!	Feld-Funktionen muessen an den Forwarder !!!
311 	ScEditEngineDefaulter* pEditEngine = ((ScCellEditSource*)pEditSource)->GetEditEngine();
312 	ScUnoEditEngine aTempEngine(pEditEngine);
313 
314 	return aTempEngine.CountFields(NULL);		// Felder zaehlen, in Zelle ist der Typ egal
315 }
316 
getByIndex(sal_Int32 nIndex)317 uno::Any SAL_CALL ScCellFieldsObj::getByIndex( sal_Int32 nIndex )
318 							throw(lang::IndexOutOfBoundsException,
319 									lang::WrappedTargetException, uno::RuntimeException)
320 {
321 	ScUnoGuard aGuard;
322 	uno::Reference<text::XTextField> xField(GetObjectByIndex_Impl(nIndex));
323 	if (xField.is())
324         return uno::makeAny(xField);
325 	else
326 		throw lang::IndexOutOfBoundsException();
327 //    return uno::Any();
328 }
329 
getElementType()330 uno::Type SAL_CALL ScCellFieldsObj::getElementType() throw(uno::RuntimeException)
331 {
332 	ScUnoGuard aGuard;
333 	return getCppuType((uno::Reference<text::XTextField>*)0);
334 }
335 
hasElements()336 sal_Bool SAL_CALL ScCellFieldsObj::hasElements() throw(uno::RuntimeException)
337 {
338 	ScUnoGuard aGuard;
339 	return ( getCount() != 0 );
340 }
341 
createEnumeration()342 uno::Reference<container::XEnumeration> SAL_CALL ScCellFieldsObj::createEnumeration()
343 													throw(uno::RuntimeException)
344 {
345 	ScUnoGuard aGuard;
346 	return new ScIndexEnumeration(this, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextFieldEnumeration")));
347 }
348 
addContainerListener(const uno::Reference<container::XContainerListener> &)349 void SAL_CALL ScCellFieldsObj::addContainerListener(
350                                 const uno::Reference<container::XContainerListener>& /* xListener */ )
351 									throw(uno::RuntimeException)
352 {
353 	DBG_ERROR("not implemented");
354 }
355 
removeContainerListener(const uno::Reference<container::XContainerListener> &)356 void SAL_CALL ScCellFieldsObj::removeContainerListener(
357                                 const uno::Reference<container::XContainerListener>& /* xListener */ )
358 									throw(uno::RuntimeException)
359 {
360 	DBG_ERROR("not implemented");
361 }
362 
363 // XRefreshable
refresh()364 void SAL_CALL ScCellFieldsObj::refresh(  )
365                                     throw (uno::RuntimeException)
366 {
367 	if (mpRefreshListeners)
368 	{
369     	//  Call all listeners.
370 		uno::Sequence< uno::Reference< uno::XInterface > > aListeners(mpRefreshListeners->getElements());
371 		sal_uInt32 nLength(aListeners.getLength());
372 		if (nLength)
373 		{
374 			const uno::Reference< uno::XInterface >* pInterfaces = aListeners.getConstArray();
375 			if (pInterfaces)
376 			{
377                 lang::EventObject aEvent;
378                 aEvent.Source.set(uno::Reference< util::XRefreshable >(const_cast<ScCellFieldsObj*>(this)));
379 				sal_uInt32 i(0);
380 				while (i < nLength)
381 				{
382 					try
383 					{
384 						while(i < nLength)
385 						{
386 							static_cast< util::XRefreshListener* >(pInterfaces->get())->refreshed(aEvent);
387 							++pInterfaces;
388 							++i;
389 						}
390 					}
391 					catch(uno::RuntimeException&)
392 					{
393 //						DBG_ERROR("a object is gone without to remove from Broadcaster");
394 						++pInterfaces;
395 						++i;
396 					}
397 				}
398 			}
399 		}
400 	}
401 }
402 
addRefreshListener(const uno::Reference<util::XRefreshListener> & xListener)403 void SAL_CALL ScCellFieldsObj::addRefreshListener( const uno::Reference< util::XRefreshListener >& xListener )
404                                     throw (uno::RuntimeException)
405 {
406 	if (xListener.is())
407     {
408 		ScUnoGuard aGuard;
409 		if (!mpRefreshListeners)
410 			mpRefreshListeners = new cppu::OInterfaceContainerHelper(aMutex);
411 		mpRefreshListeners->addInterface(xListener);
412     }
413 }
414 
removeRefreshListener(const uno::Reference<util::XRefreshListener> & xListener)415 void SAL_CALL ScCellFieldsObj::removeRefreshListener( const uno::Reference<util::XRefreshListener >& xListener )
416                                     throw (uno::RuntimeException)
417 {
418 	if (xListener.is())
419 	{
420 		ScUnoGuard aGuard;
421 		if (mpRefreshListeners)
422 			mpRefreshListeners->removeInterface(xListener);
423 	}
424 }
425 
426 //------------------------------------------------------------------------
427 
428 //	Default-ctor wird fuer SMART_REFLECTION_IMPLEMENTATION gebraucht
429 
430 
431 //UNUSED2008-05  ScCellFieldObj::ScCellFieldObj() :
432 //UNUSED2008-05      OComponentHelper( getMutex() ),
433 //UNUSED2008-05      aPropSet( lcl_GetURLPropertyMap() ),
434 //UNUSED2008-05      pDocShell( NULL )
435 //UNUSED2008-05  {
436 //UNUSED2008-05      pEditSource = NULL;
437 //UNUSED2008-05  }
438 
ScCellFieldObj(ScDocShell * pDocSh,const ScAddress & rPos,const ESelection & rSel)439 ScCellFieldObj::ScCellFieldObj(ScDocShell* pDocSh, const ScAddress& rPos,
440 											const ESelection& rSel) :
441 	OComponentHelper( getMutex() ),
442 	pPropSet( lcl_GetURLPropertySet() ),
443 	pDocShell( pDocSh ),
444 	aCellPos( rPos ),
445 	aSelection( rSel )
446 {
447 	//	pDocShell ist Null, wenn per ServiceProvider erzeugt
448 
449 	if (pDocShell)
450 	{
451 		pDocShell->GetDocument()->AddUnoObject(*this);
452 		pEditSource = new ScCellEditSource( pDocShell, aCellPos );
453 	}
454 	else
455 		pEditSource = NULL;
456 }
457 
queryAggregation(const uno::Type & rType)458 uno::Any SAL_CALL ScCellFieldObj::queryAggregation( const uno::Type& rType )
459 												throw(uno::RuntimeException)
460 {
461 	SC_QUERYINTERFACE( text::XTextField )
462 	SC_QUERYINTERFACE( text::XTextContent )			// parent of XTextField
463 	SC_QUERYINTERFACE( beans::XPropertySet )
464 	SC_QUERYINTERFACE( lang::XUnoTunnel )
465 	SC_QUERYINTERFACE( lang::XServiceInfo )
466 
467 	return OComponentHelper::queryAggregation( rType );		// XComponent
468 }
469 
getTypes()470 uno::Sequence<uno::Type> SAL_CALL ScCellFieldObj::getTypes() throw(uno::RuntimeException)
471 {
472 	static uno::Sequence<uno::Type> aTypes;
473 	if ( aTypes.getLength() == 0 )
474 	{
475 		uno::Sequence<uno::Type> aParentTypes(OComponentHelper::getTypes());
476 		long nParentLen = aParentTypes.getLength();
477 		const uno::Type* pParentPtr = aParentTypes.getConstArray();
478 
479 		aTypes.realloc( nParentLen + 4 );
480 		uno::Type* pPtr = aTypes.getArray();
481 		pPtr[nParentLen + 0] = getCppuType((const uno::Reference<text::XTextField>*)0);
482 		pPtr[nParentLen + 1] = getCppuType((const uno::Reference<beans::XPropertySet>*)0);
483 		pPtr[nParentLen + 2] = getCppuType((const uno::Reference<lang::XUnoTunnel>*)0);
484 		pPtr[nParentLen + 3] = getCppuType((const uno::Reference<lang::XServiceInfo>*)0);
485 
486 		for (long i=0; i<nParentLen; i++)
487 			pPtr[i] = pParentPtr[i];				// parent types first
488 	}
489 	return aTypes;
490 }
491 
getImplementationId()492 uno::Sequence<sal_Int8> SAL_CALL ScCellFieldObj::getImplementationId()
493 													throw(uno::RuntimeException)
494 {
495 	static uno::Sequence< sal_Int8 > aId;
496 	if( aId.getLength() == 0 )
497 	{
498 		aId.realloc( 16 );
499 		rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
500 	}
501 	return aId;
502 }
503 
queryInterface(const uno::Type & rType)504 uno::Any SAL_CALL ScCellFieldObj::queryInterface( const uno::Type& rType )
505 												throw(uno::RuntimeException)
506 {
507 	return OComponentHelper::queryInterface( rType );
508 }
509 
acquire()510 void SAL_CALL ScCellFieldObj::acquire() throw()
511 {
512 	OComponentHelper::acquire();
513 }
514 
release()515 void SAL_CALL ScCellFieldObj::release() throw()
516 {
517 	OComponentHelper::release();
518 }
519 
InitDoc(ScDocShell * pDocSh,const ScAddress & rPos,const ESelection & rSel)520 void ScCellFieldObj::InitDoc( ScDocShell* pDocSh, const ScAddress& rPos,
521 										const ESelection& rSel )
522 {
523 	if ( pDocSh && !pEditSource )
524 	{
525 		aCellPos = rPos;
526 		aSelection = rSel;
527 		pDocShell = pDocSh;
528 
529 		pDocShell->GetDocument()->AddUnoObject(*this);
530 		pEditSource = new ScCellEditSource( pDocShell, aCellPos );
531 	}
532 }
533 
~ScCellFieldObj()534 ScCellFieldObj::~ScCellFieldObj()
535 {
536 	if (pDocShell)
537 		pDocShell->GetDocument()->RemoveUnoObject(*this);
538 
539 	delete pEditSource;
540 }
541 
Notify(SfxBroadcaster &,const SfxHint & rHint)542 void ScCellFieldObj::Notify( SfxBroadcaster&, const SfxHint& rHint )
543 {
544 	//!	Updates fuer aSelection (muessen erst noch erzeugt werden) !!!!!!
545 
546 	if ( rHint.ISA( ScUpdateRefHint ) )
547 	{
548 //        const ScUpdateRefHint& rRef = (const ScUpdateRefHint&)rHint;
549 
550 		//!	Ref-Update
551 	}
552 	else if ( rHint.ISA( SfxSimpleHint ) &&
553 			((const SfxSimpleHint&)rHint).GetId() == SFX_HINT_DYING )
554 	{
555 		pDocShell = NULL;		// ungueltig geworden
556 	}
557 
558 	//	EditSource hat sich selber als Listener angemeldet
559 }
560 
561 // per getImplementation gerufen:
562 
CreateFieldItem()563 SvxFieldItem ScCellFieldObj::CreateFieldItem()
564 {
565 	DBG_ASSERT( !pEditSource, "CreateFieldItem mit eingefuegtem Feld" );
566 
567 	SvxURLField aField;
568 	aField.SetFormat(SVXURLFORMAT_APPDEFAULT);
569 	aField.SetURL( aUrl );
570 	aField.SetRepresentation( aRepresentation );
571 	aField.SetTargetFrame( aTarget );
572     return SvxFieldItem( aField, EE_FEATURE_FIELD );
573 }
574 
DeleteField()575 void ScCellFieldObj::DeleteField()
576 {
577 	if (pEditSource)
578 	{
579 		SvxTextForwarder* pForwarder = pEditSource->GetTextForwarder();
580 //		pEditEngine->QuickDelete( aSelection );
581 		pForwarder->QuickInsertText( String(), aSelection );
582 		pEditSource->UpdateData();
583 
584 		aSelection.nEndPara = aSelection.nStartPara;
585 		aSelection.nEndPos  = aSelection.nStartPos;
586 
587 		//! Broadcast, um Selektion in anderen Objekten anzupassen
588 		//!	(auch bei anderen Aktionen)
589 	}
590 }
591 
592 // XTextField
593 
getPresentation(sal_Bool bShowCommand)594 rtl::OUString SAL_CALL ScCellFieldObj::getPresentation( sal_Bool bShowCommand )
595 													throw(uno::RuntimeException)
596 {
597 	ScUnoGuard aGuard;
598 	String aRet;
599 
600 	if (pEditSource)
601 	{
602 		//!	Feld-Funktionen muessen an den Forwarder !!!
603 		ScEditEngineDefaulter* pEditEngine = ((ScCellEditSource*)pEditSource)->GetEditEngine();
604 		ScUnoEditEngine aTempEngine(pEditEngine);
605 
606 		//	Typ egal (in Zellen gibts nur URLs)
607 		SvxFieldData* pField = aTempEngine.FindByPos( aSelection.nStartPara, aSelection.nStartPos, 0 );
608 		DBG_ASSERT(pField,"getPresentation: Feld nicht gefunden");
609 		if (pField)
610 		{
611 			SvxURLField* pURL = (SvxURLField*)pField;
612 			if (bShowCommand)
613 				aRet = pURL->GetURL();
614 			else
615 				aRet = pURL->GetRepresentation();
616 		}
617 	}
618 
619 	return aRet;
620 }
621 
622 // XTextContent
623 
attach(const uno::Reference<text::XTextRange> & xTextRange)624 void SAL_CALL ScCellFieldObj::attach( const uno::Reference<text::XTextRange>& xTextRange )
625 								throw(lang::IllegalArgumentException, uno::RuntimeException)
626 {
627 	ScUnoGuard aGuard;
628 	if (xTextRange.is())
629 	{
630 		uno::Reference<text::XText> xText(xTextRange->getText());
631 		if (xText.is())
632 		{
633 			xText->insertTextContent( xTextRange, this, sal_True );
634 		}
635 	}
636 }
637 
getAnchor()638 uno::Reference<text::XTextRange> SAL_CALL ScCellFieldObj::getAnchor() throw(uno::RuntimeException)
639 {
640 	ScUnoGuard aGuard;
641 	if (pDocShell)
642 		return new ScCellObj( pDocShell, aCellPos );
643 	return NULL;
644 }
645 
646 // XComponent
647 
dispose()648 void SAL_CALL ScCellFieldObj::dispose() throw(uno::RuntimeException)
649 {
650 	OComponentHelper::dispose();
651 }
652 
addEventListener(const uno::Reference<lang::XEventListener> & xListener)653 void SAL_CALL ScCellFieldObj::addEventListener(
654 						const uno::Reference<lang::XEventListener>& xListener )
655 													throw(uno::RuntimeException)
656 {
657 	OComponentHelper::addEventListener( xListener );
658 }
659 
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)660 void SAL_CALL ScCellFieldObj::removeEventListener(
661 						const uno::Reference<lang::XEventListener>& xListener )
662 													throw(uno::RuntimeException)
663 {
664 	OComponentHelper::removeEventListener( xListener );
665 }
666 
667 // XPropertySet
668 
getPropertySetInfo()669 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScCellFieldObj::getPropertySetInfo()
670 														throw(uno::RuntimeException)
671 {
672 	ScUnoGuard aGuard;
673 	static uno::Reference<beans::XPropertySetInfo> aRef = pPropSet->getPropertySetInfo();
674 	return aRef;
675 }
676 
setPropertyValue(const rtl::OUString & aPropertyName,const uno::Any & aValue)677 void SAL_CALL ScCellFieldObj::setPropertyValue(
678 						const rtl::OUString& aPropertyName, const uno::Any& aValue )
679 				throw(beans::UnknownPropertyException, beans::PropertyVetoException,
680 						lang::IllegalArgumentException, lang::WrappedTargetException,
681 						uno::RuntimeException)
682 {
683 	ScUnoGuard aGuard;
684 	String aNameString(aPropertyName);
685 	rtl::OUString aStrVal;
686 	if (pEditSource)
687 	{
688 		//!	Feld-Funktionen muessen an den Forwarder !!!
689 		ScEditEngineDefaulter* pEditEngine = ((ScCellEditSource*)pEditSource)->GetEditEngine();
690 		ScUnoEditEngine aTempEngine(pEditEngine);
691 
692 		//	Typ egal (in Zellen gibts nur URLs)
693 		SvxFieldData* pField = aTempEngine.FindByPos( aSelection.nStartPara, aSelection.nStartPos, 0 );
694 		DBG_ASSERT(pField,"setPropertyValue: Feld nicht gefunden");
695 		if (pField)
696 		{
697 			SvxURLField* pURL = (SvxURLField*)pField;	// ist eine Kopie in der ScUnoEditEngine
698 
699 			sal_Bool bOk = sal_True;
700 			if ( aNameString.EqualsAscii( SC_UNONAME_URL ) )
701 			{
702 				if (aValue >>= aStrVal)
703 					pURL->SetURL( aStrVal );
704 			}
705 			else if ( aNameString.EqualsAscii( SC_UNONAME_REPR ) )
706 			{
707 				if (aValue >>= aStrVal)
708 					pURL->SetRepresentation( aStrVal );
709 			}
710 			else if ( aNameString.EqualsAscii( SC_UNONAME_TARGET ) )
711 			{
712 				if (aValue >>= aStrVal)
713 					pURL->SetTargetFrame( aStrVal );
714 			}
715 			else
716 				bOk = sal_False;
717 
718 			if (bOk)
719 			{
720                 pEditEngine->QuickInsertField( SvxFieldItem(*pField, EE_FEATURE_FIELD), aSelection );
721 				pEditSource->UpdateData();
722 			}
723 		}
724 	}
725 	else		// noch nicht eingefuegt
726 	{
727 		if ( aNameString.EqualsAscii( SC_UNONAME_URL ) )
728 		{
729 			if (aValue >>= aStrVal)
730 				aUrl = String( aStrVal );
731 		}
732 		else if ( aNameString.EqualsAscii( SC_UNONAME_REPR ) )
733 		{
734 			if (aValue >>= aStrVal)
735 				aRepresentation = String( aStrVal );
736 		}
737 		else if ( aNameString.EqualsAscii( SC_UNONAME_TARGET ) )
738 		{
739 			if (aValue >>= aStrVal)
740 				aTarget = String( aStrVal );
741 		}
742 	}
743 }
744 
getPropertyValue(const rtl::OUString & aPropertyName)745 uno::Any SAL_CALL ScCellFieldObj::getPropertyValue( const rtl::OUString& aPropertyName )
746 				throw(beans::UnknownPropertyException, lang::WrappedTargetException,
747 						uno::RuntimeException)
748 {
749 	ScUnoGuard aGuard;
750 	uno::Any aRet;
751 	String aNameString(aPropertyName);
752 
753 	// anchor type is always "as character", text wrap always "none"
754 
755 	if ( aNameString.EqualsAscii( SC_UNONAME_ANCTYPE ) )
756 		aRet <<= text::TextContentAnchorType_AS_CHARACTER;
757 	else if ( aNameString.EqualsAscii( SC_UNONAME_ANCTYPES ) )
758 	{
759 		uno::Sequence<text::TextContentAnchorType> aSeq(1);
760 		aSeq[0] = text::TextContentAnchorType_AS_CHARACTER;
761 		aRet <<= aSeq;
762 	}
763 	else if ( aNameString.EqualsAscii( SC_UNONAME_TEXTWRAP ) )
764 		aRet <<= text::WrapTextMode_NONE;
765 	else if (pEditSource)
766 	{
767 		//!	Feld-Funktionen muessen an den Forwarder !!!
768 		ScEditEngineDefaulter* pEditEngine = ((ScCellEditSource*)pEditSource)->GetEditEngine();
769 		ScUnoEditEngine aTempEngine(pEditEngine);
770 
771 		//	Typ egal (in Zellen gibts nur URLs)
772 		SvxFieldData* pField = aTempEngine.FindByPos( aSelection.nStartPara, aSelection.nStartPos, 0 );
773 		DBG_ASSERT(pField,"getPropertyValue: Feld nicht gefunden");
774 		if (pField)
775 		{
776 			SvxURLField* pURL = (SvxURLField*)pField;
777 
778 			if ( aNameString.EqualsAscii( SC_UNONAME_URL ) )
779 				aRet <<= rtl::OUString( pURL->GetURL() );
780 			else if ( aNameString.EqualsAscii( SC_UNONAME_REPR ) )
781 				aRet <<= rtl::OUString( pURL->GetRepresentation() );
782 			else if ( aNameString.EqualsAscii( SC_UNONAME_TARGET ) )
783 				aRet <<= rtl::OUString( pURL->GetTargetFrame() );
784 		}
785 	}
786 	else		// noch nicht eingefuegt
787 	{
788 		if ( aNameString.EqualsAscii( SC_UNONAME_URL ) )
789 			aRet <<= rtl::OUString( aUrl );
790 		else if ( aNameString.EqualsAscii( SC_UNONAME_REPR ) )
791 			aRet <<= rtl::OUString( aRepresentation );
792 		else if ( aNameString.EqualsAscii( SC_UNONAME_TARGET ) )
793 			aRet <<= rtl::OUString( aTarget );
794 	}
795 	return aRet;
796 }
797 
SC_IMPL_DUMMY_PROPERTY_LISTENER(ScCellFieldObj)798 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScCellFieldObj )
799 
800 // XUnoTunnel
801 
802 sal_Int64 SAL_CALL ScCellFieldObj::getSomething(
803 				const uno::Sequence<sal_Int8 >& rId ) throw(uno::RuntimeException)
804 {
805 	if ( rId.getLength() == 16 &&
806           0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
807 									rId.getConstArray(), 16 ) )
808 	{
809         return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
810 	}
811 	return 0;
812 }
813 
814 // static
getUnoTunnelId()815 const uno::Sequence<sal_Int8>& ScCellFieldObj::getUnoTunnelId()
816 {
817 	static uno::Sequence<sal_Int8> * pSeq = 0;
818 	if( !pSeq )
819 	{
820 		osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
821 		if( !pSeq )
822 		{
823 			static uno::Sequence< sal_Int8 > aSeq( 16 );
824 			rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
825 			pSeq = &aSeq;
826 		}
827 	}
828 	return *pSeq;
829 }
830 
831 // static
getImplementation(const uno::Reference<text::XTextContent> xObj)832 ScCellFieldObj* ScCellFieldObj::getImplementation(
833 								const uno::Reference<text::XTextContent> xObj )
834 {
835 	ScCellFieldObj* pRet = NULL;
836 	uno::Reference<lang::XUnoTunnel> xUT( xObj, uno::UNO_QUERY );
837 	if (xUT.is())
838         pRet = reinterpret_cast<ScCellFieldObj*>(sal::static_int_cast<sal_IntPtr>(xUT->getSomething(getUnoTunnelId())));
839 	return pRet;
840 }
841 
842 // XServiceInfo
843 
getImplementationName()844 rtl::OUString SAL_CALL ScCellFieldObj::getImplementationName() throw(uno::RuntimeException)
845 {
846 	return rtl::OUString::createFromAscii( "ScCellFieldObj" );
847 }
848 
supportsService(const rtl::OUString & rServiceName)849 sal_Bool SAL_CALL ScCellFieldObj::supportsService( const rtl::OUString& rServiceName )
850 													throw(uno::RuntimeException)
851 {
852 	String aServiceStr( rServiceName );
853 	return aServiceStr.EqualsAscii( SCTEXTFIELD_SERVICE ) ||
854 		   aServiceStr.EqualsAscii( SCTEXTCONTENT_SERVICE );
855 }
856 
getSupportedServiceNames()857 uno::Sequence<rtl::OUString> SAL_CALL ScCellFieldObj::getSupportedServiceNames()
858 													throw(uno::RuntimeException)
859 {
860 	uno::Sequence<rtl::OUString> aRet(2);
861 	rtl::OUString* pArray = aRet.getArray();
862 	pArray[0] = rtl::OUString::createFromAscii( SCTEXTFIELD_SERVICE );
863 	pArray[1] = rtl::OUString::createFromAscii( SCTEXTCONTENT_SERVICE );
864 	return aRet;
865 }
866 
867 //------------------------------------------------------------------------
868 
ScHeaderFieldsObj(ScHeaderFooterContentObj * pContent,sal_uInt16 nP,sal_uInt16 nT)869 ScHeaderFieldsObj::ScHeaderFieldsObj(ScHeaderFooterContentObj* pContent, sal_uInt16 nP, sal_uInt16 nT) :
870 	pContentObj( pContent ),
871 	nPart( nP ),
872 	nType( nT ),
873     mpRefreshListeners( NULL )
874 {
875 	DBG_ASSERT( pContentObj, "ScHeaderFieldsObj ohne Objekt?" );
876 
877 	if (pContentObj)
878 	{
879 		pContentObj->acquire();		// darf nicht wegkommen
880 		pEditSource = new ScHeaderFooterEditSource( pContentObj, nPart );
881 	}
882 	else
883 		pEditSource = NULL;
884 }
885 
~ScHeaderFieldsObj()886 ScHeaderFieldsObj::~ScHeaderFieldsObj()
887 {
888     delete pEditSource;
889 
890 	if (pContentObj)
891 		pContentObj->release();
892 
893 	// increment refcount to prevent double call off dtor
894 	osl_incrementInterlockedCount( &m_refCount );
895 
896     if (mpRefreshListeners)
897 	{
898 		lang::EventObject aEvent;
899 		aEvent.Source = static_cast<cppu::OWeakObject*>(this);
900 		if (mpRefreshListeners)
901 		{
902 			mpRefreshListeners->disposeAndClear(aEvent);
903 			DELETEZ( mpRefreshListeners );
904 		}
905 	}
906 }
907 
908 // XIndexAccess (via XTextFields)
909 
GetObjectByIndex_Impl(sal_Int32 Index) const910 ScHeaderFieldObj* ScHeaderFieldsObj::GetObjectByIndex_Impl(sal_Int32 Index) const
911 {
912 	//!	Feld-Funktionen muessen an den Forwarder !!!
913 	ScEditEngineDefaulter* pEditEngine = ((ScHeaderFooterEditSource*)pEditSource)->GetEditEngine();
914 	ScUnoEditEngine aTempEngine(pEditEngine);
915 
916 	TypeId aTypeId = NULL;
917 	switch (nType)
918 	{
919 		case SC_SERVICE_PAGEFIELD:	aTypeId = TYPE(SvxPageField);	 break;
920 		case SC_SERVICE_PAGESFIELD:	aTypeId = TYPE(SvxPagesField);	 break;
921 		case SC_SERVICE_DATEFIELD:	aTypeId = TYPE(SvxDateField);	 break;
922 		case SC_SERVICE_TIMEFIELD:	aTypeId = TYPE(SvxTimeField);	 break;
923 		case SC_SERVICE_TITLEFIELD:	aTypeId = TYPE(SvxFileField);	 break;
924 		case SC_SERVICE_FILEFIELD:	aTypeId = TYPE(SvxExtFileField); break;
925 		case SC_SERVICE_SHEETFIELD:	aTypeId = TYPE(SvxTableField);	 break;
926 		// bei SC_SERVICE_INVALID bleibt TypeId Null
927 	}
928 	SvxFieldData* pData = aTempEngine.FindByIndex( (sal_uInt16)Index, aTypeId );
929 	if ( pData )
930 	{
931 		sal_uInt16 nPar = aTempEngine.GetFieldPar();
932 		xub_StrLen nPos = aTempEngine.GetFieldPos();
933 
934 		sal_uInt16 nFieldType = nType;
935 		if ( nFieldType == SC_SERVICE_INVALID )
936 		{
937 			if ( pData->ISA( SvxPageField ) )		  nFieldType = SC_SERVICE_PAGEFIELD;
938 			else if ( pData->ISA( SvxPagesField ) )	  nFieldType = SC_SERVICE_PAGESFIELD;
939 			else if ( pData->ISA( SvxDateField ) )	  nFieldType = SC_SERVICE_DATEFIELD;
940 			else if ( pData->ISA( SvxTimeField ) )	  nFieldType = SC_SERVICE_TIMEFIELD;
941 			else if ( pData->ISA( SvxFileField ) )	  nFieldType = SC_SERVICE_TITLEFIELD;
942 			else if ( pData->ISA( SvxExtFileField ) ) nFieldType = SC_SERVICE_FILEFIELD;
943 			else if ( pData->ISA( SvxTableField ) )	  nFieldType = SC_SERVICE_SHEETFIELD;
944 		}
945 
946 		ESelection aSelection( nPar, nPos, nPar, nPos+1 );		// Field is 1 character
947 		return new ScHeaderFieldObj( pContentObj, nPart, nFieldType, aSelection );
948 	}
949 	return NULL;
950 }
951 
getCount()952 sal_Int32 SAL_CALL ScHeaderFieldsObj::getCount() throw(uno::RuntimeException)
953 {
954 	ScUnoGuard aGuard;
955 
956 	//!	Feld-Funktionen muessen an den Forwarder !!!
957 	ScEditEngineDefaulter* pEditEngine = ((ScHeaderFooterEditSource*)pEditSource)->GetEditEngine();
958 	ScUnoEditEngine aTempEngine(pEditEngine);
959 
960 	TypeId aTypeId = NULL;
961 	switch (nType)
962 	{
963 		case SC_SERVICE_PAGEFIELD:	aTypeId = TYPE(SvxPageField);	 break;
964 		case SC_SERVICE_PAGESFIELD:	aTypeId = TYPE(SvxPagesField);	 break;
965 		case SC_SERVICE_DATEFIELD:	aTypeId = TYPE(SvxDateField);	 break;
966 		case SC_SERVICE_TIMEFIELD:	aTypeId = TYPE(SvxTimeField);	 break;
967 		case SC_SERVICE_TITLEFIELD:	aTypeId = TYPE(SvxFileField);	 break;
968 		case SC_SERVICE_FILEFIELD:	aTypeId = TYPE(SvxExtFileField); break;
969 		case SC_SERVICE_SHEETFIELD:	aTypeId = TYPE(SvxTableField);	 break;
970 	}
971 	return aTempEngine.CountFields(aTypeId);		// Felder zaehlen
972 }
973 
getByIndex(sal_Int32 nIndex)974 uno::Any SAL_CALL ScHeaderFieldsObj::getByIndex( sal_Int32 nIndex )
975 							throw(lang::IndexOutOfBoundsException,
976 									lang::WrappedTargetException, uno::RuntimeException)
977 {
978 	ScUnoGuard aGuard;
979 	uno::Reference<text::XTextField> xField(GetObjectByIndex_Impl(nIndex));
980 	if (xField.is())
981         return uno::makeAny(xField);
982 	else
983 		throw lang::IndexOutOfBoundsException();
984 //    return uno::Any();
985 }
986 
getElementType()987 uno::Type SAL_CALL ScHeaderFieldsObj::getElementType() throw(uno::RuntimeException)
988 {
989 	ScUnoGuard aGuard;
990 	return getCppuType((uno::Reference<text::XTextField>*)0);
991 }
992 
hasElements()993 sal_Bool SAL_CALL ScHeaderFieldsObj::hasElements() throw(uno::RuntimeException)
994 {
995 	ScUnoGuard aGuard;
996 	return ( getCount() != 0 );
997 }
998 
createEnumeration()999 uno::Reference<container::XEnumeration> SAL_CALL ScHeaderFieldsObj::createEnumeration()
1000 													throw(uno::RuntimeException)
1001 {
1002 	ScUnoGuard aGuard;
1003     return new ScIndexEnumeration(this, rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.text.TextFieldEnumeration")));
1004 }
1005 
addContainerListener(const uno::Reference<container::XContainerListener> &)1006 void SAL_CALL ScHeaderFieldsObj::addContainerListener(
1007                                 const uno::Reference<container::XContainerListener>& /* xListener */ )
1008 									throw(uno::RuntimeException)
1009 {
1010 	DBG_ERROR("not implemented");
1011 }
1012 
removeContainerListener(const uno::Reference<container::XContainerListener> &)1013 void SAL_CALL ScHeaderFieldsObj::removeContainerListener(
1014                                 const uno::Reference<container::XContainerListener>& /* xListener */ )
1015 									throw(uno::RuntimeException)
1016 {
1017 	DBG_ERROR("not implemented");
1018 }
1019 
1020 // XRefreshable
refresh()1021 void SAL_CALL ScHeaderFieldsObj::refresh(  )
1022                                     throw (uno::RuntimeException)
1023 {
1024 	if (mpRefreshListeners)
1025 	{
1026     	//  Call all listeners.
1027 		uno::Sequence< uno::Reference< uno::XInterface > > aListeners(mpRefreshListeners->getElements());
1028 		sal_uInt32 nLength(aListeners.getLength());
1029 		if (nLength)
1030 		{
1031 			const uno::Reference< uno::XInterface >* pInterfaces = aListeners.getConstArray();
1032 			if (pInterfaces)
1033 			{
1034                 lang::EventObject aEvent;
1035                 aEvent.Source.set(uno::Reference< util::XRefreshable >(const_cast<ScHeaderFieldsObj*>(this)));
1036 				sal_uInt32 i(0);
1037 				while (i < nLength)
1038 				{
1039 					try
1040 					{
1041 						while(i < nLength)
1042 						{
1043 							static_cast< util::XRefreshListener* >(pInterfaces->get())->refreshed(aEvent);
1044 							++pInterfaces;
1045 							++i;
1046 						}
1047 					}
1048 					catch(uno::RuntimeException&)
1049 					{
1050 //						DBG_ERROR("a object is gone without to remove from Broadcaster");
1051 						++pInterfaces;
1052 						++i;
1053 					}
1054 				}
1055 			}
1056 		}
1057 	}
1058 }
1059 
addRefreshListener(const uno::Reference<util::XRefreshListener> & xListener)1060 void SAL_CALL ScHeaderFieldsObj::addRefreshListener( const uno::Reference< util::XRefreshListener >& xListener )
1061                                     throw (uno::RuntimeException)
1062 {
1063 	if (xListener.is())
1064     {
1065 		ScUnoGuard aGuard;
1066 		if (!mpRefreshListeners)
1067 			mpRefreshListeners = new cppu::OInterfaceContainerHelper(aMutex);
1068 		mpRefreshListeners->addInterface(xListener);
1069     }
1070 }
1071 
removeRefreshListener(const uno::Reference<util::XRefreshListener> & xListener)1072 void SAL_CALL ScHeaderFieldsObj::removeRefreshListener( const uno::Reference<util::XRefreshListener >& xListener )
1073                                     throw (uno::RuntimeException)
1074 {
1075 	if (xListener.is())
1076 	{
1077 		ScUnoGuard aGuard;
1078 		if (mpRefreshListeners)
1079 			mpRefreshListeners->removeInterface(xListener);
1080 	}
1081 }
1082 
1083 //------------------------------------------------------------------------
1084 
lcl_UnoToSvxFileFormat(sal_Int16 nUnoValue)1085 SvxFileFormat lcl_UnoToSvxFileFormat( sal_Int16 nUnoValue )
1086 {
1087 	switch( nUnoValue )
1088 	{
1089 		case text::FilenameDisplayFormat::FULL:	return SVXFILEFORMAT_FULLPATH;
1090 		case text::FilenameDisplayFormat::PATH:	return SVXFILEFORMAT_PATH;
1091 		case text::FilenameDisplayFormat::NAME:	return SVXFILEFORMAT_NAME;
1092 //		case text::FilenameDisplayFormat::NAME_AND_EXT:
1093 		default:
1094 			return SVXFILEFORMAT_NAME_EXT;
1095 	}
1096 }
1097 
lcl_SvxToUnoFileFormat(SvxFileFormat nSvxValue)1098 sal_Int16 lcl_SvxToUnoFileFormat( SvxFileFormat nSvxValue )
1099 {
1100 	switch( nSvxValue )
1101 	{
1102 		case SVXFILEFORMAT_NAME_EXT:	return text::FilenameDisplayFormat::NAME_AND_EXT;
1103 		case SVXFILEFORMAT_FULLPATH:	return text::FilenameDisplayFormat::FULL;
1104 		case SVXFILEFORMAT_PATH:		return text::FilenameDisplayFormat::PATH;
1105 //		case SVXFILEFORMAT_NAME:
1106 		default:
1107 			return text::FilenameDisplayFormat::NAME;
1108 	}
1109 }
1110 
1111 
1112 //	Default-ctor wird fuer SMART_REFLECTION_IMPLEMENTATION gebraucht
1113 //UNUSED2008-05  ScHeaderFieldObj::ScHeaderFieldObj() :
1114 //UNUSED2008-05      OComponentHelper( getMutex() ),
1115 //UNUSED2008-05      aPropSet( lcl_GetHeaderFieldPropertyMap() ),
1116 //UNUSED2008-05      pContentObj( NULL ),
1117 //UNUSED2008-05      nPart( 0 ),
1118 //UNUSED2008-05      nType( 0 ),
1119 //UNUSED2008-05      nFileFormat( SVXFILEFORMAT_NAME_EXT )
1120 //UNUSED2008-05  {
1121 //UNUSED2008-05      pEditSource = NULL;
1122 //UNUSED2008-05  }
1123 
ScHeaderFieldObj(ScHeaderFooterContentObj * pContent,sal_uInt16 nP,sal_uInt16 nT,const ESelection & rSel)1124 ScHeaderFieldObj::ScHeaderFieldObj(ScHeaderFooterContentObj* pContent, sal_uInt16 nP,
1125 											sal_uInt16 nT, const ESelection& rSel) :
1126 	OComponentHelper( getMutex() ),
1127 	pPropSet( (nT == SC_SERVICE_FILEFIELD) ? lcl_GetFileFieldPropertySet() : lcl_GetHeaderFieldPropertySet() ),
1128 	pContentObj( pContent ),
1129 	nPart( nP ),
1130 	nType( nT ),
1131 	aSelection( rSel ),
1132 	nFileFormat( SVXFILEFORMAT_NAME_EXT )
1133 {
1134 	//	pContent ist Null, wenn per ServiceProvider erzeugt
1135 
1136 	if (pContentObj)
1137 	{
1138 		pContentObj->acquire();		// darf nicht wegkommen
1139 		pEditSource = new ScHeaderFooterEditSource( pContentObj, nPart );
1140 	}
1141 	else
1142 		pEditSource = NULL;
1143 }
1144 
queryAggregation(const uno::Type & rType)1145 uno::Any SAL_CALL ScHeaderFieldObj::queryAggregation( const uno::Type& rType )
1146 												throw(uno::RuntimeException)
1147 {
1148 	SC_QUERYINTERFACE( text::XTextField )
1149 	SC_QUERYINTERFACE( text::XTextContent )			// parent of XTextField
1150 	SC_QUERYINTERFACE( beans::XPropertySet )
1151 	SC_QUERYINTERFACE( lang::XUnoTunnel )
1152 	SC_QUERYINTERFACE( lang::XServiceInfo )
1153 
1154 	return OComponentHelper::queryAggregation( rType );		// XComponent
1155 }
1156 
getTypes()1157 uno::Sequence<uno::Type> SAL_CALL ScHeaderFieldObj::getTypes() throw(uno::RuntimeException)
1158 {
1159 	static uno::Sequence<uno::Type> aTypes;
1160 	if ( aTypes.getLength() == 0 )
1161 	{
1162 		uno::Sequence<uno::Type> aParentTypes(OComponentHelper::getTypes());
1163 		long nParentLen = aParentTypes.getLength();
1164 		const uno::Type* pParentPtr = aParentTypes.getConstArray();
1165 
1166 		aTypes.realloc( nParentLen + 4 );
1167 		uno::Type* pPtr = aTypes.getArray();
1168 		pPtr[nParentLen + 0] = getCppuType((const uno::Reference<text::XTextField>*)0);
1169 		pPtr[nParentLen + 1] = getCppuType((const uno::Reference<beans::XPropertySet>*)0);
1170 		pPtr[nParentLen + 2] = getCppuType((const uno::Reference<lang::XUnoTunnel>*)0);
1171 		pPtr[nParentLen + 3] = getCppuType((const uno::Reference<lang::XServiceInfo>*)0);
1172 
1173 		for (long i=0; i<nParentLen; i++)
1174 			pPtr[i] = pParentPtr[i];				// parent types first
1175 	}
1176 	return aTypes;
1177 }
1178 
getImplementationId()1179 uno::Sequence<sal_Int8> SAL_CALL ScHeaderFieldObj::getImplementationId()
1180 													throw(uno::RuntimeException)
1181 {
1182 	static uno::Sequence< sal_Int8 > aId;
1183 	if( aId.getLength() == 0 )
1184 	{
1185 		aId.realloc( 16 );
1186 		rtl_createUuid( (sal_uInt8 *)aId.getArray(), 0, sal_True );
1187 	}
1188 	return aId;
1189 }
1190 
queryInterface(const uno::Type & rType)1191 uno::Any SAL_CALL ScHeaderFieldObj::queryInterface( const uno::Type& rType )
1192 												throw(uno::RuntimeException)
1193 {
1194 	return OComponentHelper::queryInterface( rType );
1195 }
1196 
acquire()1197 void SAL_CALL ScHeaderFieldObj::acquire() throw()
1198 {
1199 	OComponentHelper::acquire();
1200 }
1201 
release()1202 void SAL_CALL ScHeaderFieldObj::release() throw()
1203 {
1204 	OComponentHelper::release();
1205 }
1206 
InitDoc(ScHeaderFooterContentObj * pContent,sal_uInt16 nP,const ESelection & rSel)1207 void ScHeaderFieldObj::InitDoc( ScHeaderFooterContentObj* pContent, sal_uInt16 nP,
1208 										const ESelection& rSel )
1209 {
1210 	if ( pContent && !pEditSource )
1211 	{
1212 		DBG_ASSERT( !pContentObj, "ContentObj, aber kein EditSource?" );
1213 
1214 		aSelection = rSel;
1215 		nPart = nP;
1216 		pContentObj = pContent;
1217 
1218 		pContentObj->acquire();		// darf nicht wegkommen
1219 		pEditSource = new ScHeaderFooterEditSource( pContentObj, nPart );
1220 	}
1221 }
1222 
~ScHeaderFieldObj()1223 ScHeaderFieldObj::~ScHeaderFieldObj()
1224 {
1225 	delete pEditSource;
1226 
1227 	if (pContentObj)
1228 		pContentObj->release();
1229 }
1230 
1231 // per getImplementation gerufen:
1232 
CreateFieldItem()1233 SvxFieldItem ScHeaderFieldObj::CreateFieldItem()
1234 {
1235 	DBG_ASSERT( !pEditSource, "CreateFieldItem mit eingefuegtem Feld" );
1236 
1237 	switch (nType)
1238 	{
1239 		case SC_SERVICE_PAGEFIELD:
1240 			{
1241 				SvxPageField aField;
1242                 return SvxFieldItem( aField, EE_FEATURE_FIELD );
1243 			}
1244 		case SC_SERVICE_PAGESFIELD:
1245 			{
1246 				SvxPagesField aField;
1247                 return SvxFieldItem( aField, EE_FEATURE_FIELD );
1248 			}
1249 		case SC_SERVICE_DATEFIELD:
1250 			{
1251 				SvxDateField aField;
1252                 return SvxFieldItem( aField, EE_FEATURE_FIELD );
1253 			}
1254 		case SC_SERVICE_TIMEFIELD:
1255 			{
1256 				SvxTimeField aField;
1257                 return SvxFieldItem( aField, EE_FEATURE_FIELD );
1258 			}
1259 		case SC_SERVICE_TITLEFIELD:
1260 			{
1261 				SvxFileField aField;
1262                 return SvxFieldItem( aField, EE_FEATURE_FIELD );
1263 			}
1264 		case SC_SERVICE_FILEFIELD:
1265 			{
1266 				SvxExtFileField aField;
1267 				aField.SetFormat( (SvxFileFormat) nFileFormat );
1268                 return SvxFieldItem( aField, EE_FEATURE_FIELD );
1269 			}
1270 		case SC_SERVICE_SHEETFIELD:
1271 			{
1272 				SvxTableField aField;
1273                 return SvxFieldItem( aField, EE_FEATURE_FIELD );
1274 			}
1275 	}
1276 
1277     return SvxFieldItem( SvxFieldData(), EE_FEATURE_FIELD );
1278 }
1279 
DeleteField()1280 void ScHeaderFieldObj::DeleteField()
1281 {
1282 	if (pEditSource)
1283 	{
1284 		SvxTextForwarder* pForwarder = pEditSource->GetTextForwarder();
1285 //		pEditEngine->QuickDelete( aSelection );
1286 		pForwarder->QuickInsertText( String(), aSelection );
1287 		pEditSource->UpdateData();
1288 
1289 		aSelection.nEndPara = aSelection.nStartPara;
1290 		aSelection.nEndPos  = aSelection.nStartPos;
1291 
1292 		//! Broadcast, um Selektion in anderen Objekten anzupassen
1293 		//!	(auch bei anderen Aktionen)
1294 	}
1295 }
1296 
1297 // XTextField
1298 
getPresentation(sal_Bool)1299 rtl::OUString SAL_CALL ScHeaderFieldObj::getPresentation( sal_Bool /* bShowCommand */ )
1300 													throw(uno::RuntimeException)
1301 {
1302 	ScUnoGuard aGuard;
1303 	String aRet;
1304 
1305 	if (pEditSource)
1306 	{
1307 		// Feld von der EditEngine formatieren lassen, bShowCommand gibt's nicht
1308 
1309 		SvxTextForwarder* pForwarder = pEditSource->GetTextForwarder();
1310 		aRet = pForwarder->GetText( aSelection );
1311 	}
1312 
1313 	return aRet;
1314 }
1315 
1316 // XTextContent
1317 
attach(const uno::Reference<text::XTextRange> & xTextRange)1318 void SAL_CALL ScHeaderFieldObj::attach( const uno::Reference<text::XTextRange>& xTextRange )
1319 								throw(lang::IllegalArgumentException, uno::RuntimeException)
1320 {
1321 	ScUnoGuard aGuard;
1322 	if (xTextRange.is())
1323 	{
1324 		uno::Reference<text::XText> xText(xTextRange->getText());
1325 		if (xText.is())
1326 		{
1327 			xText->insertTextContent( xTextRange, this, sal_True );
1328 		}
1329 	}
1330 }
1331 
getAnchor()1332 uno::Reference<text::XTextRange> SAL_CALL ScHeaderFieldObj::getAnchor() throw(uno::RuntimeException)
1333 {
1334 	ScUnoGuard aGuard;
1335 	if (pContentObj)
1336 	{
1337 		uno::Reference<text::XText> xText;
1338 		if ( nPart == SC_HDFT_LEFT )
1339 			xText = pContentObj->getLeftText();
1340 		else if (nPart == SC_HDFT_CENTER)
1341 			xText = pContentObj->getCenterText();
1342 		else
1343 			xText = pContentObj->getRightText();
1344 		return uno::Reference<text::XTextRange>( xText, uno::UNO_QUERY );
1345 	}
1346 	return NULL;
1347 }
1348 
1349 // XComponent
1350 
dispose()1351 void SAL_CALL ScHeaderFieldObj::dispose() throw(uno::RuntimeException)
1352 {
1353 	OComponentHelper::dispose();
1354 }
1355 
addEventListener(const uno::Reference<lang::XEventListener> & xListener)1356 void SAL_CALL ScHeaderFieldObj::addEventListener(
1357 						const uno::Reference<lang::XEventListener>& xListener )
1358 													throw(uno::RuntimeException)
1359 {
1360 	OComponentHelper::addEventListener( xListener );
1361 }
1362 
removeEventListener(const uno::Reference<lang::XEventListener> & xListener)1363 void SAL_CALL ScHeaderFieldObj::removeEventListener(
1364 						const uno::Reference<lang::XEventListener>& xListener )
1365 													throw(uno::RuntimeException)
1366 {
1367 	OComponentHelper::removeEventListener( xListener );
1368 }
1369 
1370 // XPropertySet
1371 
getPropertySetInfo()1372 uno::Reference<beans::XPropertySetInfo> SAL_CALL ScHeaderFieldObj::getPropertySetInfo()
1373 														throw(uno::RuntimeException)
1374 {
1375 	ScUnoGuard aGuard;
1376 	if (nType == SC_SERVICE_FILEFIELD)
1377 	{
1378 		//	file field has different properties
1379 		static uno::Reference<beans::XPropertySetInfo> aFileFieldInfo = pPropSet->getPropertySetInfo();
1380 		return aFileFieldInfo;
1381 	}
1382 	else
1383 	{
1384 		static uno::Reference<beans::XPropertySetInfo> aRef = pPropSet->getPropertySetInfo();
1385 		return aRef;
1386 	}
1387 }
1388 
setPropertyValue(const rtl::OUString & aPropertyName,const uno::Any & aValue)1389 void SAL_CALL ScHeaderFieldObj::setPropertyValue(
1390 						const rtl::OUString& aPropertyName, const uno::Any& aValue )
1391 				throw(beans::UnknownPropertyException, beans::PropertyVetoException,
1392 						lang::IllegalArgumentException, lang::WrappedTargetException,
1393 						uno::RuntimeException)
1394 {
1395 	ScUnoGuard aGuard;
1396 	String aNameString(aPropertyName);
1397 	if ( nType == SC_SERVICE_FILEFIELD && aNameString.EqualsAscii( SC_UNONAME_FILEFORM ) )
1398 	{
1399 		sal_Int16 nIntVal = 0;
1400 		if ( aValue >>= nIntVal )
1401 		{
1402 			SvxFileFormat eFormat = lcl_UnoToSvxFileFormat( nIntVal );
1403 			if (pEditSource)
1404 			{
1405 				ScEditEngineDefaulter* pEditEngine = ((ScHeaderFooterEditSource*)pEditSource)->GetEditEngine();
1406 				ScUnoEditEngine aTempEngine(pEditEngine);
1407 				SvxFieldData* pField = aTempEngine.FindByPos(
1408 						aSelection.nStartPara, aSelection.nStartPos, TYPE(SvxExtFileField) );
1409 				DBG_ASSERT(pField,"setPropertyValue: Field not found");
1410 				if (pField)
1411 				{
1412 					SvxExtFileField* pExtFile = (SvxExtFileField*)pField;	// local to the ScUnoEditEngine
1413 					pExtFile->SetFormat( eFormat );
1414                     pEditEngine->QuickInsertField( SvxFieldItem(*pField, EE_FEATURE_FIELD), aSelection );
1415 					pEditSource->UpdateData();
1416 				}
1417 			}
1418 			else
1419                 nFileFormat = sal::static_int_cast<sal_Int16>(eFormat);     // not inserted yet - store value
1420 		}
1421 	}
1422 }
1423 
getPropertyValue(const rtl::OUString & aPropertyName)1424 uno::Any SAL_CALL ScHeaderFieldObj::getPropertyValue( const rtl::OUString& aPropertyName )
1425 				throw(beans::UnknownPropertyException, lang::WrappedTargetException,
1426 						uno::RuntimeException)
1427 {
1428 	ScUnoGuard aGuard;
1429 
1430 	//!	Properties?
1431 	uno::Any aRet;
1432 	String aNameString(aPropertyName);
1433 
1434 	// anchor type is always "as character", text wrap always "none"
1435 
1436 	if ( aNameString.EqualsAscii( SC_UNONAME_ANCTYPE ) )
1437 		aRet <<= text::TextContentAnchorType_AS_CHARACTER;
1438 	else if ( aNameString.EqualsAscii( SC_UNONAME_ANCTYPES ) )
1439 	{
1440 		uno::Sequence<text::TextContentAnchorType> aSeq(1);
1441 		aSeq[0] = text::TextContentAnchorType_AS_CHARACTER;
1442 		aRet <<= aSeq;
1443 	}
1444 	else if ( aNameString.EqualsAscii( SC_UNONAME_TEXTWRAP ) )
1445 		aRet <<= text::WrapTextMode_NONE;
1446 	else if ( nType == SC_SERVICE_FILEFIELD && aNameString.EqualsAscii( SC_UNONAME_FILEFORM ) )
1447 	{
1448 		SvxFileFormat eFormat = SVXFILEFORMAT_NAME_EXT;
1449 		if (pEditSource)
1450 		{
1451 			ScEditEngineDefaulter* pEditEngine = ((ScHeaderFooterEditSource*)pEditSource)->GetEditEngine();
1452 			ScUnoEditEngine aTempEngine(pEditEngine);
1453 			SvxFieldData* pField = aTempEngine.FindByPos(
1454 					aSelection.nStartPara, aSelection.nStartPos, TYPE(SvxExtFileField) );
1455 			DBG_ASSERT(pField,"setPropertyValue: Field not found");
1456 			if (pField)
1457 			{
1458 				const SvxExtFileField* pExtFile = (const SvxExtFileField*)pField;
1459 				eFormat = pExtFile->GetFormat();
1460 			}
1461 		}
1462 		else
1463 			eFormat = (SvxFileFormat) nFileFormat;		// not inserted yet - use stored value
1464 
1465 		sal_Int16 nIntVal = lcl_SvxToUnoFileFormat( eFormat );
1466 		aRet <<= nIntVal;
1467 	}
1468 
1469 	return aRet;
1470 }
1471 
SC_IMPL_DUMMY_PROPERTY_LISTENER(ScHeaderFieldObj)1472 SC_IMPL_DUMMY_PROPERTY_LISTENER( ScHeaderFieldObj )
1473 
1474 // XUnoTunnel
1475 
1476 sal_Int64 SAL_CALL ScHeaderFieldObj::getSomething(
1477 				const uno::Sequence<sal_Int8 >& rId ) throw(uno::RuntimeException)
1478 {
1479 	if ( rId.getLength() == 16 &&
1480           0 == rtl_compareMemory( getUnoTunnelId().getConstArray(),
1481 									rId.getConstArray(), 16 ) )
1482 	{
1483         return sal::static_int_cast<sal_Int64>(reinterpret_cast<sal_IntPtr>(this));
1484 	}
1485 	return 0;
1486 }
1487 
1488 // static
getUnoTunnelId()1489 const uno::Sequence<sal_Int8>& ScHeaderFieldObj::getUnoTunnelId()
1490 {
1491 	static uno::Sequence<sal_Int8> * pSeq = 0;
1492 	if( !pSeq )
1493 	{
1494 		osl::Guard< osl::Mutex > aGuard( osl::Mutex::getGlobalMutex() );
1495 		if( !pSeq )
1496 		{
1497 			static uno::Sequence< sal_Int8 > aSeq( 16 );
1498 			rtl_createUuid( (sal_uInt8*)aSeq.getArray(), 0, sal_True );
1499 			pSeq = &aSeq;
1500 		}
1501 	}
1502 	return *pSeq;
1503 }
1504 
1505 // static
getImplementation(const uno::Reference<text::XTextContent> xObj)1506 ScHeaderFieldObj* ScHeaderFieldObj::getImplementation(
1507 								const uno::Reference<text::XTextContent> xObj )
1508 {
1509 	ScHeaderFieldObj* pRet = NULL;
1510 	uno::Reference<lang::XUnoTunnel> xUT( xObj, uno::UNO_QUERY );
1511 	if (xUT.is())
1512         pRet = reinterpret_cast<ScHeaderFieldObj*>(sal::static_int_cast<sal_IntPtr>(xUT->getSomething(getUnoTunnelId())));
1513 	return pRet;
1514 }
1515 
1516 // XServiceInfo
1517 
getImplementationName()1518 rtl::OUString SAL_CALL ScHeaderFieldObj::getImplementationName() throw(uno::RuntimeException)
1519 {
1520 	return rtl::OUString::createFromAscii( "ScHeaderFieldObj" );
1521 }
1522 
supportsService(const rtl::OUString & rServiceName)1523 sal_Bool SAL_CALL ScHeaderFieldObj::supportsService( const rtl::OUString& rServiceName )
1524 													throw(uno::RuntimeException)
1525 {
1526 	String aServiceStr( rServiceName );
1527 	return aServiceStr.EqualsAscii( SCTEXTFIELD_SERVICE ) ||
1528 		   aServiceStr.EqualsAscii( SCTEXTCONTENT_SERVICE );
1529 }
1530 
getSupportedServiceNames()1531 uno::Sequence<rtl::OUString> SAL_CALL ScHeaderFieldObj::getSupportedServiceNames()
1532 													throw(uno::RuntimeException)
1533 {
1534 	uno::Sequence<rtl::OUString> aRet(2);
1535 	rtl::OUString* pArray = aRet.getArray();
1536 	pArray[0] = rtl::OUString::createFromAscii( SCTEXTFIELD_SERVICE );
1537 	pArray[1] = rtl::OUString::createFromAscii( SCTEXTCONTENT_SERVICE );
1538 	return aRet;
1539 }
1540 
1541 //------------------------------------------------------------------------
1542 
1543 
1544 
1545 
1546