sc: factor out some more code
[LibreOffice.git] / sw / source / core / doc / extinput.cxx
blob7ea91125b876fe6a5998dd74c32dad7ebf19ecdd
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 <algorithm>
22 #include <com/sun/star/i18n/ScriptType.hpp>
24 #include <editeng/langitem.hxx>
25 #include <osl/diagnose.h>
26 #include <svl/languageoptions.hxx>
27 #include <vcl/commandevent.hxx>
29 #include <hintids.hxx>
30 #include <extinput.hxx>
31 #include <doc.hxx>
32 #include <IDocumentUndoRedo.hxx>
33 #include <contentindex.hxx>
34 #include <ndtxt.hxx>
35 #include <swundo.hxx>
37 using namespace ::com::sun::star;
39 SwExtTextInput::SwExtTextInput( const SwPaM& rPam, Ring* pRing )
40 : SwPaM( *rPam.GetPoint(), static_cast<SwPaM*>(pRing) ),
41 m_eInputLanguage(LANGUAGE_DONTKNOW)
43 m_bIsOverwriteCursor = false;
44 m_bInsText = true;
47 SwExtTextInput::~SwExtTextInput()
49 SwDoc& rDoc = GetDoc();
50 if (rDoc.IsInDtor()) { return; /* #i58606# */ }
52 SwTextNode* pTNd = GetPoint()->GetNode().GetTextNode();
53 if( !pTNd )
54 return;
56 SwPosition& rPtPos = *GetPoint();
57 sal_Int32 nSttCnt = rPtPos.GetContentIndex();
58 sal_Int32 nEndCnt = GetMark()->GetContentIndex();
59 if( nEndCnt == nSttCnt )
60 return;
62 // Prevent IME edited text being grouped with non-IME edited text.
63 bool bKeepGroupUndo = rDoc.GetIDocumentUndoRedo().DoesGroupUndo();
64 bool bWasIME = rDoc.GetIDocumentUndoRedo().GetUndoActionCount() == 0 || rDoc.getIDocumentContentOperations().GetIME();
65 if (!bWasIME)
67 rDoc.GetIDocumentUndoRedo().DoGroupUndo(false);
69 rDoc.getIDocumentContentOperations().SetIME(true);
70 if( nEndCnt < nSttCnt )
72 std::swap(nSttCnt, nEndCnt);
75 // In order to get Undo/Redlining etc. working correctly,
76 // we need to go through the Doc interface
77 rPtPos.SetContent(nSttCnt);
78 const OUString sText( pTNd->GetText().copy(nSttCnt, nEndCnt - nSttCnt));
79 if( m_bIsOverwriteCursor && !m_sOverwriteText.isEmpty() )
81 const sal_Int32 nLen = sText.getLength();
82 const sal_Int32 nOWLen = m_sOverwriteText.getLength();
83 if( nLen > nOWLen )
85 rPtPos.AdjustContent(+nOWLen);
86 pTNd->EraseText( rPtPos, nLen - nOWLen );
87 rPtPos.SetContent(nSttCnt);
88 pTNd->ReplaceText( rPtPos, nOWLen, m_sOverwriteText );
89 if( m_bInsText )
91 rPtPos.SetContent(nSttCnt);
92 rDoc.GetIDocumentUndoRedo().StartUndo( SwUndoId::OVERWRITE, nullptr );
93 rDoc.getIDocumentContentOperations().Overwrite( *this, sText.copy( 0, nOWLen ) );
94 rDoc.getIDocumentContentOperations().InsertString( *this, sText.copy( nOWLen ) );
95 rDoc.GetIDocumentUndoRedo().EndUndo( SwUndoId::OVERWRITE, nullptr );
98 else
100 pTNd->ReplaceText( rPtPos, nLen, m_sOverwriteText.copy( 0, nLen ));
101 if( m_bInsText )
103 rPtPos.SetContent(nSttCnt);
104 rDoc.getIDocumentContentOperations().Overwrite( *this, sText );
108 else
110 pTNd->EraseText( rPtPos, nEndCnt - nSttCnt );
112 if( m_bInsText )
114 rDoc.getIDocumentContentOperations().InsertString(*this, sText);
117 if (!bWasIME)
119 rDoc.GetIDocumentUndoRedo().DoGroupUndo(bKeepGroupUndo);
121 if (m_eInputLanguage == LANGUAGE_DONTKNOW)
122 return;
124 sal_uInt16 nWhich = RES_CHRATR_LANGUAGE;
125 sal_Int16 nScriptType = SvtLanguageOptions::GetI18NScriptTypeOfLanguage(m_eInputLanguage);
126 switch(nScriptType)
128 case i18n::ScriptType::ASIAN:
129 nWhich = RES_CHRATR_CJK_LANGUAGE; break;
130 case i18n::ScriptType::COMPLEX:
131 nWhich = RES_CHRATR_CTL_LANGUAGE; break;
133 // #i41974# Only set language attribute for CJK/CTL scripts.
134 if (RES_CHRATR_LANGUAGE != nWhich && pTNd->GetLang( nSttCnt, nEndCnt-nSttCnt, nScriptType) != m_eInputLanguage)
136 SvxLanguageItem aLangItem( m_eInputLanguage, nWhich );
137 rPtPos.SetContent(nSttCnt);
138 GetMark()->SetContent(nEndCnt);
139 rDoc.getIDocumentContentOperations().InsertPoolItem(*this, aLangItem );
143 void SwExtTextInput::SetInputData( const CommandExtTextInputData& rData )
145 SwTextNode* pTNd = GetPoint()->GetNode().GetTextNode();
146 if( !pTNd )
147 return;
149 sal_Int32 nSttCnt = Start()->GetContentIndex();
150 sal_Int32 nEndCnt = End()->GetContentIndex();
152 SwContentIndex aIdx( pTNd, nSttCnt );
153 const OUString& rNewStr = rData.GetText();
155 if( m_bIsOverwriteCursor && !m_sOverwriteText.isEmpty() )
157 sal_Int32 nReplace = nEndCnt - nSttCnt;
158 const sal_Int32 nNewLen = rNewStr.getLength();
159 if( nNewLen < nReplace )
161 // We have to insert some characters from the saved original text
162 nReplace -= nNewLen;
163 aIdx += nNewLen;
164 pTNd->ReplaceText( aIdx, nReplace,
165 m_sOverwriteText.copy( nNewLen, nReplace ));
166 aIdx = nSttCnt;
167 nReplace = nNewLen;
169 else
171 const sal_Int32 nOWLen = m_sOverwriteText.getLength();
172 if( nOWLen < nReplace )
174 aIdx += nOWLen;
175 pTNd->EraseText( aIdx, nReplace-nOWLen );
176 aIdx = nSttCnt;
177 nReplace = nOWLen;
179 else
181 nReplace = std::min(nOWLen, nNewLen);
185 pTNd->ReplaceText( aIdx, nReplace, rNewStr );
186 if( !HasMark() )
187 SetMark();
188 GetMark()->Assign(*aIdx.GetContentNode(), aIdx.GetIndex());
190 else
192 if( nSttCnt < nEndCnt )
194 pTNd->EraseText( aIdx, nEndCnt - nSttCnt );
197 pTNd->InsertText(rNewStr, aIdx);
198 if( !HasMark() )
199 SetMark();
202 GetPoint()->SetContent(nSttCnt);
204 m_aAttrs.clear();
205 if( rData.GetTextAttr() )
207 const ExtTextInputAttr *pAttrs = rData.GetTextAttr();
208 m_aAttrs.insert( m_aAttrs.begin(), pAttrs, pAttrs + rData.GetText().getLength() );
212 void SwExtTextInput::SetOverwriteCursor( bool bFlag )
214 m_bIsOverwriteCursor = bFlag;
215 if (!m_bIsOverwriteCursor)
216 return;
218 const SwTextNode *const pTNd = GetPoint()->GetNode().GetTextNode();
219 if (!pTNd)
220 return;
222 const sal_Int32 nSttCnt = GetPoint()->GetContentIndex();
223 const sal_Int32 nEndCnt = GetMark()->GetContentIndex();
224 m_sOverwriteText = pTNd->GetText().copy( std::min(nSttCnt, nEndCnt) );
225 if( m_sOverwriteText.isEmpty() )
226 return;
228 const sal_Int32 nInPos = m_sOverwriteText.indexOf( CH_TXTATR_INWORD );
229 const sal_Int32 nBrkPos = m_sOverwriteText.indexOf( CH_TXTATR_BREAKWORD );
231 // Find the first attr found, if any.
232 sal_Int32 nPos = std::min(nInPos, nBrkPos);
233 if (nPos<0)
235 nPos = std::max(nInPos, nBrkPos);
237 if (nPos>=0)
239 m_sOverwriteText = m_sOverwriteText.copy( 0, nPos );
243 // The Doc interfaces
245 SwExtTextInput* SwDoc::CreateExtTextInput( const SwPaM& rPam )
247 SwExtTextInput* pNew = new SwExtTextInput( rPam, mpExtInputRing );
248 if( !mpExtInputRing )
249 mpExtInputRing = pNew;
250 pNew->SetMark();
251 return pNew;
254 void SwDoc::DeleteExtTextInput( SwExtTextInput* pDel )
256 if( pDel == mpExtInputRing )
258 if( pDel->GetNext() != mpExtInputRing )
259 mpExtInputRing = pDel->GetNext();
260 else
261 mpExtInputRing = nullptr;
263 delete pDel;
266 SwExtTextInput* SwDoc::GetExtTextInput( const SwNode& rNd,
267 sal_Int32 nContentPos ) const
269 SwExtTextInput* pRet = nullptr;
270 if( mpExtInputRing )
272 SwNodeOffset nNdIdx = rNd.GetIndex();
273 SwExtTextInput* pTmp = mpExtInputRing;
274 do {
275 SwNodeOffset nStartNode = pTmp->Start()->GetNodeIndex(),
276 nEndNode = pTmp->End()->GetNodeIndex();
277 sal_Int32 nStartCnt = pTmp->Start()->GetContentIndex();
278 sal_Int32 nEndCnt = pTmp->End()->GetContentIndex();
280 if( nStartNode <= nNdIdx && nNdIdx <= nEndNode &&
281 ( nContentPos<0 ||
282 ( nStartCnt <= nContentPos && nContentPos <= nEndCnt )))
284 pRet = pTmp;
285 break;
287 pTmp = pTmp->GetNext();
288 } while ( pTmp!=mpExtInputRing );
290 return pRet;
293 SwExtTextInput* SwDoc::GetExtTextInput() const
295 OSL_ENSURE( !mpExtInputRing || !mpExtInputRing->IsMultiSelection(),
296 "more than one InputEngine available" );
297 return mpExtInputRing;
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */