1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_svx.hxx"
30*cdf0e10cSrcweir #include <svx/sdr/contact/viewcontact.hxx>
31*cdf0e10cSrcweir #include <svx/sdr/contact/viewobjectcontact.hxx>
32*cdf0e10cSrcweir #include <svx/sdr/contact/objectcontact.hxx>
33*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygon.hxx>
34*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolygontools.hxx>
35*cdf0e10cSrcweir #include <basegfx/color/bcolor.hxx>
36*cdf0e10cSrcweir #include <drawinglayer/primitive2d/polygonprimitive2d.hxx>
37*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrix.hxx>
38*cdf0e10cSrcweir #include <svx/sdr/contact/objectcontactofpageview.hxx>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir namespace sdr
43*cdf0e10cSrcweir {
44*cdf0e10cSrcweir 	namespace contact
45*cdf0e10cSrcweir 	{
46*cdf0e10cSrcweir         // Create a Object-Specific ViewObjectContact, set ViewContact and
47*cdf0e10cSrcweir 		// ObjectContact. Always needs to return something. Default is to create
48*cdf0e10cSrcweir 		// a standard ViewObjectContact containing the given ObjectContact and *this
49*cdf0e10cSrcweir 		ViewObjectContact& ViewContact::CreateObjectSpecificViewObjectContact(ObjectContact& rObjectContact)
50*cdf0e10cSrcweir 		{
51*cdf0e10cSrcweir 			return *(new ViewObjectContact(rObjectContact, *this));
52*cdf0e10cSrcweir 		}
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 		ViewContact::ViewContact()
55*cdf0e10cSrcweir         :	maViewObjectContactVector(),
56*cdf0e10cSrcweir 			mxViewIndependentPrimitive2DSequence()
57*cdf0e10cSrcweir 		{
58*cdf0e10cSrcweir 		}
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 		// Methods to react on start getting viewed or stop getting
61*cdf0e10cSrcweir 		// viewed. This info is derived from the count of members of
62*cdf0e10cSrcweir 		// registered ViewObjectContacts. Default does nothing.
63*cdf0e10cSrcweir 		void ViewContact::StartGettingViewed()
64*cdf0e10cSrcweir 		{
65*cdf0e10cSrcweir 		}
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir 		void ViewContact::StopGettingViewed()
68*cdf0e10cSrcweir 		{
69*cdf0e10cSrcweir 		}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 		ViewContact::~ViewContact()
72*cdf0e10cSrcweir 		{
73*cdf0e10cSrcweir             deleteAllVOCs();
74*cdf0e10cSrcweir         }
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 		void ViewContact::deleteAllVOCs()
77*cdf0e10cSrcweir 		{
78*cdf0e10cSrcweir 			// get rid of all VOCs
79*cdf0e10cSrcweir 			// #i84257# To avoid that each 'delete pCandidate' again uses
80*cdf0e10cSrcweir 			// the local RemoveViewObjectContact with a search and removal in the
81*cdf0e10cSrcweir 			// vector, simply copy and clear local vector.
82*cdf0e10cSrcweir 			std::vector< ViewObjectContact* > aLocalVOCList(maViewObjectContactVector);
83*cdf0e10cSrcweir 			maViewObjectContactVector.clear();
84*cdf0e10cSrcweir 
85*cdf0e10cSrcweir 			while(!aLocalVOCList.empty())
86*cdf0e10cSrcweir 			{
87*cdf0e10cSrcweir 				ViewObjectContact* pCandidate = aLocalVOCList.back();
88*cdf0e10cSrcweir 				aLocalVOCList.pop_back();
89*cdf0e10cSrcweir 				DBG_ASSERT(pCandidate, "Corrupted ViewObjectContactList in VC (!)");
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 				// ViewObjectContacts only make sense with View and Object contacts.
92*cdf0e10cSrcweir 				// When the contact to the SdrObject is deleted like in this case,
93*cdf0e10cSrcweir 				// all ViewObjectContacts can be deleted, too.
94*cdf0e10cSrcweir 				delete pCandidate;
95*cdf0e10cSrcweir 			}
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 			// assert when there were new entries added during deletion
98*cdf0e10cSrcweir 			DBG_ASSERT(maViewObjectContactVector.empty(), "Corrupted ViewObjectContactList in VC (!)");
99*cdf0e10cSrcweir         }
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 		// get a Object-specific ViewObjectContact for a specific
102*cdf0e10cSrcweir 		// ObjectContact (->View). Always needs to return something.
103*cdf0e10cSrcweir 		ViewObjectContact& ViewContact::GetViewObjectContact(ObjectContact& rObjectContact)
104*cdf0e10cSrcweir 		{
105*cdf0e10cSrcweir 			ViewObjectContact* pRetval = 0L;
106*cdf0e10cSrcweir 			const sal_uInt32 nCount(maViewObjectContactVector.size());
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 			// first search if there exists a VOC for the given OC
109*cdf0e10cSrcweir 			for(sal_uInt32 a(0); !pRetval && a < nCount; a++)
110*cdf0e10cSrcweir 			{
111*cdf0e10cSrcweir 				ViewObjectContact* pCandidate = maViewObjectContactVector[a];
112*cdf0e10cSrcweir 				DBG_ASSERT(pCandidate, "Corrupted ViewObjectContactList (!)");
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 				if(&(pCandidate->GetObjectContact()) == &rObjectContact)
115*cdf0e10cSrcweir 				{
116*cdf0e10cSrcweir 					pRetval = pCandidate;
117*cdf0e10cSrcweir 				}
118*cdf0e10cSrcweir 			}
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 			if(!pRetval)
121*cdf0e10cSrcweir 			{
122*cdf0e10cSrcweir 				// create a new one. It's inserted to the local list from the
123*cdf0e10cSrcweir 				// VieObjectContact constructor via AddViewObjectContact()
124*cdf0e10cSrcweir 				pRetval = &CreateObjectSpecificViewObjectContact(rObjectContact);
125*cdf0e10cSrcweir 			}
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 			return *pRetval;
128*cdf0e10cSrcweir 		}
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 		// A new ViewObjectContact was created and shall be remembered.
131*cdf0e10cSrcweir 		void ViewContact::AddViewObjectContact(ViewObjectContact& rVOContact)
132*cdf0e10cSrcweir 		{
133*cdf0e10cSrcweir 			maViewObjectContactVector.push_back(&rVOContact);
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir 			if(1L == maViewObjectContactVector.size())
136*cdf0e10cSrcweir 			{
137*cdf0e10cSrcweir 				StartGettingViewed();
138*cdf0e10cSrcweir 			}
139*cdf0e10cSrcweir 		}
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 		// A ViewObjectContact was deleted and shall be forgotten.
142*cdf0e10cSrcweir 		void ViewContact::RemoveViewObjectContact(ViewObjectContact& rVOContact)
143*cdf0e10cSrcweir 		{
144*cdf0e10cSrcweir 			std::vector< ViewObjectContact* >::iterator aFindResult = std::find(maViewObjectContactVector.begin(), maViewObjectContactVector.end(), &rVOContact);
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 			if(aFindResult != maViewObjectContactVector.end())
147*cdf0e10cSrcweir 			{
148*cdf0e10cSrcweir 				maViewObjectContactVector.erase(aFindResult);
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 				if(maViewObjectContactVector.empty())
151*cdf0e10cSrcweir 				{
152*cdf0e10cSrcweir                     // This may need to get asynchron later since it eventually triggers
153*cdf0e10cSrcweir                     // deletes of OCs where the VOC is still added.
154*cdf0e10cSrcweir 					StopGettingViewed();
155*cdf0e10cSrcweir 				}
156*cdf0e10cSrcweir 			}
157*cdf0e10cSrcweir 		}
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 		// Test if this ViewContact has ViewObjectContacts at all. This can
160*cdf0e10cSrcweir 		// be used to test if this ViewContact is visualized ATM or not
161*cdf0e10cSrcweir 		bool ViewContact::HasViewObjectContacts(bool bExcludePreviews) const
162*cdf0e10cSrcweir 		{
163*cdf0e10cSrcweir 			const sal_uInt32 nCount(maViewObjectContactVector.size());
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir 			if(bExcludePreviews)
166*cdf0e10cSrcweir             {
167*cdf0e10cSrcweir                 for(sal_uInt32 a(0); a < nCount; a++)
168*cdf0e10cSrcweir                 {
169*cdf0e10cSrcweir                     if(!maViewObjectContactVector[a]->GetObjectContact().IsPreviewRenderer())
170*cdf0e10cSrcweir                     {
171*cdf0e10cSrcweir                         return true;
172*cdf0e10cSrcweir                     }
173*cdf0e10cSrcweir                 }
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir                 return false;
176*cdf0e10cSrcweir             }
177*cdf0e10cSrcweir             else
178*cdf0e10cSrcweir             {
179*cdf0e10cSrcweir     			return (0L != nCount);
180*cdf0e10cSrcweir             }
181*cdf0e10cSrcweir 		}
182*cdf0e10cSrcweir 
183*cdf0e10cSrcweir 		// Test if this ViewContact has ViewObjectContacts at all. This can
184*cdf0e10cSrcweir 		// be used to test if this ViewContact is visualized ATM or not
185*cdf0e10cSrcweir 		bool ViewContact::isAnimatedInAnyViewObjectContact() const
186*cdf0e10cSrcweir 		{
187*cdf0e10cSrcweir 			const sal_uInt32 nCount(maViewObjectContactVector.size());
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 			for(sal_uInt32 a(0); a < nCount; a++)
190*cdf0e10cSrcweir 			{
191*cdf0e10cSrcweir 				if(maViewObjectContactVector[a]->isAnimated())
192*cdf0e10cSrcweir 				{
193*cdf0e10cSrcweir 					return true;
194*cdf0e10cSrcweir 				}
195*cdf0e10cSrcweir 			}
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 			return false;
198*cdf0e10cSrcweir 		}
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 		// Access to possible sub-hierarchy and parent. GetObjectCount() default is 0L
201*cdf0e10cSrcweir 		// and GetViewContact default pops up an assert since it's an error if
202*cdf0e10cSrcweir 		// GetObjectCount has a result != 0 and it's not overloaded.
203*cdf0e10cSrcweir 		sal_uInt32 ViewContact::GetObjectCount() const
204*cdf0e10cSrcweir 		{
205*cdf0e10cSrcweir 			// no sub-objects
206*cdf0e10cSrcweir 			return 0;
207*cdf0e10cSrcweir 		}
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir 		ViewContact& ViewContact::GetViewContact(sal_uInt32 /*nIndex*/) const
210*cdf0e10cSrcweir 		{
211*cdf0e10cSrcweir 			// This is the default implementation; call would be an error
212*cdf0e10cSrcweir 			DBG_ERROR("ViewContact::GetViewContact: This call needs to be overloaded when GetObjectCount() can return results != 0 (!)");
213*cdf0e10cSrcweir 			return (ViewContact&)(*this);
214*cdf0e10cSrcweir 		}
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 		ViewContact* ViewContact::GetParentContact() const
217*cdf0e10cSrcweir 		{
218*cdf0e10cSrcweir 			// default has no parent
219*cdf0e10cSrcweir 			return 0;
220*cdf0e10cSrcweir 		}
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir 		void ViewContact::ActionChildInserted(ViewContact& rChild)
223*cdf0e10cSrcweir 		{
224*cdf0e10cSrcweir 			// propagate change to all exsisting visualisations which
225*cdf0e10cSrcweir 			// will force a VOC for the new child and invalidate it's range
226*cdf0e10cSrcweir 			const sal_uInt32 nCount(maViewObjectContactVector.size());
227*cdf0e10cSrcweir 
228*cdf0e10cSrcweir 			for(sal_uInt32 a(0); a < nCount; a++)
229*cdf0e10cSrcweir 			{
230*cdf0e10cSrcweir 				ViewObjectContact* pCandidate = maViewObjectContactVector[a];
231*cdf0e10cSrcweir 				DBG_ASSERT(pCandidate, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 				// take action at all VOCs. At the VOCs ObjectContact the initial
234*cdf0e10cSrcweir 				// rectangle will be invalidated at the associated OutputDevice.
235*cdf0e10cSrcweir 				pCandidate->ActionChildInserted(rChild);
236*cdf0e10cSrcweir 			}
237*cdf0e10cSrcweir 		}
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 		// React on changes of the object of this ViewContact
240*cdf0e10cSrcweir 		void ViewContact::ActionChanged()
241*cdf0e10cSrcweir 		{
242*cdf0e10cSrcweir 			// propagate change to all existing VOCs. This will invalidate
243*cdf0e10cSrcweir 			// all drawn visualisations in all known views
244*cdf0e10cSrcweir 			const sal_uInt32 nCount(maViewObjectContactVector.size());
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir 			for(sal_uInt32 a(0); a < nCount; a++)
247*cdf0e10cSrcweir 			{
248*cdf0e10cSrcweir 				ViewObjectContact* pCandidate = maViewObjectContactVector[a];
249*cdf0e10cSrcweir 				DBG_ASSERT(pCandidate, "ViewContact::GetViewObjectContact() invalid ViewObjectContactList (!)");
250*cdf0e10cSrcweir 
251*cdf0e10cSrcweir 				pCandidate->ActionChanged();
252*cdf0e10cSrcweir 			}
253*cdf0e10cSrcweir 		}
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 		// access to SdrObject and/or SdrPage. May return 0L like the default
256*cdf0e10cSrcweir 		// implementations do. Needs to be overloaded as needed.
257*cdf0e10cSrcweir 		SdrObject* ViewContact::TryToGetSdrObject() const
258*cdf0e10cSrcweir 		{
259*cdf0e10cSrcweir 			return 0L;
260*cdf0e10cSrcweir 		}
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 		SdrPage* ViewContact::TryToGetSdrPage() const
263*cdf0e10cSrcweir 		{
264*cdf0e10cSrcweir 			return 0L;
265*cdf0e10cSrcweir 		}
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 		//////////////////////////////////////////////////////////////////////////////
268*cdf0e10cSrcweir 		// primitive stuff
269*cdf0e10cSrcweir 
270*cdf0e10cSrcweir 		drawinglayer::primitive2d::Primitive2DSequence ViewContact::createViewIndependentPrimitive2DSequence() const
271*cdf0e10cSrcweir 		{
272*cdf0e10cSrcweir 			// This is the default impelemtation and should never be called (see header). If this is called,
273*cdf0e10cSrcweir             // someone implemented a ViewContact (VC) visualisation object without defining the visualisation by
274*cdf0e10cSrcweir             // providing a seqence of primitives -> which cannot be correct.
275*cdf0e10cSrcweir             // Since we have no access to any known model data here, the default implementation creates a yellow placeholder
276*cdf0e10cSrcweir             // hairline polygon with a default size of (1000, 1000, 5000, 3000)
277*cdf0e10cSrcweir             DBG_ERROR("ViewContact::createViewIndependentPrimitive2DSequence(): Never call the fallback base implementation, this is always an error (!)");
278*cdf0e10cSrcweir             const basegfx::B2DPolygon aOutline(basegfx::tools::createPolygonFromRect(basegfx::B2DRange(1000.0, 1000.0, 5000.0, 3000.0)));
279*cdf0e10cSrcweir 			const basegfx::BColor aYellow(1.0, 1.0, 0.0);
280*cdf0e10cSrcweir 			const drawinglayer::primitive2d::Primitive2DReference xReference(
281*cdf0e10cSrcweir                 new drawinglayer::primitive2d::PolygonHairlinePrimitive2D(aOutline, aYellow));
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir             return drawinglayer::primitive2d::Primitive2DSequence(&xReference, 1);
284*cdf0e10cSrcweir 		}
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 		drawinglayer::primitive2d::Primitive2DSequence ViewContact::getViewIndependentPrimitive2DSequence() const
287*cdf0e10cSrcweir 		{
288*cdf0e10cSrcweir 			// local up-to-date checks. Create new list and compare.
289*cdf0e10cSrcweir 			const drawinglayer::primitive2d::Primitive2DSequence xNew(createViewIndependentPrimitive2DSequence());
290*cdf0e10cSrcweir 
291*cdf0e10cSrcweir 			if(!drawinglayer::primitive2d::arePrimitive2DSequencesEqual(mxViewIndependentPrimitive2DSequence, xNew))
292*cdf0e10cSrcweir 			{
293*cdf0e10cSrcweir 				// has changed, copy content
294*cdf0e10cSrcweir 				const_cast< ViewContact* >(this)->mxViewIndependentPrimitive2DSequence = xNew;
295*cdf0e10cSrcweir 			}
296*cdf0e10cSrcweir 
297*cdf0e10cSrcweir 			// return current Primitive2DSequence
298*cdf0e10cSrcweir 			return mxViewIndependentPrimitive2DSequence;
299*cdf0e10cSrcweir 		}
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 		// add Gluepoints (if available)
302*cdf0e10cSrcweir 		drawinglayer::primitive2d::Primitive2DSequence ViewContact::createGluePointPrimitive2DSequence() const
303*cdf0e10cSrcweir 		{
304*cdf0e10cSrcweir 			// default returns empty reference
305*cdf0e10cSrcweir 			return drawinglayer::primitive2d::Primitive2DSequence();
306*cdf0e10cSrcweir 		}
307*cdf0e10cSrcweir 
308*cdf0e10cSrcweir         void ViewContact::flushViewObjectContacts(bool bWithHierarchy)
309*cdf0e10cSrcweir         {
310*cdf0e10cSrcweir             if(bWithHierarchy)
311*cdf0e10cSrcweir             {
312*cdf0e10cSrcweir                 // flush DrawingLayer hierarchy
313*cdf0e10cSrcweir 			    const sal_uInt32 nCount(GetObjectCount());
314*cdf0e10cSrcweir 
315*cdf0e10cSrcweir                 for(sal_uInt32 a(0); a < nCount; a++)
316*cdf0e10cSrcweir                 {
317*cdf0e10cSrcweir                     ViewContact& rChild = GetViewContact(a);
318*cdf0e10cSrcweir                     rChild.flushViewObjectContacts(bWithHierarchy);
319*cdf0e10cSrcweir                 }
320*cdf0e10cSrcweir             }
321*cdf0e10cSrcweir 
322*cdf0e10cSrcweir             // delete local VOCs
323*cdf0e10cSrcweir             deleteAllVOCs();
324*cdf0e10cSrcweir         }
325*cdf0e10cSrcweir 	} // end of namespace contact
326*cdf0e10cSrcweir } // end of namespace sdr
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
329*cdf0e10cSrcweir // eof
330