bump product version to 7.6.3.2-android
[LibreOffice.git] / editeng / source / uno / unoedhlp.cxx
blob2a1b1e2bd54f89ee27c44cefd404585943a1137a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #include <memory>
21 #include <editeng/unoedhlp.hxx>
22 #include <editeng/editdata.hxx>
23 #include <editeng/editeng.hxx>
24 #include <svl/itemset.hxx>
26 #include <osl/diagnose.h>
29 SvxEditSourceHint::SvxEditSourceHint( SfxHintId _nId ) :
30 TextHint( _nId ),
31 mnStart( 0 ),
32 mnEnd( 0 )
36 SvxEditSourceHint::SvxEditSourceHint( SfxHintId _nId, sal_Int32 nValue, sal_Int32 nStart, sal_Int32 nEnd ) :
37 TextHint( _nId, nValue ),
38 mnStart( nStart),
39 mnEnd( nEnd )
44 std::unique_ptr<SfxHint> SvxEditSourceHelper::EENotification2Hint( EENotify const * aNotify )
46 if( aNotify )
48 switch( aNotify->eNotificationType )
50 case EE_NOTIFY_TEXTMODIFIED:
51 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextModified, aNotify->nParagraph ) );
53 case EE_NOTIFY_PARAGRAPHINSERTED:
54 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextParaInserted, aNotify->nParagraph ) );
56 case EE_NOTIFY_PARAGRAPHREMOVED:
57 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextParaRemoved, aNotify->nParagraph ) );
59 case EE_NOTIFY_PARAGRAPHSMOVED:
60 return std::unique_ptr<SfxHint>( new SvxEditSourceHint( SfxHintId::EditSourceParasMoved, aNotify->nParagraph, aNotify->nParam1, aNotify->nParam2 ) );
62 case EE_NOTIFY_TextHeightChanged:
63 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextHeightChanged, aNotify->nParagraph ) );
65 case EE_NOTIFY_TEXTVIEWSCROLLED:
66 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextViewScrolled ) );
68 case EE_NOTIFY_TEXTVIEWSELECTIONCHANGED:
69 return std::unique_ptr<SfxHint>( new SvxEditSourceHint( SfxHintId::EditSourceSelectionChanged ) );
71 case EE_NOTIFY_PROCESSNOTIFICATIONS:
72 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextProcessNotifications ));
74 case EE_NOTIFY_TEXTVIEWSELECTIONCHANGED_ENDD_PARA:
75 return std::unique_ptr<SfxHint>( new SvxEditSourceHintEndPara );
76 default:
77 OSL_FAIL( "SvxEditSourceHelper::EENotification2Hint unknown notification" );
78 break;
82 return std::make_unique<SfxHint>( );
85 void SvxEditSourceHelper::GetAttributeRun( sal_Int32& nStartIndex, sal_Int32& nEndIndex, const EditEngine& rEE, sal_Int32 nPara, sal_Int32 nIndex, bool bInCell )
87 // IA2 CWS introduced bInCell, but also did many other changes here.
88 // Need to verify implementation with AT (IA2 and ATK)
89 // Old implementation at the end of the method for reference...
91 //added dummy attributes for the default text
92 std::vector<EECharAttrib> aCharAttribs, aTempCharAttribs;
93 rEE.GetCharAttribs( nPara, aTempCharAttribs );
95 if (!aTempCharAttribs.empty())
97 sal_Int32 nIndex2 = 0;
98 sal_Int32 nParaLen = rEE.GetTextLen(nPara);
99 for (size_t nAttr = 0; nAttr < aTempCharAttribs.size(); ++nAttr)
101 if (nIndex2 < aTempCharAttribs[nAttr].nStart)
103 EECharAttrib aEEAttr(nIndex2, aTempCharAttribs[nAttr].nStart);
104 aCharAttribs.insert(aCharAttribs.begin() + nAttr, aEEAttr);
106 nIndex2 = aTempCharAttribs[nAttr].nEnd;
107 aCharAttribs.push_back(aTempCharAttribs[nAttr]);
109 if ( nIndex2 != nParaLen )
111 EECharAttrib aEEAttr(nIndex2, nParaLen);
112 aCharAttribs.push_back(aEEAttr);
115 // find closest index in front of nIndex
116 sal_Int32 nCurrIndex;
117 sal_Int32 nClosestStartIndex_s = 0, nClosestStartIndex_e = 0;
118 for (auto const& charAttrib : aCharAttribs)
120 nCurrIndex = charAttrib.nStart;
122 if( nCurrIndex > nClosestStartIndex_s &&
123 nCurrIndex <= nIndex)
125 nClosestStartIndex_s = nCurrIndex;
127 nCurrIndex = charAttrib.nEnd;
128 if ( nCurrIndex > nClosestStartIndex_e &&
129 nCurrIndex < nIndex )
131 nClosestStartIndex_e = nCurrIndex;
134 sal_Int32 nClosestStartIndex = std::max(nClosestStartIndex_s, nClosestStartIndex_e);
136 // find closest index behind of nIndex
137 sal_Int32 nClosestEndIndex_s, nClosestEndIndex_e;
138 nClosestEndIndex_s = nClosestEndIndex_e = rEE.GetTextLen(nPara);
139 for (auto const& charAttrib : aCharAttribs)
141 nCurrIndex = charAttrib.nEnd;
143 if( nCurrIndex > nIndex &&
144 nCurrIndex < nClosestEndIndex_e )
146 nClosestEndIndex_e = nCurrIndex;
148 nCurrIndex = charAttrib.nStart;
149 if ( nCurrIndex > nIndex &&
150 nCurrIndex < nClosestEndIndex_s)
152 nClosestEndIndex_s = nCurrIndex;
155 sal_Int32 nClosestEndIndex = std::min(nClosestEndIndex_s, nClosestEndIndex_e);
157 nStartIndex = nClosestStartIndex;
158 nEndIndex = nClosestEndIndex;
160 if ( !bInCell )
161 return;
163 EPosition aStartPos( nPara, nStartIndex ), aEndPos( nPara, nEndIndex );
164 sal_Int32 nParaCount = rEE.GetParagraphCount();
165 sal_Int32 nCrrntParaLen = rEE.GetTextLen(nPara);
166 //need to find closest index in front of nIndex in the previous paragraphs
167 if ( aStartPos.nIndex == 0 )
169 SfxItemSet aCrrntSet = rEE.GetAttribs( nPara, 0, 1, GetAttribsFlags::CHARATTRIBS );
170 for ( sal_Int32 nParaIdx = nPara-1; nParaIdx >= 0; nParaIdx-- )
172 sal_uInt32 nLen = rEE.GetTextLen(nParaIdx);
173 if ( nLen )
175 sal_Int32 nStartIdx, nEndIdx;
176 GetAttributeRun( nStartIdx, nEndIdx, rEE, nParaIdx, nLen );
177 SfxItemSet aSet = rEE.GetAttribs( nParaIdx, nLen-1, nLen, GetAttribsFlags::CHARATTRIBS );
178 if ( aSet == aCrrntSet )
180 aStartPos.nPara = nParaIdx;
181 aStartPos.nIndex = nStartIdx;
182 if ( aStartPos.nIndex != 0 )
184 break;
190 //need find closest index behind nIndex in the following paragraphs
191 if ( aEndPos.nIndex == nCrrntParaLen )
193 SfxItemSet aCrrntSet = rEE.GetAttribs( nPara, nCrrntParaLen-1, nCrrntParaLen, GetAttribsFlags::CHARATTRIBS );
194 for ( sal_Int32 nParaIdx = nPara+1; nParaIdx < nParaCount; nParaIdx++ )
196 sal_Int32 nLen = rEE.GetTextLen( nParaIdx );
197 if ( nLen )
199 sal_Int32 nStartIdx, nEndIdx;
200 GetAttributeRun( nStartIdx, nEndIdx, rEE, nParaIdx, 0 );
201 SfxItemSet aSet = rEE.GetAttribs( nParaIdx, 0, 1, GetAttribsFlags::CHARATTRIBS );
202 if ( aSet == aCrrntSet )
204 aEndPos.nPara = nParaIdx;
205 aEndPos.nIndex = nEndIdx;
206 if ( aEndPos.nIndex != nLen )
208 break;
214 nStartIndex = 0;
215 if ( aStartPos.nPara > 0 )
217 for ( sal_Int32 i = 0; i < aStartPos.nPara; i++ )
219 nStartIndex += rEE.GetTextLen(i)+1;
222 nStartIndex += aStartPos.nIndex;
223 nEndIndex = 0;
224 if ( aEndPos.nPara > 0 )
226 for ( sal_Int32 i = 0; i < aEndPos.nPara; i++ )
228 nEndIndex += rEE.GetTextLen(i)+1;
231 nEndIndex += aEndPos.nIndex;
234 Point SvxEditSourceHelper::EEToUserSpace( const Point& rPoint, const Size& rEESize, bool bIsVertical )
236 return bIsVertical ? Point( -rPoint.Y() + rEESize.Height(), rPoint.X() ) : rPoint;
239 Point SvxEditSourceHelper::UserSpaceToEE( const Point& rPoint, const Size& rEESize, bool bIsVertical )
241 return bIsVertical ? Point( rPoint.Y(), -rPoint.X() + rEESize.Height() ) : rPoint;
244 tools::Rectangle SvxEditSourceHelper::EEToUserSpace( const tools::Rectangle& rRect, const Size& rEESize, bool bIsVertical )
246 return bIsVertical ? tools::Rectangle( EEToUserSpace(rRect.BottomLeft(), rEESize, bIsVertical),
247 EEToUserSpace(rRect.TopRight(), rEESize, bIsVertical) ) : rRect;
250 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */