xref: /trunk/main/svx/source/svdraw/svdglue.cxx (revision cb22d026)
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 <tools/debug.hxx>
27 
28 #include <svx/svdglue.hxx>
29 #include <svx/svdobj.hxx>
30 #include <svx/svdtrans.hxx>
31 
32 
SetReallyAbsolute(FASTBOOL bOn,const SdrObject & rObj)33 void SdrGluePoint::SetReallyAbsolute(FASTBOOL bOn, const SdrObject& rObj)
34 {
35 	if ( bReallyAbsolute != bOn )
36 	{
37 		if ( bOn )
38 		{
39 			aPos=GetAbsolutePos(rObj);
40 			bReallyAbsolute=bOn;
41 		}
42 		else
43 		{
44 			bReallyAbsolute=bOn;
45 			Point aPt(aPos);
46 			SetAbsolutePos(aPt,rObj);
47 		}
48 	}
49 }
50 
GetAbsolutePos(const SdrObject & rObj) const51 Point SdrGluePoint::GetAbsolutePos(const SdrObject& rObj) const
52 {
53 	if (bReallyAbsolute) return aPos;
54 	Rectangle aSnap(rObj.GetSnapRect());
55 	Rectangle aBound(rObj.GetSnapRect());
56 	Point aPt(aPos);
57 
58 	Point aOfs(aSnap.Center());
59 	switch (GetHorzAlign()) {
60 		case SDRHORZALIGN_LEFT  : aOfs.X()=aSnap.Left(); break;
61 		case SDRHORZALIGN_RIGHT : aOfs.X()=aSnap.Right(); break;
62 	}
63 	switch (GetVertAlign()) {
64 		case SDRVERTALIGN_TOP   : aOfs.Y()=aSnap.Top(); break;
65 		case SDRVERTALIGN_BOTTOM: aOfs.Y()=aSnap.Bottom(); break;
66 	}
67 	if (!bNoPercent) {
68 		long nXMul=aSnap.Right()-aSnap.Left();
69 		long nYMul=aSnap.Bottom()-aSnap.Top();
70 		long nXDiv=10000;
71 		long nYDiv=10000;
72 		if (nXMul!=nXDiv) {
73 			aPt.X()*=nXMul;
74 			aPt.X()/=nXDiv;
75 		}
76 		if (nYMul!=nYDiv) {
77 			aPt.Y()*=nYMul;
78 			aPt.Y()/=nYDiv;
79 		}
80 	}
81 	aPt+=aOfs;
82 	// Und nun auf's BoundRect des Objekts begrenzen
83 	if (aPt.X()<aBound.Left  ()) aPt.X()=aBound.Left  ();
84 	if (aPt.X()>aBound.Right ()) aPt.X()=aBound.Right ();
85 	if (aPt.Y()<aBound.Top   ()) aPt.Y()=aBound.Top   ();
86 	if (aPt.Y()>aBound.Bottom()) aPt.Y()=aBound.Bottom();
87 	return aPt;
88 }
89 
SetAbsolutePos(const Point & rNewPos,const SdrObject & rObj)90 void SdrGluePoint::SetAbsolutePos(const Point& rNewPos, const SdrObject& rObj)
91 {
92 	if (bReallyAbsolute) {
93 		aPos=rNewPos;
94 		return;
95 	}
96 	Rectangle aSnap(rObj.GetSnapRect());
97 	Point aPt(rNewPos);
98 
99 	Point aOfs(aSnap.Center());
100 	switch (GetHorzAlign()) {
101 		case SDRHORZALIGN_LEFT  : aOfs.X()=aSnap.Left(); break;
102 		case SDRHORZALIGN_RIGHT : aOfs.X()=aSnap.Right(); break;
103 	}
104 	switch (GetVertAlign()) {
105 		case SDRVERTALIGN_TOP   : aOfs.Y()=aSnap.Top(); break;
106 		case SDRVERTALIGN_BOTTOM: aOfs.Y()=aSnap.Bottom(); break;
107 	}
108 	aPt-=aOfs;
109 	if (!bNoPercent) {
110 		long nXMul=aSnap.Right()-aSnap.Left();
111 		long nYMul=aSnap.Bottom()-aSnap.Top();
112 		if (nXMul==0) nXMul=1;
113 		if (nYMul==0) nYMul=1;
114 		long nXDiv=10000;
115 		long nYDiv=10000;
116 		if (nXMul!=nXDiv) {
117 			aPt.X()*=nXDiv;
118 			aPt.X()/=nXMul;
119 		}
120 		if (nYMul!=nYDiv) {
121 			aPt.Y()*=nYDiv;
122 			aPt.Y()/=nYMul;
123 		}
124 	}
125 	aPos=aPt;
126 }
127 
GetAlignAngle() const128 long SdrGluePoint::GetAlignAngle() const
129 {
130 	switch (nAlign) {
131 		case SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER: return 0; // Invalid!
132 		case SDRHORZALIGN_RIGHT |SDRVERTALIGN_CENTER: return 0;
133 		case SDRHORZALIGN_RIGHT |SDRVERTALIGN_TOP   : return 4500;
134 		case SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP   : return 9000;
135 		case SDRHORZALIGN_LEFT  |SDRVERTALIGN_TOP   : return 13500;
136 		case SDRHORZALIGN_LEFT  |SDRVERTALIGN_CENTER: return 18000;
137 		case SDRHORZALIGN_LEFT  |SDRVERTALIGN_BOTTOM: return 22500;
138 		case SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM: return 27000;
139 		case SDRHORZALIGN_RIGHT |SDRVERTALIGN_BOTTOM: return 31500;
140 	} // switch
141 	return 0;
142 }
143 
SetAlignAngle(long nWink)144 void SdrGluePoint::SetAlignAngle(long nWink)
145 {
146 	nWink=NormAngle360(nWink);
147 	if (nWink>=33750 || nWink<2250) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_CENTER;
148 	else if (nWink< 6750) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_TOP   ;
149 	else if (nWink<11250) nAlign=SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP   ;
150 	else if (nWink<15750) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_TOP   ;
151 	else if (nWink<20250) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_CENTER;
152 	else if (nWink<24750) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_BOTTOM;
153 	else if (nWink<29250) nAlign=SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM;
154 	else if (nWink<33750) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_BOTTOM;
155 }
156 
EscDirToAngle(sal_uInt16 nEsc) const157 long SdrGluePoint::EscDirToAngle(sal_uInt16 nEsc) const
158 {
159 	switch (nEsc) {
160 		case SDRESC_RIGHT : return 0;
161 		case SDRESC_TOP   : return 9000;
162 		case SDRESC_LEFT  : return 18000;
163 		case SDRESC_BOTTOM: return 27000;
164 	} // switch
165 	return 0;
166 }
167 
EscAngleToDir(long nWink) const168 sal_uInt16 SdrGluePoint::EscAngleToDir(long nWink) const
169 {
170 	nWink=NormAngle360(nWink);
171 	if (nWink>=31500 || nWink<4500) return SDRESC_RIGHT;
172 	if (nWink<13500) return SDRESC_TOP;
173 	if (nWink<22500) return SDRESC_LEFT;
174 	if (nWink<31500) return SDRESC_BOTTOM;
175 	return 0;
176 }
177 
Rotate(const Point & rRef,long nWink,double sn,double cs,const SdrObject * pObj)178 void SdrGluePoint::Rotate(const Point& rRef, long nWink, double sn, double cs, const SdrObject* pObj)
179 {
180 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
181 	RotatePoint(aPt,rRef,sn,cs);
182 	// Bezugskante drehen
183 	if(nAlign != (SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER))
184 	{
185 		SetAlignAngle(GetAlignAngle()+nWink);
186 	}
187 	// rotate escape directions
188 	sal_uInt16 nEscDir0=nEscDir;
189 	sal_uInt16 nEscDir1=0;
190 	if ((nEscDir0&SDRESC_LEFT  )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_LEFT  )+nWink);
191 	if ((nEscDir0&SDRESC_TOP   )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_TOP   )+nWink);
192 	if ((nEscDir0&SDRESC_RIGHT )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_RIGHT )+nWink);
193 	if ((nEscDir0&SDRESC_BOTTOM)!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_BOTTOM)+nWink);
194 	nEscDir=nEscDir1;
195 	if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
196 }
197 
Mirror(const Point & rRef1,const Point & rRef2,const SdrObject * pObj)198 void SdrGluePoint::Mirror(const Point& rRef1, const Point& rRef2, const SdrObject* pObj)
199 {
200 	Point aPt(rRef2); aPt-=rRef1;
201 	long nWink=GetAngle(aPt);
202 	Mirror(rRef1,rRef2,nWink,pObj);
203 }
204 
Mirror(const Point & rRef1,const Point & rRef2,long nWink,const SdrObject * pObj)205 void SdrGluePoint::Mirror(const Point& rRef1, const Point& rRef2, long nWink, const SdrObject* pObj)
206 {
207 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
208 	MirrorPoint(aPt,rRef1,rRef2);
209 	// Bezugskante spiegeln
210 	if(nAlign != (SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER))
211 	{
212 		long nAW=GetAlignAngle();
213 		nAW+=2*(nWink-nAW);
214 		SetAlignAngle(nAW);
215 	}
216 	// mirror escape directions
217 	sal_uInt16 nEscDir0=nEscDir;
218 	sal_uInt16 nEscDir1=0;
219 	if ((nEscDir0&SDRESC_LEFT)!=0) {
220 		long nEW=EscDirToAngle(SDRESC_LEFT);
221 		nEW+=2*(nWink-nEW);
222 		nEscDir1|=EscAngleToDir(nEW);
223 	}
224 	if ((nEscDir0&SDRESC_TOP)!=0) {
225 		long nEW=EscDirToAngle(SDRESC_TOP);
226 		nEW+=2*(nWink-nEW);
227 		nEscDir1|=EscAngleToDir(nEW);
228 	}
229 	if ((nEscDir0&SDRESC_RIGHT)!=0) {
230 		long nEW=EscDirToAngle(SDRESC_RIGHT);
231 		nEW+=2*(nWink-nEW);
232 		nEscDir1|=EscAngleToDir(nEW);
233 	}
234 	if ((nEscDir0&SDRESC_BOTTOM)!=0) {
235 		long nEW=EscDirToAngle(SDRESC_BOTTOM);
236 		nEW+=2*(nWink-nEW);
237 		nEscDir1|=EscAngleToDir(nEW);
238 	}
239 	nEscDir=nEscDir1;
240 	if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
241 }
242 
Shear(const Point & rRef,long,double tn,FASTBOOL bVShear,const SdrObject * pObj)243 void SdrGluePoint::Shear(const Point& rRef, long /*nWink*/, double tn, FASTBOOL bVShear, const SdrObject* pObj)
244 {
245 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
246 	ShearPoint(aPt,rRef,tn,bVShear);
247 	if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
248 }
249 
Invalidate(Window & rWin,const SdrObject * pObj) const250 void SdrGluePoint::Invalidate(Window& rWin, const SdrObject* pObj) const
251 {
252 	bool bMapMerk=rWin.IsMapModeEnabled();
253 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
254 	aPt=rWin.LogicToPixel(aPt);
255 	rWin.EnableMapMode(sal_False);
256 	long x=aPt.X(),y=aPt.Y(); // size 9x9 pixel
257 
258 	// #111096#
259 	// do not erase background, that causes flicker (!)
260 	rWin.Invalidate(Rectangle(Point(x-4,y-4),Point(x+4,y+4)), INVALIDATE_NOERASE);
261 
262 	rWin.EnableMapMode(bMapMerk);
263 }
264 
IsHit(const Point & rPnt,const OutputDevice & rOut,const SdrObject * pObj) const265 FASTBOOL SdrGluePoint::IsHit(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj) const
266 {
267 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
268 	Size aSiz=rOut.PixelToLogic(Size(4,4));
269 	Rectangle aRect(aPt.X()-aSiz.Width(),aPt.Y()-aSiz.Height(),aPt.X()+aSiz.Width(),aPt.Y()+aSiz.Height());
270 	return aRect.IsInside(rPnt);
271 }
272 
273 
Clear()274 void SdrGluePointList::Clear()
275 {
276 	sal_uInt16 nAnz=GetCount();
277 	for (sal_uInt16 i=0; i<nAnz; i++) {
278 		delete GetObject(i);
279 	}
280 	aList.Clear();
281 }
282 
operator =(const SdrGluePointList & rSrcList)283 void SdrGluePointList::operator=(const SdrGluePointList& rSrcList)
284 {
285 	if (GetCount()!=0) Clear();
286 	sal_uInt16 nAnz=rSrcList.GetCount();
287 	for (sal_uInt16 i=0; i<nAnz; i++) {
288 		Insert(rSrcList[i]);
289 	}
290 }
291 
292 // Die Id's der Klebepunkte in der Liste sind stets streng monoton steigend!
293 // Ggf. wird dem neuen Klebepunkt eine neue Id zugewiesen (wenn diese bereits
294 // vergeben ist). Die Id 0 ist reserviert.
Insert(const SdrGluePoint & rGP)295 sal_uInt16 SdrGluePointList::Insert(const SdrGluePoint& rGP)
296 {
297 	SdrGluePoint* pGP=new SdrGluePoint(rGP);
298 	sal_uInt16 nId=pGP->GetId();
299 	sal_uInt16 nAnz=GetCount();
300 	sal_uInt16 nInsPos=nAnz;
301 	sal_uInt16 nLastId=nAnz!=0 ? GetObject(nAnz-1)->GetId() : 0;
302 	DBG_ASSERT(nLastId>=nAnz,"SdrGluePointList::Insert(): nLastId<nAnz");
303 	FASTBOOL bHole=nLastId>nAnz;
304 	if (nId<=nLastId) {
305 		if (!bHole || nId==0) {
306 			nId=nLastId+1;
307 		} else {
308 			FASTBOOL bBrk=sal_False;
309 			for (sal_uInt16 nNum=0; nNum<nAnz && !bBrk; nNum++) {
310 				const SdrGluePoint* pGP2=GetObject(nNum);
311 				sal_uInt16 nTmpId=pGP2->GetId();
312 				if (nTmpId==nId) {
313 					nId=nLastId+1; // bereits vorhanden
314 					bBrk=sal_True;
315 				}
316 				if (nTmpId>nId) {
317 					nInsPos=nNum; // Hier einfuegen (einsortieren)
318 					bBrk=sal_True;
319 				}
320 			}
321 		}
322 		pGP->SetId(nId);
323 	}
324 	aList.Insert(pGP,nInsPos);
325 	return nInsPos;
326 }
327 
Invalidate(Window & rWin,const SdrObject * pObj) const328 void SdrGluePointList::Invalidate(Window& rWin, const SdrObject* pObj) const
329 {
330 	sal_uInt16 nAnz=GetCount();
331 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
332 		GetObject(nNum)->Invalidate(rWin,pObj);
333 	}
334 }
335 
FindGluePoint(sal_uInt16 nId) const336 sal_uInt16 SdrGluePointList::FindGluePoint(sal_uInt16 nId) const
337 {
338 	// TODO: Hier noch einen optimaleren Suchalgorithmus implementieren.
339 	// Die Liste sollte stets sortiert sein!
340 	sal_uInt16 nAnz=GetCount();
341 	sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
342 	for (sal_uInt16 nNum=0; nNum<nAnz && nRet==SDRGLUEPOINT_NOTFOUND; nNum++) {
343 		const SdrGluePoint* pGP=GetObject(nNum);
344 		if (pGP->GetId()==nId) nRet=nNum;
345 	}
346 	return nRet;
347 }
348 
HitTest(const Point & rPnt,const OutputDevice & rOut,const SdrObject * pObj,FASTBOOL bBack,FASTBOOL bNext,sal_uInt16 nId0) const349 sal_uInt16 SdrGluePointList::HitTest(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj, FASTBOOL bBack, FASTBOOL bNext, sal_uInt16 nId0) const
350 {
351 	sal_uInt16 nAnz=GetCount();
352 	sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
353 	sal_uInt16 nNum=bBack ? 0 : nAnz;
354 	while ((bBack ? nNum<nAnz : nNum>0) && nRet==SDRGLUEPOINT_NOTFOUND) {
355 		if (!bBack) nNum--;
356 		const SdrGluePoint* pGP=GetObject(nNum);
357 		if (bNext) {
358 			if (pGP->GetId()==nId0) bNext=sal_False;
359 		} else {
360 			if (pGP->IsHit(rPnt,rOut,pObj)) nRet=nNum;
361 		}
362 		if (bBack) nNum++;
363 	}
364 	return nRet;
365 }
366 
SetReallyAbsolute(FASTBOOL bOn,const SdrObject & rObj)367 void SdrGluePointList::SetReallyAbsolute(FASTBOOL bOn, const SdrObject& rObj)
368 {
369 	sal_uInt16 nAnz=GetCount();
370 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
371 		GetObject(nNum)->SetReallyAbsolute(bOn,rObj);
372 	}
373 }
374 
Rotate(const Point & rRef,long nWink,double sn,double cs,const SdrObject * pObj)375 void SdrGluePointList::Rotate(const Point& rRef, long nWink, double sn, double cs, const SdrObject* pObj)
376 {
377 	sal_uInt16 nAnz=GetCount();
378 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
379 		GetObject(nNum)->Rotate(rRef,nWink,sn,cs,pObj);
380 	}
381 }
382 
Mirror(const Point & rRef1,const Point & rRef2,const SdrObject * pObj)383 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, const SdrObject* pObj)
384 {
385 	Point aPt(rRef2); aPt-=rRef1;
386 	long nWink=GetAngle(aPt);
387 	Mirror(rRef1,rRef2,nWink,pObj);
388 }
389 
Mirror(const Point & rRef1,const Point & rRef2,long nWink,const SdrObject * pObj)390 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, long nWink, const SdrObject* pObj)
391 {
392 	sal_uInt16 nAnz=GetCount();
393 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
394 		GetObject(nNum)->Mirror(rRef1,rRef2,nWink,pObj);
395 	}
396 }
397 
Shear(const Point & rRef,long nWink,double tn,FASTBOOL bVShear,const SdrObject * pObj)398 void SdrGluePointList::Shear(const Point& rRef, long nWink, double tn, FASTBOOL bVShear, const SdrObject* pObj)
399 {
400 	sal_uInt16 nAnz=GetCount();
401 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
402 		GetObject(nNum)->Shear(rRef,nWink,tn,bVShear,pObj);
403 	}
404 }
405 
406 /* vim: set noet sw=4 ts=4: */
407