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