Adjust includes
[LibreOffice.git] / svx / source / svdraw / svdglev.cxx
blob86f3212841b16161dc4da4ad82e31f05a41fcc28
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <svx/svdglev.hxx>
22 #include <math.h>
24 #include <svx/svdundo.hxx>
25 #include <svx/strings.hrc>
26 #include <svdglob.hxx>
27 #include <svx/svdpagv.hxx>
28 #include <svx/svdglue.hxx>
29 #include <svx/svdtrans.hxx>
30 #include <svx/svdobj.hxx>
33 SdrGlueEditView::SdrGlueEditView(SdrModel* pModel1, OutputDevice* pOut):
34 SdrPolyEditView(pModel1,pOut)
38 SdrGlueEditView::~SdrGlueEditView()
43 void SdrGlueEditView::ImpDoMarkedGluePoints(PGlueDoFunc pDoFunc, bool bConst, const void* p1, const void* p2, const void* p3, const void* p4)
45 const size_t nMarkCount=GetMarkedObjectCount();
46 for (size_t nm=0; nm<nMarkCount; ++nm) {
47 SdrMark* pM=GetSdrMarkByIndex(nm);
48 SdrObject* pObj=pM->GetMarkedSdrObj();
49 const SdrUShortCont& rPts = pM->GetMarkedGluePoints();
50 if (!rPts.empty())
52 SdrGluePointList* pGPL=nullptr;
53 if (bConst) {
54 const SdrGluePointList* pConstGPL=pObj->GetGluePointList();
55 pGPL=const_cast<SdrGluePointList*>(pConstGPL);
56 } else {
57 pGPL=pObj->ForceGluePointList();
59 if (pGPL!=nullptr)
61 if(!bConst && IsUndoEnabled() )
62 AddUndo(GetModel()->GetSdrUndoFactory().CreateUndoGeoObject(*pObj));
64 for(SdrUShortCont::const_iterator it = rPts.begin(); it != rPts.end(); ++it)
66 sal_uInt16 nPtId=*it;
67 sal_uInt16 nGlueIdx=pGPL->FindGluePoint(nPtId);
68 if (nGlueIdx!=SDRGLUEPOINT_NOTFOUND)
70 SdrGluePoint& rGP=(*pGPL)[nGlueIdx];
71 (*pDoFunc)(rGP,pObj,p1,p2,p3,p4);
74 if (!bConst)
76 pObj->SetChanged();
77 pObj->BroadcastObjectChange();
82 if (!bConst && nMarkCount!=0) mpModel->SetChanged();
86 static void ImpGetEscDir(SdrGluePoint & rGP, const SdrObject* /*pObj*/, const void* pbFirst, const void* pnThisEsc, const void* pnRet, const void*)
88 TriState& nRet=*const_cast<TriState *>(static_cast<TriState const *>(pnRet));
89 if (nRet!=TRISTATE_INDET) {
90 SdrEscapeDirection nEsc = rGP.GetEscDir();
91 bool bOn = bool(nEsc & *static_cast<SdrEscapeDirection const *>(pnThisEsc));
92 bool& bFirst=*const_cast<bool *>(static_cast<bool const *>(pbFirst));
93 if (bFirst) {
94 nRet = bOn ? TRISTATE_TRUE : TRISTATE_FALSE;
95 bFirst = false;
97 else if (nRet != (bOn ? TRISTATE_TRUE : TRISTATE_FALSE)) nRet=TRISTATE_INDET;
101 TriState SdrGlueEditView::IsMarkedGluePointsEscDir(SdrEscapeDirection nThisEsc) const
103 ForceUndirtyMrkPnt();
104 bool bFirst=true;
105 TriState nRet=TRISTATE_FALSE;
106 const_cast<SdrGlueEditView*>(this)->ImpDoMarkedGluePoints(ImpGetEscDir,true,&bFirst,&nThisEsc,&nRet);
107 return nRet;
110 static void ImpSetEscDir(SdrGluePoint& rGP, const SdrObject* /*pObj*/, const void* pnThisEsc, const void* pbOn, const void*, const void*)
112 SdrEscapeDirection nEsc=rGP.GetEscDir();
113 if (*static_cast<bool const *>(pbOn))
114 nEsc |= *static_cast<SdrEscapeDirection const *>(pnThisEsc);
115 else
116 nEsc &= ~*static_cast<SdrEscapeDirection const *>(pnThisEsc);
117 rGP.SetEscDir(nEsc);
120 void SdrGlueEditView::SetMarkedGluePointsEscDir(SdrEscapeDirection nThisEsc, bool bOn)
122 ForceUndirtyMrkPnt();
123 BegUndo(ImpGetResStr(STR_EditSetGlueEscDir),GetDescriptionOfMarkedGluePoints());
124 ImpDoMarkedGluePoints(ImpSetEscDir,false,&nThisEsc,&bOn);
125 EndUndo();
129 static void ImpGetPercent(SdrGluePoint & rGP, const SdrObject* /*pObj*/, const void* pbFirst, const void* pnRet, const void*, const void*)
131 TriState& nRet=*const_cast<TriState *>(static_cast<TriState const *>(pnRet));
132 if (nRet!=TRISTATE_INDET) {
133 bool bOn=rGP.IsPercent();
134 bool& bFirst=*const_cast<bool *>(static_cast<bool const *>(pbFirst));
135 if (bFirst) {
136 nRet = bOn ? TRISTATE_TRUE : TRISTATE_FALSE;
137 bFirst = false;
139 else if ((nRet!=TRISTATE_FALSE)!=bOn)
140 nRet=TRISTATE_INDET;
144 TriState SdrGlueEditView::IsMarkedGluePointsPercent() const
146 ForceUndirtyMrkPnt();
147 bool bFirst=true;
148 TriState nRet = TRISTATE_TRUE;
149 const_cast<SdrGlueEditView*>(this)->ImpDoMarkedGluePoints(ImpGetPercent,true,&bFirst,&nRet);
150 return nRet;
153 static void ImpSetPercent(SdrGluePoint& rGP, const SdrObject* pObj, const void* pbOn, const void*, const void*, const void*)
155 Point aPos(rGP.GetAbsolutePos(*pObj));
156 rGP.SetPercent(*static_cast<bool const *>(pbOn));
157 rGP.SetAbsolutePos(aPos,*pObj);
160 void SdrGlueEditView::SetMarkedGluePointsPercent(bool bOn)
162 ForceUndirtyMrkPnt();
163 BegUndo(ImpGetResStr(STR_EditSetGluePercent),GetDescriptionOfMarkedGluePoints());
164 ImpDoMarkedGluePoints(ImpSetPercent,false,&bOn);
165 EndUndo();
169 static void ImpGetAlign(SdrGluePoint & rGP, const SdrObject* /*pObj*/, const void* pbFirst, const void* pbDontCare, const void* pbVert, const void* pnRet)
171 SdrAlign& nRet=*const_cast<SdrAlign *>(static_cast<SdrAlign const *>(pnRet));
172 bool& bDontCare=*const_cast<bool *>(static_cast<bool const *>(pbDontCare));
173 bool bVert=*static_cast<bool const *>(pbVert);
174 if (!bDontCare) {
175 SdrAlign nAlg=SdrAlign::NONE;
176 if (bVert) {
177 nAlg=rGP.GetVertAlign();
178 } else {
179 nAlg=rGP.GetHorzAlign();
181 bool& bFirst=*const_cast<bool *>(static_cast<bool const *>(pbFirst));
182 if (bFirst) { nRet=nAlg; bFirst=false; }
183 else if (nRet!=nAlg) {
184 if (bVert) {
185 nRet=SdrAlign::VERT_DONTCARE;
186 } else {
187 nRet=SdrAlign::HORZ_DONTCARE;
189 bDontCare=true;
194 SdrAlign SdrGlueEditView::GetMarkedGluePointsAlign(bool bVert) const
196 ForceUndirtyMrkPnt();
197 bool bFirst=true;
198 bool bDontCare=false;
199 SdrAlign nRet=SdrAlign::NONE;
200 const_cast<SdrGlueEditView*>(this)->ImpDoMarkedGluePoints(ImpGetAlign,true,&bFirst,&bDontCare,&bVert,&nRet);
201 return nRet;
204 static void ImpSetAlign(SdrGluePoint& rGP, const SdrObject* pObj, const void* pbVert, const void* pnAlign, const void*, const void*)
206 Point aPos(rGP.GetAbsolutePos(*pObj));
207 if (*static_cast<bool const *>(pbVert)) { // bVert?
208 rGP.SetVertAlign(*static_cast<SdrAlign const *>(pnAlign));
209 } else {
210 rGP.SetHorzAlign(*static_cast<SdrAlign const *>(pnAlign));
212 rGP.SetAbsolutePos(aPos,*pObj);
215 void SdrGlueEditView::SetMarkedGluePointsAlign(bool bVert, SdrAlign nAlign)
217 ForceUndirtyMrkPnt();
218 BegUndo(ImpGetResStr(STR_EditSetGlueAlign),GetDescriptionOfMarkedGluePoints());
219 ImpDoMarkedGluePoints(ImpSetAlign,false,&bVert,&nAlign);
220 EndUndo();
223 void SdrGlueEditView::DeleteMarkedGluePoints()
225 BrkAction();
226 ForceUndirtyMrkPnt();
227 const bool bUndo = IsUndoEnabled();
228 if( bUndo )
229 BegUndo(ImpGetResStr(STR_EditDelete),GetDescriptionOfMarkedGluePoints(),SdrRepeatFunc::Delete);
231 const size_t nMarkCount=GetMarkedObjectCount();
232 for (size_t nm=0; nm<nMarkCount; ++nm)
234 SdrMark* pM=GetSdrMarkByIndex(nm);
235 SdrObject* pObj=pM->GetMarkedSdrObj();
236 const SdrUShortCont& rPts = pM->GetMarkedGluePoints();
237 if (!rPts.empty())
239 SdrGluePointList* pGPL=pObj->ForceGluePointList();
240 if (pGPL!=nullptr)
242 if( bUndo )
243 AddUndo(GetModel()->GetSdrUndoFactory().CreateUndoGeoObject(*pObj));
245 for(SdrUShortCont::const_iterator it = rPts.begin(); it != rPts.end(); ++it)
247 sal_uInt16 nPtId=*it;
248 sal_uInt16 nGlueIdx=pGPL->FindGluePoint(nPtId);
249 if (nGlueIdx!=SDRGLUEPOINT_NOTFOUND)
251 pGPL->Delete(nGlueIdx);
254 pObj->SetChanged();
255 pObj->BroadcastObjectChange();
259 if( bUndo )
260 EndUndo();
261 UnmarkAllGluePoints();
262 if (nMarkCount!=0)
263 mpModel->SetChanged();
267 void SdrGlueEditView::ImpCopyMarkedGluePoints()
269 const bool bUndo = IsUndoEnabled();
271 if( bUndo )
272 BegUndo();
274 const size_t nMarkCount=GetMarkedObjectCount();
275 for (size_t nm=0; nm<nMarkCount; ++nm)
277 SdrMark* pM=GetSdrMarkByIndex(nm);
278 SdrObject* pObj=pM->GetMarkedSdrObj();
279 SdrUShortCont& rPts = pM->GetMarkedGluePoints();
280 SdrGluePointList* pGPL=pObj->ForceGluePointList();
281 if (!rPts.empty() && pGPL!=nullptr)
283 if( bUndo )
284 AddUndo(GetModel()->GetSdrUndoFactory().CreateUndoGeoObject(*pObj));
286 SdrUShortCont aIdsToErase;
287 SdrUShortCont aIdsToInsert;
288 for(SdrUShortCont::const_iterator it = rPts.begin(); it != rPts.end(); ++it)
290 sal_uInt16 nPtId=*it;
291 sal_uInt16 nGlueIdx=pGPL->FindGluePoint(nPtId);
292 if (nGlueIdx!=SDRGLUEPOINT_NOTFOUND)
294 SdrGluePoint aNewGP((*pGPL)[nGlueIdx]); // clone GluePoint
295 sal_uInt16 nNewIdx=pGPL->Insert(aNewGP); // and insert it
296 sal_uInt16 nNewId=(*pGPL)[nNewIdx].GetId(); // retrieve ID of new GluePoints
297 aIdsToErase.insert(nPtId); // select it (instead of the old one)
298 aIdsToInsert.insert(nNewId);
301 for(SdrUShortCont::const_iterator it = aIdsToErase.begin(); it != aIdsToErase.end(); ++it)
302 rPts.erase(*it);
303 rPts.insert(aIdsToInsert.begin(), aIdsToInsert.end());
306 if( bUndo )
307 EndUndo();
309 if (nMarkCount!=0)
310 mpModel->SetChanged();
314 void SdrGlueEditView::ImpTransformMarkedGluePoints(PGlueTrFunc pTrFunc, const void* p1, const void* p2, const void* p3, const void* p4)
316 const size_t nMarkCount=GetMarkedObjectCount();
317 for (size_t nm=0; nm<nMarkCount; ++nm) {
318 SdrMark* pM=GetSdrMarkByIndex(nm);
319 SdrObject* pObj=pM->GetMarkedSdrObj();
320 const SdrUShortCont& rPts = pM->GetMarkedGluePoints();
321 if (!rPts.empty()) {
322 SdrGluePointList* pGPL=pObj->ForceGluePointList();
323 if (pGPL!=nullptr)
325 if( IsUndoEnabled() )
326 AddUndo(GetModel()->GetSdrUndoFactory().CreateUndoGeoObject(*pObj));
328 for(SdrUShortCont::const_iterator it = rPts.begin(); it != rPts.end(); ++it)
330 sal_uInt16 nPtId=*it;
331 sal_uInt16 nGlueIdx=pGPL->FindGluePoint(nPtId);
332 if (nGlueIdx!=SDRGLUEPOINT_NOTFOUND) {
333 SdrGluePoint& rGP=(*pGPL)[nGlueIdx];
334 Point aPos(rGP.GetAbsolutePos(*pObj));
335 (*pTrFunc)(aPos,p1,p2,p3,p4);
336 rGP.SetAbsolutePos(aPos,*pObj);
339 pObj->SetChanged();
340 pObj->BroadcastObjectChange();
344 if (nMarkCount!=0) mpModel->SetChanged();
348 static void ImpMove(Point& rPt, const void* p1, const void* /*p2*/, const void* /*p3*/, const void* /*p4*/)
350 rPt.X()+=static_cast<const Size*>(p1)->Width();
351 rPt.Y()+=static_cast<const Size*>(p1)->Height();
354 void SdrGlueEditView::MoveMarkedGluePoints(const Size& rSiz, bool bCopy)
356 ForceUndirtyMrkPnt();
357 OUString aStr(ImpGetResStr(STR_EditMove));
358 if (bCopy) aStr += ImpGetResStr(STR_EditWithCopy);
359 BegUndo(aStr,GetDescriptionOfMarkedGluePoints(),SdrRepeatFunc::Move);
360 if (bCopy) ImpCopyMarkedGluePoints();
361 ImpTransformMarkedGluePoints(ImpMove,&rSiz);
362 EndUndo();
363 AdjustMarkHdl();
367 static void ImpResize(Point& rPt, const void* p1, const void* p2, const void* p3, const void* /*p4*/)
369 ResizePoint(rPt,*static_cast<const Point*>(p1),*static_cast<const Fraction*>(p2),*static_cast<const Fraction*>(p3));
372 void SdrGlueEditView::ResizeMarkedGluePoints(const Point& rRef, const Fraction& xFact, const Fraction& yFact, bool bCopy)
374 ForceUndirtyMrkPnt();
375 OUString aStr(ImpGetResStr(STR_EditResize));
376 if (bCopy) aStr+=ImpGetResStr(STR_EditWithCopy);
377 BegUndo(aStr,GetDescriptionOfMarkedGluePoints(),SdrRepeatFunc::Resize);
378 if (bCopy) ImpCopyMarkedGluePoints();
379 ImpTransformMarkedGluePoints(ImpResize,&rRef,&xFact,&yFact);
380 EndUndo();
381 AdjustMarkHdl();
385 static void ImpRotate(Point& rPt, const void* p1, const void* /*p2*/, const void* p3, const void* p4)
387 RotatePoint(rPt,*static_cast<const Point*>(p1),*static_cast<const double*>(p3),*static_cast<const double*>(p4));
390 void SdrGlueEditView::RotateMarkedGluePoints(const Point& rRef, long nAngle, bool bCopy)
392 ForceUndirtyMrkPnt();
393 OUString aStr(ImpGetResStr(STR_EditRotate));
394 if (bCopy) aStr+=ImpGetResStr(STR_EditWithCopy);
395 BegUndo(aStr,GetDescriptionOfMarkedGluePoints(),SdrRepeatFunc::Rotate);
396 if (bCopy) ImpCopyMarkedGluePoints();
397 double nSin=sin(nAngle*nPi180);
398 double nCos=cos(nAngle*nPi180);
399 ImpTransformMarkedGluePoints(ImpRotate,&rRef,&nAngle,&nSin,&nCos);
400 EndUndo();
401 AdjustMarkHdl();
404 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */