LibreOffice Module editeng (master) 1
unoedhlp.cxx
Go to the documentation of this file.
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#include <memory>
21#include <editeng/unoedhlp.hxx>
22#include <editeng/editdata.hxx>
23#include <editeng/editeng.hxx>
24#include <svl/itemset.hxx>
25
26#include <osl/diagnose.h>
27
28
30 TextHint( _nId ),
31 mnStart( 0 ),
32 mnEnd( 0 )
33{
34}
35
36SvxEditSourceHint::SvxEditSourceHint( SfxHintId _nId, sal_Int32 nValue, sal_Int32 nStart, sal_Int32 nEnd ) :
37 TextHint( _nId, nValue ),
38 mnStart( nStart),
39 mnEnd( nEnd )
40{
41}
42
43
44std::unique_ptr<SfxHint> SvxEditSourceHelper::EENotification2Hint( EENotify const * aNotify )
45{
46 if( aNotify )
47 {
48 switch( aNotify->eNotificationType )
49 {
51 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextModified, aNotify->nParagraph ) );
52
54 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextParaInserted, aNotify->nParagraph ) );
55
57 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextParaRemoved, aNotify->nParagraph ) );
58
60 return std::unique_ptr<SfxHint>( new SvxEditSourceHint( SfxHintId::EditSourceParasMoved, aNotify->nParagraph, aNotify->nParam1, aNotify->nParam2 ) );
61
63 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextHeightChanged, aNotify->nParagraph ) );
64
66 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextViewScrolled ) );
67
69 return std::unique_ptr<SfxHint>( new SvxEditSourceHint( SfxHintId::EditSourceSelectionChanged ) );
70
72 return std::unique_ptr<SfxHint>( new TextHint( SfxHintId::TextProcessNotifications ));
73
75 return std::unique_ptr<SfxHint>( new SvxEditSourceHintEndPara );
76 default:
77 OSL_FAIL( "SvxEditSourceHelper::EENotification2Hint unknown notification" );
78 break;
79 }
80 }
81
82 return std::make_unique<SfxHint>( );
83}
84
85void SvxEditSourceHelper::GetAttributeRun( sal_Int32& nStartIndex, sal_Int32& nEndIndex, const EditEngine& rEE, sal_Int32 nPara, sal_Int32 nIndex, bool bInCell )
86{
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...
90
91 //added dummy attributes for the default text
92 std::vector<EECharAttrib> aCharAttribs, aTempCharAttribs;
93 rEE.GetCharAttribs( nPara, aTempCharAttribs );
94
95 if (!aTempCharAttribs.empty())
96 {
97 sal_Int32 nIndex2 = 0;
98 sal_Int32 nParaLen = rEE.GetTextLen(nPara);
99 for (size_t nAttr = 0; nAttr < aTempCharAttribs.size(); ++nAttr)
100 {
101 if (nIndex2 < aTempCharAttribs[nAttr].nStart)
102 {
103 EECharAttrib aEEAttr(nIndex2, aTempCharAttribs[nAttr].nStart);
104 aCharAttribs.insert(aCharAttribs.begin() + nAttr, aEEAttr);
105 }
106 nIndex2 = aTempCharAttribs[nAttr].nEnd;
107 aCharAttribs.push_back(aTempCharAttribs[nAttr]);
108 }
109 if ( nIndex2 != nParaLen )
110 {
111 EECharAttrib aEEAttr(nIndex2, nParaLen);
112 aCharAttribs.push_back(aEEAttr);
113 }
114 }
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)
119 {
120 nCurrIndex = charAttrib.nStart;
121
122 if( nCurrIndex > nClosestStartIndex_s &&
123 nCurrIndex <= nIndex)
124 {
125 nClosestStartIndex_s = nCurrIndex;
126 }
127 nCurrIndex = charAttrib.nEnd;
128 if ( nCurrIndex > nClosestStartIndex_e &&
129 nCurrIndex < nIndex )
130 {
131 nClosestStartIndex_e = nCurrIndex;
132 }
133 }
134 sal_Int32 nClosestStartIndex = std::max(nClosestStartIndex_s, nClosestStartIndex_e);
135
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)
140 {
141 nCurrIndex = charAttrib.nEnd;
142
143 if( nCurrIndex > nIndex &&
144 nCurrIndex < nClosestEndIndex_e )
145 {
146 nClosestEndIndex_e = nCurrIndex;
147 }
148 nCurrIndex = charAttrib.nStart;
149 if ( nCurrIndex > nIndex &&
150 nCurrIndex < nClosestEndIndex_s)
151 {
152 nClosestEndIndex_s = nCurrIndex;
153 }
154 }
155 sal_Int32 nClosestEndIndex = std::min(nClosestEndIndex_s, nClosestEndIndex_e);
156
157 nStartIndex = nClosestStartIndex;
158 nEndIndex = nClosestEndIndex;
159
160 if ( !bInCell )
161 return;
162
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 )
168 {
169 SfxItemSet aCrrntSet = rEE.GetAttribs( nPara, 0, 1, GetAttribsFlags::CHARATTRIBS );
170 for ( sal_Int32 nParaIdx = nPara-1; nParaIdx >= 0; nParaIdx-- )
171 {
172 sal_uInt32 nLen = rEE.GetTextLen(nParaIdx);
173 if ( nLen )
174 {
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 )
179 {
180 aStartPos.nPara = nParaIdx;
181 aStartPos.nIndex = nStartIdx;
182 if ( aStartPos.nIndex != 0 )
183 {
184 break;
185 }
186 }
187 }
188 }
189 }
190 //need find closest index behind nIndex in the following paragraphs
191 if ( aEndPos.nIndex == nCrrntParaLen )
192 {
193 SfxItemSet aCrrntSet = rEE.GetAttribs( nPara, nCrrntParaLen-1, nCrrntParaLen, GetAttribsFlags::CHARATTRIBS );
194 for ( sal_Int32 nParaIdx = nPara+1; nParaIdx < nParaCount; nParaIdx++ )
195 {
196 sal_Int32 nLen = rEE.GetTextLen( nParaIdx );
197 if ( nLen )
198 {
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 )
203 {
204 aEndPos.nPara = nParaIdx;
205 aEndPos.nIndex = nEndIdx;
206 if ( aEndPos.nIndex != nLen )
207 {
208 break;
209 }
210 }
211 }
212 }
213 }
214 nStartIndex = 0;
215 if ( aStartPos.nPara > 0 )
216 {
217 for ( sal_Int32 i = 0; i < aStartPos.nPara; i++ )
218 {
219 nStartIndex += rEE.GetTextLen(i)+1;
220 }
221 }
222 nStartIndex += aStartPos.nIndex;
223 nEndIndex = 0;
224 if ( aEndPos.nPara > 0 )
225 {
226 for ( sal_Int32 i = 0; i < aEndPos.nPara; i++ )
227 {
228 nEndIndex += rEE.GetTextLen(i)+1;
229 }
230 }
231 nEndIndex += aEndPos.nIndex;
232}
233
234Point SvxEditSourceHelper::EEToUserSpace( const Point& rPoint, const Size& rEESize, bool bIsVertical )
235{
236 return bIsVertical ? Point( -rPoint.Y() + rEESize.Height(), rPoint.X() ) : rPoint;
237}
238
239Point SvxEditSourceHelper::UserSpaceToEE( const Point& rPoint, const Size& rEESize, bool bIsVertical )
240{
241 return bIsVertical ? Point( rPoint.Y(), -rPoint.X() + rEESize.Height() ) : rPoint;
242}
243
244tools::Rectangle SvxEditSourceHelper::EEToUserSpace( const tools::Rectangle& rRect, const Size& rEESize, bool bIsVertical )
245{
246 return bIsVertical ? tools::Rectangle( EEToUserSpace(rRect.BottomLeft(), rEESize, bIsVertical),
247 EEToUserSpace(rRect.TopRight(), rEESize, bIsVertical) ) : rRect;
248}
249
250/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
size_t mnEnd
sal_Int32 GetParagraphCount() const
Definition: editeng.cxx:589
SfxItemSet GetAttribs(sal_Int32 nPara, sal_Int32 nStart, sal_Int32 nEnd, GetAttribsFlags nFlags=GetAttribsFlags::ALL) const
Definition: editeng.cxx:1791
sal_Int32 GetTextLen() const
Definition: editeng.cxx:584
void GetCharAttribs(sal_Int32 nPara, std::vector< EECharAttrib > &rLst) const
Definition: editeng.cxx:1779
constexpr tools::Long Y() const
constexpr tools::Long X() const
constexpr tools::Long Height() const
static void GetAttributeRun(sal_Int32 &nStartIndex, sal_Int32 &nEndIndex, const EditEngine &rEE, sal_Int32 nPara, sal_Int32 nIndex, bool bInCell=false)
Calculate attribute run for EditEngines.
Definition: unoedhlp.cxx:85
static Point EEToUserSpace(const Point &rPoint, const Size &rEESize, bool bIsVertical)
Convert point from edit engine to user coordinate space.
Definition: unoedhlp.cxx:234
static Point UserSpaceToEE(const Point &rPoint, const Size &rEESize, bool bIsVertical)
Convert point from user to edit engine coordinate space.
Definition: unoedhlp.cxx:239
static ::std::unique_ptr< SfxHint > EENotification2Hint(EENotify const *aNotify)
Translates EditEngine notifications into broadcastable hints.
Definition: unoedhlp.cxx:44
Extends TextHint by two additional parameters which are necessary for the SfxHintId::EditSourceParasM...
Definition: unoedhlp.hxx:38
SvxEditSourceHint(SfxHintId nId)
Definition: unoedhlp.cxx:29
constexpr Point TopRight() const
constexpr Point BottomLeft() const
@ EE_NOTIFY_PARAGRAPHSMOVED
Multiple paragraphs have been removed from the EditEngine.
Definition: editdata.hxx:315
@ EE_NOTIFY_TEXTVIEWSCROLLED
The view area of the EditEngine scrolled.
Definition: editdata.hxx:321
@ EE_NOTIFY_PARAGRAPHREMOVED
A paragraph was removed from the EditEngine.
Definition: editdata.hxx:312
@ EE_NOTIFY_TEXTVIEWSELECTIONCHANGED
The selection and/or the cursor position has changed.
Definition: editdata.hxx:324
@ EE_NOTIFY_PARAGRAPHINSERTED
A paragraph was inserted into the EditEngine.
Definition: editdata.hxx:309
@ EE_NOTIFY_PROCESSNOTIFICATIONS
The EditEngine is in a valid state again. Process pending notifications.
Definition: editdata.hxx:327
@ EE_NOTIFY_TEXTVIEWSELECTIONCHANGED_ENDD_PARA
Definition: editdata.hxx:329
@ EE_NOTIFY_TextHeightChanged
The height of at least one paragraph has changed.
Definition: editdata.hxx:318
@ EE_NOTIFY_TEXTMODIFIED
EditEngine text was modified.
Definition: editdata.hxx:306
sal_Int16 nValue
SfxHintId
sal_Int32 nIndex
int i
sal_Int32 nParam1
Definition: editdata.hxx:338
EENotifyType eNotificationType
Definition: editdata.hxx:334
sal_Int32 nParam2
Definition: editdata.hxx:339
sal_Int32 nParagraph
Definition: editdata.hxx:336
sal_Int32 nIndex
Definition: editdata.hxx:91
sal_Int32 nPara
Definition: editdata.hxx:90
sal_Int32 mnStart