xref: /aoo41x/main/sw/inc/modeltoviewhelper.hxx (revision cdf0e10c)
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 #ifndef _MODELTOVIEWHELPER_HXX
29 #define _MODELTOVIEWHELPER_HXX
30 
31 #include <sal/types.h>
32 
33 #include <vector>
34 
35 /** Some helpers for converting model strings to view strings.
36 
37     A paragraph string does not have its fields expanded, i.e., they are
38     represented by a special character inside the string with an additional
39     attribute assigned to it. For some tasks (e.g., SmartTags) it is required
40     to expand the fields to get the string as it appears in the view. Two
41     helper functions are provided to convert model positions to view positions
42     and vice versa.
43 */
44 namespace ModelToViewHelper
45 {
46     /** For each field in the model string, there is an entry in the conversion
47         map. The first value of the ConversionMapEntry points to the field
48         position in the model string, the second value points to the associated
49         position in the view string. The last entry in the conversion map
50         denotes the lengths of the model resp. view string.
51     */
52     typedef std::pair< sal_uInt32 , sal_uInt32 > ConversionMapEntry;
53     typedef std::vector< ConversionMapEntry > ConversionMap;
54 
55     /** This struct defines a position in the model string.
56 
57         The 'main' position is given by mnPos. If there's a field located at
58         this position, mbIsField is set and mnSubPos denotes the position inside
59         that field.
60     */
61     struct ModelPosition
62     {
63         sal_uInt32 mnPos;
64         sal_uInt32 mnSubPos;
65         bool mbIsField;
66 
67         ModelPosition() : mnPos(0), mnSubPos(0), mbIsField(false) {}
68     };
69 
70     /** Converts a model position into a view position
71 
72         @param pMap
73             pMap is the conversion map required for the calculation. If pMap is
74             0, no conversion takes place, i.e., it is assumed that the model
75             string is identical to the view string.
76 
77         @param nPos
78             nPos denotes a position in the model string which should be
79             converted. Note that converting model positions inside fields is
80             not supported, therefore nPos is not of type ModelPosition.
81 
82         @return
83             the position of nPos in the view string. In case the conversion
84             could not be performed (e.g., because there is not ConversionMap or
85             nPos is behind the last entry in the conversion map) nPos will
86             be returned.
87     */
88     sal_uInt32 ConvertToViewPosition( const ConversionMap* pMap, sal_uInt32 nModelPos );
89 
90     /** Converts a view position into a model position
91 
92         @param pMap
93             pMap is the conversion map required for the calculation. If pMap is
94             0, no conversion takes place, i.e., it is assumed that the model
95             string is identical to the view string.
96 
97         @param nPos
98             nPos denotes a position in the view string which should be
99             converted.
100 
101         @return
102             the position of nPos in the model string. In case the conversion
103             could not be performed (e.g., because there is not ConversionMap or
104             nPos is behind the last entry in the conversion map) a model
105             model position with mnPos = nPos and mnIsField = false will be
106             returned.
107     */
108     ModelPosition ConvertToModelPosition( const ConversionMap* pMap, sal_uInt32 nViewPos );
109 }
110 
111 #endif
112