Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sw/source/uibase/shells/langhelper.cxx
Line
Count
Source
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 <string.h>
21
22
#include <wrtsh.hxx>
23
#include <doc.hxx>
24
#include <docary.hxx>
25
#include <charfmt.hxx>
26
27
#include <sfx2/bindings.hxx>
28
#include <sfx2/request.hxx>
29
#include <sfx2/sfxdlg.hxx>
30
#include <sfx2/viewfrm.hxx>
31
#include <editeng/eeitem.hxx>
32
#include <editeng/editeng.hxx>
33
#include <editeng/editdata.hxx>
34
#include <editeng/outliner.hxx>
35
#include <editeng/editview.hxx>
36
#include <editeng/langitem.hxx>
37
#include <editeng/scripthintitem.hxx>
38
39
#include <svl/languageoptions.hxx>
40
#include <svtools/langtab.hxx>
41
#include <svl/slstitm.hxx>
42
#include <svl/stritem.hxx>
43
#include <svx/svxids.hrc>
44
#include <osl/diagnose.h>
45
46
#include <ndtxt.hxx>
47
#include <pam.hxx>
48
#include <view.hxx>
49
#include <viewopt.hxx>
50
51
#include <langhelper.hxx>
52
53
using namespace ::com::sun::star;
54
55
namespace SwLangHelper
56
{
57
58
    void GetLanguageStatus( OutlinerView* pOLV, SfxItemSet& rSet )
59
0
    {
60
0
        ESelection aSelection = pOLV->GetSelection();
61
0
        EditView& rEditView=pOLV->GetEditView();
62
0
        EditEngine& rEditEngine = rEditView.getEditEngine();
63
64
        // the value of used script types
65
0
        const SvtScriptType nScriptType =pOLV->GetSelectedScriptType();
66
0
        OUString aScriptTypesInUse( OUString::number( static_cast<int>(nScriptType) ) );//rEditEngine.GetScriptType(aSelection)
67
68
        // get keyboard language
69
0
        OUString aKeyboardLang;
70
0
        LanguageType nLang = rEditView.GetInputLanguage();
71
0
        if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_SYSTEM)
72
0
            aKeyboardLang = SvtLanguageTable::GetLanguageString( nLang );
73
74
        // get the language that is in use
75
0
        OUString aCurrentLang(u"*"_ustr);
76
0
        SfxItemSet aSet(pOLV->GetAttribs());
77
0
        nLang = SwLangHelper::GetCurrentLanguage( aSet,nScriptType );
78
0
        if (nLang != LANGUAGE_DONTKNOW)
79
0
            aCurrentLang = SvtLanguageTable::GetLanguageString( nLang );
80
81
        // build sequence for status value
82
0
        uno::Sequence<OUString> aSeq{ aCurrentLang,
83
0
                                        aScriptTypesInUse,
84
0
                                        aKeyboardLang,
85
0
                                        SwLangHelper::GetTextForLanguageGuessing(&rEditEngine, aSelection)
86
0
        };
87
88
        // set sequence as status value
89
0
        SfxStringListItem aItem( SID_LANGUAGE_STATUS );
90
0
        aItem.SetStringList( aSeq );
91
0
        rSet.Put( aItem );
92
0
    }
93
94
    bool SetLanguageStatus( const OutlinerView* pOLV, SfxRequest &rReq, SwView const &rView, SwWrtShell &rSh )
95
0
    {
96
0
        bool bRestoreSelection = false;
97
0
        ESelection   aSelection  = pOLV->GetSelection();
98
0
        EditView   & rEditView   = pOLV->GetEditView();
99
0
        SfxItemSet aEditAttr(rEditView.GetEmptyItemSet());
100
101
        // get the language
102
0
        OUString aNewLangText;
103
104
0
        const SfxStringItem* pItem = rReq.GetArg<SfxStringItem>(SID_LANGUAGE_STATUS);
105
0
        if (pItem)
106
0
            aNewLangText = pItem->GetValue();
107
108
        //!! Remember the view frame right now...
109
        //!! (call to GetView().GetViewFrame() will break if the
110
        //!! SwTextShell got destroyed meanwhile.)
111
0
        SfxViewFrame& rViewFrame = rView.GetViewFrame();
112
113
0
        if (aNewLangText == "*" )
114
0
        {
115
            // open the dialog "Tools/Options/Languages and Locales - General"
116
0
            SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
117
0
            ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateVclDialog( rView.GetFrameWeld(), SID_LANGUAGE_OPTIONS ));
118
0
            pDlg->Execute();
119
0
        }
