xref: /aoo42x/main/sw/source/core/inc/wrong.hxx (revision c0286415)
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	_WRONG_HXX
25 #define	_WRONG_HXX
26 
27 #ifndef _COM_SUN_STAR_SMARTTAGS_XSMARTTAGPROPERTIES_HPP_
28 #include <com/sun/star/container/XStringKeyMap.hpp>
29 #endif
30 
31 #include <com/sun/star/util/Color.hpp>
32 #include <com/sun/star/awt/FontUnderline.hpp>
33 #include <com/sun/star/uno/Any.hxx>
34 
35 #include <vector>
36 
37 #include <tools/string.hxx>
38 #include <tools/color.hxx>
39 #include <viewopt.hxx>
40 
41 class SwWrongList;
42 
43 enum WrongAreaLineType
44 {
45     WRONGAREA_DASHED,
46     WRONGAREA_WAVE,
47     WRONGAREA_WAVE_NORMAL,
48     WRONGAREA_WAVE_SMALL,
49     WRONGAREA_WAVE_FLAT,
50     WRONGAREA_NONE
51 };
52 
53 enum WrongListType
54 {
55     WRONGLIST_SPELL,
56     WRONGLIST_GRAMMAR,
57     WRONGLIST_SMARTTAG,
58     WRONGLIST_CHANGETRACKING
59 };
60 
61 
62 // ST2
63 class SwWrongArea
64 {
65 public:
66     rtl::OUString maType;
67     com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > mxPropertyBag;
68     xub_StrLen mnPos;
69     xub_StrLen mnLen;
70     SwWrongList* mpSubList;
71 
72     Color mColor;
73     WrongAreaLineType mLineType;
74 
75     SwWrongArea( const rtl::OUString& rType,
76                  WrongListType listType,
77                  com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag,
78                  xub_StrLen nPos,
79                  xub_StrLen nLen);
80 
81     SwWrongArea( const rtl::OUString& rType,
82                  com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag,
83                  xub_StrLen nPos,
84                  xub_StrLen nLen,
85                  SwWrongList* pSubList);
86 private:
87 
SwWrongArea()88     SwWrongArea() : mnPos(0), mnLen(0), mpSubList(NULL), mColor(0,0,0), mLineType(WRONGAREA_WAVE) {}
89 
getSmartColor(com::sun::star::uno::Reference<com::sun::star::container::XStringKeyMap> xPropertyBag)90     Color getSmartColor ( com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag)
91     {
92         try
93         {
94             if (xPropertyBag.is())
95             {
96                 const ::rtl::OUString colorKey  = ::rtl::OUString::createFromAscii ("LineColor");
97                 com::sun::star::uno::Any aLineColor = xPropertyBag->getValue(colorKey).get< com::sun::star::uno::Any>();
98                 com::sun::star::util::Color lineColor = 0;
99 
100                 if (aLineColor >>= lineColor)
101                 {
102                     return Color( lineColor );
103                 }
104             }
105         }
106         catch(::com::sun::star::container::NoSuchElementException& )
107         {
108         }
109         catch(::com::sun::star::uno::RuntimeException& )
110         {
111         }
112 
113         return SwViewOption::GetSmarttagColor( );
114     }
115 
getSmartLineType(com::sun::star::uno::Reference<com::sun::star::container::XStringKeyMap> xPropertyBag)116     WrongAreaLineType getSmartLineType( com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag )
117     {
118         try
119         {
120             if (xPropertyBag.is())
121             {
122                 const ::rtl::OUString typeKey  = ::rtl::OUString::createFromAscii ("LineType");
123                 com::sun::star::uno::Any aLineType = xPropertyBag->getValue(typeKey).get< com::sun::star::uno::Any>();
124                 ::sal_Int16 lineType = 0;
125 
126                 if (!(aLineType >>= lineType))
127                 {
128                     return WRONGAREA_DASHED;
129                 }
130                 if (::com::sun::star::awt::FontUnderline::WAVE == lineType)
131                 {
132                     return WRONGAREA_WAVE_NORMAL;
133                 }
134                 if (::com::sun::star::awt::FontUnderline::SMALLWAVE == lineType)
135                 {
136                     return WRONGAREA_WAVE_SMALL;
137                 }
138             }
139         }
140         catch(::com::sun::star::container::NoSuchElementException& )
141         {
142         }
143         catch(::com::sun::star::uno::RuntimeException& )
144         {
145         }
146 
147         return WRONGAREA_DASHED;
148     }
149 
getWrongAreaColor(WrongListType listType,com::sun::star::uno::Reference<com::sun::star::container::XStringKeyMap> xPropertyBag)150     Color getWrongAreaColor(WrongListType listType,
151                             com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag )
152     {
153         if (WRONGLIST_SPELL == listType)
154         {
155             return SwViewOption::GetSpellColor();
156         }
157         else if (WRONGLIST_GRAMMAR == listType)
158         {
159             return Color( COL_LIGHTBLUE );
160         }
161         else if (WRONGLIST_SMARTTAG == listType)
162         {
163             return  getSmartColor(xPropertyBag);
164         }
165 
166         return SwViewOption::GetSpellColor();
167     }
168 
getWrongAreaLineType(WrongListType listType,com::sun::star::uno::Reference<com::sun::star::container::XStringKeyMap> xPropertyBag)169     WrongAreaLineType getWrongAreaLineType(WrongListType listType,
170                                            com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag )
171     {
172         if (WRONGLIST_SPELL == listType)
173         {
174             return WRONGAREA_WAVE;
175         }
176         else if (WRONGLIST_GRAMMAR == listType)
177         {
178             return WRONGAREA_WAVE;
179         }
180         else if (WRONGLIST_SMARTTAG == listType)
181         {
182             return getSmartLineType(xPropertyBag);
183         }
184 
185         return WRONGAREA_WAVE;
186     }
187 
188 };
189 
190 class SwWrongList
191 {
192     std::vector<SwWrongArea> maList;
193     WrongListType            meType;
194 
195     xub_StrLen nBeginInvalid;   // Start des ungueltigen Bereichs
196     xub_StrLen nEndInvalid;     // Ende des ungueltigen Bereichs
197 
ShiftLeft(xub_StrLen & rPos,xub_StrLen nStart,xub_StrLen nEnd)198     void ShiftLeft( xub_StrLen &rPos, xub_StrLen nStart, xub_StrLen nEnd )
199     { if( rPos > nStart ) rPos = rPos > nEnd ? rPos - nEnd + nStart : nStart; }
ShiftRight(xub_StrLen & rPos,xub_StrLen nStart,xub_StrLen nEnd)200     void ShiftRight( xub_StrLen &rPos, xub_StrLen nStart, xub_StrLen nEnd )
201     { if( rPos >= nStart ) rPos += nStart - nEnd; }
202     void _Invalidate( xub_StrLen nBegin, xub_StrLen nEnd );
203 
204     void Insert(sal_uInt16 nWhere, std::vector<SwWrongArea>::iterator startPos, std::vector<SwWrongArea>::iterator endPos);
205     void Remove( sal_uInt16 nIdx, sal_uInt16 nLen );
206 
207     // forbidden and not implemented
208     SwWrongList& operator= (const SwWrongList &);
209     SwWrongList( const SwWrongList& rCpy );
210 
211 public:
212     SwWrongList( WrongListType eType );
213 
214     virtual ~SwWrongList();
215     virtual SwWrongList* Clone();
216     virtual void CopyFrom( const SwWrongList& rCopy );
217 
GetWrongListType() const218     inline WrongListType GetWrongListType() const { return meType; }
GetBeginInv() const219     inline xub_StrLen GetBeginInv() const { return nBeginInvalid; }
GetEndInv() const220     inline xub_StrLen GetEndInv() const { return nEndInvalid; }
InsideInvalid(xub_StrLen nChk) const221     inline sal_Bool InsideInvalid( xub_StrLen nChk ) const
222         { return nChk >= nBeginInvalid && nChk <= nEndInvalid; }
223     void SetInvalid( xub_StrLen nBegin, xub_StrLen nEnd );
Validate()224     inline void Validate(){ nBeginInvalid = STRING_LEN; }
225     void Invalidate( xub_StrLen nBegin, xub_StrLen nEnd );
226     sal_Bool InvalidateWrong();
227     sal_Bool Fresh( xub_StrLen &rStart, xub_StrLen &rEnd, xub_StrLen nPos,
228             xub_StrLen nLen, sal_uInt16 nIndex, xub_StrLen nCursorPos );
229     sal_uInt16 GetWrongPos( xub_StrLen nValue ) const;
230 
231     sal_Bool Check( xub_StrLen &rChk, xub_StrLen &rLn ) const;
232     sal_Bool InWrongWord( xub_StrLen &rChk, xub_StrLen &rLn ) const;
233     xub_StrLen NextWrong( xub_StrLen nChk ) const;
234 
235     void Move( xub_StrLen nPos, long nDiff );
236     void ClearList();
237 
238     // Divide the list into two part, the wrong words until nSplitPos will be
239     // removed and transferred to a new SwWrongList.
240     SwWrongList* SplitList( xub_StrLen nSplitPos );
241     // Join the next SwWrongList, nInsertPos is my own text length, where
242     // the other wrong list has to be inserted.
243     void JoinList( SwWrongList* pNext, xub_StrLen nInsertPos );
244 
Len(sal_uInt16 nIdx) const245     inline xub_StrLen Len( sal_uInt16 nIdx ) const
246     {
247         return nIdx < maList.size() ? maList[nIdx].mnLen : 0;
248     }
249 
Pos(sal_uInt16 nIdx) const250     inline xub_StrLen Pos( sal_uInt16 nIdx ) const
251     {
252         return nIdx < maList.size() ? maList[nIdx].mnPos : 0;
253     }
254 
Count() const255     inline sal_uInt16 Count() const { return (sal_uInt16)maList.size(); }
256 
Insert(const rtl::OUString & rType,com::sun::star::uno::Reference<com::sun::star::container::XStringKeyMap> xPropertyBag,xub_StrLen nNewPos,xub_StrLen nNewLen,sal_uInt16 nWhere)257     inline void Insert( const rtl::OUString& rType,
258                         com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag,
259                         xub_StrLen nNewPos, xub_StrLen nNewLen, sal_uInt16 nWhere )
260     {
261         std::vector<SwWrongArea>::iterator i = maList.begin();
262         if ( nWhere >= maList.size() )
263             i = maList.end(); // robust
264         else
265             i += nWhere;
266 
267         maList.insert(i, SwWrongArea( rType, meType, xPropertyBag, nNewPos, nNewLen) );
268     }
269 
270     void Insert( const rtl::OUString& rType,
271                  com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag,
272                  xub_StrLen nNewPos, xub_StrLen nNewLen );
273 
SubList(sal_uInt16 nIdx) const274     inline SwWrongList* SubList( sal_uInt16 nIdx ) const
275     {
276         return nIdx < maList.size() ? maList[nIdx].mpSubList : 0;
277     }
278 
279     void InsertSubList( xub_StrLen nNewPos, xub_StrLen nNewLen, sal_uInt16 nWhere, SwWrongList* pSubList );
280 
GetElement(sal_uInt16 nIdx) const281     inline const SwWrongArea* GetElement( sal_uInt16 nIdx ) const
282     {
283         return nIdx < maList.size() ? &maList[nIdx] : 0;
284     }
285     void RemoveEntry( xub_StrLen nBegin, xub_StrLen nEnd );
286     bool LookForEntry( xub_StrLen nBegin, xub_StrLen nEnd );
287 };
288 
289 #endif
290