xref: /aoo4110/main/sw/inc/modeltoviewhelper.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _MODELTOVIEWHELPER_HXX
25 #define _MODELTOVIEWHELPER_HXX
26 
27 #include <sal/types.h>
28 
29 #include <vector>
30 
31 /** Some helpers for converting model strings to view strings.
32 
33     A paragraph string does not have its fields expanded, i.e., they are
34     represented by a special character inside the string with an additional
35     attribute assigned to it. For some tasks (e.g., SmartTags) it is required
36     to expand the fields to get the string as it appears in the view. Two
37     helper functions are provided to convert model positions to view positions
38     and vice versa.
39 */
40 namespace ModelToViewHelper
41 {
42     /** For each field in the model string, there is an entry in the conversion
43         map. The first value of the ConversionMapEntry points to the field
44         position in the model string, the second value points to the associated
45         position in the view string. The last entry in the conversion map
46         denotes the lengths of the model resp. view string.
47     */
48     typedef std::pair< sal_uInt32 , sal_uInt32 > ConversionMapEntry;
49     typedef std::vector< ConversionMapEntry > ConversionMap;
50 
51     /** This struct defines a position in the model string.
52 
53         The 'main' position is given by mnPos. If there's a field located at
54         this position, mbIsField is set and mnSubPos denotes the position inside
55         that field.
56     */
57     struct ModelPosition
58     {
59         sal_uInt32 mnPos;
60         sal_uInt32 mnSubPos;
61         bool mbIsField;
62 
ModelPositionModelToViewHelper::ModelPosition63         ModelPosition() : mnPos(0), mnSubPos(0), mbIsField(false) {}
64     };
65 
66     /** Converts a model position into a view position
67 
68         @param pMap
69             pMap is the conversion map required for the calculation. If pMap is
70             0, no conversion takes place, i.e., it is assumed that the model
71             string is identical to the view string.
72 
73         @param nPos
74             nPos denotes a position in the model string which should be
75             converted. Note that converting model positions inside fields is
76             not supported, therefore nPos is not of type ModelPosition.
77 
78         @return
79             the position of nPos in the view string. In case the conversion
80             could not be performed (e.g., because there is not ConversionMap or
81             nPos is behind the last entry in the conversion map) nPos will
82             be returned.
83     */
84     sal_uInt32 ConvertToViewPosition( const ConversionMap* pMap, sal_uInt32 nModelPos );
85 
86     /** Converts a view position into a model position
87 
88         @param pMap
89             pMap is the conversion map required for the calculation. If pMap is
90             0, no conversion takes place, i.e., it is assumed that the model
91             string is identical to the view string.
92 
93         @param nPos
94             nPos denotes a position in the view string which should be
95             converted.
96 
97         @return
98             the position of nPos in the model string. In case the conversion
99             could not be performed (e.g., because there is not ConversionMap or
100             nPos is behind the last entry in the conversion map) a model
101             model position with mnPos = nPos and mnIsField = false will be
102             returned.
103     */
104     ModelPosition ConvertToModelPosition( const ConversionMap* pMap, sal_uInt32 nViewPos );
105 }
106 
107 #endif
108