120
0
        else
121
0
        {
122
            // setting the new language...
123
0
            if (!aNewLangText.isEmpty())
124
0
            {
125
0
                static constexpr OUString aSelectionLangPrefix(u"Current_"_ustr);
126
0
                static constexpr OUString aParagraphLangPrefix(u"Paragraph_"_ustr);
127
0
                static constexpr OUString aDocumentLangPrefix(u"Default_"_ustr);
128
129
0
                sal_Int32 nPos = 0;
130
0
                bool bForSelection = true;
131
0
                bool bForParagraph = false;
132
0
                if (-1 != (nPos = aNewLangText.indexOf( aSelectionLangPrefix )))
133
0
                {
134
                    // ... for the current selection
135
0
                    aNewLangText = aNewLangText.replaceAt(nPos, aSelectionLangPrefix.getLength(), u"");
136
0
                    bForSelection = true;
137
0
                }
138
0
                else if (-1 != (nPos = aNewLangText.indexOf( aParagraphLangPrefix )))
139
0
                {
140
                    // ... for the current paragraph language
141
0
                    aNewLangText = aNewLangText.replaceAt(nPos, aParagraphLangPrefix.getLength(), u"");
142
0
                    bForSelection = true;
143
0
                    bForParagraph = true;
144
0
                }
145
0
                else if (-1 != (nPos = aNewLangText.indexOf( aDocumentLangPrefix )))
146
0
                {
147
                    // ... as default document language
148
0
                    aNewLangText = aNewLangText.replaceAt(nPos, aDocumentLangPrefix.getLength(), u"");
149
0
                    bForSelection = false;
150
0
                }
151
152
0
                if (bForParagraph)
153
0
                {
154
0
                    bRestoreSelection = true;
155
0
                    SwLangHelper::SelectPara( rEditView, aSelection );
156
0
                    aSelection = pOLV->GetSelection();
157
0
                }
158
0
                if (!bForSelection) // document language to be changed...
159
0
                {
160
0
                    rSh.StartAction();
161
0
                    rSh.LockView( true );
162
0
                    rSh.Push();
163
164
                    // prepare to apply new language to all text in document
165
0
                    rSh.SelAll();
166
0
                    rSh.ExtendedSelectAll();
167
0
                }
168
169
0
                if (aNewLangText == "LANGUAGE_NONE")
170
0
                    SwLangHelper::SetLanguage_None( rSh, pOLV, aSelection, bForSelection, aEditAttr );
171
0
                else if (aNewLangText == "RESET_LANGUAGES")
172
0
                    SwLangHelper::ResetLanguages( rSh, pOLV );
173
0
                else
174
0
                    SwLangHelper::SetLanguage( rSh, pOLV, aSelection, aNewLangText, bForSelection, aEditAttr );
175
176
0
                if (!bForSelection)
177
0
                {
178
                    // need to release view and restore selection...
179
0
                    rSh.Pop(SwCursorShell::PopMode::DeleteCurrent);
180
0
                    rSh.LockView( false );
181
0
                    rSh.EndAction();
182
0
                }
183
0
            }
184
0
        }
185
186
        // invalidate slot to get the new language displayed
187
0
        rViewFrame.GetBindings().Invalidate( rReq.GetSlot() );
188
189
0
        rReq.Done();
190
0
        return bRestoreSelection;
191
0
    }
192
193
    void SetLanguage( SwWrtShell &rWrtSh, std::u16string_view rLangText, bool bIsForSelection, SfxItemSet &rCoreSet )
194
0
    {
195
0
        SetLanguage( rWrtSh, nullptr , ESelection(), rLangText, bIsForSelection, rCoreSet );
196
0
    }
197
198
    void SetLanguage( SwWrtShell &rWrtSh, OutlinerView const * pOLV, const ESelection& rSelection, std::u16string_view rLangText, bool bIsForSelection, SfxItemSet &rCoreSet )
