Update ooo320-m1
[ooovba.git] / sw / source / core / txtnode / modeltoviewhelper.cxx
blobd904f8795745c651f038f4c985ca507989da501d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: modeltoviewhelper.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sw.hxx"
33 #include <modeltoviewhelper.hxx>
35 namespace ModelToViewHelper
38 /** Converts a model position into a view position
40 sal_uInt32 ConvertToViewPosition( const ConversionMap* pMap, sal_uInt32 nModelPos )
42 sal_uInt32 nRet = nModelPos;
44 if ( !pMap )
45 return nRet;
47 // Search for entry behind nPos:
48 ConversionMap::const_iterator aIter;
49 for ( aIter = pMap->begin(); aIter != pMap->end(); ++aIter )
51 if ( (*aIter).first >= nModelPos )
53 const sal_uInt32 nPosModel = (*aIter).first;
54 const sal_uInt32 nPosExpand = (*aIter).second;
56 const sal_uInt32 nDistToNextModel = nPosModel - nModelPos;
57 nRet = nPosExpand - nDistToNextModel;
58 break;
62 return nRet;
66 /** Converts a view position into a model position
68 ModelPosition ConvertToModelPosition( const ConversionMap* pMap, sal_uInt32 nViewPos )
70 ModelPosition aRet;
71 aRet.mnPos = nViewPos;
73 if ( !pMap )
74 return aRet;
76 // Search for entry behind nPos:
77 ConversionMap::const_iterator aIter;
78 for ( aIter = pMap->begin(); aIter != pMap->end(); ++aIter )
80 if ( (*aIter).second > nViewPos )
82 const sal_uInt32 nPosModel = (*aIter).first;
83 const sal_uInt32 nPosExpand = (*aIter).second;
85 // If nViewPos is in front of first field, we are finished.
86 if ( aIter == pMap->begin() )
87 break;
89 --aIter;
91 // nPrevPosModel is the field position
92 const sal_uInt32 nPrevPosModel = (*aIter).first;
93 const sal_uInt32 nPrevPosExpand = (*aIter).second;
95 const sal_uInt32 nLengthModel = nPosModel - nPrevPosModel;
96 const sal_uInt32 nLengthExpand = nPosExpand - nPrevPosExpand;
98 const sal_uInt32 nFieldLengthExpand = nLengthExpand - nLengthModel + 1;
99 const sal_uInt32 nFieldEndExpand = nPrevPosExpand + nFieldLengthExpand;
101 // Check if nPos is outside of field:
102 if ( nFieldEndExpand <= nViewPos )
104 // nPos is outside of field:
105 const sal_uInt32 nDistToField = nViewPos - nFieldEndExpand + 1;
106 aRet.mnPos = nPrevPosModel + nDistToField;
108 else
110 // nViewPos is inside a field:
111 aRet.mnPos = nPrevPosModel;
112 aRet.mnSubPos = nViewPos - nPrevPosExpand;
113 aRet.mbIsField = true;
116 break;
120 return aRet;
123 } // namespace ModelToViewStringConverter end