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 <tools/multisel.hxx>
21 : #include <doc.hxx>
22 : #include <IMark.hxx>
23 : #include <fldbas.hxx>
24 : #include <fmtfld.hxx>
25 : #include <fmtftn.hxx>
26 : #include <modeltoviewhelper.hxx>
27 : #include <ndtxt.hxx>
28 : #include <pam.hxx>
29 : #include <txatbase.hxx>
30 : #include <txtfld.hxx>
31 : #include <txtftn.hxx>
32 : #include <scriptinfo.hxx>
33 : #include <set>
34 : #include <vector>
35 :
36 11460 : struct FieldResult
37 : {
38 : sal_Int32 m_nFieldPos;
39 : OUString m_sExpand;
40 : enum { NONE, FIELD, FOOTNOTE } m_eType;
41 3820 : explicit FieldResult(sal_Int32 const nPos)
42 3820 : : m_nFieldPos(nPos), m_eType(NONE)
43 3820 : { }
44 : };
45 :
46 : class sortfieldresults :
47 : public std::binary_function<const FieldResult&, const FieldResult&, bool>
48 : {
49 : public:
50 12695 : bool operator()(const FieldResult &rOne, const FieldResult &rTwo) const
51 : {
52 12695 : return rOne.m_nFieldPos < rTwo.m_nFieldPos;
53 : }
54 : };
55 :
56 : typedef std::set<FieldResult, sortfieldresults> FieldResultSet;
57 :
58 19439 : struct block
59 : {
60 : sal_Int32 m_nStart;
61 : sal_Int32 m_nLen;
62 : bool m_bVisible;
63 : FieldResultSet m_aAttrs;
64 6441 : block(sal_Int32 nStart, sal_Int32 nLen, bool bVisible)
65 6441 : : m_nStart(nStart), m_nLen(nLen), m_bVisible(bVisible)
66 : {
67 6441 : }
68 : };
69 :
70 : struct containsPos
71 : {
72 : const sal_Int32 m_nPos;
73 3826 : containsPos(const sal_Int32 nPos)
74 3826 : : m_nPos(nPos)
75 : {
76 3826 : }
77 3858 : bool operator() (const block& rIn) const
78 : {
79 3858 : return m_nPos >= rIn.m_nStart && m_nPos < rIn.m_nStart + rIn.m_nLen;
80 : }
81 : };
82 :
83 6533 : ModelToViewHelper::ModelToViewHelper(const SwTxtNode &rNode, sal_uInt16 eMode)
84 : {
85 6533 : const OUString& rNodeText = rNode.GetTxt();
86 6533 : m_aRetText = rNodeText;
87 :
88 6533 : if (eMode == PASSTHROUGH)
89 6535 : return;
90 :
91 6531 : Range aRange( 0, rNodeText.isEmpty() ? 0 : rNodeText.getLength() - 1);
92 6531 : MultiSelection aHiddenMulti(aRange);
93 :
94 6531 : if (eMode & HIDEINVISIBLE)
95 1291 : SwScriptInfo::selectHiddenTextProperty(rNode, aHiddenMulti);
96 :
97 6531 : if (eMode & HIDEREDLINED)
98 1291 : SwScriptInfo::selectRedLineDeleted(rNode, aHiddenMulti);
99 :
100 13062 : std::vector<block> aBlocks;
101 :
102 6531 : sal_Int32 nShownStart = 0;
103 6557 : for (size_t i = 0; i < aHiddenMulti.GetRangeCount(); ++i)
104 : {
105 26 : const Range& rRange = aHiddenMulti.GetRange(i);
106 26 : const sal_Int32 nHiddenStart = rRange.Min();
107 26 : const sal_Int32 nHiddenEnd = rRange.Max() + 1;
108 26 : const sal_Int32 nHiddenLen = nHiddenEnd - nHiddenStart;
109 :
110 26 : const sal_Int32 nShownEnd = nHiddenStart;
111 26 : const sal_Int32 nShownLen = nShownEnd - nShownStart;
112 :
113 26 : if (nShownLen)
114 20 : aBlocks.push_back(block(nShownStart, nShownLen, true));
115 :
116 26 : if (nHiddenLen)
117 26 : aBlocks.push_back(block(nHiddenStart, nHiddenLen, false));
118 :
119 26 : nShownStart = nHiddenEnd;
120 : }
121 :
122 6531 : sal_Int32 nTrailingShownLen = rNodeText.getLength() - nShownStart;
123 6531 : if (nTrailingShownLen)
124 6395 : aBlocks.push_back(block(nShownStart, nTrailingShownLen, true));
125 :
126 6531 : if (eMode & EXPANDFIELDS || eMode & EXPANDFOOTNOTE)
127 : {
128 : //first the normal fields, get their position in the node and what the text they expand
129 : //to is
130 6525 : const SwpHints* pSwpHints2 = rNode.GetpSwpHints();
131 14103 : for ( size_t i = 0; pSwpHints2 && i < pSwpHints2->Count(); ++i )
132 : {
133 7578 : const SwTxtAttr* pAttr = (*pSwpHints2)[i];
134 7578 : if (pAttr->HasDummyChar())
135 : {
136 3834 : const sal_Int32 nDummyCharPos = pAttr->GetStart();
137 3834 : if (aHiddenMulti.IsSelected(nDummyCharPos))
138 12 : continue;
139 : std::vector<block>::iterator aFind = std::find_if(aBlocks.begin(),
140 3822 : aBlocks.end(), containsPos(nDummyCharPos));
141 3822 : if (aFind != aBlocks.end())
142 : {
143 3816 : FieldResult aFieldResult(nDummyCharPos);
144 3816 : switch (pAttr->Which())
145 : {
146 : case RES_TXTATR_FIELD:
147 : case RES_TXTATR_ANNOTATION:
148 344 : if (eMode & EXPANDFIELDS)
149 : {
150 1032 : aFieldResult.m_sExpand = (eMode & REPLACEMODE)
151 : ? OUString(CHAR_ZWSP)
152 344 : : static_txtattr_cast<SwTxtFld const*>(pAttr)->
153 688 : GetFmtFld().GetField()->ExpandField(true);
154 344 : aFieldResult.m_eType = FieldResult::FIELD;
155 : }
156 344 : break;
157 : case RES_TXTATR_FTN:
158 140 : if (eMode & EXPANDFOOTNOTE)
159 : {
160 132 : const SwFmtFtn& rFtn = static_cast<SwTxtFtn const*>(pAttr)->GetFtn();
161 132 : const SwDoc *pDoc = rNode.GetDoc();
162 264 : aFieldResult.m_sExpand = (eMode & REPLACEMODE)
163 : ? OUString(CHAR_ZWSP)
164 132 : : rFtn.GetViewNumStr(*pDoc);
165 132 : aFieldResult.m_eType = FieldResult::FOOTNOTE;
166 : }
167 140 : break;
168 : default:
169 3332 : break;
170 : }
171 3816 : aFind->m_aAttrs.insert(aFieldResult);
172 : }
173 : }
174 : }
175 :
176 6525 : if (eMode & EXPANDFIELDS)
177 : {
178 : //now get the dropdown formfields, get their position in the node and what the text they expand
179 : //to is
180 6525 : SwPaM aPaM(rNode, 0, rNode, rNode.Len());
181 : std::vector<sw::mark::IFieldmark*> aDropDowns =
182 13050 : rNode.GetDoc()->getIDocumentMarkAccess()->getDropDownsFor(aPaM);
183 :
184 6529 : for (std::vector<sw::mark::IFieldmark*>::iterator aI = aDropDowns.begin(), aEnd = aDropDowns.end();
185 : aI != aEnd; ++aI)
186 : {
187 4 : sw::mark::IFieldmark *pMark = *aI;
188 4 : const sal_Int32 nDummyCharPos = pMark->GetMarkPos().nContent.GetIndex()-1;
189 4 : if (aHiddenMulti.IsSelected(nDummyCharPos))
190 0 : continue;
191 : std::vector<block>::iterator aFind = std::find_if(aBlocks.begin(), aBlocks.end(),
192 4 : containsPos(nDummyCharPos));
193 4 : if (aFind != aBlocks.end())
194 : {
195 4 : FieldResult aFieldResult(nDummyCharPos);
196 8 : aFieldResult.m_sExpand = (eMode & REPLACEMODE)
197 : ? OUString(CHAR_ZWSP)
198 4 : : sw::mark::ExpandFieldmark(pMark);
199 4 : aFieldResult.m_eType = FieldResult::FIELD;
200 4 : aFind->m_aAttrs.insert(aFieldResult);
201 : }
202 6525 : }
203 : }
204 : }
205 :
206 6531 : sal_Int32 nOffset = 0;
207 12972 : for (std::vector<block>::iterator i = aBlocks.begin(); i != aBlocks.end(); ++i)
208 : {
209 6441 : if (!i->m_bVisible)
210 : {
211 26 : const sal_Int32 nHiddenStart = i->m_nStart;
212 26 : const sal_Int32 nHiddenLen = i->m_nLen;
213 :
214 26 : m_aRetText = m_aRetText.replaceAt( nOffset + nHiddenStart, nHiddenLen, OUString() );
215 26 : m_aMap.push_back( ConversionMapEntry( nHiddenStart, nOffset + nHiddenStart ) );
216 26 : nOffset -= nHiddenLen;
217 : }
218 : else
219 : {
220 10235 : for (FieldResultSet::iterator j = i->m_aAttrs.begin(); j != i->m_aAttrs.end(); ++j)
221 : {
222 3820 : sal_Int32 const viewPos(nOffset + j->m_nFieldPos);
223 3820 : m_aRetText = m_aRetText.replaceAt(viewPos, 1, j->m_sExpand);
224 3820 : m_aMap.push_back( ConversionMapEntry(j->m_nFieldPos, viewPos) );
225 3820 : switch (j->m_eType)
226 : {
227 : case FieldResult::FIELD:
228 348 : m_FieldPositions.push_back(viewPos);
229 348 : break;
230 : case FieldResult::FOOTNOTE:
231 132 : m_FootnotePositions.push_back(viewPos);
232 132 : break;
233 : case FieldResult::NONE: /*ignore*/
234 3340 : break;
235 : }
236 3820 : nOffset += j->m_sExpand.getLength() - 1;
237 : }
238 : }
239 : }
240 :
241 6531 : if ( !m_aMap.empty() )
242 7726 : m_aMap.push_back( ConversionMapEntry( rNodeText.getLength()+1, m_aRetText.getLength()+1 ) );
243 : }
244 :
245 : /** Converts a model position into a view position
246 : */
247 12484 : sal_Int32 ModelToViewHelper::ConvertToViewPosition( sal_Int32 nModelPos ) const
248 : {
249 : // Search for entry after nPos:
250 12484 : ConversionMap::const_iterator aIter;
251 16248 : for ( aIter = m_aMap.begin(); aIter != m_aMap.end(); ++aIter )
252 : {
253 5576 : if ( (*aIter).first >= nModelPos )
254 : {
255 1812 : const sal_Int32 nPosModel = (*aIter).first;
256 1812 : const sal_Int32 nPosExpand = (*aIter).second;
257 :
258 1812 : const sal_Int32 nDistToNextModel = nPosModel - nModelPos;
259 1812 : return nPosExpand - nDistToNextModel;
260 : }
261 : }
262 :
263 10672 : return nModelPos;
264 : }
265 :
266 : /** Converts a view position into a model position
267 : */
268 26287 : ModelToViewHelper::ModelPosition ModelToViewHelper::ConvertToModelPosition( sal_Int32 nViewPos ) const
269 : {
270 26287 : ModelPosition aRet;
271 26287 : aRet.mnPos = nViewPos;
272 :
273 : // Search for entry after nPos:
274 26287 : ConversionMap::const_iterator aIter;
275 26581 : for ( aIter = m_aMap.begin(); aIter != m_aMap.end(); ++aIter )
276 : {
277 646 : if ( (*aIter).second > nViewPos )
278 : {
279 352 : const sal_Int32 nPosModel = (*aIter).first;
280 352 : const sal_Int32 nPosExpand = (*aIter).second;
281 :
282 : // If nViewPos is in front of first field, we are finished.
283 352 : if ( aIter == m_aMap.begin() )
284 166 : break;
285 :
286 186 : --aIter;
287 :
288 : // nPrevPosModel is the field position
289 186 : const sal_Int32 nPrevPosModel = (*aIter).first;
290 186 : const sal_Int32 nPrevPosExpand = (*aIter).second;
291 :
292 186 : const sal_Int32 nLengthModel = nPosModel - nPrevPosModel;
293 186 : const sal_Int32 nLengthExpand = nPosExpand - nPrevPosExpand;
294 :
295 186 : const sal_Int32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1;
296 186 : const sal_Int32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand;
297 :
298 : // Check if nPos is outside of field:
299 186 : if ( nFieldEndExpand <= nViewPos )
300 : {
301 : // nPos is outside of field:
302 46 : const sal_Int32 nDistToField = nViewPos - nFieldEndExpand + 1;
303 46 : aRet.mnPos = nPrevPosModel + nDistToField;
304 : }
305 : else
306 : {
307 : // nViewPos is inside a field:
308 140 : aRet.mnPos = nPrevPosModel;
309 140 : aRet.mnSubPos = nViewPos - nPrevPosExpand;
310 140 : aRet.mbIsField = true;
311 : }
312 :
313 186 : break;
314 : }
315 : }
316 :
317 26287 : return aRet;
318 270 : }
319 :
320 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|