199
0
    {
200
0
        const LanguageType nLang = SvtLanguageTable::GetLanguageType( rLangText );
201
0
        if (nLang == LANGUAGE_DONTKNOW)
202
0
            return;
203
204
0
        EditEngine* pEditEngine = pOLV ? &pOLV->GetEditView().getEditEngine() : nullptr;
205
0
        OSL_ENSURE( !pOLV || pEditEngine, "OutlinerView without EditEngine???" );
206
207
        //get ScriptType
208
0
        TypedWhichId<SvxLanguageItem> nLangWhichId(0);
209
0
        bool bIsSingleScriptType = true;
210
211
0
        auto nHintWhichId = pEditEngine ? EE_CHAR_SCRIPT_HINT : RES_CHRATR_SCRIPT_HINT;
212
0
        auto eHintType = i18nutil::ScriptHintType::Automatic;
213
214
0
        switch (SvtLanguageOptions::GetScriptTypeOfLanguage(nLang))
215
0
        {
216
0
            case SvtScriptType::LATIN:
217
0
                nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE : RES_CHRATR_LANGUAGE;
218
0
                eHintType = i18nutil::ScriptHintType::Latin;
219
0
                break;
220
0
            case SvtScriptType::ASIAN:
221
0
                nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE_CJK : RES_CHRATR_CJK_LANGUAGE;
222
0
                eHintType = i18nutil::ScriptHintType::Asian;
223
0
                break;
224
0
            case SvtScriptType::COMPLEX:
225
0
                nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE_CTL : RES_CHRATR_CTL_LANGUAGE;
226
0
                eHintType = i18nutil::ScriptHintType::Complex;
227
0
                break;
228
0
            default:
229
0
                bIsSingleScriptType = false;
230
0
                OSL_FAIL("unexpected case");
231
0
        }
232
233
0
        if (!bIsSingleScriptType)
234
0
            return;
235
236
        // change language for selection or paragraph
237
        // (for paragraph is handled by previously having set the selection to the
238
        // whole paragraph)
239
0
        if (bIsForSelection)
240
0
        {
241
            // apply language to current selection
242
0
            if (pEditEngine)
243
0
            {
244
0
                rCoreSet.Put( SvxLanguageItem( nLang, nLangWhichId ));
245
0
                rCoreSet.Put(SvxScriptHintItem(eHintType, nHintWhichId));
246
0
                pEditEngine->QuickSetAttribs(rCoreSet, rSelection);
247
0
            }
248
0
            else
249
0
            {
250
0
                rWrtSh.GetCurAttr( rCoreSet );
251
0
                rCoreSet.Put( SvxLanguageItem( nLang, nLangWhichId ));
252
0
                rCoreSet.Put(SvxScriptHintItem(eHintType, nHintWhichId));
253
0
                rWrtSh.SetAttrSet( rCoreSet );
254
0
            }
255
0
        }
256
0
        else // change language for all text
257
0
        {
258
            // set document default language
259
0
            switch (nLangWhichId)
260
0
            {
261
0
                 case EE_CHAR_LANGUAGE :      nLangWhichId = RES_CHRATR_LANGUAGE; break;
262
0
                 case EE_CHAR_LANGUAGE_CJK :  nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
263
0
                 case EE_CHAR_LANGUAGE_CTL :  nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
264
0
            }
265
            //Set the default document language
266
0
            rWrtSh.SetDefault( SvxLanguageItem( nLang, nLangWhichId ) );
267
0
            rWrtSh.GetDoc()->GetDocShell()->Broadcast(SfxHint(SfxHintId::LanguageChanged));
268
269
            // #i102191: hard set respective language attribute in text document
270
            // (for all text in the document - which should be selected by now...)
271
0
            rWrtSh.SetAttrItem( SvxLanguageItem( nLang, nLangWhichId ) );
272
0
        }
273
0
    }
274
275
    void SetLanguage_None( SwWrtShell &rWrtSh, bool bIsForSelection, SfxItemSet &rCoreSet )
276
0
    {
277
0
        SetLanguage_None( rWrtSh,nullptr,ESelection(),bIsForSelection,rCoreSet );
278
0
    }
279
280
    void SetLanguage_None( SwWrtShell &rWrtSh, OutlinerView const * pOLV, const ESelection& rSelection, bool bIsForSelection, SfxItemSet &rCoreSet )
