xref: /aoo41x/main/svx/source/svdraw/svdetc.cxx (revision 70d3707a)
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_svx.hxx"
26 #include "editeng/forbiddencharacterstable.hxx"
27 #include <com/sun/star/embed/XEmbeddedObject.hpp>
28 #include <com/sun/star/embed/EmbedStates.hpp>
29 #include <svx/svdetc.hxx>
30 #include "svx/svditext.hxx"
31 #include <svx/svdmodel.hxx>
32 #include <svx/svdtrans.hxx>
33 #include "svx/svdglob.hxx"
34 #include "svx/svdstr.hrc"
35 #include "svx/svdviter.hxx"
36 #include <svx/svdview.hxx>
37 #include <svx/svdoutl.hxx>
38 #include <vcl/bmpacc.hxx>
39 #include <editeng/eeitem.hxx>
40 #include <svl/itemset.hxx>
41 #include <tools/config.hxx>
42 #include <unotools/cacheoptions.hxx>
43 #include <svl/whiter.hxx>
44 #include <tools/bigint.hxx>
45 #include "editeng/fontitem.hxx"
46 #include <editeng/colritem.hxx>
47 #include <editeng/fhgtitem.hxx>
48 #include <svx/xgrad.hxx>
49 #include <svx/xfillit0.hxx>
50 #include <svx/xflclit.hxx>
51 #include <svx/xflhtit.hxx>
52 #include <svx/xbtmpit.hxx>
53 #include <svx/xflgrit.hxx>
54 #include <svx/svdoole2.hxx>
55 #include <svl/itempool.hxx>
56 #include <unotools/localedatawrapper.hxx>
57 #include <com/sun/star/lang/Locale.hpp>
58 #include <comphelper/processfactory.hxx>
59 #include <i18npool/lang.h>
60 #include <unotools/charclass.hxx>
61 #include <unotools/syslocale.hxx>
62 #include <svx/xflbckit.hxx>
63 #include <svx/extrusionbar.hxx>
64 #include <svx/fontworkbar.hxx>
65 #include <vcl/svapp.hxx> //add CHINA001
66 #include <svx/sdr/contact/viewcontact.hxx>
67 #include <svx/svdpage.hxx>
68 #include <svx/svdotable.hxx>
69 #include <svx/sdrhittesthelper.hxx>
70 
71 using namespace ::com::sun::star;
72 
73 /******************************************************************************
74 * Globale Daten der DrawingEngine
75 ******************************************************************************/
76 
77 SdrGlobalData::SdrGlobalData() :
78     pSysLocale(NULL),
79     pCharClass(NULL),
80     pLocaleData(NULL),
81 	pOutliner(NULL),
82 	pDefaults(NULL),
83 	pResMgr(NULL),
84 	nExchangeFormat(0)
85 {
86     //pSysLocale = new SvtSysLocale;
87     //pCharClass = pSysLocale->GetCharClassPtr();
88     //pLocaleData = pSysLocale->GetLocaleDataPtr();
89 
90 	svx::ExtrusionBar::RegisterInterface();
91 	svx::FontworkBar::RegisterInterface();
92 }
93 
94 SdrGlobalData::~SdrGlobalData()
95 {
96 	delete pOutliner;
97 	delete pDefaults;
98 	delete pResMgr;
99     //! do NOT delete pCharClass and pLocaleData
100     delete pSysLocale;
101 }
102 const SvtSysLocale*         SdrGlobalData::GetSysLocale()
103 {
104     if ( !pSysLocale )
105         pSysLocale = new SvtSysLocale;
106     return pSysLocale;
107 }
108 const CharClass*            SdrGlobalData::GetCharClass()
109 {
110     if ( !pCharClass )
111         pCharClass = GetSysLocale()->GetCharClassPtr();
112     return pCharClass;
113 }
114 const LocaleDataWrapper*    SdrGlobalData::GetLocaleData()
115 {
116     if ( !pLocaleData )
117         pLocaleData = GetSysLocale()->GetLocaleDataPtr();
118     return pLocaleData;
119 }
120 ////////////////////////////////////////////////////////////////////////////////////////////////////
121 
122 OLEObjCache::OLEObjCache()
123 :	Container( 0 )
124 {
125 	SvtCacheOptions aCacheOptions;
126 
127 	nSize = aCacheOptions.GetDrawingEngineOLE_Objects();
128 	pTimer = new AutoTimer();
129     Link aLink = LINK(this, OLEObjCache, UnloadCheckHdl);
130 
131     pTimer->SetTimeoutHdl(aLink);
132     pTimer->SetTimeout(20000);
133     pTimer->Start();
134 
135 	aLink.Call(pTimer);
136 }
137 
138 OLEObjCache::~OLEObjCache()
139 {
140 	pTimer->Stop();
141 	delete pTimer;
142 }
143 
144 void OLEObjCache::UnloadOnDemand()
145 {
146     if ( nSize < Count() )
147 	{
148         // more objects than configured cache size try to remove objects
149         // of course not the freshly inserted one at nIndex=0
150         sal_uIntPtr nCount2 = Count();
151         sal_uIntPtr nIndex = nCount2-1;
152         while( nIndex && nCount2 > nSize )
153 		{
154             SdrOle2Obj* pUnloadObj = (SdrOle2Obj*) GetObject(nIndex--);
155             if ( pUnloadObj )
156             {
157                 try
158                 {
159                     // it is important to get object without reinitialization to avoid reentrance
160                     uno::Reference< embed::XEmbeddedObject > xUnloadObj = pUnloadObj->GetObjRef_NoInit();
161 
162                     sal_Bool bUnload = SdrOle2Obj::CanUnloadRunningObj( xUnloadObj, pUnloadObj->GetAspect() );
163 
164                     // check whether the object can be unloaded before looking for the parent objects
165                     if ( xUnloadObj.is() && bUnload )
166                     {
167                         uno::Reference< frame::XModel > xUnloadModel( xUnloadObj->getComponent(), uno::UNO_QUERY );
168                         if ( xUnloadModel.is() )
169                         {
170                             for ( sal_uIntPtr nCheckInd = 0; nCheckInd < Count(); nCheckInd++ )
171                             {
172                                 SdrOle2Obj* pCacheObj = (SdrOle2Obj*) GetObject(nCheckInd);
173                                 if ( pCacheObj && pCacheObj != pUnloadObj )
174                                 {
175                                     uno::Reference< frame::XModel > xParentModel = pCacheObj->GetParentXModel();
176                                     if ( xUnloadModel == xParentModel )
177                                         bUnload = sal_False; // the object has running embedded objects
178                                 }
179                             }
180                         }
181                     }
182 
183                     if ( bUnload && UnloadObj(pUnloadObj) )
184                         // object was successfully unloaded
185                         nCount2--;
186                 }
187                 catch( uno::Exception& )
188                 {}
189             }
190 		}
191 	}
192 }
193 
194 void OLEObjCache::SetSize(sal_uIntPtr nNewSize)
195 {
196 	nSize = nNewSize;
197 }
198 
199 void OLEObjCache::InsertObj(SdrOle2Obj* pObj)
200 {
201 	if ( Count() )
202 	{
203 		SdrOle2Obj* pExistingObj = (SdrOle2Obj*)GetObject( 0 );
204 		if ( pObj == pExistingObj )
205 			// the object is already on the top, nothing has to be changed
206 			return;
207 	}
208 
209     // get the old position of the object to know whether it is already in container
210     sal_uIntPtr nOldPos = GetPos( pObj );
211 
212     // insert object into first position
213 	Remove( nOldPos );
214 	Insert(pObj, (sal_uIntPtr) 0L);
215 
216     if ( nOldPos == CONTAINER_ENTRY_NOTFOUND )
217     {
218         // a new object was inserted, recalculate the cache
219         UnloadOnDemand();
220     }
221 }
222 
223 void OLEObjCache::RemoveObj(SdrOle2Obj* pObj)
224 {
225     Remove(pObj);
226 }
227 
228 sal_Bool OLEObjCache::UnloadObj(SdrOle2Obj* pObj)
229 {
230 	sal_Bool bUnloaded = sal_False;
231 	if (pObj)
232 	{
233         //#i80528# The old mechanism is completely useless, only taking into account if
234         // in all views the GrafDraft feature is used. This will nearly never have been the
235         // case since no one ever used this option.
236         //
237         // A much better (and working) criteria would be the VOC contact count.
238         // The quesion is what will happen whe i make it work now suddenly? I
239         // will try it for 2.4.
240     	const sdr::contact::ViewContact& rViewContact = pObj->GetViewContact();
241 		const bool bVisible(rViewContact.HasViewObjectContacts(true));
242 
243 		if(!bVisible)
244         {
245     		bUnloaded = pObj->Unload();
246         }
247 	}
248 
249 	return bUnloaded;
250 }
251 
252 IMPL_LINK(OLEObjCache, UnloadCheckHdl, AutoTimer*, /*pTim*/)
253 {
254     UnloadOnDemand();
255     return 0;
256 }
257 
258 void ContainerSorter::DoSort(sal_uIntPtr a, sal_uIntPtr b) const
259 {
260 	sal_uIntPtr nAnz=rCont.Count();
261 	if (b>nAnz) b=nAnz;
262 	if (b>0) b--;
263 	if (a<b) ImpSubSort(a,b);
264 }
265 
266 void ContainerSorter::Is1stLessThan2nd(const void* /*pElem1*/, const void* /*pElem2*/) const
267 {
268 }
269 
270 void ContainerSorter::ImpSubSort(long nL, long nR) const
271 {
272 	long i,j;
273 	const void* pX;
274 	void* pI;
275 	void* pJ;
276 	i=nL;
277 	j=nR;
278 	pX=rCont.GetObject((nL+nR)/2);
279 	do {
280 		pI=rCont.Seek(i);
281 		while (pI!=pX && Compare(pI,pX)<0) { i++; pI=rCont.Next(); }
282 		pJ=rCont.Seek(j);
283 		while (pJ!=pX && Compare(pX,pJ)<0) { j--; pJ=rCont.Prev(); }
284 		if (i<=j) {
285 			rCont.Replace(pJ,i);
286 			rCont.Replace(pI,j);
287 			i++;
288 			j--;
289 		}
290 	} while (i<=j);
291 	if (nL<j) ImpSubSort(nL,j);
292 	if (i<nR) ImpSubSort(i,nR);
293 }
294 
295 ////////////////////////////////////////////////////////////////////////////////////////////////////
296 
297 class ImpUShortContainerSorter: public ContainerSorter {
298 public:
299 	ImpUShortContainerSorter(Container& rNewCont): ContainerSorter(rNewCont) {}
300 	virtual int Compare(const void* pElem1, const void* pElem2) const;
301 };
302 
303 int ImpUShortContainerSorter::Compare(const void* pElem1, const void* pElem2) const
304 {
305 	sal_uInt16 n1=sal_uInt16(sal_uIntPtr(pElem1));
306 	sal_uInt16 n2=sal_uInt16(sal_uIntPtr(pElem2));
307 	return n1<n2 ? -1 : n1>n2 ? 1 : 0;
308 }
309 
310 void UShortCont::Sort()
311 {
312 	ImpUShortContainerSorter aSorter(aArr);
313 	aSorter.DoSort();
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////////////////////////
317 
318 class ImpClipMerk {
319 	Region aClip;
320 	FASTBOOL   bClip;
321 public:
322 	ImpClipMerk(const OutputDevice& rOut): aClip(rOut.GetClipRegion()),bClip(rOut.IsClipRegion()) {}
323 	void Restore(OutputDevice& rOut)
324 	{
325 		// Kein Clipping in die Metafileaufzeichnung
326 		GDIMetaFile* pMtf=rOut.GetConnectMetaFile();
327 		if (pMtf!=NULL && (!pMtf->IsRecord() || pMtf->IsPause())) pMtf=NULL;
328 		if (pMtf!=NULL) pMtf->Pause(sal_True);
329 		if (bClip) rOut.SetClipRegion(aClip);
330 		else rOut.SetClipRegion();
331 		if (pMtf!=NULL) pMtf->Pause(sal_False);
332 	}
333 };
334 
335 class ImpColorMerk {
336 	Color aLineColor;
337 	Color aFillColor;
338 	Color aBckgrdColor;
339 	Font  aFont;
340 public:
341 	ImpColorMerk(const OutputDevice& rOut):
342 		aLineColor( rOut.GetLineColor() ),
343 		aFillColor( rOut.GetFillColor() ),
344 		aBckgrdColor( rOut.GetBackground().GetColor() ),
345 		aFont (rOut.GetFont()) {}
346 
347 	ImpColorMerk(const OutputDevice& rOut, sal_uInt16 nMode)
348 	{
349 		if ( (nMode & SDRHDC_SAVEPEN) == SDRHDC_SAVEPEN )
350 			aLineColor = rOut.GetLineColor();
351 
352 		if ( (nMode & SDRHDC_SAVEBRUSH) == SDRHDC_SAVEBRUSH)
353 		{
354 			aFillColor = rOut.GetFillColor();
355 			aBckgrdColor = rOut.GetBackground().GetColor();
356 		}
357 
358 		if ( (nMode & SDRHDC_SAVEFONT) == SDRHDC_SAVEFONT)
359 			aFont=rOut.GetFont();
360 	}
361 
362 	void Restore(OutputDevice& rOut, sal_uInt16 nMode=SDRHDC_SAVEPENANDBRUSHANDFONT)
363 	{
364 		if ( (nMode & SDRHDC_SAVEPEN) == SDRHDC_SAVEPEN)
365 			rOut.SetLineColor( aLineColor );
366 
367 		if ( (nMode & SDRHDC_SAVEBRUSH) == SDRHDC_SAVEBRUSH)
368 		{
369 			rOut.SetFillColor( aFillColor );
370 			rOut.SetBackground( Wallpaper( aBckgrdColor ) );
371 		}
372 		if ((nMode & SDRHDC_SAVEFONT) ==SDRHDC_SAVEFONT)
373 		{
374 			if (!rOut.GetFont().IsSameInstance(aFont))
375 			{
376 				rOut.SetFont(aFont);
377 			}
378 		}
379 	}
380 
381 	const Color& GetLineColor() const { return aLineColor; }
382 };
383 
384 ImpSdrHdcMerk::ImpSdrHdcMerk(const OutputDevice& rOut, sal_uInt16 nNewMode, FASTBOOL bAutoMerk):
385 	pFarbMerk(NULL),
386 	pClipMerk(NULL),
387 	pLineColorMerk(NULL),
388 	nMode(nNewMode)
389 {
390 	if (bAutoMerk) Save(rOut);
391 }
392 
393 ImpSdrHdcMerk::~ImpSdrHdcMerk()
394 {
395 	if (pFarbMerk!=NULL) delete pFarbMerk;
396 	if (pClipMerk!=NULL) delete pClipMerk;
397 	if (pLineColorMerk !=NULL) delete pLineColorMerk;
398 }
399 
400 void ImpSdrHdcMerk::Save(const OutputDevice& rOut)
401 {
402 	if (pFarbMerk!=NULL)
403 	{
404 		delete pFarbMerk;
405 		pFarbMerk=NULL;
406 	}
407 	if (pClipMerk!=NULL)
408 	{
409 		delete pClipMerk;
410 		pClipMerk=NULL;
411 	}
412 	if (pLineColorMerk !=NULL)
413 	{
414 		delete pLineColorMerk ;
415 		pLineColorMerk =NULL;
416 	}
417 	if ((nMode & SDRHDC_SAVECLIPPING) ==SDRHDC_SAVECLIPPING)
418 		pClipMerk=new ImpClipMerk(rOut);
419 
420 	sal_uInt16 nCol=nMode & SDRHDC_SAVEPENANDBRUSHANDFONT;
421 
422 	if (nCol==SDRHDC_SAVEPEN)
423 		pLineColorMerk=new Color( rOut.GetLineColor() );
424 	else if (nCol==SDRHDC_SAVEPENANDBRUSHANDFONT)
425 		pFarbMerk=new ImpColorMerk(rOut);
426 	else if (nCol!=0)
427 		pFarbMerk=new ImpColorMerk(rOut,nCol);
428 }
429 
430 void ImpSdrHdcMerk::Restore(OutputDevice& rOut, sal_uInt16 nMask) const
431 {
432 	nMask&=nMode; // nur restaurieren, was auch gesichert wurde
433 
434 	if ((nMask & SDRHDC_SAVECLIPPING) ==SDRHDC_SAVECLIPPING && pClipMerk!=NULL)
435 		pClipMerk->Restore(rOut);
436 
437 	sal_uInt16 nCol=nMask & SDRHDC_SAVEPENANDBRUSHANDFONT;
438 
439 	if (nCol==SDRHDC_SAVEPEN)
440 	{
441 		if (pLineColorMerk!=NULL)
442 			rOut.SetLineColor(*pLineColorMerk);
443 		else if (pFarbMerk!=NULL)
444 			rOut.SetLineColor( pFarbMerk->GetLineColor() );
445 	} else if (nCol!=0 && pFarbMerk!=NULL)
446 		pFarbMerk->Restore(rOut,nCol);
447 }
448 
449 ////////////////////////////////////////////////////////////////////////////////////////////////////
450 
451 void SdrLinkList::Clear()
452 {
453 	unsigned nAnz=GetLinkCount();
454 	for (unsigned i=0; i<nAnz; i++) {
455 		delete (Link*)aList.GetObject(i);
456 	}
457 	aList.Clear();
458 }
459 
460 unsigned SdrLinkList::FindEntry(const Link& rLink) const
461 {
462 	unsigned nAnz=GetLinkCount();
463 	for (unsigned i=0; i<nAnz; i++) {
464 		if (GetLink(i)==rLink) return i;
465 	}
466 	return 0xFFFF;
467 }
468 
469 void SdrLinkList::InsertLink(const Link& rLink, unsigned nPos)
470 {
471 	unsigned nFnd=FindEntry(rLink);
472 	if (nFnd==0xFFFF) {
473 		if (rLink.IsSet()) {
474 			aList.Insert(new Link(rLink),nPos);
475 		} else {
476 			DBG_ERROR("SdrLinkList::InsertLink(): Versuch, einen nicht gesetzten Link einzufuegen");
477 		}
478 	} else {
479 		DBG_ERROR("SdrLinkList::InsertLink(): Link schon vorhanden");
480 	}
481 }
482 
483 void SdrLinkList::RemoveLink(const Link& rLink)
484 {
485 	unsigned nFnd=FindEntry(rLink);
486 	if (nFnd!=0xFFFF) {
487 		Link* pLink=(Link*)aList.Remove(nFnd);
488 		delete pLink;
489 	} else {
490 		DBG_ERROR("SdrLinkList::RemoveLink(): Link nicht gefunden");
491 	}
492 }
493 
494 ////////////////////////////////////////////////////////////////////////////////////////////////////
495 // #98988# Re-implement GetDraftFillColor(...)
496 
497 FASTBOOL GetDraftFillColor(const SfxItemSet& rSet, Color& rCol)
498 {
499 	XFillStyle eFill=((XFillStyleItem&)rSet.Get(XATTR_FILLSTYLE)).GetValue();
500 	FASTBOOL bRetval(sal_False);
501 
502 	switch(eFill)
503 	{
504 		case XFILL_SOLID:
505 		{
506 			rCol = ((XFillColorItem&)rSet.Get(XATTR_FILLCOLOR)).GetColorValue();
507 			bRetval = sal_True;
508 
509 			break;
510 		}
511 		case XFILL_HATCH:
512 		{
513 			Color aCol1(((XFillHatchItem&)rSet.Get(XATTR_FILLHATCH)).GetHatchValue().GetColor());
514 			Color aCol2(COL_WHITE);
515 
516 			// #97870# when hatch background is activated, use object fill color as hatch color
517 			sal_Bool bFillHatchBackground = ((const XFillBackgroundItem&)(rSet.Get(XATTR_FILLBACKGROUND))).GetValue();
518 			if(bFillHatchBackground)
519 			{
520 				aCol2 = ((const XFillColorItem&)(rSet.Get(XATTR_FILLCOLOR))).GetColorValue();
521 			}
522 
523             const basegfx::BColor aAverageColor(basegfx::average(aCol1.getBColor(), aCol2.getBColor()));
524 			rCol = Color(aAverageColor);
525 			bRetval = sal_True;
526 
527 			break;
528 		}
529 		case XFILL_GRADIENT: {
530 			const XGradient& rGrad=((XFillGradientItem&)rSet.Get(XATTR_FILLGRADIENT)).GetGradientValue();
531 			Color aCol1(rGrad.GetStartColor());
532 			Color aCol2(rGrad.GetEndColor());
533             const basegfx::BColor aAverageColor(basegfx::average(aCol1.getBColor(), aCol2.getBColor()));
534 			rCol = Color(aAverageColor);
535 			bRetval = sal_True;
536 
537 			break;
538 		}
539 		case XFILL_BITMAP:
540 		{
541 			Bitmap aBitmap(((XFillBitmapItem&)rSet.Get(XATTR_FILLBITMAP)).GetGraphicObject().GetGraphic().GetBitmapEx().GetBitmap());
542 			const Size aSize(aBitmap.GetSizePixel());
543 			const sal_uInt32 nWidth = aSize.Width();
544 			const sal_uInt32 nHeight = aSize.Height();
545 			BitmapReadAccess* pAccess = aBitmap.AcquireReadAccess();
546 
547 			if(pAccess && nWidth > 0 && nHeight > 0)
548 			{
549 				sal_uInt32 nRt(0L);
550 				sal_uInt32 nGn(0L);
551 				sal_uInt32 nBl(0L);
552 				const sal_uInt32 nMaxSteps(8L);
553 				const sal_uInt32 nXStep((nWidth > nMaxSteps) ? nWidth / nMaxSteps : 1L);
554 				const sal_uInt32 nYStep((nHeight > nMaxSteps) ? nHeight / nMaxSteps : 1L);
555 				sal_uInt32 nAnz(0L);
556 
557 				for(sal_uInt32 nY(0L); nY < nHeight; nY += nYStep)
558 				{
559 					for(sal_uInt32 nX(0L); nX < nWidth; nX += nXStep)
560 					{
561 						const BitmapColor& rCol2 = (pAccess->HasPalette())
562 							? pAccess->GetPaletteColor((sal_uInt8)pAccess->GetPixel(nY, nX))
563 							: pAccess->GetPixel(nY, nX);
564 
565 						nRt += rCol2.GetRed();
566 						nGn += rCol2.GetGreen();
567 						nBl += rCol2.GetBlue();
568 						nAnz++;
569 					}
570 				}
571 
572 				nRt /= nAnz;
573 				nGn /= nAnz;
574 				nBl /= nAnz;
575 
576 				rCol = Color(sal_uInt8(nRt), sal_uInt8(nGn), sal_uInt8(nBl));
577 
578 				bRetval = sal_True;
579 			}
580 
581 			if(pAccess)
582 			{
583 				aBitmap.ReleaseAccess(pAccess);
584 			}
585 
586 			break;
587 		}
588 		default: break;
589 	}
590 
591 	return bRetval;
592 }
593 
594 ////////////////////////////////////////////////////////////////////////////////////////////////////
595 
596 SdrEngineDefaults::SdrEngineDefaults():
597 	aFontName( OutputDevice::GetDefaultFont( DEFAULTFONT_SERIF, LANGUAGE_SYSTEM, DEFAULTFONT_FLAGS_ONLYONE ).GetName() ),
598 	eFontFamily(FAMILY_ROMAN),
599 	aFontColor(COL_AUTO),
600 	nFontHeight(847),			  // 847/100mm = ca. 24 Point
601 	eMapUnit(MAP_100TH_MM),
602 	aMapFraction(1,1)
603 {
604 }
605 
606 SdrEngineDefaults& SdrEngineDefaults::GetDefaults()
607 {
608 	SdrGlobalData& rGlobalData=GetSdrGlobalData();
609 	if (rGlobalData.pDefaults==NULL) {
610 		rGlobalData.pDefaults=new SdrEngineDefaults;
611 	}
612 	return *rGlobalData.pDefaults;
613 }
614 
615 ////////////////////////////////////////////////////////////////////////////////////////////////////
616 
617 void SdrEngineDefaults::LanguageHasChanged()
618 {
619 	SdrGlobalData& rGlobalData=GetSdrGlobalData();
620 	if (rGlobalData.pResMgr!=NULL) {
621 		delete rGlobalData.pResMgr;
622 		rGlobalData.pResMgr=NULL;
623 	}
624 }
625 
626 ////////////////////////////////////////////////////////////////////////////////////////////////////
627 
628 SdrOutliner* SdrMakeOutliner( sal_uInt16 nOutlinerMode, SdrModel* pModel )
629 {
630 	//SdrEngineDefaults& rDefaults = SdrEngineDefaults::GetDefaults();
631 
632 	SfxItemPool* pPool = &pModel->GetItemPool();
633 	SdrOutliner* pOutl = new SdrOutliner( pPool, nOutlinerMode );
634 	pOutl->SetEditTextObjectPool( pPool );
635 	pOutl->SetStyleSheetPool( (SfxStyleSheetPool*) pModel->GetStyleSheetPool() );
636 	pOutl->SetDefTab( pModel->GetDefaultTabulator() );
637 	pOutl->SetForbiddenCharsTable( pModel->GetForbiddenCharsTable() );
638 	pOutl->SetAsianCompressionMode( pModel->GetCharCompressType() );
639 	pOutl->SetKernAsianPunctuation( pModel->IsKernAsianPunctuation() );
640     pOutl->SetAddExtLeading( pModel->IsAddExtLeading() );
641 
642 	return pOutl;
643 }
644 
645 ////////////////////////////////////////////////////////////////////////////////////////////////////
646 
647 
648 SdrLinkList& ImpGetUserMakeObjHdl()
649 {
650 	SdrGlobalData& rGlobalData=GetSdrGlobalData();
651 	return rGlobalData.aUserMakeObjHdl;
652 }
653 
654 SdrLinkList& ImpGetUserMakeObjUserDataHdl()
655 {
656 	SdrGlobalData& rGlobalData=GetSdrGlobalData();
657 	return rGlobalData.aUserMakeObjUserDataHdl;
658 }
659 
660 ////////////////////////////////////////////////////////////////////////////////////////////////////
661 
662 ResMgr* ImpGetResMgr()
663 {
664 	SdrGlobalData& rGlobalData = GetSdrGlobalData();
665 
666 	if(!rGlobalData.pResMgr)
667 	{
668 		ByteString aName("svx");
669 		rGlobalData.pResMgr =
670             ResMgr::CreateResMgr( aName.GetBuffer(), Application::GetSettings().GetUILocale() );
671 	}
672 
673 	return rGlobalData.pResMgr;
674 }
675 
676 ////////////////////////////////////////////////////////////////////////////////////////////////////
677 
678 String ImpGetResStr(sal_uInt16 nResID)
679 {
680 	return String(ResId(nResID, *ImpGetResMgr()));
681 }
682 
683 ////////////////////////////////////////////////////////////////////////////////////////////////////
684 
685 namespace sdr
686 {
687 String GetResourceString(sal_uInt16 nResID)
688 {
689 	return ImpGetResStr( nResID );
690 }
691 }
692 
693 ////////////////////////////////////////////////////////////////////////////////////////////////////
694 
695 sal_Bool SearchOutlinerItems(const SfxItemSet& rSet, sal_Bool bInklDefaults, sal_Bool* pbOnlyEE)
696 {
697 	sal_Bool bHas=sal_False;
698 	sal_Bool bOnly=sal_True;
699 	sal_Bool bLookOnly=pbOnlyEE!=NULL;
700 	SfxWhichIter aIter(rSet);
701 	sal_uInt16 nWhich=aIter.FirstWhich();
702 	while (((bLookOnly && bOnly) || !bHas) && nWhich!=0) {
703 		// bei bInklDefaults ist der gesamte Which-Range
704 		// ausschlaggebend, ansonsten nur die gesetzten Items
705 		// Disabled und DontCare wird als Loch im Which-Range betrachtet
706 		SfxItemState eState=rSet.GetItemState(nWhich);
707 		if ((eState==SFX_ITEM_DEFAULT && bInklDefaults) || eState==SFX_ITEM_SET) {
708 			if (nWhich<EE_ITEMS_START || nWhich>EE_ITEMS_END) bOnly=sal_False;
709 			else bHas=sal_True;
710 		}
711 		nWhich=aIter.NextWhich();
712 	}
713 	if (!bHas) bOnly=sal_False;
714 	if (pbOnlyEE!=NULL) *pbOnlyEE=bOnly;
715 	return bHas;
716 }
717 
718 sal_uInt16* RemoveWhichRange(const sal_uInt16* pOldWhichTable, sal_uInt16 nRangeBeg, sal_uInt16 nRangeEnd)
719 {
720 	// insgesamt sind 6 Faelle moeglich (je Range):
721 	//		   [Beg..End]		   zu entfernender Range
722 	// [b..e]	 [b..e]    [b..e]  Fall 1,3,2: egal, ganz weg, egal  + Ranges
723 	// [b........e]  [b........e]  Fall 4,5  : Bereich verkleinern	 | in
724 	// [b......................e]  Fall 6	 : Splitting			 + pOldWhichTable
725 	sal_uInt16 nAnz=0;
726 	while (pOldWhichTable[nAnz]!=0) nAnz++;
727 	nAnz++; // nAnz muesste nun in jedem Fall eine ungerade Zahl sein (0 am Ende des Arrays)
728 	DBG_ASSERT((nAnz&1)==1,"Joe: RemoveWhichRange: WhichTable hat keine ungerade Anzahl von Eintraegen");
729 	sal_uInt16 nAlloc=nAnz;
730 	// benoetigte Groesse des neuen Arrays ermitteln
731 	sal_uInt16 nNum=nAnz-1;
732 	while (nNum!=0) {
733 		nNum-=2;
734 		sal_uInt16 nBeg=pOldWhichTable[nNum];
735 		sal_uInt16 nEnd=pOldWhichTable[nNum+1];
736 		if (nEnd<nRangeBeg)  /*nCase=1*/ ;
737 		else if (nBeg>nRangeEnd) /* nCase=2 */ ;
738 		else if (nBeg>=nRangeBeg && nEnd<=nRangeEnd) /* nCase=3 */ nAlloc-=2;
739 		else if (nEnd<=nRangeEnd) /* nCase=4 */;
740 		else if (nBeg>=nRangeBeg) /* nCase=5*/ ;
741 		else /* nCase=6 */ nAlloc+=2;
742 	}
743 
744 	sal_uInt16* pNewWhichTable=new sal_uInt16[nAlloc];
745 	memcpy(pNewWhichTable,pOldWhichTable,nAlloc*sizeof(sal_uInt16));
746 	pNewWhichTable[nAlloc-1]=0; // im Falle 3 fehlt die 0 am Ende
747 	// nun die unerwuenschten Ranges entfernen
748 	nNum=nAlloc-1;
749 	while (nNum!=0) {
750 		nNum-=2;
751 		sal_uInt16 nBeg=pNewWhichTable[nNum];
752 		sal_uInt16 nEnd=pNewWhichTable[nNum+1];
753 		unsigned nCase=0;
754 		if (nEnd<nRangeBeg) nCase=1;
755 		else if (nBeg>nRangeEnd) nCase=2;
756 		else if (nBeg>=nRangeBeg && nEnd<=nRangeEnd) nCase=3;
757 		else if (nEnd<=nRangeEnd) nCase=4;
758 		else if (nBeg>=nRangeBeg) nCase=5;
759 		else nCase=6;
760 		switch (nCase) {
761 			case 3: {
762 				unsigned nTailBytes=(nAnz-(nNum+2))*sizeof(sal_uInt16);
763 				memcpy(&pNewWhichTable[nNum],&pNewWhichTable[nNum+2],nTailBytes);
764 				nAnz-=2; // Merken: Array hat sich verkleinert
765 			} break;
766 			case 4: pNewWhichTable[nNum+1]=nRangeBeg-1; break;
767 			case 5: pNewWhichTable[nNum]=nRangeEnd+1;	  break;
768 			case 6: {
769 				unsigned nTailBytes=(nAnz-(nNum+2))*sizeof(sal_uInt16);
770 				memcpy(&pNewWhichTable[nNum+4],&pNewWhichTable[nNum+2],nTailBytes);
771 				nAnz+=2; // Merken: Array hat sich vergroessert
772 				pNewWhichTable[nNum+2]=nRangeEnd+1;
773 				pNewWhichTable[nNum+3]=pNewWhichTable[nNum+1];
774 				pNewWhichTable[nNum+1]=nRangeBeg-1;
775 			} break;
776 		} // switch
777 	}
778 	return pNewWhichTable;
779 }
780 
781 ////////////////////////////////////////////////////////////////////////////////////////////////////
782 
783 SvdProgressInfo::SvdProgressInfo( Link *_pLink )
784 {
785 	DBG_ASSERT(_pLink!=NULL,"SvdProgressInfo(): Kein Link angegeben!!");
786 
787 	pLink = _pLink;
788 	nSumActionCount = 0;
789 	nSumCurAction	= 0;
790 
791 	nObjCount = 0;
792 	nCurObj   = 0;
793 
794 	nActionCount = 0;
795 	nCurAction	 = 0;
796 
797 	nInsertCount = 0;
798 	nCurInsert	 = 0;
799 }
800 
801 void SvdProgressInfo::Init( sal_uIntPtr _nSumActionCount, sal_uIntPtr _nObjCount )
802 {
803 	nSumActionCount = _nSumActionCount;
804 	nObjCount = _nObjCount;
805 }
806 
807 sal_Bool SvdProgressInfo::ReportActions( sal_uIntPtr nAnzActions )
808 {
809 	nSumCurAction += nAnzActions;
810 	nCurAction += nAnzActions;
811 	if(nCurAction > nActionCount)
812 		nCurAction = nActionCount;
813 
814 	return pLink->Call(NULL) == 1L;
815 }
816 
817 sal_Bool SvdProgressInfo::ReportInserts( sal_uIntPtr nAnzInserts )
818 {
819 	nSumCurAction += nAnzInserts;
820 	nCurInsert += nAnzInserts;
821 
822 	return pLink->Call(NULL) == 1L;
823 }
824 
825 sal_Bool SvdProgressInfo::ReportRescales( sal_uIntPtr nAnzRescales )
826 {
827 	nSumCurAction += nAnzRescales;
828 	return pLink->Call(NULL) == 1L;
829 }
830 
831 void SvdProgressInfo::SetActionCount( sal_uIntPtr _nActionCount )
832 {
833 	nActionCount = _nActionCount;
834 }
835 
836 void SvdProgressInfo::SetInsertCount( sal_uIntPtr _nInsertCount )
837 {
838 	nInsertCount = _nInsertCount;
839 }
840 
841 sal_Bool SvdProgressInfo::SetNextObject()
842 {
843 	nActionCount = 0;
844 	nCurAction	 = 0;
845 
846 	nInsertCount = 0;
847 	nCurInsert	 = 0;
848 
849 	nCurObj++;
850 	return ReportActions(0);
851 }
852 
853 void SvdProgressInfo::ReportError()
854 {
855 	pLink->Call((void *)1L);
856 }
857 
858 ////////////////////////////////////////////////////////////////////////////////////////////////////
859 // #i101872# isolate GetTextEditBackgroundColor to tooling; it woll anyways only be used as long
860 // as text edit is not running on overlay
861 
862 namespace
863 {
864     bool impGetSdrObjListFillColor(
865         const SdrObjList& rList,
866         const Point& rPnt,
867         const SdrPageView& rTextEditPV,
868         const SetOfByte& rVisLayers,
869         Color& rCol)
870     {
871 	    if(!rList.GetModel())
872             return false;
873 
874 	    bool bRet(false);
875 	    bool bMaster(rList.GetPage() ? rList.GetPage()->IsMasterPage() : false);
876 
877 	    for(sal_uIntPtr no(rList.GetObjCount()); !bRet && no > 0; )
878         {
879 		    no--;
880 		    SdrObject* pObj = rList.GetObj(no);
881 		    SdrObjList* pOL = pObj->GetSubList();
882 
883             if(pOL)
884             {
885                 // group object
886 			    bRet = impGetSdrObjListFillColor(*pOL, rPnt, rTextEditPV, rVisLayers, rCol);
887 		    }
888             else
889             {
890         		SdrTextObj* pText = dynamic_cast< SdrTextObj * >(pObj);
891 
892                 // #108867# Exclude zero master page object (i.e. background shape) from color query
893 			    if(pText
894                     && pObj->IsClosedObj()
895                     && (!bMaster || (!pObj->IsNotVisibleAsMaster() && 0 != no))
896                     && pObj->GetCurrentBoundRect().IsInside(rPnt)
897                     && !pText->IsHideContour()
898                     && SdrObjectPrimitiveHit(*pObj, rPnt, 0, rTextEditPV, &rVisLayers, false))
899 			    {
900             	    bRet = GetDraftFillColor(pObj->GetMergedItemSet(), rCol);
901 			    }
902 		    }
903 	    }
904 
905         return bRet;
906     }
907 
908     bool impGetSdrPageFillColor(
909         const SdrPage& rPage,
910         const Point& rPnt,
911         const SdrPageView& rTextEditPV,
912         const SetOfByte& rVisLayers,
913         Color& rCol,
914         bool bSkipBackgroundShape)
915     {
916 	    if(!rPage.GetModel())
917 		    return false;
918 
919 	    bool bRet(impGetSdrObjListFillColor(rPage, rPnt, rTextEditPV, rVisLayers, rCol));
920 
921 	    if(!bRet && !rPage.IsMasterPage())
922 	    {
923 		    if(rPage.TRG_HasMasterPage())
924 		    {
925 			    SetOfByte aSet(rVisLayers);
926 			    aSet &= rPage.TRG_GetMasterPageVisibleLayers();
927 			    SdrPage& rMasterPage = rPage.TRG_GetMasterPage();
928 
929 			    // #108867# Don't fall back to background shape on
930                 // master pages. This is later handled by
931                 // GetBackgroundColor, and is necessary to cater for
932                 // the silly ordering: 1. shapes, 2. master page
933                 // shapes, 3. page background, 4. master page
934                 // background.
935 			    bRet = impGetSdrPageFillColor(rMasterPage, rPnt, rTextEditPV, aSet, rCol, true);
936 		    }
937 	    }
938 
939         // #108867# Only now determine background color from background shapes
940         if(!bRet && !bSkipBackgroundShape)
941         {
942             rCol = rPage.GetPageBackgroundColor();
943 		    return true;
944         }
945 
946 	    return bRet;
947     }
948 
949     Color impCalcBackgroundColor(
950         const Rectangle& rArea,
951         const SdrPageView& rTextEditPV,
952         const SdrPage& rPage)
953     {
954         svtools::ColorConfig aColorConfig;
955         Color aBackground(aColorConfig.GetColorValue(svtools::DOCCOLOR).nColor);
956 	    const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
957 
958 	    if(!rStyleSettings.GetHighContrastMode())
959 	    {
960             // search in page
961             const sal_uInt16 SPOTCOUNT(5);
962             Point aSpotPos[SPOTCOUNT];
963             Color aSpotColor[SPOTCOUNT];
964             sal_uIntPtr nHeight( rArea.GetSize().Height() );
965             sal_uIntPtr nWidth( rArea.GetSize().Width() );
966             sal_uIntPtr nWidth14  = nWidth / 4;
967             sal_uIntPtr nHeight14 = nHeight / 4;
968             sal_uIntPtr nWidth34  = ( 3 * nWidth ) / 4;
969             sal_uIntPtr nHeight34 = ( 3 * nHeight ) / 4;
970 
971             sal_uInt16 i;
972             for ( i = 0; i < SPOTCOUNT; i++ )
973             {
974                 // five spots are used
975                 switch ( i )
976                 {
977                     case 0 :
978                     {
979                         // Center-Spot
980                         aSpotPos[i] = rArea.Center();
981                     }
982                     break;
983 
984                     case 1 :
985                     {
986                         // TopLeft-Spot
987                         aSpotPos[i] = rArea.TopLeft();
988                         aSpotPos[i].X() += nWidth14;
989                         aSpotPos[i].Y() += nHeight14;
990                     }
991                     break;
992 
993                     case 2 :
994                     {
995                         // TopRight-Spot
996                         aSpotPos[i] = rArea.TopLeft();
997                         aSpotPos[i].X() += nWidth34;
998                         aSpotPos[i].Y() += nHeight14;
999                     }
1000                     break;
1001 
1002                     case 3 :
1003                     {
1004                         // BottomLeft-Spot
1005                         aSpotPos[i] = rArea.TopLeft();
1006                         aSpotPos[i].X() += nWidth14;
1007                         aSpotPos[i].Y() += nHeight34;
1008                     }
1009                     break;
1010 
1011                     case 4 :
1012                     {
1013                         // BottomRight-Spot
1014                         aSpotPos[i] = rArea.TopLeft();
1015                         aSpotPos[i].X() += nWidth34;
1016                         aSpotPos[i].Y() += nHeight34;
1017                     }
1018                     break;
1019 
1020                 }
1021 
1022                 aSpotColor[i] = Color( COL_WHITE );
1023                 impGetSdrPageFillColor(rPage, aSpotPos[i], rTextEditPV, rTextEditPV.GetVisibleLayers(), aSpotColor[i], false);
1024             }
1025 
1026             sal_uInt16 aMatch[SPOTCOUNT];
1027 
1028             for ( i = 0; i < SPOTCOUNT; i++ )
1029             {
1030                 // were same spot colors found?
1031                 aMatch[i] = 0;
1032 
1033                 for ( sal_uInt16 j = 0; j < SPOTCOUNT; j++ )
1034                 {
1035                     if( j != i )
1036                     {
1037                         if( aSpotColor[i] == aSpotColor[j] )
1038                         {
1039                             aMatch[i]++;
1040                         }
1041                     }
1042                 }
1043             }
1044 
1045             // highest weight to center spot
1046             aBackground = aSpotColor[0];
1047 
1048             for ( sal_uInt16 nMatchCount = SPOTCOUNT - 1; nMatchCount > 1; nMatchCount-- )
1049             {
1050                 // which spot color was found most?
1051                 for ( i = 0; i < SPOTCOUNT; i++ )
1052                 {
1053                     if( aMatch[i] == nMatchCount )
1054                     {
1055                         aBackground = aSpotColor[i];
1056                         nMatchCount = 1;   // break outer for-loop
1057                         break;
1058                     }
1059                 }
1060             }
1061 	    }
1062 
1063         return aBackground;
1064     }
1065 } // end of anonymous namespace
1066 
1067 Color GetTextEditBackgroundColor(const SdrObjEditView& rView)
1068 {
1069     svtools::ColorConfig aColorConfig;
1070     Color aBackground(aColorConfig.GetColorValue(svtools::DOCCOLOR).nColor);
1071 	const StyleSettings& rStyleSettings = Application::GetSettings().GetStyleSettings();
1072 
1073 	if(!rStyleSettings.GetHighContrastMode())
1074 	{
1075 		bool bFound(false);
1076 		SdrTextObj* pText = dynamic_cast< SdrTextObj * >(rView.GetTextEditObject());
1077 
1078         if(pText && pText->IsClosedObj())
1079 		{
1080 			::sdr::table::SdrTableObj* pTable = dynamic_cast< ::sdr::table::SdrTableObj * >( pText );
1081 
1082             if( pTable )
1083 				bFound = GetDraftFillColor(pTable->GetActiveCellItemSet(), aBackground );
1084 
1085 			if( !bFound )
1086 				bFound=GetDraftFillColor(pText->GetMergedItemSet(), aBackground);
1087 		}
1088 
1089         if(!bFound && pText)
1090 		{
1091             SdrPageView* pTextEditPV = rView.GetTextEditPageView();
1092 
1093             if(pTextEditPV)
1094 		    {
1095 			    Point aPvOfs(pText->GetTextEditOffset());
1096 			    const SdrPage* pPg = pTextEditPV->GetPage();
1097 
1098 			    if(pPg)
1099 			    {
1100 				    Rectangle aSnapRect( pText->GetSnapRect() );
1101 				    aSnapRect.Move(aPvOfs.X(), aPvOfs.Y());
1102 
1103 				    return impCalcBackgroundColor(aSnapRect, *pTextEditPV, *pPg);
1104 			    }
1105             }
1106         }
1107     }
1108 
1109     return aBackground;
1110 }
1111 
1112 ////////////////////////////////////////////////////////////////////////////////////////////////////
1113 // eof
1114