Line data Source code
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 <algorithm>
21 :
22 : #include <com/sun/star/i18n/ScriptType.hpp>
23 :
24 : #include <editeng/langitem.hxx>
25 : #include <editeng/scripttypeitem.hxx>
26 :
27 : #include <vcl/keycodes.hxx>
28 : #include <vcl/cmdevt.hxx>
29 :
30 : #include <hintids.hxx>
31 : #include <extinput.hxx>
32 : #include <doc.hxx>
33 : #include <IDocumentUndoRedo.hxx>
34 : #include <index.hxx>
35 : #include <ndtxt.hxx>
36 : #include <txtfrm.hxx>
37 : #include <swundo.hxx>
38 :
39 : using namespace ::com::sun::star;
40 :
41 0 : SwExtTextInput::SwExtTextInput( const SwPaM& rPam, Ring* pRing )
42 0 : : SwPaM( *rPam.GetPoint(), (SwPaM*)pRing ),
43 0 : eInputLanguage(LANGUAGE_DONTKNOW)
44 : {
45 0 : bIsOverwriteCursor = false;
46 0 : bInsText = true;
47 0 : }
48 :
49 0 : SwExtTextInput::~SwExtTextInput()
50 : {
51 0 : SwDoc *const pDoc = GetDoc();
52 0 : if (pDoc->IsInDtor()) { return; /* #i58606# */ }
53 :
54 0 : SwTxtNode* pTNd = GetPoint()->nNode.GetNode().GetTxtNode();
55 0 : if( pTNd )
56 : {
57 0 : SwIndex& rIdx = GetPoint()->nContent;
58 0 : sal_Int32 nSttCnt = rIdx.GetIndex();
59 0 : sal_Int32 nEndCnt = GetMark()->nContent.GetIndex();
60 0 : if( nEndCnt != nSttCnt )
61 : {
62 0 : if( nEndCnt < nSttCnt )
63 : {
64 0 : std::swap(nSttCnt, nEndCnt);
65 : }
66 :
67 : // In order to get Undo/Redlining etc. working correctly,
68 : // we need to go through the Doc interface
69 0 : if(eInputLanguage != LANGUAGE_DONTKNOW)
70 : {
71 : // #i41974# Only set language attribute
72 : // for CJK/CTL scripts.
73 0 : bool bLang = true;
74 0 : sal_uInt16 nWhich = RES_CHRATR_LANGUAGE;
75 0 : switch(GetI18NScriptTypeOfLanguage(eInputLanguage))
76 : {
77 0 : case i18n::ScriptType::ASIAN: nWhich = RES_CHRATR_CJK_LANGUAGE; break;
78 0 : case i18n::ScriptType::COMPLEX: nWhich = RES_CHRATR_CTL_LANGUAGE; break;
79 0 : default: bLang = false;
80 : }
81 0 : if ( bLang )
82 : {
83 0 : SvxLanguageItem aLangItem( eInputLanguage, nWhich );
84 0 : pDoc->getIDocumentContentOperations().InsertPoolItem(*this, aLangItem, 0 );
85 : }
86 : }
87 0 : rIdx = nSttCnt;
88 0 : const OUString sTxt( pTNd->GetTxt().copy(nSttCnt, nEndCnt - nSttCnt));
89 0 : if( bIsOverwriteCursor && !sOverwriteText.isEmpty() )
90 : {
91 0 : const sal_Int32 nLen = sTxt.getLength();
92 0 : const sal_Int32 nOWLen = sOverwriteText.getLength();
93 0 : if( nLen > nOWLen )
94 : {
95 0 : rIdx += nOWLen;
96 0 : pTNd->EraseText( rIdx, nLen - nOWLen );
97 0 : rIdx = nSttCnt;
98 0 : pTNd->ReplaceText( rIdx, nOWLen, sOverwriteText );
99 0 : if( bInsText )
100 : {
101 0 : rIdx = nSttCnt;
102 0 : pDoc->GetIDocumentUndoRedo().StartUndo( UNDO_OVERWRITE, NULL );
103 0 : pDoc->getIDocumentContentOperations().Overwrite( *this, sTxt.copy( 0, nOWLen ) );
104 0 : pDoc->getIDocumentContentOperations().InsertString( *this, sTxt.copy( nOWLen ) );
105 0 : pDoc->GetIDocumentUndoRedo().EndUndo( UNDO_OVERWRITE, NULL );
106 : }
107 : }
108 : else
109 : {
110 0 : pTNd->ReplaceText( rIdx, nLen, sOverwriteText.copy( 0, nLen ));
111 0 : if( bInsText )
112 : {
113 0 : rIdx = nSttCnt;
114 0 : pDoc->getIDocumentContentOperations().Overwrite( *this, sTxt );
115 : }
116 : }
117 : }
118 : else
119 : {
120 0 : pTNd->EraseText( rIdx, nEndCnt - nSttCnt );
121 :
122 0 : if( bInsText )
123 : {
124 0 : pDoc->getIDocumentContentOperations().InsertString( *this, sTxt );
125 : }
126 0 : }
127 : }
128 : }
129 0 : }
130 :
131 0 : void SwExtTextInput::SetInputData( const CommandExtTextInputData& rData )
132 : {
133 0 : SwTxtNode* pTNd = GetPoint()->nNode.GetNode().GetTxtNode();
134 0 : if( pTNd )
135 : {
136 0 : sal_Int32 nSttCnt = GetPoint()->nContent.GetIndex();
137 0 : sal_Int32 nEndCnt = GetMark()->nContent.GetIndex();
138 0 : if( nEndCnt < nSttCnt )
139 : {
140 0 : std::swap(nSttCnt, nEndCnt);
141 : }
142 :
143 0 : SwIndex aIdx( pTNd, nSttCnt );
144 0 : const OUString rNewStr = rData.GetText();
145 :
146 0 : if( bIsOverwriteCursor && !sOverwriteText.isEmpty() )
147 : {
148 0 : sal_Int32 nReplace = nEndCnt - nSttCnt;
149 0 : const sal_Int32 nNewLen = rNewStr.getLength();
150 0 : if( nNewLen < nReplace )
151 : {
152 : // We have to insert some characters from the saved original text
153 0 : nReplace -= nNewLen;
154 0 : aIdx += nNewLen;
155 : pTNd->ReplaceText( aIdx, nReplace,
156 0 : sOverwriteText.copy( nNewLen, nReplace ));
157 0 : aIdx = nSttCnt;
158 0 : nReplace = nNewLen;
159 : }
160 : else
161 : {
162 0 : const sal_Int32 nOWLen = sOverwriteText.getLength();
163 0 : if( nOWLen < nReplace )
164 : {
165 0 : aIdx += nOWLen;
166 0 : pTNd->EraseText( aIdx, nReplace-nOWLen );
167 0 : aIdx = nSttCnt;
168 0 : nReplace = nOWLen;
169 : }
170 : else
171 : {
172 0 : nReplace = std::min(nOWLen, nNewLen);
173 : }
174 : }
175 :
176 0 : pTNd->ReplaceText( aIdx, nReplace, rNewStr );
177 0 : if( !HasMark() )
178 0 : SetMark();
179 0 : GetMark()->nContent = aIdx;
180 : }
181 : else
182 : {
183 0 : if( nSttCnt < nEndCnt )
184 : {
185 0 : pTNd->EraseText( aIdx, nEndCnt - nSttCnt );
186 : }
187 :
188 : pTNd->InsertText( rNewStr, aIdx,
189 0 : IDocumentContentOperations::INS_EMPTYEXPAND );
190 0 : if( !HasMark() )
191 0 : SetMark();
192 : }
193 :
194 0 : GetPoint()->nContent = nSttCnt;
195 :
196 0 : aAttrs.clear();
197 0 : if( rData.GetTextAttr() )
198 : {
199 0 : const sal_uInt16 *pAttrs = rData.GetTextAttr();
200 0 : aAttrs.insert( aAttrs.begin(), pAttrs, pAttrs + rData.GetText().getLength() );
201 0 : }
202 : }
203 0 : }
204 :
205 0 : void SwExtTextInput::SetOverwriteCursor( bool bFlag )
206 : {
207 0 : bIsOverwriteCursor = bFlag;
208 0 : if (!bIsOverwriteCursor)
209 0 : return;
210 :
211 0 : const SwTxtNode *const pTNd = GetPoint()->nNode.GetNode().GetTxtNode();
212 0 : if (pTNd)
213 : {
214 0 : const sal_Int32 nSttCnt = GetPoint()->nContent.GetIndex();
215 0 : const sal_Int32 nEndCnt = GetMark()->nContent.GetIndex();
216 0 : sOverwriteText = pTNd->GetTxt().copy( std::min(nSttCnt, nEndCnt) );
217 0 : if( !sOverwriteText.isEmpty() )
218 : {
219 0 : const sal_Int32 nInPos = sOverwriteText.indexOf( CH_TXTATR_INWORD );
220 0 : const sal_Int32 nBrkPos = sOverwriteText.indexOf( CH_TXTATR_BREAKWORD );
221 :
222 : // Find the first attr found, if any.
223 0 : sal_Int32 nPos = std::min(nInPos, nBrkPos);
224 0 : if (nPos<0)
225 : {
226 0 : nPos = std::max(nInPos, nBrkPos);
227 : }
228 0 : if (nPos>=0)
229 : {
230 0 : sOverwriteText = sOverwriteText.copy( 0, nPos );
231 : }
232 : }
233 : }
234 : }
235 :
236 : // The Doc interfaces
237 :
238 0 : SwExtTextInput* SwDoc::CreateExtTextInput( const SwPaM& rPam )
239 : {
240 0 : SwExtTextInput* pNew = new SwExtTextInput( rPam, mpExtInputRing );
241 0 : if( !mpExtInputRing )
242 0 : mpExtInputRing = pNew;
243 0 : pNew->SetMark();
244 0 : return pNew;
245 : }
246 :
247 0 : void SwDoc::DeleteExtTextInput( SwExtTextInput* pDel )
248 : {
249 0 : if( pDel == mpExtInputRing )
250 : {
251 0 : if( pDel->GetNext() != mpExtInputRing )
252 0 : mpExtInputRing = (SwPaM*)pDel->GetNext();
253 : else
254 0 : mpExtInputRing = 0;
255 : }
256 0 : delete pDel;
257 0 : }
258 :
259 318980 : SwExtTextInput* SwDoc::GetExtTextInput( const SwNode& rNd,
260 : sal_Int32 nCntntPos ) const
261 : {
262 318980 : SwExtTextInput* pRet = 0;
263 318980 : if( mpExtInputRing )
264 : {
265 0 : sal_uLong nNdIdx = rNd.GetIndex();
266 0 : SwExtTextInput* pTmp = (SwExtTextInput*)mpExtInputRing;
267 0 : do {
268 0 : sal_uLong nPt = pTmp->GetPoint()->nNode.GetIndex(),
269 0 : nMk = pTmp->GetMark()->nNode.GetIndex();
270 0 : sal_Int32 nPtCnt = pTmp->GetPoint()->nContent.GetIndex();
271 0 : sal_Int32 nMkCnt = pTmp->GetMark()->nContent.GetIndex();
272 :
273 0 : if( nPt < nMk || ( nPt == nMk && nPtCnt < nMkCnt ))
274 : {
275 0 : sal_uLong nTmp = nMk; nMk = nPt; nPt = nTmp;
276 0 : sal_Int32 nTmp2 = nMkCnt; nMkCnt = nPtCnt; nPtCnt = nTmp2;
277 : }
278 :
279 0 : if( nMk <= nNdIdx && nNdIdx <= nPt &&
280 0 : ( nCntntPos<0 ||
281 0 : ( nMkCnt <= nCntntPos && nCntntPos <= nPtCnt )))
282 : {
283 0 : pRet = pTmp;
284 0 : break;
285 : }
286 0 : pTmp = static_cast<SwExtTextInput*>(pTmp->GetNext());
287 0 : } while ( pTmp!=mpExtInputRing );
288 : }
289 318980 : return pRet;
290 : }
291 :
292 0 : SwExtTextInput* SwDoc::GetExtTextInput() const
293 : {
294 : OSL_ENSURE( !mpExtInputRing || mpExtInputRing == mpExtInputRing->GetNext(),
295 : "more than one InputEngine available" );
296 0 : return (SwExtTextInput*)mpExtInputRing;
297 270 : }
298 :
299 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|