281
0
    {
282
        // EditEngine IDs
283
0
        const sal_uInt16 aLangWhichId_EE[3] =
284
0
        {
285
0
            EE_CHAR_LANGUAGE,
286
0
            EE_CHAR_LANGUAGE_CJK,
287
0
            EE_CHAR_LANGUAGE_CTL
288
0
        };
289
290
        // Writer IDs
291
0
        const sal_uInt16 aLangWhichId_Writer[3] =
292
0
        {
293
0
            RES_CHRATR_LANGUAGE,
294
0
            RES_CHRATR_CJK_LANGUAGE,
295
0
            RES_CHRATR_CTL_LANGUAGE
296
0
        };
297
298
0
        if (bIsForSelection)
299
0
        {
300
            // change language for selection or paragraph
301
            // (for paragraph is handled by previously having set the selection to the
302
            // whole paragraph)
303
304
0
            EditEngine* pEditEngine = pOLV ? &pOLV->GetEditView().getEditEngine() : nullptr;
305
0
            OSL_ENSURE( !pOLV || pEditEngine, "OutlinerView without EditEngine???" );
306
0
            if (pEditEngine)
307
0
            {
308
0
                for (sal_uInt16 i : aLangWhichId_EE)
309
0
                    rCoreSet.Put( SvxLanguageItem( LANGUAGE_NONE, i ));
310
0
                pEditEngine->QuickSetAttribs(rCoreSet, rSelection);
311
0
            }
312
0
            else
313
0
            {
314
0
                rWrtSh.GetCurAttr( rCoreSet );
315
0
                for (sal_uInt16 i : aLangWhichId_Writer)
316
0
                    rCoreSet.Put( SvxLanguageItem( LANGUAGE_NONE, i ));
317
0
                rWrtSh.SetAttrSet( rCoreSet );
318
0
            }
319
0
        }
320
0
        else // change language for all text
321
0
        {
322
0
            o3tl::sorted_vector<sal_uInt16> aAttribs;
323
0
            for (sal_uInt16 i : aLangWhichId_Writer)
324
0
            {
325
0
                rWrtSh.SetDefault( SvxLanguageItem( LANGUAGE_NONE, i ) );
326
0
                aAttribs.insert( i );
327
0
            }
328
0
            rWrtSh.GetDoc()->GetDocShell()->Broadcast(SfxHint(SfxHintId::LanguageChanged));
329
330
            // set all language attributes to default
331
            // (for all text in the document - which should be selected by now...)
332
0
            rWrtSh.ResetAttr( aAttribs );
333
0
        }
334
0
    }
335
336
    void ResetLanguages( SwWrtShell &rWrtSh, OutlinerView const * pOLV )
337
0
    {
338
        // reset language for current selection.
339
        // The selection should already have been expanded to the whole paragraph or
340
        // to all text in the document if those are the ranges where to reset
341
        // the language attributes
342
343
0
        if (pOLV)
344
0
        {
345
0
            EditView &rEditView = pOLV->GetEditView();
346
0
            rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE );
347
0
            rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE_CJK );
348
0
            rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE_CTL );
349
350
            // ugly hack, as it seems that EditView/EditEngine does not update their spellchecking marks
351
            // when setting a new language attribute
352
0
            EditEngine& rEditEngine = rEditView.getEditEngine();
353
0
            EEControlBits nCntrl = rEditEngine.GetControlWord();
354
            // turn off
355
0
            rEditEngine.SetControlWord(nCntrl & ~EEControlBits::ONLINESPELLING);
356
            //turn back on
357
0
            rEditEngine.SetControlWord(nCntrl);
358
0
            rEditEngine.CompleteOnlineSpelling();
359
360
0
            rEditView.Invalidate();
361
0
        }
362
0
        else
363
0
        {
364
0
            rWrtSh.ResetAttr(
365
0
                { RES_CHRATR_LANGUAGE, RES_CHRATR_CJK_LANGUAGE, RES_CHRATR_CTL_LANGUAGE });
366
0
        }
367
0
    }
368
369
    /// @returns : the language for the selected text that is set for the
370
    ///     specified attribute (script type).
371
    ///     If there are more than one languages used LANGUAGE_DONTKNOW will be returned.
372
    /// @param nLangWhichId : one of
373
    ///     RES_CHRATR_LANGUAGE, RES_CHRATR_CJK_LANGUAGE, RES_CHRATR_CTL_LANGUAGE,
374
    LanguageType GetLanguage( SwWrtShell &rSh, TypedWhichId<SvxLanguageItem> nLangWhichId )
375
0
    {
376
0
        SfxItemSet aSet( rSh.GetAttrPool(), nLangWhichId, nLangWhichId );
377
0
        rSh.GetCurAttr( aSet );
378
379
0
        return GetLanguage(aSet,nLangWhichId);
380
0
    }
381
382
    LanguageType GetLanguage( SfxItemSet const & aSet, TypedWhichId<SvxLanguageItem> nLangWhichId )
