Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #ifndef _WRONG_HXX
21 : #define _WRONG_HXX
22 :
23 : #include <com/sun/star/container/XStringKeyMap.hpp>
24 :
25 : #include <com/sun/star/util/Color.hpp>
26 : #include <com/sun/star/awt/FontUnderline.hpp>
27 : #include <com/sun/star/uno/Any.hxx>
28 :
29 : #include <vector>
30 :
31 : #include <tools/string.hxx>
32 : #include <tools/color.hxx>
33 : #include <viewopt.hxx>
34 :
35 : class SwWrongList;
36 :
37 : enum WrongAreaLineType
38 : {
39 : WRONGAREA_DASHED,
40 : WRONGAREA_WAVE,
41 : WRONGAREA_WAVE_NORMAL,
42 : WRONGAREA_WAVE_SMALL,
43 : WRONGAREA_WAVE_FLAT,
44 : WRONGAREA_NONE
45 : };
46 :
47 : enum WrongListType
48 : {
49 : WRONGLIST_SPELL,
50 : WRONGLIST_GRAMMAR,
51 : WRONGLIST_SMARTTAG,
52 : WRONGLIST_CHANGETRACKING
53 : };
54 :
55 :
56 : // ST2
57 1277 : class SwWrongArea
58 : {
59 : public:
60 : OUString maType;
61 : com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > mxPropertyBag;
62 : xub_StrLen mnPos;
63 : xub_StrLen mnLen;
64 : SwWrongList* mpSubList;
65 :
66 : Color mColor;
67 : WrongAreaLineType mLineType;
68 :
69 : SwWrongArea( const OUString& rType,
70 : WrongListType listType,
71 : com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag,
72 : xub_StrLen nPos,
73 : xub_StrLen nLen);
74 :
75 : SwWrongArea( const OUString& rType,
76 : com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag,
77 : xub_StrLen nPos,
78 : xub_StrLen nLen,
79 : SwWrongList* pSubList);
80 : private:
81 :
82 : SwWrongArea() : mnPos(0), mnLen(0), mpSubList(NULL), mColor(0,0,0), mLineType(WRONGAREA_WAVE) {}
83 :
84 0 : Color getSmartColor ( com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag)
85 : {
86 : try
87 : {
88 0 : if (xPropertyBag.is())
89 : {
90 0 : const ::rtl::OUString colorKey("LineColor");
91 0 : com::sun::star::uno::Any aLineColor = xPropertyBag->getValue(colorKey).get< com::sun::star::uno::Any>();
92 0 : com::sun::star::util::Color lineColor = 0;
93 :
94 0 : if (aLineColor >>= lineColor)
95 : {
96 0 : return Color( lineColor );
97 0 : }
98 : }
99 : }
100 0 : catch(::com::sun::star::container::NoSuchElementException&)
101 : {
102 : }
103 0 : catch(::com::sun::star::uno::RuntimeException&)
104 : {
105 : }
106 :
107 0 : return SwViewOption::GetSmarttagColor( );
108 : }
109 :
110 0 : WrongAreaLineType getSmartLineType( com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag )
111 : {
112 : try
113 : {
114 0 : if (xPropertyBag.is())
115 : {
116 0 : const ::rtl::OUString typeKey("LineType");
117 0 : com::sun::star::uno::Any aLineType = xPropertyBag->getValue(typeKey).get< com::sun::star::uno::Any>();
118 0 : ::sal_Int16 lineType = 0;
119 :
120 0 : if (!(aLineType >>= lineType))
121 : {
122 0 : return WRONGAREA_DASHED;
123 : }
124 0 : if (::com::sun::star::awt::FontUnderline::WAVE == lineType)
125 : {
126 0 : return WRONGAREA_WAVE_NORMAL;
127 : }
128 0 : if (::com::sun::star::awt::FontUnderline::SMALLWAVE == lineType)
129 : {
130 0 : return WRONGAREA_WAVE_SMALL;
131 0 : }
132 : }
133 : }
134 0 : catch(::com::sun::star::container::NoSuchElementException&)
135 : {
136 : }
137 0 : catch(::com::sun::star::uno::RuntimeException&)
138 : {
139 : }
140 :
141 0 : return WRONGAREA_DASHED;
142 : }
143 :
144 284 : Color getWrongAreaColor(WrongListType listType,
145 : com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag )
146 : {
147 284 : if (WRONGLIST_SPELL == listType)
148 : {
149 284 : return SwViewOption::GetSpellColor();
150 : }
151 0 : else if (WRONGLIST_GRAMMAR == listType)
152 : {
153 0 : return Color( COL_LIGHTBLUE );
154 : }
155 0 : else if (WRONGLIST_SMARTTAG == listType)
156 : {
157 0 : return getSmartColor(xPropertyBag);
158 : }
159 :
160 0 : return SwViewOption::GetSpellColor();
161 : }
162 :
163 284 : WrongAreaLineType getWrongAreaLineType(WrongListType listType,
164 : com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag )
165 : {
166 284 : if (WRONGLIST_SPELL == listType)
167 : {
168 284 : return WRONGAREA_WAVE;
169 : }
170 0 : else if (WRONGLIST_GRAMMAR == listType)
171 : {
172 0 : return WRONGAREA_WAVE;
173 : }
174 0 : else if (WRONGLIST_SMARTTAG == listType)
175 : {
176 0 : return getSmartLineType(xPropertyBag);
177 : }
178 :
179 0 : return WRONGAREA_WAVE;
180 : }
181 :
182 : };
183 :
184 : class SwWrongList
185 : {
186 : std::vector<SwWrongArea> maList;
187 : WrongListType meType;
188 :
189 : xub_StrLen nBeginInvalid; // Start of the invalid range
190 : xub_StrLen nEndInvalid; // End of the invalid range
191 :
192 1044 : void ShiftLeft( xub_StrLen &rPos, xub_StrLen nStart, xub_StrLen nEnd )
193 1044 : { if( rPos > nStart ) rPos = rPos > nEnd ? rPos - nEnd + nStart : nStart; }
194 : void ShiftRight( xub_StrLen &rPos, xub_StrLen nStart, xub_StrLen nEnd )
195 : { if( rPos >= nStart ) rPos += nStart - nEnd; }
196 : void _Invalidate( xub_StrLen nBegin, xub_StrLen nEnd );
197 :
198 : void Insert(sal_uInt16 nWhere, std::vector<SwWrongArea>::iterator startPos, std::vector<SwWrongArea>::iterator endPos);
199 : void Remove( sal_uInt16 nIdx, sal_uInt16 nLen );
200 :
201 : // forbidden and not implemented
202 : SwWrongList& operator= (const SwWrongList &);
203 : SwWrongList( const SwWrongList& rCpy );
204 :
205 : public:
206 : SwWrongList( WrongListType eType );
207 :
208 : virtual ~SwWrongList();
209 : virtual SwWrongList* Clone();
210 : virtual void CopyFrom( const SwWrongList& rCopy );
211 :
212 0 : inline WrongListType GetWrongListType() const { return meType; }
213 10654 : inline xub_StrLen GetBeginInv() const { return nBeginInvalid; }
214 4079 : inline xub_StrLen GetEndInv() const { return nEndInvalid; }
215 : inline sal_Bool InsideInvalid( xub_StrLen nChk ) const
216 : { return nChk >= nBeginInvalid && nChk <= nEndInvalid; }
217 : void SetInvalid( xub_StrLen nBegin, xub_StrLen nEnd );
218 0 : inline void Validate(){ nBeginInvalid = STRING_LEN; }
219 : void Invalidate( xub_StrLen nBegin, xub_StrLen nEnd );
220 : sal_Bool InvalidateWrong();
221 : sal_Bool Fresh( xub_StrLen &rStart, xub_StrLen &rEnd, xub_StrLen nPos,
222 : xub_StrLen nLen, sal_uInt16 nIndex, xub_StrLen nCursorPos );
223 : sal_uInt16 GetWrongPos( xub_StrLen nValue ) const;
224 :
225 : sal_Bool Check( xub_StrLen &rChk, xub_StrLen &rLn ) const;
226 : sal_Bool InWrongWord( xub_StrLen &rChk, xub_StrLen &rLn ) const;
227 : xub_StrLen NextWrong( xub_StrLen nChk ) const;
228 :
229 : void Move( xub_StrLen nPos, long nDiff );
230 : void ClearList();
231 :
232 : // Divide the list into two part, the wrong words until nSplitPos will be
233 : // removed and transferred to a new SwWrongList.
234 : SwWrongList* SplitList( xub_StrLen nSplitPos );
235 : // Join the next SwWrongList, nInsertPos is my own text length, where
236 : // the other wrong list has to be inserted.
237 : void JoinList( SwWrongList* pNext, xub_StrLen nInsertPos );
238 :
239 2659 : inline xub_StrLen Len( sal_uInt16 nIdx ) const
240 : {
241 2659 : return nIdx < maList.size() ? maList[nIdx].mnLen : 0;
242 : }
243 :
244 4361 : inline xub_StrLen Pos( sal_uInt16 nIdx ) const
245 : {
246 4361 : return nIdx < maList.size() ? maList[nIdx].mnPos : 0;
247 : }
248 :
249 34052 : inline sal_uInt16 Count() const { return (sal_uInt16)maList.size(); }
250 :
251 284 : inline void Insert( const OUString& rType,
252 : com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag,
253 : xub_StrLen nNewPos, xub_StrLen nNewLen, sal_uInt16 nWhere )
254 : {
255 284 : std::vector<SwWrongArea>::iterator i = maList.begin();
256 284 : if ( nWhere >= maList.size() )
257 269 : i = maList.end(); // robust
258 : else
259 15 : i += nWhere;
260 :
261 284 : maList.insert(i, SwWrongArea( rType, meType, xPropertyBag, nNewPos, nNewLen) );
262 284 : }
263 :
264 : void Insert( const OUString& rType,
265 : com::sun::star::uno::Reference< com::sun::star::container::XStringKeyMap > xPropertyBag,
266 : xub_StrLen nNewPos, xub_StrLen nNewLen );
267 :
268 0 : inline SwWrongList* SubList( sal_uInt16 nIdx ) const
269 : {
270 0 : return nIdx < maList.size() ? maList[nIdx].mpSubList : 0;
271 : }
272 :
273 : void InsertSubList( xub_StrLen nNewPos, xub_StrLen nNewLen, sal_uInt16 nWhere, SwWrongList* pSubList );
274 :
275 543 : inline const SwWrongArea* GetElement( sal_uInt16 nIdx ) const
276 : {
277 543 : return nIdx < maList.size() ? &maList[nIdx] : 0;
278 : }
279 : void RemoveEntry( xub_StrLen nBegin, xub_StrLen nEnd );
280 : bool LookForEntry( xub_StrLen nBegin, xub_StrLen nEnd );
281 : };
282 :
283 : #endif
284 :
285 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|