xref: /AOO41X/main/sw/source/core/txtnode/modeltoviewhelper.cxx (revision efeef26f81c84063fb0a91bde3856d4a51172d90)
1*efeef26fSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*efeef26fSAndrew Rist  * distributed with this work for additional information
6*efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9*efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15*efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17*efeef26fSAndrew Rist  * specific language governing permissions and limitations
18*efeef26fSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*efeef26fSAndrew Rist  *************************************************************/
21*efeef26fSAndrew Rist 
22*efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir #include <modeltoviewhelper.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir namespace ModelToViewHelper
29cdf0e10cSrcweir {
30cdf0e10cSrcweir 
31cdf0e10cSrcweir /** Converts a model position into a view position
32cdf0e10cSrcweir */
ConvertToViewPosition(const ConversionMap * pMap,sal_uInt32 nModelPos)33cdf0e10cSrcweir sal_uInt32 ConvertToViewPosition( const ConversionMap* pMap, sal_uInt32 nModelPos )
34cdf0e10cSrcweir {
35cdf0e10cSrcweir     sal_uInt32 nRet = nModelPos;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir     if ( !pMap )
38cdf0e10cSrcweir         return nRet;
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     // Search for entry behind nPos:
41cdf0e10cSrcweir     ConversionMap::const_iterator aIter;
42cdf0e10cSrcweir     for ( aIter = pMap->begin(); aIter != pMap->end(); ++aIter )
43cdf0e10cSrcweir     {
44cdf0e10cSrcweir         if ( (*aIter).first >= nModelPos )
45cdf0e10cSrcweir         {
46cdf0e10cSrcweir             const sal_uInt32 nPosModel  = (*aIter).first;
47cdf0e10cSrcweir             const sal_uInt32 nPosExpand = (*aIter).second;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir             const sal_uInt32 nDistToNextModel  = nPosModel - nModelPos;
50cdf0e10cSrcweir             nRet = nPosExpand - nDistToNextModel;
51cdf0e10cSrcweir             break;
52cdf0e10cSrcweir         }
53cdf0e10cSrcweir     }
54cdf0e10cSrcweir 
55cdf0e10cSrcweir     return nRet;
56cdf0e10cSrcweir }
57cdf0e10cSrcweir 
58cdf0e10cSrcweir 
59cdf0e10cSrcweir /** Converts a view position into a model position
60cdf0e10cSrcweir */
ConvertToModelPosition(const ConversionMap * pMap,sal_uInt32 nViewPos)61cdf0e10cSrcweir ModelPosition ConvertToModelPosition( const ConversionMap* pMap, sal_uInt32 nViewPos )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     ModelPosition aRet;
64cdf0e10cSrcweir     aRet.mnPos = nViewPos;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     if ( !pMap )
67cdf0e10cSrcweir         return aRet;
68cdf0e10cSrcweir 
69cdf0e10cSrcweir     // Search for entry behind nPos:
70cdf0e10cSrcweir     ConversionMap::const_iterator aIter;
71cdf0e10cSrcweir     for ( aIter = pMap->begin(); aIter != pMap->end(); ++aIter )
72cdf0e10cSrcweir     {
73cdf0e10cSrcweir         if ( (*aIter).second > nViewPos )
74cdf0e10cSrcweir         {
75cdf0e10cSrcweir             const sal_uInt32 nPosModel  = (*aIter).first;
76cdf0e10cSrcweir             const sal_uInt32 nPosExpand = (*aIter).second;
77cdf0e10cSrcweir 
78cdf0e10cSrcweir             // If nViewPos is in front of first field, we are finished.
79cdf0e10cSrcweir             if ( aIter == pMap->begin() )
80cdf0e10cSrcweir                 break;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir             --aIter;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir             // nPrevPosModel is the field position
85cdf0e10cSrcweir             const sal_uInt32 nPrevPosModel  = (*aIter).first;
86cdf0e10cSrcweir             const sal_uInt32 nPrevPosExpand = (*aIter).second;
87cdf0e10cSrcweir 
88cdf0e10cSrcweir             const sal_uInt32 nLengthModel  = nPosModel - nPrevPosModel;
89cdf0e10cSrcweir             const sal_uInt32 nLengthExpand = nPosExpand - nPrevPosExpand;
90cdf0e10cSrcweir 
91cdf0e10cSrcweir             const sal_uInt32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1;
92cdf0e10cSrcweir             const sal_uInt32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand;
93cdf0e10cSrcweir 
94cdf0e10cSrcweir             // Check if nPos is outside of field:
95cdf0e10cSrcweir             if ( nFieldEndExpand <= nViewPos )
96cdf0e10cSrcweir             {
97cdf0e10cSrcweir                 // nPos is outside of field:
98cdf0e10cSrcweir                 const sal_uInt32 nDistToField = nViewPos - nFieldEndExpand + 1;
99cdf0e10cSrcweir                 aRet.mnPos  = nPrevPosModel + nDistToField;
100cdf0e10cSrcweir             }
101cdf0e10cSrcweir             else
102cdf0e10cSrcweir             {
103cdf0e10cSrcweir                 // nViewPos is inside a field:
104cdf0e10cSrcweir                 aRet.mnPos = nPrevPosModel;
105cdf0e10cSrcweir                 aRet.mnSubPos = nViewPos - nPrevPosExpand;
106cdf0e10cSrcweir                 aRet.mbIsField = true;
107cdf0e10cSrcweir             }
108cdf0e10cSrcweir 
109cdf0e10cSrcweir             break;
110cdf0e10cSrcweir         }
111cdf0e10cSrcweir     }
112cdf0e10cSrcweir 
113cdf0e10cSrcweir     return aRet;
114cdf0e10cSrcweir }
115cdf0e10cSrcweir 
116cdf0e10cSrcweir } // namespace ModelToViewStringConverter end
117