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 :
21 : #include <com/sun/star/i18n/ScriptType.hpp>
22 :
23 : #include <editeng/langitem.hxx>
24 : #include <editeng/scripttypeitem.hxx>
25 :
26 : #include <vcl/keycodes.hxx>
27 : #include <vcl/cmdevt.hxx>
28 :
29 : #include <hintids.hxx>
30 : #include <extinput.hxx>
31 : #include <doc.hxx>
32 : #include <IDocumentUndoRedo.hxx>
33 : #include <index.hxx>
34 : #include <ndtxt.hxx>
35 : #include <txtfrm.hxx>
36 : #include <swundo.hxx>
37 :
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 = sal_False;
46 0 : bInsText = sal_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 : xub_StrLen nSttCnt = rIdx.GetIndex(),
59 0 : nEndCnt = GetMark()->nContent.GetIndex();
60 0 : if( nEndCnt != nSttCnt )
61 : {
62 0 : if( nEndCnt < nSttCnt )
63 : {
64 0 : xub_StrLen n = nEndCnt; nEndCnt = nSttCnt; nSttCnt = n;
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->InsertPoolItem(*this, aLangItem, 0 );
85 : }
86 : }
87 0 : rIdx = nSttCnt;
88 0 : String sTxt( pTNd->GetTxt().Copy( nSttCnt, nEndCnt - nSttCnt ));
89 0 : if( bIsOverwriteCursor && sOverwriteText.Len() )
90 : {
91 0 : xub_StrLen nLen = sTxt.Len();
92 0 : if( nLen > sOverwriteText.Len() )
93 : {
94 0 : rIdx += sOverwriteText.Len();
95 0 : pTNd->EraseText( rIdx, nLen - sOverwriteText.Len() );
96 0 : rIdx = nSttCnt;
97 0 : pTNd->ReplaceText( rIdx, sOverwriteText.Len(),
98 0 : sOverwriteText );
99 0 : if( bInsText )
100 : {
101 0 : rIdx = nSttCnt;
102 0 : pDoc->GetIDocumentUndoRedo().StartUndo(
103 0 : UNDO_OVERWRITE, NULL );
104 : pDoc->Overwrite( *this, sTxt.Copy( 0,
105 0 : sOverwriteText.Len() ));
106 : pDoc->InsertString( *this,
107 0 : sTxt.Copy( sOverwriteText.Len() ) );
108 0 : pDoc->GetIDocumentUndoRedo().EndUndo(
109 0 : UNDO_OVERWRITE, NULL );
110 : }
111 : }
112 : else
113 : {
114 : pTNd->ReplaceText( rIdx, nLen,
115 0 : sOverwriteText.Copy( 0, nLen ));
116 0 : if( bInsText )
117 : {
118 0 : rIdx = nSttCnt;
119 0 : pDoc->Overwrite( *this, sTxt );
120 : }
121 : }
122 : }
123 : else
124 : {
125 0 : pTNd->EraseText( rIdx, nEndCnt - nSttCnt );
126 :
127 0 : if( bInsText )
128 : {
129 0 : pDoc->InsertString( *this, sTxt );
130 : }
131 0 : }
132 : }
133 : }
134 0 : }
135 :
136 0 : void SwExtTextInput::SetInputData( const CommandExtTextInputData& rData )
137 : {
138 0 : SwTxtNode* pTNd = GetPoint()->nNode.GetNode().GetTxtNode();
139 0 : if( pTNd )
140 : {
141 0 : xub_StrLen nSttCnt = GetPoint()->nContent.GetIndex(),
142 0 : nEndCnt = GetMark()->nContent.GetIndex();
143 0 : if( nEndCnt < nSttCnt )
144 : {
145 0 : xub_StrLen n = nEndCnt; nEndCnt = nSttCnt; nSttCnt = n;
146 : }
147 :
148 0 : SwIndex aIdx( pTNd, nSttCnt );
149 0 : const String& rNewStr = rData.GetText();
150 :
151 0 : if( bIsOverwriteCursor && sOverwriteText.Len() )
152 : {
153 0 : xub_StrLen nReplace = nEndCnt - nSttCnt;
154 0 : if( rNewStr.Len() < nReplace )
155 : {
156 : // We have to insert some characters from the saved original text
157 0 : nReplace = nReplace - rNewStr.Len();
158 0 : aIdx += rNewStr.Len();
159 : pTNd->ReplaceText( aIdx, nReplace,
160 0 : sOverwriteText.Copy( rNewStr.Len(), nReplace ));
161 0 : aIdx = nSttCnt;
162 0 : nReplace = rNewStr.Len();
163 : }
164 0 : else if( sOverwriteText.Len() < nReplace )
165 : {
166 0 : nReplace = nReplace - sOverwriteText.Len();
167 0 : aIdx += sOverwriteText.Len();
168 0 : pTNd->EraseText( aIdx, nReplace );
169 0 : aIdx = nSttCnt;
170 0 : nReplace = sOverwriteText.Len();
171 : }
172 0 : else if( (nReplace = sOverwriteText.Len()) > rNewStr.Len() )
173 0 : nReplace = rNewStr.Len();
174 :
175 0 : pTNd->ReplaceText( aIdx, nReplace, rNewStr );
176 0 : if( !HasMark() )
177 0 : SetMark();
178 0 : GetMark()->nContent = aIdx;
179 : }
180 : else
181 : {
182 0 : if( nSttCnt < nEndCnt )
183 : {
184 0 : pTNd->EraseText( aIdx, nEndCnt - nSttCnt );
185 : }
186 :
187 : pTNd->InsertText( rNewStr, aIdx,
188 0 : IDocumentContentOperations::INS_EMPTYEXPAND );
189 0 : if( !HasMark() )
190 0 : SetMark();
191 : }
192 :
193 0 : GetPoint()->nContent = nSttCnt;
194 :
195 0 : aAttrs.clear();
196 0 : if( rData.GetTextAttr() )
197 : {
198 0 : const sal_uInt16 *pAttrs = rData.GetTextAttr();
199 0 : aAttrs.insert( aAttrs.begin(), pAttrs, pAttrs + rData.GetText().Len() );
200 0 : }
201 : }
202 0 : }
203 :
204 0 : void SwExtTextInput::SetOverwriteCursor( sal_Bool bFlag )
205 : {
206 0 : bIsOverwriteCursor = bFlag;
207 :
208 : SwTxtNode* pTNd;
209 0 : if( bIsOverwriteCursor &&
210 0 : 0 != (pTNd = GetPoint()->nNode.GetNode().GetTxtNode()) )
211 : {
212 0 : xub_StrLen nSttCnt = GetPoint()->nContent.GetIndex(),
213 0 : nEndCnt = GetMark()->nContent.GetIndex();
214 0 : sOverwriteText = pTNd->GetTxt().Copy( nEndCnt < nSttCnt ? nEndCnt
215 0 : : nSttCnt );
216 0 : if( sOverwriteText.Len() )
217 : {
218 0 : xub_StrLen nInWrdAttrPos = sOverwriteText.Search( CH_TXTATR_INWORD ),
219 0 : nWrdAttrPos = sOverwriteText.Search( CH_TXTATR_BREAKWORD );
220 0 : if( nWrdAttrPos < nInWrdAttrPos )
221 0 : nInWrdAttrPos = nWrdAttrPos;
222 0 : if( STRING_NOTFOUND != nInWrdAttrPos )
223 0 : sOverwriteText.Erase( nInWrdAttrPos );
224 : }
225 : }
226 0 : }
227 :
228 : // The Doc interfaces
229 :
230 0 : SwExtTextInput* SwDoc::CreateExtTextInput( const SwPaM& rPam )
231 : {
232 0 : SwExtTextInput* pNew = new SwExtTextInput( rPam, pExtInputRing );
233 0 : if( !pExtInputRing )
234 0 : pExtInputRing = pNew;
235 0 : pNew->SetMark();
236 0 : return pNew;
237 : }
238 :
239 0 : void SwDoc::DeleteExtTextInput( SwExtTextInput* pDel )
240 : {
241 0 : if( pDel == pExtInputRing )
242 : {
243 0 : if( pDel->GetNext() != pExtInputRing )
244 0 : pExtInputRing = (SwPaM*)pDel->GetNext();
245 : else
246 0 : pExtInputRing = 0;
247 : }
248 0 : delete pDel;
249 0 : }
250 :
251 4159 : SwExtTextInput* SwDoc::GetExtTextInput( const SwNode& rNd,
252 : xub_StrLen nCntntPos ) const
253 : {
254 4159 : SwExtTextInput* pRet = 0;
255 4159 : if( pExtInputRing )
256 : {
257 0 : sal_uLong nNdIdx = rNd.GetIndex();
258 0 : SwExtTextInput* pTmp = (SwExtTextInput*)pExtInputRing;
259 0 : do {
260 0 : sal_uLong nPt = pTmp->GetPoint()->nNode.GetIndex(),
261 0 : nMk = pTmp->GetMark()->nNode.GetIndex();
262 0 : xub_StrLen nPtCnt = pTmp->GetPoint()->nContent.GetIndex(),
263 0 : nMkCnt = pTmp->GetMark()->nContent.GetIndex();
264 :
265 0 : if( nPt < nMk || ( nPt == nMk && nPtCnt < nMkCnt ))
266 : {
267 0 : sal_uLong nTmp = nMk; nMk = nPt; nPt = nTmp;
268 0 : nTmp = nMkCnt; nMkCnt = nPtCnt; nPtCnt = (xub_StrLen)nTmp;
269 : }
270 :
271 0 : if( nMk <= nNdIdx && nNdIdx <= nPt &&
272 : ( STRING_NOTFOUND == nCntntPos ||
273 : ( nMkCnt <= nCntntPos && nCntntPos <= nPtCnt )))
274 : {
275 0 : pRet = pTmp;
276 0 : break;
277 : }
278 : } while( pExtInputRing != (pTmp = (SwExtTextInput*)pExtInputRing ) );
279 : }
280 4159 : return pRet;
281 : }
282 :
283 0 : SwExtTextInput* SwDoc::GetExtTextInput() const
284 : {
285 : OSL_ENSURE( !pExtInputRing || pExtInputRing == pExtInputRing->GetNext(),
286 : "more then one InputEngine available" );
287 0 : return (SwExtTextInput*)pExtInputRing;
288 : }
289 :
290 :
291 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|