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 <sortedobjs.hxx>
21 :
22 : #include <algorithm>
23 : #include <anchoredobject.hxx>
24 : #include <fmtanchr.hxx>
25 : #include <fmtsrnd.hxx>
26 : #include <fmtwrapinfluenceonobjpos.hxx>
27 : #include <frmfmt.hxx>
28 : #include <pam.hxx>
29 : #include <svx/svdobj.hxx>
30 : #include <IDocumentDrawModelAccess.hxx>
31 :
32 : using namespace ::com::sun::star;
33 :
34 7812 : SwSortedObjs::SwSortedObjs()
35 : {
36 7812 : }
37 :
38 7812 : SwSortedObjs::~SwSortedObjs()
39 : {
40 7812 : }
41 :
42 8556322 : size_t SwSortedObjs::size() const
43 : {
44 8556322 : return maSortedObjLst.size();
45 : }
46 :
47 4460920 : SwAnchoredObject* SwSortedObjs::operator[]( size_t _nIndex ) const
48 : {
49 4460920 : SwAnchoredObject* pAnchoredObj = 0L;
50 :
51 4460920 : if ( _nIndex >= size() )
52 : {
53 : OSL_FAIL( "<SwSortedObjs::operator[]> - index out of range" );
54 : }
55 : else
56 : {
57 4460920 : pAnchoredObj = maSortedObjLst[ _nIndex ];
58 : }
59 :
60 4460920 : return pAnchoredObj;
61 : }
62 :
63 : struct ObjAnchorOrder
64 : {
65 18378 : bool operator()( const SwAnchoredObject* _pListedAnchoredObj,
66 : const SwAnchoredObject* _pNewAnchoredObj )
67 : {
68 : // get attributes of listed object
69 18378 : const SwFrmFmt& rFmtListed = _pListedAnchoredObj->GetFrmFmt();
70 18378 : const SwFmtAnchor* pAnchorListed = &(rFmtListed.GetAnchor());
71 :
72 : // get attributes of new object
73 18378 : const SwFrmFmt& rFmtNew = _pNewAnchoredObj->GetFrmFmt();
74 18378 : const SwFmtAnchor* pAnchorNew = &(rFmtNew.GetAnchor());
75 :
76 : // check for to-page anchored objects
77 19816 : if ((pAnchorListed->GetAnchorId() == FLY_AT_PAGE) &&
78 1438 : (pAnchorNew ->GetAnchorId() != FLY_AT_PAGE))
79 : {
80 70 : return true;
81 : }
82 35248 : else if ((pAnchorListed->GetAnchorId() != FLY_AT_PAGE) &&
83 16940 : (pAnchorNew ->GetAnchorId() == FLY_AT_PAGE))
84 : {
85 48 : return false;
86 : }
87 19628 : else if ((pAnchorListed->GetAnchorId() == FLY_AT_PAGE) &&
88 1368 : (pAnchorNew ->GetAnchorId() == FLY_AT_PAGE))
89 : {
90 1368 : return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
91 : }
92 :
93 : // Both objects aren't anchored to page.
94 : // Thus, check for to-fly anchored objects
95 17074 : if ((pAnchorListed->GetAnchorId() == FLY_AT_FLY) &&
96 182 : (pAnchorNew ->GetAnchorId() != FLY_AT_FLY))
97 : {
98 8 : return true;
99 : }
100 33594 : else if ((pAnchorListed->GetAnchorId() != FLY_AT_FLY) &&
101 16710 : (pAnchorNew ->GetAnchorId() == FLY_AT_FLY))
102 : {
103 0 : return false;
104 : }
105 17058 : else if ((pAnchorListed->GetAnchorId() == FLY_AT_FLY) &&
106 174 : (pAnchorNew ->GetAnchorId() == FLY_AT_FLY))
107 : {
108 174 : return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
109 : }
110 :
111 : // Both objects aren't anchor to page or to fly
112 : // Thus, compare content anchor nodes, if existing.
113 16710 : const SwPosition* pCntntAnchorListed = pAnchorListed->GetCntntAnchor();
114 16710 : const SwPosition* pCntntAnchorNew = pAnchorNew->GetCntntAnchor();
115 33420 : if ( pCntntAnchorListed && pCntntAnchorNew &&
116 16710 : pCntntAnchorListed->nNode != pCntntAnchorNew->nNode )
117 : {
118 1630 : return pCntntAnchorListed->nNode < pCntntAnchorNew->nNode;
119 : }
120 :
121 : // objects anchored at the same content.
122 : // --> OD 2006-11-29 #???# - objects have to be ordered by anchor node position
123 : // Thus, compare content anchor node positions and anchor type,
124 : // if not anchored at-paragraph
125 31412 : if ((pAnchorListed->GetAnchorId() != FLY_AT_PARA) &&
126 2416 : (pAnchorNew ->GetAnchorId() != FLY_AT_PARA) &&
127 16244 : pCntntAnchorListed && pCntntAnchorNew )
128 : {
129 1164 : if ( pCntntAnchorListed->nContent != pCntntAnchorNew->nContent )
130 : {
131 574 : return pCntntAnchorListed->nContent < pCntntAnchorNew->nContent;
132 : }
133 1174 : else if ((pAnchorListed->GetAnchorId() == FLY_AT_CHAR) &&
134 584 : (pAnchorNew ->GetAnchorId() == FLY_AS_CHAR))
135 : {
136 0 : return true;
137 : }
138 596 : else if ((pAnchorListed->GetAnchorId() == FLY_AS_CHAR) &&
139 6 : (pAnchorNew ->GetAnchorId() == FLY_AT_CHAR))
140 : {
141 6 : return false;
142 : }
143 : }
144 :
145 : // objects anchored at the same content and at the same content anchor
146 : // node position with the same anchor type
147 : // Thus, compare its wrapping style including its layer
148 14500 : const IDocumentDrawModelAccess* pIDDMA = rFmtListed.getIDocumentDrawModelAccess();
149 14500 : const SdrLayerID nHellId = pIDDMA->GetHellId();
150 14500 : const SdrLayerID nInvisibleHellId = pIDDMA->GetInvisibleHellId();
151 : const bool bWrapThroughOrHellListed =
152 15236 : rFmtListed.GetSurround().GetSurround() == SURROUND_THROUGHT ||
153 15144 : _pListedAnchoredObj->GetDrawObj()->GetLayer() == nHellId ||
154 15144 : _pListedAnchoredObj->GetDrawObj()->GetLayer() == nInvisibleHellId;
155 : const bool bWrapThroughOrHellNew =
156 15210 : rFmtNew.GetSurround().GetSurround() == SURROUND_THROUGHT ||
157 14998 : _pNewAnchoredObj->GetDrawObj()->GetLayer() == nHellId ||
158 14998 : _pNewAnchoredObj->GetDrawObj()->GetLayer() == nInvisibleHellId;
159 14500 : if ( bWrapThroughOrHellListed != bWrapThroughOrHellNew )
160 : {
161 406 : if ( bWrapThroughOrHellListed )
162 148 : return false;
163 : else
164 258 : return true;
165 : }
166 14094 : else if ( bWrapThroughOrHellListed && bWrapThroughOrHellNew )
167 : {
168 13796 : return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
169 : }
170 :
171 : // objects anchored at the same content with a set text wrapping
172 : // Thus, compare wrap influences on object position
173 : const SwFmtWrapInfluenceOnObjPos* pWrapInfluenceOnObjPosListed =
174 298 : &(rFmtListed.GetWrapInfluenceOnObjPos());
175 : const SwFmtWrapInfluenceOnObjPos* pWrapInfluenceOnObjPosNew =
176 298 : &(rFmtNew.GetWrapInfluenceOnObjPos());
177 : // #i35017# - handle ITERATIVE as ONCE_SUCCESSIVE
178 596 : if ( pWrapInfluenceOnObjPosListed->GetWrapInfluenceOnObjPos( true ) !=
179 298 : pWrapInfluenceOnObjPosNew->GetWrapInfluenceOnObjPos( true ) )
180 : {
181 : // #i35017# - constant name has changed
182 0 : if ( pWrapInfluenceOnObjPosListed->GetWrapInfluenceOnObjPos( true )
183 : == text::WrapInfluenceOnPosition::ONCE_SUCCESSIVE )
184 0 : return true;
185 : else
186 0 : return false;
187 : }
188 :
189 : // objects anchored at the same content position/page/fly with same
190 : // wrap influence.
191 : // Thus, compare anchor order number
192 298 : return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
193 : }
194 : };
195 :
196 14036 : bool SwSortedObjs::Insert( SwAnchoredObject& _rAnchoredObj )
197 : {
198 : // #i51941#
199 14036 : if ( Contains( _rAnchoredObj ) )
200 : {
201 : // list already contains object
202 : OSL_FAIL( "<SwSortedObjs::Insert()> - already contains object" );
203 0 : return true;
204 : }
205 :
206 : // find insert position
207 : std::vector< SwAnchoredObject* >::iterator aInsPosIter =
208 : std::lower_bound( maSortedObjLst.begin(), maSortedObjLst.end(),
209 14036 : &_rAnchoredObj, ObjAnchorOrder() );
210 :
211 : // insert object into list
212 14036 : maSortedObjLst.insert( aInsPosIter, &_rAnchoredObj );
213 :
214 14036 : return Contains( _rAnchoredObj );
215 : }
216 :
217 9520 : bool SwSortedObjs::Remove( SwAnchoredObject& _rAnchoredObj )
218 : {
219 9520 : bool bRet = true;
220 :
221 : std::vector< SwAnchoredObject* >::iterator aDelPosIter =
222 9520 : std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
223 :
224 9520 : if ( aDelPosIter == maSortedObjLst.end() )
225 : {
226 : // object not found.
227 0 : bRet = false;
228 : OSL_FAIL( "<SwSortedObjs::Remove()> - object not found" );
229 : }
230 : else
231 : {
232 9520 : maSortedObjLst.erase( aDelPosIter );
233 : }
234 :
235 9520 : return bRet;
236 : }
237 :
238 28510 : bool SwSortedObjs::Contains( const SwAnchoredObject& _rAnchoredObj ) const
239 : {
240 : std::vector< SwAnchoredObject* >::const_iterator aIter =
241 28510 : std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
242 :
243 28510 : return aIter != maSortedObjLst.end();
244 : }
245 :
246 268 : bool SwSortedObjs::Update( SwAnchoredObject& _rAnchoredObj )
247 : {
248 268 : if ( !Contains( _rAnchoredObj ) )
249 : {
250 : // given anchored object not found in list
251 : OSL_FAIL( "<SwSortedObjs::Update(..) - sorted list doesn't contain given anchored object" );
252 0 : return false;
253 : }
254 :
255 268 : if ( size() == 1 )
256 : {
257 : // given anchored object is the only one in the list.
258 98 : return true;
259 : }
260 :
261 170 : Remove( _rAnchoredObj );
262 170 : Insert( _rAnchoredObj );
263 :
264 170 : return Contains( _rAnchoredObj );
265 : }
266 :
267 26802 : size_t SwSortedObjs::ListPosOf( const SwAnchoredObject& _rAnchoredObj ) const
268 : {
269 : std::vector< SwAnchoredObject* >::const_iterator aIter =
270 26802 : std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
271 :
272 26802 : if ( aIter != maSortedObjLst.end() )
273 : {
274 : // #i51941#
275 : std::vector< SwAnchoredObject* >::difference_type nPos =
276 26802 : aIter - maSortedObjLst.begin();
277 26802 : return static_cast<size_t>( nPos );
278 : }
279 :
280 0 : return size();
281 270 : }
282 :
283 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|