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 .
22 #include <com/sun/star/container/NoSuchElementException.hpp>
23 #include <com/sun/star/container/XStringKeyMap.hpp>
25 #include <com/sun/star/awt/FontUnderline.hpp>
26 #include <com/sun/star/uno/Any.hxx>
32 #include <tools/color.hxx>
33 #include <swtypes.hxx>
34 #include <viewopt.hxx>
35 #include "TextFrameIndex.hxx"
38 // For MSVC (without /vmg) SwTextNode must consistently be defined for
39 // WrongListIterator::m_pGetWrongList of pointer-to-SwTextNode-member type to consistently have the
40 // same size in all translation units that include this file:
46 enum WrongAreaLineType
60 WRONGLIST_CHANGETRACKING
68 css::uno::Reference
< css::container::XStringKeyMap
> mxPropertyBag
;
71 SwWrongList
* mpSubList
;
74 WrongAreaLineType mLineType
;
76 SwWrongArea( OUString aType
,
77 WrongListType listType
,
78 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
,
82 SwWrongArea( OUString aType
,
83 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
,
86 SwWrongList
* pSubList
);
89 static Color
getGrammarColor ( css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
93 if (xPropertyBag
.is())
95 css::uno::Any aLineColor
= xPropertyBag
->getValue(u
"LineColor"_ustr
);
98 if (aLineColor
>>= lineColor
)
104 catch(const css::container::NoSuchElementException
&)
107 catch(const css::uno::RuntimeException
&)
111 return SwViewOption::GetCurrentViewOptions().GetGrammarColor();
114 static WrongAreaLineType
getGrammarLineType( css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
118 if (xPropertyBag
.is())
120 css::uno::Any aLineType
= xPropertyBag
->getValue(u
"LineType"_ustr
);
121 ::sal_Int16 lineType
= 0;
123 if (!(aLineType
>>= lineType
))
125 return WRONGAREA_WAVE
;
127 if (css::awt::FontUnderline::BOLDWAVE
== lineType
)
129 return WRONGAREA_BOLDWAVE
;
131 if (css::awt::FontUnderline::BOLD
== lineType
)
133 return WRONGAREA_BOLD
;
135 if (css::awt::FontUnderline::DASH
== lineType
)
137 return WRONGAREA_DASHED
;
139 if (css::awt::FontUnderline::SMALLWAVE
== lineType
)
141 return WRONGAREA_WAVE
; //Code draws wave height based on space that fits.
145 catch(const css::container::NoSuchElementException
&)
148 catch(const css::uno::RuntimeException
&)
152 return WRONGAREA_WAVE
;
155 static Color
getSmartColor ( css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
159 if (xPropertyBag
.is())
161 css::uno::Any aLineColor
= xPropertyBag
->getValue(u
"LineColor"_ustr
);
164 if (aLineColor
>>= lineColor
)
170 catch(const css::container::NoSuchElementException
&)
173 catch(const css::uno::RuntimeException
&)
177 return SwViewOption::GetCurrentViewOptions().GetSmarttagColor();
180 static WrongAreaLineType
getSmartLineType( css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
184 if (xPropertyBag
.is())
186 css::uno::Any aLineType
= xPropertyBag
->getValue(u
"LineType"_ustr
);
187 ::sal_Int16 lineType
= 0;
189 if (!(aLineType
>>= lineType
))
191 return WRONGAREA_DASHED
;
193 if (css::awt::FontUnderline::WAVE
== lineType
)
195 return WRONGAREA_WAVE
;
197 if (css::awt::FontUnderline::BOLDWAVE
== lineType
)
199 return WRONGAREA_BOLDWAVE
;
201 if (css::awt::FontUnderline::BOLD
== lineType
)
203 return WRONGAREA_BOLD
;
205 if (css::awt::FontUnderline::SMALLWAVE
== lineType
)
207 return WRONGAREA_WAVE
; //Code draws wave height based on space that fits.
211 catch(const css::container::NoSuchElementException
&)
214 catch(const css::uno::RuntimeException
&)
218 return WRONGAREA_DASHED
;
221 static Color
getWrongAreaColor(WrongListType listType
,
222 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
224 if (WRONGLIST_SPELL
== listType
)
226 return SwViewOption::GetCurrentViewOptions().GetSpellColor();
228 else if (WRONGLIST_GRAMMAR
== listType
)
230 return getGrammarColor(xPropertyBag
);
232 else if (WRONGLIST_SMARTTAG
== listType
)
234 return getSmartColor(xPropertyBag
);
237 return SwViewOption::GetCurrentViewOptions().GetSpellColor();
240 static WrongAreaLineType
getWrongAreaLineType(WrongListType listType
,
241 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
)
243 if (WRONGLIST_SPELL
== listType
)
245 return WRONGAREA_WAVE
;
247 else if (WRONGLIST_GRAMMAR
== listType
)
249 return getGrammarLineType(xPropertyBag
);
251 else if (WRONGLIST_SMARTTAG
== listType
)
253 return getSmartLineType(xPropertyBag
);
256 return WRONGAREA_WAVE
;
261 class SAL_DLLPUBLIC_RTTI SwWrongList
263 std::vector
<SwWrongArea
> maList
;
264 WrongListType meType
;
266 sal_Int32 mnBeginInvalid
; // Start of the invalid range
267 sal_Int32 mnEndInvalid
; // End of the invalid range
269 static void ShiftLeft( sal_Int32
&rPos
, sal_Int32 nStart
, sal_Int32 nEnd
)
270 { if( rPos
> nStart
) rPos
= rPos
> nEnd
? rPos
- nEnd
+ nStart
: nStart
; }
271 void Invalidate_( sal_Int32 nBegin
, sal_Int32 nEnd
);
273 void Insert(sal_uInt16 nWhere
, std::vector
<SwWrongArea
>::iterator startPos
, std::vector
<SwWrongArea
>::iterator
const & endPos
);
274 void Remove( sal_uInt16 nIdx
, sal_uInt16 nLen
);
276 SwWrongList
& operator= (const SwWrongList
&) = delete;
277 SwWrongList( const SwWrongList
& rCpy
) = delete;
280 SwWrongList( WrongListType eType
);
282 virtual ~SwWrongList();
283 virtual SwWrongList
* Clone();
284 virtual void CopyFrom( const SwWrongList
& rCopy
);
286 WrongListType
GetWrongListType() const { return meType
; }
287 sal_Int32
GetBeginInv() const { return mnBeginInvalid
; }
288 sal_Int32
GetEndInv() const { return mnEndInvalid
; }
289 void SetInvalid( sal_Int32 nBegin
, sal_Int32 nEnd
);
290 void Validate(){ mnBeginInvalid
= mnEndInvalid
= COMPLETE_STRING
; }
291 void Invalidate( sal_Int32 nBegin
, sal_Int32 nEnd
);
292 bool InvalidateWrong();
293 enum class FreshState
{ FRESH
, CURSOR
, NOTHING
};
294 FreshState
Fresh( sal_Int32
&rStart
, sal_Int32
&rEnd
, sal_Int32 nPos
,
295 sal_Int32 nLen
, sal_uInt16 nIndex
, sal_Int32 nCursorPos
);
296 sal_uInt16
GetWrongPos( sal_Int32 nValue
) const;
298 bool Check( sal_Int32
&rChk
, sal_Int32
&rLn
) const;
299 bool InWrongWord( sal_Int32
&rChk
, sal_Int32
&rLn
) const;
300 sal_Int32
NextWrong( sal_Int32 nChk
) const;
302 void Move( sal_Int32 nPos
, sal_Int32 nDiff
);
305 // Divide the list into two part, the wrong words until nSplitPos will be
306 // removed and transferred to a new SwWrongList.
307 std::unique_ptr
<SwWrongList
> SplitList( sal_Int32 nSplitPos
);
308 // Join the next SwWrongList, nInsertPos is my own text length, where
309 // the other wrong list has to be inserted.
310 void JoinList( SwWrongList
* pNext
, sal_Int32 nInsertPos
);
312 sal_Int32
Len( sal_uInt16 nIdx
) const
314 return nIdx
< maList
.size() ? maList
[nIdx
].mnLen
: 0;
317 sal_Int32
Pos( sal_uInt16 nIdx
) const
319 return nIdx
< maList
.size() ? maList
[nIdx
].mnPos
: 0;
322 sal_uInt16
Count() const { return o3tl::narrowing
<sal_uInt16
>(maList
.size()); }
324 void Insert( const OUString
& rType
,
325 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
,
326 sal_Int32 nNewPos
, sal_Int32 nNewLen
, sal_uInt16 nWhere
)
328 std::vector
<SwWrongArea
>::iterator i
= maList
.begin();
329 if ( nWhere
>= maList
.size() )
330 i
= maList
.end(); // robust
334 maList
.insert(i
, SwWrongArea( rType
, meType
, xPropertyBag
, nNewPos
, nNewLen
) );
337 void Insert( const OUString
& rType
,
338 css::uno::Reference
< css::container::XStringKeyMap
> const & xPropertyBag
,
339 sal_Int32 nNewPos
, sal_Int32 nNewLen
);
341 SwWrongList
* SubList( sal_uInt16 nIdx
) const
343 return nIdx
< maList
.size() ? maList
[nIdx
].mpSubList
: nullptr;
346 void InsertSubList( sal_Int32 nNewPos
, sal_Int32 nNewLen
, sal_uInt16 nWhere
, SwWrongList
* pSubList
);
348 const SwWrongArea
* GetElement( sal_uInt16 nIdx
) const
350 return nIdx
< maList
.size() ? &maList
[nIdx
] : nullptr;
352 void RemoveEntry( sal_Int32 nBegin
, sal_Int32 nEnd
);
353 bool LookForEntry( sal_Int32 nBegin
, sal_Int32 nEnd
);
363 class WrongListIteratorBase
366 SwWrongList
const* (SwTextNode::*const m_pGetWrongList
)() const;
367 sw::MergedPara
const*const m_pMergedPara
;
368 size_t m_CurrentExtent
;
369 TextFrameIndex m_CurrentIndex
;
370 SwWrongList
const*const m_pWrongList
;
373 /// for the text frame
374 WrongListIteratorBase(SwTextFrame
const& rFrame
,
375 SwWrongList
const* (SwTextNode::*pGetWrongList
)() const);
377 WrongListIteratorBase(SwWrongList
const& rWrongList
);
380 class WrongListIterator
381 : public WrongListIteratorBase
384 /// for the text frame
385 WrongListIterator(SwTextFrame
const& rFrame
,
386 SwWrongList
const* (SwTextNode::*pGetWrongList
)() const);
388 WrongListIterator(SwWrongList
const& rWrongList
);
390 bool Check(TextFrameIndex
&rStart
, TextFrameIndex
&rLen
);
391 const SwWrongArea
* GetWrongElement(TextFrameIndex nStart
);
393 bool LooksUseful() { return m_pMergedPara
|| m_pWrongList
; }
396 class WrongListIteratorCounter
397 : public WrongListIteratorBase
400 WrongListIteratorCounter(SwTextFrame
const& rFrame
,
401 SwWrongList
const* (SwTextNode::*pGetWrongList
)() const);
402 WrongListIteratorCounter(SwWrongList
const& rWrongList
);
404 sal_uInt16
GetElementCount();
405 std::optional
<std::pair
<TextFrameIndex
, TextFrameIndex
>> GetElementAt(sal_uInt16 nIndex
);
410 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */