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>
29 #include <frmatr.hxx>
30 #include <viewsh.hxx>
31 #include <viewopt.hxx>
32 #include <rootfrm.hxx>
33 #include <editeng/lrspitem.hxx>
34 #include <editeng/ulspitem.hxx>
35 
36 using namespace objectpositioning;
37 using namespace ::com::sun::star;
38 
40  : SwAnchoredObjectPosition( _rDrawObj ),
41  maRelPos( Point() ),
42  // #i26791#
43  maOffsetToFrameAnchorPos( Point() )
44 {}
45 
47 {}
48 
51 {
52  const SwRect aObjBoundRect( GetAnchoredObj().GetObjRect() );
53 
54  SwRectFnSet aRectFnSet(&GetAnchorFrame());
55 
56  const SwFrameFormat& rFrameFormat = GetFrameFormat();
57  const SvxLRSpaceItem &rLR = rFrameFormat.GetLRSpace();
58  const SvxULSpaceItem &rUL = rFrameFormat.GetULSpace();
59 
60  const bool bFlyAtFly = RndStdIds::FLY_AT_FLY == rFrameFormat.GetAnchor().GetAnchorId();
61 
62  // determine position.
63  // 'vertical' and 'horizontal' position are calculated separately
64  Point aRelPos;
65 
66  // calculate 'vertical' position
67  SwFormatVertOrient aVert( rFrameFormat.GetVertOrient() );
68  {
69  // to-frame anchored objects are *only* vertical positioned centered or
70  // bottom, if its wrap mode is 'through' and its anchor frame has fixed
71  // size. Otherwise, it's positioned top.
72  sal_Int16 eVertOrient = aVert.GetVertOrient();
73  if ( bFlyAtFly &&
74  ( eVertOrient == text::VertOrientation::CENTER ||
75  eVertOrient == text::VertOrientation::BOTTOM ) &&
76  css::text::WrapTextMode_THROUGH != rFrameFormat.GetSurround().GetSurround() &&
78  {
79  eVertOrient = text::VertOrientation::TOP;
80  }
81  // #i26791# - get vertical offset to frame anchor position.
82  SwTwips nVertOffsetToFrameAnchorPos( 0 );
83  SwTwips nRelPosY =
84  GetVertRelPos( GetAnchorFrame(), GetAnchorFrame(), eVertOrient,
85  aVert.GetRelationOrient(), aVert.GetPos(),
86  rLR, rUL, nVertOffsetToFrameAnchorPos );
87 
88  // keep the calculated relative vertical position - needed for filters
89  // (including the xml-filter)
90  {
91  SwTwips nAttrRelPosY = nRelPosY - nVertOffsetToFrameAnchorPos;
92  if ( aVert.GetVertOrient() != text::VertOrientation::NONE &&
93  aVert.GetPos() != nAttrRelPosY )
94  {
95  aVert.SetPos( nAttrRelPosY );
96  const_cast<SwFrameFormat&>(rFrameFormat).LockModify();
97  const_cast<SwFrameFormat&>(rFrameFormat).SetFormatAttr( aVert );
98  const_cast<SwFrameFormat&>(rFrameFormat).UnlockModify();
99  }
100  }
101 
102  // determine absolute 'vertical' position, depending on layout-direction
103  // #i26791# - determine offset to 'vertical' frame
104  // anchor position, depending on layout-direction
105  if( aRectFnSet.IsVert() )
106  {
107  if ( aRectFnSet.IsVertL2R() )
108  aRelPos.setX( nRelPosY );
109  else
110  aRelPos.setX( -nRelPosY - aObjBoundRect.Width() );
111  maOffsetToFrameAnchorPos.setX( nVertOffsetToFrameAnchorPos );
112  }
113  else
114  {
115  aRelPos.setY( nRelPosY );
116  maOffsetToFrameAnchorPos.setY( nVertOffsetToFrameAnchorPos );
117  }
118 
119  // if in online-layout the bottom of to-page anchored object is beyond
120  // the page bottom, the page frame has to grow by growing its body frame.
122  if ( !bFlyAtFly && GetAnchorFrame().IsPageFrame() &&
123  pSh && pSh->GetViewOptions()->getBrowseMode() )
124  {
125  const long nAnchorBottom = GetAnchorFrame().getFrameArea().Bottom();
126  const long nBottom = GetAnchorFrame().getFrameArea().Top() +
127  aRelPos.Y() + aObjBoundRect.Height();
128  if ( nAnchorBottom < nBottom )
129  {
130  static_cast<SwPageFrame&>(GetAnchorFrame()).
131  FindBodyCont()->Grow( nBottom - nAnchorBottom );
132  }
133  }
134  } // end of determination of vertical position
135 
136  // calculate 'horizontal' position
137  SwFormatHoriOrient aHori( rFrameFormat.GetHoriOrient() );
138  {
139  // consider toggle of horizontal position for even pages.
140  const bool bToggle = aHori.IsPosToggle() &&
142  sal_Int16 eHoriOrient = aHori.GetHoriOrient();
143  sal_Int16 eRelOrient = aHori.GetRelationOrient();
144  // toggle orientation
145  ToggleHoriOrientAndAlign( bToggle, eHoriOrient, eRelOrient );
146 
147  // determine alignment values:
148  // <nWidth>: 'width' of the alignment area
149  // <nOffset>: offset of alignment area, relative to 'left' of
150  // frame anchor position
151  SwTwips nWidth, nOffset;
152  {
153  bool bDummy; // in this context irrelevant output parameter
155  eRelOrient, false,
156  nWidth, nOffset, bDummy );
157  }
158 
159  SwTwips nObjWidth = aRectFnSet.GetWidth(aObjBoundRect);
160 
161  // determine relative horizontal position
162  SwTwips nRelPosX;
163  if ( text::HoriOrientation::NONE == eHoriOrient )
164  {
165  if( bToggle ||
166  ( !aHori.IsPosToggle() && GetAnchorFrame().IsRightToLeft() ) )
167  {
168  nRelPosX = nWidth - nObjWidth - aHori.GetPos();
169  }
170  else
171  {
172  nRelPosX = aHori.GetPos();
173  }
174  }
175  else if ( text::HoriOrientation::CENTER == eHoriOrient )
176  nRelPosX = (nWidth / 2) - (nObjWidth / 2);
177  else if ( text::HoriOrientation::RIGHT == eHoriOrient )
178  nRelPosX = nWidth - ( nObjWidth +
179  ( aRectFnSet.IsVert() ? rUL.GetLower() : rLR.GetRight() ) );
180  else
181  nRelPosX = aRectFnSet.IsVert() ? rUL.GetUpper() : rLR.GetLeft();
182  nRelPosX += nOffset;
183 
184  // no 'negative' relative horizontal position
185  // OD 06.11.2003 #FollowTextFlowAtFrame# - negative positions allow for
186  // to frame anchored objects.
187  if ( !bFlyAtFly && nRelPosX < 0 )
188  {
189  nRelPosX = 0;
190  }
191 
192  // determine absolute 'horizontal' position, depending on layout-direction
193  // #i26791# - determine offset to 'horizontal' frame
194  // anchor position, depending on layout-direction
195  if( aRectFnSet.IsVert() || aRectFnSet.IsVertL2R() )
196  {
197 
198  aRelPos.setY( nRelPosX );
199  maOffsetToFrameAnchorPos.setY( nOffset );
200  }
201  else
202  {
203  aRelPos.setX( nRelPosX );
204  maOffsetToFrameAnchorPos.setX( nOffset );
205  }
206 
207  // keep the calculated relative horizontal position - needed for filters
208  // (including the xml-filter)
209  {
210  SwTwips nAttrRelPosX = nRelPosX - nOffset;
211  if ( text::HoriOrientation::NONE != aHori.GetHoriOrient() &&
212  aHori.GetPos() != nAttrRelPosX )
213  {
214  aHori.SetPos( nAttrRelPosX );
215  const_cast<SwFrameFormat&>(rFrameFormat).LockModify();
216  const_cast<SwFrameFormat&>(rFrameFormat).SetFormatAttr( aHori );
217  const_cast<SwFrameFormat&>(rFrameFormat).UnlockModify();
218  }
219  }
220  } // end of determination of horizontal position
221 
222  // keep calculate relative position
223  maRelPos = aRelPos;
224 }
225 
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long GetLeft() const
SwTwips Grow(SwTwips, bool bTst=false, bool bInfo=false)
Definition: wsfrm.cxx:1462
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:444
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