383
0
    {
384
385
0
        LanguageType nLang = LANGUAGE_SYSTEM;
386
387
0
        const SvxLanguageItem *pItem = nullptr;
388
0
        SfxItemState nState = aSet.GetItemState( nLangWhichId, true, &pItem );
389
0
        if (nState > SfxItemState::DEFAULT && pItem)
390
0
        {
391
            // the item is set and can be used
392
0
            nLang = pItem->GetLanguage();
393
0
        }
394
0
        else if (nState == SfxItemState::DEFAULT)
395
0
        {
396
            // since the attribute is not set: retrieve the default value
397
0
            nLang = aSet.GetPool()->GetUserOrPoolDefaultItem( nLangWhichId ).GetLanguage();
398
0
        }
399
0
        else if (nState == SfxItemState::INVALID)
400
0
        {
401
            // there is more than one language...
402
0
            nLang = LANGUAGE_DONTKNOW;
403
0
        }
404
0
        OSL_ENSURE( nLang != LANGUAGE_SYSTEM, "failed to get the language?" );
405
406
0
        return nLang;
407
0
    }
408
409
    /// @returns: the language in use for the selected text.
410
    ///     'In use' means the language(s) matching the script type(s) of the
411
    ///     selected text. Or in other words, the language a spell checker would use.
412
    ///     If there is more than one language LANGUAGE_DONTKNOW will be returned.
413
    LanguageType GetCurrentLanguage( SwWrtShell &rSh )
414
0
    {
415
        //set language attribute to use according to the script type
416
0
        TypedWhichId<SvxLanguageItem> nLangWhichId(0);
417
0
        bool bIsSingleScriptType = true;
418
0
        switch (rSh.GetScriptType())
419
0
        {
420
0
             case SvtScriptType::LATIN :    nLangWhichId = RES_CHRATR_LANGUAGE; break;
421
0
             case SvtScriptType::ASIAN :    nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
422
0
             case SvtScriptType::COMPLEX :  nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
423
0
             default: bIsSingleScriptType = false; break;
424
0
        }
425
426
        // get language according to the script type(s) in use
427
0
        LanguageType nCurrentLang = LANGUAGE_SYSTEM;
428
0
        if (bIsSingleScriptType)
429
0
            nCurrentLang = GetLanguage( rSh, nLangWhichId );
430
0
        else
431
0
        {
432
            // check if all script types are set to LANGUAGE_NONE and return
433
            // that if this is the case. Otherwise, having multiple script types
434
            // in use always means there are several languages in use...
435
0
            const TypedWhichId<SvxLanguageItem> aScriptTypes[3] =
436
0
            {
437
0
                RES_CHRATR_LANGUAGE,
438
0
                RES_CHRATR_CJK_LANGUAGE,
439
0
                RES_CHRATR_CTL_LANGUAGE
440
0
            };
441
0
            nCurrentLang = LANGUAGE_NONE;
442
0
            for (const TypedWhichId<SvxLanguageItem>& aScriptType : aScriptTypes)
443
0
            {
444
0
                LanguageType nTmpLang = GetLanguage( rSh, aScriptType );
445
0
                if (nTmpLang != LANGUAGE_NONE)
446
0
                {
447
0
                    nCurrentLang = LANGUAGE_DONTKNOW;
448
0
                    break;
449
0
                }
450
0
            }
451
0
        }
452
0
        OSL_ENSURE( nCurrentLang != LANGUAGE_SYSTEM, "failed to get the language?" );
453
454
0
        return nCurrentLang;
455
0
    }
456
457
    /// @returns: the language in use for the selected text.
458
    ///     'In use' means the language(s) matching the script type(s) of the
459
    ///     selected text. Or in other words, the language a spell checker would use.
460
    ///     If there is more than one language LANGUAGE_DONTKNOW will be returned.
461
    LanguageType GetCurrentLanguage( SfxItemSet const & aSet, SvtScriptType nScriptType )
