summaryrefslogtreecommitdiffstats
path: root/sw/source/core/doc/extinput.cxx
blob: d9d22c25f695894d9670e3ad3a36f771cbb379d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <algorithm>

#include <com/sun/star/i18n/ScriptType.hpp>

#include <editeng/langitem.hxx>
#include <osl/diagnose.h>
#include <svl/languageoptions.hxx>
#include <vcl/commandevent.hxx>

#include <hintids.hxx>
#include <extinput.hxx>
#include <doc.hxx>
#include <IDocumentUndoRedo.hxx>
#include <index.hxx>
#include <ndtxt.hxx>
#include <swundo.hxx>

using namespace ::com::sun::star;

SwExtTextInput::SwExtTextInput( const SwPaM& rPam, Ring* pRing )
    : SwPaM( *rPam.GetPoint(), static_cast<SwPaM*>(pRing) ),
    m_eInputLanguage(LANGUAGE_DONTKNOW)
{
    m_bIsOverwriteCursor = false;
    m_bInsText = true;
}

SwExtTextInput::~SwExtTextInput()
{
    SwDoc *const pDoc = GetDoc();
    if (pDoc->IsInDtor()) { return; /* #i58606# */ }

    SwTextNode* pTNd = GetPoint()->nNode.GetNode().GetTextNode();
    if( pTNd )
    {
        SwIndex& rIdx = GetPoint()->nContent;
        sal_Int32 nSttCnt = rIdx.GetIndex();
        sal_Int32 nEndCnt = GetMark()->nContent.GetIndex();
        if( nEndCnt != nSttCnt )
        {
            // Prevent IME edited text being grouped with non-IME edited text.
            bool bKeepGroupUndo = pDoc->GetIDocumentUndoRedo().DoesGroupUndo();
            pDoc->GetIDocumentUndoRedo().DoGroupUndo(false);
            if( nEndCnt < nSttCnt )
            {
                std::swap(nSttCnt, nEndCnt);
            }

            // In order to get Undo/Redlining etc. working correctly,
            // we need to go through the Doc interface
            rIdx = nSttCnt;
            const OUString sText( pTNd->GetText().copy(nSttCnt, nEndCnt - nSttCnt));
            if( m_bIsOverwriteCursor && !m_sOverwriteText.isEmpty() )
            {
                const sal_Int32 nLen = sText.getLength();
                const sal_Int32 nOWLen = m_sOverwriteText.getLength();
                if( nLen > nOWLen )
                {
                    rIdx += nOWLen;
                    pTNd->EraseText( rIdx, nLen - nOWLen );
                    rIdx = nSttCnt;
                    pTNd->ReplaceText( rIdx, nOWLen, m_sOverwriteText );
                    if( m_bInsText )
                    {
                        rIdx = nSttCnt;
                        pDoc->GetIDocumentUndoRedo().StartUndo( SwUndoId::OVERWRITE, nullptr );
                        pDoc->getIDocumentContentOperations().Overwrite( *this, sText.copy( 0, nOWLen ) );
                        pDoc->getIDocumentContentOperations().InsertString( *this, sText.copy( nOWLen ) );
                        pDoc->GetIDocumentUndoRedo().EndUndo( SwUndoId::OVERWRITE, nullptr );
                    }
                }
                else
                {
                    pTNd->ReplaceText( rIdx, nLen, m_sOverwriteText.copy( 0, nLen ));
                    if( m_bInsText )
                    {
                        rIdx = nSttCnt;
                        pDoc->getIDocumentContentOperations().Overwrite( *this, sText );
                    }
                }
            }
            else
            {
                pTNd->EraseText( rIdx, nEndCnt - nSttCnt );

                if( m_bInsText )
                {
                    pDoc->getIDocumentContentOperations().InsertString( *this, sText );
                }
            }
            pDoc->GetIDocumentUndoRedo().DoGroupUndo(bKeepGroupUndo);
            if (m_eInputLanguage != LANGUAGE_DONTKNOW)
            {
                sal_uInt16 nWhich = RES_CHRATR_LANGUAGE;
                sal_Int16 nScriptType = SvtLanguageOptions::GetI18NScriptTypeOfLanguage(m_eInputLanguage);
                switch(nScriptType)
                {
                    case  i18n::ScriptType::ASIAN:
                        nWhich = RES_CHRATR_CJK_LANGUAGE; break;
                    case  i18n::ScriptType::COMPLEX:
                        nWhich = RES_CHRATR_CTL_LANGUAGE; break;
                }
                // #i41974# Only set language attribute for CJK/CTL scripts.
                if (RES_CHRATR_LANGUAGE != nWhich && pTNd->GetLang( nSttCnt, nEndCnt-nSttCnt, nScriptType) != m_eInputLanguage)
                {
                    SvxLanguageItem aLangItem( m_eInputLanguage, nWhich );
                    rIdx = nSttCnt;
                    GetMark()->nContent = nEndCnt;
                    pDoc->getIDocumentContentOperations().InsertPoolItem(*this, aLangItem );
                }

            }
        }
    }
}

void SwExtTextInput::SetInputData( const CommandExtTextInputData& rData )
{
    SwTextNode* pTNd = GetPoint()->nNode.GetNode().GetTextNode();
    if( pTNd )
    {
        sal_Int32 nSttCnt = GetPoint()->nContent.GetIndex();
        sal_Int32 nEndCnt = GetMark()->nContent.GetIndex();
        if( nEndCnt < nSttCnt )
        {
            std::swap(nSttCnt, nEndCnt);
        }

        SwIndex aIdx( pTNd, nSttCnt );
        const OUString& rNewStr = rData.GetText();

        if( m_bIsOverwriteCursor && !m_sOverwriteText.isEmpty() )
        {
            sal_Int32 nReplace = nEndCnt - nSttCnt;
            const sal_Int32 nNewLen = rNewStr.getLength();
            if( nNewLen < nReplace )
            {
                // We have to insert some characters from the saved original text
                nReplace -= nNewLen;
                aIdx += nNewLen;
                pTNd->ReplaceText( aIdx, nReplace,
                            m_sOverwriteText.copy( nNewLen, nReplace ));
                aIdx = nSttCnt;
                nReplace = nNewLen;
            }
            else
            {
                const sal_Int32 nOWLen = m_sOverwriteText.getLength();
                if( nOWLen < nReplace )
                {
                    aIdx += nOWLen;
                    pTNd->EraseText( aIdx, nReplace-nOWLen );
                    aIdx = nSttCnt;
                    nReplace = nOWLen;
                }
                else
                {
                    nReplace = std::min(nOWLen, nNewLen);
                }
            }

            pTNd->ReplaceText( aIdx, nReplace, rNewStr );
            if( !HasMark() )
                SetMark();
            GetMark()->nContent = aIdx;
        }
        else
        {
            if( nSttCnt < nEndCnt )
            {
                pTNd->EraseText( aIdx, nEndCnt - nSttCnt );
            }

            pTNd->InsertText( rNewStr, aIdx,
                    SwInsertFlags::EMPTYEXPAND );
            if( !HasMark() )
                SetMark();
        }

        GetPoint()->nContent = nSttCnt;

        m_aAttrs.clear();
        if( rData.GetTextAttr() )
        {
            const ExtTextInputAttr *pAttrs = rData.GetTextAttr();
            m_aAttrs.insert( m_aAttrs.begin(), pAttrs, pAttrs + rData.GetText().getLength() );
        }
    }
}

void SwExtTextInput::SetOverwriteCursor( bool bFlag )
{
    m_bIsOverwriteCursor = bFlag;
    if (!m_bIsOverwriteCursor)
        return;

    const SwTextNode *const pTNd = GetPoint()->nNode.GetNode().GetTextNode();
    if (pTNd)
    {
        const sal_Int32 nSttCnt = GetPoint()->nContent.GetIndex();
        const sal_Int32 nEndCnt = GetMark()->nContent.GetIndex();
        m_sOverwriteText = pTNd->GetText().copy( std::min(nSttCnt, nEndCnt) );
        if( !m_sOverwriteText.isEmpty() )
        {
            const sal_Int32 nInPos = m_sOverwriteText.indexOf( CH_TXTATR_INWORD );
            const sal_Int32 nBrkPos = m_sOverwriteText.indexOf( CH_TXTATR_BREAKWORD );

            // Find the first attr found, if any.
            sal_Int32 nPos = std::min(nInPos, nBrkPos);
            if (nPos<0)
            {
                nPos = std::max(nInPos, nBrkPos);
            }
            if (nPos>=0)
            {
                m_sOverwriteText = m_sOverwriteText.copy( 0, nPos );
            }
        }
    }
}

// The Doc interfaces

SwExtTextInput* SwDoc::CreateExtTextInput( const SwPaM& rPam )
{
    SwExtTextInput* pNew = new SwExtTextInput( rPam, mpExtInputRing );
    if( !mpExtInputRing )
        mpExtInputRing = pNew;
    pNew->SetMark();
    return pNew;
}

void SwDoc::DeleteExtTextInput( SwExtTextInput* pDel )
{
    if( pDel == mpExtInputRing )
    {
        if( pDel->GetNext() != mpExtInputRing )
            mpExtInputRing = pDel->GetNext();
        else
            mpExtInputRing = nullptr;
    }
    delete pDel;
}

SwExtTextInput* SwDoc::GetExtTextInput( const SwNode& rNd,
                                        sal_Int32 nContentPos ) const
{
    SwExtTextInput* pRet = nullptr;
    if( mpExtInputRing )
    {
        sal_uLong nNdIdx = rNd.GetIndex();
        SwExtTextInput* pTmp = mpExtInputRing;
        do {
            sal_uLong nPt = pTmp->GetPoint()->nNode.GetIndex(),
                  nMk = pTmp->GetMark()->nNode.GetIndex();
            sal_Int32 nPtCnt = pTmp->GetPoint()->nContent.GetIndex();
            sal_Int32 nMkCnt = pTmp->GetMark()->nContent.GetIndex();

            if( nPt < nMk || ( nPt == nMk && nPtCnt < nMkCnt ))
            {
                sal_uLong nTmp = nMk; nMk = nPt; nPt = nTmp;
                sal_Int32 nTmp2 = nMkCnt; nMkCnt = nPtCnt; nPtCnt = nTmp2;
            }

            if( nMk <= nNdIdx && nNdIdx <= nPt &&
                ( nContentPos<0 ||
                    ( nMkCnt <= nContentPos && nContentPos <= nPtCnt )))
            {
                pRet = pTmp;
                break;
            }
            pTmp = pTmp->GetNext();
        } while ( pTmp!=mpExtInputRing );
    }
    return pRet;
}

SwExtTextInput* SwDoc::GetExtTextInput() const
{
    OSL_ENSURE( !mpExtInputRing || !mpExtInputRing->IsMultiSelection(),
            "more than one InputEngine available" );
    return mpExtInputRing;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */