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