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 0 : SwSortedObjs::SwSortedObjs()
35 : {
36 0 : }
37 :
38 0 : SwSortedObjs::~SwSortedObjs()
39 : {
40 0 : }
41 :
42 0 : sal_uInt32 SwSortedObjs::Count() const
43 : {
44 0 : return maSortedObjLst.size();
45 : }
46 :
47 0 : SwAnchoredObject* SwSortedObjs::operator[]( sal_uInt32 _nIndex ) const
48 : {
49 0 : SwAnchoredObject* pAnchoredObj = 0L;
50 :
51 0 : if ( _nIndex >= Count() )
52 : {
53 : OSL_FAIL( "<SwSortedObjs::operator[]> - index out of range" );
54 : }
55 : else
56 : {
57 0 : pAnchoredObj = maSortedObjLst[ _nIndex ];
58 : }
59 :
60 0 : return pAnchoredObj;
61 : }
62 :
63 : struct ObjAnchorOrder
64 : {
65 0 : bool operator()( const SwAnchoredObject* _pListedAnchoredObj,
66 : const SwAnchoredObject* _pNewAnchoredObj )
67 : {
68 : // get attributes of listed object
69 0 : const SwFrmFmt& rFmtListed = _pListedAnchoredObj->GetFrmFmt();
70 0 : const SwFmtAnchor* pAnchorListed = &(rFmtListed.GetAnchor());
71 :
72 : // get attributes of new object
73 0 : const SwFrmFmt& rFmtNew = _pNewAnchoredObj->GetFrmFmt();
74 0 : const SwFmtAnchor* pAnchorNew = &(rFmtNew.GetAnchor());
75 :
76 : // check for to-page anchored objects
77 0 : if ((pAnchorListed->GetAnchorId() == FLY_AT_PAGE) &&
78 0 : (pAnchorNew ->GetAnchorId() != FLY_AT_PAGE))
79 : {
80 0 : return true;
81 : }
82 0 : else if ((pAnchorListed->GetAnchorId() != FLY_AT_PAGE) &&
83 0 : (pAnchorNew ->GetAnchorId() == FLY_AT_PAGE))
84 : {
85 0 : return false;
86 : }
87 0 : else if ((pAnchorListed->GetAnchorId() == FLY_AT_PAGE) &&
88 0 : (pAnchorNew ->GetAnchorId() == FLY_AT_PAGE))
89 : {
90 0 : return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
91 : }
92 :
93 : // Both objects aren't anchored to page.
94 : // Thus, check for to-fly anchored objects
95 0 : if ((pAnchorListed->GetAnchorId() == FLY_AT_FLY) &&
96 0 : (pAnchorNew ->GetAnchorId() != FLY_AT_FLY))
97 : {
98 0 : return true;
99 : }
100 0 : else if ((pAnchorListed->GetAnchorId() != FLY_AT_FLY) &&
101 0 : (pAnchorNew ->GetAnchorId() == FLY_AT_FLY))
102 : {
103 0 : return false;
104 : }
105 0 : else if ((pAnchorListed->GetAnchorId() == FLY_AT_FLY) &&
106 0 : (pAnchorNew ->GetAnchorId() == FLY_AT_FLY))
107 : {
108 0 : 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 0 : const SwPosition* pCntntAnchorListed = pAnchorListed->GetCntntAnchor();
114 0 : const SwPosition* pCntntAnchorNew = pAnchorNew->GetCntntAnchor();
115 0 : if ( pCntntAnchorListed && pCntntAnchorNew &&
116 0 : pCntntAnchorListed->nNode != pCntntAnchorNew->nNode )
117 : {
118 0 : 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 0 : if ((pAnchorListed->GetAnchorId() != FLY_AT_PARA) &&
126 0 : (pAnchorNew ->GetAnchorId() != FLY_AT_PARA) &&
127 0 : pCntntAnchorListed && pCntntAnchorNew )
128 : {
129 0 : if ( pCntntAnchorListed->nContent != pCntntAnchorNew->nContent )
130 : {
131 0 : return pCntntAnchorListed->nContent < pCntntAnchorNew->nContent;
132 : }
133 0 : else if ((pAnchorListed->GetAnchorId() == FLY_AT_CHAR) &&
134 0 : (pAnchorNew ->GetAnchorId() == FLY_AS_CHAR))
135 : {
136 0 : return true;
137 : }
138 0 : else if ((pAnchorListed->GetAnchorId() == FLY_AS_CHAR) &&
139 0 : (pAnchorNew ->GetAnchorId() == FLY_AT_CHAR))
140 : {
141 0 : 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 0 : const IDocumentDrawModelAccess* pIDDMA = rFmtListed.getIDocumentDrawModelAccess();
149 0 : const SdrLayerID nHellId = pIDDMA->GetHellId();
150 0 : const SdrLayerID nInvisibleHellId = pIDDMA->GetInvisibleHellId();
151 : const bool bWrapThroughOrHellListed =
152 0 : rFmtListed.GetSurround().GetSurround() == SURROUND_THROUGHT ||
153 0 : _pListedAnchoredObj->GetDrawObj()->GetLayer() == nHellId ||
154 0 : _pListedAnchoredObj->GetDrawObj()->GetLayer() == nInvisibleHellId;
155 : const bool bWrapThroughOrHellNew =
156 0 : rFmtNew.GetSurround().GetSurround() == SURROUND_THROUGHT ||
157 0 : _pNewAnchoredObj->GetDrawObj()->GetLayer() == nHellId ||
158 0 : _pNewAnchoredObj->GetDrawObj()->GetLayer() == nInvisibleHellId;
159 0 : if ( bWrapThroughOrHellListed != bWrapThroughOrHellNew )
160 : {
161 0 : if ( bWrapThroughOrHellListed )
162 0 : return false;
163 : else
164 0 : return true;
165 : }
166 0 : else if ( bWrapThroughOrHellListed && bWrapThroughOrHellNew )
167 : {
168 0 : 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 0 : &(rFmtListed.GetWrapInfluenceOnObjPos());
175 : const SwFmtWrapInfluenceOnObjPos* pWrapInfluenceOnObjPosNew =
176 0 : &(rFmtNew.GetWrapInfluenceOnObjPos());
177 : // #i35017# - handle ITERATIVE as ONCE_SUCCESSIVE
178 0 : if ( pWrapInfluenceOnObjPosListed->GetWrapInfluenceOnObjPos( true ) !=
179 0 : 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 0 : return pAnchorListed->GetOrder() < pAnchorNew->GetOrder();
193 : }
194 : };
195 :
196 0 : bool SwSortedObjs::Insert( SwAnchoredObject& _rAnchoredObj )
197 : {
198 : // #i51941#
199 0 : 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 0 : &_rAnchoredObj, ObjAnchorOrder() );
210 :
211 : // insert object into list
212 0 : maSortedObjLst.insert( aInsPosIter, &_rAnchoredObj );
213 :
214 0 : return Contains( _rAnchoredObj );
215 : }
216 :
217 0 : bool SwSortedObjs::Remove( SwAnchoredObject& _rAnchoredObj )
218 : {
219 0 : bool bRet = true;
220 :
221 : std::vector< SwAnchoredObject* >::iterator aDelPosIter =
222 0 : std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
223 :
224 0 : 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 0 : maSortedObjLst.erase( aDelPosIter );
233 : }
234 :
235 0 : return bRet;
236 : }
237 :
238 0 : bool SwSortedObjs::Contains( const SwAnchoredObject& _rAnchoredObj ) const
239 : {
240 : std::vector< SwAnchoredObject* >::const_iterator aIter =
241 0 : std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
242 :
243 0 : return aIter != maSortedObjLst.end();
244 : }
245 :
246 0 : bool SwSortedObjs::Update( SwAnchoredObject& _rAnchoredObj )
247 : {
248 0 : 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 0 : if ( Count() == 1 )
256 : {
257 : // given anchored object is the only one in the list.
258 0 : return true;
259 : }
260 :
261 0 : Remove( _rAnchoredObj );
262 0 : Insert( _rAnchoredObj );
263 :
264 0 : return Contains( _rAnchoredObj );
265 : }
266 :
267 0 : sal_uInt32 SwSortedObjs::ListPosOf( const SwAnchoredObject& _rAnchoredObj ) const
268 : {
269 0 : sal_uInt32 nRetLstPos = Count();
270 :
271 : std::vector< SwAnchoredObject* >::const_iterator aIter =
272 0 : std::find( maSortedObjLst.begin(), maSortedObjLst.end(), &_rAnchoredObj );
273 :
274 0 : if ( aIter != maSortedObjLst.end() )
275 : {
276 : // #i51941#
277 : std::vector< SwAnchoredObject* >::difference_type nPos =
278 0 : aIter - maSortedObjLst.begin();
279 0 : nRetLstPos = sal_uInt32( nPos );
280 : }
281 :
282 0 : return nRetLstPos;
283 : }
284 :
285 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|