LCOV - code coverage report
Current view: top level - libreoffice/sw/source/ui/shells - langhelper.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 1 264 0.4 %
Date: 2012-12-27 Functions: 2 18 11.1 %
Legend: Lines: hit not hit

          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 <string.h>
      22             : 
      23             : #include <vcl/window.hxx>
      24             : 
      25             : #include <wrtsh.hxx>
      26             : #include <doc.hxx>
      27             : #include <docary.hxx>
      28             : #include <charfmt.hxx>
      29             : 
      30             : #include <sfx2/bindings.hxx>
      31             : #include <sfx2/dispatch.hxx>
      32             : #include <sfx2/request.hxx>
      33             : #include <editeng/eeitem.hxx>
      34             : #include <editeng/editeng.hxx>
      35             : #include <editeng/editdata.hxx>
      36             : #include <editeng/outliner.hxx>
      37             : #include <editeng/editview.hxx>
      38             : #include <editeng/scripttypeitem.hxx>
      39             : #include <editeng/langitem.hxx>
      40             : 
      41             : #include <svl/languageoptions.hxx>
      42             : #include <svtools/langtab.hxx>
      43             : #include <svl/slstitm.hxx>
      44             : #include <svl/stritem.hxx>
      45             : 
      46             : #include <ndtxt.hxx>
      47             : #include <pam.hxx>
      48             : #include <view.hxx>
      49             : #include <viewopt.hxx>
      50             : 
      51             : #include "swabstdlg.hxx"
      52             : 
      53             : #include <vcl/msgbox.hxx>
      54             : 
      55             : #include <langhelper.hxx>
      56             : 
      57             : using namespace ::com::sun::star;
      58             : 
      59             : namespace SwLangHelper
      60             : {
      61             : 
      62           0 :     sal_uInt16 GetLanguageStatus( OutlinerView* pOLV, SfxItemSet& rSet )
      63             :     {
      64           0 :         ESelection aSelection = pOLV->GetSelection();
      65           0 :         EditView& rEditView=pOLV->GetEditView();
      66           0 :         EditEngine* pEditEngine=rEditView.GetEditEngine();
      67             : 
      68             :         // the value of used script types
      69           0 :         const sal_uInt16 nScriptType =pOLV->GetSelectedScriptType();
      70           0 :         String aScriptTypesInUse( String::CreateFromInt32( nScriptType ) );//pEditEngine->GetScriptType(aSelection)
      71             : 
      72           0 :         SvtLanguageTable aLangTable;
      73             : 
      74             :         // get keyboard language
      75           0 :         String aKeyboardLang;
      76           0 :         LanguageType nLang = LANGUAGE_DONTKNOW;
      77             : 
      78           0 :         Window* pWin = rEditView.GetWindow();
      79           0 :         if(pWin)
      80           0 :             nLang = pWin->GetInputLanguage();
      81           0 :         if (nLang != LANGUAGE_DONTKNOW && nLang != LANGUAGE_SYSTEM)
      82           0 :             aKeyboardLang = aLangTable.GetString( nLang );
      83             : 
      84             :         // get the language that is in use
      85           0 :         String aCurrentLang = rtl::OUString("*");
      86           0 :         SfxItemSet aSet(pOLV->GetAttribs());
      87           0 :         nLang = SwLangHelper::GetCurrentLanguage( aSet,nScriptType );
      88           0 :         if (nLang != LANGUAGE_DONTKNOW)
      89           0 :             aCurrentLang = aLangTable.GetString( nLang );
      90             : 
      91             :         // build sequence for status value
      92           0 :         uno::Sequence< ::rtl::OUString > aSeq( 4 );
      93           0 :         aSeq[0] = aCurrentLang;
      94           0 :         aSeq[1] = aScriptTypesInUse;
      95           0 :         aSeq[2] = aKeyboardLang;
      96           0 :         aSeq[3] = SwLangHelper::GetTextForLanguageGuessing( pEditEngine, aSelection );
      97             : 
      98             :         // set sequence as status value
      99           0 :         SfxStringListItem aItem( SID_LANGUAGE_STATUS );
     100           0 :         aItem.SetStringList( aSeq );
     101           0 :         rSet.Put( aItem, SID_LANGUAGE_STATUS );
     102           0 :         return 0;
     103             :     }
     104             : 
     105           0 :     bool SetLanguageStatus( OutlinerView* pOLV, SfxRequest &rReq, SwView &rView, SwWrtShell &rSh )
     106             :     {
     107           0 :         bool bRestoreSelection = false;
     108           0 :         SfxItemSet aEditAttr(pOLV->GetAttribs());
     109           0 :         ESelection   aSelection  = pOLV->GetSelection();
     110           0 :         EditView   & rEditView   = pOLV->GetEditView();
     111           0 :         EditEngine * pEditEngine = rEditView.GetEditEngine();
     112             : 
     113             :         // get the language
     114           0 :         String aNewLangTxt;
     115             : 
     116           0 :         SFX_REQUEST_ARG( rReq, pItem, SfxStringItem, SID_LANGUAGE_STATUS , sal_False );
     117           0 :         if (pItem)
     118           0 :             aNewLangTxt = pItem->GetValue();
     119             : 
     120             :         //!! Remember the view frame right now...
     121             :         //!! (call to GetView().GetViewFrame() will break if the
     122             :         //!! SwTextShell got destroyed meanwhile.)
     123           0 :         SfxViewFrame *pViewFrame = rView.GetViewFrame();
     124             : 
     125           0 :         if (aNewLangTxt.EqualsAscii( "*" ))
     126             :         {
     127             :             // open the dialog "Tools/Options/Language Settings - Language"
     128           0 :             SfxAbstractDialogFactory* pFact = SfxAbstractDialogFactory::Create();
     129           0 :             if (pFact)
     130             :             {
     131           0 :                 VclAbstractDialog* pDlg = pFact->CreateVclDialog( rView.GetWindow(), SID_LANGUAGE_OPTIONS );
     132           0 :                 pDlg->Execute();
     133           0 :                 delete pDlg;
     134             :             }
     135             :         }
     136             :         else
     137             :         {
     138             :             // setting the new language...
     139           0 :             if (aNewLangTxt.Len() > 0)
     140             :             {
     141           0 :                 const rtl::OUString aSelectionLangPrefix("Current_");
     142           0 :                 const rtl::OUString aParagraphLangPrefix("Paragraph_");
     143           0 :                 const rtl::OUString aDocumentLangPrefix("Default_");
     144           0 :                 const String aStrNone( rtl::OUString("LANGUAGE_NONE") );
     145           0 :                 const String aStrResetLangs( rtl::OUString("RESET_LANGUAGES") );
     146             : 
     147           0 :                 xub_StrLen nPos = 0;
     148           0 :                 bool bForSelection = true;
     149           0 :                 bool bForParagraph = false;
     150           0 :                 if (STRING_NOTFOUND != (nPos = aNewLangTxt.Search( aSelectionLangPrefix, 0 )))
     151             :                 {
     152             :                     // ... for the current selection
     153           0 :                     aNewLangTxt = aNewLangTxt.Erase( nPos, aSelectionLangPrefix.getLength() );
     154           0 :                     bForSelection = true;
     155             :                 }
     156           0 :                 else if (STRING_NOTFOUND != (nPos = aNewLangTxt.Search( aParagraphLangPrefix , 0 )))
     157             :                 {
     158             :                     // ... for the current paragraph language
     159           0 :                     aNewLangTxt = aNewLangTxt.Erase( nPos, aParagraphLangPrefix.getLength() );
     160           0 :                     bForSelection = true;
     161           0 :                     bForParagraph = true;
     162             :                 }
     163           0 :                 else if (STRING_NOTFOUND != (nPos = aNewLangTxt.Search( aDocumentLangPrefix , 0 )))
     164             :                 {
     165             :                     // ... as default document language
     166           0 :                     aNewLangTxt = aNewLangTxt.Erase( nPos, aDocumentLangPrefix.getLength() );
     167           0 :                     bForSelection = false;
     168             :                 }
     169             : 
     170           0 :                 if (bForParagraph)
     171             :                 {
     172           0 :                     bRestoreSelection = true;
     173           0 :                     SwLangHelper::SelectPara( rEditView, aSelection );
     174           0 :                     aSelection = pOLV->GetSelection();
     175             :                 }
     176           0 :                 if (!bForSelection) // document language to be changed...
     177             :                 {
     178           0 :                     rSh.StartAction();
     179           0 :                     rSh.LockView( sal_True );
     180           0 :                     rSh.Push();
     181             : 
     182             :                     // prepare to apply new language to all text in document
     183           0 :                     rSh.SelAll();
     184           0 :                     rSh.ExtendedSelectAll();
     185             :                 }
     186             : 
     187           0 :                 if (aNewLangTxt == aStrNone)
     188           0 :                     SwLangHelper::SetLanguage_None( rSh, pOLV, aSelection, bForSelection, aEditAttr );
     189           0 :                 else if (aNewLangTxt == aStrResetLangs)
     190           0 :                     SwLangHelper::ResetLanguages( rSh, pOLV, aSelection, bForSelection );
     191             :                 else
     192           0 :                     SwLangHelper::SetLanguage( rSh, pOLV, aSelection, aNewLangTxt, bForSelection, aEditAttr );
     193             : 
     194             :                 // ugly hack, as it seems that EditView/EditEngine does not update their spellchecking marks
     195             :                 // when setting a new language attribute
     196           0 :                 if (bForSelection)
     197             :                 {
     198           0 :                     const SwViewOption* pVOpt = rView.GetWrtShellPtr()->GetViewOptions();
     199           0 :                     sal_uLong nCntrl = pEditEngine->GetControlWord();
     200             :                     // turn off
     201           0 :                     nCntrl &= ~EE_CNTRL_ONLINESPELLING;
     202           0 :                     pEditEngine->SetControlWord(nCntrl);
     203             : 
     204             :                     //turn back on
     205           0 :                     if (pVOpt->IsOnlineSpell())
     206           0 :                         nCntrl |= EE_CNTRL_ONLINESPELLING;
     207             :                     else
     208           0 :                         nCntrl &= ~EE_CNTRL_ONLINESPELLING;
     209           0 :                     pEditEngine->SetControlWord(nCntrl);
     210             : 
     211           0 :                     pEditEngine->CompleteOnlineSpelling();
     212           0 :                     rEditView.Invalidate();
     213             :                 }
     214             : 
     215           0 :                 if (!bForSelection)
     216             :                 {
     217             :                     // need to release view and restore selection...
     218           0 :                     rSh.Pop( sal_False );
     219           0 :                     rSh.LockView( sal_False );
     220           0 :                     rSh.EndAction();
     221           0 :                 }
     222             :             }
     223             :         }
     224             : 
     225             :         // invalidate slot to get the new language displayed
     226           0 :         pViewFrame->GetBindings().Invalidate( rReq.GetSlot() );
     227             : 
     228           0 :         rReq.Done();
     229           0 :         return bRestoreSelection;
     230             :     }
     231             : 
     232             : 
     233           0 :     void SetLanguage( SwWrtShell &rWrtSh, const String &rLangText, bool bIsForSelection, SfxItemSet &rCoreSet )
     234             :     {
     235           0 :         SetLanguage( rWrtSh, 0 , ESelection(), rLangText, bIsForSelection, rCoreSet );
     236           0 :     }
     237             : 
     238           0 :     void SetLanguage( SwWrtShell &rWrtSh, OutlinerView* pOLV, ESelection aSelection, const String &rLangText, bool bIsForSelection, SfxItemSet &rCoreSet )
     239             :     {
     240           0 :         const LanguageType nLang = SvtLanguageTable().GetType( rLangText );
     241           0 :         if (nLang != LANGUAGE_DONTKNOW)
     242             :         {
     243           0 :             sal_uInt16 nScriptType = SvtLanguageOptions::GetScriptTypeOfLanguage( nLang );
     244             : 
     245           0 :             EditEngine* pEditEngine = pOLV ? pOLV->GetEditView().GetEditEngine() : NULL;
     246             :             OSL_ENSURE( !pOLV || pEditEngine, "OutlinerView without EditEngine???" );
     247             : 
     248             :             //get ScriptType
     249           0 :             sal_uInt16 nLangWhichId = 0;
     250           0 :             bool bIsSingleScriptType = true;
     251           0 :             switch (nScriptType)
     252             :             {
     253           0 :                 case SCRIPTTYPE_LATIN :    nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE : RES_CHRATR_LANGUAGE; break;
     254           0 :                 case SCRIPTTYPE_ASIAN :    nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE_CJK : RES_CHRATR_CJK_LANGUAGE; break;
     255           0 :                 case SCRIPTTYPE_COMPLEX :  nLangWhichId = pEditEngine ? EE_CHAR_LANGUAGE_CTL : RES_CHRATR_CTL_LANGUAGE; break;
     256             :                 default:
     257           0 :                     bIsSingleScriptType = false;
     258             :                     OSL_FAIL("unexpected case" );
     259             :             }
     260           0 :             if (bIsSingleScriptType)
     261             :             {
     262             :                 // change language for selection or paragraph
     263             :                 // (for paragraph is handled by previosuly having set the selection to the
     264             :                 // whole paragraph)
     265           0 :                 if (bIsForSelection)
     266             :                 {
     267             :                     // apply language to current selection
     268           0 :                     if (pEditEngine)
     269             :                     {
     270           0 :                         rCoreSet.Put( SvxLanguageItem( nLang, nLangWhichId ));
     271           0 :                         pEditEngine->QuickSetAttribs( rCoreSet, aSelection);
     272             :                     }
     273             :                     else
     274             :                     {
     275           0 :                         rWrtSh.GetCurAttr( rCoreSet );
     276           0 :                         rCoreSet.Put( SvxLanguageItem( nLang, nLangWhichId ));
     277           0 :                         rWrtSh.SetAttr( rCoreSet );
     278             :                     }
     279             :                 }
     280             :                 else // change language for all text
     281             :                 {
     282             :                     // set document default language
     283           0 :                     switch (nLangWhichId)
     284             :                     {
     285           0 :                          case EE_CHAR_LANGUAGE :      nLangWhichId = RES_CHRATR_LANGUAGE; break;
     286           0 :                          case EE_CHAR_LANGUAGE_CJK :  nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
     287           0 :                          case EE_CHAR_LANGUAGE_CTL :  nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
     288             :                     }
     289             :                     //Set the default document language
     290           0 :                     rWrtSh.SetDefault( SvxLanguageItem( nLang, nLangWhichId ) );
     291             : 
     292             :                     //Resolves: fdo#35282 Clear the language from all Text Styles, and
     293             :                     //fallback to default document language
     294           0 :                     const SwTxtFmtColls *pColls = rWrtSh.GetDoc()->GetTxtFmtColls();
     295           0 :                     for(sal_uInt16 i = 0, nCount = pColls->size(); i < nCount; ++i)
     296             :                     {
     297           0 :                         SwTxtFmtColl &rTxtColl = *(*pColls)[ i ];
     298           0 :                         rTxtColl.ResetFmtAttr(nLangWhichId);
     299             :                     }
     300             :                     //Resolves: fdo#35282 Clear the language from all Character Styles,
     301             :                     //and fallback to default document language
     302           0 :                     const SwCharFmts *pCharFmts = rWrtSh.GetDoc()->GetCharFmts();
     303           0 :                     for(sal_uInt16 i = 0, nCount = pCharFmts->size(); i < nCount; ++i)
     304             :                     {
     305           0 :                         SwCharFmt &rCharFmt = *(*pCharFmts)[ i ];
     306           0 :                         rCharFmt.ResetFmtAttr(nLangWhichId);
     307             :                     }
     308             : 
     309             :                     // #i102191: hard set respective language attribute in text document
     310             :                     // (for all text in the document - which should be selected by now...)
     311           0 :                     rWrtSh.SetAttr( SvxLanguageItem( nLang, nLangWhichId ) );
     312             :                 }
     313             :             }
     314             :         }
     315           0 :     }
     316             : 
     317           0 :     void SetLanguage_None( SwWrtShell &rWrtSh, bool bIsForSelection, SfxItemSet &rCoreSet )
     318             :     {
     319           0 :         SetLanguage_None( rWrtSh,0,ESelection(),bIsForSelection,rCoreSet );
     320           0 :     }
     321             : 
     322           0 :     void SetLanguage_None( SwWrtShell &rWrtSh, OutlinerView* pOLV, ESelection aSelection, bool bIsForSelection, SfxItemSet &rCoreSet )
     323             :     {
     324             :         // EditEngine IDs
     325             :         const sal_uInt16 aLangWhichId_EE[3] =
     326             :         {
     327             :             EE_CHAR_LANGUAGE,
     328             :             EE_CHAR_LANGUAGE_CJK,
     329             :             EE_CHAR_LANGUAGE_CTL
     330           0 :         };
     331             : 
     332             :         // Writewr IDs
     333             :         const sal_uInt16 aLangWhichId_Writer[3] =
     334             :         {
     335             :             RES_CHRATR_LANGUAGE,
     336             :             RES_CHRATR_CJK_LANGUAGE,
     337             :             RES_CHRATR_CTL_LANGUAGE
     338           0 :         };
     339             : 
     340           0 :         if (bIsForSelection)
     341             :         {
     342             :             // change language for selection or paragraph
     343             :             // (for paragraph is handled by previosuly having set the selection to the
     344             :             // whole paragraph)
     345             : 
     346           0 :             EditEngine* pEditEngine = pOLV ? pOLV->GetEditView().GetEditEngine() : NULL;
     347             :             OSL_ENSURE( !pOLV || pEditEngine, "OutlinerView without EditEngine???" );
     348           0 :             if (pEditEngine)
     349             :             {
     350           0 :                 for (sal_uInt16 i = 0; i < 3; ++i)
     351           0 :                     rCoreSet.Put( SvxLanguageItem( LANGUAGE_NONE, aLangWhichId_EE[i] ));
     352           0 :                 pEditEngine->QuickSetAttribs( rCoreSet, aSelection);
     353             :             }
     354             :             else
     355             :             {
     356           0 :                 rWrtSh.GetCurAttr( rCoreSet );
     357           0 :                 for (sal_uInt16 i = 0; i < 3; ++i)
     358           0 :                     rCoreSet.Put( SvxLanguageItem( LANGUAGE_NONE, aLangWhichId_Writer[i] ));
     359           0 :                 rWrtSh.SetAttr( rCoreSet );
     360             :             }
     361             :         }
     362             :         else // change language for all text
     363             :         {
     364           0 :             std::set<sal_uInt16> aAttribs;
     365           0 :             for (sal_uInt16 i = 0; i < 3; ++i)
     366             :             {
     367           0 :                 rWrtSh.SetDefault( SvxLanguageItem( LANGUAGE_NONE, aLangWhichId_Writer[i] ) );
     368           0 :                 aAttribs.insert( aLangWhichId_Writer[i] );
     369             :             }
     370             : 
     371             :             // set all language attributes to default
     372             :             // (for all text in the document - which should be selected by now...)
     373           0 :             rWrtSh.ResetAttr( aAttribs );
     374             :         }
     375           0 :     }
     376             : 
     377           0 :     void ResetLanguages( SwWrtShell &rWrtSh, bool bIsForSelection )
     378             :     {
     379           0 :         ResetLanguages( rWrtSh, 0 , ESelection(), bIsForSelection );
     380           0 :     }
     381             : 
     382           0 :     void ResetLanguages( SwWrtShell &rWrtSh, OutlinerView* pOLV, ESelection aSelection, bool bIsForSelection )
     383             :     {
     384             :         (void) bIsForSelection;
     385             :         (void) aSelection;
     386             : 
     387             :         // reset language for current selection.
     388             :         // The selection should already have been expanded to the whole paragraph or
     389             :         // to all text in the document if those are the ranges where to reset
     390             :         // the language attributes
     391             : 
     392           0 :         if (pOLV)
     393             :         {
     394           0 :             EditView &rEditView = pOLV->GetEditView();
     395           0 :             rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE );
     396           0 :             rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE_CJK );
     397           0 :             rEditView.RemoveAttribs( true, EE_CHAR_LANGUAGE_CTL );
     398             :         }
     399             :         else
     400             :         {
     401           0 :             std::set<sal_uInt16> aAttribs;
     402           0 :             aAttribs.insert( RES_CHRATR_LANGUAGE );
     403           0 :             aAttribs.insert( RES_CHRATR_CJK_LANGUAGE );
     404           0 :             aAttribs.insert( RES_CHRATR_CTL_LANGUAGE );
     405           0 :             rWrtSh.ResetAttr( aAttribs );
     406             :         }
     407           0 :     }
     408             : 
     409             : 
     410             :     /// @returns : the language for the selected text that is set for the
     411             :     ///     specified attribute (script type).
     412             :     ///     If there are more than one languages used LANGUAGE_DONTKNOW will be returned.
     413             :     /// @param nLangWhichId : one of
     414             :     ///     RES_CHRATR_LANGUAGE, RES_CHRATR_CJK_LANGUAGE, RES_CHRATR_CTL_LANGUAGE,
     415           0 :     LanguageType GetLanguage( SwWrtShell &rSh, sal_uInt16 nLangWhichId )
     416             :     {
     417           0 :         SfxItemSet aSet( rSh.GetAttrPool(), nLangWhichId, nLangWhichId );
     418           0 :         rSh.GetCurAttr( aSet );
     419             : 
     420           0 :         return GetLanguage(aSet,nLangWhichId);
     421             :     }
     422             : 
     423           0 :     LanguageType GetLanguage( SfxItemSet aSet, sal_uInt16 nLangWhichId )
     424             :     {
     425             : 
     426           0 :         LanguageType nLang = LANGUAGE_SYSTEM;
     427             : 
     428           0 :         const SfxPoolItem *pItem = 0;
     429           0 :         SfxItemState nState = aSet.GetItemState( nLangWhichId, sal_True, &pItem );
     430           0 :         if (nState > SFX_ITEM_DEFAULT && pItem)
     431             :         {
     432             :             // the item is set and can be used
     433           0 :             nLang = (dynamic_cast< const SvxLanguageItem* >(pItem))->GetLanguage();
     434             :         }
     435           0 :         else if (nState == SFX_ITEM_DEFAULT)
     436             :         {
     437             :             // since the attribute is not set: retrieve the default value
     438           0 :             nLang = (dynamic_cast< const SvxLanguageItem& >(aSet.GetPool()->GetDefaultItem( nLangWhichId ))).GetLanguage();
     439             :         }
     440           0 :         else if (nState == SFX_ITEM_DONTCARE)
     441             :         {
     442             :             // there is more than one language...
     443           0 :             nLang = LANGUAGE_DONTKNOW;
     444             :         }
     445             :         OSL_ENSURE( nLang != LANGUAGE_SYSTEM, "failed to get the language?" );
     446             : 
     447           0 :         return nLang;
     448             :     }
     449             : 
     450             :     /// @returns: the language in use for the selected text.
     451             :     ///     'In use' means the language(s) matching the script type(s) of the
     452             :     ///     selected text. Or in other words, the language a spell checker would use.
     453             :     ///     If there is more than one language LANGUAGE_DONTKNOW will be returned.
     454           0 :     LanguageType GetCurrentLanguage( SwWrtShell &rSh )
     455             :     {
     456             :         // get all script types used in current selection
     457           0 :         const sal_uInt16 nScriptType = rSh.GetScriptType();
     458             : 
     459             :         //set language attribute to use according to the script type
     460           0 :         sal_uInt16 nLangWhichId = 0;
     461           0 :         bool bIsSingleScriptType = true;
     462           0 :         switch (nScriptType)
     463             :         {
     464           0 :              case SCRIPTTYPE_LATIN :    nLangWhichId = RES_CHRATR_LANGUAGE; break;
     465           0 :              case SCRIPTTYPE_ASIAN :    nLangWhichId = RES_CHRATR_CJK_LANGUAGE; break;
     466           0 :              case SCRIPTTYPE_COMPLEX :  nLangWhichId = RES_CHRATR_CTL_LANGUAGE; break;
     467           0 :              default: bIsSingleScriptType = false; break;
     468             :         }
     469             : 
     470             :         // get language according to the script type(s) in use
     471           0 :         LanguageType nCurrentLang = LANGUAGE_SYSTEM;
     472           0 :         if (bIsSingleScriptType)
     473           0 :             nCurrentLang = GetLanguage( rSh, nLangWhichId );
     474             :         else
     475             :         {
     476             :             // check if all script types are set to LANGUAGE_NONE and return
     477             :             // that if this is the case. Otherwise, having multiple script types
     478             :             // in use always means there are several languages in use...
     479             :             const sal_uInt16 aScriptTypes[3] =
     480             :             {
     481             :                 RES_CHRATR_LANGUAGE,
     482             :                 RES_CHRATR_CJK_LANGUAGE,
     483             :                 RES_CHRATR_CTL_LANGUAGE
     484           0 :             };
     485           0 :             nCurrentLang = LANGUAGE_NONE;
     486           0 :             for (sal_uInt16 i = 0; i < 3; ++i)
     487             :             {
     488           0 :                 LanguageType nTmpLang = GetLanguage( rSh, aScriptTypes[i] );
     489           0 :                 if (nTmpLang != LANGUAGE_NONE)
     490             :                 {
     491           0 :                     nCurrentLang = LANGUAGE_DONTKNOW;
     492           0 :                     break;
     493             :                 }
     494             :             }
     495             :         }
     496             :         OSL_ENSURE( nCurrentLang != LANGUAGE_SYSTEM, "failed to get the language?" );
     497             : 
     498           0 :         return nCurrentLang;
     499             :     }
     500             : 
     501             :     /// @returns: the language in use for the selected text.
     502             :     ///     'In use' means the language(s) matching the script type(s) of the
     503             :     ///     selected text. Or in other words, the language a spell checker would use.
     504             :     ///     If there is more than one language LANGUAGE_DONTKNOW will be returned.
     505           0 :     LanguageType GetCurrentLanguage( SfxItemSet aSet, sal_uInt16 nScriptType )
     506             :     {
     507             :         //set language attribute to use according to the script type
     508           0 :         sal_uInt16 nLangWhichId = 0;
     509           0 :         bool bIsSingleScriptType = true;
     510           0 :         switch (nScriptType)
     511             :         {
     512           0 :              case SCRIPTTYPE_LATIN :    nLangWhichId = EE_CHAR_LANGUAGE; break;
     513           0 :              case SCRIPTTYPE_ASIAN :    nLangWhichId = EE_CHAR_LANGUAGE_CJK; break;
     514           0 :              case SCRIPTTYPE_COMPLEX :  nLangWhichId = EE_CHAR_LANGUAGE_CTL; break;
     515           0 :              default: bIsSingleScriptType = false;
     516             :         }
     517             : 
     518             :         // get language according to the script type(s) in use
     519           0 :         LanguageType nCurrentLang = LANGUAGE_SYSTEM;
     520           0 :         if (bIsSingleScriptType)
     521           0 :             nCurrentLang = GetLanguage( aSet, nLangWhichId );
     522             :         else
     523             :         {
     524             :             // check if all script types are set to LANGUAGE_NONE and return
     525             :             // that if this is the case. Otherwise, having multiple script types
     526             :             // in use always means there are several languages in use...
     527             :             const sal_uInt16 aScriptTypes[3] =
     528             :             {
     529             :                 EE_CHAR_LANGUAGE,
     530             :                 EE_CHAR_LANGUAGE_CJK,
     531             :                 EE_CHAR_LANGUAGE_CTL
     532           0 :             };
     533           0 :             nCurrentLang = LANGUAGE_NONE;
     534           0 :             for (sal_uInt16 i = 0; i < 3; ++i)
     535             :             {
     536           0 :                 LanguageType nTmpLang = GetLanguage( aSet, aScriptTypes[i] );
     537           0 :                 if (nTmpLang != LANGUAGE_NONE)
     538             :                 {
     539           0 :                     nCurrentLang = LANGUAGE_DONTKNOW;
     540           0 :                     break;
     541             :                 }
     542             :             }
     543             :         }
     544             :         OSL_ENSURE( nCurrentLang != LANGUAGE_SYSTEM, "failed to get the language?" );
     545             : 
     546           0 :         return nCurrentLang;
     547             :     }
     548             : 
     549           0 :     String GetTextForLanguageGuessing( SwWrtShell &rSh )
     550             :     {
     551             :         // string for guessing language
     552           0 :         String aText;
     553           0 :         SwPaM *pCrsr = rSh.GetCrsr();
     554           0 :         SwTxtNode *pNode = pCrsr->GetNode()->GetTxtNode();
     555           0 :         if (pNode)
     556             :         {
     557           0 :             aText = pNode->GetTxt();
     558           0 :             if (aText.Len() > 0)
     559             :             {
     560           0 :                 xub_StrLen nStt = 0;
     561           0 :                 xub_StrLen nEnd = pCrsr->GetPoint()->nContent.GetIndex();
     562             :                 // at most 100 chars to the left...
     563           0 :                 nStt = nEnd > 100 ? nEnd - 100 : 0;
     564             :                 // ... and 100 to the right of the cursor position
     565           0 :                 nEnd = aText.Len() - nEnd > 100 ? nEnd + 100 : aText.Len();
     566           0 :                 aText = aText.Copy( nStt, nEnd - nStt );
     567             :             }
     568             :         }
     569           0 :         return aText;
     570             :     }
     571             : 
     572           0 :     String GetTextForLanguageGuessing( EditEngine* rEditEngine, ESelection aDocSelection )
     573             :     {
     574             :         // string for guessing language
     575           0 :         String aText;
     576             : 
     577           0 :         aText = rEditEngine->GetText(aDocSelection);
     578           0 :         if (aText.Len() > 0)
     579             :         {
     580           0 :             xub_StrLen nStt = 0;
     581           0 :             xub_StrLen nEnd = aDocSelection.nEndPos;
     582             :             // at most 100 chars to the left...
     583           0 :             nStt = nEnd > 100 ? nEnd - 100 : 0;
     584             :             // ... and 100 to the right of the cursor position
     585           0 :             nEnd = aText.Len() - nEnd > 100 ? nEnd + 100 : aText.Len();
     586           0 :             aText = aText.Copy( nStt, nEnd - nStt );
     587             :         }
     588             : 
     589           0 :         return aText;
     590             :     }
     591             : 
     592             : 
     593           0 :     void SelectPara( EditView &rEditView, const ESelection &rCurSel )
     594             :     {
     595           0 :         ESelection aParaSel( rCurSel.nStartPara, 0, rCurSel.nStartPara, USHRT_MAX );
     596           0 :         rEditView.SetSelection( aParaSel );
     597           0 :     }
     598             : 
     599           0 :     void SelectCurrentPara( SwWrtShell &rWrtSh )
     600             :     {
     601             :         // select current para
     602           0 :         if (!rWrtSh.IsSttPara())
     603           0 :             rWrtSh.MovePara( fnParaCurr, fnParaStart );
     604           0 :         if (!rWrtSh.HasMark())
     605           0 :             rWrtSh.SetMark();
     606           0 :         rWrtSh.SwapPam();
     607           0 :         if (!rWrtSh.IsEndPara())
     608           0 :             rWrtSh.MovePara( fnParaCurr, fnParaEnd );
     609             :     #if OSL_DEBUG_LEVEL > 1
     610             :         String aSelTxt;
     611             :         rWrtSh.GetSelectedText( aSelTxt );
     612             :         (void) aSelTxt;
     613             :     #endif
     614           0 :     }
     615          30 : }
     616             : 
     617             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10