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