1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SW_SOURCE_CORE_INC_WRONG_HXX
21 #define INCLUDED_SW_SOURCE_CORE_INC_WRONG_HXX
23 #include <com/sun/star/container/NoSuchElementException.hpp>
24 #include <com/sun/star/container/XStringKeyMap.hpp>
26 #include <com/sun/star/util/Color.hpp>
27 #include <com/sun/star/awt/FontUnderline.hpp>
28 #include <com/sun/star/uno/Any.hxx>
34 #include <tools/color.hxx>
35 #include <swtypes.hxx>
36 #include <viewopt.hxx>
37 #include "TextFrameIndex.hxx"
40 // For MSVC (without /vmg) SwTextNode must consistently be defined for
41 // WrongListIterator::m_pGetWrongList of pointer-to-SwTextNode-member type to consistently have the
42 // same size in all translation units that include this file:
48 enum WrongAreaLineType
62 WRONGLIST_CHANGETRACKING
70 css::uno::Reference
< css::container::XStringKeyMap
> mxPropertyBag
;
73 SwWrongList
* mpSubList
;
76 WrongAreaLineType mLineType
;
78 SwWrongArea( const OUString
& rType
,
79 WrongListType listType
,
80 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
,
84 SwWrongArea( const OUString
& rType
,
85 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
,
88 SwWrongList
* pSubList
);
91 static Color
getGrammarColor ( css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
95 if (xPropertyBag
.is())
97 css::uno::Any aLineColor
= xPropertyBag
->getValue("LineColor");
98 css::util::Color lineColor
= 0;
100 if (aLineColor
>>= lineColor
)
102 return Color( lineColor
);
106 catch(const css::container::NoSuchElementException
&)
109 catch(const css::uno::RuntimeException
&)
113 return COL_LIGHTBLUE
;
116 static WrongAreaLineType
getGrammarLineType( css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
120 if (xPropertyBag
.is())
122 css::uno::Any aLineType
= xPropertyBag
->getValue("LineType");
123 ::sal_Int16 lineType
= 0;
125 if (!(aLineType
>>= lineType
))
127 return WRONGAREA_WAVE
;
129 if (css::awt::FontUnderline::BOLDWAVE
== lineType
)
131 return WRONGAREA_BOLDWAVE
;
133 if (css::awt::FontUnderline::BOLD
== lineType
)
135 return WRONGAREA_BOLD
;
137 if (css::awt::FontUnderline::DASH
== lineType
)
139 return WRONGAREA_DASHED
;
141 if (css::awt::FontUnderline::SMALLWAVE
== lineType
)
143 return WRONGAREA_WAVE
; //Code draws wave height based on space that fits.
147 catch(const css::container::NoSuchElementException
&)
150 catch(const css::uno::RuntimeException
&)
154 return WRONGAREA_WAVE
;
157 static Color
getSmartColor ( css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
161 if (xPropertyBag
.is())
163 css::uno::Any aLineColor
= xPropertyBag
->getValue("LineColor");
164 css::util::Color lineColor
= 0;
166 if (aLineColor
>>= lineColor
)
168 return Color( lineColor
);
172 catch(const css::container::NoSuchElementException
&)
175 catch(const css::uno::RuntimeException
&)
179 return SwViewOption::GetSmarttagColor( );
182 static WrongAreaLineType
getSmartLineType( css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
186 if (xPropertyBag
.is())
188 css::uno::Any aLineType
= xPropertyBag
->getValue("LineType");
189 ::sal_Int16 lineType
= 0;
191 if (!(aLineType
>>= lineType
))
193 return WRONGAREA_DASHED
;
195 if (css::awt::FontUnderline::WAVE
== lineType
)
197 return WRONGAREA_WAVE
;
199 if (css::awt::FontUnderline::BOLDWAVE
== lineType
)
201 return WRONGAREA_BOLDWAVE
;
203 if (css::awt::FontUnderline::BOLD
== lineType
)
205 return WRONGAREA_BOLD
;
207 if (css::awt::FontUnderline::SMALLWAVE
== lineType
)
209 return WRONGAREA_WAVE
; //Code draws wave height based on space that fits.
213 catch(const css::container::NoSuchElementException
&)
216 catch(const css::uno::RuntimeException
&)
220 return WRONGAREA_DASHED
;
223 static Color
getWrongAreaColor(WrongListType listType
,
224 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
226 if (WRONGLIST_SPELL
== listType
)
228 return SwViewOption::GetSpellColor();
230 else if (WRONGLIST_GRAMMAR
== listType
)
232 return getGrammarColor(xPropertyBag
);
234 else if (WRONGLIST_SMARTTAG
== listType
)
236 return getSmartColor(xPropertyBag
);
239 return SwViewOption::GetSpellColor();
242 static WrongAreaLineType
getWrongAreaLineType(WrongListType listType
,
243 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
245 if (WRONGLIST_SPELL
== listType
)
247 return WRONGAREA_WAVE
;
249 else if (WRONGLIST_GRAMMAR
== listType
)
251 return getGrammarLineType(xPropertyBag
);
253 else if (WRONGLIST_SMARTTAG
== listType
)
255 return getSmartLineType(xPropertyBag
);
258 return WRONGAREA_WAVE
;
265 std::vector
<SwWrongArea
> maList
;
266 WrongListType meType
;
268 sal_Int32 mnBeginInvalid
; // Start of the invalid range
269 sal_Int32 mnEndInvalid
; // End of the invalid range
271 static void ShiftLeft( sal_Int32
&rPos
, sal_Int32 nStart
, sal_Int32 nEnd
)
272 { if( rPos
> nStart
) rPos
= rPos
> nEnd
? rPos
- nEnd
+ nStart
: nStart
; }
273 void Invalidate_( sal_Int32 nBegin
, sal_Int32 nEnd
);
275 void Insert(sal_uInt16 nWhere
, std::vector
<SwWrongArea
>::iterator startPos
, std::vector
<SwWrongArea
>::iterator
const & endPos
);
276 void Remove( sal_uInt16 nIdx
, sal_uInt16 nLen
);
278 SwWrongList
& operator= (const SwWrongList
&) = delete;
279 SwWrongList( const SwWrongList
& rCpy
) = delete;
282 SwWrongList( WrongListType eType
);
284 virtual ~SwWrongList();
285 virtual SwWrongList
* Clone();
286 virtual void CopyFrom( const SwWrongList
& rCopy
);
288 WrongListType
GetWrongListType() const { return meType
; }
289 sal_Int32
GetBeginInv() const { return mnBeginInvalid
; }
290 sal_Int32
GetEndInv() const { return mnEndInvalid
; }
291 void SetInvalid( sal_Int32 nBegin
, sal_Int32 nEnd
);
292 void Validate(){ mnBeginInvalid
= mnEndInvalid
= COMPLETE_STRING
; }
293 void Invalidate( sal_Int32 nBegin
, sal_Int32 nEnd
);
294 bool InvalidateWrong();
295 enum class FreshState
{ FRESH
, CURSOR
, NOTHING
};
296 FreshState
Fresh( sal_Int32
&rStart
, sal_Int32
&rEnd
, sal_Int32 nPos
,
297 sal_Int32 nLen
, sal_uInt16 nIndex
, sal_Int32 nCursorPos
);
298 sal_uInt16
GetWrongPos( sal_Int32 nValue
) const;
300 bool Check( sal_Int32
&rChk
, sal_Int32
&rLn
) const;
301 bool InWrongWord( sal_Int32
&rChk
, sal_Int32
&rLn
) const;
302 sal_Int32
NextWrong( sal_Int32 nChk
) const;
304 void Move( sal_Int32 nPos
, sal_Int32 nDiff
);
307 // Divide the list into two part, the wrong words until nSplitPos will be
308 // removed and transferred to a new SwWrongList.
309 SwWrongList
* SplitList( sal_Int32 nSplitPos
);
310 // Join the next SwWrongList, nInsertPos is my own text length, where
311 // the other wrong list has to be inserted.
312 void JoinList( SwWrongList
* pNext
, sal_Int32 nInsertPos
);
314 sal_Int32
Len( sal_uInt16 nIdx
) const
316 return nIdx
< maList
.size() ? maList
[nIdx
].mnLen
: 0;
319 sal_Int32
Pos( sal_uInt16 nIdx
) const
321 return nIdx
< maList
.size() ? maList
[nIdx
].mnPos
: 0;
324 sal_uInt16
Count() const { return static_cast<sal_uInt16
>(maList
.size()); }
326 void Insert( const OUString
& rType
,
327 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
,
328 sal_Int32 nNewPos
, sal_Int32 nNewLen
, sal_uInt16 nWhere
)
330 std::vector
<SwWrongArea
>::iterator i
= maList
.begin();
331 if ( nWhere
>= maList
.size() )
332 i
= maList
.end(); // robust
336 maList
.insert(i
, SwWrongArea( rType
, meType
, xPropertyBag
, nNewPos
, nNewLen
) );
339 void Insert( const OUString
& rType
,
340 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
,
341 sal_Int32 nNewPos
, sal_Int32 nNewLen
);
343 SwWrongList
* SubList( sal_uInt16 nIdx
) const
345 return nIdx
< maList
.size() ? maList
[nIdx
].mpSubList
: nullptr;
348 void InsertSubList( sal_Int32 nNewPos
, sal_Int32 nNewLen
, sal_uInt16 nWhere
, SwWrongList
* pSubList
);
350 const SwWrongArea
* GetElement( sal_uInt16 nIdx
) const
352 return nIdx
< maList
.size() ? &maList
[nIdx
] : nullptr;
354 void RemoveEntry( sal_Int32 nBegin
, sal_Int32 nEnd
);
355 bool LookForEntry( sal_Int32 nBegin
, sal_Int32 nEnd
);
365 class WrongListIteratorBase
368 SwWrongList
const* (SwTextNode::*const m_pGetWrongList
)() const;
369 sw::MergedPara
const*const m_pMergedPara
;
370 size_t m_CurrentExtent
;
371 TextFrameIndex m_CurrentIndex
;
372 SwWrongList
const*const m_pWrongList
;
375 /// for the text frame
376 WrongListIteratorBase(SwTextFrame
const& rFrame
,
377 SwWrongList
const* (SwTextNode::*pGetWrongList
)() const);
379 WrongListIteratorBase(SwWrongList
const& rWrongList
);
382 class WrongListIterator
383 : public WrongListIteratorBase
386 /// for the text frame
387 WrongListIterator(SwTextFrame
const& rFrame
,
388 SwWrongList
const* (SwTextNode::*pGetWrongList
)() const);
390 WrongListIterator(SwWrongList
const& rWrongList
);
392 bool Check(TextFrameIndex
&rStart
, TextFrameIndex
&rLen
);
393 const SwWrongArea
* GetWrongElement(TextFrameIndex nStart
);
395 bool LooksUseful() { return m_pMergedPara
|| m_pWrongList
; }
398 class WrongListIteratorCounter
399 : public WrongListIteratorBase
402 WrongListIteratorCounter(SwTextFrame
const& rFrame
,
403 SwWrongList
const* (SwTextNode::*pGetWrongList
)() const);
404 WrongListIteratorCounter(SwWrongList
const& rWrongList
);
406 sal_uInt16
GetElementCount();
407 std::optional
<std::pair
<TextFrameIndex
, TextFrameIndex
>> GetElementAt(sal_uInt16 nIndex
);
414 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */