xref: /AOO41X/main/svx/source/svdraw/svdglue.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svx.hxx"
30 #include <tools/debug.hxx>
31 
32 #include <svx/svdglue.hxx>
33 #include <svx/svdobj.hxx>
34 #include <svx/svdtrans.hxx>
35 
36 ////////////////////////////////////////////////////////////////////////////////////////////////////
37 
38 void SdrGluePoint::SetReallyAbsolute(FASTBOOL bOn, const SdrObject& rObj)
39 {
40 	if ( bReallyAbsolute != bOn )
41 	{
42 	   if ( bOn )
43 	   {
44 		   aPos=GetAbsolutePos(rObj);
45 		   bReallyAbsolute=bOn;
46 	   }
47 	   else
48 	   {
49 		   bReallyAbsolute=bOn;
50 		   Point aPt(aPos);
51 		   SetAbsolutePos(aPt,rObj);
52 	   }
53 	}
54 }
55 
56 Point SdrGluePoint::GetAbsolutePos(const SdrObject& rObj) const
57 {
58 	if (bReallyAbsolute) return aPos;
59 	Rectangle aSnap(rObj.GetSnapRect());
60 	Rectangle aBound(rObj.GetSnapRect());
61 	Point aPt(aPos);
62 
63 	Point aOfs(aSnap.Center());
64 	switch (GetHorzAlign()) {
65 		case SDRHORZALIGN_LEFT  : aOfs.X()=aSnap.Left(); break;
66 		case SDRHORZALIGN_RIGHT : aOfs.X()=aSnap.Right(); break;
67 	}
68 	switch (GetVertAlign()) {
69 		case SDRVERTALIGN_TOP   : aOfs.Y()=aSnap.Top(); break;
70 		case SDRVERTALIGN_BOTTOM: aOfs.Y()=aSnap.Bottom(); break;
71 	}
72 	if (!bNoPercent) {
73 		long nXMul=aSnap.Right()-aSnap.Left();
74 		long nYMul=aSnap.Bottom()-aSnap.Top();
75 		long nXDiv=10000;
76 		long nYDiv=10000;
77 		if (nXMul!=nXDiv) {
78 			aPt.X()*=nXMul;
79 			aPt.X()/=nXDiv;
80 		}
81 		if (nYMul!=nYDiv) {
82 			aPt.Y()*=nYMul;
83 			aPt.Y()/=nYDiv;
84 		}
85 	}
86 	aPt+=aOfs;
87 	// Und nun auf's BoundRect des Objekts begrenzen
88 	if (aPt.X()<aBound.Left  ()) aPt.X()=aBound.Left  ();
89 	if (aPt.X()>aBound.Right ()) aPt.X()=aBound.Right ();
90 	if (aPt.Y()<aBound.Top   ()) aPt.Y()=aBound.Top   ();
91 	if (aPt.Y()>aBound.Bottom()) aPt.Y()=aBound.Bottom();
92 	return aPt;
93 }
94 
95 void SdrGluePoint::SetAbsolutePos(const Point& rNewPos, const SdrObject& rObj)
96 {
97 	if (bReallyAbsolute) {
98 		aPos=rNewPos;
99 		return;
100 	}
101 	Rectangle aSnap(rObj.GetSnapRect());
102 	Point aPt(rNewPos);
103 
104 	Point aOfs(aSnap.Center());
105 	switch (GetHorzAlign()) {
106 		case SDRHORZALIGN_LEFT  : aOfs.X()=aSnap.Left(); break;
107 		case SDRHORZALIGN_RIGHT : aOfs.X()=aSnap.Right(); break;
108 	}
109 	switch (GetVertAlign()) {
110 		case SDRVERTALIGN_TOP   : aOfs.Y()=aSnap.Top(); break;
111 		case SDRVERTALIGN_BOTTOM: aOfs.Y()=aSnap.Bottom(); break;
112 	}
113 	aPt-=aOfs;
114 	if (!bNoPercent) {
115 		long nXMul=aSnap.Right()-aSnap.Left();
116 		long nYMul=aSnap.Bottom()-aSnap.Top();
117 		if (nXMul==0) nXMul=1;
118 		if (nYMul==0) nYMul=1;
119 		long nXDiv=10000;
120 		long nYDiv=10000;
121 		if (nXMul!=nXDiv) {
122 			aPt.X()*=nXDiv;
123 			aPt.X()/=nXMul;
124 		}
125 		if (nYMul!=nYDiv) {
126 			aPt.Y()*=nYDiv;
127 			aPt.Y()/=nYMul;
128 		}
129 	}
130 	aPos=aPt;
131 }
132 
133 long SdrGluePoint::GetAlignAngle() const
134 {
135 	switch (nAlign) {
136 		case SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER: return 0; // Invalid!
137 		case SDRHORZALIGN_RIGHT |SDRVERTALIGN_CENTER: return 0;
138 		case SDRHORZALIGN_RIGHT |SDRVERTALIGN_TOP   : return 4500;
139 		case SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP   : return 9000;
140 		case SDRHORZALIGN_LEFT  |SDRVERTALIGN_TOP   : return 13500;
141 		case SDRHORZALIGN_LEFT  |SDRVERTALIGN_CENTER: return 18000;
142 		case SDRHORZALIGN_LEFT  |SDRVERTALIGN_BOTTOM: return 22500;
143 		case SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM: return 27000;
144 		case SDRHORZALIGN_RIGHT |SDRVERTALIGN_BOTTOM: return 31500;
145 	} // switch
146 	return 0;
147 }
148 
149 void SdrGluePoint::SetAlignAngle(long nWink)
150 {
151 	nWink=NormAngle360(nWink);
152 	if (nWink>=33750 || nWink<2250) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_CENTER;
153 	else if (nWink< 6750) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_TOP   ;
154 	else if (nWink<11250) nAlign=SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP   ;
155 	else if (nWink<15750) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_TOP   ;
156 	else if (nWink<20250) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_CENTER;
157 	else if (nWink<24750) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_BOTTOM;
158 	else if (nWink<29250) nAlign=SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM;
159 	else if (nWink<33750) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_BOTTOM;
160 }
161 
162 long SdrGluePoint::EscDirToAngle(sal_uInt16 nEsc) const
163 {
164 	switch (nEsc) {
165 		case SDRESC_RIGHT : return 0;
166 		case SDRESC_TOP   : return 9000;
167 		case SDRESC_LEFT  : return 18000;
168 		case SDRESC_BOTTOM: return 27000;
169 	} // switch
170 	return 0;
171 }
172 
173 sal_uInt16 SdrGluePoint::EscAngleToDir(long nWink) const
174 {
175 	nWink=NormAngle360(nWink);
176 	if (nWink>=31500 || nWink<4500) return SDRESC_RIGHT;
177 	if (nWink<13500) return SDRESC_TOP;
178 	if (nWink<22500) return SDRESC_LEFT;
179 	if (nWink<31500) return SDRESC_BOTTOM;
180 	return 0;
181 }
182 
183 void SdrGluePoint::Rotate(const Point& rRef, long nWink, double sn, double cs, const SdrObject* pObj)
184 {
185 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
186 	RotatePoint(aPt,rRef,sn,cs);
187 	// Bezugskante drehen
188 	if(nAlign != (SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER))
189 	{
190 		SetAlignAngle(GetAlignAngle()+nWink);
191 	}
192 	// Austrittsrichtungen drehen
193 	sal_uInt16 nEscDir0=nEscDir;
194 	sal_uInt16 nEscDir1=0;
195 	if ((nEscDir0&SDRESC_LEFT  )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_LEFT  )+nWink);
196 	if ((nEscDir0&SDRESC_TOP   )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_TOP   )+nWink);
197 	if ((nEscDir0&SDRESC_RIGHT )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_RIGHT )+nWink);
198 	if ((nEscDir0&SDRESC_BOTTOM)!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_BOTTOM)+nWink);
199 	nEscDir=nEscDir1;
200 	if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
201 }
202 
203 void SdrGluePoint::Mirror(const Point& rRef1, const Point& rRef2, const SdrObject* pObj)
204 {
205 	Point aPt(rRef2); aPt-=rRef1;
206 	long nWink=GetAngle(aPt);
207 	Mirror(rRef1,rRef2,nWink,pObj);
208 }
209 
210 void SdrGluePoint::Mirror(const Point& rRef1, const Point& rRef2, long nWink, const SdrObject* pObj)
211 {
212 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
213 	MirrorPoint(aPt,rRef1,rRef2);
214 	// Bezugskante spiegeln
215 	if(nAlign != (SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER))
216 	{
217 		long nAW=GetAlignAngle();
218 		nAW+=2*(nWink-nAW);
219 		SetAlignAngle(nAW);
220 	}
221 	// Austrittsrichtungen spiegeln
222 	sal_uInt16 nEscDir0=nEscDir;
223 	sal_uInt16 nEscDir1=0;
224 	if ((nEscDir0&SDRESC_LEFT)!=0) {
225 		long nEW=EscDirToAngle(SDRESC_LEFT);
226 		nEW+=2*(nWink-nEW);
227 		nEscDir1|=EscAngleToDir(nEW);
228 	}
229 	if ((nEscDir0&SDRESC_TOP)!=0) {
230 		long nEW=EscDirToAngle(SDRESC_TOP);
231 		nEW+=2*(nWink-nEW);
232 		nEscDir1|=EscAngleToDir(nEW);
233 	}
234 	if ((nEscDir0&SDRESC_RIGHT)!=0) {
235 		long nEW=EscDirToAngle(SDRESC_RIGHT);
236 		nEW+=2*(nWink-nEW);
237 		nEscDir1|=EscAngleToDir(nEW);
238 	}
239 	if ((nEscDir0&SDRESC_BOTTOM)!=0) {
240 		long nEW=EscDirToAngle(SDRESC_BOTTOM);
241 		nEW+=2*(nWink-nEW);
242 		nEscDir1|=EscAngleToDir(nEW);
243 	}
244 	nEscDir=nEscDir1;
245 	if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
246 }
247 
248 void SdrGluePoint::Shear(const Point& rRef, long /*nWink*/, double tn, FASTBOOL bVShear, const SdrObject* pObj)
249 {
250 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
251 	ShearPoint(aPt,rRef,tn,bVShear);
252 	if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
253 }
254 
255 void SdrGluePoint::Draw(OutputDevice& rOut, const SdrObject* pObj) const
256 {
257 	Color aBackPenColor(COL_WHITE);
258 	Color aForePenColor(COL_LIGHTBLUE);
259 
260 	bool bMapMerk=rOut.IsMapModeEnabled();
261 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
262 	aPt=rOut.LogicToPixel(aPt);
263 	rOut.EnableMapMode(sal_False);
264 	long x=aPt.X(),y=aPt.Y(); // Groesse erstmal fest auf 7 Pixel
265 
266 	rOut.SetLineColor( aBackPenColor );
267 	rOut.DrawLine(Point(x-2,y-3),Point(x+3,y+2));
268 	rOut.DrawLine(Point(x-3,y-2),Point(x+2,y+3));
269 	rOut.DrawLine(Point(x-3,y+2),Point(x+2,y-3));
270 	rOut.DrawLine(Point(x-2,y+3),Point(x+3,y-2));
271 
272 	if (bNoPercent)
273 	{
274 		switch (GetHorzAlign())
275 		{
276 			case SDRHORZALIGN_LEFT  : rOut.DrawLine(Point(x-3,y-1),Point(x-3,y+1)); break;
277 			case SDRHORZALIGN_RIGHT : rOut.DrawLine(Point(x+3,y-1),Point(x+3,y+1)); break;
278 		}
279 
280 		switch (GetVertAlign())
281 		{
282 			case SDRVERTALIGN_TOP   : rOut.DrawLine(Point(x-1,y-3),Point(x+1,y-3)); break;
283 			case SDRVERTALIGN_BOTTOM: rOut.DrawLine(Point(x-1,y+3),Point(x+1,y+3)); break;
284 		}
285 	}
286 
287 	rOut.SetLineColor( aForePenColor );
288 	rOut.DrawLine(Point(x-2,y-2),Point(x+2,y+2));
289 	rOut.DrawLine(Point(x-2,y+2),Point(x+2,y-2));
290 	rOut.EnableMapMode(bMapMerk);
291 }
292 
293 void SdrGluePoint::Invalidate(Window& rWin, const SdrObject* pObj) const
294 {
295 	bool bMapMerk=rWin.IsMapModeEnabled();
296 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
297 	aPt=rWin.LogicToPixel(aPt);
298 	rWin.EnableMapMode(sal_False);
299 	long x=aPt.X(),y=aPt.Y(); // Groesse erstmal fest auf 7 Pixel
300 
301 	// #111096#
302 	// do not erase background, that causes flicker (!)
303 	rWin.Invalidate(Rectangle(Point(x-3,y-3),Point(x+3,y+3)), INVALIDATE_NOERASE);
304 
305 	rWin.EnableMapMode(bMapMerk);
306 }
307 
308 FASTBOOL SdrGluePoint::IsHit(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj) const
309 {
310 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
311 	Size aSiz=rOut.PixelToLogic(Size(3,3));
312 	Rectangle aRect(aPt.X()-aSiz.Width(),aPt.Y()-aSiz.Height(),aPt.X()+aSiz.Width(),aPt.Y()+aSiz.Height());
313 	return aRect.IsInside(rPnt);
314 }
315 
316 ////////////////////////////////////////////////////////////////////////////////////////////////////
317 
318 void SdrGluePointList::Clear()
319 {
320 	sal_uInt16 nAnz=GetCount();
321 	for (sal_uInt16 i=0; i<nAnz; i++) {
322 		delete GetObject(i);
323 	}
324 	aList.Clear();
325 }
326 
327 void SdrGluePointList::operator=(const SdrGluePointList& rSrcList)
328 {
329 	if (GetCount()!=0) Clear();
330 	sal_uInt16 nAnz=rSrcList.GetCount();
331 	for (sal_uInt16 i=0; i<nAnz; i++) {
332 		Insert(rSrcList[i]);
333 	}
334 }
335 
336 // Die Id's der Klebepunkte in der Liste sind stets streng monoton steigend!
337 // Ggf. wird dem neuen Klebepunkt eine neue Id zugewiesen (wenn diese bereits
338 // vergeben ist). Die Id 0 ist reserviert.
339 sal_uInt16 SdrGluePointList::Insert(const SdrGluePoint& rGP)
340 {
341 	SdrGluePoint* pGP=new SdrGluePoint(rGP);
342 	sal_uInt16 nId=pGP->GetId();
343 	sal_uInt16 nAnz=GetCount();
344 	sal_uInt16 nInsPos=nAnz;
345 	sal_uInt16 nLastId=nAnz!=0 ? GetObject(nAnz-1)->GetId() : 0;
346 	DBG_ASSERT(nLastId>=nAnz,"SdrGluePointList::Insert(): nLastId<nAnz");
347 	FASTBOOL bHole=nLastId>nAnz;
348 	if (nId<=nLastId) {
349 		if (!bHole || nId==0) {
350 			nId=nLastId+1;
351 		} else {
352 			FASTBOOL bBrk=sal_False;
353 			for (sal_uInt16 nNum=0; nNum<nAnz && !bBrk; nNum++) {
354 				const SdrGluePoint* pGP2=GetObject(nNum);
355 				sal_uInt16 nTmpId=pGP2->GetId();
356 				if (nTmpId==nId) {
357 					nId=nLastId+1; // bereits vorhanden
358 					bBrk=sal_True;
359 				}
360 				if (nTmpId>nId) {
361 					nInsPos=nNum; // Hier einfuegen (einsortieren)
362 					bBrk=sal_True;
363 				}
364 			}
365 		}
366 		pGP->SetId(nId);
367 	}
368 	aList.Insert(pGP,nInsPos);
369 	return nInsPos;
370 }
371 
372 void SdrGluePointList::Invalidate(Window& rWin, const SdrObject* pObj) const
373 {
374 	sal_uInt16 nAnz=GetCount();
375 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
376 		GetObject(nNum)->Invalidate(rWin,pObj);
377 	}
378 }
379 
380 sal_uInt16 SdrGluePointList::FindGluePoint(sal_uInt16 nId) const
381 {
382 	// Hier noch einen optimaleren Suchalgorithmus implementieren.
383 	// Die Liste sollte stets sortiert sein!!!!
384 	sal_uInt16 nAnz=GetCount();
385 	sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
386 	for (sal_uInt16 nNum=0; nNum<nAnz && nRet==SDRGLUEPOINT_NOTFOUND; nNum++) {
387 		const SdrGluePoint* pGP=GetObject(nNum);
388 		if (pGP->GetId()==nId) nRet=nNum;
389 	}
390 	return nRet;
391 }
392 
393 sal_uInt16 SdrGluePointList::HitTest(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj, FASTBOOL bBack, FASTBOOL bNext, sal_uInt16 nId0) const
394 {
395 	sal_uInt16 nAnz=GetCount();
396 	sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
397 	sal_uInt16 nNum=bBack ? 0 : nAnz;
398 	while ((bBack ? nNum<nAnz : nNum>0) && nRet==SDRGLUEPOINT_NOTFOUND) {
399 		if (!bBack) nNum--;
400 		const SdrGluePoint* pGP=GetObject(nNum);
401 		if (bNext) {
402 			if (pGP->GetId()==nId0) bNext=sal_False;
403 		} else {
404 			if (pGP->IsHit(rPnt,rOut,pObj)) nRet=nNum;
405 		}
406 		if (bBack) nNum++;
407 	}
408 	return nRet;
409 }
410 
411 void SdrGluePointList::SetReallyAbsolute(FASTBOOL bOn, const SdrObject& rObj)
412 {
413 	sal_uInt16 nAnz=GetCount();
414 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
415 		GetObject(nNum)->SetReallyAbsolute(bOn,rObj);
416 	}
417 }
418 
419 void SdrGluePointList::Rotate(const Point& rRef, long nWink, double sn, double cs, const SdrObject* pObj)
420 {
421 	sal_uInt16 nAnz=GetCount();
422 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
423 		GetObject(nNum)->Rotate(rRef,nWink,sn,cs,pObj);
424 	}
425 }
426 
427 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, const SdrObject* pObj)
428 {
429 	Point aPt(rRef2); aPt-=rRef1;
430 	long nWink=GetAngle(aPt);
431 	Mirror(rRef1,rRef2,nWink,pObj);
432 }
433 
434 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, long nWink, const SdrObject* pObj)
435 {
436 	sal_uInt16 nAnz=GetCount();
437 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
438 		GetObject(nNum)->Mirror(rRef1,rRef2,nWink,pObj);
439 	}
440 }
441 
442 void SdrGluePointList::Shear(const Point& rRef, long nWink, double tn, FASTBOOL bVShear, const SdrObject* pObj)
443 {
444 	sal_uInt16 nAnz=GetCount();
445 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
446 		GetObject(nNum)->Shear(rRef,nWink,tn,bVShear,pObj);
447 	}
448 }
449 
450 // eof
451