1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sw.hxx"
30 #include <modeltoviewhelper.hxx>
31 
32 namespace ModelToViewHelper
33 {
34 
35 /** Converts a model position into a view position
36 */
37 sal_uInt32 ConvertToViewPosition( const ConversionMap* pMap, sal_uInt32 nModelPos )
38 {
39     sal_uInt32 nRet = nModelPos;
40 
41     if ( !pMap )
42         return nRet;
43 
44     // Search for entry behind nPos:
45     ConversionMap::const_iterator aIter;
46     for ( aIter = pMap->begin(); aIter != pMap->end(); ++aIter )
47     {
48         if ( (*aIter).first >= nModelPos )
49         {
50             const sal_uInt32 nPosModel  = (*aIter).first;
51             const sal_uInt32 nPosExpand = (*aIter).second;
52 
53             const sal_uInt32 nDistToNextModel  = nPosModel - nModelPos;
54             nRet = nPosExpand - nDistToNextModel;
55             break;
56         }
57     }
58 
59     return nRet;
60 }
61 
62 
63 /** Converts a view position into a model position
64 */
65 ModelPosition ConvertToModelPosition( const ConversionMap* pMap, sal_uInt32 nViewPos )
66 {
67     ModelPosition aRet;
68     aRet.mnPos = nViewPos;
69 
70     if ( !pMap )
71         return aRet;
72 
73     // Search for entry behind nPos:
74     ConversionMap::const_iterator aIter;
75     for ( aIter = pMap->begin(); aIter != pMap->end(); ++aIter )
76     {
77         if ( (*aIter).second > nViewPos )
78         {
79             const sal_uInt32 nPosModel  = (*aIter).first;
80             const sal_uInt32 nPosExpand = (*aIter).second;
81 
82             // If nViewPos is in front of first field, we are finished.
83             if ( aIter == pMap->begin() )
84                 break;
85 
86             --aIter;
87 
88             // nPrevPosModel is the field position
89             const sal_uInt32 nPrevPosModel  = (*aIter).first;
90             const sal_uInt32 nPrevPosExpand = (*aIter).second;
91 
92             const sal_uInt32 nLengthModel  = nPosModel - nPrevPosModel;
93             const sal_uInt32 nLengthExpand = nPosExpand - nPrevPosExpand;
94 
95             const sal_uInt32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1;
96             const sal_uInt32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand;
97 
98             // Check if nPos is outside of field:
99             if ( nFieldEndExpand <= nViewPos )
100             {
101                 // nPos is outside of field:
102                 const sal_uInt32 nDistToField = nViewPos - nFieldEndExpand + 1;
103                 aRet.mnPos  = nPrevPosModel + nDistToField;
104             }
105             else
106             {
107                 // nViewPos is inside a field:
108                 aRet.mnPos = nPrevPosModel;
109                 aRet.mnSubPos = nViewPos - nPrevPosExpand;
110                 aRet.mbIsField = true;
111             }
112 
113             break;
114         }
115     }
116 
117     return aRet;
118 }
119 
120 } // namespace ModelToViewStringConverter end
121