Line data Source code
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 :
20 : #include <tolayoutanchoredobjectposition.hxx>
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 <IDocumentSettingAccess.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 :
40 :
41 0 : SwToLayoutAnchoredObjectPosition::SwToLayoutAnchoredObjectPosition( SdrObject& _rDrawObj )
42 : : SwAnchoredObjectPosition( _rDrawObj ),
43 : maRelPos( Point() ),
44 : // #i26791#
45 0 : maOffsetToFrmAnchorPos( Point() )
46 0 : {}
47 :
48 0 : SwToLayoutAnchoredObjectPosition::~SwToLayoutAnchoredObjectPosition()
49 0 : {}
50 :
51 : /** calculate position for object position type TO_LAYOUT
52 :
53 : @author OD
54 : */
55 0 : void SwToLayoutAnchoredObjectPosition::CalcPosition()
56 : {
57 0 : const SwRect aObjBoundRect( GetAnchoredObj().GetObjRect() );
58 :
59 0 : SWRECTFN( (&GetAnchorFrm()) );
60 :
61 0 : const SwFrmFmt& rFrmFmt = GetFrmFmt();
62 0 : const SvxLRSpaceItem &rLR = rFrmFmt.GetLRSpace();
63 0 : const SvxULSpaceItem &rUL = rFrmFmt.GetULSpace();
64 :
65 0 : const bool bFlyAtFly = FLY_AT_FLY == rFrmFmt.GetAnchor().GetAnchorId();
66 :
67 : // determine position.
68 : // 'vertical' and 'horizontal' position are calculated separately
69 0 : Point aRelPos;
70 :
71 : // calculate 'vertical' position
72 0 : SwFmtVertOrient aVert( rFrmFmt.GetVertOrient() );
73 : {
74 : // to-frame anchored objects are *only* vertical positioned centered or
75 : // bottom, if its wrap mode is 'throught' and its anchor frame has fixed
76 : // size. Otherwise, it's positioned top.
77 0 : sal_Int16 eVertOrient = aVert.GetVertOrient();
78 0 : if ( ( bFlyAtFly &&
79 : ( eVertOrient == text::VertOrientation::CENTER ||
80 : eVertOrient == text::VertOrientation::BOTTOM ) &&
81 0 : SURROUND_THROUGHT != rFrmFmt.GetSurround().GetSurround() &&
82 0 : !GetAnchorFrm().HasFixSize() ) )
83 : {
84 0 : eVertOrient = text::VertOrientation::TOP;
85 : }
86 : // #i26791# - get vertical offset to frame anchor position.
87 0 : SwTwips nVertOffsetToFrmAnchorPos( 0L );
88 : SwTwips nRelPosY =
89 0 : _GetVertRelPos( GetAnchorFrm(), GetAnchorFrm(), eVertOrient,
90 0 : aVert.GetRelationOrient(), aVert.GetPos(),
91 0 : rLR, rUL, nVertOffsetToFrmAnchorPos );
92 :
93 :
94 : // keep the calculated relative vertical position - needed for filters
95 : // (including the xml-filter)
96 : {
97 0 : SwTwips nAttrRelPosY = nRelPosY - nVertOffsetToFrmAnchorPos;
98 0 : if ( aVert.GetVertOrient() != text::VertOrientation::NONE &&
99 0 : aVert.GetPos() != nAttrRelPosY )
100 : {
101 0 : aVert.SetPos( nAttrRelPosY );
102 0 : const_cast<SwFrmFmt&>(rFrmFmt).LockModify();
103 0 : const_cast<SwFrmFmt&>(rFrmFmt).SetFmtAttr( aVert );
104 0 : const_cast<SwFrmFmt&>(rFrmFmt).UnlockModify();
105 : }
106 : }
107 :
108 : // determine absolute 'vertical' position, depending on layout-direction
109 : // #i26791# - determine offset to 'vertical' frame
110 : // anchor position, depending on layout-direction
111 0 : if( bVert )
112 : {
113 : OSL_ENSURE( !bRev, "<SwToLayoutAnchoredObjectPosition::CalcPosition()> - reverse layout set." );
114 : //Badaa: 2008-04-18 * Support for Classical Mongolian Script (SCMS) joint with Jiayanmin
115 0 : if ( bVertL2R )
116 0 : aRelPos.X() = nRelPosY;
117 : else
118 0 : aRelPos.X() = -nRelPosY - aObjBoundRect.Width();
119 0 : maOffsetToFrmAnchorPos.X() = nVertOffsetToFrmAnchorPos;
120 : }
121 : else
122 : {
123 0 : aRelPos.Y() = nRelPosY;
124 0 : maOffsetToFrmAnchorPos.Y() = nVertOffsetToFrmAnchorPos;
125 : }
126 :
127 : // if in online-layout the bottom of to-page anchored object is beyond
128 : // the page bottom, the page frame has to grow by growing its body frame.
129 0 : const ViewShell *pSh = GetAnchorFrm().getRootFrm()->GetCurrShell();
130 0 : if ( !bFlyAtFly && GetAnchorFrm().IsPageFrm() &&
131 0 : pSh && pSh->GetViewOptions()->getBrowseMode() )
132 : {
133 0 : const long nAnchorBottom = GetAnchorFrm().Frm().Bottom();
134 0 : const long nBottom = GetAnchorFrm().Frm().Top() +
135 0 : aRelPos.Y() + aObjBoundRect.Height();
136 0 : if ( nAnchorBottom < nBottom )
137 : {
138 0 : static_cast<SwPageFrm&>(GetAnchorFrm()).
139 0 : FindBodyCont()->Grow( nBottom - nAnchorBottom );
140 : }
141 : }
142 : } // end of determination of vertical position
143 :
144 : // calculate 'horizontal' position
145 0 : SwFmtHoriOrient aHori( rFrmFmt.GetHoriOrient() );
146 : {
147 : // consider toggle of horizontal position for even pages.
148 0 : const bool bToggle = aHori.IsPosToggle() &&
149 0 : !GetAnchorFrm().FindPageFrm()->OnRightPage();
150 0 : sal_Int16 eHoriOrient = aHori.GetHoriOrient();
151 0 : sal_Int16 eRelOrient = aHori.GetRelationOrient();
152 : // toggle orientation
153 0 : _ToggleHoriOrientAndAlign( bToggle, eHoriOrient, eRelOrient );
154 :
155 : // determine alignment values:
156 : // <nWidth>: 'width' of the alignment area
157 : // <nOffset>: offset of alignment area, relative to 'left' of
158 : // frame anchor position
159 : SwTwips nWidth, nOffset;
160 : {
161 : bool bDummy; // in this context irrelevant output parameter
162 0 : _GetHoriAlignmentValues( GetAnchorFrm(), GetAnchorFrm(),
163 : eRelOrient, false,
164 0 : nWidth, nOffset, bDummy );
165 : }
166 :
167 0 : SwTwips nObjWidth = (aObjBoundRect.*fnRect->fnGetWidth)();
168 :
169 : // determine relative horizontal position
170 : SwTwips nRelPosX;
171 0 : if ( text::HoriOrientation::NONE == eHoriOrient )
172 : {
173 0 : if( bToggle ||
174 0 : ( !aHori.IsPosToggle() && GetAnchorFrm().IsRightToLeft() ) )
175 : {
176 0 : nRelPosX = nWidth - nObjWidth - aHori.GetPos();
177 : }
178 : else
179 : {
180 0 : nRelPosX = aHori.GetPos();
181 : }
182 : }
183 0 : else if ( text::HoriOrientation::CENTER == eHoriOrient )
184 0 : nRelPosX = (nWidth / 2) - (nObjWidth / 2);
185 0 : else if ( text::HoriOrientation::RIGHT == eHoriOrient )
186 : nRelPosX = nWidth - ( nObjWidth +
187 0 : ( bVert ? rUL.GetLower() : rLR.GetRight() ) );
188 : else
189 0 : nRelPosX = bVert ? rUL.GetUpper() : rLR.GetLeft();
190 0 : nRelPosX += nOffset;
191 :
192 : // no 'negative' relative horizontal position
193 : // OD 06.11.2003 #FollowTextFlowAtFrame# - negative positions allow for
194 : // to frame anchored objects.
195 0 : if ( !bFlyAtFly && nRelPosX < 0 )
196 : {
197 0 : nRelPosX = 0;
198 : }
199 :
200 : // determine absolute 'horizontal' position, depending on layout-direction
201 : // #i26791# - determine offset to 'horizontal' frame
202 : // anchor position, depending on layout-direction
203 : //Badaa: 2008-04-18 * Support for Classical Mongolian Script (SCMS) joint with Jiayanmin
204 : // --> OD 2009-09-04 #mongolianlayout#
205 0 : if( bVert || bVertL2R )
206 : {
207 :
208 0 : aRelPos.Y() = nRelPosX;
209 0 : maOffsetToFrmAnchorPos.Y() = nOffset;
210 : }
211 : else
212 : {
213 0 : aRelPos.X() = nRelPosX;
214 0 : maOffsetToFrmAnchorPos.X() = nOffset;
215 : }
216 :
217 : // keep the calculated relative horizontal position - needed for filters
218 : // (including the xml-filter)
219 : {
220 0 : SwTwips nAttrRelPosX = nRelPosX - nOffset;
221 0 : if ( text::HoriOrientation::NONE != aHori.GetHoriOrient() &&
222 0 : aHori.GetPos() != nAttrRelPosX )
223 : {
224 0 : aHori.SetPos( nAttrRelPosX );
225 0 : const_cast<SwFrmFmt&>(rFrmFmt).LockModify();
226 0 : const_cast<SwFrmFmt&>(rFrmFmt).SetFmtAttr( aHori );
227 0 : const_cast<SwFrmFmt&>(rFrmFmt).UnlockModify();
228 : }
229 : }
230 : } // end of determination of horizontal position
231 :
232 : // keep calculate relative position
233 0 : maRelPos = aRelPos;
234 0 : }
235 :
236 : /** calculated relative position for object position
237 :
238 : @author OD
239 : */
240 0 : Point SwToLayoutAnchoredObjectPosition::GetRelPos() const
241 : {
242 0 : return maRelPos;
243 : }
244 :
245 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|