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 pTNd
->EraseText( rPtPos
, nEndCnt
- nSttCnt
);
114 rDoc
.getIDocumentContentOperations().InsertString(*this, sText
);
119 rDoc
.GetIDocumentUndoRedo().DoGroupUndo(bKeepGroupUndo
);
121 if (m_eInputLanguage
== LANGUAGE_DONTKNOW
)
124 sal_uInt16 nWhich
= RES_CHRATR_LANGUAGE
;
125 sal_Int16 nScriptType
= SvtLanguageOptions::GetI18NScriptTypeOfLanguage(m_eInputLanguage
);
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();
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
164 pTNd
->ReplaceText( aIdx
, nReplace
,
165 m_sOverwriteText
.copy( nNewLen
, nReplace
));
171 const sal_Int32 nOWLen
= m_sOverwriteText
.getLength();
172 if( nOWLen
< nReplace
)
175 pTNd
->EraseText( aIdx
, nReplace
-nOWLen
);
181 nReplace
= std::min(nOWLen
, nNewLen
);
185 pTNd
->ReplaceText( aIdx
, nReplace
, rNewStr
);
188 GetMark()->Assign(*aIdx
.GetContentNode(), aIdx
.GetIndex());
192 if( nSttCnt
< nEndCnt
)
194 pTNd
->EraseText( aIdx
, nEndCnt
- nSttCnt
);
197 pTNd
->InsertText(rNewStr
, aIdx
);
202 GetPoint()->SetContent(nSttCnt
);
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
)
218 const SwTextNode
*const pTNd
= GetPoint()->GetNode().GetTextNode();
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() )
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
);
235 nPos
= std::max(nInPos
, nBrkPos
);
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
;
254 void SwDoc::DeleteExtTextInput( SwExtTextInput
* pDel
)
256 if( pDel
== mpExtInputRing
)
258 if( pDel
->GetNext() != mpExtInputRing
)
259 mpExtInputRing
= pDel
->GetNext();
261 mpExtInputRing
= nullptr;
266 SwExtTextInput
* SwDoc::GetExtTextInput( const SwNode
& rNd
,
267 sal_Int32 nContentPos
) const
269 SwExtTextInput
* pRet
= nullptr;
272 SwNodeOffset nNdIdx
= rNd
.GetIndex();
273 SwExtTextInput
* pTmp
= mpExtInputRing
;
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
&&
282 ( nStartCnt
<= nContentPos
&& nContentPos
<= nEndCnt
)))
287 pTmp
= pTmp
->GetNext();
288 } while ( pTmp
!=mpExtInputRing
);
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: */