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/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>
32 #include <IDocumentUndoRedo.hxx>
33 #include <contentindex.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;
47 SwExtTextInput::~SwExtTextInput()
49 SwDoc
& rDoc
= GetDoc();
50 if (rDoc
.IsInDtor()) { return; /* #i58606# */ }
52 SwTextNode
* pTNd
= GetPoint()->GetNode().GetTextNode();
56 SwPosition
& rPtPos
= *GetPoint();
57 sal_Int32 nSttCnt
= rPtPos
.GetContentIndex();
58 sal_Int32 nEndCnt
= GetMark()->GetContentIndex();
59 if( nEndCnt
== nSttCnt
)
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();
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();
85 rPtPos
.AdjustContent(+nOWLen
);
86 pTNd
->EraseText( rPtPos
, nLen
- nOWLen
);
87 rPtPos
.SetContent(nSttCnt
);
88 pTNd
->ReplaceText( rPtPos
, nOWLen
, m_sOverwriteText
);
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 );
100 pTNd
->ReplaceText( rPtPos
, nLen
, m_sOverwriteText
.copy( 0, nLen
));
103 rPtPos
.SetContent(nSttCnt
);
104 rDoc
.getIDocumentContentOperations().Overwrite( *this, sText
);
110 // 1. Insert text at start position with EMPTYEXPAND to use correct formatting
112 // 2. Then remove old (not tracked) content
115 sal_Int32 nLenghtOfOldString
= nEndCnt
- nSttCnt
;
119 rPtPos
.SetContent(nSttCnt
);
120 rDoc
.getIDocumentContentOperations().InsertString( *this, sText
, SwInsertFlags::EMPTYEXPAND
);
123 pTNd
->EraseText( rPtPos
, nLenghtOfOldString
);
127 rDoc
.GetIDocumentUndoRedo().DoGroupUndo(bKeepGroupUndo
);
129 if (m_eInputLanguage
== LANGUAGE_DONTKNOW
)
132 sal_uInt16 nWhich
= RES_CHRATR_LANGUAGE
;
133 sal_Int16 nScriptType
= SvtLanguageOptions::GetI18NScriptTypeOfLanguage(m_eInputLanguage
);
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();
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
172 pTNd
->ReplaceText( aIdx
, nReplace
,
173 m_sOverwriteText
.copy( nNewLen
, nReplace
));
179 const sal_Int32 nOWLen
= m_sOverwriteText
.getLength();
180 if( nOWLen
< nReplace
)
183 pTNd
->EraseText( aIdx
, nReplace
-nOWLen
);
189 nReplace
= std::min(nOWLen
, nNewLen
);
193 pTNd
->ReplaceText( aIdx
, nReplace
, rNewStr
);
196 GetMark()->Assign(*aIdx
.GetContentNode(), aIdx
.GetIndex());
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
);
211 GetPoint()->SetContent(nSttCnt
);
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
)
227 const SwTextNode
*const pTNd
= GetPoint()->GetNode().GetTextNode();
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() )
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
);
244 nPos
= std::max(nInPos
, nBrkPos
);
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
;
263 void SwDoc::DeleteExtTextInput( SwExtTextInput
* pDel
)
265 if( pDel
== mpExtInputRing
)
267 if( pDel
->GetNext() != mpExtInputRing
)
268 mpExtInputRing
= pDel
->GetNext();
270 mpExtInputRing
= nullptr;
275 SwExtTextInput
* SwDoc::GetExtTextInput( const SwNode
& rNd
,
276 sal_Int32 nContentPos
) const
278 SwExtTextInput
* pRet
= nullptr;
281 SwNodeOffset nNdIdx
= rNd
.GetIndex();
282 SwExtTextInput
* pTmp
= mpExtInputRing
;
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
&&
291 ( nStartCnt
<= nContentPos
&& nContentPos
<= nEndCnt
)))
296 pTmp
= pTmp
->GetNext();
297 } while ( pTmp
!=mpExtInputRing
);
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: */