LibreOffice Module sw (master)  1
tolayoutanchoredobjectposition.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
21 #include <anchoredobject.hxx>
22 #include <frame.hxx>
23 #include <pagefrm.hxx>
24 #include <svx/svdobj.hxx>
25 #include <frmfmt.hxx>
26 #include <fmtanchr.hxx>
27 #include <fmtornt.hxx>
28 #include <fmtsrnd.hxx>
30 #include <frmatr.hxx>
31 #include <viewsh.hxx>
32 #include <viewopt.hxx>
33 #include <rootfrm.hxx>
34 #include <editeng/lrspitem.hxx>
35 #include <editeng/ulspitem.hxx>
36 
37 using namespace objectpositioning;
38 using namespace ::com::sun::star;
39 
41  : SwAnchoredObjectPosition( _rDrawObj ),
42  maRelPos( Point() ),
43  // #i26791#
44  maOffsetToFrameAnchorPos( Point() )
45 {}
46 
48 {}
49 
52 {
53  const SwRect aObjBoundRect( GetAnchoredObj().GetObjRect() );
54 
55  SwRectFnSet aRectFnSet(&GetAnchorFrame());
56 
57  const SwFrameFormat& rFrameFormat = GetFrameFormat();
58  const SvxLRSpaceItem &rLR = rFrameFormat.GetLRSpace();
59  const SvxULSpaceItem &rUL = rFrameFormat.GetULSpace();
60 
61  const bool bFlyAtFly = RndStdIds::FLY_AT_FLY == rFrameFormat.GetAnchor().GetAnchorId();
62 
63  // determine position.
64  // 'vertical' and 'horizontal' position are calculated separately
65  Point aRelPos;
66 
67  // calculate 'vertical' position
68  SwFormatVertOrient aVert( rFrameFormat.GetVertOrient() );
69  {
70  // to-frame anchored objects are *only* vertical positioned centered or
71  // bottom, if its wrap mode is 'through' and its anchor frame has fixed
72  // size. Otherwise, it's positioned top.
73  sal_Int16 eVertOrient = aVert.GetVertOrient();
74  if ( bFlyAtFly &&
75  ( eVertOrient == text::VertOrientation::CENTER ||
76  eVertOrient == text::VertOrientation::BOTTOM ) &&
77  css::text::WrapTextMode_THROUGH != rFrameFormat.GetSurround().GetSurround() &&
79  {
80  eVertOrient = text::VertOrientation::TOP;
81  }
82  // #i26791# - get vertical offset to frame anchor position.
83  SwTwips nVertOffsetToFrameAnchorPos( 0 );
84  SwTwips nRelPosY =
85  GetVertRelPos( GetAnchorFrame(), GetAnchorFrame(), eVertOrient,
86  aVert.GetRelationOrient(), aVert.GetPos(),
87  rLR, rUL, nVertOffsetToFrameAnchorPos );
88 
89  // keep the calculated relative vertical position - needed for filters
90  // (including the xml-filter)
91  {
92  SwTwips nAttrRelPosY = nRelPosY - nVertOffsetToFrameAnchorPos;
93  if ( aVert.GetVertOrient() != text::VertOrientation::NONE &&
94  aVert.GetPos() != nAttrRelPosY )
95  {
96  aVert.SetPos( nAttrRelPosY );
97  const_cast<SwFrameFormat&>(rFrameFormat).LockModify();
98  const_cast<SwFrameFormat&>(rFrameFormat).SetFormatAttr( aVert );
99  const_cast<SwFrameFormat&>(rFrameFormat).UnlockModify();
100  }
101  }
102 
103  // determine absolute 'vertical' position, depending on layout-direction
104  // #i26791# - determine offset to 'vertical' frame
105  // anchor position, depending on layout-direction
106  if( aRectFnSet.IsVert() )
107  {
108  if ( aRectFnSet.IsVertL2R() )
109  aRelPos.setX( nRelPosY );
110  else
111  aRelPos.setX( -nRelPosY - aObjBoundRect.Width() );
112  maOffsetToFrameAnchorPos.setX( nVertOffsetToFrameAnchorPos );
113  }
114  else
115  {
116  aRelPos.setY( nRelPosY );
117  maOffsetToFrameAnchorPos.setY( nVertOffsetToFrameAnchorPos );
118  }
119 
120  // if in online-layout the bottom of to-page anchored object is beyond
121  // the page bottom, the page frame has to grow by growing its body frame.
123  if ( !bFlyAtFly && GetAnchorFrame().IsPageFrame() &&
124  pSh && pSh->GetViewOptions()->getBrowseMode() )
125  {
126  const long nAnchorBottom = GetAnchorFrame().getFrameArea().Bottom();
127  const long nBottom = GetAnchorFrame().getFrameArea().Top() +
128  aRelPos.Y() + aObjBoundRect.Height();
129  if ( nAnchorBottom < nBottom )
130  {
131  static_cast<SwPageFrame&>(GetAnchorFrame()).
132  FindBodyCont()->Grow( nBottom - nAnchorBottom );
133  }
134  }
135  } // end of determination of vertical position
136 
137  // calculate 'horizontal' position
138  SwFormatHoriOrient aHori( rFrameFormat.GetHoriOrient() );
139  {
140  // consider toggle of horizontal position for even pages.
141  const bool bToggle = aHori.IsPosToggle() &&
143  sal_Int16 eHoriOrient = aHori.GetHoriOrient();
144  sal_Int16 eRelOrient = aHori.GetRelationOrient();
145  // toggle orientation
146  ToggleHoriOrientAndAlign( bToggle, eHoriOrient, eRelOrient );
147 
148  // determine alignment values:
149  // <nWidth>: 'width' of the alignment area
150  // <nOffset>: offset of alignment area, relative to 'left' of
151  // frame anchor position
152  SwTwips nWidth, nOffset;
153  {
154  bool bDummy; // in this context irrelevant output parameter
156  eRelOrient, false,
157  nWidth, nOffset, bDummy );
158  }
159 
160  SwTwips nObjWidth = aRectFnSet.GetWidth(aObjBoundRect);
161 
162  // determine relative horizontal position
163  SwTwips nRelPosX;
164  if ( text::HoriOrientation::NONE == eHoriOrient )
165  {
166  if( bToggle ||
167  ( !aHori.IsPosToggle() && GetAnchorFrame().IsRightToLeft() ) )
168  {
169  nRelPosX = nWidth - nObjWidth - aHori.GetPos();
170  }
171  else
172  {
173  nRelPosX = aHori.GetPos();
174  }
175  }
176  else if ( text::HoriOrientation::CENTER == eHoriOrient )
177  nRelPosX = (nWidth / 2) - (nObjWidth / 2);
178  else if ( text::HoriOrientation::RIGHT == eHoriOrient )
179  nRelPosX = nWidth - ( nObjWidth +
180  ( aRectFnSet.IsVert() ? rUL.GetLower() : rLR.GetRight() ) );
181  else
182  nRelPosX = aRectFnSet.IsVert() ? rUL.GetUpper() : rLR.GetLeft();
183  nRelPosX += nOffset;
184 
185  // no 'negative' relative horizontal position
186  // OD 06.11.2003 #FollowTextFlowAtFrame# - negative positions allow for
187  // to frame anchored objects.
188  if ( !bFlyAtFly && nRelPosX < 0 )
189  {
190  nRelPosX = 0;
191  }
192 
193  // determine absolute 'horizontal' position, depending on layout-direction
194  // #i26791# - determine offset to 'horizontal' frame
195  // anchor position, depending on layout-direction
196  if( aRectFnSet.IsVert() || aRectFnSet.IsVertL2R() )
197  {
198 
199  aRelPos.setY( nRelPosX );
200  maOffsetToFrameAnchorPos.setY( nOffset );
201  }
202  else
203  {
204  aRelPos.setX( nRelPosX );
205  maOffsetToFrameAnchorPos.setX( nOffset );
206  }
207 
208  // keep the calculated relative horizontal position - needed for filters
209  // (including the xml-filter)
210  {
211  SwTwips nAttrRelPosX = nRelPosX - nOffset;
212  if ( text::HoriOrientation::NONE != aHori.GetHoriOrient() &&
213  aHori.GetPos() != nAttrRelPosX )
214  {
215  aHori.SetPos( nAttrRelPosX );
216  const_cast<SwFrameFormat&>(rFrameFormat).LockModify();
217  const_cast<SwFrameFormat&>(rFrameFormat).SetFormatAttr( aHori );
218  const_cast<SwFrameFormat&>(rFrameFormat).UnlockModify();
219  }
220  }
221  } // end of determination of horizontal position
222 
223  // keep calculate relative position
224  maRelPos = aRelPos;
225 }
226 
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long GetLeft() const
SwTwips Grow(SwTwips, bool bTst=false, bool bInfo=false)
Definition: wsfrm.cxx:1463
sal_uInt16 GetLower() const
const SwFormatVertOrient & GetVertOrient(bool=true) const
Definition: fmtornt.hxx:106
void Height(long nNew)
Definition: swrect.hxx:189
long SwTwips
Definition: swtypes.hxx:49
bool IsVert() const
Definition: frame.hxx:1343
Of course Writer needs its own rectangles.
Definition: swrect.hxx:34
static void ToggleHoriOrientAndAlign(const bool _bToggleLeftRight, sal_Int16 &_ioeHoriOrient, sal_Int16 &_iopeRelOrient)
toggle given horizontal orientation and relative alignment
void Top(const long nTop)
Definition: swrect.hxx:202
const SwRect & getFrameArea() const
Definition: frame.hxx:175
void setX(long nX)
bool getBrowseMode() const
Definition: viewopt.hxx:433
bool OnRightPage() const
Definition: frame.hxx:711
const SwFormatSurround & GetSurround(bool=true) const
Definition: fmtsrnd.hxx:66
void setY(long nY)
SwTwips GetVertRelPos(const SwFrame &_rVertOrientFrame, const SwFrame &_rPageAlignLayFrame, const sal_Int16 _eVertOrient, const sal_Int16 _eRelOrient, const SwTwips _nVertPos, const SvxLRSpaceItem &_rLRSpacing, const SvxULSpaceItem &_rULSpacing, SwTwips &_roVertOffsetToFrameAnchorPos) const
Style of a layout element.
Definition: frmfmt.hxx:57
const SwFormatAnchor & GetAnchor(bool=true) const
Definition: fmtanchr.hxx:81
RndStdIds GetAnchorId() const
Definition: fmtanchr.hxx:65
const SwFormatHoriOrient & GetHoriOrient(bool=true) const
Definition: fmtornt.hxx:108
SwPageFrame * FindPageFrame()
Definition: frame.hxx:658
css::text::WrapTextMode GetSurround() const
Definition: fmtsrnd.hxx:51
void GetHoriAlignmentValues(const SwFrame &_rHoriOrientFrame, const SwFrame &_rPageAlignLayFrame, const sal_Int16 _eRelOrient, const bool _bObjWrapThrough, SwTwips &_orAlignAreaWidth, SwTwips &_orAlignAreaOffset, bool &_obAlignedRelToPage) const
determine alignment values for horizontal position of object
bool HasFixSize() const
Definition: frame.hxx:648
A page of the document layout.
Definition: pagefrm.hxx:40
long GetWidth(const SwRect &rRect) const
Definition: frame.hxx:1357
void Bottom(const long nBottom)
Definition: swrect.hxx:207
const SvxULSpaceItem & GetULSpace(bool=true) const
Definition: frmatr.hxx:76
bool IsPosToggle() const
Definition: fmtornt.hxx:95
const SwViewOption * GetViewOptions() const
Definition: viewsh.hxx:426
void Width(long nNew)
Definition: swrect.hxx:185
bool IsRightToLeft() const
Definition: frame.hxx:963
bool IsVertL2R() const
Definition: frame.hxx:1344
sal_Int16 GetVertOrient() const
Definition: fmtornt.hxx:54
long GetRight() const
virtual void CalcPosition() override
calculate position for object
SwViewShell * GetCurrShell() const
Definition: rootfrm.hxx:204
SwRootFrame * getRootFrame()
Definition: frame.hxx:657
long Y() const
sal_uInt16 GetUpper() const
const SvxLRSpaceItem & GetLRSpace(bool=true) const
Definition: frmatr.hxx:74