xref: /AOO41X/main/svx/source/svdraw/svdglue.cxx (revision 8809db7a87f97847b57a57f4cd2b0104b2b83182)
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 
34 void SdrGluePoint::SetReallyAbsolute(FASTBOOL bOn, const SdrObject& rObj)
35 {
36     if ( bReallyAbsolute != bOn )
37     {
38        if ( bOn )
39        {
40            aPos=GetAbsolutePos(rObj);
41            bReallyAbsolute=bOn;
42        }
43        else
44        {
45            bReallyAbsolute=bOn;
46            Point aPt(aPos);
47            SetAbsolutePos(aPt,rObj);
48        }
49     }
50 }
51 
52 Point SdrGluePoint::GetAbsolutePos(const SdrObject& rObj) const
53 {
54     if (bReallyAbsolute) return aPos;
55     Rectangle aSnap(rObj.GetSnapRect());
56     Rectangle aBound(rObj.GetSnapRect());
57     Point aPt(aPos);
58 
59     Point aOfs(aSnap.Center());
60     switch (GetHorzAlign()) {
61         case SDRHORZALIGN_LEFT  : aOfs.X()=aSnap.Left(); break;
62         case SDRHORZALIGN_RIGHT : aOfs.X()=aSnap.Right(); break;
63     }
64     switch (GetVertAlign()) {
65         case SDRVERTALIGN_TOP   : aOfs.Y()=aSnap.Top(); break;
66         case SDRVERTALIGN_BOTTOM: aOfs.Y()=aSnap.Bottom(); break;
67     }
68     if (!bNoPercent) {
69         long nXMul=aSnap.Right()-aSnap.Left();
70         long nYMul=aSnap.Bottom()-aSnap.Top();
71         long nXDiv=10000;
72         long nYDiv=10000;
73         if (nXMul!=nXDiv) {
74             aPt.X()*=nXMul;
75             aPt.X()/=nXDiv;
76         }
77         if (nYMul!=nYDiv) {
78             aPt.Y()*=nYMul;
79             aPt.Y()/=nYDiv;
80         }
81     }
82     aPt+=aOfs;
83     // Und nun auf's BoundRect des Objekts begrenzen
84     if (aPt.X()<aBound.Left  ()) aPt.X()=aBound.Left  ();
85     if (aPt.X()>aBound.Right ()) aPt.X()=aBound.Right ();
86     if (aPt.Y()<aBound.Top   ()) aPt.Y()=aBound.Top   ();
87     if (aPt.Y()>aBound.Bottom()) aPt.Y()=aBound.Bottom();
88     return aPt;
89 }
90 
91 void SdrGluePoint::SetAbsolutePos(const Point& rNewPos, const SdrObject& rObj)
92 {
93     if (bReallyAbsolute) {
94         aPos=rNewPos;
95         return;
96     }
97     Rectangle aSnap(rObj.GetSnapRect());
98     Point aPt(rNewPos);
99 
100     Point aOfs(aSnap.Center());
101     switch (GetHorzAlign()) {
102         case SDRHORZALIGN_LEFT  : aOfs.X()=aSnap.Left(); break;
103         case SDRHORZALIGN_RIGHT : aOfs.X()=aSnap.Right(); break;
104     }
105     switch (GetVertAlign()) {
106         case SDRVERTALIGN_TOP   : aOfs.Y()=aSnap.Top(); break;
107         case SDRVERTALIGN_BOTTOM: aOfs.Y()=aSnap.Bottom(); break;
108     }
109     aPt-=aOfs;
110     if (!bNoPercent) {
111         long nXMul=aSnap.Right()-aSnap.Left();
112         long nYMul=aSnap.Bottom()-aSnap.Top();
113         if (nXMul==0) nXMul=1;
114         if (nYMul==0) nYMul=1;
115         long nXDiv=10000;
116         long nYDiv=10000;
117         if (nXMul!=nXDiv) {
118             aPt.X()*=nXDiv;
119             aPt.X()/=nXMul;
120         }
121         if (nYMul!=nYDiv) {
122             aPt.Y()*=nYDiv;
123             aPt.Y()/=nYMul;
124         }
125     }
126     aPos=aPt;
127 }
128 
129 long SdrGluePoint::GetAlignAngle() const
130 {
131     switch (nAlign) {
132         case SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER: return 0; // Invalid!
133         case SDRHORZALIGN_RIGHT |SDRVERTALIGN_CENTER: return 0;
134         case SDRHORZALIGN_RIGHT |SDRVERTALIGN_TOP   : return 4500;
135         case SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP   : return 9000;
136         case SDRHORZALIGN_LEFT  |SDRVERTALIGN_TOP   : return 13500;
137         case SDRHORZALIGN_LEFT  |SDRVERTALIGN_CENTER: return 18000;
138         case SDRHORZALIGN_LEFT  |SDRVERTALIGN_BOTTOM: return 22500;
139         case SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM: return 27000;
140         case SDRHORZALIGN_RIGHT |SDRVERTALIGN_BOTTOM: return 31500;
141     } // switch
142     return 0;
143 }
144 
145 void SdrGluePoint::SetAlignAngle(long nWink)
146 {
147     nWink=NormAngle360(nWink);
148     if (nWink>=33750 || nWink<2250) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_CENTER;
149     else if (nWink< 6750) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_TOP   ;
150     else if (nWink<11250) nAlign=SDRHORZALIGN_CENTER|SDRVERTALIGN_TOP   ;
151     else if (nWink<15750) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_TOP   ;
152     else if (nWink<20250) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_CENTER;
153     else if (nWink<24750) nAlign=SDRHORZALIGN_LEFT  |SDRVERTALIGN_BOTTOM;
154     else if (nWink<29250) nAlign=SDRHORZALIGN_CENTER|SDRVERTALIGN_BOTTOM;
155     else if (nWink<33750) nAlign=SDRHORZALIGN_RIGHT |SDRVERTALIGN_BOTTOM;
156 }
157 
158 long SdrGluePoint::EscDirToAngle(sal_uInt16 nEsc) const
159 {
160     switch (nEsc) {
161         case SDRESC_RIGHT : return 0;
162         case SDRESC_TOP   : return 9000;
163         case SDRESC_LEFT  : return 18000;
164         case SDRESC_BOTTOM: return 27000;
165     } // switch
166     return 0;
167 }
168 
169 sal_uInt16 SdrGluePoint::EscAngleToDir(long nWink) const
170 {
171     nWink=NormAngle360(nWink);
172     if (nWink>=31500 || nWink<4500) return SDRESC_RIGHT;
173     if (nWink<13500) return SDRESC_TOP;
174     if (nWink<22500) return SDRESC_LEFT;
175     if (nWink<31500) return SDRESC_BOTTOM;
176     return 0;
177 }
178 
179 void SdrGluePoint::Rotate(const Point& rRef, long nWink, double sn, double cs, const SdrObject* pObj)
180 {
181     Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
182     RotatePoint(aPt,rRef,sn,cs);
183     // Bezugskante drehen
184     if(nAlign != (SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER))
185     {
186         SetAlignAngle(GetAlignAngle()+nWink);
187     }
188     // Austrittsrichtungen drehen
189     sal_uInt16 nEscDir0=nEscDir;
190     sal_uInt16 nEscDir1=0;
191     if ((nEscDir0&SDRESC_LEFT  )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_LEFT  )+nWink);
192     if ((nEscDir0&SDRESC_TOP   )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_TOP   )+nWink);
193     if ((nEscDir0&SDRESC_RIGHT )!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_RIGHT )+nWink);
194     if ((nEscDir0&SDRESC_BOTTOM)!=0) nEscDir1|=EscAngleToDir(EscDirToAngle(SDRESC_BOTTOM)+nWink);
195     nEscDir=nEscDir1;
196     if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
197 }
198 
199 void SdrGluePoint::Mirror(const Point& rRef1, const Point& rRef2, const SdrObject* pObj)
200 {
201     Point aPt(rRef2); aPt-=rRef1;
202     long nWink=GetAngle(aPt);
203     Mirror(rRef1,rRef2,nWink,pObj);
204 }
205 
206 void SdrGluePoint::Mirror(const Point& rRef1, const Point& rRef2, long nWink, const SdrObject* pObj)
207 {
208     Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
209     MirrorPoint(aPt,rRef1,rRef2);
210     // Bezugskante spiegeln
211     if(nAlign != (SDRHORZALIGN_CENTER|SDRVERTALIGN_CENTER))
212     {
213         long nAW=GetAlignAngle();
214         nAW+=2*(nWink-nAW);
215         SetAlignAngle(nAW);
216     }
217     // Austrittsrichtungen spiegeln
218     sal_uInt16 nEscDir0=nEscDir;
219     sal_uInt16 nEscDir1=0;
220     if ((nEscDir0&SDRESC_LEFT)!=0) {
221         long nEW=EscDirToAngle(SDRESC_LEFT);
222         nEW+=2*(nWink-nEW);
223         nEscDir1|=EscAngleToDir(nEW);
224     }
225     if ((nEscDir0&SDRESC_TOP)!=0) {
226         long nEW=EscDirToAngle(SDRESC_TOP);
227         nEW+=2*(nWink-nEW);
228         nEscDir1|=EscAngleToDir(nEW);
229     }
230     if ((nEscDir0&SDRESC_RIGHT)!=0) {
231         long nEW=EscDirToAngle(SDRESC_RIGHT);
232         nEW+=2*(nWink-nEW);
233         nEscDir1|=EscAngleToDir(nEW);
234     }
235     if ((nEscDir0&SDRESC_BOTTOM)!=0) {
236         long nEW=EscDirToAngle(SDRESC_BOTTOM);
237         nEW+=2*(nWink-nEW);
238         nEscDir1|=EscAngleToDir(nEW);
239     }
240     nEscDir=nEscDir1;
241     if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
242 }
243 
244 void SdrGluePoint::Shear(const Point& rRef, long /*nWink*/, double tn, FASTBOOL bVShear, const SdrObject* pObj)
245 {
246     Point aPt(pObj!=NULL ? GetAbsolutePos(*pObj) : GetPos());
247     ShearPoint(aPt,rRef,tn,bVShear);
248     if (pObj!=NULL) SetAbsolutePos(aPt,*pObj); else SetPos(aPt);
249 }
250 
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(); // Groesse erstmal fest auf 7 Pixel
296 
297     // #111096#
298     // do not erase background, that causes flicker (!)
299     rWin.Invalidate(Rectangle(Point(x-3,y-3),Point(x+3,y+3)), 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(3,3));
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 
314 void SdrGluePointList::Clear()
315 {
316     sal_uInt16 nAnz=GetCount();
317     for (sal_uInt16 i=0; i<nAnz; i++) {
318         delete GetObject(i);
319     }
320     aList.Clear();
321 }
322 
323 void SdrGluePointList::operator=(const SdrGluePointList& rSrcList)
324 {
325     if (GetCount()!=0) Clear();
326     sal_uInt16 nAnz=rSrcList.GetCount();
327     for (sal_uInt16 i=0; i<nAnz; i++) {
328         Insert(rSrcList[i]);
329     }
330 }
331 
332 // Die Id's der Klebepunkte in der Liste sind stets streng monoton steigend!
333 // Ggf. wird dem neuen Klebepunkt eine neue Id zugewiesen (wenn diese bereits
334 // vergeben ist). Die Id 0 ist reserviert.
335 sal_uInt16 SdrGluePointList::Insert(const SdrGluePoint& rGP)
336 {
337     SdrGluePoint* pGP=new SdrGluePoint(rGP);
338     sal_uInt16 nId=pGP->GetId();
339     sal_uInt16 nAnz=GetCount();
340     sal_uInt16 nInsPos=nAnz;
341     sal_uInt16 nLastId=nAnz!=0 ? GetObject(nAnz-1)->GetId() : 0;
342     DBG_ASSERT(nLastId>=nAnz,"SdrGluePointList::Insert(): nLastId<nAnz");
343     FASTBOOL bHole=nLastId>nAnz;
344     if (nId<=nLastId) {
345         if (!bHole || nId==0) {
346             nId=nLastId+1;
347         } else {
348             FASTBOOL bBrk=sal_False;
349             for (sal_uInt16 nNum=0; nNum<nAnz && !bBrk; nNum++) {
350                 const SdrGluePoint* pGP2=GetObject(nNum);
351                 sal_uInt16 nTmpId=pGP2->GetId();
352                 if (nTmpId==nId) {
353                     nId=nLastId+1; // bereits vorhanden
354                     bBrk=sal_True;
355                 }
356                 if (nTmpId>nId) {
357                     nInsPos=nNum; // Hier einfuegen (einsortieren)
358                     bBrk=sal_True;
359                 }
360             }
361         }
362         pGP->SetId(nId);
363     }
364     aList.Insert(pGP,nInsPos);
365     return nInsPos;
366 }
367 
368 void SdrGluePointList::Invalidate(Window& rWin, const SdrObject* pObj) const
369 {
370     sal_uInt16 nAnz=GetCount();
371     for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
372         GetObject(nNum)->Invalidate(rWin,pObj);
373     }
374 }
375 
376 sal_uInt16 SdrGluePointList::FindGluePoint(sal_uInt16 nId) const
377 {
378     // Hier noch einen optimaleren Suchalgorithmus implementieren.
379     // Die Liste sollte stets sortiert sein!!!!
380     sal_uInt16 nAnz=GetCount();
381     sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
382     for (sal_uInt16 nNum=0; nNum<nAnz && nRet==SDRGLUEPOINT_NOTFOUND; nNum++) {
383         const SdrGluePoint* pGP=GetObject(nNum);
384         if (pGP->GetId()==nId) nRet=nNum;
385     }
386     return nRet;
387 }
388 
389 sal_uInt16 SdrGluePointList::HitTest(const Point& rPnt, const OutputDevice& rOut, const SdrObject* pObj, FASTBOOL bBack, FASTBOOL bNext, sal_uInt16 nId0) const
390 {
391     sal_uInt16 nAnz=GetCount();
392     sal_uInt16 nRet=SDRGLUEPOINT_NOTFOUND;
393     sal_uInt16 nNum=bBack ? 0 : nAnz;
394     while ((bBack ? nNum<nAnz : nNum>0) && nRet==SDRGLUEPOINT_NOTFOUND) {
395         if (!bBack) nNum--;
396         const SdrGluePoint* pGP=GetObject(nNum);
397         if (bNext) {
398             if (pGP->GetId()==nId0) bNext=sal_False;
399         } else {
400             if (pGP->IsHit(rPnt,rOut,pObj)) nRet=nNum;
401         }
402         if (bBack) nNum++;
403     }
404     return nRet;
405 }
406 
407 void SdrGluePointList::SetReallyAbsolute(FASTBOOL bOn, const SdrObject& rObj)
408 {
409     sal_uInt16 nAnz=GetCount();
410     for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
411         GetObject(nNum)->SetReallyAbsolute(bOn,rObj);
412     }
413 }
414 
415 void SdrGluePointList::Rotate(const Point& rRef, long nWink, double sn, double cs, const SdrObject* pObj)
416 {
417     sal_uInt16 nAnz=GetCount();
418     for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
419         GetObject(nNum)->Rotate(rRef,nWink,sn,cs,pObj);
420     }
421 }
422 
423 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, const SdrObject* pObj)
424 {
425     Point aPt(rRef2); aPt-=rRef1;
426     long nWink=GetAngle(aPt);
427     Mirror(rRef1,rRef2,nWink,pObj);
428 }
429 
430 void SdrGluePointList::Mirror(const Point& rRef1, const Point& rRef2, long nWink, const SdrObject* pObj)
431 {
432     sal_uInt16 nAnz=GetCount();
433     for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
434         GetObject(nNum)->Mirror(rRef1,rRef2,nWink,pObj);
435     }
436 }
437 
438 void SdrGluePointList::Shear(const Point& rRef, long nWink, double tn, FASTBOOL bVShear, const SdrObject* pObj)
439 {
440     sal_uInt16 nAnz=GetCount();
441     for (sal_uInt16 nNum=0; nNum<nAnz; nNum++) {
442         GetObject(nNum)->Shear(rRef,nWink,tn,bVShear,pObj);
443     }
444 }
445 
446 // eof
447