1efeef26fSAndrew Rist /**************************************************************
2*329107e9Smseidel *
3efeef26fSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist * or more contributor license agreements. See the NOTICE file
5efeef26fSAndrew Rist * distributed with this work for additional information
6efeef26fSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7efeef26fSAndrew Rist * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist * with the License. You may obtain a copy of the License at
10*329107e9Smseidel *
11efeef26fSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12*329107e9Smseidel *
13efeef26fSAndrew Rist * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist * software distributed under the License is distributed on an
15efeef26fSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist * KIND, either express or implied. See the License for the
17efeef26fSAndrew Rist * specific language governing permissions and limitations
18efeef26fSAndrew Rist * under the License.
19*329107e9Smseidel *
20efeef26fSAndrew Rist *************************************************************/
21efeef26fSAndrew Rist
22efeef26fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sw.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include <com/sun/star/i18n/ScriptType.hpp>
28cdf0e10cSrcweir
29cdf0e10cSrcweir #include <editeng/langitem.hxx>
30cdf0e10cSrcweir #include <editeng/scripttypeitem.hxx>
31cdf0e10cSrcweir
32cdf0e10cSrcweir #include <vcl/keycodes.hxx>
33cdf0e10cSrcweir #include <vcl/cmdevt.hxx>
34cdf0e10cSrcweir
35cdf0e10cSrcweir #include <hintids.hxx>
36cdf0e10cSrcweir #include <extinput.hxx>
37cdf0e10cSrcweir #include <doc.hxx>
38cdf0e10cSrcweir #include <IDocumentUndoRedo.hxx>
39cdf0e10cSrcweir #include <index.hxx>
40cdf0e10cSrcweir #include <ndtxt.hxx>
41cdf0e10cSrcweir #include <txtfrm.hxx>
42cdf0e10cSrcweir #include <swundo.hxx>
43cdf0e10cSrcweir
44cdf0e10cSrcweir
45cdf0e10cSrcweir using namespace ::com::sun::star;
46cdf0e10cSrcweir
SwExtTextInput(const SwPaM & rPam,Ring * pRing)47cdf0e10cSrcweir SwExtTextInput::SwExtTextInput( const SwPaM& rPam, Ring* pRing )
48*329107e9Smseidel : SwPaM( *rPam.GetPoint(), (SwPaM*)pRing ),
49*329107e9Smseidel eInputLanguage(LANGUAGE_DONTKNOW)
50cdf0e10cSrcweir {
51cdf0e10cSrcweir bIsOverwriteCursor = sal_False;
52cdf0e10cSrcweir bInsText = sal_True;
53cdf0e10cSrcweir }
54cdf0e10cSrcweir
~SwExtTextInput()55cdf0e10cSrcweir SwExtTextInput::~SwExtTextInput()
56cdf0e10cSrcweir {
57*329107e9Smseidel SwDoc *const pDoc = GetDoc();
58*329107e9Smseidel if (pDoc->IsInDtor()) { return; /* #i58606# */ }
59cdf0e10cSrcweir
60cdf0e10cSrcweir SwTxtNode* pTNd = GetPoint()->nNode.GetNode().GetTxtNode();
61cdf0e10cSrcweir if( pTNd )
62cdf0e10cSrcweir {
63cdf0e10cSrcweir SwIndex& rIdx = GetPoint()->nContent;
64cdf0e10cSrcweir xub_StrLen nSttCnt = rIdx.GetIndex(),
65cdf0e10cSrcweir nEndCnt = GetMark()->nContent.GetIndex();
66cdf0e10cSrcweir if( nEndCnt != nSttCnt )
67cdf0e10cSrcweir {
68cdf0e10cSrcweir if( nEndCnt < nSttCnt )
69cdf0e10cSrcweir {
70cdf0e10cSrcweir xub_StrLen n = nEndCnt; nEndCnt = nSttCnt; nSttCnt = n;
71cdf0e10cSrcweir }
72cdf0e10cSrcweir
73cdf0e10cSrcweir // damit Undo / Redlining usw. richtig funktioniert,
74cdf0e10cSrcweir // muss ueber die Doc-Schnittstellen gegangen werden !!!
75*329107e9Smseidel if(eInputLanguage != LANGUAGE_DONTKNOW)
76*329107e9Smseidel {
77cdf0e10cSrcweir // --> FME 2005-02-11 #i41974# Only set language attribute
78cdf0e10cSrcweir // for CJK/CTL scripts.
79cdf0e10cSrcweir bool bLang = true;
80cdf0e10cSrcweir // <--
81cdf0e10cSrcweir sal_uInt16 nWhich = RES_CHRATR_LANGUAGE;
82cdf0e10cSrcweir switch(GetI18NScriptTypeOfLanguage(eInputLanguage))
83cdf0e10cSrcweir {
84cdf0e10cSrcweir case i18n::ScriptType::ASIAN: nWhich = RES_CHRATR_CJK_LANGUAGE; break;
85cdf0e10cSrcweir case i18n::ScriptType::COMPLEX: nWhich = RES_CHRATR_CTL_LANGUAGE; break;
86cdf0e10cSrcweir default: bLang = false;
87cdf0e10cSrcweir }
88cdf0e10cSrcweir if ( bLang )
89cdf0e10cSrcweir {
90cdf0e10cSrcweir SvxLanguageItem aLangItem( eInputLanguage, nWhich );
91cdf0e10cSrcweir pDoc->InsertPoolItem(*this, aLangItem, 0 );
92cdf0e10cSrcweir }
93cdf0e10cSrcweir }
94cdf0e10cSrcweir rIdx = nSttCnt;
95cdf0e10cSrcweir String sTxt( pTNd->GetTxt().Copy( nSttCnt, nEndCnt - nSttCnt ));
96cdf0e10cSrcweir if( bIsOverwriteCursor && sOverwriteText.Len() )
97cdf0e10cSrcweir {
98*329107e9Smseidel xub_StrLen nLen = sTxt.Len();
99cdf0e10cSrcweir if( nLen > sOverwriteText.Len() )
100cdf0e10cSrcweir {
101cdf0e10cSrcweir rIdx += sOverwriteText.Len();
102*329107e9Smseidel pTNd->EraseText( rIdx, nLen - sOverwriteText.Len() );
103cdf0e10cSrcweir rIdx = nSttCnt;
104*329107e9Smseidel pTNd->ReplaceText( rIdx, sOverwriteText.Len(),
105cdf0e10cSrcweir sOverwriteText );
106cdf0e10cSrcweir if( bInsText )
107cdf0e10cSrcweir {
108cdf0e10cSrcweir rIdx = nSttCnt;
109cdf0e10cSrcweir pDoc->GetIDocumentUndoRedo().StartUndo(
110cdf0e10cSrcweir UNDO_OVERWRITE, NULL );
111cdf0e10cSrcweir pDoc->Overwrite( *this, sTxt.Copy( 0,
112cdf0e10cSrcweir sOverwriteText.Len() ));
113cdf0e10cSrcweir pDoc->InsertString( *this,
114cdf0e10cSrcweir sTxt.Copy( sOverwriteText.Len() ) );
115cdf0e10cSrcweir pDoc->GetIDocumentUndoRedo().EndUndo(
116cdf0e10cSrcweir UNDO_OVERWRITE, NULL );
117cdf0e10cSrcweir }
118cdf0e10cSrcweir }
119cdf0e10cSrcweir else
120cdf0e10cSrcweir {
121cdf0e10cSrcweir pTNd->ReplaceText( rIdx, nLen,
122cdf0e10cSrcweir sOverwriteText.Copy( 0, nLen ));
123cdf0e10cSrcweir if( bInsText )
124cdf0e10cSrcweir {
125cdf0e10cSrcweir rIdx = nSttCnt;
126cdf0e10cSrcweir pDoc->Overwrite( *this, sTxt );
127cdf0e10cSrcweir }
128cdf0e10cSrcweir }
129cdf0e10cSrcweir }
130cdf0e10cSrcweir else
131*329107e9Smseidel {
132cdf0e10cSrcweir pTNd->EraseText( rIdx, nEndCnt - nSttCnt );
133cdf0e10cSrcweir
134cdf0e10cSrcweir if( bInsText )
135cdf0e10cSrcweir {
136cdf0e10cSrcweir pDoc->InsertString( *this, sTxt );
137cdf0e10cSrcweir }
138cdf0e10cSrcweir }
139cdf0e10cSrcweir }
140cdf0e10cSrcweir }
141cdf0e10cSrcweir }
142cdf0e10cSrcweir
SetInputData(const CommandExtTextInputData & rData)143cdf0e10cSrcweir void SwExtTextInput::SetInputData( const CommandExtTextInputData& rData )
144cdf0e10cSrcweir {
145cdf0e10cSrcweir SwTxtNode* pTNd = GetPoint()->nNode.GetNode().GetTxtNode();
146cdf0e10cSrcweir if( pTNd )
147cdf0e10cSrcweir {
148cdf0e10cSrcweir xub_StrLen nSttCnt = GetPoint()->nContent.GetIndex(),
149cdf0e10cSrcweir nEndCnt = GetMark()->nContent.GetIndex();
150cdf0e10cSrcweir if( nEndCnt < nSttCnt )
151cdf0e10cSrcweir {
152cdf0e10cSrcweir xub_StrLen n = nEndCnt; nEndCnt = nSttCnt; nSttCnt = n;
153cdf0e10cSrcweir }
154cdf0e10cSrcweir
155cdf0e10cSrcweir SwIndex aIdx( pTNd, nSttCnt );
156cdf0e10cSrcweir const String& rNewStr = rData.GetText();
157cdf0e10cSrcweir
158cdf0e10cSrcweir if( bIsOverwriteCursor && sOverwriteText.Len() )
159cdf0e10cSrcweir {
160cdf0e10cSrcweir xub_StrLen nReplace = nEndCnt - nSttCnt;
161cdf0e10cSrcweir if( rNewStr.Len() < nReplace )
162cdf0e10cSrcweir {
163cdf0e10cSrcweir // then we must insert from the saved original text
164cdf0e10cSrcweir // some characters
165cdf0e10cSrcweir nReplace = nReplace - rNewStr.Len();
166cdf0e10cSrcweir aIdx += rNewStr.Len();
167*329107e9Smseidel pTNd->ReplaceText( aIdx, nReplace,
168cdf0e10cSrcweir sOverwriteText.Copy( rNewStr.Len(), nReplace ));
169cdf0e10cSrcweir aIdx = nSttCnt;
170cdf0e10cSrcweir nReplace = rNewStr.Len();
171cdf0e10cSrcweir }
172cdf0e10cSrcweir else if( sOverwriteText.Len() < nReplace )
173cdf0e10cSrcweir {
174cdf0e10cSrcweir nReplace = nReplace - sOverwriteText.Len();
175cdf0e10cSrcweir aIdx += sOverwriteText.Len();
176*329107e9Smseidel pTNd->EraseText( aIdx, nReplace );
177cdf0e10cSrcweir aIdx = nSttCnt;
178cdf0e10cSrcweir nReplace = sOverwriteText.Len();
179cdf0e10cSrcweir }
180cdf0e10cSrcweir else if( (nReplace = sOverwriteText.Len()) > rNewStr.Len() )
181cdf0e10cSrcweir nReplace = rNewStr.Len();
182cdf0e10cSrcweir
183*329107e9Smseidel pTNd->ReplaceText( aIdx, nReplace, rNewStr );
184cdf0e10cSrcweir if( !HasMark() )
185cdf0e10cSrcweir SetMark();
186cdf0e10cSrcweir GetMark()->nContent = aIdx;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir else
189cdf0e10cSrcweir {
190cdf0e10cSrcweir if( nSttCnt < nEndCnt )
191*329107e9Smseidel {
192*329107e9Smseidel pTNd->EraseText( aIdx, nEndCnt - nSttCnt );
193*329107e9Smseidel }
194cdf0e10cSrcweir
195*329107e9Smseidel pTNd->InsertText( rNewStr, aIdx,
196*329107e9Smseidel IDocumentContentOperations::INS_EMPTYEXPAND );
197cdf0e10cSrcweir if( !HasMark() )
198cdf0e10cSrcweir SetMark();
199cdf0e10cSrcweir }
200cdf0e10cSrcweir
201cdf0e10cSrcweir GetPoint()->nContent = nSttCnt;
202cdf0e10cSrcweir
203cdf0e10cSrcweir if( aAttrs.Count() )
204cdf0e10cSrcweir aAttrs.Remove( 0, aAttrs.Count() );
205cdf0e10cSrcweir if( rData.GetTextAttr() )
206cdf0e10cSrcweir aAttrs.Insert( rData.GetTextAttr(), rData.GetText().Len(), 0 );
207cdf0e10cSrcweir }
208cdf0e10cSrcweir }
209cdf0e10cSrcweir
SetOverwriteCursor(sal_Bool bFlag)210cdf0e10cSrcweir void SwExtTextInput::SetOverwriteCursor( sal_Bool bFlag )
211cdf0e10cSrcweir {
212cdf0e10cSrcweir bIsOverwriteCursor = bFlag;
213cdf0e10cSrcweir
214cdf0e10cSrcweir SwTxtNode* pTNd;
215cdf0e10cSrcweir if( bIsOverwriteCursor &&
216cdf0e10cSrcweir 0 != (pTNd = GetPoint()->nNode.GetNode().GetTxtNode()) )
217cdf0e10cSrcweir {
218cdf0e10cSrcweir xub_StrLen nSttCnt = GetPoint()->nContent.GetIndex(),
219cdf0e10cSrcweir nEndCnt = GetMark()->nContent.GetIndex();
220cdf0e10cSrcweir sOverwriteText = pTNd->GetTxt().Copy( nEndCnt < nSttCnt ? nEndCnt
221cdf0e10cSrcweir : nSttCnt );
222cdf0e10cSrcweir if( sOverwriteText.Len() )
223cdf0e10cSrcweir {
224cdf0e10cSrcweir xub_StrLen nInWrdAttrPos = sOverwriteText.Search( CH_TXTATR_INWORD ),
225cdf0e10cSrcweir nWrdAttrPos = sOverwriteText.Search( CH_TXTATR_BREAKWORD );
226cdf0e10cSrcweir if( nWrdAttrPos < nInWrdAttrPos )
227cdf0e10cSrcweir nInWrdAttrPos = nWrdAttrPos;
228cdf0e10cSrcweir if( STRING_NOTFOUND != nInWrdAttrPos )
229cdf0e10cSrcweir sOverwriteText.Erase( nInWrdAttrPos );
230cdf0e10cSrcweir }
231cdf0e10cSrcweir }
232cdf0e10cSrcweir }
233cdf0e10cSrcweir
234cdf0e10cSrcweir // die Doc Schnittstellen:
235cdf0e10cSrcweir
CreateExtTextInput(const SwPaM & rPam)236cdf0e10cSrcweir SwExtTextInput* SwDoc::CreateExtTextInput( const SwPaM& rPam )
237cdf0e10cSrcweir {
238cdf0e10cSrcweir SwExtTextInput* pNew = new SwExtTextInput( rPam, pExtInputRing );
239cdf0e10cSrcweir if( !pExtInputRing )
240cdf0e10cSrcweir pExtInputRing = pNew;
241cdf0e10cSrcweir pNew->SetMark();
242cdf0e10cSrcweir return pNew;
243cdf0e10cSrcweir }
244cdf0e10cSrcweir
DeleteExtTextInput(SwExtTextInput * pDel)245cdf0e10cSrcweir void SwDoc::DeleteExtTextInput( SwExtTextInput* pDel )
246cdf0e10cSrcweir {
247cdf0e10cSrcweir if( pDel == pExtInputRing )
248cdf0e10cSrcweir {
249cdf0e10cSrcweir if( pDel->GetNext() != pExtInputRing )
250cdf0e10cSrcweir pExtInputRing = (SwPaM*)pDel->GetNext();
251cdf0e10cSrcweir else
252cdf0e10cSrcweir pExtInputRing = 0;
253cdf0e10cSrcweir }
254cdf0e10cSrcweir delete pDel;
255cdf0e10cSrcweir }
256cdf0e10cSrcweir
GetExtTextInput(const SwNode & rNd,xub_StrLen nCntntPos) const257cdf0e10cSrcweir SwExtTextInput* SwDoc::GetExtTextInput( const SwNode& rNd,
258cdf0e10cSrcweir xub_StrLen nCntntPos ) const
259cdf0e10cSrcweir {
260cdf0e10cSrcweir SwExtTextInput* pRet = 0;
261cdf0e10cSrcweir if( pExtInputRing )
262cdf0e10cSrcweir {
263cdf0e10cSrcweir sal_uLong nNdIdx = rNd.GetIndex();
264cdf0e10cSrcweir SwExtTextInput* pTmp = (SwExtTextInput*)pExtInputRing;
265cdf0e10cSrcweir do {
266cdf0e10cSrcweir sal_uLong nPt = pTmp->GetPoint()->nNode.GetIndex(),
267cdf0e10cSrcweir nMk = pTmp->GetMark()->nNode.GetIndex();
268cdf0e10cSrcweir xub_StrLen nPtCnt = pTmp->GetPoint()->nContent.GetIndex(),
269cdf0e10cSrcweir nMkCnt = pTmp->GetMark()->nContent.GetIndex();
270cdf0e10cSrcweir
271cdf0e10cSrcweir if( nPt < nMk || ( nPt == nMk && nPtCnt < nMkCnt ))
272cdf0e10cSrcweir {
273cdf0e10cSrcweir sal_uLong nTmp = nMk; nMk = nPt; nPt = nTmp;
274cdf0e10cSrcweir nTmp = nMkCnt; nMkCnt = nPtCnt; nPtCnt = (xub_StrLen)nTmp;
275cdf0e10cSrcweir }
276cdf0e10cSrcweir
277cdf0e10cSrcweir if( nMk <= nNdIdx && nNdIdx <= nPt &&
278cdf0e10cSrcweir ( STRING_NOTFOUND == nCntntPos ||
279cdf0e10cSrcweir ( nMkCnt <= nCntntPos && nCntntPos <= nPtCnt )))
280cdf0e10cSrcweir {
281cdf0e10cSrcweir pRet = pTmp;
282cdf0e10cSrcweir break;
283cdf0e10cSrcweir }
284cdf0e10cSrcweir } while( pExtInputRing != (pTmp = (SwExtTextInput*)pExtInputRing ) );
285cdf0e10cSrcweir }
286cdf0e10cSrcweir return pRet;
287cdf0e10cSrcweir }
288cdf0e10cSrcweir
GetExtTextInput() const289cdf0e10cSrcweir SwExtTextInput* SwDoc::GetExtTextInput() const
290cdf0e10cSrcweir {
291cdf0e10cSrcweir ASSERT( !pExtInputRing || pExtInputRing == pExtInputRing->GetNext(),
292*329107e9Smseidel "more than one InputEngine available" );
293cdf0e10cSrcweir return (SwExtTextInput*)pExtInputRing;
294cdf0e10cSrcweir }
295cdf0e10cSrcweir
296