xref: /AOO41X/main/svx/source/svdraw/svdglue.cxx (revision c42e87e31ef440175195aacd42fdea1e272b3aef)
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 
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 
51 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 
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 
128 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 
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 
157 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 
168 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 
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 
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 
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 
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 
250 // Unused code?!
251 void SdrGluePoint::Draw(OutputDevice& rOut, const SdrObject* pObj) const
252 {
253 	Color aBackPenColor(COL_WHITE);
254 	Color aForePenColor(COL_LIGHTBLUE);
255 
256 	bool bMapMerk=rOut.IsMapModeEnabled();
257 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
258 	aPt=rOut.LogicToPixel(aPt);
259 	rOut.EnableMapMode(sal_False);
260 	long x=aPt.X(),y=aPt.Y(); // Groesse erstmal fest auf 7 Pixel
261 
262 	rOut.SetLineColor( aBackPenColor );
263 	rOut.DrawLine(Point(x-2,y-3),Point(x+3,y+2));
264 	rOut.DrawLine(Point(x-3,y-2),Point(x+2,y+3));
265 	rOut.DrawLine(Point(x-3,y+2),Point(x+2,y-3));
266 	rOut.DrawLine(Point(x-2,y+3),Point(x+3,y-2));
267 
268 	if (bNoPercent)
269 	{
270 		switch (GetHorzAlign())
271 		{
272 			case SDRHORZALIGN_LEFT  : rOut.DrawLine(Point(x-3,y-1),Point(x-3,y+1)); break;
273 			case SDRHORZALIGN_RIGHT : rOut.DrawLine(Point(x+3,y-1),Point(x+3,y+1)); break;
274 		}
275 
276 		switch (GetVertAlign())
277 		{
278 			case SDRVERTALIGN_TOP   : rOut.DrawLine(Point(x-1,y-3),Point(x+1,y-3)); break;
279 			case SDRVERTALIGN_BOTTOM: rOut.DrawLine(Point(x-1,y+3),Point(x+1,y+3)); break;
280 		}
281 	}
282 
283 	rOut.SetLineColor( aForePenColor );
284 	rOut.DrawLine(Point(x-2,y-2),Point(x+2,y+2));
285 	rOut.DrawLine(Point(x-2,y+2),Point(x+2,y-2));
286 	rOut.EnableMapMode(bMapMerk);
287 }
288 
289 void SdrGluePoint::Invalidate(Window& rWin, const SdrObject* pObj) const
290 {
291 	bool bMapMerk=rWin.IsMapModeEnabled();
292 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
293 	aPt=rWin.LogicToPixel(aPt);
294 	rWin.EnableMapMode(sal_False);
295 	long x=aPt.X(),y=aPt.Y(); // size 9x9 pixel
296 
297 	// #111096#
298 	// do not erase background, that causes flicker (!)
299 	rWin.Invalidate(Rectangle(Point(x-4,y-4),Point(x+4,y+4)), INVALIDATE_NOERASE);
300 
301 	rWin.EnableMapMode(bMapMerk);
302 }
303 
304 FASTBOOL SdrGluePoint::IsHit(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj) const
305 {
306 	Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
307 	Size aSiz=rOut.PixelToLogic(Size(4,4));
308 	Rectangle aRect(aPt.X()-aSiz.Width(),aPt.Y()-aSiz.Height(),aPt.X()+aSiz.Width(),aPt.Y()+aSiz.Height());
309 	return aRect.IsInside(rPnt);
310 }
311 
312 
313 void SdrGluePointList::Clear()
314 {
315 	sal_uInt16 nAnz=GetCount();
316 	for (sal_uInt16 i=0; i<nAnz; i++) {
317 		delete GetObject(i);
318 	}
319 	aList.Clear();
320 }
321 
322 void SdrGluePointList::operator=(const SdrGluePointList& rSrcList)
323 {
324 	if (GetCount()!=0) Clear();
325 	sal_uInt16 nAnz=rSrcList.GetCount();
326 	for (sal_uInt16 i=0; i<nAnz; i++) {
327 		Insert(rSrcList[i]);
328 	}
329 }
330 
331 // Die Id's der Klebepunkte in der Liste sind stets streng monoton steigend!
332 // Ggf. wird dem neuen Klebepunkt eine neue Id zugewiesen (wenn diese bereits
333 // vergeben ist). Die Id 0 ist reserviert.
334 sal_uInt16 SdrGluePointList::Insert(const SdrGluePoint& rGP)
335 {
336 	SdrGluePoint* pGP=new SdrGluePoint(rGP);
337 	sal_uInt16 nId=pGP->GetId();
338 	sal_uInt16 nAnz=GetCount();
339 	sal_uInt16 nInsPos=nAnz;
340 	sal_uInt16 nLastId=nAnz!=0 ? GetObject(nAnz-1)->GetId() : 0;
341 	DBG_ASSERT(nLastId>=nAnz,"SdrGluePointList::Insert(): nLastId<nAnz");
342 	FASTBOOL bHole=nLastId>nAnz;
343 	if (nId<=nLastId) {
344 		if (!bHole || nId==0) {
345 			nId=nLastId+1;
346 		} else {
347 			FASTBOOL bBrk=sal_False;
348 			for (sal_uInt16 nNum=0; nNum<nAnz && !bBrk; nNum++) {
349 				const SdrGluePoint* pGP2=GetObject(nNum);
350 				sal_uInt16 nTmpId=pGP2->GetId();
351 				if (nTmpId==nId) {
352 					nId=nLastId+1; // bereits vorhanden
353 					bBrk=sal_True;
354 				}
355 				if (nTmpId>nId) {
356 					nInsPos=nNum; // Hier einfuegen (einsortieren)
357 					bBrk=sal_True;
358 				}
359 			}
360 		}
361 		pGP->SetId(nId);
362 	}
363 	aList.Insert(pGP,nInsPos);
364 	return nInsPos;
365 }
366 
367 void SdrGluePointList::Invalidate(Window& rWin, const SdrObject* pObj) const
368 {
369 	sal_uInt16 nAnz=GetCount();
370 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
371 		GetObject(nNum)->Invalidate(rWin,pObj);
372 	}
373 }
374 
375 sal_uInt16 SdrGluePointList::FindGluePoint(sal_uInt16 nId) const
376 {
377 	// TODO: Hier noch einen optimaleren Suchalgorithmus implementieren.
378 	// Die Liste sollte stets sortiert sein!
379 	sal_uInt16 nAnz=GetCount();
380 	sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
381 	for (sal_uInt16 nNum=0; nNum<nAnz && nRet==SDRGLUEPOINT_NOTFOUND; nNum++) {
382 		const SdrGluePoint* pGP=GetObject(nNum);
383 		if (pGP->GetId()==nId) nRet=nNum;
384 	}
385 	return nRet;
386 }
387 
388 sal_uInt16 SdrGluePointList::HitTest(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj, FASTBOOL bBack, FASTBOOL bNext, sal_uInt16 nId0) const
389 {
390 	sal_uInt16 nAnz=GetCount();
391 	sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
392 	sal_uInt16 nNum=bBack ? 0 : nAnz;
393 	while ((bBack ? nNum<nAnz : nNum>0) && nRet==SDRGLUEPOINT_NOTFOUND) {
394 		if (!bBack) nNum--;
395 		const SdrGluePoint* pGP=GetObject(nNum);
396 		if (bNext) {
397 			if (pGP->GetId()==nId0) bNext=sal_False;
398 		} else {
399 			if (pGP->IsHit(rPnt,rOut,pObj)) nRet=nNum;
400 		}
401 		if (bBack) nNum++;
402 	}
403 	return nRet;
404 }
405 
406 void SdrGluePointList::SetReallyAbsolute(FASTBOOL bOn, const SdrObject& rObj)
407 {
408 	sal_uInt16 nAnz=GetCount();
409 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
410 		GetObject(nNum)->SetReallyAbsolute(bOn,rObj);
411 	}
412 }
413 
414 void SdrGluePointList::Rotate(const Point& rRef, long nWink, double sn, double cs, const SdrObject* pObj)
415 {
416 	sal_uInt16 nAnz=GetCount();
417 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
418 		GetObject(nNum)->Rotate(rRef,nWink,sn,cs,pObj);
419 	}
420 }
421 
422 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, const SdrObject* pObj)
423 {
424 	Point aPt(rRef2); aPt-=rRef1;
425 	long nWink=GetAngle(aPt);
426 	Mirror(rRef1,rRef2,nWink,pObj);
427 }
428 
429 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, long nWink, const SdrObject* pObj)
430 {
431 	sal_uInt16 nAnz=GetCount();
432 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
433 		GetObject(nNum)->Mirror(rRef1,rRef2,nWink,pObj);
434 	}
435 }
436 
437 void SdrGluePointList::Shear(const Point& rRef, long nWink, double tn, FASTBOOL bVShear, const SdrObject* pObj)
438 {
439 	sal_uInt16 nAnz=GetCount();
440 	for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
441 		GetObject(nNum)->Shear(rRef,nWink,tn,bVShear,pObj);
442 	}
443 }
444 
445 /* vim: set noet sw=4 ts=4: */
446