462
0
    {
463
        //set language attribute to use according to the script type
464
0
        TypedWhichId<SvxLanguageItem> nLangWhichId(0);
465
0
        bool bIsSingleScriptType = true;
466
0
        switch (nScriptType)
467
0
        {
468
0
             case SvtScriptType::LATIN :    nLangWhichId = EE_CHAR_LANGUAGE; break;
469
0
             case SvtScriptType::ASIAN :    nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
470
0
             case SvtScriptType::COMPLEX :  nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
471
0
             default: bIsSingleScriptType = false;
472
0
        }
473
474
        // get language according to the script type(s) in use
475
0
        LanguageType nCurrentLang = LANGUAGE_SYSTEM;
476
0
        if (bIsSingleScriptType)
477
0
            nCurrentLang = GetLanguage( aSet, nLangWhichId );
478
0
        else
479
0
        {
480
            // check if all script types are set to LANGUAGE_NONE and return
481
            // that if this is the case. Otherwise, having multiple script types
482
            // in use always means there are several languages in use...
483
0
            const TypedWhichId<SvxLanguageItem> aScriptTypes[3] =
484
0
            {
485
0
                EE_CHAR_LANGUAGE,
486
0
                EE_CHAR_LANGUAGE_CJK,
487
0
                EE_CHAR_LANGUAGE_CTL
488
0
            };
489
0
            nCurrentLang = LANGUAGE_NONE;
490
0
            for (const TypedWhichId<SvxLanguageItem>& aScriptType : aScriptTypes)
491
0
            {
492
0
                LanguageType nTmpLang = GetLanguage( aSet, aScriptType );
493
0
                if (nTmpLang != LANGUAGE_NONE)
494
0
                {
495
0
                    nCurrentLang = LANGUAGE_DONTKNOW;
496
0
                    break;
497
0
                }
498
0
            }
499
0
        }
500
0
        OSL_ENSURE( nCurrentLang != LANGUAGE_SYSTEM, "failed to get the language?" );
501
502
0
        return nCurrentLang;
503
0
    }
504
505
    OUString GetTextForLanguageGuessing( SwWrtShell const &rSh )
506
0
    {
507
        // string for guessing language
508
0
        OUString aText;
509
0
        SwPaM *pCursor = rSh.GetCursor();
510
0
        SwTextNode *pNode = pCursor->GetPointNode().GetTextNode();
511
0
        if (pNode)
512
0
        {
513
0
            aText = pNode->GetText();
514
0
            if (!aText.isEmpty())
515
0
            {
516
0
                sal_Int32 nEnd = pCursor->GetPoint()->GetContentIndex();
517
                // at most 100 chars to the left...
518
0
                const sal_Int32 nStt = nEnd > 100 ? nEnd - 100 : 0;
519
                // ... and 100 to the right of the cursor position
520
0
                nEnd = aText.getLength() - nEnd > 100 ? nEnd + 100 : aText.getLength();
521
0
                aText = aText.copy( nStt, nEnd - nStt );
522
0
            }
523
0
        }
524
0
        return aText;
525
0
    }
526
527
    OUString GetTextForLanguageGuessing(EditEngine const * rEditEngine, const ESelection& rDocSelection)
528
0
    {
529
        // string for guessing language
530
531
        // get the full text of the paragraph that the end of selection is in
532
0
        OUString aText = rEditEngine->GetText(rDocSelection.end.nIndex);
533
0
        if (!aText.isEmpty())
534
0
        {
535
0
            sal_Int32 nStt = 0;
536
0
            sal_Int32 nEnd = rDocSelection.end.nIndex;
537
            // at most 100 chars to the left...
538
0
            nStt = nEnd > 100 ? nEnd - 100 : 0;
539
            // ... and 100 to the right of the cursor position
540
0
            nEnd = aText.getLength() - nEnd > 100 ? nEnd + 100 : aText.getLength();
541
0
            aText = aText.copy( nStt, nEnd - nStt );
542
0
        }
543
544
0
        return aText;
545
0
    }
546
547
    void SelectPara( EditView &rEditView, const ESelection &rCurSel )
548
0
    {
549
0
        ESelection aParaSel(rCurSel.start.nPara, 0, rCurSel.start.nPara, EE_TEXTPOS_MAX);
550
0
        rEditView.SetSelection( aParaSel );
551
0
    }
552
553
    void SelectCurrentPara( SwWrtShell &rWrtSh )
554
0
    {
555
        // select current para
556
0
        if (!rWrtSh.IsSttPara())
557
0
            rWrtSh.MovePara( GoCurrPara, fnParaStart );
558
0
        if (!rWrtSh.HasMark())
559
0
            rWrtSh.SetMark();
560
0
        rWrtSh.SwapPam();
561
0
        if (!rWrtSh.IsEndPara())
562
0
            rWrtSh.MovePara( GoCurrPara, fnParaEnd );
563
0
    }
564
}
565
566
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */