xref: /AOO41X/main/sw/source/core/objectpositioning/tolayoutanchoredobjectposition.cxx (revision efeef26f81c84063fb0a91bde3856d4a51172d90)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir #include <tolayoutanchoredobjectposition.hxx>
27cdf0e10cSrcweir #include <anchoredobject.hxx>
28cdf0e10cSrcweir #include <frame.hxx>
29cdf0e10cSrcweir #include <pagefrm.hxx>
30cdf0e10cSrcweir #include <svx/svdobj.hxx>
31cdf0e10cSrcweir #include <frmfmt.hxx>
32cdf0e10cSrcweir #include <fmtanchr.hxx>
33cdf0e10cSrcweir #include <fmtornt.hxx>
34cdf0e10cSrcweir #include <fmtsrnd.hxx>
35cdf0e10cSrcweir #include <IDocumentSettingAccess.hxx>
36cdf0e10cSrcweir #include <frmatr.hxx>
37cdf0e10cSrcweir #include "viewsh.hxx"
38cdf0e10cSrcweir #include "viewopt.hxx"
39cdf0e10cSrcweir #include "rootfrm.hxx"
40cdf0e10cSrcweir #include <editeng/lrspitem.hxx>
41cdf0e10cSrcweir #include <editeng/ulspitem.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir using namespace objectpositioning;
44cdf0e10cSrcweir using namespace ::com::sun::star;
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
SwToLayoutAnchoredObjectPosition(SdrObject & _rDrawObj)47cdf0e10cSrcweir SwToLayoutAnchoredObjectPosition::SwToLayoutAnchoredObjectPosition( SdrObject& _rDrawObj )
48cdf0e10cSrcweir     : SwAnchoredObjectPosition( _rDrawObj ),
49cdf0e10cSrcweir       maRelPos( Point() ),
50cdf0e10cSrcweir       // --> OD 2004-06-17 #i26791#
51cdf0e10cSrcweir       maOffsetToFrmAnchorPos( Point() )
52cdf0e10cSrcweir {}
53cdf0e10cSrcweir 
~SwToLayoutAnchoredObjectPosition()54cdf0e10cSrcweir SwToLayoutAnchoredObjectPosition::~SwToLayoutAnchoredObjectPosition()
55cdf0e10cSrcweir {}
56cdf0e10cSrcweir 
57cdf0e10cSrcweir /** calculate position for object position type TO_LAYOUT
58cdf0e10cSrcweir 
59cdf0e10cSrcweir     @author OD
60cdf0e10cSrcweir */
CalcPosition()61cdf0e10cSrcweir void SwToLayoutAnchoredObjectPosition::CalcPosition()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     const SwRect aObjBoundRect( GetAnchoredObj().GetObjRect() );
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     SWRECTFN( (&GetAnchorFrm()) );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     const SwFrmFmt& rFrmFmt = GetFrmFmt();
68cdf0e10cSrcweir     const SvxLRSpaceItem &rLR = rFrmFmt.GetLRSpace();
69cdf0e10cSrcweir     const SvxULSpaceItem &rUL = rFrmFmt.GetULSpace();
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     const bool bFlyAtFly = FLY_AT_FLY == rFrmFmt.GetAnchor().GetAnchorId();
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     // determine position.
74cdf0e10cSrcweir     // 'vertical' and 'horizontal' position are calculated separately
75cdf0e10cSrcweir     Point aRelPos;
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     // calculate 'vertical' position
78cdf0e10cSrcweir     SwFmtVertOrient aVert( rFrmFmt.GetVertOrient() );
79cdf0e10cSrcweir     {
80cdf0e10cSrcweir         // to-frame anchored objects are *only* vertical positioned centered or
81cdf0e10cSrcweir         // bottom, if its wrap mode is 'throught' and its anchor frame has fixed
82cdf0e10cSrcweir         // size. Otherwise, it's positioned top.
83cdf0e10cSrcweir         sal_Int16 eVertOrient = aVert.GetVertOrient();
84cdf0e10cSrcweir         if ( ( bFlyAtFly &&
85cdf0e10cSrcweir                ( eVertOrient == text::VertOrientation::CENTER ||
86cdf0e10cSrcweir                  eVertOrient == text::VertOrientation::BOTTOM ) &&
87cdf0e10cSrcweir              SURROUND_THROUGHT != rFrmFmt.GetSurround().GetSurround() &&
88cdf0e10cSrcweir              !GetAnchorFrm().HasFixSize() ) )
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir             eVertOrient = text::VertOrientation::TOP;
91cdf0e10cSrcweir         }
92cdf0e10cSrcweir         // --> OD 2004-06-17 #i26791# - get vertical offset to frame anchor position.
93cdf0e10cSrcweir         SwTwips nVertOffsetToFrmAnchorPos( 0L );
94cdf0e10cSrcweir         SwTwips nRelPosY =
95cdf0e10cSrcweir                 _GetVertRelPos( GetAnchorFrm(), GetAnchorFrm(), eVertOrient,
96cdf0e10cSrcweir                                 aVert.GetRelationOrient(), aVert.GetPos(),
97cdf0e10cSrcweir                                 rLR, rUL, nVertOffsetToFrmAnchorPos );
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         // keep the calculated relative vertical position - needed for filters
101cdf0e10cSrcweir         // (including the xml-filter)
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             SwTwips nAttrRelPosY = nRelPosY - nVertOffsetToFrmAnchorPos;
104cdf0e10cSrcweir             if ( aVert.GetVertOrient() != text::VertOrientation::NONE &&
105cdf0e10cSrcweir                  aVert.GetPos() != nAttrRelPosY )
106cdf0e10cSrcweir             {
107cdf0e10cSrcweir                 aVert.SetPos( nAttrRelPosY );
108cdf0e10cSrcweir                 const_cast<SwFrmFmt&>(rFrmFmt).LockModify();
109cdf0e10cSrcweir                 const_cast<SwFrmFmt&>(rFrmFmt).SetFmtAttr( aVert );
110cdf0e10cSrcweir                 const_cast<SwFrmFmt&>(rFrmFmt).UnlockModify();
111cdf0e10cSrcweir             }
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         // determine absolute 'vertical' position, depending on layout-direction
115cdf0e10cSrcweir         // --> OD 2004-06-17 #i26791# - determine offset to 'vertical' frame
116cdf0e10cSrcweir         // anchor position, depending on layout-direction
117cdf0e10cSrcweir         if( bVert )
118cdf0e10cSrcweir         {
119cdf0e10cSrcweir             ASSERT( !bRev, "<SwToLayoutAnchoredObjectPosition::CalcPosition()> - reverse layout set." );
120cdf0e10cSrcweir             //Badaa: 2008-04-18 * Support for Classical Mongolian Script (SCMS) joint with Jiayanmin
121cdf0e10cSrcweir             if ( bVertL2R )
122cdf0e10cSrcweir                	aRelPos.X() = nRelPosY;
123cdf0e10cSrcweir             else
124cdf0e10cSrcweir                	aRelPos.X() = -nRelPosY - aObjBoundRect.Width();
125cdf0e10cSrcweir             maOffsetToFrmAnchorPos.X() = nVertOffsetToFrmAnchorPos;
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir         else
128cdf0e10cSrcweir         {
129cdf0e10cSrcweir             aRelPos.Y() = nRelPosY;
130cdf0e10cSrcweir             maOffsetToFrmAnchorPos.Y() = nVertOffsetToFrmAnchorPos;
131cdf0e10cSrcweir         }
132cdf0e10cSrcweir 
133cdf0e10cSrcweir         // if in online-layout the bottom of to-page anchored object is beyond
134cdf0e10cSrcweir         // the page bottom, the page frame has to grow by growing its body frame.
135cdf0e10cSrcweir         const ViewShell *pSh = GetAnchorFrm().getRootFrm()->GetCurrShell();
136cdf0e10cSrcweir         if ( !bFlyAtFly && GetAnchorFrm().IsPageFrm() &&
137cdf0e10cSrcweir              pSh && pSh->GetViewOptions()->getBrowseMode() )
138cdf0e10cSrcweir         {
139cdf0e10cSrcweir             const long nAnchorBottom = GetAnchorFrm().Frm().Bottom();
140cdf0e10cSrcweir             const long nBottom = GetAnchorFrm().Frm().Top() +
141cdf0e10cSrcweir                                  aRelPos.Y() + aObjBoundRect.Height();
142cdf0e10cSrcweir             if ( nAnchorBottom < nBottom )
143cdf0e10cSrcweir             {
144cdf0e10cSrcweir                 static_cast<SwPageFrm&>(GetAnchorFrm()).
145cdf0e10cSrcweir                         FindBodyCont()->Grow( nBottom - nAnchorBottom );
146cdf0e10cSrcweir             }
147cdf0e10cSrcweir         }
148cdf0e10cSrcweir     } // end of determination of vertical position
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     // calculate 'horizontal' position
151cdf0e10cSrcweir     SwFmtHoriOrient aHori( rFrmFmt.GetHoriOrient() );
152cdf0e10cSrcweir     {
153cdf0e10cSrcweir         // consider toggle of horizontal position for even pages.
154cdf0e10cSrcweir         const bool bToggle = aHori.IsPosToggle() &&
155cdf0e10cSrcweir                              !GetAnchorFrm().FindPageFrm()->OnRightPage();
156cdf0e10cSrcweir         sal_Int16 eHoriOrient = aHori.GetHoriOrient();
157cdf0e10cSrcweir         sal_Int16 eRelOrient = aHori.GetRelationOrient();
158cdf0e10cSrcweir         // toggle orientation
159cdf0e10cSrcweir         _ToggleHoriOrientAndAlign( bToggle, eHoriOrient, eRelOrient );
160cdf0e10cSrcweir 
161cdf0e10cSrcweir         // determine alignment values:
162cdf0e10cSrcweir         // <nWidth>: 'width' of the alignment area
163cdf0e10cSrcweir         // <nOffset>: offset of alignment area, relative to 'left' of
164cdf0e10cSrcweir         //            frame anchor position
165cdf0e10cSrcweir         SwTwips nWidth, nOffset;
166cdf0e10cSrcweir         {
167cdf0e10cSrcweir             bool bDummy; // in this context irrelevant output parameter
168cdf0e10cSrcweir             _GetHoriAlignmentValues( GetAnchorFrm(), GetAnchorFrm(),
169cdf0e10cSrcweir                                      eRelOrient, false,
170cdf0e10cSrcweir                                      nWidth, nOffset, bDummy );
171cdf0e10cSrcweir         }
172cdf0e10cSrcweir 
173cdf0e10cSrcweir         SwTwips nObjWidth = (aObjBoundRect.*fnRect->fnGetWidth)();
174cdf0e10cSrcweir 
175cdf0e10cSrcweir         // determine relative horizontal position
176cdf0e10cSrcweir         SwTwips nRelPosX;
177cdf0e10cSrcweir         if ( text::HoriOrientation::NONE == eHoriOrient )
178cdf0e10cSrcweir         {
179cdf0e10cSrcweir             if( bToggle ||
180cdf0e10cSrcweir                 ( !aHori.IsPosToggle() && GetAnchorFrm().IsRightToLeft() ) )
181cdf0e10cSrcweir             {
182cdf0e10cSrcweir                 nRelPosX = nWidth - nObjWidth - aHori.GetPos();
183cdf0e10cSrcweir             }
184cdf0e10cSrcweir             else
185cdf0e10cSrcweir             {
186cdf0e10cSrcweir                 nRelPosX = aHori.GetPos();
187cdf0e10cSrcweir             }
188cdf0e10cSrcweir         }
189cdf0e10cSrcweir         else if ( text::HoriOrientation::CENTER == eHoriOrient )
190cdf0e10cSrcweir             nRelPosX = (nWidth / 2) - (nObjWidth / 2);
191cdf0e10cSrcweir         else if ( text::HoriOrientation::RIGHT == eHoriOrient )
192cdf0e10cSrcweir             nRelPosX = nWidth - ( nObjWidth +
193cdf0e10cSrcweir                              ( bVert ? rUL.GetLower() : rLR.GetRight() ) );
194cdf0e10cSrcweir         else
195cdf0e10cSrcweir             nRelPosX = bVert ? rUL.GetUpper() : rLR.GetLeft();
196cdf0e10cSrcweir         nRelPosX += nOffset;
197cdf0e10cSrcweir 
198cdf0e10cSrcweir         // no 'negative' relative horizontal position
199cdf0e10cSrcweir         // OD 06.11.2003 #FollowTextFlowAtFrame# - negative positions allow for
200cdf0e10cSrcweir         // to frame anchored objects.
201cdf0e10cSrcweir         if ( !bFlyAtFly && nRelPosX < 0 )
202cdf0e10cSrcweir         {
203cdf0e10cSrcweir             nRelPosX = 0;
204cdf0e10cSrcweir         }
205cdf0e10cSrcweir 
206cdf0e10cSrcweir         // determine absolute 'horizontal' position, depending on layout-direction
207cdf0e10cSrcweir         // --> OD 2004-06-17 #i26791# - determine offset to 'horizontal' frame
208cdf0e10cSrcweir         // anchor position, depending on layout-direction
209cdf0e10cSrcweir         //Badaa: 2008-04-18 * Support for Classical Mongolian Script (SCMS) joint with Jiayanmin
210cdf0e10cSrcweir         // --> OD 2009-09-04 #mongolianlayout#
211cdf0e10cSrcweir         if( bVert || bVertL2R )
212cdf0e10cSrcweir         // <--
213cdf0e10cSrcweir         {
214cdf0e10cSrcweir 
215cdf0e10cSrcweir             aRelPos.Y() = nRelPosX;
216cdf0e10cSrcweir             maOffsetToFrmAnchorPos.Y() = nOffset;
217cdf0e10cSrcweir         }
218cdf0e10cSrcweir         else
219cdf0e10cSrcweir         {
220cdf0e10cSrcweir             aRelPos.X() = nRelPosX;
221cdf0e10cSrcweir             maOffsetToFrmAnchorPos.X() = nOffset;
222cdf0e10cSrcweir         }
223cdf0e10cSrcweir 
224cdf0e10cSrcweir         // keep the calculated relative horizontal position - needed for filters
225cdf0e10cSrcweir         // (including the xml-filter)
226cdf0e10cSrcweir         {
227cdf0e10cSrcweir             SwTwips nAttrRelPosX = nRelPosX - nOffset;
228cdf0e10cSrcweir             if ( text::HoriOrientation::NONE != aHori.GetHoriOrient() &&
229cdf0e10cSrcweir                  aHori.GetPos() != nAttrRelPosX )
230cdf0e10cSrcweir             {
231cdf0e10cSrcweir                 aHori.SetPos( nAttrRelPosX );
232cdf0e10cSrcweir                 const_cast<SwFrmFmt&>(rFrmFmt).LockModify();
233cdf0e10cSrcweir                 const_cast<SwFrmFmt&>(rFrmFmt).SetFmtAttr( aHori );
234cdf0e10cSrcweir                 const_cast<SwFrmFmt&>(rFrmFmt).UnlockModify();
235cdf0e10cSrcweir             }
236cdf0e10cSrcweir         }
237cdf0e10cSrcweir     } // end of determination of horizontal position
238cdf0e10cSrcweir 
239cdf0e10cSrcweir     // keep calculate relative position
240cdf0e10cSrcweir     maRelPos = aRelPos;
241cdf0e10cSrcweir }
242cdf0e10cSrcweir 
243cdf0e10cSrcweir /** calculated relative position for object position
244cdf0e10cSrcweir 
245cdf0e10cSrcweir     @author OD
246cdf0e10cSrcweir */
GetRelPos() const247cdf0e10cSrcweir Point SwToLayoutAnchoredObjectPosition::GetRelPos() const
248cdf0e10cSrcweir {
249cdf0e10cSrcweir     return maRelPos;
250cdf0e10cSrcweir }
251cdf0e10cSrcweir 
252