android: Update app-specific/MIME type icons
[LibreOffice.git] / sw / source / core / doc / extinput.cxx
blobc8563facbd85eaa6eec4aafeffa60f5ac4b10bc6
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 // 1. Insert text at start position with EMPTYEXPAND to use correct formatting
111 // ABC<NEW><OLD>
112 // 2. Then remove old (not tracked) content
113 // ABC<NEW>
115 sal_Int32 nLenghtOfOldString = nEndCnt - nSttCnt;
117 if( m_bInsText )
119 rPtPos.SetContent(nSttCnt);
120 rDoc.getIDocumentContentOperations().InsertString( *this, sText, SwInsertFlags::EMPTYEXPAND );
123 pTNd->EraseText( rPtPos, nLenghtOfOldString );
125 if (!bWasIME)
127 rDoc.GetIDocumentUndoRedo().DoGroupUndo(bKeepGroupUndo);
129 if (m_eInputLanguage == LANGUAGE_DONTKNOW)
130 return;
132 sal_uInt16 nWhich = RES_CHRATR_LANGUAGE;
133 sal_Int16 nScriptType = SvtLanguageOptions::GetI18NScriptTypeOfLanguage(m_eInputLanguage);
134 switch(nScriptType)
136 case i18n::ScriptType::ASIAN:
137 nWhich = RES_CHRATR_CJK_LANGUAGE; break;
138 case i18n::ScriptType::COMPLEX:
139 nWhich = RES_CHRATR_CTL_LANGUAGE; break;
141 // #i41974# Only set language attribute for CJK/CTL scripts.
142 if (RES_CHRATR_LANGUAGE != nWhich && pTNd->GetLang( nSttCnt, nEndCnt-nSttCnt, nScriptType) != m_eInputLanguage)
144 SvxLanguageItem aLangItem( m_eInputLanguage, nWhich );
145 rPtPos.SetContent(nSttCnt);
146 GetMark()->SetContent(nEndCnt);
147 rDoc.getIDocumentContentOperations().InsertPoolItem(*this, aLangItem );
151 void SwExtTextInput::SetInputData( const CommandExtTextInputData& rData )
153 SwTextNode* pTNd = GetPoint()->GetNode().GetTextNode();
154 if( !pTNd )
155 return;
157 sal_Int32 nSttCnt = Start()->GetContentIndex();
158 sal_Int32 nEndCnt = End()->GetContentIndex();
160 SwContentIndex aIdx( pTNd, nSttCnt );
161 const OUString& rNewStr = rData.GetText();
163 if( m_bIsOverwriteCursor && !m_sOverwriteText.isEmpty() )
165 sal_Int32 nReplace = nEndCnt - nSttCnt;
166 const sal_Int32 nNewLen = rNewStr.getLength();
167 if( nNewLen < nReplace )
169 // We have to insert some characters from the saved original text
170 nReplace -= nNewLen;
171 aIdx += nNewLen;
172 pTNd->ReplaceText( aIdx, nReplace,
173 m_sOverwriteText.copy( nNewLen, nReplace ));
174 aIdx = nSttCnt;
175 nReplace = nNewLen;
177 else
179 const sal_Int32 nOWLen = m_sOverwriteText.getLength();
180 if( nOWLen < nReplace )
182 aIdx += nOWLen;
183 pTNd->EraseText( aIdx, nReplace-nOWLen );
184 aIdx = nSttCnt;
185 nReplace = nOWLen;
187 else
189 nReplace = std::min(nOWLen, nNewLen);
193 pTNd->ReplaceText( aIdx, nReplace, rNewStr );
194 if( !HasMark() )
195 SetMark();
196 GetMark()->Assign(*aIdx.GetContentNode(), aIdx.GetIndex());
198 else
200 if( nSttCnt < nEndCnt )
202 pTNd->EraseText( aIdx, nEndCnt - nSttCnt );
205 // NOHINTEXPAND so we can use correct formatting in destructor when we finish composing
206 pTNd->InsertText( rNewStr, aIdx, SwInsertFlags::NOHINTEXPAND );
207 if( !HasMark() )
208 SetMark();
211 GetPoint()->SetContent(nSttCnt);
213 m_aAttrs.clear();
214 if( rData.GetTextAttr() )
216 const ExtTextInputAttr *pAttrs = rData.GetTextAttr();
217 m_aAttrs.insert( m_aAttrs.begin(), pAttrs, pAttrs + rData.GetText().getLength() );
221 void SwExtTextInput::SetOverwriteCursor( bool bFlag )
223 m_bIsOverwriteCursor = bFlag;
224 if (!m_bIsOverwriteCursor)
225 return;
227 const SwTextNode *const pTNd = GetPoint()->GetNode().GetTextNode();
228 if (!pTNd)
229 return;
231 const sal_Int32 nSttCnt = GetPoint()->GetContentIndex();
232 const sal_Int32 nEndCnt = GetMark()->GetContentIndex();
233 m_sOverwriteText = pTNd->GetText().copy( std::min(nSttCnt, nEndCnt) );
234 if( m_sOverwriteText.isEmpty() )
235 return;
237 const sal_Int32 nInPos = m_sOverwriteText.indexOf( CH_TXTATR_INWORD );
238 const sal_Int32 nBrkPos = m_sOverwriteText.indexOf( CH_TXTATR_BREAKWORD );
240 // Find the first attr found, if any.
241 sal_Int32 nPos = std::min(nInPos, nBrkPos);
242 if (nPos<0)
244 nPos = std::max(nInPos, nBrkPos);
246 if (nPos>=0)
248 m_sOverwriteText = m_sOverwriteText.copy( 0, nPos );
252 // The Doc interfaces
254 SwExtTextInput* SwDoc::CreateExtTextInput( const SwPaM& rPam )
256 SwExtTextInput* pNew = new SwExtTextInput( rPam, mpExtInputRing );
257 if( !mpExtInputRing )
258 mpExtInputRing = pNew;
259 pNew->SetMark();
260 return pNew;
263 void SwDoc::DeleteExtTextInput( SwExtTextInput* pDel )
265 if( pDel == mpExtInputRing )
267 if( pDel->GetNext() != mpExtInputRing )
268 mpExtInputRing = pDel->GetNext();
269 else
270 mpExtInputRing = nullptr;
272 delete pDel;
275 SwExtTextInput* SwDoc::GetExtTextInput( const SwNode& rNd,
276 sal_Int32 nContentPos ) const
278 SwExtTextInput* pRet = nullptr;
279 if( mpExtInputRing )
281 SwNodeOffset nNdIdx = rNd.GetIndex();
282 SwExtTextInput* pTmp = mpExtInputRing;
283 do {
284 SwNodeOffset nStartNode = pTmp->Start()->GetNodeIndex(),
285 nEndNode = pTmp->End()->GetNodeIndex();
286 sal_Int32 nStartCnt = pTmp->Start()->GetContentIndex();
287 sal_Int32 nEndCnt = pTmp->End()->GetContentIndex();
289 if( nStartNode <= nNdIdx && nNdIdx <= nEndNode &&
290 ( nContentPos<0 ||
291 ( nStartCnt <= nContentPos && nContentPos <= nEndCnt )))
293 pRet = pTmp;
294 break;
296 pTmp = pTmp->GetNext();
297 } while ( pTmp!=mpExtInputRing );
299 return pRet;
302 SwExtTextInput* SwDoc::GetExtTextInput() const
304 OSL_ENSURE( !mpExtInputRing || !mpExtInputRing->IsMultiSelection(),
305 "more than one InputEngine available" );
306 return mpExtInputRing;
309 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */